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

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