nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0110-Add-efi-export-env-and-efi-load-env-commands.patch

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