tpm_ftpm_tee.c 10 KB

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