stm32-bsec-optee-ta.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * OP-TEE STM32MP BSEC PTA interface, used by STM32 ROMEM driver
  4. *
  5. * Copyright (C) 2022, STMicroelectronics - All Rights Reserved
  6. */
  7. #include <linux/tee_drv.h>
  8. #include "stm32-bsec-optee-ta.h"
  9. /*
  10. * Read OTP memory
  11. *
  12. * [in] value[0].a OTP start offset in byte
  13. * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock)
  14. * [out] memref[1].buffer Output buffer to store read values
  15. * [out] memref[1].size Size of OTP to be read
  16. *
  17. * Return codes:
  18. * TEE_SUCCESS - Invoke command success
  19. * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
  20. * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller
  21. */
  22. #define PTA_BSEC_READ_MEM 0x0
  23. /*
  24. * Write OTP memory
  25. *
  26. * [in] value[0].a OTP start offset in byte
  27. * [in] value[0].b Access type (0:shadow, 1:fuse, 2:lock)
  28. * [in] memref[1].buffer Input buffer to read values
  29. * [in] memref[1].size Size of OTP to be written
  30. *
  31. * Return codes:
  32. * TEE_SUCCESS - Invoke command success
  33. * TEE_ERROR_BAD_PARAMETERS - Incorrect input param
  34. * TEE_ERROR_ACCESS_DENIED - OTP not accessible by caller
  35. */
  36. #define PTA_BSEC_WRITE_MEM 0x1
  37. /* value of PTA_BSEC access type = value[in] b */
  38. #define SHADOW_ACCESS 0
  39. #define FUSE_ACCESS 1
  40. #define LOCK_ACCESS 2
  41. /* Bitfield definition for LOCK status */
  42. #define LOCK_PERM BIT(30)
  43. /* OP-TEE STM32MP BSEC TA UUID */
  44. static const uuid_t stm32mp_bsec_ta_uuid =
  45. UUID_INIT(0x94cf71ad, 0x80e6, 0x40b5,
  46. 0xa7, 0xc6, 0x3d, 0xc5, 0x01, 0xeb, 0x28, 0x03);
  47. /*
  48. * Check whether this driver supports the BSEC TA in the TEE instance
  49. * represented by the params (ver/data) to this function.
  50. */
  51. static int stm32_bsec_optee_ta_match(struct tee_ioctl_version_data *ver,
  52. const void *data)
  53. {
  54. /* Currently this driver only supports GP compliant, OP-TEE based TA */
  55. if ((ver->impl_id == TEE_IMPL_ID_OPTEE) &&
  56. (ver->gen_caps & TEE_GEN_CAP_GP))
  57. return 1;
  58. else
  59. return 0;
  60. }
  61. /* Open a session to OP-TEE for STM32MP BSEC TA */
  62. static int stm32_bsec_ta_open_session(struct tee_context *ctx, u32 *id)
  63. {
  64. struct tee_ioctl_open_session_arg sess_arg;
  65. int rc;
  66. memset(&sess_arg, 0, sizeof(sess_arg));
  67. export_uuid(sess_arg.uuid, &stm32mp_bsec_ta_uuid);
  68. sess_arg.clnt_login = TEE_IOCTL_LOGIN_REE_KERNEL;
  69. sess_arg.num_params = 0;
  70. rc = tee_client_open_session(ctx, &sess_arg, NULL);
  71. if ((rc < 0) || (sess_arg.ret != 0)) {
  72. pr_err("%s: tee_client_open_session failed err:%#x, ret:%#x\n",
  73. __func__, sess_arg.ret, rc);
  74. if (!rc)
  75. rc = -EINVAL;
  76. } else {
  77. *id = sess_arg.session;
  78. }
  79. return rc;
  80. }
  81. /* close a session to OP-TEE for STM32MP BSEC TA */
  82. static void stm32_bsec_ta_close_session(void *ctx, u32 id)
  83. {
  84. tee_client_close_session(ctx, id);
  85. }
  86. /* stm32_bsec_optee_ta_open() - initialize the STM32MP BSEC TA */
  87. int stm32_bsec_optee_ta_open(struct tee_context **ctx)
  88. {
  89. struct tee_context *tee_ctx;
  90. u32 session_id;
  91. int rc;
  92. /* Open context with TEE driver */
  93. tee_ctx = tee_client_open_context(NULL, stm32_bsec_optee_ta_match, NULL, NULL);
  94. if (IS_ERR(tee_ctx)) {
  95. rc = PTR_ERR(tee_ctx);
  96. if (rc == -ENOENT)
  97. return -EPROBE_DEFER;
  98. pr_err("%s: tee_client_open_context failed (%d)\n", __func__, rc);
  99. return rc;
  100. }
  101. /* Check STM32MP BSEC TA presence */
  102. rc = stm32_bsec_ta_open_session(tee_ctx, &session_id);
  103. if (rc) {
  104. tee_client_close_context(tee_ctx);
  105. return rc;
  106. }
  107. stm32_bsec_ta_close_session(tee_ctx, session_id);
  108. *ctx = tee_ctx;
  109. return 0;
  110. }
  111. /* stm32_bsec_optee_ta_open() - release the PTA STM32MP BSEC TA */
  112. void stm32_bsec_optee_ta_close(void *ctx)
  113. {
  114. tee_client_close_context(ctx);
  115. }
  116. /* stm32_bsec_optee_ta_read() - nvmem read access using PTA client driver */
  117. int stm32_bsec_optee_ta_read(struct tee_context *ctx, unsigned int offset,
  118. void *buf, size_t bytes)
  119. {
  120. struct tee_shm *shm;
  121. struct tee_ioctl_invoke_arg arg;
  122. struct tee_param param[2];
  123. u8 *shm_buf;
  124. u32 start, num_bytes;
  125. int ret;
  126. u32 session_id;
  127. ret = stm32_bsec_ta_open_session(ctx, &session_id);
  128. if (ret)
  129. return ret;
  130. memset(&arg, 0, sizeof(arg));
  131. memset(&param, 0, sizeof(param));
  132. arg.func = PTA_BSEC_READ_MEM;
  133. arg.session = session_id;
  134. arg.num_params = 2;
  135. /* align access on 32bits */
  136. start = ALIGN_DOWN(offset, 4);
  137. num_bytes = round_up(offset + bytes - start, 4);
  138. param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
  139. param[0].u.value.a = start;
  140. param[0].u.value.b = SHADOW_ACCESS;
  141. shm = tee_shm_alloc_kernel_buf(ctx, num_bytes);
  142. if (IS_ERR(shm)) {
  143. ret = PTR_ERR(shm);
  144. goto out_tee_session;
  145. }
  146. param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_OUTPUT;
  147. param[1].u.memref.shm = shm;
  148. param[1].u.memref.size = num_bytes;
  149. ret = tee_client_invoke_func(ctx, &arg, param);
  150. if (ret < 0 || arg.ret != 0) {
  151. pr_err("TA_BSEC invoke failed TEE err:%#x, ret:%#x\n",
  152. arg.ret, ret);
  153. if (!ret)
  154. ret = -EIO;
  155. }
  156. if (!ret) {
  157. shm_buf = tee_shm_get_va(shm, 0);
  158. if (IS_ERR(shm_buf)) {
  159. ret = PTR_ERR(shm_buf);
  160. pr_err("tee_shm_get_va failed for transmit (%d)\n", ret);
  161. } else {
  162. /* read data from 32 bits aligned buffer */
  163. memcpy(buf, &shm_buf[offset % 4], bytes);
  164. }
  165. }
  166. tee_shm_free(shm);
  167. out_tee_session:
  168. stm32_bsec_ta_close_session(ctx, session_id);
  169. return ret;
  170. }
  171. /* stm32_bsec_optee_ta_write() - nvmem write access using PTA client driver */
  172. int stm32_bsec_optee_ta_write(struct tee_context *ctx, unsigned int lower,
  173. unsigned int offset, void *buf, size_t bytes)
  174. { struct tee_shm *shm;
  175. struct tee_ioctl_invoke_arg arg;
  176. struct tee_param param[2];
  177. u8 *shm_buf;
  178. int ret;
  179. u32 session_id;
  180. ret = stm32_bsec_ta_open_session(ctx, &session_id);
  181. if (ret)
  182. return ret;
  183. /* Allow only writing complete 32-bits aligned words */
  184. if ((bytes % 4) || (offset % 4))
  185. return -EINVAL;
  186. memset(&arg, 0, sizeof(arg));
  187. memset(&param, 0, sizeof(param));
  188. arg.func = PTA_BSEC_WRITE_MEM;
  189. arg.session = session_id;
  190. arg.num_params = 2;
  191. param[0].attr = TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT;
  192. param[0].u.value.a = offset;
  193. param[0].u.value.b = FUSE_ACCESS;
  194. shm = tee_shm_alloc_kernel_buf(ctx, bytes);
  195. if (IS_ERR(shm)) {
  196. ret = PTR_ERR(shm);
  197. goto out_tee_session;
  198. }
  199. param[1].attr = TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INPUT;
  200. param[1].u.memref.shm = shm;
  201. param[1].u.memref.size = bytes;
  202. shm_buf = tee_shm_get_va(shm, 0);
  203. if (IS_ERR(shm_buf)) {
  204. ret = PTR_ERR(shm_buf);
  205. pr_err("tee_shm_get_va failed for transmit (%d)\n", ret);
  206. tee_shm_free(shm);
  207. goto out_tee_session;
  208. }
  209. memcpy(shm_buf, buf, bytes);
  210. ret = tee_client_invoke_func(ctx, &arg, param);
  211. if (ret < 0 || arg.ret != 0) {
  212. pr_err("TA_BSEC invoke failed TEE err:%#x, ret:%#x\n", arg.ret, ret);
  213. if (!ret)
  214. ret = -EIO;
  215. }
  216. pr_debug("Write OTPs %d to %zu, ret=%d\n", offset / 4, (offset + bytes) / 4, ret);
  217. /* Lock the upper OTPs with ECC protection, word programming only */
  218. if (!ret && ((offset + bytes) >= (lower * 4))) {
  219. u32 start, nb_lock;
  220. u32 *lock = (u32 *)shm_buf;
  221. int i;
  222. /*
  223. * don't lock the lower OTPs, no ECC protection and incremental
  224. * bit programming, a second write is allowed
  225. */
  226. start = max_t(u32, offset, lower * 4);
  227. nb_lock = (offset + bytes - start) / 4;
  228. param[0].u.value.a = start;
  229. param[0].u.value.b = LOCK_ACCESS;
  230. param[1].u.memref.size = nb_lock * 4;
  231. for (i = 0; i < nb_lock; i++)
  232. lock[i] = LOCK_PERM;
  233. ret = tee_client_invoke_func(ctx, &arg, param);
  234. if (ret < 0 || arg.ret != 0) {
  235. pr_err("TA_BSEC invoke failed TEE err:%#x, ret:%#x\n", arg.ret, ret);
  236. if (!ret)
  237. ret = -EIO;
  238. }
  239. pr_debug("Lock upper OTPs %d to %d, ret=%d\n",
  240. start / 4, start / 4 + nb_lock, ret);
  241. }
  242. tee_shm_free(shm);
  243. out_tee_session:
  244. stm32_bsec_ta_close_session(ctx, session_id);
  245. return ret;
  246. }