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