tpm2-cmd.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. /*
  2. * Copyright (C) 2014 Intel Corporation
  3. *
  4. * Authors:
  5. * Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
  6. *
  7. * Maintained by: <tpmdd-devel@lists.sourceforge.net>
  8. *
  9. * This file contains TPM2 protocol implementations of the commands
  10. * used by the kernel internally.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; version 2
  15. * of the License.
  16. */
  17. #include "tpm.h"
  18. struct tpm2_startup_in {
  19. __be16 startup_type;
  20. } __packed;
  21. struct tpm2_self_test_in {
  22. u8 full_test;
  23. } __packed;
  24. struct tpm2_pcr_read_in {
  25. __be32 pcr_selects_cnt;
  26. __be16 hash_alg;
  27. u8 pcr_select_size;
  28. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  29. } __packed;
  30. struct tpm2_pcr_read_out {
  31. __be32 update_cnt;
  32. __be32 pcr_selects_cnt;
  33. __be16 hash_alg;
  34. u8 pcr_select_size;
  35. u8 pcr_select[TPM2_PCR_SELECT_MIN];
  36. __be32 digests_cnt;
  37. __be16 digest_size;
  38. u8 digest[TPM_DIGEST_SIZE];
  39. } __packed;
  40. struct tpm2_null_auth_area {
  41. __be32 handle;
  42. __be16 nonce_size;
  43. u8 attributes;
  44. __be16 auth_size;
  45. } __packed;
  46. struct tpm2_pcr_extend_in {
  47. __be32 pcr_idx;
  48. __be32 auth_area_size;
  49. struct tpm2_null_auth_area auth_area;
  50. __be32 digest_cnt;
  51. __be16 hash_alg;
  52. u8 digest[TPM_DIGEST_SIZE];
  53. } __packed;
  54. struct tpm2_get_tpm_pt_in {
  55. __be32 cap_id;
  56. __be32 property_id;
  57. __be32 property_cnt;
  58. } __packed;
  59. struct tpm2_get_tpm_pt_out {
  60. u8 more_data;
  61. __be32 subcap_id;
  62. __be32 property_cnt;
  63. __be32 property_id;
  64. __be32 value;
  65. } __packed;
  66. struct tpm2_get_random_in {
  67. __be16 size;
  68. } __packed;
  69. struct tpm2_get_random_out {
  70. __be16 size;
  71. u8 buffer[TPM_MAX_RNG_DATA];
  72. } __packed;
  73. union tpm2_cmd_params {
  74. struct tpm2_startup_in startup_in;
  75. struct tpm2_self_test_in selftest_in;
  76. struct tpm2_pcr_read_in pcrread_in;
  77. struct tpm2_pcr_read_out pcrread_out;
  78. struct tpm2_pcr_extend_in pcrextend_in;
  79. struct tpm2_get_tpm_pt_in get_tpm_pt_in;
  80. struct tpm2_get_tpm_pt_out get_tpm_pt_out;
  81. struct tpm2_get_random_in getrandom_in;
  82. struct tpm2_get_random_out getrandom_out;
  83. };
  84. struct tpm2_cmd {
  85. tpm_cmd_header header;
  86. union tpm2_cmd_params params;
  87. } __packed;
  88. /*
  89. * Array with one entry per ordinal defining the maximum amount
  90. * of time the chip could take to return the result. The values
  91. * of the SHORT, MEDIUM, and LONG durations are taken from the
  92. * PC Client Profile (PTP) specification.
  93. */
  94. static const u8 tpm2_ordinal_duration[TPM2_CC_LAST - TPM2_CC_FIRST + 1] = {
  95. TPM_UNDEFINED, /* 11F */
  96. TPM_UNDEFINED, /* 120 */
  97. TPM_LONG, /* 121 */
  98. TPM_UNDEFINED, /* 122 */
  99. TPM_UNDEFINED, /* 123 */
  100. TPM_UNDEFINED, /* 124 */
  101. TPM_UNDEFINED, /* 125 */
  102. TPM_UNDEFINED, /* 126 */
  103. TPM_UNDEFINED, /* 127 */
  104. TPM_UNDEFINED, /* 128 */
  105. TPM_LONG, /* 129 */
  106. TPM_UNDEFINED, /* 12a */
  107. TPM_UNDEFINED, /* 12b */
  108. TPM_UNDEFINED, /* 12c */
  109. TPM_UNDEFINED, /* 12d */
  110. TPM_UNDEFINED, /* 12e */
  111. TPM_UNDEFINED, /* 12f */
  112. TPM_UNDEFINED, /* 130 */
  113. TPM_UNDEFINED, /* 131 */
  114. TPM_UNDEFINED, /* 132 */
  115. TPM_UNDEFINED, /* 133 */
  116. TPM_UNDEFINED, /* 134 */
  117. TPM_UNDEFINED, /* 135 */
  118. TPM_UNDEFINED, /* 136 */
  119. TPM_UNDEFINED, /* 137 */
  120. TPM_UNDEFINED, /* 138 */
  121. TPM_UNDEFINED, /* 139 */
  122. TPM_UNDEFINED, /* 13a */
  123. TPM_UNDEFINED, /* 13b */
  124. TPM_UNDEFINED, /* 13c */
  125. TPM_UNDEFINED, /* 13d */
  126. TPM_MEDIUM, /* 13e */
  127. TPM_UNDEFINED, /* 13f */
  128. TPM_UNDEFINED, /* 140 */
  129. TPM_UNDEFINED, /* 141 */
  130. TPM_UNDEFINED, /* 142 */
  131. TPM_LONG, /* 143 */
  132. TPM_MEDIUM, /* 144 */
  133. TPM_UNDEFINED, /* 145 */
  134. TPM_UNDEFINED, /* 146 */
  135. TPM_UNDEFINED, /* 147 */
  136. TPM_UNDEFINED, /* 148 */
  137. TPM_UNDEFINED, /* 149 */
  138. TPM_UNDEFINED, /* 14a */
  139. TPM_UNDEFINED, /* 14b */
  140. TPM_UNDEFINED, /* 14c */
  141. TPM_UNDEFINED, /* 14d */
  142. TPM_LONG, /* 14e */
  143. TPM_UNDEFINED, /* 14f */
  144. TPM_UNDEFINED, /* 150 */
  145. TPM_UNDEFINED, /* 151 */
  146. TPM_UNDEFINED, /* 152 */
  147. TPM_UNDEFINED, /* 153 */
  148. TPM_UNDEFINED, /* 154 */
  149. TPM_UNDEFINED, /* 155 */
  150. TPM_UNDEFINED, /* 156 */
  151. TPM_UNDEFINED, /* 157 */
  152. TPM_UNDEFINED, /* 158 */
  153. TPM_UNDEFINED, /* 159 */
  154. TPM_UNDEFINED, /* 15a */
  155. TPM_UNDEFINED, /* 15b */
  156. TPM_MEDIUM, /* 15c */
  157. TPM_UNDEFINED, /* 15d */
  158. TPM_UNDEFINED, /* 15e */
  159. TPM_UNDEFINED, /* 15f */
  160. TPM_UNDEFINED, /* 160 */
  161. TPM_UNDEFINED, /* 161 */
  162. TPM_UNDEFINED, /* 162 */
  163. TPM_UNDEFINED, /* 163 */
  164. TPM_UNDEFINED, /* 164 */
  165. TPM_UNDEFINED, /* 165 */
  166. TPM_UNDEFINED, /* 166 */
  167. TPM_UNDEFINED, /* 167 */
  168. TPM_UNDEFINED, /* 168 */
  169. TPM_UNDEFINED, /* 169 */
  170. TPM_UNDEFINED, /* 16a */
  171. TPM_UNDEFINED, /* 16b */
  172. TPM_UNDEFINED, /* 16c */
  173. TPM_UNDEFINED, /* 16d */
  174. TPM_UNDEFINED, /* 16e */
  175. TPM_UNDEFINED, /* 16f */
  176. TPM_UNDEFINED, /* 170 */
  177. TPM_UNDEFINED, /* 171 */
  178. TPM_UNDEFINED, /* 172 */
  179. TPM_UNDEFINED, /* 173 */
  180. TPM_UNDEFINED, /* 174 */
  181. TPM_UNDEFINED, /* 175 */
  182. TPM_UNDEFINED, /* 176 */
  183. TPM_LONG, /* 177 */
  184. TPM_UNDEFINED, /* 178 */
  185. TPM_UNDEFINED, /* 179 */
  186. TPM_MEDIUM, /* 17a */
  187. TPM_LONG, /* 17b */
  188. TPM_UNDEFINED, /* 17c */
  189. TPM_UNDEFINED, /* 17d */
  190. TPM_UNDEFINED, /* 17e */
  191. TPM_UNDEFINED, /* 17f */
  192. TPM_UNDEFINED, /* 180 */
  193. TPM_UNDEFINED, /* 181 */
  194. TPM_MEDIUM, /* 182 */
  195. TPM_UNDEFINED, /* 183 */
  196. TPM_UNDEFINED, /* 184 */
  197. TPM_MEDIUM, /* 185 */
  198. TPM_MEDIUM, /* 186 */
  199. TPM_UNDEFINED, /* 187 */
  200. TPM_UNDEFINED, /* 188 */
  201. TPM_UNDEFINED, /* 189 */
  202. TPM_UNDEFINED, /* 18a */
  203. TPM_UNDEFINED, /* 18b */
  204. TPM_UNDEFINED, /* 18c */
  205. TPM_UNDEFINED, /* 18d */
  206. TPM_UNDEFINED, /* 18e */
  207. TPM_UNDEFINED /* 18f */
  208. };
  209. #define TPM2_PCR_READ_IN_SIZE \
  210. (sizeof(struct tpm_input_header) + \
  211. sizeof(struct tpm2_pcr_read_in))
  212. static const struct tpm_input_header tpm2_pcrread_header = {
  213. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  214. .length = cpu_to_be32(TPM2_PCR_READ_IN_SIZE),
  215. .ordinal = cpu_to_be32(TPM2_CC_PCR_READ)
  216. };
  217. /**
  218. * tpm2_pcr_read() - read a PCR value
  219. * @chip: TPM chip to use.
  220. * @pcr_idx: index of the PCR to read.
  221. * @ref_buf: buffer to store the resulting hash,
  222. *
  223. * 0 is returned when the operation is successful. If a negative number is
  224. * returned it remarks a POSIX error code. If a positive number is returned
  225. * it remarks a TPM error.
  226. */
  227. int tpm2_pcr_read(struct tpm_chip *chip, int pcr_idx, u8 *res_buf)
  228. {
  229. int rc;
  230. struct tpm2_cmd cmd;
  231. u8 *buf;
  232. if (pcr_idx >= TPM2_PLATFORM_PCR)
  233. return -EINVAL;
  234. cmd.header.in = tpm2_pcrread_header;
  235. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  236. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  237. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  238. memset(cmd.params.pcrread_in.pcr_select, 0,
  239. sizeof(cmd.params.pcrread_in.pcr_select));
  240. cmd.params.pcrread_in.pcr_select[pcr_idx >> 3] = 1 << (pcr_idx & 0x7);
  241. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  242. "attempting to read a pcr value");
  243. if (rc == 0) {
  244. buf = cmd.params.pcrread_out.digest;
  245. memcpy(res_buf, buf, TPM_DIGEST_SIZE);
  246. }
  247. return rc;
  248. }
  249. #define TPM2_GET_PCREXTEND_IN_SIZE \
  250. (sizeof(struct tpm_input_header) + \
  251. sizeof(struct tpm2_pcr_extend_in))
  252. static const struct tpm_input_header tpm2_pcrextend_header = {
  253. .tag = cpu_to_be16(TPM2_ST_SESSIONS),
  254. .length = cpu_to_be32(TPM2_GET_PCREXTEND_IN_SIZE),
  255. .ordinal = cpu_to_be32(TPM2_CC_PCR_EXTEND)
  256. };
  257. /**
  258. * tpm2_pcr_extend() - extend a PCR value
  259. * @chip: TPM chip to use.
  260. * @pcr_idx: index of the PCR.
  261. * @hash: hash value to use for the extend operation.
  262. *
  263. * 0 is returned when the operation is successful. If a negative number is
  264. * returned it remarks a POSIX error code. If a positive number is returned
  265. * it remarks a TPM error.
  266. */
  267. int tpm2_pcr_extend(struct tpm_chip *chip, int pcr_idx, const u8 *hash)
  268. {
  269. struct tpm2_cmd cmd;
  270. int rc;
  271. cmd.header.in = tpm2_pcrextend_header;
  272. cmd.params.pcrextend_in.pcr_idx = cpu_to_be32(pcr_idx);
  273. cmd.params.pcrextend_in.auth_area_size =
  274. cpu_to_be32(sizeof(struct tpm2_null_auth_area));
  275. cmd.params.pcrextend_in.auth_area.handle =
  276. cpu_to_be32(TPM2_RS_PW);
  277. cmd.params.pcrextend_in.auth_area.nonce_size = 0;
  278. cmd.params.pcrextend_in.auth_area.attributes = 0;
  279. cmd.params.pcrextend_in.auth_area.auth_size = 0;
  280. cmd.params.pcrextend_in.digest_cnt = cpu_to_be32(1);
  281. cmd.params.pcrextend_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  282. memcpy(cmd.params.pcrextend_in.digest, hash, TPM_DIGEST_SIZE);
  283. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  284. "attempting extend a PCR value");
  285. return rc;
  286. }
  287. #define TPM2_GETRANDOM_IN_SIZE \
  288. (sizeof(struct tpm_input_header) + \
  289. sizeof(struct tpm2_get_random_in))
  290. static const struct tpm_input_header tpm2_getrandom_header = {
  291. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  292. .length = cpu_to_be32(TPM2_GETRANDOM_IN_SIZE),
  293. .ordinal = cpu_to_be32(TPM2_CC_GET_RANDOM)
  294. };
  295. /**
  296. * tpm2_get_random() - get random bytes from the TPM RNG
  297. * @chip: TPM chip to use
  298. * @out: destination buffer for the random bytes
  299. * @max: the max number of bytes to write to @out
  300. *
  301. * 0 is returned when the operation is successful. If a negative number is
  302. * returned it remarks a POSIX error code. If a positive number is returned
  303. * it remarks a TPM error.
  304. */
  305. int tpm2_get_random(struct tpm_chip *chip, u8 *out, size_t max)
  306. {
  307. struct tpm2_cmd cmd;
  308. u32 recd;
  309. u32 num_bytes;
  310. int err;
  311. int total = 0;
  312. int retries = 5;
  313. u8 *dest = out;
  314. num_bytes = min_t(u32, max, sizeof(cmd.params.getrandom_out.buffer));
  315. if (!out || !num_bytes ||
  316. max > sizeof(cmd.params.getrandom_out.buffer))
  317. return -EINVAL;
  318. do {
  319. cmd.header.in = tpm2_getrandom_header;
  320. cmd.params.getrandom_in.size = cpu_to_be16(num_bytes);
  321. err = tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  322. "attempting get random");
  323. if (err)
  324. break;
  325. recd = min_t(u32, be16_to_cpu(cmd.params.getrandom_out.size),
  326. num_bytes);
  327. memcpy(dest, cmd.params.getrandom_out.buffer, recd);
  328. dest += recd;
  329. total += recd;
  330. num_bytes -= recd;
  331. } while (retries-- && total < max);
  332. return total ? total : -EIO;
  333. }
  334. #define TPM2_GET_TPM_PT_IN_SIZE \
  335. (sizeof(struct tpm_input_header) + \
  336. sizeof(struct tpm2_get_tpm_pt_in))
  337. static const struct tpm_input_header tpm2_get_tpm_pt_header = {
  338. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  339. .length = cpu_to_be32(TPM2_GET_TPM_PT_IN_SIZE),
  340. .ordinal = cpu_to_be32(TPM2_CC_GET_CAPABILITY)
  341. };
  342. /**
  343. * tpm2_get_tpm_pt() - get value of a TPM_CAP_TPM_PROPERTIES type property
  344. * @chip: TPM chip to use.
  345. * @property_id: property ID.
  346. * @value: output variable.
  347. * @desc: passed to tpm_transmit_cmd()
  348. *
  349. * 0 is returned when the operation is successful. If a negative number is
  350. * returned it remarks a POSIX error code. If a positive number is returned
  351. * it remarks a TPM error.
  352. */
  353. ssize_t tpm2_get_tpm_pt(struct tpm_chip *chip, u32 property_id, u32 *value,
  354. const char *desc)
  355. {
  356. struct tpm2_cmd cmd;
  357. int rc;
  358. cmd.header.in = tpm2_get_tpm_pt_header;
  359. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  360. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(property_id);
  361. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  362. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), desc);
  363. if (!rc)
  364. *value = cmd.params.get_tpm_pt_out.value;
  365. return rc;
  366. }
  367. #define TPM2_STARTUP_IN_SIZE \
  368. (sizeof(struct tpm_input_header) + \
  369. sizeof(struct tpm2_startup_in))
  370. static const struct tpm_input_header tpm2_startup_header = {
  371. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  372. .length = cpu_to_be32(TPM2_STARTUP_IN_SIZE),
  373. .ordinal = cpu_to_be32(TPM2_CC_STARTUP)
  374. };
  375. /**
  376. * tpm2_startup() - send startup command to the TPM chip
  377. * @chip: TPM chip to use.
  378. * @startup_type startup type. The value is either
  379. * TPM_SU_CLEAR or TPM_SU_STATE.
  380. *
  381. * 0 is returned when the operation is successful. If a negative number is
  382. * returned it remarks a POSIX error code. If a positive number is returned
  383. * it remarks a TPM error.
  384. */
  385. int tpm2_startup(struct tpm_chip *chip, u16 startup_type)
  386. {
  387. struct tpm2_cmd cmd;
  388. cmd.header.in = tpm2_startup_header;
  389. cmd.params.startup_in.startup_type = cpu_to_be16(startup_type);
  390. return tpm_transmit_cmd(chip, &cmd, sizeof(cmd),
  391. "attempting to start the TPM");
  392. }
  393. EXPORT_SYMBOL_GPL(tpm2_startup);
  394. #define TPM2_SHUTDOWN_IN_SIZE \
  395. (sizeof(struct tpm_input_header) + \
  396. sizeof(struct tpm2_startup_in))
  397. static const struct tpm_input_header tpm2_shutdown_header = {
  398. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  399. .length = cpu_to_be32(TPM2_SHUTDOWN_IN_SIZE),
  400. .ordinal = cpu_to_be32(TPM2_CC_SHUTDOWN)
  401. };
  402. /**
  403. * tpm2_shutdown() - send shutdown command to the TPM chip
  404. * @chip: TPM chip to use.
  405. * @shutdown_type shutdown type. The value is either
  406. * TPM_SU_CLEAR or TPM_SU_STATE.
  407. */
  408. void tpm2_shutdown(struct tpm_chip *chip, u16 shutdown_type)
  409. {
  410. struct tpm2_cmd cmd;
  411. int rc;
  412. cmd.header.in = tpm2_shutdown_header;
  413. cmd.params.startup_in.startup_type = cpu_to_be16(shutdown_type);
  414. rc = tpm_transmit_cmd(chip, &cmd, sizeof(cmd), "stopping the TPM");
  415. /* In places where shutdown command is sent there's no much we can do
  416. * except print the error code on a system failure.
  417. */
  418. if (rc < 0)
  419. dev_warn(chip->pdev, "transmit returned %d while stopping the TPM",
  420. rc);
  421. }
  422. EXPORT_SYMBOL_GPL(tpm2_shutdown);
  423. /*
  424. * tpm2_calc_ordinal_duration() - maximum duration for a command
  425. * @chip: TPM chip to use.
  426. * @ordinal: command code number.
  427. *
  428. * 0 is returned when the operation is successful. If a negative number is
  429. * returned it remarks a POSIX error code. If a positive number is returned
  430. * it remarks a TPM error.
  431. */
  432. unsigned long tpm2_calc_ordinal_duration(struct tpm_chip *chip, u32 ordinal)
  433. {
  434. int index = TPM_UNDEFINED;
  435. int duration = 0;
  436. if (ordinal >= TPM2_CC_FIRST && ordinal <= TPM2_CC_LAST)
  437. index = tpm2_ordinal_duration[ordinal - TPM2_CC_FIRST];
  438. if (index != TPM_UNDEFINED)
  439. duration = chip->vendor.duration[index];
  440. if (duration <= 0)
  441. duration = 2 * 60 * HZ;
  442. return duration;
  443. }
  444. EXPORT_SYMBOL_GPL(tpm2_calc_ordinal_duration);
  445. #define TPM2_SELF_TEST_IN_SIZE \
  446. (sizeof(struct tpm_input_header) + \
  447. sizeof(struct tpm2_self_test_in))
  448. static const struct tpm_input_header tpm2_selftest_header = {
  449. .tag = cpu_to_be16(TPM2_ST_NO_SESSIONS),
  450. .length = cpu_to_be32(TPM2_SELF_TEST_IN_SIZE),
  451. .ordinal = cpu_to_be32(TPM2_CC_SELF_TEST)
  452. };
  453. /**
  454. * tpm2_continue_selftest() - start a self test
  455. * @chip: TPM chip to use
  456. * @full: test all commands instead of testing only those that were not
  457. * previously tested.
  458. *
  459. * 0 is returned when the operation is successful. If a negative number is
  460. * returned it remarks a POSIX error code. If a positive number is returned
  461. * it remarks a TPM error.
  462. */
  463. static int tpm2_start_selftest(struct tpm_chip *chip, bool full)
  464. {
  465. int rc;
  466. struct tpm2_cmd cmd;
  467. cmd.header.in = tpm2_selftest_header;
  468. cmd.params.selftest_in.full_test = full;
  469. rc = tpm_transmit_cmd(chip, &cmd, TPM2_SELF_TEST_IN_SIZE,
  470. "continue selftest");
  471. /* At least some prototype chips seem to give RC_TESTING error
  472. * immediately. This is a workaround for that.
  473. */
  474. if (rc == TPM2_RC_TESTING) {
  475. dev_warn(chip->pdev, "Got RC_TESTING, ignoring\n");
  476. rc = 0;
  477. }
  478. return rc;
  479. }
  480. /**
  481. * tpm2_do_selftest() - run a full self test
  482. * @chip: TPM chip to use
  483. *
  484. * During the self test TPM2 commands return with the error code RC_TESTING.
  485. * Waiting is done by issuing PCR read until it executes successfully.
  486. *
  487. * 0 is returned when the operation is successful. If a negative number is
  488. * returned it remarks a POSIX error code. If a positive number is returned
  489. * it remarks a TPM error.
  490. */
  491. int tpm2_do_selftest(struct tpm_chip *chip)
  492. {
  493. int rc;
  494. unsigned int loops;
  495. unsigned int delay_msec = 100;
  496. unsigned long duration;
  497. struct tpm2_cmd cmd;
  498. int i;
  499. duration = tpm2_calc_ordinal_duration(chip, TPM2_CC_SELF_TEST);
  500. loops = jiffies_to_msecs(duration) / delay_msec;
  501. rc = tpm2_start_selftest(chip, true);
  502. if (rc)
  503. return rc;
  504. for (i = 0; i < loops; i++) {
  505. /* Attempt to read a PCR value */
  506. cmd.header.in = tpm2_pcrread_header;
  507. cmd.params.pcrread_in.pcr_selects_cnt = cpu_to_be32(1);
  508. cmd.params.pcrread_in.hash_alg = cpu_to_be16(TPM2_ALG_SHA1);
  509. cmd.params.pcrread_in.pcr_select_size = TPM2_PCR_SELECT_MIN;
  510. cmd.params.pcrread_in.pcr_select[0] = 0x01;
  511. cmd.params.pcrread_in.pcr_select[1] = 0x00;
  512. cmd.params.pcrread_in.pcr_select[2] = 0x00;
  513. rc = tpm_transmit_cmd(chip, (u8 *) &cmd, sizeof(cmd), NULL);
  514. if (rc < 0)
  515. break;
  516. rc = be32_to_cpu(cmd.header.out.return_code);
  517. if (rc != TPM2_RC_TESTING)
  518. break;
  519. msleep(delay_msec);
  520. }
  521. return rc;
  522. }
  523. EXPORT_SYMBOL_GPL(tpm2_do_selftest);
  524. /**
  525. * tpm2_gen_interrupt() - generate an interrupt
  526. * @chip: TPM chip to use
  527. *
  528. * 0 is returned when the operation is successful. If a negative number is
  529. * returned it remarks a POSIX error code. If a positive number is returned
  530. * it remarks a TPM error.
  531. */
  532. int tpm2_gen_interrupt(struct tpm_chip *chip)
  533. {
  534. u32 dummy;
  535. return tpm2_get_tpm_pt(chip, 0x100, &dummy,
  536. "attempting to generate an interrupt");
  537. }
  538. EXPORT_SYMBOL_GPL(tpm2_gen_interrupt);
  539. /**
  540. * tpm2_probe() - probe TPM 2.0
  541. * @chip: TPM chip to use
  542. *
  543. * Send idempotent TPM 2.0 command and see whether TPM 2.0 chip replied based on
  544. * the reply tag.
  545. */
  546. int tpm2_probe(struct tpm_chip *chip)
  547. {
  548. struct tpm2_cmd cmd;
  549. int rc;
  550. cmd.header.in = tpm2_get_tpm_pt_header;
  551. cmd.params.get_tpm_pt_in.cap_id = cpu_to_be32(TPM2_CAP_TPM_PROPERTIES);
  552. cmd.params.get_tpm_pt_in.property_id = cpu_to_be32(0x100);
  553. cmd.params.get_tpm_pt_in.property_cnt = cpu_to_be32(1);
  554. rc = tpm_transmit(chip, (const char *) &cmd, sizeof(cmd));
  555. if (rc < 0)
  556. return rc;
  557. else if (rc < TPM_HEADER_SIZE)
  558. return -EFAULT;
  559. if (be16_to_cpu(cmd.header.out.tag) == TPM2_ST_NO_SESSIONS)
  560. chip->flags |= TPM_CHIP_FLAG_TPM2;
  561. return 0;
  562. }
  563. EXPORT_SYMBOL_GPL(tpm2_probe);