dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

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

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