efi_variable_tee.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI variable service via OP-TEE
  4. *
  5. * Copyright (C) 2019 Linaro Ltd. <sughosh.ganu@linaro.org>
  6. * Copyright (C) 2019 Linaro Ltd. <ilias.apalodimas@linaro.org>
  7. */
  8. #include <common.h>
  9. #include <efi.h>
  10. #include <efi_api.h>
  11. #include <efi_loader.h>
  12. #include <efi_variable.h>
  13. #include <tee.h>
  14. #include <malloc.h>
  15. #include <mm_communication.h>
  16. static efi_uintn_t max_buffer_size; /* comm + var + func + data */
  17. static efi_uintn_t max_payload_size; /* func + data */
  18. struct mm_connection {
  19. struct udevice *tee;
  20. u32 session;
  21. };
  22. /**
  23. * get_connection() - Retrieve OP-TEE session for a specific UUID.
  24. *
  25. * @conn: session buffer to fill
  26. * Return: status code
  27. */
  28. static int get_connection(struct mm_connection *conn)
  29. {
  30. static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
  31. struct udevice *tee = NULL;
  32. struct tee_open_session_arg arg;
  33. int rc;
  34. tee = tee_find_device(tee, NULL, NULL, NULL);
  35. if (!tee)
  36. return -ENODEV;
  37. memset(&arg, 0, sizeof(arg));
  38. tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
  39. rc = tee_open_session(tee, &arg, 0, NULL);
  40. if (!rc) {
  41. conn->tee = tee;
  42. conn->session = arg.session;
  43. }
  44. return rc;
  45. }
  46. /**
  47. * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
  48. *
  49. * @comm_buf: locally allocted communcation buffer
  50. * @dsize: buffer size
  51. * Return: status code
  52. */
  53. static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
  54. {
  55. ulong buf_size;
  56. efi_status_t ret;
  57. struct efi_mm_communicate_header *mm_hdr;
  58. struct mm_connection conn = { NULL, 0 };
  59. struct tee_invoke_arg arg;
  60. struct tee_param param[2];
  61. struct tee_shm *shm = NULL;
  62. int rc;
  63. if (!comm_buf)
  64. return EFI_INVALID_PARAMETER;
  65. mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
  66. buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
  67. if (dsize != buf_size)
  68. return EFI_INVALID_PARAMETER;
  69. rc = get_connection(&conn);
  70. if (rc) {
  71. log_err("Unable to open OP-TEE session (err=%d)\n", rc);
  72. return EFI_UNSUPPORTED;
  73. }
  74. if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
  75. log_err("Unable to register shared memory\n");
  76. return EFI_UNSUPPORTED;
  77. }
  78. memset(&arg, 0, sizeof(arg));
  79. arg.func = PTA_STMM_CMDID_COMMUNICATE;
  80. arg.session = conn.session;
  81. memset(param, 0, sizeof(param));
  82. param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
  83. param[0].u.memref.size = buf_size;
  84. param[0].u.memref.shm = shm;
  85. param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
  86. rc = tee_invoke_func(conn.tee, &arg, 2, param);
  87. if (rc)
  88. return EFI_INVALID_PARAMETER;
  89. tee_shm_free(shm);
  90. tee_close_session(conn.tee, conn.session);
  91. switch (param[1].u.value.a) {
  92. case ARM_SMC_MM_RET_SUCCESS:
  93. ret = EFI_SUCCESS;
  94. break;
  95. case ARM_SMC_MM_RET_INVALID_PARAMS:
  96. ret = EFI_INVALID_PARAMETER;
  97. break;
  98. case ARM_SMC_MM_RET_DENIED:
  99. ret = EFI_ACCESS_DENIED;
  100. break;
  101. case ARM_SMC_MM_RET_NO_MEMORY:
  102. ret = EFI_OUT_OF_RESOURCES;
  103. break;
  104. default:
  105. ret = EFI_ACCESS_DENIED;
  106. }
  107. return ret;
  108. }
  109. /**
  110. * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send
  111. * it to OP-TEE
  112. *
  113. * @comm_buf: locally allocted communcation buffer
  114. * @dsize: buffer size
  115. * Return: status code
  116. */
  117. static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
  118. {
  119. efi_status_t ret;
  120. struct efi_mm_communicate_header *mm_hdr;
  121. struct smm_variable_communicate_header *var_hdr;
  122. dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
  123. mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
  124. var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
  125. ret = optee_mm_communicate(comm_buf, dsize);
  126. if (ret != EFI_SUCCESS) {
  127. log_err("%s failed!\n", __func__);
  128. return ret;
  129. }
  130. return var_hdr->ret_status;
  131. }
  132. /**
  133. * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the
  134. * header data.
  135. *
  136. * @dptr: pointer address of the corresponding StandAloneMM
  137. * function
  138. * @payload_size: buffer size
  139. * @func: standAloneMM function number
  140. * @ret: EFI return code
  141. * Return: buffer or NULL
  142. */
  143. static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
  144. efi_uintn_t func, efi_status_t *ret)
  145. {
  146. const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
  147. struct efi_mm_communicate_header *mm_hdr;
  148. struct smm_variable_communicate_header *var_hdr;
  149. u8 *comm_buf;
  150. /* In the init function we initialize max_buffer_size with
  151. * get_max_payload(). So skip the test if max_buffer_size is initialized
  152. * StandAloneMM will perform similar checks and drop the buffer if it's
  153. * too long
  154. */
  155. if (max_buffer_size && max_buffer_size <
  156. (MM_COMMUNICATE_HEADER_SIZE +
  157. MM_VARIABLE_COMMUNICATE_SIZE +
  158. payload_size)) {
  159. *ret = EFI_INVALID_PARAMETER;
  160. return NULL;
  161. }
  162. comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
  163. MM_VARIABLE_COMMUNICATE_SIZE +
  164. payload_size);
  165. if (!comm_buf) {
  166. *ret = EFI_OUT_OF_RESOURCES;
  167. return NULL;
  168. }
  169. mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
  170. guidcpy(&mm_hdr->header_guid, &mm_var_guid);
  171. mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
  172. var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
  173. var_hdr->function = func;
  174. if (dptr)
  175. *dptr = var_hdr->data;
  176. *ret = EFI_SUCCESS;
  177. return comm_buf;
  178. }
  179. /**
  180. * get_max_payload() - Get variable payload size from StandAloneMM.
  181. *
  182. * @size: size of the variable in storage
  183. * Return: status code
  184. */
  185. efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
  186. {
  187. struct smm_variable_payload_size *var_payload = NULL;
  188. efi_uintn_t payload_size;
  189. u8 *comm_buf = NULL;
  190. efi_status_t ret;
  191. if (!size) {
  192. ret = EFI_INVALID_PARAMETER;
  193. goto out;
  194. }
  195. payload_size = sizeof(*var_payload);
  196. comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
  197. SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
  198. if (!comm_buf)
  199. goto out;
  200. ret = mm_communicate(comm_buf, payload_size);
  201. if (ret != EFI_SUCCESS)
  202. goto out;
  203. *size = var_payload->size;
  204. out:
  205. free(comm_buf);
  206. return ret;
  207. }
  208. efi_status_t efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor,
  209. u32 *attributes, efi_uintn_t *data_size,
  210. void *data, u64 *timep)
  211. {
  212. struct smm_variable_access *var_acc;
  213. efi_uintn_t payload_size;
  214. efi_uintn_t name_size;
  215. efi_uintn_t tmp_dsize;
  216. u8 *comm_buf = NULL;
  217. efi_status_t ret;
  218. if (!variable_name || !vendor || !data_size) {
  219. ret = EFI_INVALID_PARAMETER;
  220. goto out;
  221. }
  222. /* Check payload size */
  223. name_size = u16_strsize(variable_name);
  224. if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
  225. ret = EFI_INVALID_PARAMETER;
  226. goto out;
  227. }
  228. /* Trim output buffer size */
  229. tmp_dsize = *data_size;
  230. if (name_size + tmp_dsize >
  231. max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
  232. tmp_dsize = max_payload_size -
  233. MM_VARIABLE_ACCESS_HEADER_SIZE -
  234. name_size;
  235. }
  236. /* Get communication buffer and initialize header */
  237. payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
  238. comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
  239. SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
  240. if (!comm_buf)
  241. goto out;
  242. /* Fill in contents */
  243. guidcpy(&var_acc->guid, vendor);
  244. var_acc->data_size = tmp_dsize;
  245. var_acc->name_size = name_size;
  246. var_acc->attr = attributes ? *attributes : 0;
  247. memcpy(var_acc->name, variable_name, name_size);
  248. /* Communicate */
  249. ret = mm_communicate(comm_buf, payload_size);
  250. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  251. /* Update with reported data size for trimmed case */
  252. *data_size = var_acc->data_size;
  253. }
  254. if (ret != EFI_SUCCESS)
  255. goto out;
  256. if (attributes)
  257. *attributes = var_acc->attr;
  258. if (data)
  259. memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
  260. var_acc->data_size);
  261. else
  262. ret = EFI_INVALID_PARAMETER;
  263. out:
  264. free(comm_buf);
  265. return ret;
  266. }
  267. efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
  268. u16 *variable_name,
  269. efi_guid_t *guid)
  270. {
  271. struct smm_variable_getnext *var_getnext;
  272. efi_uintn_t payload_size;
  273. efi_uintn_t out_name_size;
  274. efi_uintn_t in_name_size;
  275. efi_uintn_t tmp_dsize;
  276. u8 *comm_buf = NULL;
  277. efi_status_t ret;
  278. if (!variable_name_size || !variable_name || !guid) {
  279. ret = EFI_INVALID_PARAMETER;
  280. goto out;
  281. }
  282. out_name_size = *variable_name_size;
  283. in_name_size = u16_strsize(variable_name);
  284. if (out_name_size < in_name_size) {
  285. ret = EFI_INVALID_PARAMETER;
  286. goto out;
  287. }
  288. if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
  289. ret = EFI_INVALID_PARAMETER;
  290. goto out;
  291. }
  292. /* Trim output buffer size */
  293. tmp_dsize = *variable_name_size;
  294. if (in_name_size + tmp_dsize >
  295. max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
  296. tmp_dsize = max_payload_size -
  297. MM_VARIABLE_GET_NEXT_HEADER_SIZE -
  298. in_name_size;
  299. }
  300. payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
  301. comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
  302. SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
  303. &ret);
  304. if (!comm_buf)
  305. goto out;
  306. /* Fill in contents */
  307. guidcpy(&var_getnext->guid, guid);
  308. var_getnext->name_size = out_name_size;
  309. memcpy(var_getnext->name, variable_name, in_name_size);
  310. memset((u8 *)var_getnext->name + in_name_size, 0x0,
  311. out_name_size - in_name_size);
  312. /* Communicate */
  313. ret = mm_communicate(comm_buf, payload_size);
  314. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  315. /* Update with reported data size for trimmed case */
  316. *variable_name_size = var_getnext->name_size;
  317. }
  318. if (ret != EFI_SUCCESS)
  319. goto out;
  320. guidcpy(guid, &var_getnext->guid);
  321. memcpy(variable_name, (u8 *)var_getnext->name,
  322. var_getnext->name_size);
  323. out:
  324. free(comm_buf);
  325. return ret;
  326. }
  327. efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor,
  328. u32 attributes, efi_uintn_t data_size,
  329. const void *data, bool ro_check)
  330. {
  331. struct smm_variable_access *var_acc;
  332. efi_uintn_t payload_size;
  333. efi_uintn_t name_size;
  334. u8 *comm_buf = NULL;
  335. efi_status_t ret;
  336. if (!variable_name || variable_name[0] == 0 || !vendor) {
  337. ret = EFI_INVALID_PARAMETER;
  338. goto out;
  339. }
  340. if (data_size > 0 && !data) {
  341. ret = EFI_INVALID_PARAMETER;
  342. goto out;
  343. }
  344. /* Check payload size */
  345. name_size = u16_strsize(variable_name);
  346. payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
  347. if (payload_size > max_payload_size) {
  348. ret = EFI_INVALID_PARAMETER;
  349. goto out;
  350. }
  351. /* Get communication buffer and initialize header */
  352. comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
  353. SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
  354. if (!comm_buf)
  355. goto out;
  356. /* Fill in contents */
  357. guidcpy(&var_acc->guid, vendor);
  358. var_acc->data_size = data_size;
  359. var_acc->name_size = name_size;
  360. var_acc->attr = attributes;
  361. memcpy(var_acc->name, variable_name, name_size);
  362. memcpy((u8 *)var_acc->name + name_size, data, data_size);
  363. /* Communicate */
  364. ret = mm_communicate(comm_buf, payload_size);
  365. out:
  366. free(comm_buf);
  367. return ret;
  368. }
  369. efi_status_t efi_query_variable_info_int(u32 attributes,
  370. u64 *max_variable_storage_size,
  371. u64 *remain_variable_storage_size,
  372. u64 *max_variable_size)
  373. {
  374. struct smm_variable_query_info *mm_query_info;
  375. efi_uintn_t payload_size;
  376. efi_status_t ret;
  377. u8 *comm_buf;
  378. payload_size = sizeof(*mm_query_info);
  379. comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
  380. SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
  381. &ret);
  382. if (!comm_buf)
  383. goto out;
  384. mm_query_info->attr = attributes;
  385. ret = mm_communicate(comm_buf, payload_size);
  386. if (ret != EFI_SUCCESS)
  387. goto out;
  388. *max_variable_storage_size = mm_query_info->max_variable_storage;
  389. *remain_variable_storage_size =
  390. mm_query_info->remaining_variable_storage;
  391. *max_variable_size = mm_query_info->max_variable_size;
  392. out:
  393. free(comm_buf);
  394. return ret;
  395. }
  396. /**
  397. * efi_get_variable_runtime() - runtime implementation of GetVariable()
  398. *
  399. * @variable_name: name of the variable
  400. * @guid: vendor GUID
  401. * @attributes: attributes of the variable
  402. * @data_size: size of the buffer to which the variable value is copied
  403. * @data: buffer to which the variable value is copied
  404. * Return: status code
  405. */
  406. static efi_status_t __efi_runtime EFIAPI
  407. efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
  408. u32 *attributes, efi_uintn_t *data_size, void *data)
  409. {
  410. return EFI_UNSUPPORTED;
  411. }
  412. /**
  413. * efi_get_next_variable_name_runtime() - runtime implementation of
  414. * GetNextVariable()
  415. *
  416. * @variable_name_size: size of variable_name buffer in byte
  417. * @variable_name: name of uefi variable's name in u16
  418. * @guid: vendor's guid
  419. * Return: status code
  420. */
  421. static efi_status_t __efi_runtime EFIAPI
  422. efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
  423. u16 *variable_name, efi_guid_t *guid)
  424. {
  425. return EFI_UNSUPPORTED;
  426. }
  427. /**
  428. * efi_query_variable_info() - get information about EFI variables
  429. *
  430. * This function implements the QueryVariableInfo() runtime service.
  431. *
  432. * See the Unified Extensible Firmware Interface (UEFI) specification for
  433. * details.
  434. *
  435. * @attributes: bitmask to select variables to be
  436. * queried
  437. * @maximum_variable_storage_size: maximum size of storage area for the
  438. * selected variable types
  439. * @remaining_variable_storage_size: remaining size of storage are for the
  440. * selected variable types
  441. * @maximum_variable_size: maximum size of a variable of the
  442. * selected type
  443. * Return: status code
  444. */
  445. efi_status_t EFIAPI __efi_runtime
  446. efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
  447. u64 *remain_variable_storage_size,
  448. u64 *max_variable_size)
  449. {
  450. return EFI_UNSUPPORTED;
  451. }
  452. /**
  453. * efi_set_variable_runtime() - runtime implementation of SetVariable()
  454. *
  455. * @variable_name: name of the variable
  456. * @guid: vendor GUID
  457. * @attributes: attributes of the variable
  458. * @data_size: size of the buffer with the variable value
  459. * @data: buffer with the variable value
  460. * Return: status code
  461. */
  462. static efi_status_t __efi_runtime EFIAPI
  463. efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
  464. u32 attributes, efi_uintn_t data_size,
  465. const void *data)
  466. {
  467. return EFI_UNSUPPORTED;
  468. }
  469. /**
  470. * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
  471. */
  472. void efi_variables_boot_exit_notify(void)
  473. {
  474. u8 *comm_buf;
  475. efi_status_t ret;
  476. comm_buf = setup_mm_hdr(NULL, 0,
  477. SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
  478. if (comm_buf)
  479. ret = mm_communicate(comm_buf, 0);
  480. else
  481. ret = EFI_NOT_FOUND;
  482. if (ret != EFI_SUCCESS)
  483. log_err("Unable to notify StMM for ExitBootServices\n");
  484. free(comm_buf);
  485. /* Update runtime service table */
  486. efi_runtime_services.query_variable_info =
  487. efi_query_variable_info_runtime;
  488. efi_runtime_services.get_variable = efi_get_variable_runtime;
  489. efi_runtime_services.get_next_variable_name =
  490. efi_get_next_variable_name_runtime;
  491. efi_runtime_services.set_variable = efi_set_variable_runtime;
  492. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  493. }
  494. /**
  495. * efi_init_variables() - initialize variable services
  496. *
  497. * Return: status code
  498. */
  499. efi_status_t efi_init_variables(void)
  500. {
  501. efi_status_t ret;
  502. ret = get_max_payload(&max_payload_size);
  503. if (ret != EFI_SUCCESS)
  504. return ret;
  505. max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
  506. MM_VARIABLE_COMMUNICATE_SIZE +
  507. max_payload_size;
  508. return EFI_SUCCESS;
  509. }