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