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