dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0101-blscfg-add-blscfg-module-to-parse-Boot-Loader-Specif.patch

4fe85b
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
4fe85b
From: Fedora Ninjas <grub2-owner@fedoraproject.org>
4fe85b
Date: Tue, 22 Jan 2013 06:31:38 +0100
4fe85b
Subject: [PATCH] blscfg: add blscfg module to parse Boot Loader Specification
4fe85b
 snippets
4fe85b
4fe85b
http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec
4fe85b
4fe85b
Works like this:
4fe85b
4fe85b
 insmod blscfg
4fe85b
 bls_import
4fe85b
4fe85b
Done! You should now have menu items for your snippets in place.
4fe85b
4fe85b
Signed-off-by: Peter Jones <grub2-owner@fedoraproject.org>
4fe85b
---
4fe85b
 grub-core/Makefile.core.def |   8 ++
4fe85b
 grub-core/commands/blscfg.c | 201 ++++++++++++++++++++++++++++++++++++++++++++
4fe85b
 2 files changed, 209 insertions(+)
4fe85b
 create mode 100644 grub-core/commands/blscfg.c
4fe85b
4fe85b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
4fe85b
index ec46506e39e..7bf1c8a5880 100644
4fe85b
--- a/grub-core/Makefile.core.def
4fe85b
+++ b/grub-core/Makefile.core.def
4fe85b
@@ -746,6 +746,14 @@ module = {
4fe85b
   common = commands/blocklist.c;
4fe85b
 };
4fe85b
 
4fe85b
+module = {
4fe85b
+  name = blscfg;
4fe85b
+  common = commands/blscfg.c;
4fe85b
+  enable = i386_efi;
4fe85b
+  enable = x86_64_efi;
4fe85b
+  enable = i386_pc;
4fe85b
+};
4fe85b
+
4fe85b
 module = {
4fe85b
   name = boot;
4fe85b
   common = commands/boot.c;
4fe85b
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
4fe85b
new file mode 100644
4fe85b
index 00000000000..4274aca5a9d
4fe85b
--- /dev/null
4fe85b
+++ b/grub-core/commands/blscfg.c
4fe85b
@@ -0,0 +1,201 @@
4fe85b
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/
4fe85b
+
4fe85b
+/* bls.c - implementation of the boot loader spec */
4fe85b
+
4fe85b
+/*
4fe85b
+ *  GRUB  --  GRand Unified Bootloader
4fe85b
+ *
4fe85b
+ *  GRUB is free software: you can redistribute it and/or modify
4fe85b
+ *  it under the terms of the GNU General Public License as published by
4fe85b
+ *  the Free Software Foundation, either version 3 of the License, or
4fe85b
+ *  (at your option) any later version.
4fe85b
+ *
4fe85b
+ *  GRUB is distributed in the hope that it will be useful,
4fe85b
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
4fe85b
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4fe85b
+ *  GNU General Public License for more details.
4fe85b
+ *
4fe85b
+ *  You should have received a copy of the GNU General Public License
4fe85b
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
4fe85b
+ */
4fe85b
+
4fe85b
+#include <grub/types.h>
4fe85b
+#include <grub/misc.h>
4fe85b
+#include <grub/mm.h>
4fe85b
+#include <grub/err.h>
4fe85b
+#include <grub/dl.h>
4fe85b
+#include <grub/extcmd.h>
4fe85b
+#include <grub/i18n.h>
4fe85b
+#include <grub/fs.h>
4fe85b
+#include <grub/env.h>
4fe85b
+#include <grub/file.h>
4fe85b
+#include <grub/normal.h>
4fe85b
+
4fe85b
+GRUB_MOD_LICENSE ("GPLv3+");
4fe85b
+
4fe85b
+#ifdef GRUB_MACHINE_EFI
4fe85b
+#define GRUB_LINUX_CMD "linuxefi"
4fe85b
+#define GRUB_INITRD_CMD "initrdefi"
4fe85b
+#define GRUB_BLS_CONFIG_PATH "/EFI/fedora/loader/entries/"
4fe85b
+#define GRUB_BOOT_DEVICE "($boot)"
4fe85b
+#else
4fe85b
+#define GRUB_LINUX_CMD "linux"
4fe85b
+#define GRUB_INITRD_CMD "initrd"
4fe85b
+#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
4fe85b
+#define GRUB_BOOT_DEVICE "($root)"
4fe85b
+#endif
4fe85b
+
4fe85b
+static int parse_entry (
4fe85b
+    const char *filename,
4fe85b
+    const struct grub_dirhook_info *info __attribute__ ((unused)),
4fe85b
+    void *data __attribute__ ((unused)))
4fe85b
+{
4fe85b
+  grub_size_t n;
4fe85b
+  char *p;
4fe85b
+  grub_file_t f = NULL;
4fe85b
+  grub_off_t sz;
4fe85b
+  char *title = NULL, *options = NULL, *clinux = NULL, *initrd = NULL, *src = NULL;
4fe85b
+  const char *args[2] = { NULL, NULL };
4fe85b
+
4fe85b
+  if (filename[0] == '.')
4fe85b
+    return 0;
4fe85b
+
4fe85b
+  n = grub_strlen (filename);
4fe85b
+  if (n <= 5)
4fe85b
+    return 0;
4fe85b
+
4fe85b
+  if (grub_strcmp (filename + n - 5, ".conf") != 0)
4fe85b
+    return 0;
4fe85b
+
4fe85b
+  p = grub_xasprintf (GRUB_BLS_CONFIG_PATH "%s", filename);
4fe85b
+
4fe85b
+  f = grub_file_open (p);
4fe85b
+  if (!f)
4fe85b
+    goto finish;
4fe85b
+
4fe85b
+  sz = grub_file_size (f);
4fe85b
+  if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024)
4fe85b
+    goto finish;
4fe85b
+
4fe85b
+  for (;;)
4fe85b
+    {
4fe85b
+      char *buf;
4fe85b
+
4fe85b
+      buf = grub_file_getline (f);
4fe85b
+      if (!buf)
4fe85b
+	break;
4fe85b
+
4fe85b
+      if (grub_strncmp (buf, "title ", 6) == 0)
4fe85b
+	{
4fe85b
+	  grub_free (title);
4fe85b
+	  title = grub_strdup (buf + 6);
4fe85b
+	  if (!title)
4fe85b
+	    goto finish;
4fe85b
+	}
4fe85b
+      else if (grub_strncmp (buf, "options ", 8) == 0)
4fe85b
+	{
4fe85b
+	  grub_free (options);
4fe85b
+	  options = grub_strdup (buf + 8);
4fe85b
+	  if (!options)
4fe85b
+	    goto finish;
4fe85b
+	}
4fe85b
+      else if (grub_strncmp (buf, "linux ", 6) == 0)
4fe85b
+	{
4fe85b
+	  grub_free (clinux);
4fe85b
+	  clinux = grub_strdup (buf + 6);
4fe85b
+	  if (!clinux)
4fe85b
+	    goto finish;
4fe85b
+	}
4fe85b
+      else if (grub_strncmp (buf, "initrd ", 7) == 0)
4fe85b
+	{
4fe85b
+	  grub_free (initrd);
4fe85b
+	  initrd = grub_strdup (buf + 7);
4fe85b
+	  if (!initrd)
4fe85b
+	    goto finish;
4fe85b
+	}
4fe85b
+
4fe85b
+      grub_free(buf);
4fe85b
+    }
4fe85b
+
4fe85b
+  if (!linux)
4fe85b
+    {
4fe85b
+      grub_printf ("Skipping file %s with no 'linux' key.", p);
4fe85b
+      goto finish;
4fe85b
+    }
4fe85b
+
4fe85b
+  args[0] = title ? title : filename;
4fe85b
+
4fe85b
+  src = grub_xasprintf ("load_video\n"
4fe85b
+			"set gfx_payload=keep\n"
4fe85b
+			"insmod gzio\n"
4fe85b
+			GRUB_LINUX_CMD " %s%s%s%s\n"
4fe85b
+			"%s%s%s%s",
4fe85b
+			GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options : "",
4fe85b
+			initrd ? GRUB_INITRD_CMD " " : "", initrd ? GRUB_BOOT_DEVICE : "", initrd ? initrd : "", initrd ? "\n" : "");
4fe85b
+
4fe85b
+  grub_normal_add_menu_entry (1, args, NULL, NULL, "bls", NULL, NULL, src, 0);
4fe85b
+
4fe85b
+finish:
4fe85b
+  grub_free (p);
4fe85b
+  grub_free (title);
4fe85b
+  grub_free (options);
4fe85b
+  grub_free (clinux);
4fe85b
+  grub_free (initrd);
4fe85b
+  grub_free (src);
4fe85b
+
4fe85b
+  if (f)
4fe85b
+    grub_file_close (f);
4fe85b
+
4fe85b
+  return 0;
4fe85b
+}
4fe85b
+
4fe85b
+static grub_err_t
4fe85b
+grub_cmd_bls_import (grub_extcmd_context_t ctxt __attribute__ ((unused)),
4fe85b
+		     int argc __attribute__ ((unused)),
4fe85b
+		     char **args __attribute__ ((unused)))
4fe85b
+{
4fe85b
+  grub_fs_t fs;
4fe85b
+  grub_device_t dev;
4fe85b
+  static grub_err_t r;
4fe85b
+  const char *devid;
4fe85b
+
4fe85b
+  devid = grub_env_get ("root");
4fe85b
+  if (!devid)
4fe85b
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "root");
4fe85b
+
4fe85b
+  dev = grub_device_open (devid);
4fe85b
+  if (!dev)
4fe85b
+    return grub_errno;
4fe85b
+
4fe85b
+  fs = grub_fs_probe (dev);
4fe85b
+  if (!fs)
4fe85b
+    {
4fe85b
+      r = grub_errno;
4fe85b
+      goto finish;
4fe85b
+    }
4fe85b
+
4fe85b
+  r = fs->dir (dev, GRUB_BLS_CONFIG_PATH, parse_entry, NULL);
4fe85b
+
4fe85b
+finish:
4fe85b
+  if (dev)
4fe85b
+    grub_device_close (dev);
4fe85b
+
4fe85b
+  return r;
4fe85b
+}
4fe85b
+
4fe85b
+static grub_extcmd_t cmd;
4fe85b
+
4fe85b
+GRUB_MOD_INIT(bls)
4fe85b
+{
4fe85b
+  cmd = grub_register_extcmd ("bls_import",
4fe85b
+			      grub_cmd_bls_import,
4fe85b
+			      0,
4fe85b
+			      NULL,
4fe85b
+			      N_("Import Boot Loader Specification snippets."),
4fe85b
+			      NULL);
4fe85b
+}
4fe85b
+
4fe85b
+GRUB_MOD_FINI(bls)
4fe85b
+{
4fe85b
+  grub_unregister_extcmd (cmd);
4fe85b
+}