tpm_ftpm_tee.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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_send() - send TPM commands through the TEE shared memory
  31. * and retrieve the response.
  32. * @chip: the tpm_chip description as specified in driver/char/tpm/tpm.h
  33. * @buf: the buffer to send and to store the response.
  34. * @bufsiz: the size of the buffer.
  35. * @cmd_len: the number of bytes to send.
  36. *
  37. * Return:
  38. * In case of success, returns the number of bytes received.
  39. * On failure, -errno
  40. */
  41. static int ftpm_tee_tpm_op_send(struct tpm_chip *chip, u8 *buf, size_t bufsiz,
  42. size_t cmd_len)
  43. {
  44. struct ftpm_tee_private *pvt_data = dev_get_drvdata(chip->dev.parent);
  45. size_t resp_len;
  46. int rc;
  47. u8 *temp_buf;
  48. struct tpm_header *resp_header;
  49. struct tee_ioctl_invoke_arg transceive_args;
  50. struct tee_param command_params[4];
  51. struct tee_shm *shm = pvt_data->shm;
  52. if (cmd_len > MAX_COMMAND_SIZE) {
  53. dev_err(&chip->dev,
  54. "%s: len=%zd exceeds MAX_COMMAND_SIZE supported by fTPM TA\n",
  55. __func__, cmd_len);
  56. return -EIO;
  57. }
  58. memset(&transceive_args, 0, sizeof(transceive_args));
  59. memset(command_params, 0, sizeof(command_params));
  60. /* Invoke FTPM_OPTEE_TA_SUBMIT_COMMAND function of fTPM TA */
  61. transceive_args = (struct tee_ioctl_invoke_arg) {
  62. .func = FTPM_OPTEE_TA_SUBMIT_COMMAND,
  63. .session = pvt_data->session,
  64. .num_params = 4,
  65. };
  66. /* Fill FTPM_OPTEE_TA_SUBMIT_COMMAND parameters */
  67. command_params[0] = (struct tee_param) {
  68. .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT,
  69. .u.memref = {
  70. .shm = shm,
  71. .size = cmd_len,
  72. .shm_offs = 0,
  73. },
  74. };
  75. temp_buf = tee_shm_get_va(shm, 0);
  76. if (IS_ERR(temp_buf)) {
  77. dev_err(&chip->dev, "%s: tee_shm_get_va failed for transmit\n",
  78. __func__);
  79. return PTR_ERR(temp_buf);
  80. }
  81. memset(temp_buf, 0, (MAX_COMMAND_SIZE + MAX_RESPONSE_SIZE));
  82. memcpy(temp_buf, buf, cmd_len);
  83. command_params[1] = (struct tee_param) {
  84. .attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT,
  85. .u.memref = {
  86. .shm = shm,
  87. .size = MAX_RESPONSE_SIZE,
  88. .shm_offs = MAX_COMMAND_SIZE,
  89. },
  90. };
  91. rc = tee_client_invoke_func(pvt_data->ctx, &transceive_args,
  92. command_params);
  93. if ((rc < 0) || (transceive_args.ret != 0)) {
  94. dev_err(&chip->dev, "%s: SUBMIT_COMMAND invoke error: 0x%x\n",
  95. __func__, transceive_args.ret);
  96. return (rc < 0) ? rc : transceive_args.ret;
  97. }
  98. temp_buf = tee_shm_get_va(shm, command_params[1].u.memref.shm_offs);
  99. if (IS_ERR(temp_buf)) {
  100. dev_err(&chip->dev, "%s: tee_shm_get_va failed for receive\n",
  101. __func__);
  102. return PTR_ERR(temp_buf);
  103. }
  104. resp_header = (struct tpm_header *)temp_buf;
  105. resp_len = be32_to_cpu(resp_header->length);
  106. /* sanity check resp_len */
  107. if (resp_len < TPM_HEADER_SIZE) {
  108. dev_err(&chip->dev, "%s: tpm response header too small\n",
  109. __func__);
  110. return -EIO;
  111. }
  112. if (resp_len > MAX_RESPONSE_SIZE) {
  113. dev_err(&chip->dev,
  114. "%s: resp_len=%zd exceeds MAX_RESPONSE_SIZE\n",
  115. __func__, resp_len);
  116. return -EIO;
  117. }
  118. if (resp_len > bufsiz) {
  119. dev_err(&chip->dev,
  120. "%s: resp_len=%zd exceeds bufsiz=%zd\n",
  121. __func__, resp_len, bufsiz);
  122. return -EIO;
  123. }
  124. memcpy(buf, temp_buf, resp_len);
  125. return resp_len;
  126. }
  127. static const struct tpm_class_ops ftpm_tee_tpm_ops = {
  128. .flags = TPM_OPS_AUTO_STARTUP,
  129. .send = ftpm_tee_tpm_op_send,
  130. };
  131. /*
  132. * Check whether this driver supports the fTPM TA in the TEE instance
  133. * represented by the params (ver/data) to this function.
  134. */
  135. static int ftpm_tee_match(struct tee_ioctl_version_data *ver, const void *data)
  136. {
  137. /*
  138. * Currently this driver only support GP Complaint OPTEE based fTPM TA
  139. */
  140. if ((ver->impl_id == TEE_IMPL_ID_OPTEE) &&
  141. (ver->gen_caps & TEE_GEN_CAP_GP))
  142. return 1;
  143. else
  144. return 0;
  145. }
  146. /**
  147. * ftpm_tee_probe_generic() - initialize the fTPM
  148. * @dev: the device description.
  149. *
  150. * Return:
  151. * On success, 0. On failure, -errno.
  152. */
  153. static int ftpm_tee_probe_generic(struct device *dev)
  154. {
  155. int rc;
  156. struct tpm_chip *chip;
  157. struct ftpm_tee_private *pvt_data = NULL;
  158. struct tee_ioctl_open_session_arg sess_arg;
  159. pvt_data = devm_kzalloc(dev, sizeof(struct ftpm_tee_private),
  160. GFP_KERNEL);
  161. if (!pvt_data)
  162. return -ENOMEM;
  163. dev_set_drvdata(dev, pvt_data);
  164. /* Open context with TEE driver */
  165. pvt_data->ctx = tee_client_open_context(NULL, ftpm_tee_match, NULL,
  166. NULL);
  167. if (IS_ERR(pvt_data->ctx)) {
  168. if (PTR_ERR(pvt_data->ctx) == -ENOENT)
  169. return -EPROBE_DEFER;
  170. dev_err(dev, "%s: tee_client_open_context failed\n", __func__);
  171. return PTR_ERR(pvt_data->ctx);
  172. }
  173. /* Open a session with fTPM TA */
  174. memset(&sess_arg, 0, sizeof(sess_arg));
  175. export_uuid(sess_arg.uuid, &ftpm_ta_uuid);
  176. sess_arg.clnt_login = TEE_IOCTL_LOGIN_PUBLIC;
  177. sess_arg.num_params = 0;
  178. rc = tee_client_open_session(pvt_data->ctx, &sess_arg, NULL);
  179. if ((rc < 0) || (sess_arg.ret != 0)) {
  180. dev_err(dev, "%s: tee_client_open_session failed, err=%x\n",
  181. __func__, sess_arg.ret);
  182. rc = -EINVAL;
  183. goto out_tee_session;
  184. }
  185. pvt_data->session = sess_arg.session;
  186. /* Allocate dynamic shared memory with fTPM TA */
  187. pvt_data->shm = tee_shm_alloc_kernel_buf(pvt_data->ctx,
  188. MAX_COMMAND_SIZE +
  189. MAX_RESPONSE_SIZE);
  190. if (IS_ERR(pvt_data->shm)) {
  191. dev_err(dev, "%s: tee_shm_alloc_kernel_buf failed\n", __func__);
  192. rc = -ENOMEM;
  193. goto out_shm_alloc;
  194. }
  195. /* Allocate new struct tpm_chip instance */
  196. chip = tpm_chip_alloc(dev, &ftpm_tee_tpm_ops);
  197. if (IS_ERR(chip)) {
  198. dev_err(dev, "%s: tpm_chip_alloc failed\n", __func__);
  199. rc = PTR_ERR(chip);
  200. goto out_chip_alloc;
  201. }
  202. pvt_data->chip = chip;
  203. pvt_data->chip->flags |= TPM_CHIP_FLAG_TPM2 | TPM_CHIP_FLAG_SYNC;
  204. /* Create a character device for the fTPM */
  205. rc = tpm_chip_register(pvt_data->chip);
  206. if (rc) {
  207. dev_err(dev, "%s: tpm_chip_register failed with rc=%d\n",
  208. __func__, rc);
  209. goto out_chip;
  210. }
  211. return 0;
  212. out_chip:
  213. put_device(&pvt_data->chip->dev);
  214. out_chip_alloc:
  215. tee_shm_free(pvt_data->shm);
  216. out_shm_alloc:
  217. tee_client_close_session(pvt_data->ctx, pvt_data->session);
  218. out_tee_session:
  219. tee_client_close_context(pvt_data->ctx);
  220. return rc;
  221. }
  222. static int ftpm_tee_probe(struct tee_client_device *tcdev)
  223. {
  224. struct device *dev = &tcdev->dev;
  225. return ftpm_tee_probe_generic(dev);
  226. }
  227. static int ftpm_plat_tee_probe(struct platform_device *pdev)
  228. {
  229. struct device *dev = &pdev->dev;
  230. return ftpm_tee_probe_generic(dev);
  231. }
  232. /**
  233. * ftpm_tee_remove_generic() - remove the TPM device
  234. * @dev: the device description.
  235. *
  236. * Return:
  237. * 0 always.
  238. */
  239. static void ftpm_tee_remove_generic(struct device *dev)
  240. {
  241. struct ftpm_tee_private *pvt_data = dev_get_drvdata(dev);
  242. /* Release the chip */
  243. tpm_chip_unregister(pvt_data->chip);
  244. /* frees chip */
  245. put_device(&pvt_data->chip->dev);
  246. /* Free the shared memory pool */
  247. tee_shm_free(pvt_data->shm);
  248. /* close the existing session with fTPM TA*/
  249. tee_client_close_session(pvt_data->ctx, pvt_data->session);
  250. /* close the context with TEE driver */
  251. tee_client_close_context(pvt_data->ctx);
  252. /* memory allocated with devm_kzalloc() is freed automatically */
  253. }
  254. static void ftpm_tee_remove(struct tee_client_device *tcdev)
  255. {
  256. struct device *dev = &tcdev->dev;
  257. ftpm_tee_remove_generic(dev);
  258. }
  259. static void ftpm_plat_tee_remove(struct platform_device *pdev)
  260. {
  261. struct device *dev = &pdev->dev;
  262. ftpm_tee_remove_generic(dev);
  263. }
  264. /**
  265. * ftpm_plat_tee_shutdown() - shutdown the TPM device
  266. * @pdev: the platform_device description.
  267. */
  268. static void ftpm_plat_tee_shutdown(struct platform_device *pdev)
  269. {
  270. struct ftpm_tee_private *pvt_data = dev_get_drvdata(&pdev->dev);
  271. tee_shm_free(pvt_data->shm);
  272. tee_client_close_session(pvt_data->ctx, pvt_data->session);
  273. tee_client_close_context(pvt_data->ctx);
  274. }
  275. static const struct of_device_id of_ftpm_tee_ids[] = {
  276. { .compatible = "microsoft,ftpm" },
  277. { }
  278. };
  279. MODULE_DEVICE_TABLE(of, of_ftpm_tee_ids);
  280. static struct platform_driver ftpm_tee_plat_driver = {
  281. .driver = {
  282. .name = "ftpm-tee",
  283. .of_match_table = of_ftpm_tee_ids,
  284. },
  285. .shutdown = ftpm_plat_tee_shutdown,
  286. .probe = ftpm_plat_tee_probe,
  287. .remove = ftpm_plat_tee_remove,
  288. };
  289. /* UUID of the fTPM TA */
  290. static const struct tee_client_device_id optee_ftpm_id_table[] = {
  291. {UUID_INIT(0xbc50d971, 0xd4c9, 0x42c4,
  292. 0x82, 0xcb, 0x34, 0x3f, 0xb7, 0xf3, 0x78, 0x96)},
  293. {}
  294. };
  295. MODULE_DEVICE_TABLE(tee, optee_ftpm_id_table);
  296. static struct tee_client_driver ftpm_tee_driver = {
  297. .probe = ftpm_tee_probe,
  298. .remove = ftpm_tee_remove,
  299. .id_table = optee_ftpm_id_table,
  300. .driver = {
  301. .name = "optee-ftpm",
  302. },
  303. };
  304. static int __init ftpm_mod_init(void)
  305. {
  306. int rc;
  307. rc = platform_driver_register(&ftpm_tee_plat_driver);
  308. if (rc)
  309. return rc;
  310. rc = tee_client_driver_register(&ftpm_tee_driver);
  311. if (rc) {
  312. platform_driver_unregister(&ftpm_tee_plat_driver);
  313. return rc;
  314. }
  315. return 0;
  316. }
  317. static void __exit ftpm_mod_exit(void)
  318. {
  319. platform_driver_unregister(&ftpm_tee_plat_driver);
  320. tee_client_driver_unregister(&ftpm_tee_driver);
  321. }
  322. module_init(ftpm_mod_init);
  323. module_exit(ftpm_mod_exit);
  324. MODULE_AUTHOR("Thirupathaiah Annapureddy <thiruan@microsoft.com>");
  325. MODULE_DESCRIPTION("TPM Driver for fTPM TA in TEE");
  326. MODULE_LICENSE("GPL v2");