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

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