efi_var_file: refactor to move buffer functions

Currently efi_var_file.c has functions to store/read
EFI variables to/from memory buffer. These functions
can be used with other EFI variable stores so move
them out to efi_var_common.c

Signed-off-by: Shantur Rathore <i@shantur.com>
Signed-off-by: Michal Simek <michal.simek@amd.com>
Tested-by: Neil Armstrong <neil.armstrong@linaro.org> # on AML-S905D3-CC
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Reviewed-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
This commit is contained in:
Shantur Rathore
2026-02-11 16:56:20 +01:00
committed by Heinrich Schuchardt
parent fd2d986876
commit 38ea3bfc65
5 changed files with 60 additions and 63 deletions

View File

@@ -161,6 +161,11 @@ efi_status_t efi_var_to_file(void);
efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *lenp,
u32 check_attr_mask);
/* GUID used by Shim to store the MOK database */
#define SHIM_LOCK_GUID \
EFI_GUID(0x605dab50, 0xe046, 0x4300, \
0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
/**
* efi_var_restore() - restore EFI variables from buffer
*

View File

@@ -53,7 +53,7 @@ ifeq ($(CONFIG_EFI_MM_COMM_TEE),y)
obj-y += efi_variable_tee.o
else
obj-y += efi_variable.o
obj-y += efi_var_file.o
obj-$(CONFIG_EFI_VARIABLE_FILE_STORE) += efi_var_file.o
obj-$(CONFIG_EFI_VARIABLES_PRESEED) += efi_var_seed.o
endif
obj-y += efi_watchdog.o

View File

@@ -41,6 +41,7 @@ static const struct efi_auth_var_name_type name_type[] = {
static bool efi_secure_boot;
static enum efi_secure_mode efi_secure_mode;
static const efi_guid_t shim_lock_guid = SHIM_LOCK_GUID;
/**
* efi_efi_get_variable() - retrieve value of a UEFI variable
@@ -488,3 +489,44 @@ efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *
return EFI_SUCCESS;
}
efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe)
{
struct efi_var_entry *var, *last_var;
u16 *data;
efi_status_t ret;
if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC ||
buf->crc32 != crc32(0, (u8 *)buf->var,
buf->length - sizeof(struct efi_var_file))) {
log_err("Invalid EFI variables file\n");
return EFI_INVALID_PARAMETER;
}
last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
for (var = buf->var; var < last_var;
var = (struct efi_var_entry *)ALIGN((uintptr_t)data + var->length, 8)) {
data = var->name + u16_strlen(var->name) + 1;
/*
* Secure boot related and volatile variables shall only be
* restored from U-Boot's preseed.
*/
if (!safe &&
(efi_auth_var_get_type(var->name, &var->guid) !=
EFI_AUTH_VAR_NONE ||
!guidcmp(&var->guid, &shim_lock_guid) ||
!(var->attr & EFI_VARIABLE_NON_VOLATILE)))
continue;
if (!var->length)
continue;
if (efi_var_mem_find(&var->guid, var->name, NULL))
continue;
ret = efi_var_mem_ins(var->name, &var->guid, var->attr,
var->length, data, 0, NULL,
var->time);
if (ret != EFI_SUCCESS)
log_err("Failed to set EFI variable %ls\n", var->name);
}
return EFI_SUCCESS;
}

View File

@@ -14,17 +14,9 @@
#include <mapmem.h>
#include <efi_loader.h>
#include <efi_variable.h>
#include <u-boot/crc.h>
#define PART_STR_LEN 10
/* GUID used by Shim to store the MOK database */
#define SHIM_LOCK_GUID \
EFI_GUID(0x605dab50, 0xe046, 0x4300, \
0xab, 0xb6, 0x3d, 0xd8, 0x10, 0xdd, 0x8b, 0x23)
static const efi_guid_t shim_lock_guid = SHIM_LOCK_GUID;
/**
* efi_set_blk_dev_to_system_partition() - select EFI system partition
*
@@ -59,7 +51,6 @@ static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void)
*/
efi_status_t efi_var_to_file(void)
{
#ifdef CONFIG_EFI_VARIABLE_FILE_STORE
efi_status_t ret;
struct efi_var_file *buf;
loff_t len;
@@ -91,52 +82,6 @@ error:
out:
free(buf);
return ret;
#else
return EFI_SUCCESS;
#endif
}
efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe)
{
struct efi_var_entry *var, *last_var;
u16 *data;
efi_status_t ret;
if (buf->reserved || buf->magic != EFI_VAR_FILE_MAGIC ||
buf->crc32 != crc32(0, (u8 *)buf->var,
buf->length - sizeof(struct efi_var_file))) {
log_err("Invalid EFI variables file\n");
return EFI_INVALID_PARAMETER;
}
last_var = (struct efi_var_entry *)((u8 *)buf + buf->length);
for (var = buf->var; var < last_var;
var = (struct efi_var_entry *)
ALIGN((uintptr_t)data + var->length, 8)) {
data = var->name + u16_strlen(var->name) + 1;
/*
* Secure boot related and volatile variables shall only be
* restored from U-Boot's preseed.
*/
if (!safe &&
(efi_auth_var_get_type(var->name, &var->guid) !=
EFI_AUTH_VAR_NONE ||
!guidcmp(&var->guid, &shim_lock_guid) ||
!(var->attr & EFI_VARIABLE_NON_VOLATILE)))
continue;
if (!var->length)
continue;
if (efi_var_mem_find(&var->guid, var->name, NULL))
continue;
ret = efi_var_mem_ins(var->name, &var->guid, var->attr,
var->length, data, 0, NULL,
var->time);
if (ret != EFI_SUCCESS)
log_err("Failed to set EFI variable %ls\n", var->name);
}
return EFI_SUCCESS;
}
/**
@@ -155,7 +100,6 @@ efi_status_t efi_var_restore(struct efi_var_file *buf, bool safe)
*/
efi_status_t efi_var_from_file(void)
{
#ifdef CONFIG_EFI_VARIABLE_FILE_STORE
struct efi_var_file *buf;
loff_t len;
efi_status_t ret;
@@ -180,6 +124,5 @@ efi_status_t efi_var_from_file(void)
log_err("Invalid EFI variables file\n");
error:
free(buf);
#endif
return EFI_SUCCESS;
}

View File

@@ -397,11 +397,15 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,
ret = EFI_SUCCESS;
/*
* Write non-volatile EFI variables to file
* Write non-volatile EFI variables
* TODO: check if a value change has occured to avoid superfluous writes
*/
if (attributes & EFI_VARIABLE_NON_VOLATILE)
if (attributes & EFI_VARIABLE_NON_VOLATILE) {
if (IS_ENABLED(CONFIG_EFI_VARIABLE_NO_STORE))
return EFI_SUCCESS;
efi_var_to_file();
}
return EFI_SUCCESS;
}
@@ -594,9 +598,12 @@ efi_status_t efi_init_variables(void)
if (ret != EFI_SUCCESS)
return ret;
ret = efi_var_from_file();
if (ret != EFI_SUCCESS)
return ret;
if (!IS_ENABLED(CONFIG_EFI_VARIABLE_NO_STORE)) {
ret = efi_var_from_file();
if (ret != EFI_SUCCESS)
return ret;
}
if (IS_ENABLED(CONFIG_EFI_VARIABLES_PRESEED)) {
ret = efi_var_restore((struct efi_var_file *)
__efi_var_file_begin, true);