Blame SOURCES/0076-Add-linux-and-initrd-commands-for-grub-emu.patch

8e15ce
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8e15ce
From: Michael Chang <mchang@suse.com>
8e15ce
Date: Tue, 6 Feb 2018 09:09:00 +0100
8e15ce
Subject: [PATCH] Add linux and initrd commands for grub-emu
8e15ce
8e15ce
When using grub-emu, the linux and initrd commands are used as arguments
8e15ce
to the kexec command line tool, to allow booting the selected menu entry.
8e15ce
---
8e15ce
 grub-core/Makefile.core.def  |   1 -
8e15ce
 grub-core/kern/emu/main.c    |   4 +
8e15ce
 grub-core/kern/emu/misc.c    |  18 ++++-
8e15ce
 grub-core/loader/emu/linux.c | 172 +++++++++++++++++++++++++++++++++++++++++++
8e15ce
 include/grub/emu/exec.h      |   4 +-
8e15ce
 include/grub/emu/hostfile.h  |   3 +-
8e15ce
 include/grub/emu/misc.h      |   3 +
8e15ce
 grub-core/Makefile.am        |   1 +
8e15ce
 8 files changed, 202 insertions(+), 4 deletions(-)
8e15ce
 create mode 100644 grub-core/loader/emu/linux.c
8e15ce
8e15ce
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
8e15ce
index 058c88ac3af..5354f9613d3 100644
8e15ce
--- a/grub-core/Makefile.core.def
8e15ce
+++ b/grub-core/Makefile.core.def
8e15ce
@@ -1826,7 +1826,6 @@ module = {
8e15ce
 
8e15ce
   common = loader/linux.c;
8e15ce
   common = lib/cmdline.c;
8e15ce
-  enable = noemu;
8e15ce
 
8e15ce
   efi = loader/efi/linux.c;
8e15ce
 };
8e15ce
diff --git a/grub-core/kern/emu/main.c b/grub-core/kern/emu/main.c
8e15ce
index 55ea5a11ccd..846fe9715ec 100644
8e15ce
--- a/grub-core/kern/emu/main.c
8e15ce
+++ b/grub-core/kern/emu/main.c
8e15ce
@@ -107,6 +107,7 @@ static struct argp_option options[] = {
8e15ce
    N_("use GRUB files in the directory DIR [default=%s]"), 0},
8e15ce
   {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
8e15ce
   {"hold",     'H', N_("SECS"),      OPTION_ARG_OPTIONAL, N_("wait until a debugger will attach"), 0},
8e15ce
+  {"kexec",       'X', 0,      0, N_("try the untryable."), 0},
8e15ce
   { 0, 0, 0, 0, 0, 0 }
8e15ce
 };
8e15ce
 
8e15ce
@@ -164,6 +165,9 @@ argp_parser (int key, char *arg, struct argp_state *state)
8e15ce
     case 'v':
8e15ce
       verbosity++;
8e15ce
       break;
8e15ce
+    case 'X':
8e15ce
+      grub_util_set_kexecute();
8e15ce
+      break;
8e15ce
 
8e15ce
     case ARGP_KEY_ARG:
8e15ce
       {
8e15ce
diff --git a/grub-core/kern/emu/misc.c b/grub-core/kern/emu/misc.c
8e15ce
index 0ff13bcaf8c..eeea092752d 100644
8e15ce
--- a/grub-core/kern/emu/misc.c
8e15ce
+++ b/grub-core/kern/emu/misc.c
8e15ce
@@ -39,6 +39,7 @@
8e15ce
 #include <grub/emu/misc.h>
8e15ce
 
8e15ce
 int verbosity;
8e15ce
+int kexecute;
8e15ce
 
8e15ce
 void
8e15ce
 grub_util_warn (const char *fmt, ...)
8e15ce
@@ -82,7 +83,7 @@ grub_util_error (const char *fmt, ...)
8e15ce
   vfprintf (stderr, fmt, ap);
8e15ce
   va_end (ap);
8e15ce
   fprintf (stderr, ".\n");
8e15ce
-  exit (1);
8e15ce
+  grub_exit (1);
8e15ce
 }
8e15ce
 
8e15ce
 void *
8e15ce
@@ -154,6 +155,9 @@ void
8e15ce
 __attribute__ ((noreturn))
8e15ce
 grub_exit (int rc)
8e15ce
 {
8e15ce
+#if defined (GRUB_KERNEL)
8e15ce
+  grub_reboot();
8e15ce
+#endif
8e15ce
   exit (rc < 0 ? 1 : rc);
8e15ce
 }
8e15ce
 #endif
8e15ce
@@ -215,3 +219,15 @@ grub_util_load_image (const char *path, char *buf)
8e15ce
 
8e15ce
   fclose (fp);
8e15ce
 }
8e15ce
+
8e15ce
+void
8e15ce
+grub_util_set_kexecute(void)
8e15ce
+{
8e15ce
+  kexecute++;
8e15ce
+}
8e15ce
+
8e15ce
+int
8e15ce
+grub_util_get_kexecute(void)
8e15ce
+{
8e15ce
+  return kexecute;
8e15ce
+}
8e15ce
diff --git a/grub-core/loader/emu/linux.c b/grub-core/loader/emu/linux.c
8e15ce
new file mode 100644
8e15ce
index 00000000000..fda9e00d24c
8e15ce
--- /dev/null
8e15ce
+++ b/grub-core/loader/emu/linux.c
8e15ce
@@ -0,0 +1,172 @@
8e15ce
+/*
8e15ce
+ *  GRUB  --  GRand Unified Bootloader
8e15ce
+ *  Copyright (C) 2006,2007,2008,2009,2010  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
+
8e15ce
+#include <grub/loader.h>
8e15ce
+#include <grub/dl.h>
8e15ce
+#include <grub/command.h>
8e15ce
+#include <grub/time.h>
8e15ce
+
8e15ce
+#include <grub/emu/exec.h>
8e15ce
+#include <grub/emu/hostfile.h>
8e15ce
+#include <grub/emu/misc.h>
8e15ce
+
8e15ce
+GRUB_MOD_LICENSE ("GPLv3+");
8e15ce
+
8e15ce
+static grub_dl_t my_mod;
8e15ce
+
8e15ce
+static char *kernel_path;
8e15ce
+static char *initrd_path;
8e15ce
+static char *boot_cmdline;
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_linux_boot (void)
8e15ce
+{
8e15ce
+  grub_err_t rc = GRUB_ERR_NONE;
8e15ce
+  char *initrd_param;
8e15ce
+  const char *kexec[] = { "kexec", "-l", kernel_path, boot_cmdline, NULL, NULL };
8e15ce
+  const char *systemctl[] = { "systemctl", "kexec", NULL };
8e15ce
+  int kexecute = grub_util_get_kexecute();
8e15ce
+
8e15ce
+  if (initrd_path) {
8e15ce
+    initrd_param = grub_xasprintf("--initrd=%s", initrd_path);
8e15ce
+    kexec[3] = initrd_param;
8e15ce
+    kexec[4] = boot_cmdline;
8e15ce
+  } else {
8e15ce
+    initrd_param = grub_xasprintf("%s", "");
8e15ce
+  }
8e15ce
+
8e15ce
+  grub_printf("%serforming 'kexec -l %s %s %s'\n",
8e15ce
+	(kexecute) ? "P" : "Not p",
8e15ce
+	kernel_path, initrd_param, boot_cmdline);
8e15ce
+
8e15ce
+  if (kexecute)
8e15ce
+    rc = grub_util_exec(kexec);
8e15ce
+
8e15ce
+  grub_free(initrd_param);
8e15ce
+
8e15ce
+  if (rc != GRUB_ERR_NONE) {
8e15ce
+    grub_error (rc, N_("Error trying to perform kexec load operation."));
8e15ce
+    grub_sleep (3);
8e15ce
+    return rc;
8e15ce
+  }
8e15ce
+  if (kexecute < 1)
8e15ce
+    grub_fatal (N_("Use '"PACKAGE"-emu --kexec' to force a system restart."));
8e15ce
+
8e15ce
+  grub_printf("Performing 'systemctl kexec' (%s) ",
8e15ce
+		(kexecute==1) ? "do-or-die" : "just-in-case");
8e15ce
+  rc = grub_util_exec (systemctl);
8e15ce
+
8e15ce
+  if (kexecute == 1)
8e15ce
+    grub_fatal (N_("Error trying to perform 'systemctl kexec'"));
8e15ce
+
8e15ce
+  /* need to check read-only root before resetting hard!? */
8e15ce
+  grub_printf("Performing 'kexec -e'");
8e15ce
+  kexec[1] = "-e";
8e15ce
+  kexec[2] = NULL;
8e15ce
+  rc = grub_util_exec(kexec);
8e15ce
+  if ( rc != GRUB_ERR_NONE )
8e15ce
+    grub_fatal (N_("Error trying to directly perform 'kexec -e'."));
8e15ce
+
8e15ce
+  return rc;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_linux_unload (void)
8e15ce
+{
8e15ce
+  grub_dl_unref (my_mod);
8e15ce
+  if ( boot_cmdline != NULL )
8e15ce
+    grub_free (boot_cmdline);
8e15ce
+  boot_cmdline = NULL;
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_cmd_linux (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[])
8e15ce
+{
8e15ce
+  int i;
8e15ce
+  char *tempstr;
8e15ce
+
8e15ce
+  grub_dl_ref (my_mod);
8e15ce
+
8e15ce
+  if (argc == 0)
8e15ce
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
8e15ce
+
8e15ce
+  if ( !grub_util_is_regular(argv[0]) )
8e15ce
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("Cannot find kernel file %s"), argv[0]);
8e15ce
+
8e15ce
+  if ( kernel_path != NULL )
8e15ce
+    grub_free(kernel_path);
8e15ce
+
8e15ce
+  kernel_path = grub_xasprintf("%s", argv[0]);
8e15ce
+
8e15ce
+  if ( boot_cmdline != NULL ) {
8e15ce
+    grub_free(boot_cmdline);
8e15ce
+    boot_cmdline = NULL;
8e15ce
+  }
8e15ce
+
8e15ce
+  if ( argc > 1 )
8e15ce
+  {
8e15ce
+    boot_cmdline = grub_xasprintf("--command-line=%s", argv[1]);
8e15ce
+    for ( i = 2; i < argc; i++ ) {
8e15ce
+      tempstr = grub_xasprintf("%s %s", boot_cmdline, argv[i]);
8e15ce
+      grub_free(boot_cmdline);
8e15ce
+      boot_cmdline = tempstr;
8e15ce
+    }
8e15ce
+  }
8e15ce
+
8e15ce
+  grub_loader_set (grub_linux_boot, grub_linux_unload, 0);
8e15ce
+
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_err_t
8e15ce
+grub_cmd_initrd (grub_command_t cmd __attribute__ ((unused)), int argc, char *argv[])
8e15ce
+{
8e15ce
+  if (argc == 0)
8e15ce
+    return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("filename expected"));
8e15ce
+
8e15ce
+  if ( !grub_util_is_regular(argv[0]) )
8e15ce
+    return grub_error(GRUB_ERR_FILE_NOT_FOUND, N_("Cannot find initrd file %s"), argv[0]);
8e15ce
+
8e15ce
+  if ( initrd_path != NULL )
8e15ce
+    grub_free(initrd_path);
8e15ce
+
8e15ce
+  initrd_path = grub_xasprintf("%s", argv[0]);
8e15ce
+
8e15ce
+  grub_dl_unref (my_mod);
8e15ce
+
8e15ce
+  return GRUB_ERR_NONE;
8e15ce
+}
8e15ce
+
8e15ce
+static grub_command_t cmd_linux, cmd_initrd;
8e15ce
+
8e15ce
+GRUB_MOD_INIT(linux)
8e15ce
+{
8e15ce
+  cmd_linux = grub_register_command ("linux", grub_cmd_linux, 0, N_("Load Linux."));
8e15ce
+  cmd_initrd = grub_register_command ("initrd", grub_cmd_initrd, 0, N_("Load initrd."));
8e15ce
+  my_mod = mod;
8e15ce
+  kernel_path = NULL;
8e15ce
+  initrd_path = NULL;
8e15ce
+  boot_cmdline = NULL;
8e15ce
+}
8e15ce
+
8e15ce
+GRUB_MOD_FINI(linux)
8e15ce
+{
8e15ce
+  grub_unregister_command (cmd_linux);
8e15ce
+  grub_unregister_command (cmd_initrd);
8e15ce
+}
8e15ce
diff --git a/include/grub/emu/exec.h b/include/grub/emu/exec.h
8e15ce
index d1073ef86af..1b61b4a2e5d 100644
8e15ce
--- a/include/grub/emu/exec.h
8e15ce
+++ b/include/grub/emu/exec.h
8e15ce
@@ -23,6 +23,8 @@
8e15ce
 #include <stdarg.h>
8e15ce
 
8e15ce
 #include <sys/types.h>
8e15ce
+#include <grub/symbol.h>
8e15ce
+
8e15ce
 pid_t
8e15ce
 grub_util_exec_pipe (const char *const *argv, int *fd);
8e15ce
 pid_t
8e15ce
@@ -32,7 +34,7 @@ int
8e15ce
 grub_util_exec_redirect_all (const char *const *argv, const char *stdin_file,
8e15ce
 			     const char *stdout_file, const char *stderr_file);
8e15ce
 int
8e15ce
-grub_util_exec (const char *const *argv);
8e15ce
+EXPORT_FUNC(grub_util_exec) (const char *const *argv);
8e15ce
 int
8e15ce
 grub_util_exec_redirect (const char *const *argv, const char *stdin_file,
8e15ce
 			 const char *stdout_file);
8e15ce
diff --git a/include/grub/emu/hostfile.h b/include/grub/emu/hostfile.h
8e15ce
index cfb1e2b5661..a61568e36e9 100644
8e15ce
--- a/include/grub/emu/hostfile.h
8e15ce
+++ b/include/grub/emu/hostfile.h
8e15ce
@@ -22,6 +22,7 @@
8e15ce
 #include <grub/disk.h>
8e15ce
 #include <grub/partition.h>
8e15ce
 #include <sys/types.h>
8e15ce
+#include <grub/symbol.h>
8e15ce
 #include <grub/osdep/hostfile.h>
8e15ce
 
8e15ce
 int
8e15ce
@@ -29,7 +30,7 @@ grub_util_is_directory (const char *path);
8e15ce
 int
8e15ce
 grub_util_is_special_file (const char *path);
8e15ce
 int
8e15ce
-grub_util_is_regular (const char *path);
8e15ce
+EXPORT_FUNC(grub_util_is_regular) (const char *path);
8e15ce
 
8e15ce
 char *
8e15ce
 grub_util_path_concat (size_t n, ...);
8e15ce
diff --git a/include/grub/emu/misc.h b/include/grub/emu/misc.h
8e15ce
index ff9c48a6490..01056954b96 100644
8e15ce
--- a/include/grub/emu/misc.h
8e15ce
+++ b/include/grub/emu/misc.h
8e15ce
@@ -57,6 +57,9 @@ void EXPORT_FUNC(grub_util_warn) (const char *fmt, ...) __attribute__ ((format (
8e15ce
 void EXPORT_FUNC(grub_util_info) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2)));
8e15ce
 void EXPORT_FUNC(grub_util_error) (const char *fmt, ...) __attribute__ ((format (GNU_PRINTF, 1, 2), noreturn));
8e15ce
 
8e15ce
+void EXPORT_FUNC(grub_util_set_kexecute) (void);
8e15ce
+int EXPORT_FUNC(grub_util_get_kexecute) (void) WARN_UNUSED_RESULT;
8e15ce
+
8e15ce
 grub_uint64_t EXPORT_FUNC (grub_util_get_cpu_time_ms) (void);
8e15ce
 
8e15ce
 #ifdef HAVE_DEVICE_MAPPER
8e15ce
diff --git a/grub-core/Makefile.am b/grub-core/Makefile.am
8e15ce
index ee88e44e97a..80e7a83edf9 100644
8e15ce
--- a/grub-core/Makefile.am
8e15ce
+++ b/grub-core/Makefile.am
8e15ce
@@ -307,6 +307,7 @@ KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/net.h
8e15ce
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/hostdisk.h
8e15ce
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/hostfile.h
8e15ce
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/extcmd.h
8e15ce
+KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/emu/exec.h
8e15ce
 if COND_GRUB_EMU_SDL
8e15ce
 KERNEL_HEADER_FILES += $(top_srcdir)/include/grub/sdl.h
8e15ce
 endif