|
|
5593c8 |
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
|
5593c8 |
From: Peter Jones <pjones@redhat.com>
|
|
|
5593c8 |
Date: Wed, 16 Jan 2019 13:21:46 -0500
|
|
|
5593c8 |
Subject: [PATCH] Add efi-export-env and efi-load-env commands
|
|
|
5593c8 |
|
|
|
5593c8 |
This adds "efi-export-env VARIABLE" and "efi-load-env", which manipulate the
|
|
|
5593c8 |
environment block stored in the EFI variable
|
|
|
5593c8 |
GRUB_ENV-91376aff-cba6-42be-949d-06fde81128e8.
|
|
|
5593c8 |
|
|
|
5593c8 |
Signed-off-by: Peter Jones <pjones@redhat.com>
|
|
|
5593c8 |
---
|
|
|
5593c8 |
grub-core/Makefile.core.def | 6 ++
|
|
|
5593c8 |
grub-core/commands/efi/env.c | 168 +++++++++++++++++++++++++++++++++++++++++++
|
|
|
5593c8 |
grub-core/kern/efi/efi.c | 3 +
|
|
|
5593c8 |
grub-core/kern/efi/init.c | 5 --
|
|
|
5593c8 |
grub-core/lib/envblk.c | 43 +++++++++++
|
|
|
5593c8 |
util/grub-set-bootflag.c | 1 +
|
|
|
5593c8 |
include/grub/efi/efi.h | 5 ++
|
|
|
5593c8 |
include/grub/lib/envblk.h | 3 +
|
|
|
5593c8 |
8 files changed, 229 insertions(+), 5 deletions(-)
|
|
|
5593c8 |
create mode 100644 grub-core/commands/efi/env.c
|
|
|
5593c8 |
|
|
|
5593c8 |
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
|
|
|
d3c3ab |
index 1e15345107e..81fc274148e 100644
|
|
|
5593c8 |
--- a/grub-core/Makefile.core.def
|
|
|
5593c8 |
+++ b/grub-core/Makefile.core.def
|
|
|
5593c8 |
@@ -820,6 +820,12 @@ module = {
|
|
|
5593c8 |
enable = efi;
|
|
|
5593c8 |
};
|
|
|
5593c8 |
|
|
|
5593c8 |
+module = {
|
|
|
5593c8 |
+ name = efienv;
|
|
|
5593c8 |
+ common = commands/efi/env.c;
|
|
|
5593c8 |
+ enable = efi;
|
|
|
5593c8 |
+};
|
|
|
5593c8 |
+
|
|
|
5593c8 |
module = {
|
|
|
5593c8 |
name = efifwsetup;
|
|
|
5593c8 |
efi = commands/efi/efifwsetup.c;
|
|
|
5593c8 |
diff --git a/grub-core/commands/efi/env.c b/grub-core/commands/efi/env.c
|
|
|
5593c8 |
new file mode 100644
|
|
|
d3c3ab |
index 00000000000..cbd13e03e81
|
|
|
5593c8 |
--- /dev/null
|
|
|
5593c8 |
+++ b/grub-core/commands/efi/env.c
|
|
|
5593c8 |
@@ -0,0 +1,168 @@
|
|
|
5593c8 |
+/*
|
|
|
5593c8 |
+ * GRUB -- GRand Unified Bootloader
|
|
|
5593c8 |
+ * Copyright (C) 2012 Free Software Foundation, Inc.
|
|
|
5593c8 |
+ *
|
|
|
5593c8 |
+ * GRUB is free software: you can redistribute it and/or modify
|
|
|
5593c8 |
+ * it under the terms of the GNU General Public License as published by
|
|
|
5593c8 |
+ * the Free Software Foundation, either version 3 of the License, or
|
|
|
5593c8 |
+ * (at your option) any later version.
|
|
|
5593c8 |
+ *
|
|
|
5593c8 |
+ * GRUB is distributed in the hope that it will be useful,
|
|
|
5593c8 |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
5593c8 |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
5593c8 |
+ * GNU General Public License for more details.
|
|
|
5593c8 |
+ *
|
|
|
5593c8 |
+ * You should have received a copy of the GNU General Public License
|
|
|
5593c8 |
+ * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
|
|
|
5593c8 |
+ */
|
|
|
5593c8 |
+#include <grub/dl.h>
|
|
|
5593c8 |
+#include <grub/mm.h>
|
|
|
5593c8 |
+#include <grub/misc.h>
|
|
|
5593c8 |
+#include <grub/types.h>
|
|
|
5593c8 |
+#include <grub/mm.h>
|
|
|
5593c8 |
+#include <grub/misc.h>
|
|
|
5593c8 |
+#include <grub/efi/api.h>
|
|
|
5593c8 |
+#include <grub/efi/efi.h>
|
|
|
5593c8 |
+#include <grub/env.h>
|
|
|
5593c8 |
+#include <grub/lib/envblk.h>
|
|
|
5593c8 |
+#include <grub/command.h>
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+GRUB_MOD_LICENSE ("GPLv3+");
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static const grub_efi_guid_t grub_env_guid = GRUB_EFI_GRUB_VARIABLE_GUID;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static grub_err_t
|
|
|
5593c8 |
+grub_efi_export_env(grub_command_t cmd __attribute__ ((unused)),
|
|
|
5593c8 |
+ int argc, char *argv[])
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ const char *value;
|
|
|
5593c8 |
+ char *old_value;
|
|
|
5593c8 |
+ struct grub_envblk envblk_s = { NULL, 0 };
|
|
|
5593c8 |
+ grub_envblk_t envblk = &envblk_s;
|
|
|
5593c8 |
+ grub_err_t err;
|
|
|
5593c8 |
+ int changed = 1;
|
|
|
5593c8 |
+ grub_efi_status_t status;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "argc:%d\n", argc);
|
|
|
5593c8 |
+ for (int i = 0; i < argc; i++)
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "argv[%d]: %s\n", i, argv[i]);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (argc != 1)
|
|
|
5593c8 |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("variable name expected"));
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_efi_get_variable ("GRUB_ENV", &grub_env_guid, &envblk_s.size,
|
|
|
5593c8 |
+ (void **) &envblk_s.buf);
|
|
|
5593c8 |
+ if (!envblk_s.buf || envblk_s.size < 1)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ char *buf = grub_malloc (1025);
|
|
|
5593c8 |
+ if (!buf)
|
|
|
5593c8 |
+ return grub_errno;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_memcpy (buf, GRUB_ENVBLK_SIGNATURE, sizeof (GRUB_ENVBLK_SIGNATURE) - 1);
|
|
|
5593c8 |
+ grub_memset (buf + sizeof (GRUB_ENVBLK_SIGNATURE) - 1, '#',
|
|
|
5593c8 |
+ DEFAULT_ENVBLK_SIZE - sizeof (GRUB_ENVBLK_SIGNATURE) + 1);
|
|
|
5593c8 |
+ buf[1024] = '\0';
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ envblk_s.buf = buf;
|
|
|
5593c8 |
+ envblk_s.size = 1024;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+ else
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ char *buf = grub_realloc (envblk_s.buf, envblk_s.size + 1);
|
|
|
5593c8 |
+ if (!buf)
|
|
|
5593c8 |
+ return grub_errno;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ envblk_s.buf = buf;
|
|
|
5593c8 |
+ envblk_s.buf[envblk_s.size] = '\0';
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ err = grub_envblk_get(envblk, argv[0], &old_value);
|
|
|
5593c8 |
+ if (err != GRUB_ERR_NONE)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "grub_envblk_get returned %d\n", err);
|
|
|
5593c8 |
+ return err;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ value = grub_env_get(argv[0]);
|
|
|
5593c8 |
+ if ((!value && !old_value) ||
|
|
|
5593c8 |
+ (value && old_value && !grub_strcmp(old_value, value)))
|
|
|
5593c8 |
+ changed = 0;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (old_value)
|
|
|
5593c8 |
+ grub_free(old_value);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (changed == 0)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "No changes necessary\n");
|
|
|
5593c8 |
+ return 0;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (value)
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "setting \"%s\" to \"%s\"\n", argv[0], value);
|
|
|
5593c8 |
+ grub_envblk_set(envblk, argv[0], value);
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+ else
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "deleting \"%s\" from envblk\n", argv[0]);
|
|
|
5593c8 |
+ grub_envblk_delete(envblk, argv[0]);
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "envblk is %lu bytes:\n\"%s\"\n", envblk_s.size, envblk_s.buf);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "removing GRUB_ENV\n");
|
|
|
5593c8 |
+ status = grub_efi_set_variable ("GRUB_ENV", &grub_env_guid, NULL, 0);
|
|
|
5593c8 |
+ if (status != GRUB_EFI_SUCCESS)
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "removal returned %ld\n", status);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "setting GRUB_ENV\n");
|
|
|
5593c8 |
+ status = grub_efi_set_variable ("GRUB_ENV", &grub_env_guid,
|
|
|
5593c8 |
+ envblk_s.buf, envblk_s.size);
|
|
|
5593c8 |
+ if (status != GRUB_EFI_SUCCESS)
|
|
|
5593c8 |
+ grub_dprintf ("efienv", "setting GRUB_ENV returned %ld\n", status);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return 0;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static int
|
|
|
5593c8 |
+set_var (const char *name, const char *value,
|
|
|
5593c8 |
+ void *whitelist __attribute__((__unused__)))
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ grub_env_set (name, value);
|
|
|
5593c8 |
+ return 0;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static grub_err_t
|
|
|
5593c8 |
+grub_efi_load_env(grub_command_t cmd __attribute__ ((unused)),
|
|
|
5593c8 |
+ int argc, char *argv[] __attribute__((__unused__)))
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ struct grub_envblk envblk_s = { NULL, 0 };
|
|
|
5593c8 |
+ grub_envblk_t envblk = &envblk_s;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_efi_get_variable ("GRUB_ENV", &grub_env_guid, &envblk_s.size,
|
|
|
5593c8 |
+ (void **) &envblk_s.buf);
|
|
|
5593c8 |
+ if (!envblk_s.buf || envblk_s.size < 1)
|
|
|
5593c8 |
+ return 0;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (argc > 0)
|
|
|
5593c8 |
+ return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("unexpected argument"));
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_envblk_iterate (envblk, NULL, set_var);
|
|
|
5593c8 |
+ grub_free (envblk_s.buf);
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static grub_command_t export_cmd, loadenv_cmd;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+GRUB_MOD_INIT(lsefi)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ export_cmd = grub_register_command ("efi-export-env", grub_efi_export_env,
|
|
|
5593c8 |
+ N_("VARIABLE_NAME"), N_("Export environment variable to UEFI."));
|
|
|
5593c8 |
+ loadenv_cmd = grub_register_command ("efi-load-env", grub_efi_load_env,
|
|
|
5593c8 |
+ NULL, N_("Load the grub environment from UEFI."));
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+GRUB_MOD_FINI(lsefi)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ grub_unregister_command (export_cmd);
|
|
|
5593c8 |
+ grub_unregister_command (loadenv_cmd);
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
diff --git a/grub-core/kern/efi/efi.c b/grub-core/kern/efi/efi.c
|
|
|
d3c3ab |
index 2a446f5031b..14bc10eb564 100644
|
|
|
5593c8 |
--- a/grub-core/kern/efi/efi.c
|
|
|
5593c8 |
+++ b/grub-core/kern/efi/efi.c
|
|
|
5593c8 |
@@ -225,6 +225,9 @@ grub_efi_set_variable(const char *var, const grub_efi_guid_t *guid,
|
|
|
5593c8 |
if (status == GRUB_EFI_SUCCESS)
|
|
|
5593c8 |
return GRUB_ERR_NONE;
|
|
|
5593c8 |
|
|
|
5593c8 |
+ if (status == GRUB_EFI_NOT_FOUND && datasize == 0)
|
|
|
5593c8 |
+ return GRUB_ERR_NONE;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
return grub_error (GRUB_ERR_IO, "could not set EFI variable `%s'", var);
|
|
|
5593c8 |
}
|
|
|
5593c8 |
|
|
|
5593c8 |
diff --git a/grub-core/kern/efi/init.c b/grub-core/kern/efi/init.c
|
|
|
d3c3ab |
index 2d12e6188fd..0574d8d6217 100644
|
|
|
5593c8 |
--- a/grub-core/kern/efi/init.c
|
|
|
5593c8 |
+++ b/grub-core/kern/efi/init.c
|
|
|
5593c8 |
@@ -85,11 +85,6 @@ stack_protector_init (void)
|
|
|
5593c8 |
|
|
|
5593c8 |
grub_addr_t grub_modbase;
|
|
|
5593c8 |
|
|
|
5593c8 |
-#define GRUB_EFI_GRUB_VARIABLE_GUID \
|
|
|
5593c8 |
- { 0x91376aff, 0xcba6, 0x42be, \
|
|
|
5593c8 |
- { 0x94, 0x9d, 0x06, 0xfd, 0xe8, 0x11, 0x28, 0xe8 } \
|
|
|
5593c8 |
- }
|
|
|
5593c8 |
-
|
|
|
5593c8 |
/* Helper for grub_efi_env_init */
|
|
|
5593c8 |
static int
|
|
|
5593c8 |
set_var (const char *name, const char *value,
|
|
|
5593c8 |
diff --git a/grub-core/lib/envblk.c b/grub-core/lib/envblk.c
|
|
|
d3c3ab |
index 2e4e78b132d..874506da169 100644
|
|
|
5593c8 |
--- a/grub-core/lib/envblk.c
|
|
|
5593c8 |
+++ b/grub-core/lib/envblk.c
|
|
|
5593c8 |
@@ -223,6 +223,49 @@ grub_envblk_delete (grub_envblk_t envblk, const char *name)
|
|
|
5593c8 |
}
|
|
|
5593c8 |
}
|
|
|
5593c8 |
|
|
|
5593c8 |
+struct get_var_state {
|
|
|
5593c8 |
+ const char * const name;
|
|
|
5593c8 |
+ char * value;
|
|
|
5593c8 |
+ int found;
|
|
|
5593c8 |
+};
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+static int
|
|
|
5593c8 |
+get_var (const char * const name, const char * const value, void *statep)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ struct get_var_state *state = (struct get_var_state *)statep;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (!grub_strcmp(state->name, name))
|
|
|
5593c8 |
+ {
|
|
|
5593c8 |
+ state->found = 1;
|
|
|
5593c8 |
+ state->value = grub_strdup(value);
|
|
|
5593c8 |
+ if (!state->value)
|
|
|
5593c8 |
+ grub_errno = grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return 1;
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return 0;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+grub_err_t
|
|
|
5593c8 |
+grub_envblk_get (grub_envblk_t envblk, const char * const name, char ** const value)
|
|
|
5593c8 |
+{
|
|
|
5593c8 |
+ struct get_var_state state = {
|
|
|
5593c8 |
+ .name = name,
|
|
|
5593c8 |
+ .value = NULL,
|
|
|
5593c8 |
+ .found = 0,
|
|
|
5593c8 |
+ };
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ grub_envblk_iterate(envblk, (void *)&state, get_var);
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ *value = state.value;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ if (state.found && !state.value)
|
|
|
5593c8 |
+ return grub_errno;
|
|
|
5593c8 |
+
|
|
|
5593c8 |
+ return GRUB_ERR_NONE;
|
|
|
5593c8 |
+}
|
|
|
5593c8 |
+
|
|
|
5593c8 |
void
|
|
|
5593c8 |
grub_envblk_iterate (grub_envblk_t envblk,
|
|
|
5593c8 |
void *hook_data,
|
|
|
5593c8 |
diff --git a/util/grub-set-bootflag.c b/util/grub-set-bootflag.c
|
|
|
d3c3ab |
index bb198f02351..6a79ee67444 100644
|
|
|
5593c8 |
--- a/util/grub-set-bootflag.c
|
|
|
5593c8 |
+++ b/util/grub-set-bootflag.c
|
|
|
5593c8 |
@@ -25,6 +25,7 @@
|
|
|
5593c8 |
|
|
|
5593c8 |
#include <config-util.h> /* For *_DIR_NAME defines */
|
|
|
5593c8 |
#include <grub/types.h>
|
|
|
5593c8 |
+#include <grub/err.h>
|
|
|
5593c8 |
#include <grub/lib/envblk.h> /* For GRUB_ENVBLK_DEFCFG define */
|
|
|
5593c8 |
#include <errno.h>
|
|
|
5593c8 |
#include <stdio.h>
|
|
|
5593c8 |
diff --git a/include/grub/efi/efi.h b/include/grub/efi/efi.h
|
|
|
d3c3ab |
index 2e0691454b1..8dfc89a33b9 100644
|
|
|
5593c8 |
--- a/include/grub/efi/efi.h
|
|
|
5593c8 |
+++ b/include/grub/efi/efi.h
|
|
|
5593c8 |
@@ -24,6 +24,11 @@
|
|
|
5593c8 |
#include <grub/dl.h>
|
|
|
5593c8 |
#include <grub/efi/api.h>
|
|
|
5593c8 |
|
|
|
5593c8 |
+#define GRUB_EFI_GRUB_VARIABLE_GUID \
|
|
|
5593c8 |
+ { 0x91376aff, 0xcba6, 0x42be, \
|
|
|
5593c8 |
+ { 0x94, 0x9d, 0x06, 0xfd, 0xe8, 0x11, 0x28, 0xe8 } \
|
|
|
5593c8 |
+ }
|
|
|
5593c8 |
+
|
|
|
5593c8 |
/* Variables. */
|
|
|
5593c8 |
extern grub_efi_system_table_t *EXPORT_VAR(grub_efi_system_table);
|
|
|
5593c8 |
extern grub_efi_handle_t EXPORT_VAR(grub_efi_image_handle);
|
|
|
5593c8 |
diff --git a/include/grub/lib/envblk.h b/include/grub/lib/envblk.h
|
|
|
d3c3ab |
index c3e65592170..ab969af2461 100644
|
|
|
5593c8 |
--- a/include/grub/lib/envblk.h
|
|
|
5593c8 |
+++ b/include/grub/lib/envblk.h
|
|
|
5593c8 |
@@ -22,6 +22,8 @@
|
|
|
5593c8 |
#define GRUB_ENVBLK_SIGNATURE "# GRUB Environment Block\n"
|
|
|
5593c8 |
#define GRUB_ENVBLK_DEFCFG "grubenv"
|
|
|
5593c8 |
|
|
|
5593c8 |
+#define DEFAULT_ENVBLK_SIZE 1024
|
|
|
5593c8 |
+
|
|
|
5593c8 |
#ifndef ASM_FILE
|
|
|
5593c8 |
|
|
|
5593c8 |
struct grub_envblk
|
|
|
5593c8 |
@@ -33,6 +35,7 @@ typedef struct grub_envblk *grub_envblk_t;
|
|
|
5593c8 |
|
|
|
5593c8 |
grub_envblk_t grub_envblk_open (char *buf, grub_size_t size);
|
|
|
5593c8 |
int grub_envblk_set (grub_envblk_t envblk, const char *name, const char *value);
|
|
|
5593c8 |
+grub_err_t grub_envblk_get (grub_envblk_t envblk, const char * const name, char ** const value);
|
|
|
5593c8 |
void grub_envblk_delete (grub_envblk_t envblk, const char *name);
|
|
|
5593c8 |
void grub_envblk_iterate (grub_envblk_t envblk,
|
|
|
5593c8 |
void *hook_data,
|