efi_variable_tee.c 20 KB

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