1 // SPDX-License-Identifier: GPL-2.0+
3 * EFI variable service via OP-TEE
5 * Copyright (C) 2019 Linaro Ltd. <sughosh.ganu@linaro.org>
6 * Copyright (C) 2019 Linaro Ltd. <ilias.apalodimas@linaro.org>
12 #include <efi_loader.h>
13 #include <efi_variable.h>
16 #include <mm_communication.h>
18 extern struct efi_var_file __efi_runtime_data *efi_var_buf;
19 static efi_uintn_t max_buffer_size; /* comm + var + func + data */
20 static efi_uintn_t max_payload_size; /* func + data */
22 struct mm_connection {
28 * get_connection() - Retrieve OP-TEE session for a specific UUID.
30 * @conn: session buffer to fill
33 static int get_connection(struct mm_connection *conn)
35 static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
36 struct udevice *tee = NULL;
37 struct tee_open_session_arg arg;
40 tee = tee_find_device(tee, NULL, NULL, NULL);
44 memset(&arg, 0, sizeof(arg));
45 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
46 rc = tee_open_session(tee, &arg, 0, NULL);
50 /* Check the internal OP-TEE result */
51 if (arg.ret != TEE_SUCCESS) {
57 conn->session = arg.session;
65 * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
67 * @comm_buf: locally allocted communcation buffer
71 static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
75 struct efi_mm_communicate_header *mm_hdr;
76 struct mm_connection conn = { NULL, 0 };
77 struct tee_invoke_arg arg;
78 struct tee_param param[2];
79 struct tee_shm *shm = NULL;
83 return EFI_INVALID_PARAMETER;
85 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
86 buf_size = mm_hdr->message_len + sizeof(efi_guid_t) + sizeof(size_t);
88 if (dsize != buf_size)
89 return EFI_INVALID_PARAMETER;
91 rc = get_connection(&conn);
93 log_err("Unable to open OP-TEE session (err=%d)\n", rc);
94 return EFI_UNSUPPORTED;
97 if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
98 log_err("Unable to register shared memory\n");
99 tee_close_session(conn.tee, conn.session);
100 return EFI_UNSUPPORTED;
103 memset(&arg, 0, sizeof(arg));
104 arg.func = PTA_STMM_CMDID_COMMUNICATE;
105 arg.session = conn.session;
107 memset(param, 0, sizeof(param));
108 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
109 param[0].u.memref.size = buf_size;
110 param[0].u.memref.shm = shm;
111 param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
113 rc = tee_invoke_func(conn.tee, &arg, 2, param);
115 tee_close_session(conn.tee, conn.session);
117 return EFI_DEVICE_ERROR;
118 if (arg.ret == TEE_ERROR_EXCESS_DATA)
119 log_err("Variable payload too large\n");
120 if (arg.ret != TEE_SUCCESS)
121 return EFI_DEVICE_ERROR;
123 switch (param[1].u.value.a) {
124 case ARM_SVC_SPM_RET_SUCCESS:
128 case ARM_SVC_SPM_RET_INVALID_PARAMS:
129 ret = EFI_INVALID_PARAMETER;
132 case ARM_SVC_SPM_RET_DENIED:
133 ret = EFI_ACCESS_DENIED;
136 case ARM_SVC_SPM_RET_NO_MEMORY:
137 ret = EFI_OUT_OF_RESOURCES;
141 ret = EFI_ACCESS_DENIED;
148 * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send
151 * @comm_buf: locally allocted communcation buffer
152 * @dsize: buffer size
153 * Return: status code
155 static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
158 struct efi_mm_communicate_header *mm_hdr;
159 struct smm_variable_communicate_header *var_hdr;
161 dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
162 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
163 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
165 ret = optee_mm_communicate(comm_buf, dsize);
166 if (ret != EFI_SUCCESS) {
167 log_err("%s failed!\n", __func__);
171 return var_hdr->ret_status;
175 * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the
178 * @dptr: pointer address of the corresponding StandAloneMM
180 * @payload_size: buffer size
181 * @func: standAloneMM function number
182 * @ret: EFI return code
183 * Return: buffer or NULL
185 static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
186 efi_uintn_t func, efi_status_t *ret)
188 const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
189 struct efi_mm_communicate_header *mm_hdr;
190 struct smm_variable_communicate_header *var_hdr;
193 /* In the init function we initialize max_buffer_size with
194 * get_max_payload(). So skip the test if max_buffer_size is initialized
195 * StandAloneMM will perform similar checks and drop the buffer if it's
198 if (max_buffer_size && max_buffer_size <
199 (MM_COMMUNICATE_HEADER_SIZE +
200 MM_VARIABLE_COMMUNICATE_SIZE +
202 *ret = EFI_INVALID_PARAMETER;
206 comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
207 MM_VARIABLE_COMMUNICATE_SIZE +
210 *ret = EFI_OUT_OF_RESOURCES;
214 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
215 guidcpy(&mm_hdr->header_guid, &mm_var_guid);
216 mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
218 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
219 var_hdr->function = func;
221 *dptr = var_hdr->data;
228 * get_max_payload() - Get variable payload size from StandAloneMM.
230 * @size: size of the variable in storage
231 * Return: status code
233 efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
235 struct smm_variable_payload_size *var_payload = NULL;
236 efi_uintn_t payload_size;
241 ret = EFI_INVALID_PARAMETER;
245 payload_size = sizeof(*var_payload);
246 comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
247 SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
251 ret = mm_communicate(comm_buf, payload_size);
252 if (ret != EFI_SUCCESS)
255 /* Make sure the buffer is big enough for storing variables */
256 if (var_payload->size < MM_VARIABLE_ACCESS_HEADER_SIZE + 0x20) {
257 ret = EFI_DEVICE_ERROR;
260 *size = var_payload->size;
262 * There seems to be a bug in EDK2 miscalculating the boundaries and
263 * size checks, so deduct 2 more bytes to fulfill this requirement. Fix
264 * it up here to ensure backwards compatibility with older versions
265 * (cf. StandaloneMmPkg/Drivers/StandaloneMmCpu/AArch64/EventHandle.c.
266 * sizeof (EFI_MM_COMMUNICATE_HEADER) instead the size minus the
267 * flexible array member).
269 * size is guaranteed to be > 2 due to checks on the beginning.
278 * StMM can store internal attributes and properties for variables, i.e enabling
281 static efi_status_t set_property_int(const u16 *variable_name,
282 efi_uintn_t name_size,
283 const efi_guid_t *vendor,
284 struct var_check_property *var_property)
286 struct smm_variable_var_check_property *smm_property;
287 efi_uintn_t payload_size;
291 payload_size = sizeof(*smm_property) + name_size;
292 if (payload_size > max_payload_size) {
293 ret = EFI_INVALID_PARAMETER;
296 comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
297 SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_SET,
302 guidcpy(&smm_property->guid, vendor);
303 smm_property->name_size = name_size;
304 memcpy(&smm_property->property, var_property,
305 sizeof(smm_property->property));
306 memcpy(smm_property->name, variable_name, name_size);
308 ret = mm_communicate(comm_buf, payload_size);
315 static efi_status_t get_property_int(const u16 *variable_name,
316 efi_uintn_t name_size,
317 const efi_guid_t *vendor,
318 struct var_check_property *var_property)
320 struct smm_variable_var_check_property *smm_property;
321 efi_uintn_t payload_size;
325 memset(var_property, 0, sizeof(*var_property));
326 payload_size = sizeof(*smm_property) + name_size;
327 if (payload_size > max_payload_size) {
328 ret = EFI_INVALID_PARAMETER;
331 comm_buf = setup_mm_hdr((void **)&smm_property, payload_size,
332 SMM_VARIABLE_FUNCTION_VAR_CHECK_VARIABLE_PROPERTY_GET,
337 guidcpy(&smm_property->guid, vendor);
338 smm_property->name_size = name_size;
339 memcpy(smm_property->name, variable_name, name_size);
341 ret = mm_communicate(comm_buf, payload_size);
343 * Currently only R/O property is supported in StMM.
344 * Variables that are not set to R/O will not set the property in StMM
345 * and the call will return EFI_NOT_FOUND. We are setting the
346 * properties to 0x0 so checking against that is enough for the
347 * EFI_NOT_FOUND case.
349 if (ret == EFI_NOT_FOUND)
351 if (ret != EFI_SUCCESS)
353 memcpy(var_property, &smm_property->property, sizeof(*var_property));
360 efi_status_t efi_get_variable_int(const u16 *variable_name,
361 const efi_guid_t *vendor,
362 u32 *attributes, efi_uintn_t *data_size,
363 void *data, u64 *timep)
365 struct var_check_property var_property;
366 struct smm_variable_access *var_acc;
367 efi_uintn_t payload_size;
368 efi_uintn_t name_size;
369 efi_uintn_t tmp_dsize;
371 efi_status_t ret, tmp;
373 if (!variable_name || !vendor || !data_size) {
374 ret = EFI_INVALID_PARAMETER;
378 /* Check payload size */
379 name_size = u16_strsize(variable_name);
380 if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
381 ret = EFI_INVALID_PARAMETER;
385 /* Trim output buffer size */
386 tmp_dsize = *data_size;
387 if (name_size + tmp_dsize >
388 max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
389 tmp_dsize = max_payload_size -
390 MM_VARIABLE_ACCESS_HEADER_SIZE -
394 /* Get communication buffer and initialize header */
395 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
396 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
397 SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
401 /* Fill in contents */
402 guidcpy(&var_acc->guid, vendor);
403 var_acc->data_size = tmp_dsize;
404 var_acc->name_size = name_size;
405 var_acc->attr = attributes ? *attributes : 0;
406 memcpy(var_acc->name, variable_name, name_size);
409 ret = mm_communicate(comm_buf, payload_size);
410 if (ret != EFI_SUCCESS && ret != EFI_BUFFER_TOO_SMALL)
413 /* Update with reported data size for trimmed case */
414 *data_size = var_acc->data_size;
416 * UEFI > 2.7 needs the attributes set even if the buffer is
420 tmp = get_property_int(variable_name, name_size, vendor,
422 if (tmp != EFI_SUCCESS) {
426 *attributes = var_acc->attr;
427 if (var_property.property &
428 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)
429 *attributes |= EFI_VARIABLE_READ_ONLY;
432 /* return if ret is EFI_BUFFER_TOO_SMALL */
433 if (ret != EFI_SUCCESS)
437 memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
440 ret = EFI_INVALID_PARAMETER;
447 efi_status_t efi_get_next_variable_name_int(efi_uintn_t *variable_name_size,
451 struct smm_variable_getnext *var_getnext;
452 efi_uintn_t payload_size;
453 efi_uintn_t out_name_size;
454 efi_uintn_t in_name_size;
458 if (!variable_name_size || !variable_name || !guid) {
459 ret = EFI_INVALID_PARAMETER;
463 out_name_size = *variable_name_size;
464 in_name_size = u16_strsize(variable_name);
466 if (out_name_size < in_name_size) {
467 ret = EFI_INVALID_PARAMETER;
471 if (in_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
472 ret = EFI_INVALID_PARAMETER;
476 /* Trim output buffer size */
477 if (out_name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE)
478 out_name_size = max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE;
480 payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
481 comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
482 SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
487 /* Fill in contents */
488 guidcpy(&var_getnext->guid, guid);
489 var_getnext->name_size = out_name_size;
490 memcpy(var_getnext->name, variable_name, in_name_size);
491 memset((u8 *)var_getnext->name + in_name_size, 0x0,
492 out_name_size - in_name_size);
495 ret = mm_communicate(comm_buf, payload_size);
496 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
497 /* Update with reported data size for trimmed case */
498 *variable_name_size = var_getnext->name_size;
500 if (ret != EFI_SUCCESS)
503 guidcpy(guid, &var_getnext->guid);
504 memcpy(variable_name, var_getnext->name, var_getnext->name_size);
511 efi_status_t efi_set_variable_int(const u16 *variable_name,
512 const efi_guid_t *vendor, u32 attributes,
513 efi_uintn_t data_size, const void *data,
516 efi_status_t ret, alt_ret = EFI_SUCCESS;
517 struct var_check_property var_property;
518 struct smm_variable_access *var_acc;
519 efi_uintn_t payload_size;
520 efi_uintn_t name_size;
524 if (!variable_name || variable_name[0] == 0 || !vendor) {
525 ret = EFI_INVALID_PARAMETER;
528 if (data_size > 0 && !data) {
529 ret = EFI_INVALID_PARAMETER;
532 /* Check payload size */
533 name_size = u16_strsize(variable_name);
534 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
535 if (payload_size > max_payload_size) {
536 ret = EFI_INVALID_PARAMETER;
541 * Allocate the buffer early, before switching to RW (if needed)
542 * so we won't need to account for any failures in reading/setting
543 * the properties, if the allocation fails
545 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
546 SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
550 ro = !!(attributes & EFI_VARIABLE_READ_ONLY);
551 attributes &= EFI_VARIABLE_MASK;
554 * The API has the ability to override RO flags. If no RO check was
555 * requested switch the variable to RW for the duration of this call
557 ret = get_property_int(variable_name, name_size, vendor,
559 if (ret != EFI_SUCCESS)
562 if (var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) {
563 /* Bypass r/o check */
565 var_property.property &= ~VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
566 ret = set_property_int(variable_name, name_size, vendor, &var_property);
567 if (ret != EFI_SUCCESS)
570 ret = EFI_WRITE_PROTECTED;
575 /* Fill in contents */
576 guidcpy(&var_acc->guid, vendor);
577 var_acc->data_size = data_size;
578 var_acc->name_size = name_size;
579 var_acc->attr = attributes;
580 memcpy(var_acc->name, variable_name, name_size);
581 memcpy((u8 *)var_acc->name + name_size, data, data_size);
584 ret = mm_communicate(comm_buf, payload_size);
585 if (ret != EFI_SUCCESS)
588 if (ro && !(var_property.property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY)) {
589 var_property.revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
590 var_property.property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
591 var_property.attributes = attributes;
592 var_property.minsize = 1;
593 var_property.maxsize = var_acc->data_size;
594 ret = set_property_int(variable_name, name_size, vendor, &var_property);
597 if (alt_ret != EFI_SUCCESS)
600 if (!u16_strcmp(variable_name, u"PK"))
601 alt_ret = efi_init_secure_state();
604 return alt_ret == EFI_SUCCESS ? ret : alt_ret;
607 efi_status_t efi_query_variable_info_int(u32 attributes,
608 u64 *max_variable_storage_size,
609 u64 *remain_variable_storage_size,
610 u64 *max_variable_size)
612 struct smm_variable_query_info *mm_query_info;
613 efi_uintn_t payload_size;
617 payload_size = sizeof(*mm_query_info);
618 comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
619 SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
624 mm_query_info->attr = attributes;
625 ret = mm_communicate(comm_buf, payload_size);
626 if (ret != EFI_SUCCESS)
628 *max_variable_storage_size = mm_query_info->max_variable_storage;
629 *remain_variable_storage_size =
630 mm_query_info->remaining_variable_storage;
631 *max_variable_size = mm_query_info->max_variable_size;
639 * efi_query_variable_info() - get information about EFI variables
641 * This function implements the QueryVariableInfo() runtime service.
643 * See the Unified Extensible Firmware Interface (UEFI) specification for
646 * @attributes: bitmask to select variables to be
648 * @maximum_variable_storage_size: maximum size of storage area for the
649 * selected variable types
650 * @remaining_variable_storage_size: remaining size of storage are for the
651 * selected variable types
652 * @maximum_variable_size: maximum size of a variable of the
654 * Return: status code
656 efi_status_t EFIAPI __efi_runtime
657 efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
658 u64 *remain_variable_storage_size,
659 u64 *max_variable_size)
661 return EFI_UNSUPPORTED;
665 * efi_set_variable_runtime() - runtime implementation of SetVariable()
667 * @variable_name: name of the variable
669 * @attributes: attributes of the variable
670 * @data_size: size of the buffer with the variable value
671 * @data: buffer with the variable value
672 * Return: status code
674 static efi_status_t __efi_runtime EFIAPI
675 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
676 u32 attributes, efi_uintn_t data_size,
679 return EFI_UNSUPPORTED;
683 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
685 void efi_variables_boot_exit_notify(void)
690 struct efi_var_file *var_buf;
692 comm_buf = setup_mm_hdr(NULL, 0,
693 SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
695 ret = mm_communicate(comm_buf, 0);
699 if (ret != EFI_SUCCESS)
700 log_err("Unable to notify StMM for ExitBootServices\n");
704 * Populate the list for runtime variables.
705 * asking EFI_VARIABLE_RUNTIME_ACCESS is redundant, since
706 * efi_var_mem_notify_exit_boot_services will clean those, but that's fine
708 ret = efi_var_collect(&var_buf, &len, EFI_VARIABLE_RUNTIME_ACCESS);
709 if (ret != EFI_SUCCESS)
710 log_err("Can't populate EFI variables. No runtime variables will be available\n");
712 efi_var_buf_update(var_buf);
715 /* Update runtime service table */
716 efi_runtime_services.query_variable_info =
717 efi_query_variable_info_runtime;
718 efi_runtime_services.get_variable = efi_get_variable_runtime;
719 efi_runtime_services.get_next_variable_name =
720 efi_get_next_variable_name_runtime;
721 efi_runtime_services.set_variable = efi_set_variable_runtime;
722 efi_update_table_header_crc32(&efi_runtime_services.hdr);
726 * efi_init_variables() - initialize variable services
728 * Return: status code
730 efi_status_t efi_init_variables(void)
734 /* Create a cached copy of the variables that will be enabled on ExitBootServices() */
735 ret = efi_var_mem_init();
736 if (ret != EFI_SUCCESS)
739 ret = get_max_payload(&max_payload_size);
740 if (ret != EFI_SUCCESS)
743 max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
744 MM_VARIABLE_COMMUNICATE_SIZE +
747 ret = efi_init_secure_state();
748 if (ret != EFI_SUCCESS)