dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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