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