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

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