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