tpm_ftpm_tee.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Microsoft Corporation
  4. *
  5. * Implements a firmware TPM as described here:
  6. * https://www.microsoft.com/en-us/research/publication/ftpm-software-implementation-tpm-chip/
  7. *
  8. * A reference implementation is available here:
  9. * https://github.com/microsoft/ms-tpm-20-ref/tree/master/Samples/ARM32-FirmwareTPM/optee_ta/fTPM
  10. */
  11. #include <linux/acpi.h>
  12. #include <linux/of.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/tee_drv.h>
  15. #include <linux/tpm.h>
  16. #include <linux/uuid.h>
  17. #include "tpm.h"
  18. #include "tpm_ftpm_tee.h"
  19. /*
  20. * TA_FTPM_UUID: BC50D971-D4C9-42C4-82CB-343FB7F37896
  21. *
  22. * Randomly generated, and must correspond to the GUID on the TA side.
  23. * Defined here in the reference implementation:
  24. * https://github.com/microsoft/ms-tpm-20-ref/blob/master/Samples/ARM32-FirmwareTPM/optee_ta/fTPM/include/fTPM.h#L42
  25. */
  26. static const uuid_t ftpm_ta_uuid =
  27. UUID_INIT(0xBC50D971, 0xD4C9, 0x42C4,
  28. 0x82, 0xCB, 0x34, 0x3F, 0xB7, 0xF3, 0x78, 0x96);
  29. /**
  30. * ftpm_tee_tpm_op_recv() - retrieve fTPM response.
  31. * @chip: the tpm_chip description as specified in driver/char/tpm/tpm.h.
  32. * @buf: the buffer to store data.
  33. * @count: the number of bytes to read.
  34. *
  35. * Return:
  36. * In case of success the number of bytes received.
  37. * On failure, -errno.
  38. */
  39. static int ftpm_tee_tpm_op_recv(struct tpm_chip *chip, u8 *buf, size_t count)
  40. {
  41. struct ftpm_tee_private *pvt_data = dev_get_drvdata(chip->dev.parent);
  42. size_t len;
  43. len = pvt_data->resp_len;
  44. if (count < len) {
  45. dev_err(&chip->dev,
  46. "%s: Invalid size in recv: count=%zd, resp_len=%zd\n",
  47. __func__, count, len);
  48. return -EIO;
  49. }
  50. memcpy(buf, pvt_data->resp_buf, len);
  51. pvt_data->resp_len = 0;
  52. return len;
  53. }
  54. /**
  55. * ftpm_tee_tpm_op_send() - send TPM commands through the TEE shared memory.
  56. * @chip: the tpm_chip description as specified in driver/char/tpm/tpm.h
  57. * @buf: the buffer to send.
  58. * @len: the number of bytes to send.
  59. *
  60. * Return:
  61. * In case of success, returns 0.
  62. * On failure, -errno
  63. */
  64. static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t len)
  65. {
  66. struct ftpm_tee_private *pvt_data = dev_get_drvdata(chip->dev.parent);
  67. size_t resp_len;
  68. int rc;
  69. u8 *temp_buf;
  70. struct tpm_header *resp_header;
  71. struct tee_ioctl_invoke_arg transceive_args;
  72. struct tee_param command_params[4];
  73. struct tee_shm *shm = pvt_data->shm;
  74. if (len > MAX_COMMAND_SIZE) {
  75. dev_err(&chip->dev,
  76. "%s: len=%zd exceeds MAX_COMMAND_SIZE supported by fTPM TA\n",
  77. __func__, len);
  78. return -EIO;
  79. }
  80. memset(&transceive_args, 0, sizeof(transceive_args));
  81. memset(command_params, 0, sizeof(command_params));
  82. pvt_data->resp_len = 0;
  83. /* Invoke FTPM_OPTEE_TA_SUBMIT_COMMAND function of fTPM TA */
  84. transceive_args = (struct tee_ioctl_invoke_arg) {
  85. .func = FTPM_OPTEE_TA_SUBMIT_COMMAND,
  86. .session = pvt_data->session,
  87. .num_params = 4,
  88. };
  89. /* Fill FTPM_OPTEE_TA_SUBMIT_COMMAND parameters */
  90. command_params[0] = (struct tee_param) {
  91. .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT,
  92. .u.memref = {
  93. .shm = shm,
  94. .size = len,
  95. .shm_offs = 0,
  96. },
  97. };
  98. temp_buf = tee_shm_get_va(shm, 0);
  99. if (IS_ERR(temp_buf)) {
  100. dev_err(&chip->dev, "%s: tee_shm_get_va failed for transmit\n",
  101. __func__);
  102. return PTR_ERR(temp_buf);
  103. }
  104. memset(temp_buf, 0, (MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE));
  105. memcpy(temp_buf, buf, len);
  106. command_params[1] = (struct tee_param) {
  107. .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT,
  108. .u.memref = {
  109. .shm = shm,
  110. .size = MAX_RESPONSE_SIZE,
  111. .shm_offs = MAX_COMMAND_SIZE,
  112. },
  113. };
  114. rc = tee_client_invoke_func(pvt_data->ctx, &transceive_args,
  115. command_params);
  116. if ((rc < 0) || (transceive_args.ret != 0)) {
  117. dev_err(&chip->dev, "%s: SUBMIT_COMMAND invoke error: 0x%x\n",
  118. __func__, transceive_args.ret);
  119. return (rc < 0) ? rc : transceive_args.ret;
  120. }
  121. temp_buf = tee_shm_get_va(shm, command_params[1].u.memref.shm_offs);
  122. if (IS_ERR(temp_buf)) {
  123. dev_err(&chip->dev, "%s: tee_shm_get_va failed for receive\n",
  124. __func__);
  125. return PTR_ERR(temp_buf);
  126. }
  127. resp_header = (struct tpm_header *)temp_buf;
  128. resp_len = be32_to_cpu(resp_header->length);
  129. /* sanity check resp_len */
  130. if (resp_len < TPM_HEADER_SIZE) {
  131. dev_err(&chip->dev, "%s: tpm response header too small\n",
  132. __func__);
  133. return -EIO;
  134. }
  135. if (resp_len > MAX_RESPONSE_SIZE) {
  136. dev_err(&chip->dev,
  137. "%s: resp_len=%zd exceeds MAX_RESPONSE_SIZE\n",
  138. __func__, resp_len);
  139. return -EIO;
  140. }
  141. /* sanity checks look good, cache the response */
  142. memcpy(pvt_data->resp_buf, temp_buf, resp_len);
  143. pvt_data->resp_len = resp_len;
  144. return 0;
  145. }
  146. static void ftpm_tee_tpm_op_cancel(struct tpm_chip *chip)
  147. {
  148. /* not supported */
  149. }
  150. static u8 ftpm_tee_tpm_op_status(struct tpm_chip *chip)
  151. {
  152. return 0;
  153. }
  154. static bool ftpm_tee_tpm_req_canceled(struct tpm_chip *chip, u8 status)
  155. {
  156. return false;
  157. }
  158. static const struct tpm_class_ops ftpm_tee_tpm_ops = {
  159. .flags = TPM_OPS_AUTO_STARTUP,
  160. .recv = ftpm_tee_tpm_op_recv,
  161. .send = ftpm_tee_tpm_op_send,
  162. .cancel = ftpm_tee_tpm_op_cancel,
  163. .status = ftpm_tee_tpm_op_status,
  164. .req_complete_mask = 0,
  165. .req_complete_val = 0,
  166. .req_canceled = ftpm_tee_tpm_req_canceled,
  167. };
  168. /*
  169. * Check whether this driver supports the fTPM TA in the TEE instance
  170. * represented by the params (ver/data) to this function.
  171. */
  172. static int ftpm_tee_match(struct tee_ioctl_version_data *ver, const void *data)
  173. {
  174. /*
  175. * Currently this driver only support GP Complaint OPTEE based fTPM TA
  176. */
  177. if ((ver->impl_id == TEE_IMPL_ID_OPTEE) &&
  178. (ver->gen_caps & TEE_GEN_CAP_GP))
  179. return 1;
  180. else
  181. return 0;
  182. }
  183. /**
  184. * ftpm_tee_probe() - initialize the fTPM
  185. * @dev: the device description.
  186. *
  187. * Return:
  188. * On success, 0. On failure, -errno.
  189. */
  190. static int ftpm_tee_probe(struct device *dev)
  191. {
  192. int rc;
  193. struct tpm_chip *chip;
  194. struct ftpm_tee_private *pvt_data = NULL;
  195. struct tee_ioctl_open_session_arg sess_arg;
  196. pvt_data = devm_kzalloc(dev, sizeof(struct ftpm_tee_private),
  197. GFP_KERNEL);
  198. if (!pvt_data)
  199. return -ENOMEM;
  200. dev_set_drvdata(dev, pvt_data);
  201. /* Open context with TEE driver */
  202. pvt_data->ctx = tee_client_open_context(NULL, ftpm_tee_match, NULL,
  203. NULL);
  204. if (IS_ERR(pvt_data->ctx)) {
  205. if (PTR_ERR(pvt_data->ctx) == -ENOENT)
  206. return -EPROBE_DEFER;
  207. dev_err(dev, "%s: tee_client_open_context failed\n", __func__);
  208. return PTR_ERR(pvt_data->ctx);
  209. }
  210. /* Open a session with fTPM TA */
  211. memset(&sess_arg, 0, sizeof(sess_arg));
  212. export_uuid(sess_arg.uuid, &ftpm_ta_uuid);
  213. sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
  214. sess_arg.num_params = 0;
  215. rc = tee_client_open_session(pvt_data->ctx, &sess_arg, NULL);
  216. if ((rc < 0) || (sess_arg.ret != 0)) {
  217. dev_err(dev, "%s: tee_client_open_session failed, err=%x\n",
  218. __func__, sess_arg.ret);
  219. rc = -EINVAL;
  220. goto out_tee_session;
  221. }
  222. pvt_data->session = sess_arg.session;
  223. /* Allocate dynamic shared memory with fTPM TA */
  224. pvt_data->shm = tee_shm_alloc_kernel_buf(pvt_data->ctx,
  225. MAX_COMMAND_SIZE +
  226. MAX_RESPONSE_SIZE);
  227. if (IS_ERR(pvt_data->shm)) {
  228. dev_err(dev, "%s: tee_shm_alloc_kernel_buf failed\n", __func__);
  229. rc = -ENOMEM;
  230. goto out_shm_alloc;
  231. }
  232. /* Allocate new struct tpm_chip instance */
  233. chip = tpm_chip_alloc(dev, &ftpm_tee_tpm_ops);
  234. if (IS_ERR(chip)) {
  235. dev_err(dev, "%s: tpm_chip_alloc failed\n", __func__);
  236. rc = PTR_ERR(chip);
  237. goto out_chip_alloc;
  238. }
  239. pvt_data->chip = chip;
  240. pvt_data->chip->flags |= TPM_CHIP_FLAG_TPM2;
  241. /* Create a character device for the fTPM */
  242. rc = tpm_chip_register(pvt_data->chip);
  243. if (rc) {
  244. dev_err(dev, "%s: tpm_chip_register failed with rc=%d\n",
  245. __func__, rc);
  246. goto out_chip;
  247. }
  248. return 0;
  249. out_chip:
  250. put_device(&pvt_data->chip->dev);
  251. out_chip_alloc:
  252. tee_shm_free(pvt_data->shm);
  253. out_shm_alloc:
  254. tee_client_close_session(pvt_data->ctx, pvt_data->session);
  255. out_tee_session:
  256. tee_client_close_context(pvt_data->ctx);
  257. return rc;
  258. }
  259. static int ftpm_plat_tee_probe(struct platform_device *pdev)
  260. {
  261. struct device *dev = &pdev->dev;
  262. return ftpm_tee_probe(dev);
  263. }
  264. /**
  265. * ftpm_tee_remove() - remove the TPM device
  266. * @dev: the device description.
  267. *
  268. * Return:
  269. * 0 always.
  270. */
  271. static int ftpm_tee_remove(struct device *dev)
  272. {
  273. struct ftpm_tee_private *pvt_data = dev_get_drvdata(dev);
  274. /* Release the chip */
  275. tpm_chip_unregister(pvt_data->chip);
  276. /* frees chip */
  277. put_device(&pvt_data->chip->dev);
  278. /* Free the shared memory pool */
  279. tee_shm_free(pvt_data->shm);
  280. /* close the existing session with fTPM TA*/
  281. tee_client_close_session(pvt_data->ctx, pvt_data->session);
  282. /* close the context with TEE driver */
  283. tee_client_close_context(pvt_data->ctx);
  284. /* memory allocated with devm_kzalloc() is freed automatically */
  285. return 0;
  286. }
  287. static void ftpm_plat_tee_remove(struct platform_device *pdev)
  288. {
  289. struct device *dev = &pdev->dev;
  290. ftpm_tee_remove(dev);
  291. }
  292. /**
  293. * ftpm_plat_tee_shutdown() - shutdown the TPM device
  294. * @pdev: the platform_device description.
  295. */
  296. static void ftpm_plat_tee_shutdown(struct platform_device *pdev)
  297. {
  298. struct ftpm_tee_private *pvt_data = dev_get_drvdata(&pdev->dev);
  299. tee_shm_free(pvt_data->shm);
  300. tee_client_close_session(pvt_data->ctx, pvt_data->session);
  301. tee_client_close_context(pvt_data->ctx);
  302. }
  303. static const struct of_device_id of_ftpm_tee_ids[] = {
  304. { .compatible = "microsoft,ftpm" },
  305. { }
  306. };
  307. MODULE_DEVICE_TABLE(of, of_ftpm_tee_ids);
  308. static struct platform_driver ftpm_tee_plat_driver = {
  309. .driver = {
  310. .name = "ftpm-tee",
  311. .of_match_table = of_match_ptr(of_ftpm_tee_ids),
  312. },
  313. .shutdown = ftpm_plat_tee_shutdown,
  314. .probe = ftpm_plat_tee_probe,
  315. .remove_new = ftpm_plat_tee_remove,
  316. };
  317. /* UUID of the fTPM TA */
  318. static const struct tee_client_device_id optee_ftpm_id_table[] = {
  319. {UUID_INIT(0xbc50d971, 0xd4c9, 0x42c4,
  320. 0x82, 0xcb, 0x34, 0x3f, 0xb7, 0xf3, 0x78, 0x96)},
  321. {}
  322. };
  323. MODULE_DEVICE_TABLE(tee, optee_ftpm_id_table);
  324. static struct tee_client_driver ftpm_tee_driver = {
  325. .id_table = optee_ftpm_id_table,
  326. .driver = {
  327. .name = "optee-ftpm",
  328. .bus = &tee_bus_type,
  329. .probe = ftpm_tee_probe,
  330. .remove = ftpm_tee_remove,
  331. },
  332. };
  333. static int __init ftpm_mod_init(void)
  334. {
  335. int rc;
  336. rc = platform_driver_register(&ftpm_tee_plat_driver);
  337. if (rc)
  338. return rc;
  339. rc = driver_register(&ftpm_tee_driver.driver);
  340. if (rc) {
  341. platform_driver_unregister(&ftpm_tee_plat_driver);
  342. return rc;
  343. }
  344. return 0;
  345. }
  346. static void __exit ftpm_mod_exit(void)
  347. {
  348. platform_driver_unregister(&ftpm_tee_plat_driver);
  349. driver_unregister(&ftpm_tee_driver.driver);
  350. }
  351. module_init(ftpm_mod_init);
  352. module_exit(ftpm_mod_exit);
  353. MODULE_AUTHOR("Thirupathaiah Annapureddy <thiruan@microsoft.com>");
  354. MODULE_DESCRIPTION("TPM Driver for fTPM TA in TEE");
  355. MODULE_LICENSE("GPL v2");