dcavalca / rpms / grub2

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