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 <[email protected]>
---
 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

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20211008074238.604102-1-michael.adler%40siemens.com.

Reply via email to