efi_variable_tee.c 19 KB

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