dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

f96e0b
From 86e2916cbfa955b04b86b19bb92a29be42368d39 Mon Sep 17 00:00:00 2001
f96e0b
From: Fedora Ninjas <grub2-owner@fedoraproject.org>
f96e0b
Date: Tue, 22 Jan 2013 06:31:38 +0100
f96e0b
Subject: [PATCH 460/482] blscfg: add blscfg module to parse Boot Loader
f96e0b
 Specification snippets
f96e0b
f96e0b
http://www.freedesktop.org/wiki/Specifications/BootLoaderSpec
f96e0b
f96e0b
Works like this:
f96e0b
f96e0b
 insmod blscfg
f96e0b
 bls_import
f96e0b
f96e0b
Done! You should now have menu items for your snippets in place.
f96e0b
f96e0b
Signed-off-by: Peter Jones <grub2-owner@fedoraproject.org>
f96e0b
---
f96e0b
 grub-core/Makefile.core.def |   8 ++
f96e0b
 grub-core/commands/blscfg.c | 201 ++++++++++++++++++++++++++++++++++++++++++++
f96e0b
 2 files changed, 209 insertions(+)
f96e0b
 create mode 100644 grub-core/commands/blscfg.c
f96e0b
f96e0b
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
f96e0b
index def6606..08089de 100644
f96e0b
--- a/grub-core/Makefile.core.def
f96e0b
+++ b/grub-core/Makefile.core.def
f96e0b
@@ -653,6 +653,14 @@ module = {
f96e0b
 };
f96e0b
 
f96e0b
 module = {
f96e0b
+  name = blscfg;
f96e0b
+  common = commands/blscfg.c;
f96e0b
+  enable = i386_efi;
f96e0b
+  enable = x86_64_efi;
f96e0b
+  enable = i386_pc;
f96e0b
+};
f96e0b
+
f96e0b
+module = {
f96e0b
   name = boot;
f96e0b
   common = commands/boot.c;
f96e0b
   i386_pc = lib/i386/pc/biosnum.c;
f96e0b
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
f96e0b
new file mode 100644
f96e0b
index 0000000..4274aca
f96e0b
--- /dev/null
f96e0b
+++ b/grub-core/commands/blscfg.c
f96e0b
@@ -0,0 +1,201 @@
f96e0b
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/
f96e0b
+
f96e0b
+/* bls.c - implementation of the boot loader spec */
f96e0b
+
f96e0b
+/*
f96e0b
+ *  GRUB  --  GRand Unified Bootloader
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/extcmd.h>
f96e0b
+#include <grub/i18n.h>
f96e0b
+#include <grub/fs.h>
f96e0b
+#include <grub/env.h>
f96e0b
+#include <grub/file.h>
f96e0b
+#include <grub/normal.h>
f96e0b
+
f96e0b
+GRUB_MOD_LICENSE ("GPLv3+");
f96e0b
+
f96e0b
+#ifdef GRUB_MACHINE_EFI
f96e0b
+#define GRUB_LINUX_CMD "linuxefi"
f96e0b
+#define GRUB_INITRD_CMD "initrdefi"
f96e0b
+#define GRUB_BLS_CONFIG_PATH "/EFI/fedora/loader/entries/"
f96e0b
+#define GRUB_BOOT_DEVICE "($boot)"
f96e0b
+#else
f96e0b
+#define GRUB_LINUX_CMD "linux"
f96e0b
+#define GRUB_INITRD_CMD "initrd"
f96e0b
+#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
f96e0b
+#define GRUB_BOOT_DEVICE "($root)"
f96e0b
+#endif
f96e0b
+
f96e0b
+static int parse_entry (
f96e0b
+    const char *filename,
f96e0b
+    const struct grub_dirhook_info *info __attribute__ ((unused)),
f96e0b
+    void *data __attribute__ ((unused)))
f96e0b
+{
f96e0b
+  grub_size_t n;
f96e0b
+  char *p;
f96e0b
+  grub_file_t f = NULL;
f96e0b
+  grub_off_t sz;
f96e0b
+  char *title = NULL, *options = NULL, *clinux = NULL, *initrd = NULL, *src = NULL;
f96e0b
+  const char *args[2] = { NULL, NULL };
f96e0b
+
f96e0b
+  if (filename[0] == '.')
f96e0b
+    return 0;
f96e0b
+
f96e0b
+  n = grub_strlen (filename);
f96e0b
+  if (n <= 5)
f96e0b
+    return 0;
f96e0b
+
f96e0b
+  if (grub_strcmp (filename + n - 5, ".conf") != 0)
f96e0b
+    return 0;
f96e0b
+
f96e0b
+  p = grub_xasprintf (GRUB_BLS_CONFIG_PATH "%s", filename);
f96e0b
+
f96e0b
+  f = grub_file_open (p);
f96e0b
+  if (!f)
f96e0b
+    goto finish;
f96e0b
+
f96e0b
+  sz = grub_file_size (f);
f96e0b
+  if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024)
f96e0b
+    goto finish;
f96e0b
+
f96e0b
+  for (;;)
f96e0b
+    {
f96e0b
+      char *buf;
f96e0b
+
f96e0b
+      buf = grub_file_getline (f);
f96e0b
+      if (!buf)
f96e0b
+	break;
f96e0b
+
f96e0b
+      if (grub_strncmp (buf, "title ", 6) == 0)
f96e0b
+	{
f96e0b
+	  grub_free (title);
f96e0b
+	  title = grub_strdup (buf + 6);
f96e0b
+	  if (!title)
f96e0b
+	    goto finish;
f96e0b
+	}
f96e0b
+      else if (grub_strncmp (buf, "options ", 8) == 0)
f96e0b
+	{
f96e0b
+	  grub_free (options);
f96e0b
+	  options = grub_strdup (buf + 8);
f96e0b
+	  if (!options)
f96e0b
+	    goto finish;
f96e0b
+	}
f96e0b
+      else if (grub_strncmp (buf, "linux ", 6) == 0)
f96e0b
+	{
f96e0b
+	  grub_free (clinux);
f96e0b
+	  clinux = grub_strdup (buf + 6);
f96e0b
+	  if (!clinux)
f96e0b
+	    goto finish;
f96e0b
+	}
f96e0b
+      else if (grub_strncmp (buf, "initrd ", 7) == 0)
f96e0b
+	{
f96e0b
+	  grub_free (initrd);
f96e0b
+	  initrd = grub_strdup (buf + 7);
f96e0b
+	  if (!initrd)
f96e0b
+	    goto finish;
f96e0b
+	}
f96e0b
+
f96e0b
+      grub_free(buf);
f96e0b
+    }
f96e0b
+
f96e0b
+  if (!linux)
f96e0b
+    {
f96e0b
+      grub_printf ("Skipping file %s with no 'linux' key.", p);
f96e0b
+      goto finish;
f96e0b
+    }
f96e0b
+
f96e0b
+  args[0] = title ? title : filename;
f96e0b
+
f96e0b
+  src = grub_xasprintf ("load_video\n"
f96e0b
+			"set gfx_payload=keep\n"
f96e0b
+			"insmod gzio\n"
f96e0b
+			GRUB_LINUX_CMD " %s%s%s%s\n"
f96e0b
+			"%s%s%s%s",
f96e0b
+			GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options : "",
f96e0b
+			initrd ? GRUB_INITRD_CMD " " : "", initrd ? GRUB_BOOT_DEVICE : "", initrd ? initrd : "", initrd ? "\n" : "");
f96e0b
+
f96e0b
+  grub_normal_add_menu_entry (1, args, NULL, NULL, "bls", NULL, NULL, src, 0);
f96e0b
+
f96e0b
+finish:
f96e0b
+  grub_free (p);
f96e0b
+  grub_free (title);
f96e0b
+  grub_free (options);
f96e0b
+  grub_free (clinux);
f96e0b
+  grub_free (initrd);
f96e0b
+  grub_free (src);
f96e0b
+
f96e0b
+  if (f)
f96e0b
+    grub_file_close (f);
f96e0b
+
f96e0b
+  return 0;
f96e0b
+}
f96e0b
+
f96e0b
+static grub_err_t
f96e0b
+grub_cmd_bls_import (grub_extcmd_context_t ctxt __attribute__ ((unused)),
f96e0b
+		     int argc __attribute__ ((unused)),
f96e0b
+		     char **args __attribute__ ((unused)))
f96e0b
+{
f96e0b
+  grub_fs_t fs;
f96e0b
+  grub_device_t dev;
f96e0b
+  static grub_err_t r;
f96e0b
+  const char *devid;
f96e0b
+
f96e0b
+  devid = grub_env_get ("root");
f96e0b
+  if (!devid)
f96e0b
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "root");
f96e0b
+
f96e0b
+  dev = grub_device_open (devid);
f96e0b
+  if (!dev)
f96e0b
+    return grub_errno;
f96e0b
+
f96e0b
+  fs = grub_fs_probe (dev);
f96e0b
+  if (!fs)
f96e0b
+    {
f96e0b
+      r = grub_errno;
f96e0b
+      goto finish;
f96e0b
+    }
f96e0b
+
f96e0b
+  r = fs->dir (dev, GRUB_BLS_CONFIG_PATH, parse_entry, NULL);
f96e0b
+
f96e0b
+finish:
f96e0b
+  if (dev)
f96e0b
+    grub_device_close (dev);
f96e0b
+
f96e0b
+  return r;
f96e0b
+}
f96e0b
+
f96e0b
+static grub_extcmd_t cmd;
f96e0b
+
f96e0b
+GRUB_MOD_INIT(bls)
f96e0b
+{
f96e0b
+  cmd = grub_register_extcmd ("bls_import",
f96e0b
+			      grub_cmd_bls_import,
f96e0b
+			      0,
f96e0b
+			      NULL,
f96e0b
+			      N_("Import Boot Loader Specification snippets."),
f96e0b
+			      NULL);
f96e0b
+}
f96e0b
+
f96e0b
+GRUB_MOD_FINI(bls)
f96e0b
+{
f96e0b
+  grub_unregister_extcmd (cmd);
f96e0b
+}
f96e0b
-- 
f96e0b
1.8.2.1
f96e0b