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