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>
15 #include <mm_communication.h>
17 static efi_uintn_t max_buffer_size; /* comm + var + func + data */
18 static efi_uintn_t max_payload_size; /* func + data */
20 struct mm_connection {
26 * get_connection() - Retrieve OP-TEE session for a specific UUID.
28 * @conn: session buffer to fill
31 static int get_connection(struct mm_connection *conn)
33 static const struct tee_optee_ta_uuid uuid = PTA_STMM_UUID;
34 struct udevice *tee = NULL;
35 struct tee_open_session_arg arg;
38 tee = tee_find_device(tee, NULL, NULL, NULL);
42 memset(&arg, 0, sizeof(arg));
43 tee_optee_ta_uuid_to_octets(arg.uuid, &uuid);
44 rc = tee_open_session(tee, &arg, 0, NULL);
47 conn->session = arg.session;
54 * optee_mm_communicate() - Pass a buffer to StandaloneMM running in OP-TEE
56 * @comm_buf: locally allocted communcation buffer
60 static efi_status_t optee_mm_communicate(void *comm_buf, ulong dsize)
64 struct efi_mm_communicate_header *mm_hdr;
65 struct mm_connection conn = { NULL, 0 };
66 struct tee_invoke_arg arg;
67 struct tee_param param[2];
68 struct tee_shm *shm = NULL;
72 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);
77 if (dsize != buf_size)
78 return EFI_INVALID_PARAMETER;
80 rc = get_connection(&conn);
82 log_err("Unable to open OP-TEE session (err=%d)\n", rc);
83 return EFI_UNSUPPORTED;
86 if (tee_shm_register(conn.tee, comm_buf, buf_size, 0, &shm)) {
87 log_err("Unable to register shared memory\n");
88 return EFI_UNSUPPORTED;
91 memset(&arg, 0, sizeof(arg));
92 arg.func = PTA_STMM_CMDID_COMMUNICATE;
93 arg.session = conn.session;
95 memset(param, 0, sizeof(param));
96 param[0].attr = TEE_PARAM_ATTR_TYPE_MEMREF_INOUT;
97 param[0].u.memref.size = buf_size;
98 param[0].u.memref.shm = shm;
99 param[1].attr = TEE_PARAM_ATTR_TYPE_VALUE_OUTPUT;
101 rc = tee_invoke_func(conn.tee, &arg, 2, param);
103 return EFI_INVALID_PARAMETER;
105 tee_close_session(conn.tee, conn.session);
107 switch (param[1].u.value.a) {
108 case ARM_SMC_MM_RET_SUCCESS:
112 case ARM_SMC_MM_RET_INVALID_PARAMS:
113 ret = EFI_INVALID_PARAMETER;
116 case ARM_SMC_MM_RET_DENIED:
117 ret = EFI_ACCESS_DENIED;
120 case ARM_SMC_MM_RET_NO_MEMORY:
121 ret = EFI_OUT_OF_RESOURCES;
125 ret = EFI_ACCESS_DENIED;
132 * mm_communicate() - Adjust the cmonnucation buffer to StandAlonneMM and send
135 * @comm_buf: locally allocted communcation buffer
136 * @dsize: buffer size
137 * Return: status code
139 static efi_status_t mm_communicate(u8 *comm_buf, efi_uintn_t dsize)
142 struct efi_mm_communicate_header *mm_hdr;
143 struct smm_variable_communicate_header *var_hdr;
145 dsize += MM_COMMUNICATE_HEADER_SIZE + MM_VARIABLE_COMMUNICATE_SIZE;
146 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
147 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
149 ret = optee_mm_communicate(comm_buf, dsize);
150 if (ret != EFI_SUCCESS) {
151 log_err("%s failed!\n", __func__);
155 return var_hdr->ret_status;
159 * setup_mm_hdr() - Allocate a buffer for StandAloneMM and initialize the
162 * @dptr: pointer address of the corresponding StandAloneMM
164 * @payload_size: buffer size
165 * @func: standAloneMM function number
166 * @ret: EFI return code
167 * Return: buffer or NULL
169 static u8 *setup_mm_hdr(void **dptr, efi_uintn_t payload_size,
170 efi_uintn_t func, efi_status_t *ret)
172 const efi_guid_t mm_var_guid = EFI_MM_VARIABLE_GUID;
173 struct efi_mm_communicate_header *mm_hdr;
174 struct smm_variable_communicate_header *var_hdr;
177 /* In the init function we initialize max_buffer_size with
178 * get_max_payload(). So skip the test if max_buffer_size is initialized
179 * StandAloneMM will perform similar checks and drop the buffer if it's
182 if (max_buffer_size && max_buffer_size <
183 (MM_COMMUNICATE_HEADER_SIZE +
184 MM_VARIABLE_COMMUNICATE_SIZE +
186 *ret = EFI_INVALID_PARAMETER;
190 comm_buf = calloc(1, MM_COMMUNICATE_HEADER_SIZE +
191 MM_VARIABLE_COMMUNICATE_SIZE +
194 *ret = EFI_OUT_OF_RESOURCES;
198 mm_hdr = (struct efi_mm_communicate_header *)comm_buf;
199 guidcpy(&mm_hdr->header_guid, &mm_var_guid);
200 mm_hdr->message_len = MM_VARIABLE_COMMUNICATE_SIZE + payload_size;
202 var_hdr = (struct smm_variable_communicate_header *)mm_hdr->data;
203 var_hdr->function = func;
205 *dptr = var_hdr->data;
212 * get_max_payload() - Get variable payload size from StandAloneMM.
214 * @size: size of the variable in storage
215 * Return: status code
217 efi_status_t EFIAPI get_max_payload(efi_uintn_t *size)
219 struct smm_variable_payload_size *var_payload = NULL;
220 efi_uintn_t payload_size;
225 ret = EFI_INVALID_PARAMETER;
229 payload_size = sizeof(*var_payload);
230 comm_buf = setup_mm_hdr((void **)&var_payload, payload_size,
231 SMM_VARIABLE_FUNCTION_GET_PAYLOAD_SIZE, &ret);
235 ret = mm_communicate(comm_buf, payload_size);
236 if (ret != EFI_SUCCESS)
239 *size = var_payload->size;
247 * efi_get_variable() - retrieve value of a UEFI variable
249 * This function implements the GetVariable runtime service.
251 * See the Unified Extensible Firmware Interface (UEFI) specification for
254 * @name: name of the variable
256 * @attr: attributes of the variable
257 * @data_size: size of the buffer to which the variable value is copied
258 * @data: buffer to which the variable value is copied
259 * Return: status code
261 efi_status_t EFIAPI efi_get_variable(u16 *name, const efi_guid_t *guid,
262 u32 *attr, efi_uintn_t *data_size,
265 struct smm_variable_access *var_acc;
266 efi_uintn_t payload_size;
267 efi_uintn_t name_size;
268 efi_uintn_t tmp_dsize;
272 EFI_ENTRY("\"%ls\" %pUl %p %p %p", name, guid, attr, data_size, data);
274 if (!name || !guid || !data_size) {
275 ret = EFI_INVALID_PARAMETER;
279 /* Check payload size */
280 name_size = u16_strsize(name);
281 if (name_size > max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
282 ret = EFI_INVALID_PARAMETER;
286 /* Trim output buffer size */
287 tmp_dsize = *data_size;
288 if (name_size + tmp_dsize >
289 max_payload_size - MM_VARIABLE_ACCESS_HEADER_SIZE) {
290 tmp_dsize = max_payload_size -
291 MM_VARIABLE_ACCESS_HEADER_SIZE -
295 /* Get communication buffer and initialize header */
296 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + tmp_dsize;
297 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
298 SMM_VARIABLE_FUNCTION_GET_VARIABLE, &ret);
302 /* Fill in contents */
303 guidcpy(&var_acc->guid, guid);
304 var_acc->data_size = tmp_dsize;
305 var_acc->name_size = name_size;
306 var_acc->attr = attr ? *attr : 0;
307 memcpy(var_acc->name, name, name_size);
310 ret = mm_communicate(comm_buf, payload_size);
311 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
312 /* Update with reported data size for trimmed case */
313 *data_size = var_acc->data_size;
315 if (ret != EFI_SUCCESS)
319 *attr = var_acc->attr;
321 memcpy(data, (u8 *)var_acc->name + var_acc->name_size,
324 ret = EFI_INVALID_PARAMETER;
328 return EFI_EXIT(ret);
332 * efi_get_next_variable_name() - enumerate the current variable names
334 * @variable_name_size: size of variable_name buffer in bytes
335 * @variable_name: name of uefi variable's name in u16
336 * @guid: vendor's guid
338 * This function implements the GetNextVariableName service.
340 * See the Unified Extensible Firmware Interface (UEFI) specification for
343 * Return: status code
345 efi_status_t EFIAPI efi_get_next_variable_name(efi_uintn_t *variable_name_size,
349 struct smm_variable_getnext *var_getnext;
350 efi_uintn_t payload_size;
351 efi_uintn_t out_name_size;
352 efi_uintn_t in_name_size;
353 efi_uintn_t tmp_dsize;
354 efi_uintn_t name_size;
358 EFI_ENTRY("%p \"%ls\" %pUl", variable_name_size, variable_name, guid);
360 if (!variable_name_size || !variable_name || !guid) {
361 ret = EFI_INVALID_PARAMETER;
365 out_name_size = *variable_name_size;
366 in_name_size = u16_strsize(variable_name);
368 if (out_name_size < in_name_size) {
369 ret = EFI_INVALID_PARAMETER;
373 name_size = u16_strsize(variable_name);
374 if (name_size > max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
375 ret = EFI_INVALID_PARAMETER;
379 /* Trim output buffer size */
380 tmp_dsize = *variable_name_size;
381 if (name_size + tmp_dsize >
382 max_payload_size - MM_VARIABLE_GET_NEXT_HEADER_SIZE) {
383 tmp_dsize = max_payload_size -
384 MM_VARIABLE_GET_NEXT_HEADER_SIZE -
388 payload_size = MM_VARIABLE_GET_NEXT_HEADER_SIZE + out_name_size;
389 comm_buf = setup_mm_hdr((void **)&var_getnext, payload_size,
390 SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME,
395 /* Fill in contents */
396 guidcpy(&var_getnext->guid, guid);
397 var_getnext->name_size = out_name_size;
398 memcpy(var_getnext->name, variable_name, in_name_size);
399 memset((u8 *)var_getnext->name + in_name_size, 0x0,
400 out_name_size - in_name_size);
403 ret = mm_communicate(comm_buf, payload_size);
404 if (ret == EFI_SUCCESS || ret == EFI_BUFFER_TOO_SMALL) {
405 /* Update with reported data size for trimmed case */
406 *variable_name_size = var_getnext->name_size;
408 if (ret != EFI_SUCCESS)
411 guidcpy(guid, &var_getnext->guid);
412 memcpy(variable_name, (u8 *)var_getnext->name,
413 var_getnext->name_size);
417 return EFI_EXIT(ret);
421 * efi_set_variable() - set value of a UEFI variable
423 * This function implements the SetVariable runtime service.
425 * See the Unified Extensible Firmware Interface (UEFI) specification for
428 * @name: name of the variable
430 * @attr: attributes of the variable
431 * @data_size: size of the buffer with the variable value
432 * @data: buffer with the variable value
433 * Return: status code
435 efi_status_t EFIAPI efi_set_variable(u16 *name, const efi_guid_t *guid,
436 u32 attr, efi_uintn_t data_size,
439 struct smm_variable_access *var_acc;
440 efi_uintn_t payload_size;
441 efi_uintn_t name_size;
445 EFI_ENTRY("\"%ls\" %pUl %x %zu %p", name, guid, attr, data_size, data);
447 if (!name || name[0] == 0 || !guid) {
448 ret = EFI_INVALID_PARAMETER;
451 if (data_size > 0 && !data) {
452 ret = EFI_INVALID_PARAMETER;
456 /* Check payload size */
457 name_size = u16_strsize(name);
458 payload_size = MM_VARIABLE_ACCESS_HEADER_SIZE + name_size + data_size;
459 if (payload_size > max_payload_size) {
460 ret = EFI_INVALID_PARAMETER;
464 /* Get communication buffer and initialize header */
465 comm_buf = setup_mm_hdr((void **)&var_acc, payload_size,
466 SMM_VARIABLE_FUNCTION_SET_VARIABLE, &ret);
470 /* Fill in contents */
471 guidcpy(&var_acc->guid, guid);
472 var_acc->data_size = data_size;
473 var_acc->name_size = name_size;
474 var_acc->attr = attr;
475 memcpy(var_acc->name, name, name_size);
476 memcpy((u8 *)var_acc->name + name_size, data, data_size);
479 ret = mm_communicate(comm_buf, payload_size);
483 return EFI_EXIT(ret);
487 * efi_query_variable_info() - get information about EFI variables
489 * This function implements the QueryVariableInfo() runtime service.
491 * See the Unified Extensible Firmware Interface (UEFI) specification for
494 * @attributes: bitmask to select variables to be
496 * @maximum_variable_storage_size: maximum size of storage area for the
497 * selected variable types
498 * @remaining_variable_storage_size: remaining size of storage are for the
499 * selected variable types
500 * @maximum_variable_size: maximum size of a variable of the
502 * Returns: status code
504 efi_status_t EFIAPI __efi_runtime
505 efi_query_variable_info(u32 attributes, u64 *max_variable_storage_size,
506 u64 *remain_variable_storage_size,
507 u64 *max_variable_size)
509 struct smm_variable_query_info *mm_query_info;
510 efi_uintn_t payload_size;
514 EFI_ENTRY("%x %p %p %p", attributes, max_variable_storage_size,
515 remain_variable_storage_size, max_variable_size);
517 payload_size = sizeof(*mm_query_info);
518 comm_buf = setup_mm_hdr((void **)&mm_query_info, payload_size,
519 SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO,
524 mm_query_info->attr = attributes;
525 ret = mm_communicate(comm_buf, payload_size);
526 if (ret != EFI_SUCCESS)
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;
535 return EFI_EXIT(ret);
539 * efi_get_variable_runtime() - runtime implementation of GetVariable()
541 * @variable_name: name of the variable
543 * @attributes: attributes of the variable
544 * @data_size: size of the buffer to which the variable value is copied
545 * @data: buffer to which the variable value is copied
546 * Return: status code
548 static efi_status_t __efi_runtime EFIAPI
549 efi_get_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
550 u32 *attributes, efi_uintn_t *data_size, void *data)
552 return EFI_UNSUPPORTED;
556 * efi_get_next_variable_name_runtime() - runtime implementation of
559 * @variable_name_size: size of variable_name buffer in byte
560 * @variable_name: name of uefi variable's name in u16
561 * @guid: vendor's guid
562 * Return: status code
564 static efi_status_t __efi_runtime EFIAPI
565 efi_get_next_variable_name_runtime(efi_uintn_t *variable_name_size,
566 u16 *variable_name, efi_guid_t *guid)
568 return EFI_UNSUPPORTED;
572 * efi_query_variable_info() - get information about EFI variables
574 * This function implements the QueryVariableInfo() runtime service.
576 * See the Unified Extensible Firmware Interface (UEFI) specification for
579 * @attributes: bitmask to select variables to be
581 * @maximum_variable_storage_size: maximum size of storage area for the
582 * selected variable types
583 * @remaining_variable_storage_size: remaining size of storage are for the
584 * selected variable types
585 * @maximum_variable_size: maximum size of a variable of the
587 * Return: status code
589 efi_status_t EFIAPI __efi_runtime
590 efi_query_variable_info_runtime(u32 attributes, u64 *max_variable_storage_size,
591 u64 *remain_variable_storage_size,
592 u64 *max_variable_size)
594 return EFI_UNSUPPORTED;
598 * efi_set_variable_runtime() - runtime implementation of SetVariable()
600 * @variable_name: name of the variable
602 * @attributes: attributes of the variable
603 * @data_size: size of the buffer with the variable value
604 * @data: buffer with the variable value
605 * Return: status code
607 static efi_status_t __efi_runtime EFIAPI
608 efi_set_variable_runtime(u16 *variable_name, const efi_guid_t *guid,
609 u32 attributes, efi_uintn_t data_size,
612 return EFI_UNSUPPORTED;
616 * efi_variables_boot_exit_notify() - notify ExitBootServices() is called
618 void efi_variables_boot_exit_notify(void)
623 comm_buf = setup_mm_hdr(NULL, 0,
624 SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE, &ret);
626 ret = mm_communicate(comm_buf, 0);
630 if (ret != EFI_SUCCESS)
631 log_err("Unable to notify StMM for ExitBootServices\n");
634 /* Update runtime service table */
635 efi_runtime_services.query_variable_info =
636 efi_query_variable_info_runtime;
637 efi_runtime_services.get_variable = efi_get_variable_runtime;
638 efi_runtime_services.get_next_variable_name =
639 efi_get_next_variable_name_runtime;
640 efi_runtime_services.set_variable = efi_set_variable_runtime;
641 efi_update_table_header_crc32(&efi_runtime_services.hdr);
645 * efi_init_variables() - initialize variable services
647 * Return: status code
649 efi_status_t efi_init_variables(void)
653 ret = get_max_payload(&max_payload_size);
654 if (ret != EFI_SUCCESS)
657 max_buffer_size = MM_COMMUNICATE_HEADER_SIZE +
658 MM_VARIABLE_COMMUNICATE_SIZE +