Save/Load environment variable support
PATCH 1/1 Save/Load environment variable support
Hi,
I separate the save/load environment variable function from my previous patch on environment block. It contains the command save_env/load_env/list_env and tool grub-editenv, but without the kernel patch.
Signature · 1 line
Bean
commands/loadenv.c new file +250 -0
@@ -0,0 +1,250 @@ +/* loadenv.c - command to load/save environment variable. */ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2008 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/mm.h> +#include <grub/arg.h> +#include <grub/file.h> +#include <grub/disk.h> +#include <grub/misc.h> +#include <grub/env.h> +#include <grub/envblk.h> + +static const struct grub_arg_option options[] = + { + {"file" ,'f' ,0 ,"specify filename" ,0 ,ARG_TYPE_PATHNAME }, + {0 ,0 ,0 ,0 ,0 ,0 } + }; + +char buffer[GRUB_ENVBLK_MAXLEN ]; +grub_envblk_t envblk; + +static grub_file_t +read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector, unsigned offset, unsigned length)) +{ +char *buf =0 ; +grub_file_t file; + +if (! filename) + { +char *prefix; + + prefix =grub_env_get ("prefix" ); +if (prefix) + { +int len; + + len =grub_strlen (prefix); + buf =grub_malloc (len +1 +sizeof (GRUB_ENVBLK_DEFCFG )); +grub_strcpy (buf, prefix); + buf[len] ='/' ; +grub_strcpy (buf + len +1 ,GRUB_ENVBLK_DEFCFG ); + filename = buf; + } +else + { +grub_error (GRUB_ERR_FILE_NOT_FOUND ,"prefix is not found" ); +return 0 ; + } + } + + file =grub_file_open (filename); +grub_free (buf); +if (! file) +return 0 ; + +if (read_hook) + { +if (! file->device->disk) + { +grub_file_close (file); +grub_error (GRUB_ERR_BAD_DEVICE , +"this command is available only for disk devices." ); +return 0 ; + } + file->read_hook = read_hook; + } + +if (grub_file_read (file, buffer,GRUB_ENVBLK_MAXLEN ) !=GRUB_ENVBLK_MAXLEN ) + { +grub_file_close (file); +grub_error (GRUB_ERR_BAD_FILE_TYPE ,"file too short" ); +return 0 ; + } + + envblk =grub_envblk_find (buffer); +if (! envblk) + { +grub_file_close (file); +grub_error (GRUB_ERR_BAD_FILE_TYPE ,"environment block not found" ); +return 0 ; + } + +return file; +} + +static grub_err_t +grub_cmd_load_env (struct grub_arg_list *state, +int argc __attribute__ ((unused)),char **args __attribute__ ((unused))) + +{ +grub_file_t file; + + autoint hook (char *name,char *value); +int hook (char *name,char *value) + { +grub_env_set (name, value); + +return 0 ; + } + + file =read_envblk_file ((state[0 ].set) ? state[0 ].arg :0 ,0 ); +if (! file) +return grub_errno; + +grub_file_close (file); + +grub_envblk_iterate (envblk, hook); + +return grub_errno; +} + +static grub_err_t +grub_cmd_list_env (struct grub_arg_list *state, +int argc __attribute__ ((unused)),char **args __attribute__ ((unused))) +{ +grub_file_t file; + + autoint hook (char *name,char *value); +int hook (char *name,char *value) + { +grub_printf ("%s=%s\n" , name, value); + +return 0 ; + } + + file =read_envblk_file ((state[0 ].set) ? state[0 ].arg :0 ,0 ); +if (! file) +return grub_errno; + +grub_file_close (file); + +grub_envblk_iterate (envblk, hook); + +return grub_errno; +} + +static grub_err_t +grub_cmd_save_env (struct grub_arg_list *state,int argc,char **args) +{ +grub_file_t file; +grub_disk_t disk; +grub_disk_addr_t addr[GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ]; +char buf[GRUB_DISK_SECTOR_SIZE ]; +int num =0 ; + + auto void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, unsigned offset, +unsigned length); + + void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, +unsigned offset,unsigned length) + { +if ((offset !=0 ) || (length !=GRUB_DISK_SECTOR_SIZE )) +return ; + +if (num < (GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS )) + addr[num++] = sector; + } + +if (! argc) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"No variable is specified" ); + + file =read_envblk_file ((state[0 ].set) ? state[0 ].arg :0 , hook); +if (! file) +return grub_errno; + + file->read_hook =0 ; + +if (num !=GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ) + { +grub_error (GRUB_ERR_BAD_DEVICE ,"invalid blocklist" ); + goto quit; + } + + disk = file->device->disk; +for (num =0 ; num < (GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ); num++) + { +if (disk->dev->read (disk, addr[num],1 , buf)) + goto quit; + +if (grub_memcmp (&buffer[num <<GRUB_DISK_SECTOR_BITS ], buf, +GRUB_DISK_SECTOR_SIZE )) + { +grub_error (GRUB_ERR_BAD_DEVICE ,"invalid blocklist" ); + goto quit; + } + } + +while (argc) + { +char *value; + + value =grub_env_get (args[0 ]); +if (value) + { +if (grub_envblk_insert (envblk, args[0 ], value)) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,"environment block too small" ); + goto quit; + } + } + + argc--; + args++; + } + +for (num =0 ; num < (GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ); num++) +if (disk->dev->write (disk, addr[num],1 , + &buffer[num <<GRUB_DISK_SECTOR_BITS ])) + goto quit; + +quit: +grub_file_close (file); + +return grub_errno; +} + +GRUB_MOD_INIT (loadenv) +{ + (void ) mod; +grub_register_command ("load_env" , grub_cmd_load_env,GRUB_COMMAND_FLAG_BOTH , +"load_env [-f FILE]" ,"Load variables from environment block file." , options); +grub_register_command ("list_env" , grub_cmd_list_env,GRUB_COMMAND_FLAG_BOTH , +"list_env [-f FILE]" ,"List variables from environment block file." , options); +grub_register_command ("save_env" , grub_cmd_save_env,GRUB_COMMAND_FLAG_BOTH , +"save_env [-f FILE] variable_name [...]" ,"Save variables to environment block file." , options); +} + +GRUB_MOD_FINI (loadenv) +{ +grub_unregister_command ("load_env" ); +grub_unregister_command ("list_env" ); +grub_unregister_command ("save_env" ); +}
conf/common.rmk +26 -11
@@ -94,6 +94,11 @@ grub_fstest_init.c: grub_fstest_init.lst $(filter-out grub_fstest_init.c,$(grub_ rm -f $@; sh $(srcdir)/geninit.sh $< $(filter %.c,$^) > $@ DISTCLEANFILES += grub_fstest_init.c +# for grub-editenv +bin_UTILITIES += grub-editenv +grub_editenv_SOURCES = util/grub-editenv.c util/envblk.c util/misc.c +CLEANFILES += grub-editenv + # For update-grub update-grub: util/update-grub.in config.status ./config.status --file=$@:$<
@@ -282,7 +287,7 @@ pkglib_MODULES += hello.mod boot.mod terminal.mod ls.mod \ cmp.mod cat.mod help.mod font.mod search.mod \ loopback.mod configfile.mod echo.mod \ terminfo.mod test.mod blocklist.mod hexdump.mod \ - read.mod sleep.mod + read.mod sleep.mod loadenv.mod # For hello.mod. hello_mod_SOURCES = hello/hello.c
@@ -364,8 +369,23 @@ hexdump_mod_SOURCES = commands/hexdump.c hexdump_mod_CFLAGS = $(COMMON_CFLAGS) hexdump_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For read.mod. +read_mod_SOURCES = commands/read.c +read_mod_CFLAGS = $(COMMON_CFLAGS) +read_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For sleep.mod. +sleep_mod_SOURCES = commands/sleep.c +sleep_mod_CFLAGS = $(COMMON_CFLAGS) +sleep_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For loadenv.mod. +loadenv_mod_SOURCES = commands/loadenv.c util/envblk.c +loadenv_mod_CFLAGS = $(COMMON_CFLAGS) +loadenv_mod_LDFLAGS = $(COMMON_LDFLAGS) + # Misc. -pkglib_MODULES += gzio.mod elf.mod +pkglib_MODULES += gzio.mod elf.mod findroot.mod # For elf.mod. elf_mod_SOURCES = kern/elf.c
@@ -377,12 +397,7 @@ gzio_mod_SOURCES = io/gzio.c gzio_mod_CFLAGS = $(COMMON_CFLAGS) gzio_mod_LDFLAGS = $(COMMON_LDFLAGS) -# For read.mod. -read_mod_SOURCES = commands/read.c -read_mod_CFLAGS = $(COMMON_CFLAGS) -read_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For sleep.mod. -sleep_mod_SOURCES = commands/sleep.c -sleep_mod_CFLAGS = $(COMMON_CFLAGS) -sleep_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For findroot.mod. +findroot_mod_SOURCES = kern/findroot.c +findroot_mod_CFLAGS = $(COMMON_CFLAGS) +findroot_mod_LDFLAGS = $(COMMON_LDFLAGS)
include/grub/envblk.h new file +51 -0
51 lines not on this page · raw message
util/envblk.c new file +171 -0
171 lines not on this page · raw message
util/grub-editenv.c new file +228 -0
228 lines not on this page · raw message
Quoted 16 lines · Bean
Hi,
I separate the save/load environment variable function from my previous patch on environment block. It contains the command save_env/load_env/list_env and tool grub-editenv, but without the kernel patch.
Signature · 1 line
Beancommands/loadenv.c new file +0
-0
commands/loadenv.c line 1
Quoted 1 line
commands/loadenv.c new file +1
-0@@ -0,0 +1,250 @@ +/* loadenv.c - command to load/save environment variable. */
Is this useful as a standalone command? If we're in a stage in which commands can be executed, the same effect could be archieved with the "." command on a grub.cfg-like formatted file?
IIRC you explained that this feature could be useful as the basis for findroot style search, or to override existing variables like "root", "prefix" or "default". In both cases (if I understood correctly) you want it to be run automaticaly as the init routine for your module.
However if your code is small enough, maybe it could replace the existing use of grub_prefix (I commented this on the other mail), thereby reducing kernel size and making it preferable to be part of kernel than a separate module. Do you think that's possible?
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What good is a phone call… if you are unable to speak? (as seen on /.)
Quoted 28 lines · Robert Millan
Quoted 16 lines · Bean
Hi,
I separate the save/load environment variable function from my previous patch on environment block. It contains the command save_env/load_env/list_env and tool grub-editenv, but without the kernel patch.
--
Beancommands/loadenv.c new file +0
-0commands/loadenv.c line 1
Quoted 1 line
commands/loadenv.c new file +1
-0@@ -0,0 +1,250 @@ +/* loadenv.c - command to load/save environment variable. */ Is this useful as a standalone command? If we're in a stage in which commands can be executed, the same effect could be archieved with the "." command on a grub.cfg-like formatted file?
IIRC you explained that this feature could be useful as the basis for findroot style search, or to override existing variables like "root", "prefix" or "default". In both cases (if I understood correctly) you want it to be run automaticaly as the init routine for your module.
Quoted 4 lines
However if your code is small enough, maybe it could replace the existing use of
grub_prefix(I commented this on the other mail), thereby reducing kernel size and making it preferable to be part of kernel than a separate module. Do you think that's possible?
Hi,
Yes, this is a standalone command, it's used to support persistent variable. For example, we can archive the result of savedefault using the following command:
load_env
menuentry aa {
save_env default
}
load_env loads the environment variable from $prefix/grubenv, and save_env save selected variable to it. grubenv contain a environment block, so that we can manage it using grub-editenv, just like the environment block in the kernel.
Signature · 1 line
Bean
Quoted 9 lines · Bean
Hi,
Yes, this is a standalone command, it's used to support persistent variable. For example, we can archive the result of savedefault using the following command:
load_env
Quoted 8 lines
menuentry aa {
save_envdefault
}
load_envloads the environment variable from $prefix/grubenv, andsave_envsave selected variable to it. grubenv contain a environment block, so that we can manage it using grub-editenv, just like the environment block in the kernel.
Sounds interesting.. does the 8 kiB size assure us that we won't have any trouble with filesystems when writing to it?
Also, which parts of this would be reused by findroot?
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What good is a phone call… if you are unable to speak? (as seen on /.)
Quoted 14 lines · Robert Millan
Quoted 13 lines · Bean
Hi,
Yes, this is a standalone command, it's used to support persistent variable. For example, we can archive the result of savedefault using the following command:
load_envmenuentry aa {
save_envdefault
}
Quoted 7 lines
Quoted 4 lines
load_envloads the environment variable from $prefix/grubenv, andsave_envsave selected variable to it. grubenv contain a environment block, so that we can manage it using grub-editenv, just like the environment block in the kernel.Sounds interesting.. does the 8 kiB size assure us that we won't have any trouble with filesystems when writing to it?
I first read from the disk, compare content, then write back. If it's tail packing by the file system, it would fail at the compare stage.
Quoted 2 lines
Also, which parts of this would be reused by findroot?
No parts is shared by findroot. Actually, findroot only work on variable, whose value is fetched in main.c.
Signature · 1 line
Bean
conf/common.rmk gzio_mod_SOURCES = io/gzio.c · line 403
Quoted 9 lines · Bean
# Misc.
conf/common.rmk +5
-1-pkglib_MODULES += gzio.mod elf.mod +pkglib_MODULES += gzio.mod elf.mod findroot.mod +# For findroot.mod. +findroot_mod_SOURCES = kern/findroot.c +findroot_mod_CFLAGS = $(COMMON_CFLAGS) +findroot_mod_LDFLAGS = $(COMMON_LDFLAGS)
Some findroot bits were mixed up in the patch.
include/grub/envblk.h line 30
Quoted 5 lines
include/grub/envblk.h new file +5
-0+/* Names of important environment variables. */ +#define GRUB_ENVBLK_RDIR "rdir" +#define GRUB_ENVBLK_UUID "uuid" +#define GRUB_ENVBLK_LABEL "label" +#define GRUB_ENVBLK_IDFILE "idfile"
I'd prefer if the envblk part was agnostic about which variables have a meaning.
util/envblk.c line 24
Quoted 6 lines
+++
b/util/envblk.cutil/envblk.c new file +3
-0@@ -0,0 +1,171 @@ +/* envblk.c - Common function for environment block. */ + +#ifdef GRUB_UTIL
Files that are meant to be used both by grub and util are usually put in the non-util dir (I'm not sure which non-util dir would fit, though, as it depends on what you want to do next).
util/envblk.c line 32
Quoted 7 lines
util/envblk.c new file +7
-0+#include <string.h> + +#define grub_strlen strlen +#define grub_strcpy strcpy +#define grub_strchr strchr +#define grub_memcmp memcmp +#define grub_memcpy memcpy
Uhm can we avoid this? The rest of non-util code just calls the grub_* functions (linking with kern/misc.c in this case).
util/envblk.c line 49
Quoted 10 lines
util/envblk.c new file +10
-0+grub_envblk_t +grub_envblk_find (char *buf) +{ +grub_uint32_t *pd; +int len; + + pd = (grub_uint32_t *) buf; + +for (len =GRUB_ENVBLK_MAXLEN -6 ; len >0 ; len -=4 , pd++) +if (*pd ==GRUB_ENVBLK_SIGNATURE )
I wonder if this would be dangerous when run on files where env block presence is optional, like core.img (though we can always think about this later when that option is added).
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What good is a phone call… if you are unable to speak? (as seen on /.)
Quoted 5 lines · Robert Millan
Quoted 4 lines · Bean
# Misc.
conf/common.rmk +1
-1-pkglib_MODULES += gzio.mod elf.mod +pkglib_MODULES += gzio.mod elf.mod findroot.mod
Quoted 7 lines
conf/common.rmk gzio_mod_SOURCES = io/gzio.c · line 403
Quoted 5 lines
[...]
conf/common.rmk +4
-0+# For findroot.mod. +findroot_mod_SOURCES = kern/findroot.c +findroot_mod_CFLAGS = $(COMMON_CFLAGS) +findroot_mod_LDFLAGS = $(COMMON_LDFLAGS)Some findroot bits were mixed up in the patch.
Oh, I forget to remove them.
Quoted 21 lines
include/grub/envblk.h line 30
Quoted 6 lines
include/grub/envblk.h new file +5
-0+/* Names of important environment variables. */ +#define GRUB_ENVBLK_RDIR "rdir" +#define GRUB_ENVBLK_UUID "uuid" +#define GRUB_ENVBLK_LABEL "label" +#define GRUB_ENVBLK_IDFILE "idfile"I'd prefer if the envblk part was agnostic about which variables have a meaning.
util/envblk.c line 24
Quoted 6 lines
+++
b/util/envblk.cutil/envblk.c new file +3
-0@@ -0,0 +1,171 @@ +/* envblk.c - Common function for environment block. */ + +#ifdef GRUB_UTIL Files that are meant to be used both by grub and util are usually put in the non-util dir (I'm not sure which non-util dir would fit, though, as it depends on what you want to do next).
Quoted 10 lines
util/envblk.c line 32
Quoted 7 lines
util/envblk.c new file +7
-0+#include <string.h> + +#define grub_strlen strlen +#define grub_strcpy strcpy +#define grub_strchr strchr +#define grub_memcmp memcmp +#define grub_memcpy memcpyUhm can we avoid this? The rest of non-util code just calls the grub_* functions (linking with
kern/misc.cin this case).
I fount out that if I use grub_*, I end up adding quite a few of extra modules that serve no purpose other than string manipulation. Perhaps we should separate those routines from kern/misc.c ?
Quoted 15 lines
util/envblk.c line 49
Quoted 11 lines
util/envblk.c new file +10
-0+grub_envblk_t +grub_envblk_find (char *buf) +{ +grub_uint32_t *pd; +int len; + + pd = (grub_uint32_t *) buf; + +for (len =GRUB_ENVBLK_MAXLEN -6 ; len >0 ; len -=4 , pd++) +if (*pd ==GRUB_ENVBLK_SIGNATURE )I wonder if this would be dangerous when run on files where env block presence is optional, like core.img (though we can always think about this later when that option is added).
Yes, perhaps we should store a pointer to environment block so that we don't need to scan for it.
Signature · 1 line
Bean
Quoted 9 lines · Bean
Quoted 8 lines
util/envblk.c line 32
Quoted 5 lines
util/envblk.c new file +5
-0+#define grub_strlen strlen +#define grub_strcpy strcpy +#define grub_strchr strchr +#define grub_memcmp memcmp +#define grub_memcpy memcpyUhm can we avoid this? The rest of non-util code just calls the grub_* functions (linking with
kern/misc.cin this case).
Quoted 3 lines
I fount out that if I use grub_*, I end up adding quite a few of extra modules that serve no purpose other than string manipulation. Perhaps we should separate those routines from
kern/misc.c?
Which modules? If these functions are part of kernel, they shouldn't be dragging modules in, AFAICT.
Quoted 9 lines
Quoted 6 lines
util/envblk.c line 49
Quoted 2 lines
util/envblk.c new file +2
-0+for (len =GRUB_ENVBLK_MAXLEN -6 ; len >0 ; len -=4 , pd++) +if (*pd ==GRUB_ENVBLK_SIGNATURE )I wonder if this would be dangerous when run on files where env block presence is optional, like core.img (though we can always think about this later when that option is added).
Yes, perhaps we should store a pointer to environment block so that we don't need to scan for it.
If the env block could have variable length (in core.img, not the filesystem one), then maybe a zero-length block could be embedded unconditionaly.
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What good is a phone call… if you are unable to speak? (as seen on /.)
Quoted 10 lines · Robert Millan
Quoted 9 lines · Bean
Quoted 8 lines
util/envblk.c line 32
Quoted 5 lines
util/envblk.c new file +5
-0+#define grub_strlen strlen +#define grub_strcpy strcpy +#define grub_strchr strchr +#define grub_memcmp memcmp +#define grub_memcpy memcpyUhm can we avoid this? The rest of non-util code just calls the grub_* functions (linking with
kern/misc.cin this case).
Quoted 6 lines
Quoted 3 lines
I fount out that if I use grub_*, I end up adding quite a few of extra modules that serve no purpose other than string manipulation. Perhaps we should separate those routines from
kern/misc.c?Which modules? If these functions are part of kernel, they shouldn't be dragging modules in, AFAICT.
Oh, I actually means the files, in kern/misc.c, there is reference to other function, which requires other unrelated files to be included.
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
util/envblk.c line 49
Quoted 3 lines
util/envblk.c new file +2
-0+for (len =GRUB_ENVBLK_MAXLEN -6 ; len >0 ; len -=4 , pd++) +if (*pd ==GRUB_ENVBLK_SIGNATURE )I wonder if this would be dangerous when run on files where env block presence is optional, like core.img (though we can always think about this later when that option is added).
Quoted 5 lines
Quoted 2 lines
Yes, perhaps we should store a pointer to environment block so that we don't need to scan for it.
If the env block could have variable length (in core.img, not the filesystem one), then maybe a zero-length block could be embedded unconditionaly.
It can have zero length. The len variable is used to count the distant to the end of buffer.
Signature · 1 line
Bean
Hi,
This is the new patch, some changes:
1, envblk.h, remove GRUB_ENVBLK_RDIR and the like, they're not needed in this patch.
2, util/envblk.c, use grub_* function for string manipulation. 3, commands/loadenv.c, use grub_disk_read/grub_disk_write to read/write disk, the problem of lower level api is that they don't update the cache.
If there is no problem, I'd like to commit this soon.
Signature · 1 line
Bean
commands/loadenv.c new file +257 -0
@@ -0,0 +1,257 @@ +/* loadenv.c - command to load/save environment variable. */ +/* +* GRUB -- GRand Unified Bootloader +* Copyright (C) 2008 Free Software Foundation, Inc. +* +* GRUB is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* GRUB is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with GRUB. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include <grub/normal.h> +#include <grub/dl.h> +#include <grub/mm.h> +#include <grub/arg.h> +#include <grub/file.h> +#include <grub/disk.h> +#include <grub/misc.h> +#include <grub/env.h> +#include <grub/envblk.h> +#include <grub/partition.h> + +static const struct grub_arg_option options[] = + { + {"file" ,'f' ,0 ,"specify filename" ,0 ,ARG_TYPE_PATHNAME }, + {0 ,0 ,0 ,0 ,0 ,0 } + }; + +char buffer[GRUB_ENVBLK_MAXLEN ]; +grub_envblk_t envblk; + +static grub_file_t +read_envblk_file (char *filename, void NESTED_FUNC_ATTR read_hook (grub_disk_addr_t sector, unsigned offset, unsigned length)) +{ +char *buf =0 ; +grub_file_t file; + +if (! filename) + { +char *prefix; + + prefix =grub_env_get ("prefix" ); +if (prefix) + { +int len; + + len =grub_strlen (prefix); + buf =grub_malloc (len +1 +sizeof (GRUB_ENVBLK_DEFCFG )); +grub_strcpy (buf, prefix); + buf[len] ='/' ; +grub_strcpy (buf + len +1 ,GRUB_ENVBLK_DEFCFG ); + filename = buf; + } +else + { +grub_error (GRUB_ERR_FILE_NOT_FOUND ,"prefix is not found" ); +return 0 ; + } + } + + file =grub_file_open (filename); +grub_free (buf); +if (! file) +return 0 ; + +if (read_hook) + { +if (! file->device->disk) + { +grub_file_close (file); +grub_error (GRUB_ERR_BAD_DEVICE , +"this command is available only for disk devices." ); +return 0 ; + } + file->read_hook = read_hook; + } + +if (grub_file_read (file, buffer,GRUB_ENVBLK_MAXLEN ) !=GRUB_ENVBLK_MAXLEN ) + { +grub_file_close (file); +grub_error (GRUB_ERR_BAD_FILE_TYPE ,"file too short" ); +return 0 ; + } + + envblk =grub_envblk_find (buffer); +if (! envblk) + { +grub_file_close (file); +grub_error (GRUB_ERR_BAD_FILE_TYPE ,"environment block not found" ); +return 0 ; + } + +return file; +} + +static grub_err_t +grub_cmd_load_env (struct grub_arg_list *state, +int argc __attribute__ ((unused)),char **args __attribute__ ((unused))) + +{ +grub_file_t file; + + autoint hook (char *name,char *value); +int hook (char *name,char *value) + { +grub_env_set (name, value); + +return 0 ; + } + + file =read_envblk_file ((state[0 ].set) ? state[0 ].arg :0 ,0 ); +if (! file) +return grub_errno; + +grub_file_close (file); + +grub_envblk_iterate (envblk, hook); + +return grub_errno; +} + +static grub_err_t +grub_cmd_list_env (struct grub_arg_list *state, +int argc __attribute__ ((unused)),char **args __attribute__ ((unused))) +{ +grub_file_t file; + + autoint hook (char *name,char *value); +int hook (char *name,char *value) + { +grub_printf ("%s=%s\n" , name, value); + +return 0 ; + } + + file =read_envblk_file ((state[0 ].set) ? state[0 ].arg :0 ,0 ); +if (! file) +return grub_errno; + +grub_file_close (file); + +grub_envblk_iterate (envblk, hook); + +return grub_errno; +} + +static grub_err_t +grub_cmd_save_env (struct grub_arg_list *state,int argc,char **args) +{ +grub_file_t file; +grub_disk_t disk; +grub_disk_addr_t addr[GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ]; +char buf[GRUB_DISK_SECTOR_SIZE ]; +grub_disk_addr_t part_start =0 ; +int num =0 ; + + auto void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, unsigned offset, +unsigned length); + + void NESTED_FUNC_ATTR hook (grub_disk_addr_t sector, +unsigned offset,unsigned length) + { +if ((offset !=0 ) || (length !=GRUB_DISK_SECTOR_SIZE )) +return ; + +if (num < (GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS )) + addr[num++] = sector; + } + +if (! argc) +return grub_error (GRUB_ERR_BAD_ARGUMENT ,"No variable is specified" ); + + file =read_envblk_file ((state[0 ].set) ? state[0 ].arg :0 , hook); +if (! file) +return grub_errno; + + file->read_hook =0 ; + +if (num !=GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ) + { +grub_error (GRUB_ERR_BAD_DEVICE ,"invalid blocklist" ); + goto quit; + } + + disk = file->device->disk; +if (disk->partition) + part_start =grub_partition_get_start (disk->partition); + +for (num =0 ; num < (GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ); num++) + { +if (grub_disk_read (disk, addr[num] - part_start,0 , +GRUB_DISK_SECTOR_SIZE , buf)) + goto quit; + +if (grub_memcmp (&buffer[num <<GRUB_DISK_SECTOR_BITS ], buf, +GRUB_DISK_SECTOR_SIZE )) + { +grub_error (GRUB_ERR_BAD_DEVICE ,"invalid blocklist" ); + goto quit; + } + } + +while (argc) + { +char *value; + + value =grub_env_get (args[0 ]); +if (value) + { +if (grub_envblk_insert (envblk, args[0 ], value)) + { +grub_error (GRUB_ERR_BAD_ARGUMENT ,"environment block too small" ); + goto quit; + } + } + + argc--; + args++; + } + +for (num =0 ; num < (GRUB_ENVBLK_MAXLEN >>GRUB_DISK_SECTOR_BITS ); num++) +if (grub_disk_write (disk, addr[num] - part_start,0 , +GRUB_DISK_SECTOR_SIZE , + &buffer[num <<GRUB_DISK_SECTOR_BITS ])) + goto quit; + +quit: +grub_file_close (file); + +return grub_errno; +} + +GRUB_MOD_INIT (loadenv) +{ + (void ) mod; +grub_register_command ("load_env" , grub_cmd_load_env,GRUB_COMMAND_FLAG_BOTH , +"load_env [-f FILE]" ,"Load variables from environment block file." , options); +grub_register_command ("list_env" , grub_cmd_list_env,GRUB_COMMAND_FLAG_BOTH , +"list_env [-f FILE]" ,"List variables from environment block file." , options); +grub_register_command ("save_env" , grub_cmd_save_env,GRUB_COMMAND_FLAG_BOTH , +"save_env [-f FILE] variable_name [...]" ,"Save variables to environment block file." , options); +} + +GRUB_MOD_FINI (loadenv) +{ +grub_unregister_command ("load_env" ); +grub_unregister_command ("list_env" ); +grub_unregister_command ("save_env" ); +}
conf/common.rmk +21 -11
@@ -94,6 +94,11 @@ grub_fstest_init.c: grub_fstest_init.lst $(filter-out grub_fstest_init.c,$(grub_ rm -f $@; sh $(srcdir)/geninit.sh $< $(filter %.c,$^) > $@ DISTCLEANFILES += grub_fstest_init.c +# for grub-editenv +bin_UTILITIES += grub-editenv +grub_editenv_SOURCES = util/grub-editenv.c util/envblk.c util/misc.c kern/misc.c kern/err.c +CLEANFILES += grub-editenv + # For update-grub update-grub: util/update-grub.in config.status ./config.status --file=$@:$<
@@ -282,7 +287,7 @@ pkglib_MODULES += hello.mod boot.mod terminal.mod ls.mod \ cmp.mod cat.mod help.mod font.mod search.mod \ loopback.mod fs_uuid.mod configfile.mod echo.mod \ terminfo.mod test.mod blocklist.mod hexdump.mod \ - read.mod sleep.mod + read.mod sleep.mod loadenv.mod # For hello.mod. hello_mod_SOURCES = hello/hello.c
@@ -369,6 +374,21 @@ hexdump_mod_SOURCES = commands/hexdump.c hexdump_mod_CFLAGS = $(COMMON_CFLAGS) hexdump_mod_LDFLAGS = $(COMMON_LDFLAGS) +# For read.mod. +read_mod_SOURCES = commands/read.c +read_mod_CFLAGS = $(COMMON_CFLAGS) +read_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For sleep.mod. +sleep_mod_SOURCES = commands/sleep.c +sleep_mod_CFLAGS = $(COMMON_CFLAGS) +sleep_mod_LDFLAGS = $(COMMON_LDFLAGS) + +# For loadenv.mod. +loadenv_mod_SOURCES = commands/loadenv.c util/envblk.c +loadenv_mod_CFLAGS = $(COMMON_CFLAGS) +loadenv_mod_LDFLAGS = $(COMMON_LDFLAGS) + # Misc. pkglib_MODULES += gzio.mod elf.mod
@@ -381,13 +401,3 @@ elf_mod_LDFLAGS = $(COMMON_LDFLAGS) gzio_mod_SOURCES = io/gzio.c gzio_mod_CFLAGS = $(COMMON_CFLAGS) gzio_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For read.mod. -read_mod_SOURCES = commands/read.c -read_mod_CFLAGS = $(COMMON_CFLAGS) -read_mod_LDFLAGS = $(COMMON_LDFLAGS) - -# For sleep.mod. -sleep_mod_SOURCES = commands/sleep.c -sleep_mod_CFLAGS = $(COMMON_CFLAGS) -sleep_mod_LDFLAGS = $(COMMON_LDFLAGS)
include/grub/envblk.h new file +45 -0
45 lines not on this page · raw message
util/envblk.c new file +156 -0
156 lines not on this page · raw message
util/grub-editenv.c new file +258 -0
258 lines not on this page · raw message
Quoted 4 lines · Bean
Hi,
This is the new patch, some changes:
Quoted 8 lines
1,
envblk.h, removeGRUB_ENVBLK_RDIRand the like, they're not needed in this patch.
2,util/envblk.c, use grub_* function for string manipulation. 3,commands/loadenv.c, use grub_disk_read/grub_disk_write to read/write disk, the problem of lower level api is that they don't update the cache.If there is no problem, I'd like to commit this soon.
Fine with me!
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What good is a phone call… if you are unable to speak? (as seen on /.)
Quoted 12 lines · Robert Millan
Quoted 11 lines · Bean
Hi,
This is the new patch, some changes:
1,
envblk.h, removeGRUB_ENVBLK_RDIRand the like, they're not needed in this patch.
2,util/envblk.c, use grub_* function for string manipulation. 3,commands/loadenv.c, use grub_disk_read/grub_disk_write to read/write disk, the problem of lower level api is that they don't update the cache.
Quoted 3 lines
Quoted 1 line
If there is no problem, I'd like to commit this soon.
Fine with me!
Committed.
Signature · 1 line
Bean
Quoted 13 lines · Bean
Quoted 12 lines · Robert Millan
Quoted 11 lines · Bean
Hi,
This is the new patch, some changes:
1,
envblk.h, removeGRUB_ENVBLK_RDIRand the like, they're not needed in this patch.
2,util/envblk.c, use grub_* function for string manipulation. 3,commands/loadenv.c, use grub_disk_read/grub_disk_write to read/write disk, the problem of lower level api is that they don't update the cache.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
If there is no problem, I'd like to commit this soon.
Fine with me!
Committed.
Great! Can you explain how it works?
--
Marco
Quoted 1 line · Marco Gerards
Great! Can you explain how it works?
Very good question. It's not "discoverable". I could not find way to figure out that /boot/grub/grubenv is the default without looking at the code.
load_env without arguments merely prints "error: file not found" without telling which file it needs. Help texts are silent about the defaults. Many commands ignore extra arguments silently. The whole code needs a serious cleanup with end users in mind.
We also have too many commands regarding environment. There is even freebsd_loadenv, which should probably merged with load_env somehow. Or maybe not. Maybe all native environment files should be handled with one command, such as "env".
We also need mechanisms to implement "savedefault" functionality, perhaps with easy examples. Maybe update-grub should use it.
Anyway, to start using it, create the environment file: grub-editenv (by the way, grub-env would be a better name):
grub-editenv /boot/grub/grubenv create
grub-editenv /boot/grub/grubenv set foo=bar
Now you can inspect it with "list_env", load it into environment with "load_env" and save variables into it with "save_env".
Signature · 2 lines
Regards, Pavel Roskin
Quoted 4 lines · Pavel Roskin
Quoted 1 line · Marco Gerards
Great! Can you explain how it works?
Quoted 8 lines
Very good question. It's not "discoverable". I could not find way to figure out that
/boot/grub/grubenvis the default without looking at the code.
load_envwithout arguments merely prints "error: file not found" without telling which file it needs. Help texts are silent about the defaults. Many commands ignore extra arguments silently. The whole code needs a serious cleanup with end users in mind.
load_env would load /boot/grub/grubenv with no input, you can also overwrite the default with -f parameter.
BTW, I think update-grub should create a new grubenv if it doesn't exists, so that user won't see the file not found error.
Quoted 5 lines
We also have too many commands regarding environment. There is even
freebsd_loadenv, which should probably merged withload_envsomehow. Or maybe not. Maybe all native environment files should be handled with one command, such as "env".
freebsd_loadenv is used to load the hint file, which is plain text. It also add the FreeBSD prefix so that they won't conflict with native variables of grub2.
Quoted 4 lines
We also need mechanisms to implement "savedefault" functionality, perhaps with easy examples. Maybe update-grub should use it.
Quoted 8 lines
Anyway, to start using it, create the environment file: grub-editenv (by the way, grub-env would be a better name):
grub-editenv
/boot/grub/grubenvcreate
grub-editenv/boot/grub/grubenvset foo=barNow you can inspect it with "
list_env", load it into environment with "load_env" and save variables into it with "save_env".
Yes, this is the basic idea.
Signature · 1 line
Bean
Quoted 3 lines · Bean
BTW, I think update-grub should create a new grubenv if it doesn't exists, so that user won't see the file not found error.
Would be interesting if update-grub added a "savedefault" blob to each menuentry, and loaded the default afterwards (unless instructed otherwise by the user).
Signature · 5 lines
Robert Millan <GPLv2> I know my rights; I want my phone call! <DRM> What good is a phone call… if you are unable to speak? (as seen on /.)
Quoted 7 lines · Pavel Roskin
Quoted 1 line · Marco Gerards
Great! Can you explain how it works?
Very good question. It's not "discoverable". I could not find way to figure out that
/boot/grub/grubenvis the default without looking at the code.
Actually, I meant how it works technically.
--
Marco
Quoted 11 lines · Marco Gerards
Quoted 7 lines · Pavel Roskin
Quoted 1 line · Marco Gerards
Great! Can you explain how it works?
Very good question. It's not "discoverable". I could not find way to figure out that
/boot/grub/grubenvis the default without looking at the code.Actually, I meant how it works technically.
Hi,
It first reads the first 8192 bytes into memory, then, set read_hook and do it again. Inside the hook, it records the sector location. It then uses the location list to read from disk, and compare the result. If they match, we assume it's the right one.
There are some consideration about the environment file. First, it must be at least 8192 bytes long, this is used to avoid tail packing in some fs. Then, we have the signature "GeNv", followed by the length of the block. The signature doesn't need to the at the beginning, but it has to been within the first 8192 byte, and dword aligned. This is used to simplify the search function, also reduce the change of a mismatch. The environment block consists of name=value strings, terminated by an empty string.
The environment block can be embedded in other files, like core.img. Then we can use grub-editenv, or save_env from grub console, to set variable which can be used to locate root at runtime, like uuid, etc. But this is the subject of another patch.
Signature · 1 line
Bean