environment: support reading and writing of efivar
PATCH 1/1 environment: support reading and writing of efivar
scripts/bareboxenv: skip file creation to allow reading the destination file, when existing.
Fixes #29
--- common/environment.c | 62 +++++++++++++++++++++++++++++++++++++++----- scripts/bareboxenv.c | 11 +------- 2 files changed, 57 insertions(+), 16 deletions(-)
common/environment.c +56 -6
@@ -305,9 +305,10 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags)struct envfs_super *super;int envfd, size, ret;struct action_data data = {}; -void *buf =NULL , *wbuf; +void *buf =NULL , *wbuf, *bufWithEfi;struct envfs_entry *env;const char *defenv_path =default_environment_path_get (); +uint32_t magic;if (!filename) filename = defenv_path;
@@ -342,7 +343,9 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) } } - buf =xzalloc (size +sizeof (struct envfs_super )); + bufWithEfi =xzalloc (size +sizeof (struct envfs_super ) + +4 );// four byte efi attributes + buf = bufWithEfi +4 ; data.writep = buf +sizeof (struct envfs_super ); super = buf;
@@ -370,7 +373,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) super->crc =ENVFS_32 (crc32 (0 , buf +sizeof (struct envfs_super ), size)); super->sb_crc =ENVFS_32 (crc32 (0 , buf,sizeof (struct envfs_super ) -4 )); - envfd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR ); + envfd =open (filename,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR );if (envfd <0 ) {printf ("could not open %s: %m\n" , filename); ret = -errno;
@@ -385,6 +388,34 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) goto out; } + wbuf = buf; + +/* check if we writing efi vars */ + ret =pread (envfd, &magic,sizeof (uint32_t ), +4 );// four byte efi var attributes +if (ret ==-1 && errno ==ENOENT ) { +// skip as file don't exist + goto skip_efi_read; + } +if (ret <sizeof (u_int32_t)) { +perror ("read of destination file failed" ); + ret = -errno; + goto skip_efi_read; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("looks like we writing efi vars, keep attributes\n" ); + ret =pread (envfd, bufWithEfi,sizeof (uint32_t ),0 ); +if (ret <sizeof (uint32_t )) { +perror ("read of efi attributes failed" ); + ret = -errno; + goto out; + } + size +=sizeof (uint32_t ); + wbuf = bufWithEfi; + } + +skip_efi_read: ret =erase (envfd,ERASE_SIZE_ALL ,0 ,ERASE_TO_WRITE );/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
@@ -395,8 +426,6 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) size +=sizeof (struct envfs_super ); - wbuf = buf; -while (size) {ssize_t now =write (envfd, wbuf, size);if (now <0 ) {
@@ -425,7 +454,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) out:close (envfd); out1: -free (buf); +free (bufWithEfi);#ifdef __BAREBOX__unlink_recursive (TMPDIR ,NULL );#endif
@@ -449,6 +478,7 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)int envfd;int ret =0 ;size_t size, rsize; +uint32_t magic;if (!filename) filename =default_environment_path_get ();
@@ -466,6 +496,26 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)return -1 ; } +/* check if we reading efi vars */ + ret =pread (envfd, &magic,sizeof (uint32_t ), +4 );// four byte efi var attributes +if (ret <sizeof (u_int32_t)) { +perror ("read" ); + ret = -errno; + goto out; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("looks like we reading efi vars, skip attributes\n" ); + ret =read (envfd, &magic, +sizeof (uint32_t ));// simply reuse the memory +if (ret <sizeof (uint32_t )) { +perror ("read" ); + ret = -errno; + goto out; + } + } +/* read superblock */ ret =read (envfd, &super,sizeof (struct envfs_super ));if ( ret <sizeof (struct envfs_super )) {
scripts/bareboxenv.c +1 -10
@@ -117,7 +117,7 @@ static void usage(char *prgname)int main (int argc,char *argv[]) {int opt; -int save =0 , load =0 , pad =0 , err =0 , fd; +int save =0 , load =0 , pad =0 , err =0 ;char *filename =NULL , *dirname =NULL ;unsigned envfs_flags =0 ;int verbose =0 ;
@@ -156,15 +156,6 @@ int main(int argc, char *argv[])exit (1 ); } -if (save) { - fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY ,0644 ); -if (fd <0 ) { -perror ("open" ); -exit (1 ); - } -close (fd); - } -if (save && pad) {if (truncate (filename, pad)) {perror ("truncate" );
Signature · 1 line
2.45.3
Hi,
Thanks for your patch. Please see the comments inline.
Quoted 2 lines · Jonas Licht
scripts/bareboxenv: skip file creation to allow reading the destination file, when existing.
Can you elaborate a bit on what's done here? e.g. On EFI, barebox environment can be stored into an EFI variable. An EFI variable's value as read from Linux efivarfs is preceded by four bytes of metadata, which we need to maintain when rewriting the variable.
Quoted 2 lines
Signed-off-by Jonas Licht <[email protected]>Fixes #29
Quoted 10 lines
--- common/environment.c | 62 +++++++++++++++++++++++++++++++++++++++----- scripts/bareboxenv.c | 11 +------- 2 files changed, 57 insertions(+), 16 deletions(-)common/environment.c +0
-0
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 308
Quoted 5 lines
common/environment.c +1
-1@@ -305,9 +305,10 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags)struct envfs_super *super;int envfd, size, ret;struct action_data data = {}; -void *buf =NULL , *wbuf; +void *buf =NULL , *wbuf, *bufWithEfi;
barebox is written in kernel coding style, so please use snake case for variables.
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 347
Quoted 13 lines
common/environment.c +3
-1struct envfs_entry *env;const char *defenv_path =default_environment_path_get (); +uint32_t magic;if (!filename) filename = defenv_path;@@ -342,7 +343,9 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) } } - buf =xzalloc (size +sizeof (struct envfs_super )); + bufWithEfi =xzalloc (size +sizeof (struct envfs_super ) + +4 );// four byte efi attributes
Nit: #define EFIVAR_ATTR_SIZE 4 at the top of the file and use it here instead of the comment.
Quoted 14 lines
common/environment.c +2
-1+ buf = bufWithEfi +4 ; data.writep = buf +sizeof (struct envfs_super ); super = buf;@@ -370,7 +373,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) super->crc =ENVFS_32 (crc32 (0 , buf +sizeof (struct envfs_super ), size)); super->sb_crc =ENVFS_32 (crc32 (0 , buf,sizeof (struct envfs_super ) -4 )); - envfd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR ); + envfd =open (filename,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR );if (envfd <0 ) {printf ("could not open %s: %m\n" , filename); ret = -errno;
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 393
Quoted 6 lines
common/environment.c +3
-0@@ -385,6 +388,34 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) goto out; } + wbuf = buf; + +/* check if we writing efi vars */
I think it will be more readable to factor this out into a helper function. That function should also have an early exit with:
if (!IS_ENABLED(CONFIG_EFI)) return buf;
So, non-EFI platforms can skip this code altogether.
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 401
Quoted 8 lines
common/environment.c +8
-0+ ret =pread (envfd, &magic,sizeof (uint32_t ), +4 );// four byte efi var attributes +if (ret ==-1 && errno ==ENOENT ) { +// skip as file don't exist + goto skip_efi_read; + } +if (ret <sizeof (u_int32_t)) { +perror ("read of destination file failed" );
perror("pread") is enough given that there are no instances of perror.
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 407
Quoted 6 lines
common/environment.c +6
-0+ ret = -errno; + goto skip_efi_read; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("looks like we writing efi vars, keep attributes\n" );
"Assuming EFI variable. Keeping attributes\n"
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 411
Quoted 4 lines
common/environment.c +4
-0+ ret =pread (envfd, bufWithEfi,sizeof (uint32_t ),0 ); +if (ret <sizeof (uint32_t )) { +perror ("read of efi attributes failed" ); + ret = -errno;
errno could be clobbered by perror before, so you need to record it before doing anything else (including calling perror).
Quoted 78 lines
common/environment.c +29
-3+ goto out; + } + size +=sizeof (uint32_t ); + wbuf = bufWithEfi; + } + +skip_efi_read: ret =erase (envfd,ERASE_SIZE_ALL ,0 ,ERASE_TO_WRITE );/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ @@ -395,8 +426,6 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) size +=sizeof (struct envfs_super ); - wbuf = buf; -while (size) {ssize_t now =write (envfd, wbuf, size);if (now <0 ) {@@ -425,7 +454,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) out:close (envfd); out1: -free (buf); +free (bufWithEfi);#ifdef __BAREBOX__unlink_recursive (TMPDIR ,NULL );#endif @@ -449,6 +478,7 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)int envfd;int ret =0 ;size_t size, rsize; +uint32_t magic;if (!filename) filename =default_environment_path_get ();@@ -466,6 +496,26 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)return -1 ; } +/* check if we reading efi vars */ + ret =pread (envfd, &magic,sizeof (uint32_t ), +4 );// four byte efi var attributes +if (ret <sizeof (u_int32_t)) { +perror ("read" ); + ret = -errno; + goto out; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("looks like we reading efi vars, skip attributes\n" ); + ret =read (envfd, &magic, +sizeof (uint32_t ));// simply reuse the memory +if (ret <sizeof (uint32_t )) { +perror ("read" ); + ret = -errno; + goto out; + } + } +/* read superblock */ ret =read (envfd, &super,sizeof (struct envfs_super ));if ( ret <sizeof (struct envfs_super )) {scripts/bareboxenv.c +1
-1@@ -117,7 +117,7 @@ static void usage(char *prgname)int main (int argc,char *argv[]) {int opt; -int save =0 , load =0 , pad =0 , err =0 , fd; +int save =0 , load =0 , pad =0 , err =0 ;char *filename =NULL , *dirname =NULL ;unsigned envfs_flags =0 ;int verbose =0 ;@@ -156,15 +156,6 @@ int main(int argc, char *argv[])exit (1 );
scripts/bareboxenv.c int main(int argc, char *argv[]) · line 166
Quoted 10 lines
scripts/bareboxenv.c +0
-8} -if (save) { - fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY ,0644 ); -if (fd <0 ) { -perror ("open" ); -exit (1 ); - } -close (fd); - }
Did you verify proper operation in the non-EFI case?
Thanks,
Ahmad
Quoted 4 lines
scripts/bareboxenv.c +0
-1-if (save && pad) {if (truncate (filename, pad)) {perror ("truncate" );
PATCH v2 1/1 environment: support reading and writing of efivar
In reply to Jonas Lichtscripts/bareboxenv: skip file creation to allow reading the destination file, when existing.
In envfs_load before reading the superblock, we check if our superblock magic exists after an EFIVAR_ATTR_SIZE offset. This is necessary because linux kernel adds four byte variable attributes in front of each efi variable in sysfs. When the magic is found after EFIVAR_ATTR_SIZE, we skip reading the four bytes variable attributes.
For envfs_save detection of an efi var is done the same way. But we need to keep the attributes, as they are required for writing as well.
In order to write to efivars, you still need to remove immutable flag from the file with 'chattr -i' .
--- common/environment.c | 80 ++++++++++++++++++++++++++++++++++++++++---- scripts/Makefile | 2 ++ scripts/bareboxenv.c | 11 +----- 3 files changed, 76 insertions(+), 17 deletions(-)
common/environment.c +73 -7
@@ -32,8 +32,11 @@#include <magicvar.h> #else #define EXPORT_SYMBOL (x) +#include <autoconf.h> #endif +#define EFIVAR_ATTR_SIZE 4 +struct envfs_entry {char *name;void *buf;
@@ -290,6 +293,41 @@ static int file_remove_action(const char *filename, struct stat *statbuf, }#endif +static int detect_efi_vars (int fd,void * buf_with_efi,void ** wbuf,int *size) { +uint32_t magic; +int ret; + + *wbuf = buf_with_efi +EFIVAR_ATTR_SIZE ; +#ifndef CONFIG_EFI +return 0 ; +#endif + + ret =pread (fd, &magic,sizeof (uint32_t ), +EFIVAR_ATTR_SIZE ); +if (ret ==-1 && errno ==ENOENT ) { +perror ("pread" ); +// skip as file don't exist +return 0 ; + } +if (ret <sizeof (u_int32_t)) { +perror ("pread" ); +return 0 ; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("Assuming EFI variable. Keeping attributes\n" ); + ret =pread (fd, buf_with_efi,EFIVAR_ATTR_SIZE ,0 ); +if (ret <EFIVAR_ATTR_SIZE ) { + ret = -errno; +perror ("read of efi attributes failed" ); +return ret; + } + *size +=EFIVAR_ATTR_SIZE ; + *wbuf = buf_with_efi; + } +return 0 ; +} +/** * Make the current environment persistent * @param[in] filename where to store
@@ -305,7 +343,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags)struct envfs_super *super;int envfd, size, ret;struct action_data data = {}; -void *buf =NULL , *wbuf; +void *buf =NULL , *wbuf =NULL , *buf_with_efi;struct envfs_entry *env;const char *defenv_path =default_environment_path_get ();
@@ -342,7 +380,9 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) } } - buf =xzalloc (size +sizeof (struct envfs_super )); + buf_with_efi =xzalloc (size +sizeof (struct envfs_super ) + +EFIVAR_ATTR_SIZE ); + buf = buf_with_efi +EFIVAR_ATTR_SIZE ; data.writep = buf +sizeof (struct envfs_super ); super = buf;
@@ -370,7 +410,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) super->crc =ENVFS_32 (crc32 (0 , buf +sizeof (struct envfs_super ), size)); super->sb_crc =ENVFS_32 (crc32 (0 , buf,sizeof (struct envfs_super ) -4 )); - envfd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR ); + envfd =open (filename,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR );if (envfd <0 ) {printf ("could not open %s: %m\n" , filename); ret = -errno;
@@ -385,6 +425,12 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) goto out; } +/* check if we writing efi vars */ + ret =detect_efi_vars (envfd, buf_with_efi, &wbuf, &size); +if (ret !=0 ) { + goto out; + } + ret =erase (envfd,ERASE_SIZE_ALL ,0 ,ERASE_TO_WRITE );/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
@@ -395,12 +441,11 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) size +=sizeof (struct envfs_super ); - wbuf = buf; -while (size) {ssize_t now =write (envfd, wbuf, size);if (now <0 ) { ret = -errno; +perror ("write" ); goto out; }
@@ -418,14 +463,14 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) ret =0 ; -#ifdef CONFIG_NVVAR +#if definedCONFIG_NVVAR && defined __BAREBOX__if (defenv_path && !strcmp (filename, defenv_path))nv_var_set_clean ();#endif out:close (envfd); out1: -free (buf); +free (buf_with_efi);#ifdef __BAREBOX__unlink_recursive (TMPDIR ,NULL );#endif
@@ -449,6 +494,7 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)int envfd;int ret =0 ;size_t size, rsize; +uint32_t magic;if (!filename) filename =default_environment_path_get ();
@@ -466,6 +512,26 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)return -1 ; } +/* check if we reading efi vars */ + ret =pread (envfd, &magic,sizeof (uint32_t ), +EFIVAR_ATTR_SIZE ); +if (ret <sizeof (u_int32_t)) { +perror ("read" ); + ret = -errno; + goto out; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("Assuming EFI variable. Skip attributes\n" ); + ret =read (envfd, &magic, +sizeof (uint32_t ));// simply reuse the memory +if (ret <sizeof (uint32_t )) { +perror ("read" ); + ret = -errno; + goto out; + } + } +/* read superblock */ ret =read (envfd, &super,sizeof (struct envfs_super ));if ( ret <sizeof (struct envfs_super )) {
scripts/Makefile +2 -0
@@ -30,6 +30,7 @@ hostprogs-always-$(CONFIG_RK_IMAGE) += rkimage HOSTCFLAGS_rkimage.o = `$(PKG_CONFIG) --cflags openssl` HOSTLDLIBS_rkimage = `$(PKG_CONFIG) --libs openssl` KBUILD_HOSTCFLAGS += -I$(srctree)/scripts/include/ +KBUILD_HOSTCFLAGS += -I$(objtree)/include/generated/ HOSTCFLAGS_mxsimage.o = `$(PKG_CONFIG) --cflags openssl` HOSTLDLIBS_mxsimage = `$(PKG_CONFIG) --libs openssl` HOSTCFLAGS_omap3-usb-loader.o = `$(PKG_CONFIG) --cflags libusb-1.0`
@@ -57,6 +58,7 @@ omap4_usbboot-target-userccflags += `$(CROSS_PKG_CONFIG) --cflags libusb-1.0` omap4_usbboot-target-userldlibs += -lpthread `$(CROSS_PKG_CONFIG) --libs libusb-1.0` rk-usb-loader-target-userccflags += `$(CROSS_PKG_CONFIG) --cflags libusb-1.0` rk-usb-loader-target-userldlibs += `$(CROSS_PKG_CONFIG) --libs libusb-1.0` +bareboxenv-target-userccflags += -I$(objtree)/include/generated/ userccflags += -I $(srctree)/$(src)/include -isystem $(srctree)/scripts/include
scripts/bareboxenv.c +1 -10
@@ -117,7 +117,7 @@ static void usage(char *prgname)int main (int argc,char *argv[]) {int opt; -int save =0 , load =0 , pad =0 , err =0 , fd; +int save =0 , load =0 , pad =0 , err =0 ;char *filename =NULL , *dirname =NULL ;unsigned envfs_flags =0 ;int verbose =0 ;
@@ -156,15 +156,6 @@ int main(int argc, char *argv[])exit (1 ); } -if (save) { - fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY ,0644 ); -if (fd <0 ) { -perror ("open" ); -exit (1 ); - } -close (fd); - } -if (save && pad) {if (truncate (filename, pad)) {perror ("truncate" );
Signature · 1 line
2.45.3
PATCH v3 1/1 environment: support reading and writing of efivar
In reply to Jonas Lichtscripts/bareboxenv: skip file creation to allow reading the destination file, when existing.
In envfs_load before reading the superblock, we check if our superblock magic exists after an EFIVAR_ATTR_SIZE offset. This is necessary because linux kernel adds four byte variable attributes in front of each efi variable in sysfs. When the magic is found after EFIVAR_ATTR_SIZE, we skip reading the four bytes variable attributes.
For envfs_save detection of an efi var is done the same way. But we need to keep the attributes, as they are required for writing as well.
In order to write to efivars, you still need to remove immutable flag from the file with 'chattr -i' .
---
Skip the detect code when __BAREBOX__ is defined as well. I've noticed that the first pread in detect_efi_vars never comes back in __BAREBOX__ , when the efivar file don't exists, but doing this on a file at /tmp works.
common/environment.c | 84 ++++++++++++++++++++++++++++++++++++++++---- scripts/Makefile | 2 ++ scripts/bareboxenv.c | 11 +----- 3 files changed, 80 insertions(+), 17 deletions(-)
common/environment.c +77 -7
@@ -32,8 +32,11 @@#include <magicvar.h> #else #define EXPORT_SYMBOL (x) +#include <autoconf.h> #endif +#define EFIVAR_ATTR_SIZE 4 +struct envfs_entry {char *name;void *buf;
@@ -290,6 +293,41 @@ static int file_remove_action(const char *filename, struct stat *statbuf, }#endif +static int detect_efi_vars (int fd,void * buf_with_efi,void ** wbuf,int *size) { +uint32_t magic; +int ret; + + *wbuf = buf_with_efi +EFIVAR_ATTR_SIZE ; +#if !definedCONFIG_EFI || defined __BAREBOX__ +return 0 ; +#endif + + ret =pread (fd, &magic,sizeof (uint32_t ), +EFIVAR_ATTR_SIZE ); +if (ret ==-1 && errno ==ENOENT ) { +perror ("pread" ); +// skip as file don't exist +return 0 ; + } +if (ret <sizeof (u_int32_t)) { +perror ("pread" ); +return 0 ; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("Assuming EFI variable. Keeping attributes\n" ); + ret =pread (fd, buf_with_efi,EFIVAR_ATTR_SIZE ,0 ); +if (ret <EFIVAR_ATTR_SIZE ) { + ret = -errno; +perror ("read of efi attributes failed" ); +return ret; + } + *size +=EFIVAR_ATTR_SIZE ; + *wbuf = buf_with_efi; + } +return 0 ; +} +/** * Make the current environment persistent * @param[in] filename where to store
@@ -305,7 +343,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags)struct envfs_super *super;int envfd, size, ret;struct action_data data = {}; -void *buf =NULL , *wbuf; +void *buf =NULL , *wbuf =NULL , *buf_with_efi;struct envfs_entry *env;const char *defenv_path =default_environment_path_get ();
@@ -342,7 +380,9 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) } } - buf =xzalloc (size +sizeof (struct envfs_super )); + buf_with_efi =xzalloc (size +sizeof (struct envfs_super ) + +EFIVAR_ATTR_SIZE ); + buf = buf_with_efi +EFIVAR_ATTR_SIZE ; data.writep = buf +sizeof (struct envfs_super ); super = buf;
@@ -370,7 +410,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) super->crc =ENVFS_32 (crc32 (0 , buf +sizeof (struct envfs_super ), size)); super->sb_crc =ENVFS_32 (crc32 (0 , buf,sizeof (struct envfs_super ) -4 )); - envfd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR ); + envfd =open (filename,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR );if (envfd <0 ) {printf ("could not open %s: %m\n" , filename); ret = -errno;
@@ -385,6 +425,12 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) goto out; } +/* check if we writing efi vars */ + ret =detect_efi_vars (envfd, buf_with_efi, &wbuf, &size); +if (ret !=0 ) { + goto out; + } + ret =erase (envfd,ERASE_SIZE_ALL ,0 ,ERASE_TO_WRITE );/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
@@ -395,12 +441,11 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) size +=sizeof (struct envfs_super ); - wbuf = buf; -while (size) {ssize_t now =write (envfd, wbuf, size);if (now <0 ) { ret = -errno; +perror ("write" ); goto out; }
@@ -418,14 +463,14 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) ret =0 ; -#ifdef CONFIG_NVVAR +#if definedCONFIG_NVVAR && defined __BAREBOX__if (defenv_path && !strcmp (filename, defenv_path))nv_var_set_clean ();#endif out:close (envfd); out1: -free (buf); +free (buf_with_efi);#ifdef __BAREBOX__unlink_recursive (TMPDIR ,NULL );#endif
@@ -449,6 +494,9 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)int envfd;int ret =0 ;size_t size, rsize; +#if definedCONFIG_EFI && !defined __BAREBOX__ +uint32_t magic; +#endif if (!filename) filename =default_environment_path_get ();
@@ -466,6 +514,28 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)return -1 ; } +#if definedCONFIG_EFI && !defined __BAREBOX__ +/* check if we reading efi vars */ + ret =pread (envfd, &magic,sizeof (uint32_t ), +EFIVAR_ATTR_SIZE ); +if (ret <sizeof (u_int32_t)) { +perror ("read" ); + ret = -errno; + goto out; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("Assuming EFI variable. Skip attributes\n" ); + ret =read (envfd, &magic, +sizeof (uint32_t ));// simply reuse the memory +if (ret <sizeof (uint32_t )) { +perror ("read" ); + ret = -errno; + goto out; + } + } +#endif +/* read superblock */ ret =read (envfd, &super,sizeof (struct envfs_super ));if ( ret <sizeof (struct envfs_super )) {
scripts/Makefile +2 -0
@@ -30,6 +30,7 @@ hostprogs-always-$(CONFIG_RK_IMAGE) += rkimage HOSTCFLAGS_rkimage.o = `$(PKG_CONFIG) --cflags openssl` HOSTLDLIBS_rkimage = `$(PKG_CONFIG) --libs openssl` KBUILD_HOSTCFLAGS += -I$(srctree)/scripts/include/ +KBUILD_HOSTCFLAGS += -I$(objtree)/include/generated/ HOSTCFLAGS_mxsimage.o = `$(PKG_CONFIG) --cflags openssl` HOSTLDLIBS_mxsimage = `$(PKG_CONFIG) --libs openssl` HOSTCFLAGS_omap3-usb-loader.o = `$(PKG_CONFIG) --cflags libusb-1.0`
@@ -57,6 +58,7 @@ omap4_usbboot-target-userccflags += `$(CROSS_PKG_CONFIG) --cflags libusb-1.0` omap4_usbboot-target-userldlibs += -lpthread `$(CROSS_PKG_CONFIG) --libs libusb-1.0` rk-usb-loader-target-userccflags += `$(CROSS_PKG_CONFIG) --cflags libusb-1.0` rk-usb-loader-target-userldlibs += `$(CROSS_PKG_CONFIG) --libs libusb-1.0` +bareboxenv-target-userccflags += -I$(objtree)/include/generated/ userccflags += -I $(srctree)/$(src)/include -isystem $(srctree)/scripts/include
scripts/bareboxenv.c +1 -10
@@ -117,7 +117,7 @@ static void usage(char *prgname)int main (int argc,char *argv[]) {int opt; -int save =0 , load =0 , pad =0 , err =0 , fd; +int save =0 , load =0 , pad =0 , err =0 ;char *filename =NULL , *dirname =NULL ;unsigned envfs_flags =0 ;int verbose =0 ;
@@ -156,15 +156,6 @@ int main(int argc, char *argv[])exit (1 ); } -if (save) { - fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY ,0644 ); -if (fd <0 ) { -perror ("open" ); -exit (1 ); - } -close (fd); - } -if (save && pad) {if (truncate (filename, pad)) {perror ("truncate" );
Signature · 1 line
2.45.3
Hello Jonas,
Quoted 11 lines · Jonas Licht
scripts/bareboxenv: skip file creation to allow reading the destination file, when existing.In
envfs_loadbefore reading the superblock, we check if our superblock magic exists after anEFIVAR_ATTR_SIZEoffset. This is necessary because linux kernel adds four byte variable attributes in front of each efi variable in sysfs. When the magic is found afterEFIVAR_ATTR_SIZE, we skip reading the four bytes variable attributes.
Forenvfs_savedetection of an efi var is done the same way. But we need to keep the attributes, as they are required for writing as well.
Quoted 2 lines
In order to write to efivars, you still need to remove immutable flag from the file with 'chattr -i' .
Sorry for taking so long to get back to you.
I agree this is a problem we want to fix, but I wasn't overly happy with the complexity the fix introduced.
How about this alternative:
- We define in scripts/bareboxenv.c following function:
bool is_efivarfs_fd(int fd)
{
struct statfs env_statfs;
int ret;
ret = fstatvfs(fd, &env_statfs);
if (ret)
return false;
return bundle_statfs.f_type == EFIVARFS_MAGIC;
}In common/environment.c, in the first #ifdef __BAREBOX__ branch, we #define is_efivarfs_fd(fd) false.
- Instead of checking the magic, we just call is_efivarfs_fd and then read the 4 bytes
- We get rid of the #ifdefs and just let the compiler drop dead code when building for __BAREBOX__.
- As we limit the effect to bareboxenv as hosted tool, we don't need to check for CONFIG_EFI either
What do you think?
Some minor comments below.
Quoted 4 lines
Skip the detect code when
__BAREBOX__is defined as well. I've noticed that the first pread indetect_efi_varsnever comes back in__BAREBOX__, when the efivar file don't exists, but doing this on a file at /tmp works.
Oh, so that's a bug in the barebox efivarfs implementation?
Quoted 1 line
+#include <
autoconf.h>
autoconf.h should be included by the buildsystem (-include option), but with suggested changes above we can avoid it altogether.
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 384
Quoted 11 lines
common/environment.c +3
-1+void *buf =NULL , *wbuf =NULL , *buf_with_efi;struct envfs_entry *env;const char *defenv_path =default_environment_path_get ();@@ -342,7 +380,9 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) } } - buf =xzalloc (size +sizeof (struct envfs_super )); + buf_with_efi =xzalloc (size +sizeof (struct envfs_super ) + +EFIVAR_ATTR_SIZE );
Alignment is wonky, but you can just keep it a single line here.
Quoted 14 lines
common/environment.c +2
-1+ buf = buf_with_efi +EFIVAR_ATTR_SIZE ; data.writep = buf +sizeof (struct envfs_super ); super = buf;@@ -370,7 +410,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) super->crc =ENVFS_32 (crc32 (0 , buf +sizeof (struct envfs_super ), size)); super->sb_crc =ENVFS_32 (crc32 (0 , buf,sizeof (struct envfs_super ) -4 )); - envfd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR ); + envfd =open (filename,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR );if (envfd <0 ) {printf ("could not open %s: %m\n" , filename); ret = -errno;
common/environment.c int envfs_save(const char *filename, const char *dirname, unsigned flags) · line 432
Quoted 8 lines
common/environment.c +5
-0@@ -385,6 +425,12 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) goto out; } +/* check if we writing efi vars */ + ret =detect_efi_vars (envfd, buf_with_efi, &wbuf, &size); +if (ret !=0 ) { + goto out; + }
No braces for single line if clauses.
Quoted 36 lines
common/environment.c +4
-4+ ret =erase (envfd,ERASE_SIZE_ALL ,0 ,ERASE_TO_WRITE );/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */ @@ -395,12 +441,11 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) size +=sizeof (struct envfs_super ); - wbuf = buf; -while (size) {ssize_t now =write (envfd, wbuf, size);if (now <0 ) { ret = -errno; +perror ("write" ); goto out; }@@ -418,14 +463,14 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) ret =0 ; -#ifdef CONFIG_NVVAR +#if definedCONFIG_NVVAR && defined __BAREBOX__if (defenv_path && !strcmp (filename, defenv_path))nv_var_set_clean ();#endif out:close (envfd); out1: -free (buf); +free (buf_with_efi);#ifdef __BAREBOX__unlink_recursive (TMPDIR ,NULL );#endif
common/environment.c int envfs_load(const char *filename, const char *dir, unsigned flags) · line 499
Quoted 6 lines
common/environment.c +3
-0@@ -449,6 +494,9 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)int envfd;int ret =0 ;size_t size, rsize; +#if definedCONFIG_EFI && !defined __BAREBOX__ +uint32_t magic; +#endif
Won't be needed with suggested rework.
common/environment.c int envfs_load(const char *filename, const char *dir, unsigned flags) · line 520
Quoted 11 lines
common/environment.c +4
-0if (!filename) filename =default_environment_path_get ();@@ -466,6 +514,28 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)return -1 ; } +#if definedCONFIG_EFI && !defined __BAREBOX__ +/* check if we reading efi vars */ + ret =pread (envfd, &magic,sizeof (uint32_t ), +EFIVAR_ATTR_SIZE );
Just make it a single line. With suggested changes above this becomes a normal read.
Thanks,
Ahmad
Quoted 70 lines
common/environment.c +18
-0+if (ret <sizeof (u_int32_t)) { +perror ("read" ); + ret = -errno; + goto out; + } + +if (ENVFS_32 (magic) ==ENVFS_MAGIC ) { +pr_info ("Assuming EFI variable. Skip attributes\n" ); + ret =read (envfd, &magic, +sizeof (uint32_t ));// simply reuse the memory +if (ret <sizeof (uint32_t )) { +perror ("read" ); + ret = -errno; + goto out; + } + } +#endif +/* read superblock */ ret =read (envfd, &super,sizeof (struct envfs_super ));if ( ret <sizeof (struct envfs_super )) {scripts/Makefile +2
-0@@ -30,6 +30,7 @@ hostprogs-always-$(CONFIG_RK_IMAGE) += rkimage HOSTCFLAGS_rkimage.o = `$(PKG_CONFIG) --cflags openssl` HOSTLDLIBS_rkimage = `$(PKG_CONFIG) --libs openssl` KBUILD_HOSTCFLAGS += -I$(srctree)/scripts/include/ +KBUILD_HOSTCFLAGS += -I$(objtree)/include/generated/ HOSTCFLAGS_mxsimage.o = `$(PKG_CONFIG) --cflags openssl` HOSTLDLIBS_mxsimage = `$(PKG_CONFIG) --libs openssl` HOSTCFLAGS_omap3-usb-loader.o = `$(PKG_CONFIG) --cflags libusb-1.0`@@ -57,6 +58,7 @@ omap4_usbboot-target-userccflags += `$(CROSS_PKG_CONFIG) --cflags libusb-1.0` omap4_usbboot-target-userldlibs += -lpthread `$(CROSS_PKG_CONFIG) --libs libusb-1.0` rk-usb-loader-target-userccflags += `$(CROSS_PKG_CONFIG) --cflags libusb-1.0` rk-usb-loader-target-userldlibs += `$(CROSS_PKG_CONFIG) --libs libusb-1.0` +bareboxenv-target-userccflags += -I$(objtree)/include/generated/ userccflags += -I $(srctree)/$(src)/include -isystem $(srctree)/scripts/includescripts/bareboxenv.c +1
-10@@ -117,7 +117,7 @@ static void usage(char *prgname)int main (int argc,char *argv[]) {int opt; -int save =0 , load =0 , pad =0 , err =0 , fd; +int save =0 , load =0 , pad =0 , err =0 ;char *filename =NULL , *dirname =NULL ;unsigned envfs_flags =0 ;int verbose =0 ;@@ -156,15 +156,6 @@ int main(int argc, char *argv[])exit (1 ); } -if (save) { - fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY ,0644 ); -if (fd <0 ) { -perror ("open" ); -exit (1 ); - } -close (fd); - } -if (save && pad) {if (truncate (filename, pad)) {perror ("truncate" );
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
Am Fr., 4. Apr. 2025 um 16:51 Uhr schrieb Ahmad Fatoum <[email protected]>:
Quoted 2 lines
Hello Jonas,
Hello Ahmad,
Quoted 8 lines · Jonas Licht
Quoted 4 lines
Skip the detect code when
__BAREBOX__is defined as well. I've noticed that the first pread indetect_efi_varsnever comes back in__BAREBOX__, when the efivar file don't exists, but doing this on a file at /tmp works.Oh, so that's a bug in the barebox efivarfs implementation?
Yes it looks like the barebox efivarfs implementation is not capable of doing a pread, or do have a bug there.
Thanks,
Jonas
PATCH v4 1/1 environment: support reading and writing of efivar
In reply to Jonas Lichtscripts/bareboxenv: skip file creation to allow reading the destination file, when existing.
In envfs_load before reading the superblock, we check with fstatfs if the file descriptor is pointing to an efi vars.
If this is the case we skip reading the four bytes variable attributes. For envfs_save detection of an efi var is done the same way. But we need to keep the attributes, as they are required for writing as well. We only do the fstatfs in scripts/bareboxenv as it is not needed in __BAREBOX__
In order to write to efivars, you still need to remove immutable flag from the file with 'chattr -i' .
--- common/environment.c | 58 ++++++++++++++++++++++++++++++++++++++------ scripts/bareboxenv.c | 26 ++++++++++++-------- 2 files changed, 67 insertions(+), 17 deletions(-)
common/environment.c +51 -7
@@ -34,6 +34,8 @@#define EXPORT_SYMBOL (x)#endif +#define EFIVAR_ATTR_SIZE 4 +struct envfs_entry {char *name;void *buf;
@@ -147,6 +149,11 @@ static int do_compare_file(const char *filename, const char *base) return ret; } +static bool is_efivarsfs_fd (int fd) +{ +return false; +} +#else #define ERASE_SIZE_ALL 0static inline int protect (int fd,size_t count,unsigned long offset,int prot)
@@ -290,6 +297,25 @@ static int file_remove_action(const char *filename, struct stat *statbuf, }#endif +static int detect_efi_vars (int fd,void * buf_with_efi,void ** wbuf,int *size) { +int ret; + + *wbuf = buf_with_efi +EFIVAR_ATTR_SIZE ; +if (!is_efivarsfs_fd (fd)) +return 0 ; + +pr_info ("Assuming EFI variable. Keeping attributes\n" ); + ret =read (fd, buf_with_efi,EFIVAR_ATTR_SIZE ); +if (ret <EFIVAR_ATTR_SIZE ) { + ret = -errno; +perror ("read of efi attributes failed" ); +return ret; + } + *size +=EFIVAR_ATTR_SIZE ; + *wbuf = buf_with_efi; +return 0 ; +} +/** * Make the current environment persistent * @param[in] filename where to store
@@ -305,7 +331,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags)struct envfs_super *super;int envfd, size, ret;struct action_data data = {}; -void *buf =NULL , *wbuf; +void *buf =NULL , *wbuf =NULL , *buf_with_efi;struct envfs_entry *env;const char *defenv_path =default_environment_path_get ();
@@ -342,7 +368,8 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) } } - buf =xzalloc (size +sizeof (struct envfs_super )); + buf_with_efi =xzalloc (size +sizeof (struct envfs_super ) +EFIVAR_ATTR_SIZE ); + buf = buf_with_efi +EFIVAR_ATTR_SIZE ; data.writep = buf +sizeof (struct envfs_super ); super = buf;
@@ -370,7 +397,7 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) super->crc =ENVFS_32 (crc32 (0 , buf +sizeof (struct envfs_super ), size)); super->sb_crc =ENVFS_32 (crc32 (0 , buf,sizeof (struct envfs_super ) -4 )); - envfd =open (filename,O_WRONLY |O_CREAT ,S_IRUSR |S_IWUSR ); + envfd =open (filename,O_RDWR |O_CREAT ,S_IRUSR |S_IWUSR );if (envfd <0 ) {printf ("could not open %s: %m\n" , filename); ret = -errno;
@@ -385,6 +412,11 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) goto out; } +/* check if we writing efi vars */ + ret =detect_efi_vars (envfd, buf_with_efi, &wbuf, &size); +if (ret !=0 ) + goto out; + ret =erase (envfd,ERASE_SIZE_ALL ,0 ,ERASE_TO_WRITE );/* ENOSYS and EOPNOTSUPP aren't errors here, many devices don't need it */
@@ -395,12 +427,11 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) size +=sizeof (struct envfs_super ); - wbuf = buf; -while (size) {ssize_t now =write (envfd, wbuf, size);if (now <0 ) { ret = -errno; +perror ("write" ); goto out; }
@@ -418,14 +449,14 @@ int envfs_save(const char *filename, const char *dirname, unsigned flags) ret =0 ; -#ifdef CONFIG_NVVAR +#if definedCONFIG_NVVAR && defined __BAREBOX__if (defenv_path && !strcmp (filename, defenv_path))nv_var_set_clean ();#endif out:close (envfd); out1: -free (buf); +free (buf_with_efi);#ifdef __BAREBOX__unlink_recursive (TMPDIR ,NULL );#endif
@@ -449,6 +480,7 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)int envfd;int ret =0 ;size_t size, rsize; +uint32_t efi_attributes;if (!filename) filename =default_environment_path_get ();
@@ -466,6 +498,18 @@ int envfs_load(const char *filename, const char *dir, unsigned flags)return -1 ; } +/* check if we reading efi vars */ +if (is_efivarsfs_fd (envfd)) { +pr_info ("Assuming EFI variable. Skip attributes\n" ); + ret =read (envfd, &efi_attributes, +EFIVAR_ATTR_SIZE ); +if (ret <EFIVAR_ATTR_SIZE ) { +perror ("read" ); + ret = -errno; + goto out; + } + } +/* read superblock */ ret =read (envfd, &super,sizeof (struct envfs_super ));if ( ret <sizeof (struct envfs_super )) {
scripts/bareboxenv.c +16 -10
@@ -6,6 +6,9 @@#include <stdio.h> #include <sys/types.h> #include <sys/stat.h> +#include <sys/statfs.h> +#include <linux/magic.h> +#include <stdbool.h> #include <fcntl.h> #include <unistd.h> #include <stdint.h>
@@ -83,6 +86,18 @@ static char *concat_subpath_file(const char *path, const char *f)return concat_path_file (path, f); } +static bool is_efivarsfs_fd (int fd) +{ +struct statfs env_statfs; +int ret; + + ret =fstatfs (fd, &env_statfs); +if (ret) +return false; + +return env_statfs.f_type ==EFIVARFS_MAGIC ; +} +#include <linux/list.h> #include <linux/list_sort.h> #include "../lib/list_sort.c"
@@ -117,7 +132,7 @@ static void usage(char *prgname)int main (int argc,char *argv[]) {int opt; -int save =0 , load =0 , pad =0 , err =0 , fd; +int save =0 , load =0 , pad =0 , err =0 ;char *filename =NULL , *dirname =NULL ;unsigned envfs_flags =0 ;int verbose =0 ;
@@ -156,15 +171,6 @@ int main(int argc, char *argv[])exit (1 ); } -if (save) { - fd =open (filename,O_CREAT |O_TRUNC |O_WRONLY ,0644 ); -if (fd <0 ) { -perror ("open" ); -exit (1 ); - } -close (fd); - } -if (save && pad) {if (truncate (filename, pad)) {perror ("truncate" );
Signature · 1 line
2.49.0