Blame SOURCES/0380-New-command-nativedisk.patch

f96e0b
From 97f30af3b3b4bbfe51efa1f8bb0cd344280a028e Mon Sep 17 00:00:00 2001
f96e0b
From: Vladimir 'phcoder' Serbinenko <phcoder@gmail.com>
f96e0b
Date: Sun, 28 Apr 2013 15:31:33 +0200
f96e0b
Subject: [PATCH 380/482] 	New command `nativedisk'.
f96e0b
f96e0b
---
f96e0b
 ChangeLog                       |   4 +
f96e0b
 docs/grub.texi                  |   8 ++
f96e0b
 grub-core/Makefile.core.def     |   5 +
f96e0b
 grub-core/commands/nativedisk.c | 250 ++++++++++++++++++++++++++++++++++++++++
f96e0b
 grub-core/kern/dl.c             |  46 +++-----
f96e0b
 grub-core/normal/main.c         |   3 +-
f96e0b
 include/grub/dl.h               |  29 ++++-
f96e0b
 7 files changed, 316 insertions(+), 29 deletions(-)
f96e0b
 create mode 100644 grub-core/commands/nativedisk.c
f96e0b
f96e0b
diff --git a/ChangeLog b/ChangeLog
f96e0b
index 835895b..69c7839 100644
f96e0b
--- a/ChangeLog
f96e0b
+++ b/ChangeLog
f96e0b
@@ -1,5 +1,9 @@
f96e0b
 2013-04-28  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
 
f96e0b
+	New command `nativedisk'.
f96e0b
+
f96e0b
+2013-04-28  Vladimir Serbinenko  <phcoder@gmail.com>
f96e0b
+
f96e0b
 	* grub-core/io/lzopio.c: Use GRUB_PROPERLY_ALIGNED_ARRAY.
f96e0b
 	* grub-core/loader/i386/bsd.c: Likewise.
f96e0b
 
f96e0b
diff --git a/docs/grub.texi b/docs/grub.texi
f96e0b
index 754e191..587c64e 100644
f96e0b
--- a/docs/grub.texi
f96e0b
+++ b/docs/grub.texi
f96e0b
@@ -3411,6 +3411,7 @@ you forget a command, you can run the command @command{help}
f96e0b
 * lsfonts::                     List loaded fonts
f96e0b
 * lsmod::                       Show loaded modules
f96e0b
 * md5sum::                      Compute or check MD5 hash
f96e0b
+* nativedisk::                  Switch to native disk drivers
f96e0b
 * normal::                      Enter normal mode
f96e0b
 * normal_exit::                 Exit from normal mode
f96e0b
 * parttool::                    Modify partition table entries
f96e0b
@@ -4035,6 +4036,13 @@ Alias for @code{hashsum --hash md5 arg @dots{}}. See command @command{hashsum}
f96e0b
 @end deffn
f96e0b
 
f96e0b
 
f96e0b
+@node nativedisk
f96e0b
+@subsection nativedisk
f96e0b
+
f96e0b
+@deffn Command nativedisk
f96e0b
+Switch from firmware disk drivers to native ones.
f96e0b
+@end deffn
f96e0b
+
f96e0b
 @node normal
f96e0b
 @subsection normal
f96e0b
 
f96e0b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
f96e0b
index 7f93723..2e73d89 100644
f96e0b
--- a/grub-core/Makefile.core.def
f96e0b
+++ b/grub-core/Makefile.core.def
f96e0b
@@ -508,6 +508,11 @@ module = {
f96e0b
 };
f96e0b
 
f96e0b
 module = {
f96e0b
+  name = nativedisk;
f96e0b
+  common = commands/nativedisk.c;
f96e0b
+};
f96e0b
+
f96e0b
+module = {
f96e0b
   name = emupci;
f96e0b
   common = bus/emu/pci.c;
f96e0b
   common = commands/lspci.c;
f96e0b
diff --git a/grub-core/commands/nativedisk.c b/grub-core/commands/nativedisk.c
f96e0b
new file mode 100644
f96e0b
index 0000000..adb4043
f96e0b
--- /dev/null
f96e0b
+++ b/grub-core/commands/nativedisk.c
f96e0b
@@ -0,0 +1,250 @@
f96e0b
+/*
f96e0b
+ *  GRUB  --  GRand Unified Bootloader
f96e0b
+ *  Copyright (C) 2013  Free Software Foundation, Inc.
f96e0b
+ *
f96e0b
+ *  GRUB is free software: you can redistribute it and/or modify
f96e0b
+ *  it under the terms of the GNU General Public License as published by
f96e0b
+ *  the Free Software Foundation, either version 3 of the License, or
f96e0b
+ *  (at your option) any later version.
f96e0b
+ *
f96e0b
+ *  GRUB is distributed in the hope that it will be useful,
f96e0b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
f96e0b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f96e0b
+ *  GNU General Public License for more details.
f96e0b
+ *
f96e0b
+ *  You should have received a copy of the GNU General Public License
f96e0b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
f96e0b
+ */
f96e0b
+
f96e0b
+#include <grub/types.h>
f96e0b
+#include <grub/misc.h>
f96e0b
+#include <grub/mm.h>
f96e0b
+#include <grub/err.h>
f96e0b
+#include <grub/dl.h>
f96e0b
+#include <grub/command.h>
f96e0b
+#include <grub/i18n.h>
f96e0b
+#include <grub/device.h>
f96e0b
+#include <grub/mm.h>
f96e0b
+#include <grub/fs.h>
f96e0b
+#include <grub/env.h>
f96e0b
+#include <grub/file.h>
f96e0b
+
f96e0b
+GRUB_MOD_LICENSE ("GPLv3+");
f96e0b
+
f96e0b
+static const char *modnames_def[] = { "pata", "ahci", "usbms", "ohci", "uhci", "ehci" };
f96e0b
+
f96e0b
+static grub_err_t
f96e0b
+get_uuid (const char *name, char **uuid)
f96e0b
+{
f96e0b
+  grub_device_t dev;
f96e0b
+  grub_fs_t fs = 0;
f96e0b
+
f96e0b
+  dev = grub_device_open (name);
f96e0b
+  if (!dev)
f96e0b
+    return grub_errno;
f96e0b
+  if (dev)
f96e0b
+    fs = grub_fs_probe (dev);
f96e0b
+  if (!fs)
f96e0b
+    {
f96e0b
+      grub_device_close (dev);
f96e0b
+      return grub_errno;
f96e0b
+    }
f96e0b
+  if (!fs->uuid || fs->uuid (dev, uuid))
f96e0b
+    {
f96e0b
+      grub_device_close (dev);
f96e0b
+
f96e0b
+      if (!grub_errno)
f96e0b
+	grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
f96e0b
+		    N_("%s does not support UUIDs"), fs->name);
f96e0b
+
f96e0b
+      return grub_errno;
f96e0b
+    }
f96e0b
+  grub_device_close (dev);
f96e0b
+  return GRUB_ERR_NONE;
f96e0b
+}
f96e0b
+
f96e0b
+struct search_ctx
f96e0b
+{
f96e0b
+  char *root_uuid;
f96e0b
+  char *prefix_uuid;
f96e0b
+  const char *prefix_path;
f96e0b
+  int prefix_found, root_found;
f96e0b
+};
f96e0b
+
f96e0b
+static int
f96e0b
+iterate_device (const char *name, void *data)
f96e0b
+{
f96e0b
+  struct search_ctx *ctx = data;
f96e0b
+  char *cur_uuid;
f96e0b
+
f96e0b
+  if (get_uuid (name, &cur_uuid))
f96e0b
+    {
f96e0b
+      grub_print_error ();
f96e0b
+      return 0;
f96e0b
+    }
f96e0b
+  if (grub_strcasecmp (cur_uuid, ctx->prefix_uuid) == 0)
f96e0b
+    {
f96e0b
+      char *prefix;
f96e0b
+      prefix = grub_xasprintf ("(%s)/%s", name, ctx->prefix_path);
f96e0b
+      grub_env_set ("prefix", prefix);
f96e0b
+      grub_free (prefix);
f96e0b
+      ctx->prefix_found = 1;
f96e0b
+    }
f96e0b
+  if (grub_strcasecmp (cur_uuid, ctx->root_uuid) == 0)
f96e0b
+    {
f96e0b
+      grub_env_set ("root", name);
f96e0b
+      ctx->root_found = 1;
f96e0b
+    }
f96e0b
+  return ctx->prefix_found && ctx->root_found;
f96e0b
+}
f96e0b
+
f96e0b
+static grub_err_t
f96e0b
+grub_cmd_nativedisk (grub_command_t cmd __attribute__ ((unused)),
f96e0b
+		     int argc, char **args_in)
f96e0b
+{
f96e0b
+  char *uuid_root = 0, *uuid_prefix, *prefdev = 0;
f96e0b
+  const char *prefix = 0;
f96e0b
+  const char *path_prefix = 0;
f96e0b
+  int mods_loaded = 0;
f96e0b
+  grub_dl_t *mods;
f96e0b
+  struct search_ctx ctx;
f96e0b
+  const char **args;
f96e0b
+  grub_fs_autoload_hook_t saved_autoload;
f96e0b
+  int i;
f96e0b
+
f96e0b
+  if (argc == 0)
f96e0b
+    {
f96e0b
+      argc = ARRAY_SIZE (modnames_def);
f96e0b
+      args = modnames_def;
f96e0b
+    }
f96e0b
+  else
f96e0b
+    args = (const char **) args_in;
f96e0b
+
f96e0b
+  prefix = grub_env_get ("prefix");
f96e0b
+
f96e0b
+  if (! prefix)
f96e0b
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "prefix");
f96e0b
+
f96e0b
+  if (prefix)
f96e0b
+    path_prefix = (prefix[0] == '(') ? grub_strchr (prefix, ')') : NULL;
f96e0b
+  if (path_prefix)
f96e0b
+    path_prefix++;
f96e0b
+  else
f96e0b
+    path_prefix = prefix;
f96e0b
+
f96e0b
+  mods = grub_malloc (argc * sizeof (mods[0]));
f96e0b
+  if (!mods)
f96e0b
+    return grub_errno;
f96e0b
+
f96e0b
+  if (get_uuid (NULL, &uuid_root))
f96e0b
+    return grub_errno;
f96e0b
+
f96e0b
+  prefdev = grub_file_get_device_name (prefix);
f96e0b
+  if (grub_errno)
f96e0b
+    {
f96e0b
+      grub_print_error ();
f96e0b
+      prefdev = 0;
f96e0b
+    }
f96e0b
+
f96e0b
+  if (get_uuid (prefdev, &uuid_prefix))
f96e0b
+    {
f96e0b
+      grub_free (uuid_root);
f96e0b
+      return grub_errno;
f96e0b
+    }
f96e0b
+
f96e0b
+  for (mods_loaded = 0; mods_loaded < argc; mods_loaded++)
f96e0b
+    {
f96e0b
+      char *filename;
f96e0b
+      grub_dl_t mod;
f96e0b
+      grub_file_t file = NULL;
f96e0b
+      grub_ssize_t size;
f96e0b
+      void *core = 0;
f96e0b
+
f96e0b
+      mod = grub_dl_get (args[mods_loaded]);
f96e0b
+      if (mod)
f96e0b
+	{
f96e0b
+	  mods[mods_loaded] = 0;
f96e0b
+	  continue;
f96e0b
+	}
f96e0b
+
f96e0b
+      filename = grub_xasprintf ("%s/" GRUB_TARGET_CPU "-" GRUB_PLATFORM "/%s.mod",
f96e0b
+				 prefix, args[mods_loaded]);
f96e0b
+      if (! filename)
f96e0b
+	goto fail;
f96e0b
+
f96e0b
+      file = grub_file_open (filename);
f96e0b
+      grub_free (filename);
f96e0b
+      if (! file)
f96e0b
+	goto fail;
f96e0b
+
f96e0b
+      size = grub_file_size (file);
f96e0b
+      core = grub_malloc (size);
f96e0b
+      if (! core)
f96e0b
+	{
f96e0b
+	  grub_file_close (file);
f96e0b
+	  goto fail;
f96e0b
+	}
f96e0b
+
f96e0b
+      if (grub_file_read (file, core, size) != (grub_ssize_t) size)
f96e0b
+	{
f96e0b
+	  grub_file_close (file);
f96e0b
+	  grub_free (core);
f96e0b
+	  goto fail;
f96e0b
+	}
f96e0b
+
f96e0b
+      grub_file_close (file);
f96e0b
+
f96e0b
+      mods[mods_loaded] = grub_dl_load_core_noinit (core, size);
f96e0b
+      if (! mods[mods_loaded])
f96e0b
+	goto fail;
f96e0b
+    }
f96e0b
+
f96e0b
+  for (i = 0; i < argc; i++)
f96e0b
+    if (mods[i])
f96e0b
+      grub_dl_init (mods[i]);
f96e0b
+
f96e0b
+  /* No need to autoload FS since obviously we already have the necessary fs modules.  */
f96e0b
+  saved_autoload = grub_fs_autoload_hook;
f96e0b
+  grub_fs_autoload_hook = 0;
f96e0b
+
f96e0b
+  ctx.root_uuid = uuid_root;
f96e0b
+  ctx.prefix_uuid = uuid_prefix;
f96e0b
+  ctx.prefix_path = path_prefix;
f96e0b
+  ctx.prefix_found = 0;
f96e0b
+  ctx.root_found = 0;
f96e0b
+
f96e0b
+  /* FIXME: try to guess the correct values.  */
f96e0b
+  grub_device_iterate (iterate_device, &ctx;;
f96e0b
+
f96e0b
+  grub_fs_autoload_hook = saved_autoload;
f96e0b
+
f96e0b
+  grub_free (uuid_root);
f96e0b
+  grub_free (uuid_prefix);
f96e0b
+
f96e0b
+  return GRUB_ERR_NONE;
f96e0b
+
f96e0b
+ fail:
f96e0b
+  grub_free (uuid_root);
f96e0b
+  grub_free (uuid_prefix);
f96e0b
+
f96e0b
+  for (i = 0; i < mods_loaded; i++)
f96e0b
+    if (mods[i])
f96e0b
+      {
f96e0b
+	mods[i]->fini = 0;
f96e0b
+	grub_dl_unload (mods[i]);
f96e0b
+      }
f96e0b
+  return grub_errno;
f96e0b
+}
f96e0b
+
f96e0b
+static grub_command_t cmd;
f96e0b
+
f96e0b
+GRUB_MOD_INIT(nativedisk)
f96e0b
+{
f96e0b
+  cmd = grub_register_command ("nativedisk", grub_cmd_nativedisk, "[MODULE1 MODULE2 ...]",
f96e0b
+			       N_("Switch to native disk drivers. If no modules are specified default set (pata,ahci,usbms,ohci,uhci,ehci) is used"));
f96e0b
+}
f96e0b
+
f96e0b
+GRUB_MOD_FINI(nativedisk)
f96e0b
+{
f96e0b
+  grub_unregister_command (cmd);
f96e0b
+}
f96e0b
diff --git a/grub-core/kern/dl.c b/grub-core/kern/dl.c
f96e0b
index 641146d..6c086ad 100644
f96e0b
--- a/grub-core/kern/dl.c
f96e0b
+++ b/grub-core/kern/dl.c
f96e0b
@@ -51,6 +51,7 @@ grub_dl_t grub_dl_head = 0;
f96e0b
 grub_err_t
f96e0b
 grub_dl_add (grub_dl_t mod);
f96e0b
 
f96e0b
+/* Keep global so that GDB scripts work.  */
f96e0b
 grub_err_t
f96e0b
 grub_dl_add (grub_dl_t mod)
f96e0b
 {
f96e0b
@@ -58,9 +59,6 @@ grub_dl_add (grub_dl_t mod)
f96e0b
     return grub_error (GRUB_ERR_BAD_MODULE,
f96e0b
 		       "`%s' is already loaded", mod->name);
f96e0b
 
f96e0b
-  mod->next = grub_dl_head;
f96e0b
-  grub_dl_head = mod;
f96e0b
-
f96e0b
   return GRUB_ERR_NONE;
f96e0b
 }
f96e0b
 
f96e0b
@@ -77,18 +75,6 @@ grub_dl_remove (grub_dl_t mod)
f96e0b
       }
f96e0b
 }
f96e0b
 
f96e0b
-grub_dl_t
f96e0b
-grub_dl_get (const char *name)
f96e0b
-{
f96e0b
-  grub_dl_t l;
f96e0b
-
f96e0b
-  for (l = grub_dl_head; l; l = l->next)
f96e0b
-    if (grub_strcmp (name, l->name) == 0)
f96e0b
-      return l;
f96e0b
-
f96e0b
-  return 0;
f96e0b
-}
f96e0b
-
f96e0b
 
f96e0b
 
f96e0b
 struct grub_symbol
f96e0b
@@ -447,13 +433,6 @@ grub_dl_resolve_symbols (grub_dl_t mod, Elf_Ehdr *e)
f96e0b
   return GRUB_ERR_NONE;
f96e0b
 }
f96e0b
 
f96e0b
-static void
f96e0b
-grub_dl_call_init (grub_dl_t mod)
f96e0b
-{
f96e0b
-  if (mod->init)
f96e0b
-    (mod->init) (mod);
f96e0b
-}
f96e0b
-
f96e0b
 /* Me, Vladimir Serbinenko, hereby I add this module check as per new
f96e0b
    GNU module policy. Note that this license check is informative only.
f96e0b
    Modules have to be licensed under GPLv3 or GPLv3+ (optionally
f96e0b
@@ -595,7 +574,7 @@ grub_dl_flush_cache (grub_dl_t mod)
f96e0b
 
f96e0b
 /* Load a module from core memory.  */
f96e0b
 grub_dl_t
f96e0b
-grub_dl_load_core (void *addr, grub_size_t size)
f96e0b
+grub_dl_load_core_noinit (void *addr, grub_size_t size)
f96e0b
 {
f96e0b
   Elf_Ehdr *e;
f96e0b
   grub_dl_t mod;
f96e0b
@@ -651,10 +630,6 @@ grub_dl_load_core (void *addr, grub_size_t size)
f96e0b
   grub_dprintf ("modules", "module name: %s\n", mod->name);
f96e0b
   grub_dprintf ("modules", "init function: %p\n", mod->init);
f96e0b
 
f96e0b
-  grub_boot_time ("Initing module %s", mod->name);
f96e0b
-  grub_dl_call_init (mod);
f96e0b
-  grub_boot_time ("Module %s inited", mod->name);
f96e0b
-
f96e0b
   if (grub_dl_add (mod))
f96e0b
     {
f96e0b
       grub_dl_unload (mod);
f96e0b
@@ -664,6 +639,23 @@ grub_dl_load_core (void *addr, grub_size_t size)
f96e0b
   return mod;
f96e0b
 }
f96e0b
 
f96e0b
+grub_dl_t
f96e0b
+grub_dl_load_core (void *addr, grub_size_t size)
f96e0b
+{
f96e0b
+  grub_dl_t mod;
f96e0b
+
f96e0b
+  mod = grub_dl_load_core_noinit (addr, size);
f96e0b
+
f96e0b
+  if (!mod)
f96e0b
+    return NULL;
f96e0b
+
f96e0b
+  grub_boot_time ("Initing module %s", mod->name);
f96e0b
+  grub_dl_init (mod);
f96e0b
+  grub_boot_time ("Module %s inited", mod->name);
f96e0b
+
f96e0b
+  return mod;
f96e0b
+}
f96e0b
+
f96e0b
 /* Load a module from the file FILENAME.  */
f96e0b
 grub_dl_t
f96e0b
 grub_dl_load_file (const char *filename)
f96e0b
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
f96e0b
index 2f203dd..759e0a4 100644
f96e0b
--- a/grub-core/normal/main.c
f96e0b
+++ b/grub-core/normal/main.c
f96e0b
@@ -506,7 +506,8 @@ static void (*grub_xputs_saved) (const char *str);
f96e0b
 static const char *features[] = {
f96e0b
   "feature_chainloader_bpb", "feature_ntldr", "feature_platform_search_hint",
f96e0b
   "feature_default_font_path", "feature_all_video_module",
f96e0b
-  "feature_menuentry_id", "feature_menuentry_options", "feature_200_final"
f96e0b
+  "feature_menuentry_id", "feature_menuentry_options", "feature_200_final",
f96e0b
+  "feature_nativedisk_cmd"
f96e0b
 };
f96e0b
 
f96e0b
 GRUB_MOD_INIT(normal)
f96e0b
diff --git a/include/grub/dl.h b/include/grub/dl.h
f96e0b
index 3119978..f34b5a1 100644
f96e0b
--- a/include/grub/dl.h
f96e0b
+++ b/include/grub/dl.h
f96e0b
@@ -25,6 +25,7 @@
f96e0b
 #include <grub/err.h>
f96e0b
 #include <grub/types.h>
f96e0b
 #include <grub/elf.h>
f96e0b
+#include <grub/list.h>
f96e0b
 #endif
f96e0b
 
f96e0b
 /*
f96e0b
@@ -181,15 +182,41 @@ typedef struct grub_dl *grub_dl_t;
f96e0b
 grub_dl_t grub_dl_load_file (const char *filename);
f96e0b
 grub_dl_t EXPORT_FUNC(grub_dl_load) (const char *name);
f96e0b
 grub_dl_t grub_dl_load_core (void *addr, grub_size_t size);
f96e0b
+grub_dl_t EXPORT_FUNC(grub_dl_load_core_noinit) (void *addr, grub_size_t size);
f96e0b
 int EXPORT_FUNC(grub_dl_unload) (grub_dl_t mod);
f96e0b
 void grub_dl_unload_unneeded (void);
f96e0b
 int EXPORT_FUNC(grub_dl_ref) (grub_dl_t mod);
f96e0b
 int EXPORT_FUNC(grub_dl_unref) (grub_dl_t mod);
f96e0b
 extern grub_dl_t EXPORT_VAR(grub_dl_head);
f96e0b
 
f96e0b
+#ifndef GRUB_UTIL
f96e0b
+
f96e0b
 #define FOR_DL_MODULES(var) FOR_LIST_ELEMENTS ((var), (grub_dl_head))
f96e0b
 
f96e0b
-grub_dl_t EXPORT_FUNC(grub_dl_get) (const char *name);
f96e0b
+static inline void
f96e0b
+grub_dl_init (grub_dl_t mod)
f96e0b
+{
f96e0b
+  if (mod->init)
f96e0b
+    (mod->init) (mod);
f96e0b
+
f96e0b
+  mod->next = grub_dl_head;
f96e0b
+  grub_dl_head = mod;
f96e0b
+}
f96e0b
+
f96e0b
+static inline grub_dl_t
f96e0b
+grub_dl_get (const char *name)
f96e0b
+{
f96e0b
+  grub_dl_t l;
f96e0b
+
f96e0b
+  FOR_DL_MODULES(l)
f96e0b
+    if (grub_strcmp (name, l->name) == 0)
f96e0b
+      return l;
f96e0b
+
f96e0b
+  return 0;
f96e0b
+}
f96e0b
+
f96e0b
+#endif
f96e0b
+
f96e0b
 grub_err_t grub_dl_register_symbol (const char *name, void *addr,
f96e0b
 				    int isfunc, grub_dl_t mod);
f96e0b
 
f96e0b
-- 
f96e0b
1.8.2.1
f96e0b