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

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