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

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