[PATCH v2] bg_setenv: add option to preserve existing env

8 views
Skip to first unread message

Michael Adler

unread,
Oct 8, 2021, 3:44:25 AM10/8/21
to efibootg...@googlegroups.com, Michael Adler
The default behavior of bg_setenv is to discard any existing entries in
BGENV.dat before applying the new settings provided by the command line.

This is sometimes undesirable, e.g. it is not possible to update the
kernelargs without specifying the path to the kernel (well actually, it
is, but the system won't boot any more since the new BGENV.dat has no
kernel set).

This commit introduces the new argument `--preserve` (short: `-P`) for
bg_setenv. With it, all existing entries are preserved while updating
the ones specified via the command line. For instance,

bg_setenv -P -f BGENV.DAT --args "root=/dev/sdb"

would update the kernelargs while preserving all other settings.

Signed-off-by: Michael Adler <michae...@siemens.com>
---
tools/bg_setenv.c | 11 +++++++++++
1 file changed, 11 insertions(+)

diff --git a/tools/bg_setenv.c b/tools/bg_setenv.c
index d26eeed..9056102 100644
--- a/tools/bg_setenv.c
+++ b/tools/bg_setenv.c
@@ -25,6 +25,7 @@ static char doc[] =
"bg_setenv/bg_printenv - Environment tool for the EFI Boot Guard";

static struct argp_option options_setenv[] = {
+ {"preserve", 'P', 0, 0, "Preserve existing entries"},
{"kernel", 'k', "KERNEL", 0, "Set kernel to load"},
{"args", 'a', "KERNEL_ARGS", 0, "Set kernel arguments"},
{"part", 'p', "ENV_PART", 0,
@@ -173,6 +174,9 @@ static bool verbosity = false;

static char *envfilepath = NULL;

+/* whether to keep existing entries in BGENV before applying the new settings */
+static bool preserve_env = false;
+
static char *ustatemap[] = {"OK", "INSTALLED", "TESTING", "FAILED", "UNKNOWN"};

static uint8_t str2ustate(char *str)
@@ -410,6 +414,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
/* Set user-defined variable(s) */
e = set_uservars(arg);
break;
+ case 'P':
+ preserve_env = true;
+ break;
case 'V':
fprintf(stdout, "EFI Boot Guard %s\n", EFIBOOTGUARD_VERSION);
exit(0);
@@ -608,6 +615,10 @@ static int dumpenv_to_file(char *envfilepath) {
memset(&data, 0, sizeof(BG_ENVDATA));
env.data = &data;

+ if (preserve_env && !get_env(envfilepath, &data)) {
+ return 1;
+ }
+
update_environment(&env);
if (verbosity) {
dump_env(env.data);
--
2.33.0

Jan Kiszka

unread,
Oct 8, 2021, 4:04:29 AM10/8/21
to Michael Adler, efibootg...@googlegroups.com
Thanks, applied.

Looking at the error handling of dumpenv_to_file at this chance, we need
another patch...

Jan

--
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
Reply all
Reply to author
Forward
0 new messages