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