dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone
d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
Date: Fri, 2 Feb 2018 11:36:29 +0100
d9d99f
Subject: [PATCH] Enable blscfg command for the emu platform
d9d99f
d9d99f
Allow grub-emu to call a blscfg command. This may be useful for platforms
d9d99f
that don't support GRUB, so grub-emu can be used to parse the BLS configs
d9d99f
and kexec a new kernel using that information.
d9d99f
d9d99f
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
---
d9d99f
 grub-core/Makefile.core.def |  1 +
d9d99f
 grub-core/commands/blscfg.c | 46 +++++++++++++++++++++++++++++++++++----------
d9d99f
 2 files changed, 37 insertions(+), 10 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
d9d99f
index 96ccb402125..e52d776887a 100644
d9d99f
--- a/grub-core/Makefile.core.def
d9d99f
+++ b/grub-core/Makefile.core.def
d9d99f
@@ -777,6 +777,7 @@ module = {
d9d99f
   common = commands/blscfg.c;
d9d99f
   enable = efi;
d9d99f
   enable = i386_pc;
d9d99f
+  enable = emu;
d9d99f
 };
d9d99f
 
d9d99f
 module = {
d9d99f
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
d9d99f
index 86796c8cd83..e0b65534af4 100644
d9d99f
--- a/grub-core/commands/blscfg.c
d9d99f
+++ b/grub-core/commands/blscfg.c
d9d99f
@@ -37,7 +37,12 @@ GRUB_MOD_LICENSE ("GPLv3+");
d9d99f
 #include "loadenv.h"
d9d99f
 
d9d99f
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
d9d99f
+#ifdef GRUB_MACHINE_EMU
d9d99f
+#define GRUB_BOOT_DEVICE "/boot"
d9d99f
+#else
d9d99f
 #define GRUB_BOOT_DEVICE "($root)"
d9d99f
+#endif
d9d99f
+
d9d99f
 #ifdef GRUB_MACHINE_EFI
d9d99f
 #define GRUB_LINUX_CMD "linuxefi"
d9d99f
 #define GRUB_INITRD_CMD "initrdefi"
d9d99f
@@ -46,6 +51,13 @@ GRUB_MOD_LICENSE ("GPLv3+");
d9d99f
 #define GRUB_INITRD_CMD "initrd"
d9d99f
 #endif
d9d99f
 
d9d99f
+enum
d9d99f
+  {
d9d99f
+    PLATFORM_EFI,
d9d99f
+    PLATFORM_EMU,
d9d99f
+    PLATFORM_BIOS,
d9d99f
+  };
d9d99f
+
d9d99f
 #define grub_free(x) ({grub_dprintf("blscfg", "%s freeing %p\n", __func__, x); grub_free(x); })
d9d99f
 
d9d99f
 struct keyval
d9d99f
@@ -641,7 +653,7 @@ finish:
d9d99f
 struct find_entry_info {
d9d99f
 	grub_device_t dev;
d9d99f
 	grub_fs_t fs;
d9d99f
-	int efi;
d9d99f
+	int platform;
d9d99f
 };
d9d99f
 
d9d99f
 /*
d9d99f
@@ -668,13 +680,16 @@ static int find_entry (const char *filename,
d9d99f
       !grub_strcmp (filename, ".."))
d9d99f
     return 0;
d9d99f
 
d9d99f
-  if (info->efi && !grub_strcasecmp (filename, "boot"))
d9d99f
+  if (info->platform == PLATFORM_EFI && !grub_strcasecmp (filename, "boot"))
d9d99f
     return 0;
d9d99f
 
d9d99f
   saved_env_buf = grub_malloc (512);
d9d99f
 
d9d99f
   // set a default blsdir
d9d99f
-  if (info->efi)
d9d99f
+  if (info->platform == PLATFORM_EMU)
d9d99f
+    default_blsdir = grub_xasprintf ("%s%s", GRUB_BOOT_DEVICE,
d9d99f
+				     GRUB_BLS_CONFIG_PATH);
d9d99f
+  else if (info->platform == PLATFORM_EFI)
d9d99f
     default_blsdir = grub_xasprintf ("/EFI/%s%s", filename,
d9d99f
 				     GRUB_BLS_CONFIG_PATH);
d9d99f
   else
d9d99f
@@ -686,7 +701,7 @@ static int find_entry (const char *filename,
d9d99f
   /*
d9d99f
    * try to load a grubenv from /EFI/wherever/grubenv
d9d99f
    */
d9d99f
-  if (info->efi)
d9d99f
+  if (info->platform == PLATFORM_EFI)
d9d99f
     grubenv_path = grub_xasprintf ("(%s)/EFI/%s/grubenv", devid, filename);
d9d99f
   else
d9d99f
     grubenv_path = grub_xasprintf ("(%s)/grub2/grubenv", devid);
d9d99f
@@ -740,7 +755,7 @@ static int find_entry (const char *filename,
d9d99f
     goto finish;
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "blsdir: \"%s\"\n", blsdir);
d9d99f
-  if (blsdir[0] != '/' && info->efi)
d9d99f
+  if (blsdir[0] != '/' && info->platform == PLATFORM_EFI)
d9d99f
     blsdir = grub_xasprintf ("/EFI/%s/%s/", filename, blsdir);
d9d99f
   else
d9d99f
     blsdir = grub_strdup (blsdir);
d9d99f
@@ -818,15 +833,21 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
d9d99f
     {
d9d99f
       .dev = NULL,
d9d99f
       .fs = NULL,
d9d99f
-      .efi = 0,
d9d99f
+      .platform = PLATFORM_BIOS,
d9d99f
     };
d9d99f
 
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "finding boot\n");
d9d99f
+
d9d99f
+#ifdef GRUB_MACHINE_EMU
d9d99f
+  devid = "host";
d9d99f
+  grub_env_set ("boot", devid);
d9d99f
+#else
d9d99f
   devid = grub_env_get ("boot");
d9d99f
   if (!devid)
d9d99f
     return grub_error (GRUB_ERR_FILE_NOT_FOUND,
d9d99f
 		       N_("variable `%s' isn't set"), "boot");
d9d99f
+#endif
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "opening %s\n", devid);
d9d99f
   dev = grub_device_open (devid);
d9d99f
@@ -844,11 +865,16 @@ grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
d9d99f
   info.dev = dev;
d9d99f
   info.fs = fs;
d9d99f
 #ifdef GRUB_MACHINE_EFI
d9d99f
-  info.efi = 1;
d9d99f
+  info.platform = PLATFORM_EFI;
d9d99f
   grub_dprintf ("blscfg", "scanning /EFI/\n");
d9d99f
   r = fs->dir (dev, "/EFI/", find_entry, &info;;
d9d99f
+#elif GRUB_MACHINE_EMU
d9d99f
+  info.platform = PLATFORM_EMU;
d9d99f
+  grub_dprintf ("blscfg", "scanning %s%s\n", GRUB_BOOT_DEVICE,
d9d99f
+		GRUB_BLS_CONFIG_PATH);
d9d99f
+  r = fs->dir (dev, "/boot/loader/",
d9d99f
+	       find_entry, &info;;
d9d99f
 #else
d9d99f
-  info.efi = 0;
d9d99f
   grub_dprintf ("blscfg", "scanning %s\n", GRUB_BLS_CONFIG_PATH);
d9d99f
   r = fs->dir (dev, "/", find_entry, &info;;
d9d99f
 #endif
d9d99f
@@ -863,7 +889,7 @@ finish:
d9d99f
 static grub_extcmd_t cmd;
d9d99f
 static grub_extcmd_t oldcmd;
d9d99f
 
d9d99f
-GRUB_MOD_INIT(bls)
d9d99f
+GRUB_MOD_INIT(blscfg)
d9d99f
 {
d9d99f
   grub_dprintf("blscfg", "%s got here\n", __func__);
d9d99f
   cmd = grub_register_extcmd ("blscfg",
d9d99f
@@ -880,7 +906,7 @@ GRUB_MOD_INIT(bls)
d9d99f
 				 NULL);
d9d99f
 }
d9d99f
 
d9d99f
-GRUB_MOD_FINI(bls)
d9d99f
+GRUB_MOD_FINI(blscfg)
d9d99f
 {
d9d99f
   grub_unregister_extcmd (cmd);
d9d99f
   grub_unregister_extcmd (oldcmd);