diff mbox series

efi_loader: avoid adding variables twice

Message ID 20221229065528.421350-1-ilias.apalodimas@linaro.org
State Superseded
Headers show
Series efi_loader: avoid adding variables twice | expand

Commit Message

Ilias Apalodimas Dec. 29, 2022, 6:55 a.m. UTC
When the efi subsystem starts we restore variables that are both in a
file or stored into the .efi_runtime section of U-Boot.  However once
a variable gets created or changed the preseeded entries will end up in
the file.  As a consequence on the next boot we will end up adding
identical variable entries twice.

Fix this by checking if the to be inserted variable already exists.
Also swap the restoration order and start with the file instead of the
builtin variables,  so a user can replace the preseeded ones if needed.

Tested-by: Leo Yan <leo.yan@linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
---
 lib/efi_loader/efi_var_mem.c  | 3 +++
 lib/efi_loader/efi_variable.c | 6 +++---
 2 files changed, 6 insertions(+), 3 deletions(-)

Comments

Heinrich Schuchardt Dec. 29, 2022, 8:16 a.m. UTC | #1
On 12/29/22 07:55, Ilias Apalodimas wrote:
> When the efi subsystem starts we restore variables that are both in a
> file or stored into the .efi_runtime section of U-Boot.  However once
> a variable gets created or changed the preseeded entries will end up in
> the file.  As a consequence on the next boot we will end up adding
> identical variable entries twice.
>
> Fix this by checking if the to be inserted variable already exists.
> Also swap the restoration order and start with the file instead of the
> builtin variables,  so a user can replace the preseeded ones if needed.
>
> Tested-by: Leo Yan <leo.yan@linaro.org>
> Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>

With this version updating variables does not work anymore.

Thanks for sending v2.

Best regards

Heinrich
diff mbox series

Patch

diff --git a/lib/efi_loader/efi_var_mem.c b/lib/efi_loader/efi_var_mem.c
index 0bac594e004d..17a2ad2e4951 100644
--- a/lib/efi_loader/efi_var_mem.c
+++ b/lib/efi_loader/efi_var_mem.c
@@ -144,6 +144,9 @@  efi_status_t __efi_runtime efi_var_mem_ins(
 	struct efi_var_entry *var;
 	u32 var_name_len;
 
+	if (efi_var_mem_find(vendor, variable_name, NULL))
+		return EFI_SUCCESS;
+
 	var = (struct efi_var_entry *)
 	      ((uintptr_t)efi_var_buf + efi_var_buf->length);
 	for (var_name_len = 0; variable_name[var_name_len]; ++var_name_len)
diff --git a/lib/efi_loader/efi_variable.c b/lib/efi_loader/efi_variable.c
index 8ca2d85694c8..503a33ed65c5 100644
--- a/lib/efi_loader/efi_variable.c
+++ b/lib/efi_loader/efi_variable.c
@@ -425,6 +425,9 @@  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_VARIABLES_PRESEED)) {
 		ret = efi_var_restore((struct efi_var_file *)
 				      __efi_var_file_begin, true);
@@ -432,9 +435,6 @@  efi_status_t efi_init_variables(void)
 			log_err("Invalid EFI variable seed\n");
 	}
 
-	ret = efi_var_from_file();
-	if (ret != EFI_SUCCESS)
-		return ret;
 
 	return efi_init_secure_state();
 }