efi_variable_tee.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. #define OPTEE_PAGE_SIZE BIT(12)
  17. extern struct efi_var_file __efi_runtime_data *efi_var_buf;
  18. static efi_uintn_t max_buffer_size; /* comm + var + func + data */
  19. static efi_uintn_t max_payload_size; /* func + data */
  20. struct mm_connection {
  21. struct udevice *tee;
  22. u32 session;
  23. };
  24. /**
  25. * get_connection() - Retrieve OP-TEE session for a specific UUID.
  26. *
  27. * @conn: session buffer to fill
  28. * Return: status code
  29. */
  30. static int get_connection(struct mm_connection *conn)
  31. {
  32. static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
  33. struct udevice *tee = NULL;
  34. struct tee_open_session_arg arg;
  35. int rc;
  36. tee = tee_find_device(tee, NULL, NULL, NULL);
  37. if (!tee)
  38. return -ENODEV;
  39. memset(&arg, 0, sizeof(arg));
  40. tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
  41. rc = tee_open_session(tee, &arg, 0, NULL);
  42. if (!rc) {
  43. conn->tee = tee;
  44. conn->session = arg.session;
  45. }
  46. return rc;
  47. }
  48. /**
  49. * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
  50. *
  51. * @comm_buf: locally allocted communcation buffer
  52. * @dsize: buffer size
  53. * Return: status code
  54. */
  55. static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
  56. {
  57. ulong buf_size;
  58. efi_status_t ret;
  59. struct efi_mm_communicate_header *mm_hdr;
  60. struct mm_connection conn = { NULL, 0 };
  61. struct tee_invoke_arg arg;
  62. struct tee_param param[2];
  63. struct tee_shm *shm = NULL;
  64. int rc;
  65. if (!comm_buf)
  66. return EFI_INVALID_PARAMETER;
  67. mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
  68. buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
  69. if (dsize != buf_size)
  70. return EFI_INVALID_PARAMETER;
  71. rc = get_connection(&conn);
  72. if (rc) {
  73. log_err("Unable to open OP-TEE session (err=%d)\n", rc);
  74. return EFI_UNSUPPORTED;
  75. }
  76. if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
  77. log_err("Unable to register shared memory\n");
  78. return EFI_UNSUPPORTED;
  79. }
  80. memset(&arg, 0, sizeof(arg));
  81. arg.func = PTA_STMM_CMDID_COMMUNICATE;
  82. arg.session = conn.session;
  83. memset(param, 0, sizeof(param));
  84. param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
  85. param[0].u.memref.size = buf_size;
  86. param[0].u.memref.shm = shm;
  87. param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
  88. rc = tee_invoke_func(conn.tee, &arg, 2, param);
  89. tee_shm_free(shm);
  90. tee_close_session(conn.tee, conn.session);
  91. if (rc || arg.ret != TEE_SUCCESS)
  92. return EFI_DEVICE_ERROR;
  93. switch (param[1].u.value.a) {
  94. case ARM_SVC_SPM_RET_SUCCESS:
  95. ret = EFI_SUCCESS;
  96. break;
  97. case ARM_SVC_SPM_RET_INVALID_PARAMS:
  98. ret = EFI_INVALID_PARAMETER;
  99. break;
  100. case ARM_SVC_SPM_RET_DENIED:
  101. ret = EFI_ACCESS_DENIED;
  102. break;
  103. case ARM_SVC_SPM_RET_NO_MEMORY:
  104. ret = EFI_OUT_OF_RESOURCES;
  105. break;
  106. default:
  107. ret = EFI_ACCESS_DENIED;
  108. }
  109. return ret;
  110. }
  111. /**
  112. * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send
  113. * it to OP-TEE
  114. *
  115. * @comm_buf: locally allocted communcation buffer
  116. * @dsize: buffer size
  117. * Return: status code
  118. */
  119. static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
  120. {
  121. efi_status_t ret;
  122. struct efi_mm_communicate_header *mm_hdr;
  123. struct smm_variable_communicate_header *var_hdr;
  124. dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
  125. mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
  126. var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
  127. ret = optee_mm_communicate(comm_buf, dsize);
  128. if (ret != EFI_SUCCESS) {
  129. log_err("%s failed!\n", __func__);
  130. return ret;
  131. }
  132. return var_hdr->ret_status;
  133. }
  134. /**
  135. * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the
  136. * header data.
  137. *
  138. * @dptr: pointer address of the corresponding StandAloneMM
  139. * function
  140. * @payload_size: buffer size
  141. * @func: standAloneMM function number
  142. * @ret: EFI return code
  143. * Return: buffer or NULL
  144. */
  145. static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
  146. efi_uintn_t func, efi_status_t *ret)
  147. {
  148. const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
  149. struct efi_mm_communicate_header *mm_hdr;
  150. struct smm_variable_communicate_header *var_hdr;
  151. u8 *comm_buf;
  152. /* In the init function we initialize max_buffer_size with
  153. * get_max_payload(). So skip the test if max_buffer_size is initialized
  154. * StandAloneMM will perform similar checks and drop the buffer if it's
  155. * too long
  156. */
  157. if (max_buffer_size && max_buffer_size <
  158. (MM_COMMUNICATE_HEADER_SIZE +
  159. MM_VARIABLE_COMMUNICATE_SIZE +
  160. payload_size)) {
  161. *ret = EFI_INVALID_PARAMETER;
  162. return NULL;
  163. }
  164. comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
  165. MM_VARIABLE_COMMUNICATE_SIZE +
  166. payload_size);
  167. if (!comm_buf) {
  168. *ret = EFI_OUT_OF_RESOURCES;
  169. return NULL;
  170. }
  171. mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
  172. guidcpy(&mm_hdr->header_guid, &mm_var_guid);
  173. mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
  174. var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
  175. var_hdr->function = func;
  176. if (dptr)
  177. *dptr = var_hdr->data;
  178. *ret = EFI_SUCCESS;
  179. return comm_buf;
  180. }
  181. /**
  182. * get_max_payload() - Get variable payload size from StandAloneMM.
  183. *
  184. * @size: size of the variable in storage
  185. * Return: status code
  186. */
  187. efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
  188. {
  189. struct smm_variable_payload_size *var_payload = NULL;
  190. efi_uintn_t payload_size;
  191. u8 *comm_buf = NULL;
  192. efi_status_t ret;
  193. if (!size) {
  194. ret = EFI_INVALID_PARAMETER;
  195. goto out;
  196. }
  197. payload_size = sizeof(*var_payload);
  198. comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
  199. SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
  200. if (!comm_buf)
  201. goto out;
  202. ret = mm_communicate(comm_buf, payload_size);
  203. if (ret != EFI_SUCCESS)
  204. goto out;
  205. /* Make sure the buffer is big enough for storing variables */
  206. if (var_payload->size < MM_VARIABLE_ACCESS_HEADER_SIZE + 0x20) {
  207. ret = EFI_DEVICE_ERROR;
  208. goto out;
  209. }
  210. *size = var_payload->size;
  211. /*
  212. * Although the max payload is configurable on StMM, we only share a
  213. * single page from OP-TEE for the non-secure buffer used to communicate
  214. * with StMM. Since OP-TEE will reject to map anything bigger than that,
  215. * make sure we are in bounds.
  216. */
  217. if (*size > OPTEE_PAGE_SIZE)
  218. *size = OPTEE_PAGE_SIZE - MM_COMMUNICATE_HEADER_SIZE -
  219. MM_VARIABLE_COMMUNICATE_SIZE;
  220. /*
  221. * There seems to be a bug in EDK2 miscalculating the boundaries and
  222. * size checks, so deduct 2 more bytes to fulfill this requirement. Fix
  223. * it up here to ensure backwards compatibility with older versions
  224. * (cf. StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c.
  225. * sizeof (EFI_MM_COMMUNICATE_HEADER) instead the size minus the
  226. * flexible array member).
  227. *
  228. * size is guaranteed to be > 2 due to checks on the beginning.
  229. */
  230. *size -= 2;
  231. out:
  232. free(comm_buf);
  233. return ret;
  234. }
  235. /*
  236. * StMM can store internal attributes and properties for variables, i.e enabling
  237. * R/O variables
  238. */
  239. static efi_status_t set_property_int(u16 *variable_name, efi_uintn_t name_size,
  240. const efi_guid_t *vendor,
  241. struct var_check_property *var_property)
  242. {
  243. struct smm_variable_var_check_property *smm_property;
  244. efi_uintn_t payload_size;
  245. u8 *comm_buf = NULL;
  246. efi_status_t ret;
  247. payload_size = sizeof(*smm_property) + name_size;
  248. if (payload_size > max_payload_size) {
  249. ret = EFI_INVALID_PARAMETER;
  250. goto out;
  251. }
  252. comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
  253. SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET,
  254. &ret);
  255. if (!comm_buf)
  256. goto out;
  257. guidcpy(&smm_property->guid, vendor);
  258. smm_property->name_size = name_size;
  259. memcpy(&smm_property->property, var_property,
  260. sizeof(smm_property->property));
  261. memcpy(smm_property->name, variable_name, name_size);
  262. ret = mm_communicate(comm_buf, payload_size);
  263. out:
  264. free(comm_buf);
  265. return ret;
  266. }
  267. static efi_status_t get_property_int(u16 *variable_name, efi_uintn_t name_size,
  268. const efi_guid_t *vendor,
  269. struct var_check_property *var_property)
  270. {
  271. struct smm_variable_var_check_property *smm_property;
  272. efi_uintn_t payload_size;
  273. u8 *comm_buf = NULL;
  274. efi_status_t ret;
  275. memset(var_property, 0, sizeof(*var_property));
  276. payload_size = sizeof(*smm_property) + name_size;
  277. if (payload_size > max_payload_size) {
  278. ret = EFI_INVALID_PARAMETER;
  279. goto out;
  280. }
  281. comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
  282. SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET,
  283. &ret);
  284. if (!comm_buf)
  285. goto out;
  286. guidcpy(&smm_property->guid, vendor);
  287. smm_property->name_size = name_size;
  288. memcpy(smm_property->name, variable_name, name_size);
  289. ret = mm_communicate(comm_buf, payload_size);
  290. /*
  291. * Currently only R/O property is supported in StMM.
  292. * Variables that are not set to R/O will not set the property in StMM
  293. * and the call will return EFI_NOT_FOUND. We are setting the
  294. * properties to 0x0 so checking against that is enough for the
  295. * EFI_NOT_FOUND case.
  296. */
  297. if (ret == EFI_NOT_FOUND)
  298. ret = EFI_SUCCESS;
  299. if (ret != EFI_SUCCESS)
  300. goto out;
  301. memcpy(var_property, &smm_property->property, sizeof(*var_property));
  302. out:
  303. free(comm_buf);
  304. return ret;
  305. }
  306. efi_status_t efi_get_variable_int(u16 *variable_name, const efi_guid_t *vendor,
  307. u32 *attributes, efi_uintn_t *data_size,
  308. void *data, u64 *timep)
  309. {
  310. struct var_check_property var_property;
  311. struct smm_variable_access *var_acc;
  312. efi_uintn_t payload_size;
  313. efi_uintn_t name_size;
  314. efi_uintn_t tmp_dsize;
  315. u8 *comm_buf = NULL;
  316. efi_status_t ret;
  317. if (!variable_name || !vendor || !data_size) {
  318. ret = EFI_INVALID_PARAMETER;
  319. goto out;
  320. }
  321. /* Check payload size */
  322. name_size = u16_strsize(variable_name);
  323. if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
  324. ret = EFI_INVALID_PARAMETER;
  325. goto out;
  326. }
  327. /* Trim output buffer size */
  328. tmp_dsize = *data_size;
  329. if (name_size + tmp_dsize >
  330. max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
  331. tmp_dsize = max_payload_size -
  332. MM_VARIABLE_ACCESS_HEADER_SIZE -
  333. name_size;
  334. }
  335. /* Get communication buffer and initialize header */
  336. payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
  337. comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
  338. SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
  339. if (!comm_buf)
  340. goto out;
  341. /* Fill in contents */
  342. guidcpy(&var_acc->guid, vendor);
  343. var_acc->data_size = tmp_dsize;
  344. var_acc->name_size = name_size;
  345. var_acc->attr = attributes ? *attributes : 0;
  346. memcpy(var_acc->name, variable_name, name_size);
  347. /* Communicate */
  348. ret = mm_communicate(comm_buf, payload_size);
  349. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  350. /* Update with reported data size for trimmed case */
  351. *data_size = var_acc->data_size;
  352. }
  353. if (ret != EFI_SUCCESS)
  354. goto out;
  355. ret = get_property_int(variable_name, name_size, vendor, &var_property);
  356. if (ret != EFI_SUCCESS)
  357. goto out;
  358. if (attributes) {
  359. *attributes = var_acc->attr;
  360. if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
  361. *attributes |= EFI_VARIABLE_READ_ONLY;
  362. }
  363. if (data)
  364. memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
  365. var_acc->data_size);
  366. else
  367. ret = EFI_INVALID_PARAMETER;
  368. out:
  369. free(comm_buf);
  370. return ret;
  371. }
  372. efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
  373. u16 *variable_name,
  374. efi_guid_t *guid)
  375. {
  376. struct smm_variable_getnext *var_getnext;
  377. efi_uintn_t payload_size;
  378. efi_uintn_t out_name_size;
  379. efi_uintn_t in_name_size;
  380. u8 *comm_buf = NULL;
  381. efi_status_t ret;
  382. if (!variable_name_size || !variable_name || !guid) {
  383. ret = EFI_INVALID_PARAMETER;
  384. goto out;
  385. }
  386. out_name_size = *variable_name_size;
  387. in_name_size = u16_strsize(variable_name);
  388. if (out_name_size < in_name_size) {
  389. ret = EFI_INVALID_PARAMETER;
  390. goto out;
  391. }
  392. if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
  393. ret = EFI_INVALID_PARAMETER;
  394. goto out;
  395. }
  396. /* Trim output buffer size */
  397. if (out_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
  398. out_name_size = max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE;
  399. payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
  400. comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
  401. SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
  402. &ret);
  403. if (!comm_buf)
  404. goto out;
  405. /* Fill in contents */
  406. guidcpy(&var_getnext->guid, guid);
  407. var_getnext->name_size = out_name_size;
  408. memcpy(var_getnext->name, variable_name, in_name_size);
  409. memset((u8 *)var_getnext->name + in_name_size, 0x0,
  410. out_name_size - in_name_size);
  411. /* Communicate */
  412. ret = mm_communicate(comm_buf, payload_size);
  413. if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
  414. /* Update with reported data size for trimmed case */
  415. *variable_name_size = var_getnext->name_size;
  416. }
  417. if (ret != EFI_SUCCESS)
  418. goto out;
  419. guidcpy(guid, &var_getnext->guid);
  420. memcpy(variable_name, var_getnext->name, var_getnext->name_size);
  421. out:
  422. free(comm_buf);
  423. return ret;
  424. }
  425. efi_status_t efi_set_variable_int(u16 *variable_name, const efi_guid_t *vendor,
  426. u32 attributes, efi_uintn_t data_size,
  427. const void *data, bool ro_check)
  428. {
  429. efi_status_t ret, alt_ret = EFI_SUCCESS;
  430. struct var_check_property var_property;
  431. struct smm_variable_access *var_acc;
  432. efi_uintn_t payload_size;
  433. efi_uintn_t name_size;
  434. u8 *comm_buf = NULL;
  435. bool ro;
  436. if (!variable_name || variable_name[0] == 0 || !vendor) {
  437. ret = EFI_INVALID_PARAMETER;
  438. goto out;
  439. }
  440. if (data_size > 0 && !data) {
  441. ret = EFI_INVALID_PARAMETER;
  442. goto out;
  443. }
  444. /* Check payload size */
  445. name_size = u16_strsize(variable_name);
  446. payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
  447. if (payload_size > max_payload_size) {
  448. ret = EFI_INVALID_PARAMETER;
  449. goto out;
  450. }
  451. /*
  452. * Allocate the buffer early, before switching to RW (if needed)
  453. * so we won't need to account for any failures in reading/setting
  454. * the properties, if the allocation fails
  455. */
  456. comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
  457. SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
  458. if (!comm_buf)
  459. goto out;
  460. ro = !!(attributes & EFI_VARIABLE_READ_ONLY);
  461. attributes &= EFI_VARIABLE_MASK;
  462. /*
  463. * The API has the ability to override RO flags. If no RO check was
  464. * requested switch the variable to RW for the duration of this call
  465. */
  466. ret = get_property_int(variable_name, name_size, vendor,
  467. &var_property);
  468. if (ret != EFI_SUCCESS)
  469. goto out;
  470. if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) {
  471. /* Bypass r/o check */
  472. if (!ro_check) {
  473. var_property.property &= ~VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
  474. ret = set_property_int(variable_name, name_size, vendor, &var_property);
  475. if (ret != EFI_SUCCESS)
  476. goto out;
  477. } else {
  478. ret = EFI_WRITE_PROTECTED;
  479. goto out;
  480. }
  481. }
  482. /* Fill in contents */
  483. guidcpy(&var_acc->guid, vendor);
  484. var_acc->data_size = data_size;
  485. var_acc->name_size = name_size;
  486. var_acc->attr = attributes;
  487. memcpy(var_acc->name, variable_name, name_size);
  488. memcpy((u8 *)var_acc->name + name_size, data, data_size);
  489. /* Communicate */
  490. ret = mm_communicate(comm_buf, payload_size);
  491. if (ret != EFI_SUCCESS)
  492. alt_ret = ret;
  493. if (ro && !(var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)) {
  494. var_property.revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
  495. var_property.property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
  496. var_property.attributes = attributes;
  497. var_property.minsize = 1;
  498. var_property.maxsize = var_acc->data_size;
  499. ret = set_property_int(variable_name, name_size, vendor, &var_property);
  500. }
  501. if (alt_ret != EFI_SUCCESS)
  502. goto out;
  503. if (!u16_strcmp(variable_name, L"PK"))
  504. alt_ret = efi_init_secure_state();
  505. out:
  506. free(comm_buf);
  507. return alt_ret == EFI_SUCCESS ? ret : alt_ret;
  508. }
  509. efi_status_t efi_query_variable_info_int(u32 attributes,
  510. u64 *max_variable_storage_size,
  511. u64 *remain_variable_storage_size,
  512. u64 *max_variable_size)
  513. {
  514. struct smm_variable_query_info *mm_query_info;
  515. efi_uintn_t payload_size;
  516. efi_status_t ret;
  517. u8 *comm_buf;
  518. payload_size = sizeof(*mm_query_info);
  519. comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
  520. SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
  521. &ret);
  522. if (!comm_buf)
  523. goto out;
  524. mm_query_info->attr = attributes;
  525. ret = mm_communicate(comm_buf, payload_size);
  526. if (ret != EFI_SUCCESS)
  527. goto out;
  528. *max_variable_storage_size = mm_query_info->max_variable_storage;
  529. *remain_variable_storage_size =
  530. mm_query_info->remaining_variable_storage;
  531. *max_variable_size = mm_query_info->max_variable_size;
  532. out:
  533. free(comm_buf);
  534. return ret;
  535. }
  536. /**
  537. * efi_query_variable_info() - get information about EFI variables
  538. *
  539. * This function implements the QueryVariableInfo() runtime service.
  540. *
  541. * See the Unified Extensible Firmware Interface (UEFI) specification for
  542. * details.
  543. *
  544. * @attributes: bitmask to select variables to be
  545. * queried
  546. * @maximum_variable_storage_size: maximum size of storage area for the
  547. * selected variable types
  548. * @remaining_variable_storage_size: remaining size of storage are for the
  549. * selected variable types
  550. * @maximum_variable_size: maximum size of a variable of the
  551. * selected type
  552. * Return: status code
  553. */
  554. efi_status_t EFIAPI __efi_runtime
  555. efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
  556. u64 *remain_variable_storage_size,
  557. u64 *max_variable_size)
  558. {
  559. return EFI_UNSUPPORTED;
  560. }
  561. /**
  562. * efi_set_variable_runtime() - runtime implementation of SetVariable()
  563. *
  564. * @variable_name: name of the variable
  565. * @guid: vendor GUID
  566. * @attributes: attributes of the variable
  567. * @data_size: size of the buffer with the variable value
  568. * @data: buffer with the variable value
  569. * Return: status code
  570. */
  571. static efi_status_t __efi_runtime EFIAPI
  572. efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
  573. u32 attributes, efi_uintn_t data_size,
  574. const void *data)
  575. {
  576. return EFI_UNSUPPORTED;
  577. }
  578. /**
  579. * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
  580. */
  581. void efi_variables_boot_exit_notify(void)
  582. {
  583. efi_status_t ret;
  584. u8 *comm_buf;
  585. loff_t len;
  586. struct efi_var_file *var_buf;
  587. comm_buf = setup_mm_hdr(NULL, 0,
  588. SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
  589. if (comm_buf)
  590. ret = mm_communicate(comm_buf, 0);
  591. else
  592. ret = EFI_NOT_FOUND;
  593. if (ret != EFI_SUCCESS)
  594. log_err("Unable to notify StMM for ExitBootServices\n");
  595. free(comm_buf);
  596. /*
  597. * Populate the list for runtime variables.
  598. * asking EFI_VARIABLE_RUNTIME_ACCESS is redundant, since
  599. * efi_var_mem_notify_exit_boot_services will clean those, but that's fine
  600. */
  601. ret = efi_var_collect(&var_buf, &len, EFI_VARIABLE_RUNTIME_ACCESS);
  602. if (ret != EFI_SUCCESS)
  603. log_err("Can't populate EFI variables. No runtime variables will be available\n");
  604. else
  605. memcpy(efi_var_buf, var_buf, len);
  606. free(var_buf);
  607. /* Update runtime service table */
  608. efi_runtime_services.query_variable_info =
  609. efi_query_variable_info_runtime;
  610. efi_runtime_services.get_variable = efi_get_variable_runtime;
  611. efi_runtime_services.get_next_variable_name =
  612. efi_get_next_variable_name_runtime;
  613. efi_runtime_services.set_variable = efi_set_variable_runtime;
  614. efi_update_table_header_crc32(&efi_runtime_services.hdr);
  615. }
  616. /**
  617. * efi_init_variables() - initialize variable services
  618. *
  619. * Return: status code
  620. */
  621. efi_status_t efi_init_variables(void)
  622. {
  623. efi_status_t ret;
  624. /* Create a cached copy of the variables that will be enabled on ExitBootServices() */
  625. ret = efi_var_mem_init();
  626. if (ret != EFI_SUCCESS)
  627. return ret;
  628. ret = get_max_payload(&max_payload_size);
  629. if (ret != EFI_SUCCESS)
  630. return ret;
  631. max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
  632. MM_VARIABLE_COMMUNICATE_SIZE +
  633. max_payload_size;
  634. ret = efi_init_secure_state();
  635. if (ret != EFI_SUCCESS)
  636. return ret;
  637. return EFI_SUCCESS;
  638. }