efi_vars: Implement SPI Flash storage for EFI
v5 cover efi_vars: Implement SPI Flash storage for EFI
This is updated series based on v3 version sent here
https://lore.kernel.org/all/[email protected]/
that's why I am continuing on v4 instead of starting from scratch.
Tested on kv260 with saving variables to location where User MTD partition is.CONFIG_EFI_VARIABLE_SF_STORE=yCONFIG_EFI_RT_VOLATILE_STORE=y CONFIG_EFI_VARIABLE_SF_OFFSET=0x22a0000
In Linux:
root@som:~# mount | grep efivars
none on /sys/firmware/efi/efivars type efivarfs (rw,relatime)
root@som:~# printf '\x07\x00\x00\x00Hello' \
| sudo tee /sys/firmware/efi/efivars/TestVar-12345678-1234-1234-1234-123456789abc
Helloroot@som:~#
root@som:~# dd if=/sys/firmware/efi/efivars/VarToFile-b2ac5fc9-92b7-4acd-aeac-11e818c3130c \
of=/tmp/vars skip=4 bs=1
1352+0 records in
1352+0 records out
1352 bytes (1.4 kB, 1.3 KiB) copied, 0.380992 s, 3.5 kB/s
root@som:~# flashcp /tmp/vars /dev/mtd16
root@som:~# rebootReboot and then in U-Boot:
ZynqMP> pri -e
...
TestVar:
12345678-1234-1234-1234-123456789abc (12345678-1234-1234-1234-123456789abc)
NV|BS|RT, DataSize = 0x5
00000000: 48 65 6c 6c 6f Hello...
Thanks,
Michal
Changes in v5:
- Invert logic in efi_variable.c and avoid #if
Changes in v4:
- New patch based on review comments from v3
- Extend Kconfig description
- Extend commit message and describe efivar missing part
- use unify methods for reading/writing variable
Changes in v3:
- Fixed compiler warnings.
Changes in v2:
- Refactored efi_var_file to move common parts out as requested
- Changed ifdefs to use CONFIG_IS_DEFINED
- Fixed typos
Michal Simek (1):
efi_var: Unify read/write access helper function
Shantur Rathore (2):
efi_var_file: refactor to move buffer functions
efi_vars: Implement SPI Flash store
include/efi_variable.h | 18 +++---- lib/efi_loader/Kconfig | 34 +++++++++++- lib/efi_loader/Makefile | 3 +- lib/efi_loader/efi_var_common.c | 42 +++++++++++++++ lib/efi_loader/efi_var_file.c | 65 ++--------------------- lib/efi_loader/efi_var_sf.c | 92 +++++++++++++++++++++++++++++++++ lib/efi_loader/efi_variable.c | 19 ++++--- 7 files changed, 195 insertions(+), 78 deletions(-) create mode 100644 lib/efi_loader/efi_var_sf.c
Signature · 4 lines
2.43.0 base-commit: f1e312364c7ea2bc4936472dda22b8c0447cdb03 branch: debian-sent3
PATCH v5 1/3 efi_var_file: refactor to move buffer functions
From: Shantur Rathore <[email protected]>
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
---
Changes in v5:
- Invert logic in efi_variable.c and avoid #if
include/efi_variable.h | 5 +++ lib/efi_loader/Makefile | 2 +- lib/efi_loader/efi_var_common.c | 42 ++++++++++++++++++++++++ lib/efi_loader/efi_var_file.c | 57 --------------------------------- lib/efi_loader/efi_variable.c | 17 +++++++--- 5 files changed, 60 insertions(+), 63 deletions(-)
include/efi_variable.h +5 -0
@@ -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 *
lib/efi_loader/Makefile +1 -1
@@ -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
lib/efi_loader/efi_var_common.c +42 -0
51 lines not on this page · raw message
lib/efi_loader/efi_var_file.c +0 -57
89 lines not on this page · raw message
lib/efi_loader/efi_variable.c +12 -5
@@ -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_NOT_READY ; +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);
Signature · 1 line
2.43.0
Re: [PATCH v5 1/3] efi_var_file: refactor to move buffer functions
Hi Michal,
Thanks for following up on this
lib/efi_loader/Makefile ifeq ($(CONFIG_EFI_MM_COMM_TEE),y) · line 56
Quoted 12 lines
*
efi_var_restore()- restore EFI variables from buffer *lib/efi_loader/Makefile +1
-1@@ -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
hrmm what if we compile with EFI_VARIABLE_NO_STORE?
Quoted 6 lines
lib/efi_loader/efi_variable.c +0
-0@@ -397,11 +397,15 @@ efi_status_t efi_set_variable_int(const u16 *variable_name, ret =EFI_SUCCESS ;
lib/efi_loader/efi_variable.c efi_status_t efi_set_variable_int(const u16 *variable_name, · line 405
Quoted 10 lines
lib/efi_loader/efi_variable.c +4
-2/* -* 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_NOT_READY ;
I think we should just return success here. EFI_VARIABLE_NO_STORE means "dont store on file, but we still have to update the memory backend.
[...]
Thanks
/Ilias
Re: [PATCH v5 1/3] efi_var_file: refactor to move buffer functions
Quoted 5 lines · Ilias Apalodimas
Hi Michal,
Thanks for following up on this
Quoted 14 lines
lib/efi_loader/Makefile ifeq ($(CONFIG_EFI_MM_COMM_TEE),y) · line 56
Quoted 12 lines
*
efi_var_restore()- restore EFI variables from buffer *lib/efi_loader/Makefile +1
-1@@ -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.ohrmm what if we compile with
EFI_VARIABLE_NO_STORE?
What's the concern here?
efi_var_to_file/efi_var_from_file() are just defined in efi_var_file.c
But that functions are never called from efi_variable.c
if (attributes & EFI_VARIABLE_NON_VOLATILE) {
if (IS_ENABLED(CONFIG_EFI_VARIABLE_NO_STORE))
return EFI_NOT_READY;
efi_var_to_file();I mean undefined reference to efi_var_to_file() is there but because there is return above function is unreachable and compiler just removes it.
Quoted 8 lines
Quoted 8 lines
lib/efi_loader/efi_variable.c +0
-0@@ -397,11 +397,15 @@ efi_status_t efi_set_variable_int(const u16 *variable_name, ret =EFI_SUCCESS ;
Quoted 13 lines
lib/efi_loader/efi_variable.c efi_status_t efi_set_variable_int(const u16 *variable_name, · line 405
Quoted 9 lines
lib/efi_loader/efi_variable.c +4
-2/* -* 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_NOT_READY ;I think we should just return success here.
EFI_VARIABLE_NO_STOREmeans "dont store on file, but we still have to update the memory backend.
Yes before this patch EFI_SUCCESS was returned. Will fix.
Thanks,
Michal
PATCH v5 2/3 efi_var: Unify read/write access helper function
In reply to Michal Simekefi_var_to/from_file() suggest method where variables are placed. But there is no reason for it and generic name can be used to wire also different locations for variables.
---
(no changes since v4)
Changes in v4:
- New patch based on review comments from v3
include/efi_variable.h | 13 ++++--------- lib/efi_loader/efi_var_file.c | 8 ++++---- lib/efi_loader/efi_variable.c | 4 ++-- 3 files changed, 10 insertions(+), 15 deletions(-)
include/efi_variable.h +4 -9
@@ -137,13 +137,11 @@ struct efi_var_file { };/** -* efi_var_to_file() - save non-volatile variables as file -* -* File ubootefi.var is created on the EFI system partion. +* efi_var_write() - save non-volatile variables * * Return: status code */ -efi_status_t efi_var_to_file (void ); +efi_status_t efi_var_write (void );/** * efi_var_collect() - collect variables in buffer
@@ -178,17 +176,14 @@ efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *efi_status_t efi_var_restore (struct efi_var_file *buf,bool safe);/** -* efi_var_from_file() - read variables from file -* -* File ubootefi.var is read from the EFI system partitions and the variables -* stored in the file are created. +* efi_var_read() - read variables * * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS * is returned. * * Return: status code */ -efi_status_t efi_var_from_file (void ); +efi_status_t efi_var_read (void );/** * efi_var_mem_init() - set-up variable list
lib/efi_loader/efi_var_file.c +4 -4
@@ -43,13 +43,13 @@ static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void) }/** -* efi_var_to_file() - save non-volatile variables as file +* efi_var_write() - save non-volatile variables as file * * File ubootefi.var is created on the EFI system partion. * * Return: status code */ -efi_status_t efi_var_to_file (void ) +efi_status_t efi_var_write (void ) {efi_status_t ret;struct efi_var_file *buf;
@@ -85,7 +85,7 @@ out: }/** -* efi_var_from_file() - read variables from file +* efi_var_read() - read variables from file * * File ubootefi.var is read from the EFI system partitions and the variables * stored in the file are created.
@@ -98,7 +98,7 @@ out:* * Return: status code */ -efi_status_t efi_var_from_file (void ) +efi_status_t efi_var_read (void ) {struct efi_var_file *buf;loff_t len;
lib/efi_loader/efi_variable.c +2 -2
@@ -404,7 +404,7 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,if (IS_ENABLED (CONFIG_EFI_VARIABLE_NO_STORE ))return EFI_NOT_READY ; -efi_var_to_file (); +efi_var_write (); }return EFI_SUCCESS ;
@@ -599,7 +599,7 @@ efi_status_t efi_init_variables(void)return ret;if (!IS_ENABLED (CONFIG_EFI_VARIABLE_NO_STORE )) { - ret =efi_var_from_file (); + ret =efi_var_read ();if (ret !=EFI_SUCCESS )return ret; }
Signature · 1 line
2.43.0
Re: [PATCH v5 2/3] efi_var: Unify read/write access helper function
I am ok with this with a small nit.
Can we rename efi_var_read/write to efi_var_from/to_storage? I want to make it clealer what these functions are used for
With the changes above
Quoted 121 lines · Michal Simek
efi_var_to/from_file() suggest method where variables are placed. But there is no reason for it and generic name can be used to wire also different locations for variables.
Signed-off-by Michal Simek <[email protected]>---
(no changes since v4)
Changes in v4:
- New patch based on review comments from v3include/efi_variable.h | 13 ++++--------- lib/efi_loader/efi_var_file.c | 8 ++++---- lib/efi_loader/efi_variable.c | 4 ++-- 3 files changed, 10 insertions(+), 15 deletions(-)include/efi_variable.h +4
-9@@ -137,13 +137,11 @@ struct efi_var_file { };/** -* efi_var_to_file() - save non-volatile variables as file -* -* File ubootefi.var is created on the EFI system partion. +* efi_var_write() - save non-volatile variables * * Return: status code */ -efi_status_t efi_var_to_file (void ); +efi_status_t efi_var_write (void );/** * efi_var_collect() - collect variables in buffer @@ -178,17 +176,14 @@ efi_status_t __maybe_unused efi_var_collect(struct efi_var_file **bufp, loff_t *efi_status_t efi_var_restore (struct efi_var_file *buf,bool safe);/** -* efi_var_from_file() - read variables from file -* -* File ubootefi.var is read from the EFI system partitions and the variables -* stored in the file are created. +* efi_var_read() - read variables * * In case the file does not exist yet or a variable cannot be set EFI_SUCCESS * is returned. * * Return: status code */ -efi_status_t efi_var_from_file (void ); +efi_status_t efi_var_read (void );/** * efi_var_mem_init() - set-up variable list lib/efi_loader/efi_var_file.c +4
-4@@ -43,13 +43,13 @@ static efi_status_t __maybe_unused efi_set_blk_dev_to_system_partition(void) }/** -* efi_var_to_file() - save non-volatile variables as file +* efi_var_write() - save non-volatile variables as file * * File ubootefi.var is created on the EFI system partion. * * Return: status code */ -efi_status_t efi_var_to_file (void ) +efi_status_t efi_var_write (void ) {efi_status_t ret;struct efi_var_file *buf;@@ -85,7 +85,7 @@ out: }/** -* efi_var_from_file() - read variables from file +* efi_var_read() - read variables from file * * File ubootefi.var is read from the EFI system partitions and the variables * stored in the file are created. @@ -98,7 +98,7 @@ out:* * Return: status code */ -efi_status_t efi_var_from_file (void ) +efi_status_t efi_var_read (void ) {struct efi_var_file *buf;loff_t len;lib/efi_loader/efi_variable.c +2
-3@@ -404,7 +404,7 @@ efi_status_t efi_set_variable_int(const u16 *variable_name,if (IS_ENABLED (CONFIG_EFI_VARIABLE_NO_STORE ))return EFI_NOT_READY ; -efi_var_to_file (); +efi_var_write (); }return EFI_SUCCESS ;@@ -599,7 +599,7 @@ efi_status_t efi_init_variables(void)return ret;if (!IS_ENABLED (CONFIG_EFI_VARIABLE_NO_STORE )) { - ret =efi_var_from_file (); + ret =efi_var_read ();if (ret !=EFI_SUCCESS )return ret; } --2.43.0
PATCH v5 3/3 efi_vars: Implement SPI Flash store
In reply to Michal SimekFrom: Shantur Rathore <[email protected]>
Currently U-Boot uses ESP as storage for EFI variables. Devices with SPI Flash are used for storing environment with this commit we allow EFI variables to be stored on SPI Flash.
https://github.com/rhboot/efivar
is not updated to support this location that's why you can mimic it by running:
dd if=/sys/firmware/efi/efivars/VarToFile-b2ac5fc9-92b7-4acd-aeac-11e818c3130c \
of=/tmp/vars skip=4 bs=1
flashcp /tmp/vars /dev/mtdX
where mtdX should match location defined by CONFIG_EFI_VARIABLE_SF_OFFSET/CONFIG_EFI_VAR_BUF_SIZE.
---
(no changes since v4)
Changes in v4:
- Extend Kconfig description
- Extend commit message and describe efivar missing part
- use unify methods for reading/writing variable
Changes in v3:
- Fixed compiler warnings.
Changes in v2:
- Refactored efi_var_file to move common parts out as requested
- Changed ifdefs to use CONFIG_IS_DEFINED
- Fixed typos
lib/efi_loader/Kconfig | 34 +++++++++++++- lib/efi_loader/Makefile | 1 + lib/efi_loader/efi_var_sf.c | 92 +++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 lib/efi_loader/efi_var_sf.c
lib/efi_loader/Kconfig +33 -1
@@ -123,6 +123,24 @@ config EFI_VARIABLE_FILE_STORE Select this option if you want non-volatile UEFI variables to be stored as file /ubootefi.var on the EFI system partition. +config EFI_VARIABLE_SF_STORE + bool "Store non-volatile UEFI variables in SPI Flash" + depends on SPI_FLASH + help + Select this option if you want non-volatile UEFI variables to be + stored in SPI Flash. + + Define CONFIG_EFI_VARIABLE_SF_OFFSET as offset in SPI Flash to use as + the storage for variables. CONFIG_EFI_VAR_BUF_SIZE defines the space + needed. + + Note that SPI Flash devices have a limited number of program/erase + cycles. Frequent updates to UEFI variables may cause excessive wear + and can permanently damage the flash device, particularly on SPI NAND + or low-end SPI NOR parts without wear leveling. This option should be + used with care on such systems, and is not recommended for platforms + where UEFI variables are updated frequently. + config EFI_MM_COMM_TEE bool "UEFI variables storage service via the trusted world" depends on OPTEE
@@ -152,7 +170,7 @@ endchoice config EFI_RT_VOLATILE_STORE bool "Allow variable runtime services in volatile storage (e.g RAM)" - depends on EFI_VARIABLE_FILE_STORE + depends on EFI_VARIABLE_FILE_STORE || EFI_VARIABLE_SF_STORE help When EFI variables are stored on file we don't allow SetVariableRT, since the OS doesn't know how to write that file. At the same time
@@ -193,6 +211,20 @@ config FFA_SHARED_MM_BUF_ADDR the MM SP in secure world. It is assumed that the MM SP knows the address of the shared MM communication buffer. +config EFI_VARIABLE_SF_OFFSET + hex "EFI variables in SPI flash offset" + depends on EFI_VARIABLE_SF_STORE + help + Offset from the start of the SPI Flash where EFI variables will be stored. + This should be aligned to the sector size of SPI Flash. + +config EFI_VARIABLE_SF_DEVICE_INDEX + int "Device Index for target SPI Flash" + default 0 + help + The index of SPI Flash device used for storing EFI variables. This would be + needed if there are more than 1 SPI Flash devices available to use. + config EFI_VARIABLES_PRESEED bool "Initial values for UEFI variables" depends on !COMPILE_TEST
lib/efi_loader/Makefile +1 -0
@@ -54,6 +54,7 @@ obj-y += efi_variable_tee.o else obj-y += efi_variable.o obj-$(CONFIG_EFI_VARIABLE_FILE_STORE) += efi_var_file.o +obj-$(CONFIG_EFI_VARIABLE_SF_STORE) += efi_var_sf.o obj-$(CONFIG_EFI_VARIABLES_PRESEED) += efi_var_seed.o endif obj-y += efi_watchdog.o
lib/efi_loader/efi_var_sf.c new file +92 -0
92 lines not on this page · raw message
Signature · 1 line
2.43.0