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