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

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