nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Peter Jones <pjones@redhat.com>
5593c8
Date: Tue, 22 Jan 2013 06:31:38 +0100
5593c8
Subject: [PATCH] blscfg: add blscfg module to parse Boot Loader Specification
5593c8
 snippets
5593c8
5593c8
The BootLoaderSpec (BLS) defines a scheme where different bootloaders can
5593c8
share a format for boot items and a configuration directory that accepts
5593c8
these common configurations as drop-in files.
5593c8
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
5593c8
[wjt: some cleanups and fixes]
5593c8
Signed-off-by: Will Thompson <wjt@endlessm.com>
5593c8
---
5593c8
 grub-core/Makefile.core.def    |   11 +
5593c8
 grub-core/commands/blscfg.c    | 1177 ++++++++++++++++++++++++++++++++++++++++
5593c8
 grub-core/commands/legacycfg.c |    5 +-
5593c8
 grub-core/commands/loadenv.c   |   77 +--
5593c8
 grub-core/commands/menuentry.c |   20 +-
5593c8
 grub-core/normal/main.c        |    6 +
5593c8
 grub-core/commands/loadenv.h   |   93 ++++
5593c8
 include/grub/compiler.h        |    2 +
5593c8
 include/grub/menu.h            |   13 +
5593c8
 include/grub/normal.h          |    2 +-
5593c8
 10 files changed, 1324 insertions(+), 82 deletions(-)
5593c8
 create mode 100644 grub-core/commands/blscfg.c
5593c8
 create mode 100644 grub-core/commands/loadenv.h
5593c8
5593c8
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
5593c8
index c865a08b027..c15e91943b9 100644
5593c8
--- a/grub-core/Makefile.core.def
5593c8
+++ b/grub-core/Makefile.core.def
5593c8
@@ -814,6 +814,16 @@ module = {
5593c8
   common = commands/blocklist.c;
5593c8
 };
5593c8
 
5593c8
+module = {
5593c8
+  name = blscfg;
5593c8
+  common = commands/blscfg.c;
5593c8
+  common = commands/loadenv.h;
5593c8
+  enable = powerpc_ieee1275;
5593c8
+  enable = efi;
5593c8
+  enable = i386_pc;
5593c8
+  enable = emu;
5593c8
+};
5593c8
+
5593c8
 module = {
5593c8
   name = boot;
5593c8
   common = commands/boot.c;
5593c8
@@ -980,6 +990,7 @@ module = {
5593c8
 module = {
5593c8
   name = loadenv;
5593c8
   common = commands/loadenv.c;
5593c8
+  common = commands/loadenv.h;
5593c8
   common = lib/envblk.c;
5593c8
 };
5593c8
 
5593c8
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
5593c8
new file mode 100644
5593c8
index 00000000000..e907a6a5d28
5593c8
--- /dev/null
5593c8
+++ b/grub-core/commands/blscfg.c
5593c8
@@ -0,0 +1,1177 @@
5593c8
+/*-*- Mode: C; c-basic-offset: 2; indent-tabs-mode: t -*-*/
5593c8
+
5593c8
+/* bls.c - implementation of the boot loader spec */
5593c8
+
5593c8
+/*
5593c8
+ *  GRUB  --  GRand Unified Bootloader
5593c8
+ *
5593c8
+ *  GRUB is free software: you can redistribute it and/or modify
5593c8
+ *  it under the terms of the GNU General Public License as published by
5593c8
+ *  the Free Software Foundation, either version 3 of the License, or
5593c8
+ *  (at your option) any later version.
5593c8
+ *
5593c8
+ *  GRUB is distributed in the hope that it will be useful,
5593c8
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
5593c8
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5593c8
+ *  GNU General Public License for more details.
5593c8
+ *
5593c8
+ *  You should have received a copy of the GNU General Public License
5593c8
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
5593c8
+ */
5593c8
+
5593c8
+#include <grub/list.h>
5593c8
+#include <grub/types.h>
5593c8
+#include <grub/misc.h>
5593c8
+#include <grub/mm.h>
5593c8
+#include <grub/err.h>
5593c8
+#include <grub/dl.h>
5593c8
+#include <grub/extcmd.h>
5593c8
+#include <grub/i18n.h>
5593c8
+#include <grub/fs.h>
5593c8
+#include <grub/env.h>
5593c8
+#include <grub/file.h>
5593c8
+#include <grub/normal.h>
5593c8
+#include <grub/lib/envblk.h>
5593c8
+
5593c8
+#include <stdbool.h>
5593c8
+
5593c8
+GRUB_MOD_LICENSE ("GPLv3+");
5593c8
+
5593c8
+#include "loadenv.h"
5593c8
+
5593c8
+#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
5593c8
+#ifdef GRUB_MACHINE_EMU
5593c8
+#define GRUB_BOOT_DEVICE "/boot"
5593c8
+#else
5593c8
+#define GRUB_BOOT_DEVICE "($root)"
5593c8
+#endif
5593c8
+
5593c8
+struct keyval
5593c8
+{
5593c8
+  const char *key;
5593c8
+  char *val;
5593c8
+};
5593c8
+
5593c8
+static struct bls_entry *entries = NULL;
5593c8
+
5593c8
+#define FOR_BLS_ENTRIES(var) FOR_LIST_ELEMENTS (var, entries)
5593c8
+
5593c8
+static int bls_add_keyval(struct bls_entry *entry, char *key, char *val)
5593c8
+{
5593c8
+  char *k, *v;
5593c8
+  struct keyval **kvs, *kv;
5593c8
+  int new_n = entry->nkeyvals + 1;
5593c8
+
5593c8
+  kvs = grub_realloc (entry->keyvals, new_n * sizeof (struct keyval *));
5593c8
+  if (!kvs)
5593c8
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
5593c8
+		       "couldn't find space for BLS entry");
5593c8
+  entry->keyvals = kvs;
5593c8
+
5593c8
+  kv = grub_malloc (sizeof (struct keyval));
5593c8
+  if (!kv)
5593c8
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
5593c8
+		       "couldn't find space for BLS entry");
5593c8
+
5593c8
+  k = grub_strdup (key);
5593c8
+  if (!k)
5593c8
+    {
5593c8
+      grub_free (kv);
5593c8
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
5593c8
+			 "couldn't find space for BLS entry");
5593c8
+    }
5593c8
+
5593c8
+  v = grub_strdup (val);
5593c8
+  if (!v)
5593c8
+    {
5593c8
+      grub_free (k);
5593c8
+      grub_free (kv);
5593c8
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
5593c8
+			 "couldn't find space for BLS entry");
5593c8
+    }
5593c8
+
5593c8
+  kv->key = k;
5593c8
+  kv->val = v;
5593c8
+
5593c8
+  entry->keyvals[entry->nkeyvals] = kv;
5593c8
+  grub_dprintf("blscfg", "new keyval at %p:%s:%s\n", entry->keyvals[entry->nkeyvals], k, v);
5593c8
+  entry->nkeyvals = new_n;
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+/* Find they value of the key named by keyname.  If there are allowed to be
5593c8
+ * more than one, pass a pointer to an int set to -1 the first time, and pass
5593c8
+ * the same pointer through each time after, and it'll return them in sorted
5593c8
+ * order as defined in the BLS fragment file */
5593c8
+static char *bls_get_val(struct bls_entry *entry, const char *keyname, int *last)
5593c8
+{
5593c8
+  int idx, start = 0;
5593c8
+  struct keyval *kv = NULL;
5593c8
+
5593c8
+  if (last)
5593c8
+    start = *last + 1;
5593c8
+
5593c8
+  for (idx = start; idx < entry->nkeyvals; idx++) {
5593c8
+    kv = entry->keyvals[idx];
5593c8
+
5593c8
+    if (!grub_strcmp (keyname, kv->key))
5593c8
+      break;
5593c8
+  }
5593c8
+
5593c8
+  if (idx == entry->nkeyvals) {
5593c8
+    if (last)
5593c8
+      *last = -1;
5593c8
+    return NULL;
5593c8
+  }
5593c8
+
5593c8
+  if (last)
5593c8
+    *last = idx;
5593c8
+
5593c8
+  return kv->val;
5593c8
+}
5593c8
+
5593c8
+#define goto_return(x) ({ ret = (x); goto finish; })
5593c8
+
5593c8
+/* compare alpha and numeric segments of two versions */
5593c8
+/* return 1: a is newer than b */
5593c8
+/*        0: a and b are the same version */
5593c8
+/*       -1: b is newer than a */
5593c8
+static int vercmp(const char * a, const char * b)
5593c8
+{
5593c8
+    char oldch1, oldch2;
5593c8
+    char *abuf, *bbuf;
5593c8
+    char *str1, *str2;
5593c8
+    char * one, * two;
5593c8
+    int rc;
5593c8
+    int isnum;
5593c8
+    int ret = 0;
5593c8
+
5593c8
+    grub_dprintf("blscfg", "%s comparing %s and %s\n", __func__, a, b);
5593c8
+    if (!grub_strcmp(a, b))
5593c8
+	    return 0;
5593c8
+
5593c8
+    abuf = grub_malloc(grub_strlen(a) + 1);
5593c8
+    bbuf = grub_malloc(grub_strlen(b) + 1);
5593c8
+    str1 = abuf;
5593c8
+    str2 = bbuf;
5593c8
+    grub_strcpy(str1, a);
5593c8
+    grub_strcpy(str2, b);
5593c8
+
5593c8
+    one = str1;
5593c8
+    two = str2;
5593c8
+
5593c8
+    /* loop through each version segment of str1 and str2 and compare them */
5593c8
+    while (*one || *two) {
5593c8
+	while (*one && !grub_isalnum(*one) && *one != '~' && *one != '+') one++;
5593c8
+	while (*two && !grub_isalnum(*two) && *two != '~' && *two != '+') two++;
5593c8
+
5593c8
+	/* handle the tilde separator, it sorts before everything else */
5593c8
+	if (*one == '~' || *two == '~') {
5593c8
+	    if (*one != '~') goto_return (1);
5593c8
+	    if (*two != '~') goto_return (-1);
5593c8
+	    one++;
5593c8
+	    two++;
5593c8
+	    continue;
5593c8
+	}
5593c8
+
5593c8
+	/*
5593c8
+	 * Handle plus separator. Concept is the same as tilde,
5593c8
+	 * except that if one of the strings ends (base version),
5593c8
+	 * the other is considered as higher version.
5593c8
+	 */
5593c8
+	if (*one == '+' || *two == '+') {
5593c8
+	    if (!*one) return -1;
5593c8
+	    if (!*two) return 1;
5593c8
+	    if (*one != '+') goto_return (1);
5593c8
+	    if (*two != '+') goto_return (-1);
5593c8
+	    one++;
5593c8
+	    two++;
5593c8
+	    continue;
5593c8
+	}
5593c8
+
5593c8
+	/* If we ran to the end of either, we are finished with the loop */
5593c8
+	if (!(*one && *two)) break;
5593c8
+
5593c8
+	str1 = one;
5593c8
+	str2 = two;
5593c8
+
5593c8
+	/* grab first completely alpha or completely numeric segment */
5593c8
+	/* leave one and two pointing to the start of the alpha or numeric */
5593c8
+	/* segment and walk str1 and str2 to end of segment */
5593c8
+	if (grub_isdigit(*str1)) {
5593c8
+	    while (*str1 && grub_isdigit(*str1)) str1++;
5593c8
+	    while (*str2 && grub_isdigit(*str2)) str2++;
5593c8
+	    isnum = 1;
5593c8
+	} else {
5593c8
+	    while (*str1 && grub_isalpha(*str1)) str1++;
5593c8
+	    while (*str2 && grub_isalpha(*str2)) str2++;
5593c8
+	    isnum = 0;
5593c8
+	}
5593c8
+
5593c8
+	/* save character at the end of the alpha or numeric segment */
5593c8
+	/* so that they can be restored after the comparison */
5593c8
+	oldch1 = *str1;
5593c8
+	*str1 = '\0';
5593c8
+	oldch2 = *str2;
5593c8
+	*str2 = '\0';
5593c8
+
5593c8
+	/* this cannot happen, as we previously tested to make sure that */
5593c8
+	/* the first string has a non-null segment */
5593c8
+	if (one == str1) goto_return(-1);	/* arbitrary */
5593c8
+
5593c8
+	/* take care of the case where the two version segments are */
5593c8
+	/* different types: one numeric, the other alpha (i.e. empty) */
5593c8
+	/* numeric segments are always newer than alpha segments */
5593c8
+	/* XXX See patch #60884 (and details) from bugzilla #50977. */
5593c8
+	if (two == str2) goto_return (isnum ? 1 : -1);
5593c8
+
5593c8
+	if (isnum) {
5593c8
+	    grub_size_t onelen, twolen;
5593c8
+	    /* this used to be done by converting the digit segments */
5593c8
+	    /* to ints using atoi() - it's changed because long  */
5593c8
+	    /* digit segments can overflow an int - this should fix that. */
5593c8
+
5593c8
+	    /* throw away any leading zeros - it's a number, right? */
5593c8
+	    while (*one == '0') one++;
5593c8
+	    while (*two == '0') two++;
5593c8
+
5593c8
+	    /* whichever number has more digits wins */
5593c8
+	    onelen = grub_strlen(one);
5593c8
+	    twolen = grub_strlen(two);
5593c8
+	    if (onelen > twolen) goto_return (1);
5593c8
+	    if (twolen > onelen) goto_return (-1);
5593c8
+	}
5593c8
+
5593c8
+	/* grub_strcmp will return which one is greater - even if the two */
5593c8
+	/* segments are alpha or if they are numeric.  don't return  */
5593c8
+	/* if they are equal because there might be more segments to */
5593c8
+	/* compare */
5593c8
+	rc = grub_strcmp(one, two);
5593c8
+	if (rc) goto_return (rc < 1 ? -1 : 1);
5593c8
+
5593c8
+	/* restore character that was replaced by null above */
5593c8
+	*str1 = oldch1;
5593c8
+	one = str1;
5593c8
+	*str2 = oldch2;
5593c8
+	two = str2;
5593c8
+    }
5593c8
+
5593c8
+    /* this catches the case where all numeric and alpha segments have */
5593c8
+    /* compared identically but the segment sepparating characters were */
5593c8
+    /* different */
5593c8
+    if ((!*one) && (!*two)) goto_return (0);
5593c8
+
5593c8
+    /* whichever version still has characters left over wins */
5593c8
+    if (!*one) goto_return (-1); else goto_return (1);
5593c8
+
5593c8
+finish:
5593c8
+    grub_free (abuf);
5593c8
+    grub_free (bbuf);
5593c8
+    return ret;
5593c8
+}
5593c8
+
5593c8
+/* returns name/version/release */
5593c8
+/* NULL string pointer returned if nothing found */
5593c8
+static void
5593c8
+split_package_string (char *package_string, char **name,
5593c8
+                     char **version, char **release)
5593c8
+{
5593c8
+  char *package_version, *package_release;
5593c8
+
5593c8
+  /* Release */
5593c8
+  package_release = grub_strrchr (package_string, '-');
5593c8
+
5593c8
+  if (package_release != NULL)
5593c8
+      *package_release++ = '\0';
5593c8
+
5593c8
+  *release = package_release;
5593c8
+
5593c8
+  if (name == NULL)
5593c8
+    {
5593c8
+      *version = package_string;
5593c8
+    }
5593c8
+  else
5593c8
+    {
5593c8
+      /* Version */
5593c8
+      package_version = grub_strrchr(package_string, '-');
5593c8
+
5593c8
+      if (package_version != NULL)
5593c8
+	*package_version++ = '\0';
5593c8
+
5593c8
+      *version = package_version;
5593c8
+      /* Name */
5593c8
+      *name = package_string;
5593c8
+    }
5593c8
+
5593c8
+  /* Bubble up non-null values from release to name */
5593c8
+  if (name != NULL && *name == NULL)
5593c8
+    {
5593c8
+      *name = (*version == NULL ? *release : *version);
5593c8
+      *version = *release;
5593c8
+      *release = NULL;
5593c8
+    }
5593c8
+  if (*version == NULL)
5593c8
+    {
5593c8
+      *version = *release;
5593c8
+      *release = NULL;
5593c8
+    }
5593c8
+}
5593c8
+
5593c8
+static int
5593c8
+split_cmp(char *nvr0, char *nvr1, int has_name)
5593c8
+{
5593c8
+  int ret = 0;
5593c8
+  char *name0, *version0, *release0;
5593c8
+  char *name1, *version1, *release1;
5593c8
+
5593c8
+  split_package_string(nvr0, has_name ? &name0 : NULL, &version0, &release0);
5593c8
+  split_package_string(nvr1, has_name ? &name1 : NULL, &version1, &release1);
5593c8
+
5593c8
+  if (has_name)
5593c8
+    {
5593c8
+      ret = vercmp(name0 == NULL ? "" : name0,
5593c8
+		   name1 == NULL ? "" : name1);
5593c8
+      if (ret != 0)
5593c8
+	return ret;
5593c8
+    }
5593c8
+
5593c8
+  ret = vercmp(version0 == NULL ? "" : version0,
5593c8
+	       version1 == NULL ? "" : version1);
5593c8
+  if (ret != 0)
5593c8
+    return ret;
5593c8
+
5593c8
+  ret = vercmp(release0 == NULL ? "" : release0,
5593c8
+	       release1 == NULL ? "" : release1);
5593c8
+  return ret;
5593c8
+}
5593c8
+
5593c8
+/* return 1: e0 is newer than e1 */
5593c8
+/*        0: e0 and e1 are the same version */
5593c8
+/*       -1: e1 is newer than e0 */
5593c8
+static int bls_cmp(const struct bls_entry *e0, const struct bls_entry *e1)
5593c8
+{
5593c8
+  char *id0, *id1;
5593c8
+  int r;
5593c8
+
5593c8
+  id0 = grub_strdup(e0->filename);
5593c8
+  id1 = grub_strdup(e1->filename);
5593c8
+
5593c8
+  r = split_cmp(id0, id1, 1);
5593c8
+
5593c8
+  grub_free(id0);
5593c8
+  grub_free(id1);
5593c8
+
5593c8
+  return r;
5593c8
+}
5593c8
+
5593c8
+static void list_add_tail(struct bls_entry *head, struct bls_entry *item)
5593c8
+{
5593c8
+  item->next = head;
5593c8
+  if (head->prev)
5593c8
+    head->prev->next = item;
5593c8
+  item->prev = head->prev;
5593c8
+  head->prev = item;
5593c8
+}
5593c8
+
5593c8
+static int bls_add_entry(struct bls_entry *entry)
5593c8
+{
5593c8
+  struct bls_entry *e, *last = NULL;
5593c8
+  int rc;
5593c8
+
5593c8
+  if (!entries) {
5593c8
+    grub_dprintf ("blscfg", "Add entry with id \"%s\"\n", entry->filename);
5593c8
+    entries = entry;
5593c8
+    return 0;
5593c8
+  }
5593c8
+
5593c8
+  FOR_BLS_ENTRIES(e) {
5593c8
+    rc = bls_cmp(entry, e);
5593c8
+
5593c8
+    if (!rc)
5593c8
+      return GRUB_ERR_BAD_ARGUMENT;
5593c8
+
5593c8
+    if (rc == 1) {
5593c8
+      grub_dprintf ("blscfg", "Add entry with id \"%s\"\n", entry->filename);
5593c8
+      list_add_tail (e, entry);
5593c8
+      if (e == entries) {
5593c8
+	entries = entry;
5593c8
+	entry->prev = NULL;
5593c8
+      }
5593c8
+      return 0;
5593c8
+    }
5593c8
+    last = e;
5593c8
+  }
5593c8
+
5593c8
+  if (last) {
5593c8
+    grub_dprintf ("blscfg", "Add entry with id \"%s\"\n", entry->filename);
5593c8
+    last->next = entry;
5593c8
+    entry->prev = last;
5593c8
+  }
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+struct read_entry_info {
5593c8
+  const char *devid;
5593c8
+  const char *dirname;
5593c8
+  grub_file_t file;
5593c8
+};
5593c8
+
5593c8
+static int read_entry (
5593c8
+    const char *filename,
5593c8
+    const struct grub_dirhook_info *dirhook_info UNUSED,
5593c8
+    void *data)
5593c8
+{
5593c8
+  grub_size_t m = 0, n, clip = 0;
5593c8
+  int rc = 0;
5593c8
+  char *p = NULL;
5593c8
+  grub_file_t f = NULL;
5593c8
+  struct bls_entry *entry;
5593c8
+  struct read_entry_info *info = (struct read_entry_info *)data;
5593c8
+
5593c8
+  grub_dprintf ("blscfg", "filename: \"%s\"\n", filename);
5593c8
+
5593c8
+  n = grub_strlen (filename);
5593c8
+
5593c8
+  if (info->file)
5593c8
+    {
5593c8
+      f = info->file;
5593c8
+    }
5593c8
+  else
5593c8
+    {
5593c8
+      if (filename[0] == '.')
5593c8
+	return 0;
5593c8
+
5593c8
+      if (n <= 5)
5593c8
+	return 0;
5593c8
+
5593c8
+      if (grub_strcmp (filename + n - 5, ".conf") != 0)
5593c8
+	return 0;
5593c8
+
5593c8
+      p = grub_xasprintf ("(%s)%s/%s", info->devid, info->dirname, filename);
5593c8
+
5593c8
+      f = grub_file_open (p, GRUB_FILE_TYPE_CONFIG);
5593c8
+      if (!f)
5593c8
+	goto finish;
5593c8
+    }
5593c8
+
5593c8
+  entry = grub_zalloc (sizeof (*entry));
5593c8
+  if (!entry)
5593c8
+    goto finish;
5593c8
+
5593c8
+  if (info->file)
5593c8
+    {
5593c8
+      char *slash;
5593c8
+
5593c8
+      if (n > 5 && !grub_strcmp (filename + n - 5, ".conf") == 0)
5593c8
+	clip = 5;
5593c8
+
5593c8
+      slash = grub_strrchr (filename, '/');
5593c8
+      if (!slash)
5593c8
+	slash = grub_strrchr (filename, '\\');
5593c8
+
5593c8
+      while (*slash == '/' || *slash == '\\')
5593c8
+	slash++;
5593c8
+
5593c8
+      m = slash ? slash - filename : 0;
5593c8
+    }
5593c8
+  else
5593c8
+    {
5593c8
+      m = 0;
5593c8
+      clip = 5;
5593c8
+    }
5593c8
+  n -= m;
5593c8
+
5593c8
+  entry->filename = grub_strndup(filename + m, n - clip);
5593c8
+  if (!entry->filename)
5593c8
+    goto finish;
5593c8
+
5593c8
+  entry->filename[n - 5] = '\0';
5593c8
+
5593c8
+  for (;;)
5593c8
+    {
5593c8
+      char *buf;
5593c8
+      char *separator;
5593c8
+
5593c8
+      buf = grub_file_getline (f);
5593c8
+      if (!buf)
5593c8
+	break;
5593c8
+
5593c8
+      while (buf && buf[0] && (buf[0] == ' ' || buf[0] == '\t'))
5593c8
+	buf++;
5593c8
+      if (buf[0] == '#')
5593c8
+	continue;
5593c8
+
5593c8
+      separator = grub_strchr (buf, ' ');
5593c8
+
5593c8
+      if (!separator)
5593c8
+	separator = grub_strchr (buf, '\t');
5593c8
+
5593c8
+      if (!separator || separator[1] == '\0')
5593c8
+	{
5593c8
+	  grub_free (buf);
5593c8
+	  break;
5593c8
+	}
5593c8
+
5593c8
+      separator[0] = '\0';
5593c8
+
5593c8
+      do {
5593c8
+	separator++;
5593c8
+      } while (*separator == ' ' || *separator == '\t');
5593c8
+
5593c8
+      rc = bls_add_keyval (entry, buf, separator);
5593c8
+      grub_free (buf);
5593c8
+      if (rc < 0)
5593c8
+	break;
5593c8
+    }
5593c8
+
5593c8
+    if (!rc)
5593c8
+      bls_add_entry(entry);
5593c8
+
5593c8
+finish:
5593c8
+  if (p)
5593c8
+    grub_free (p);
5593c8
+
5593c8
+  if (f)
5593c8
+    grub_file_close (f);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static grub_envblk_t saved_env = NULL;
5593c8
+
5593c8
+static int UNUSED
5593c8
+save_var (const char *name, const char *value, void *whitelist UNUSED)
5593c8
+{
5593c8
+  const char *val = grub_env_get (name);
5593c8
+  grub_dprintf("blscfg", "saving \"%s\"\n", name);
5593c8
+
5593c8
+  if (val)
5593c8
+    grub_envblk_set (saved_env, name, value);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static int UNUSED
5593c8
+unset_var (const char *name, const char *value UNUSED, void *whitelist)
5593c8
+{
5593c8
+  grub_dprintf("blscfg", "restoring \"%s\"\n", name);
5593c8
+  if (! whitelist)
5593c8
+    {
5593c8
+      grub_env_unset (name);
5593c8
+      return 0;
5593c8
+    }
5593c8
+
5593c8
+  if (test_whitelist_membership (name,
5593c8
+				 (const grub_env_whitelist_t *) whitelist))
5593c8
+    grub_env_unset (name);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static char **bls_make_list (struct bls_entry *entry, const char *key, int *num)
5593c8
+{
5593c8
+  int last = -1;
5593c8
+  char *val;
5593c8
+
5593c8
+  int nlist = 0;
5593c8
+  char **list = NULL;
5593c8
+
5593c8
+  list = grub_malloc (sizeof (char *));
5593c8
+  if (!list)
5593c8
+    return NULL;
5593c8
+  list[0] = NULL;
5593c8
+
5593c8
+  while (1)
5593c8
+    {
5593c8
+      char **new;
5593c8
+
5593c8
+      val = bls_get_val (entry, key, &last);
5593c8
+      if (!val)
5593c8
+	break;
5593c8
+
5593c8
+      new = grub_realloc (list, (nlist + 2) * sizeof (char *));
5593c8
+      if (!new)
5593c8
+	break;
5593c8
+
5593c8
+      list = new;
5593c8
+      list[nlist++] = val;
5593c8
+      list[nlist] = NULL;
5593c8
+  }
5593c8
+
5593c8
+  if (!nlist)
5593c8
+    {
5593c8
+      grub_free (list);
5593c8
+      return NULL;
5593c8
+    }
5593c8
+
5593c8
+  if (num)
5593c8
+    *num = nlist;
5593c8
+
5593c8
+  return list;
5593c8
+}
5593c8
+
5593c8
+static char *field_append(bool is_var, char *buffer, const char *start, const char *end)
5593c8
+{
5593c8
+  char *tmp = grub_strndup(start, end - start + 1);
5593c8
+  const char *field = tmp;
5593c8
+  int term = is_var ? 2 : 1;
5593c8
+
5593c8
+  if (is_var) {
5593c8
+    field = grub_env_get (tmp);
5593c8
+    if (!field)
5593c8
+      return buffer;
5593c8
+  }
5593c8
+
5593c8
+  if (!buffer)
5593c8
+    buffer = grub_zalloc (grub_strlen(field) + term);
5593c8
+  else
5593c8
+    buffer = grub_realloc (buffer, grub_strlen(buffer) + grub_strlen(field) + term);
5593c8
+
5593c8
+  if (!buffer)
5593c8
+    return NULL;
5593c8
+
5593c8
+  tmp = buffer + grub_strlen(buffer);
5593c8
+  tmp = grub_stpcpy (tmp, field);
5593c8
+
5593c8
+  if (is_var)
5593c8
+      tmp = grub_stpcpy (tmp, " ");
5593c8
+
5593c8
+  return buffer;
5593c8
+}
5593c8
+
5593c8
+static char *expand_val(const char *value)
5593c8
+{
5593c8
+  char *buffer = NULL;
5593c8
+  const char *start = value;
5593c8
+  const char *end = value;
5593c8
+  bool is_var = false;
5593c8
+
5593c8
+  if (!value)
5593c8
+    return NULL;
5593c8
+
5593c8
+  while (*value) {
5593c8
+    if (*value == '$') {
5593c8
+      if (start != end) {
5593c8
+	buffer = field_append(is_var, buffer, start, end);
5593c8
+	if (!buffer)
5593c8
+	  return NULL;
5593c8
+      }
5593c8
+
5593c8
+      is_var = true;
5593c8
+      start = value + 1;
5593c8
+    } else if (is_var) {
5593c8
+      if (!grub_isalnum(*value) && *value != '_') {
5593c8
+	buffer = field_append(is_var, buffer, start, end);
5593c8
+	is_var = false;
5593c8
+	start = value;
5593c8
+	if (*start == ' ')
5593c8
+	  start++;
5593c8
+      }
5593c8
+    }
5593c8
+
5593c8
+    end = value;
5593c8
+    value++;
5593c8
+  }
5593c8
+
5593c8
+  if (start != end) {
5593c8
+    buffer = field_append(is_var, buffer, start, end);
5593c8
+    if (!buffer)
5593c8
+      return NULL;
5593c8
+  }
5593c8
+
5593c8
+  return buffer;
5593c8
+}
5593c8
+
5593c8
+static char **early_initrd_list (const char *initrd)
5593c8
+{
5593c8
+  int nlist = 0;
5593c8
+  char **list = NULL;
5593c8
+  char *separator;
5593c8
+
5593c8
+  while ((separator = grub_strchr (initrd, ' ')))
5593c8
+    {
5593c8
+      list = grub_realloc (list, (nlist + 2) * sizeof (char *));
5593c8
+      if (!list)
5593c8
+        return NULL;
5593c8
+
5593c8
+      list[nlist++] = grub_strndup(initrd, separator - initrd);
5593c8
+      list[nlist] = NULL;
5593c8
+      initrd = separator + 1;
5593c8
+  }
5593c8
+
5593c8
+  list = grub_realloc (list, (nlist + 2) * sizeof (char *));
5593c8
+  if (!list)
5593c8
+    return NULL;
5593c8
+
5593c8
+  list[nlist++] = grub_strndup(initrd, grub_strlen(initrd));
5593c8
+  list[nlist] = NULL;
5593c8
+
5593c8
+  return list;
5593c8
+}
5593c8
+
5593c8
+static void create_entry (struct bls_entry *entry)
5593c8
+{
5593c8
+  int argc = 0;
5593c8
+  const char **argv = NULL;
5593c8
+
5593c8
+  char *title = NULL;
5593c8
+  char *clinux = NULL;
5593c8
+  char *options = NULL;
5593c8
+  char **initrds = NULL;
5593c8
+  char *initrd = NULL;
5593c8
+  const char *early_initrd = NULL;
5593c8
+  char **early_initrds = NULL;
5593c8
+  char *initrd_prefix = NULL;
5593c8
+  char *devicetree = NULL;
5593c8
+  char *dt = NULL;
5593c8
+  char *id = entry->filename;
5593c8
+  char *dotconf = id;
5593c8
+  char *hotkey = NULL;
5593c8
+
5593c8
+  char *users = NULL;
5593c8
+  char **classes = NULL;
5593c8
+
5593c8
+  char **args = NULL;
5593c8
+
5593c8
+  char *src = NULL;
5593c8
+  int i, index;
5593c8
+  bool add_dt_prefix = false;
5593c8
+
5593c8
+  grub_dprintf("blscfg", "%s got here\n", __func__);
5593c8
+  clinux = bls_get_val (entry, "linux", NULL);
5593c8
+  if (!clinux)
5593c8
+    {
5593c8
+      grub_dprintf ("blscfg", "Skipping file %s with no 'linux' key.\n", entry->filename);
5593c8
+      goto finish;
5593c8
+    }
5593c8
+
5593c8
+  /*
5593c8
+   * strip the ".conf" off the end before we make it our "id" field.
5593c8
+   */
5593c8
+  do
5593c8
+    {
5593c8
+      dotconf = grub_strstr(dotconf, ".conf");
5593c8
+    } while (dotconf != NULL && dotconf[5] != '\0');
5593c8
+  if (dotconf)
5593c8
+    dotconf[0] = '\0';
5593c8
+
5593c8
+  title = bls_get_val (entry, "title", NULL);
5593c8
+  options = expand_val (bls_get_val (entry, "options", NULL));
5593c8
+
5593c8
+  if (!options)
5593c8
+    options = expand_val (grub_env_get("default_kernelopts"));
5593c8
+
5593c8
+  initrds = bls_make_list (entry, "initrd", NULL);
5593c8
+
5593c8
+  devicetree = expand_val (bls_get_val (entry, "devicetree", NULL));
5593c8
+
5593c8
+  if (!devicetree)
5593c8
+    {
5593c8
+      devicetree = expand_val (grub_env_get("devicetree"));
5593c8
+      add_dt_prefix = true;
5593c8
+    }
5593c8
+
5593c8
+  hotkey = bls_get_val (entry, "grub_hotkey", NULL);
5593c8
+  users = expand_val (bls_get_val (entry, "grub_users", NULL));
5593c8
+  classes = bls_make_list (entry, "grub_class", NULL);
5593c8
+  args = bls_make_list (entry, "grub_arg", &argc);
5593c8
+
5593c8
+  argc += 1;
5593c8
+  argv = grub_malloc ((argc + 1) * sizeof (char *));
5593c8
+  argv[0] = title ? title : clinux;
5593c8
+  for (i = 1; i < argc; i++)
5593c8
+    argv[i] = args[i-1];
5593c8
+  argv[argc] = NULL;
5593c8
+
5593c8
+  early_initrd = grub_env_get("early_initrd");
5593c8
+
5593c8
+  grub_dprintf ("blscfg", "adding menu entry for \"%s\" with id \"%s\"\n",
5593c8
+		title, id);
5593c8
+  if (early_initrd)
5593c8
+    {
5593c8
+      early_initrds = early_initrd_list(early_initrd);
5593c8
+      if (!early_initrds)
5593c8
+      {
5593c8
+	grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+	goto finish;
5593c8
+      }
5593c8
+
5593c8
+      if (initrds != NULL && initrds[0] != NULL)
5593c8
+	{
5593c8
+	  initrd_prefix = grub_strrchr (initrds[0], '/');
5593c8
+	  initrd_prefix = grub_strndup(initrds[0], initrd_prefix - initrds[0] + 1);
5593c8
+	}
5593c8
+      else
5593c8
+	{
5593c8
+	  initrd_prefix = grub_strrchr (clinux, '/');
5593c8
+	  initrd_prefix = grub_strndup(clinux, initrd_prefix - clinux + 1);
5593c8
+	}
5593c8
+
5593c8
+      if (!initrd_prefix)
5593c8
+	{
5593c8
+	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+	  goto finish;
5593c8
+	}
5593c8
+    }
5593c8
+
5593c8
+  if (early_initrds || initrds)
5593c8
+    {
5593c8
+      int initrd_size = sizeof ("initrd");
5593c8
+      char *tmp;
5593c8
+
5593c8
+      for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++)
5593c8
+	initrd_size += sizeof (" " GRUB_BOOT_DEVICE) \
5593c8
+		       + grub_strlen(initrd_prefix)  \
5593c8
+		       + grub_strlen (early_initrds[i]) + 1;
5593c8
+
5593c8
+      for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
5593c8
+	initrd_size += sizeof (" " GRUB_BOOT_DEVICE) \
5593c8
+		       + grub_strlen (initrds[i]) + 1;
5593c8
+      initrd_size += 1;
5593c8
+
5593c8
+      initrd = grub_malloc (initrd_size);
5593c8
+      if (!initrd)
5593c8
+	{
5593c8
+	  grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+	  goto finish;
5593c8
+	}
5593c8
+
5593c8
+      tmp = grub_stpcpy(initrd, "initrd");
5593c8
+      for (i = 0; early_initrds != NULL && early_initrds[i] != NULL; i++)
5593c8
+	{
5593c8
+	  grub_dprintf ("blscfg", "adding early initrd %s\n", early_initrds[i]);
5593c8
+	  tmp = grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
5593c8
+	  tmp = grub_stpcpy (tmp, initrd_prefix);
5593c8
+	  tmp = grub_stpcpy (tmp, early_initrds[i]);
5593c8
+	  grub_free(early_initrds[i]);
5593c8
+	}
5593c8
+
5593c8
+      for (i = 0; initrds != NULL && initrds[i] != NULL; i++)
5593c8
+	{
5593c8
+	  grub_dprintf ("blscfg", "adding initrd %s\n", initrds[i]);
5593c8
+	  tmp = grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
5593c8
+	  tmp = grub_stpcpy (tmp, initrds[i]);
5593c8
+	}
5593c8
+      tmp = grub_stpcpy (tmp, "\n");
5593c8
+    }
5593c8
+
5593c8
+  if (devicetree)
5593c8
+    {
5593c8
+      char *prefix = NULL;
5593c8
+      int dt_size;
5593c8
+
5593c8
+      if (add_dt_prefix)
5593c8
+	{
5593c8
+	  prefix = grub_strrchr (clinux, '/');
5593c8
+	  prefix = grub_strndup(clinux, prefix - clinux + 1);
5593c8
+	  if (!prefix)
5593c8
+	    {
5593c8
+	      grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+	      goto finish;
5593c8
+	    }
5593c8
+	}
5593c8
+
5593c8
+      dt_size = sizeof("devicetree " GRUB_BOOT_DEVICE) + grub_strlen(devicetree) + 1;
5593c8
+
5593c8
+      if (add_dt_prefix)
5593c8
+	{
5593c8
+	  dt_size += grub_strlen(prefix);
5593c8
+	}
5593c8
+
5593c8
+      dt = grub_malloc (dt_size);
5593c8
+      if (!dt)
5593c8
+        {
5593c8
+          grub_error (GRUB_ERR_OUT_OF_MEMORY, N_("out of memory"));
5593c8
+        goto finish;
5593c8
+        }
5593c8
+      char *tmp = dt;
5593c8
+      tmp = grub_stpcpy (dt, "devicetree");
5593c8
+      tmp = grub_stpcpy (tmp, " " GRUB_BOOT_DEVICE);
5593c8
+      if (add_dt_prefix)
5593c8
+        tmp = grub_stpcpy (tmp, prefix);
5593c8
+      tmp = grub_stpcpy (tmp, devicetree);
5593c8
+      tmp = grub_stpcpy (tmp, "\n");
5593c8
+
5593c8
+      grub_free(prefix);
5593c8
+    }
5593c8
+
5593c8
+  grub_dprintf ("blscfg2", "devicetree %s for id:\"%s\"\n", dt, id);
5593c8
+
5593c8
+  const char *sdval = grub_env_get("save_default");
5593c8
+  bool savedefault = ((NULL != sdval) && (grub_strcmp(sdval, "true") == 0));
5593c8
+  src = grub_xasprintf ("%sload_video\n"
5593c8
+			"set gfxpayload=keep\n"
5593c8
+			"insmod gzio\n"
5593c8
+			"linux %s%s%s%s\n"
5593c8
+			"%s%s",
5593c8
+			savedefault ? "savedefault\n" : "",
5593c8
+			GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options : "",
5593c8
+			initrd ? initrd : "", dt ? dt : "");
5593c8
+
5593c8
+  grub_normal_add_menu_entry (argc, argv, classes, id, users, hotkey, NULL, src, 0, &index, entry);
5593c8
+  grub_dprintf ("blscfg", "Added entry %d id:\"%s\"\n", index, id);
5593c8
+
5593c8
+finish:
5593c8
+  grub_free (dt);
5593c8
+  grub_free (initrd);
5593c8
+  grub_free (initrd_prefix);
5593c8
+  grub_free (early_initrds);
5593c8
+  grub_free (devicetree);
5593c8
+  grub_free (initrds);
5593c8
+  grub_free (options);
5593c8
+  grub_free (classes);
5593c8
+  grub_free (args);
5593c8
+  grub_free (argv);
5593c8
+  grub_free (src);
5593c8
+}
5593c8
+
5593c8
+struct find_entry_info {
5593c8
+	const char *dirname;
5593c8
+	const char *devid;
5593c8
+	grub_device_t dev;
5593c8
+	grub_fs_t fs;
5593c8
+};
5593c8
+
5593c8
+/*
5593c8
+ * info: the filesystem object the file is on.
5593c8
+ */
5593c8
+static int find_entry (struct find_entry_info *info)
5593c8
+{
5593c8
+  struct read_entry_info read_entry_info;
5593c8
+  grub_fs_t blsdir_fs = NULL;
5593c8
+  grub_device_t blsdir_dev = NULL;
5593c8
+  const char *blsdir = info->dirname;
5593c8
+  int fallback = 0;
5593c8
+  int r = 0;
5593c8
+
5593c8
+  if (!blsdir) {
5593c8
+    blsdir = grub_env_get ("blsdir");
5593c8
+    if (!blsdir)
5593c8
+      blsdir = GRUB_BLS_CONFIG_PATH;
5593c8
+  }
5593c8
+
5593c8
+  read_entry_info.file = NULL;
5593c8
+  read_entry_info.dirname = blsdir;
5593c8
+
5593c8
+  grub_dprintf ("blscfg", "scanning blsdir: %s\n", blsdir);
5593c8
+
5593c8
+  blsdir_dev = info->dev;
5593c8
+  blsdir_fs = info->fs;
5593c8
+  read_entry_info.devid = info->devid;
5593c8
+
5593c8
+read_fallback:
5593c8
+  r = blsdir_fs->fs_dir (blsdir_dev, read_entry_info.dirname, read_entry,
5593c8
+			 &read_entry_info);
5593c8
+  if (r != 0) {
5593c8
+      grub_dprintf ("blscfg", "read_entry returned error\n");
5593c8
+      grub_err_t e;
5593c8
+      do
5593c8
+	{
5593c8
+	  e = grub_error_pop();
5593c8
+	} while (e);
5593c8
+  }
5593c8
+
5593c8
+  if (r && !info->dirname && !fallback) {
5593c8
+    read_entry_info.dirname = "/boot" GRUB_BLS_CONFIG_PATH;
5593c8
+    grub_dprintf ("blscfg", "Entries weren't found in %s, fallback to %s\n",
5593c8
+		  blsdir, read_entry_info.dirname);
5593c8
+    fallback = 1;
5593c8
+    goto read_fallback;
5593c8
+  }
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+bls_load_entries (const char *path)
5593c8
+{
5593c8
+  grub_size_t len;
5593c8
+  grub_fs_t fs;
5593c8
+  grub_device_t dev;
5593c8
+  static grub_err_t r;
5593c8
+  const char *devid = NULL;
5593c8
+  char *blsdir = NULL;
5593c8
+  struct find_entry_info info = {
5593c8
+      .dev = NULL,
5593c8
+      .fs = NULL,
5593c8
+      .dirname = NULL,
5593c8
+  };
5593c8
+  struct read_entry_info rei = {
5593c8
+      .devid = NULL,
5593c8
+      .dirname = NULL,
5593c8
+  };
5593c8
+
5593c8
+  if (path) {
5593c8
+    len = grub_strlen (path);
5593c8
+    if (grub_strcmp (path + len - 5, ".conf") == 0) {
5593c8
+      rei.file = grub_file_open (path, GRUB_FILE_TYPE_CONFIG);
5593c8
+      if (!rei.file)
5593c8
+	return grub_errno;
5593c8
+      /*
5593c8
+       * read_entry() closes the file
5593c8
+       */
5593c8
+      return read_entry(path, NULL, &rei;;
5593c8
+    } else if (path[0] == '(') {
5593c8
+      devid = path + 1;
5593c8
+
5593c8
+      blsdir = grub_strchr (path, ')');
5593c8
+      if (!blsdir)
5593c8
+	return grub_error (GRUB_ERR_BAD_ARGUMENT, N_("Filepath isn't correct"));
5593c8
+
5593c8
+      *blsdir = '\0';
5593c8
+      blsdir = blsdir + 1;
5593c8
+    }
5593c8
+  }
5593c8
+
5593c8
+  if (!devid) {
5593c8
+#ifdef GRUB_MACHINE_EMU
5593c8
+    devid = "host";
5593c8
+#else
5593c8
+    devid = grub_env_get ("root");
5593c8
+#endif
5593c8
+    if (!devid)
5593c8
+      return grub_error (GRUB_ERR_FILE_NOT_FOUND,
5593c8
+			 N_("variable `%s' isn't set"), "root");
5593c8
+  }
5593c8
+
5593c8
+  grub_dprintf ("blscfg", "opening %s\n", devid);
5593c8
+  dev = grub_device_open (devid);
5593c8
+  if (!dev)
5593c8
+    return grub_errno;
5593c8
+
5593c8
+  grub_dprintf ("blscfg", "probing fs\n");
5593c8
+  fs = grub_fs_probe (dev);
5593c8
+  if (!fs)
5593c8
+    {
5593c8
+      r = grub_errno;
5593c8
+      goto finish;
5593c8
+    }
5593c8
+
5593c8
+  info.dirname = blsdir;
5593c8
+  info.devid = devid;
5593c8
+  info.dev = dev;
5593c8
+  info.fs = fs;
5593c8
+  find_entry(&info;;
5593c8
+
5593c8
+finish:
5593c8
+  if (dev)
5593c8
+    grub_device_close (dev);
5593c8
+
5593c8
+  return r;
5593c8
+}
5593c8
+
5593c8
+static bool
5593c8
+is_default_entry(const char *def_entry, struct bls_entry *entry, int idx)
5593c8
+{
5593c8
+  const char *title;
5593c8
+  int def_idx;
5593c8
+
5593c8
+  if (!def_entry)
5593c8
+    return false;
5593c8
+
5593c8
+  if (grub_strcmp(def_entry, entry->filename) == 0)
5593c8
+    return true;
5593c8
+
5593c8
+  title = bls_get_val(entry, "title", NULL);
5593c8
+
5593c8
+  if (title && grub_strcmp(def_entry, title) == 0)
5593c8
+    return true;
5593c8
+
5593c8
+  def_idx = (int)grub_strtol(def_entry, NULL, 0);
5593c8
+  if (grub_errno == GRUB_ERR_BAD_NUMBER) {
5593c8
+    grub_errno = GRUB_ERR_NONE;
5593c8
+    return false;
5593c8
+  }
5593c8
+
5593c8
+  if (def_idx == idx)
5593c8
+    return true;
5593c8
+
5593c8
+  return false;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+bls_create_entries (bool show_default, bool show_non_default, char *entry_id)
5593c8
+{
5593c8
+  const char *def_entry = NULL;
5593c8
+  struct bls_entry *entry = NULL;
5593c8
+  int idx = 0;
5593c8
+
5593c8
+  def_entry = grub_env_get("default");
5593c8
+
5593c8
+  grub_dprintf ("blscfg", "%s Creating entries from bls\n", __func__);
5593c8
+  FOR_BLS_ENTRIES(entry) {
5593c8
+    if (entry->visible) {
5593c8
+      idx++;
5593c8
+      continue;
5593c8
+    }
5593c8
+
5593c8
+    if ((show_default && is_default_entry(def_entry, entry, idx)) ||
5593c8
+	(show_non_default && !is_default_entry(def_entry, entry, idx)) ||
5593c8
+	(entry_id && grub_strcmp(entry_id, entry->filename) == 0)) {
5593c8
+      create_entry(entry);
5593c8
+      entry->visible = 1;
5593c8
+    }
5593c8
+    idx++;
5593c8
+  }
5593c8
+
5593c8
+  return GRUB_ERR_NONE;
5593c8
+}
5593c8
+
5593c8
+static grub_err_t
5593c8
+grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
5593c8
+		 int argc, char **args)
5593c8
+{
5593c8
+  grub_err_t r;
5593c8
+  char *path = NULL;
5593c8
+  char *entry_id = NULL;
5593c8
+  bool show_default = true;
5593c8
+  bool show_non_default = true;
5593c8
+
5593c8
+  if (argc == 1) {
5593c8
+    if (grub_strcmp (args[0], "default") == 0) {
5593c8
+      show_non_default = false;
5593c8
+    } else if (grub_strcmp (args[0], "non-default") == 0) {
5593c8
+      show_default = false;
5593c8
+    } else if (args[0][0] == '(') {
5593c8
+      path = args[0];
5593c8
+    } else {
5593c8
+      entry_id = args[0];
5593c8
+      show_default = false;
5593c8
+      show_non_default = false;
5593c8
+    }
5593c8
+  }
5593c8
+
5593c8
+  r = bls_load_entries(path);
5593c8
+  if (r)
5593c8
+    return r;
5593c8
+
5593c8
+  return bls_create_entries(show_default, show_non_default, entry_id);
5593c8
+}
5593c8
+
5593c8
+static grub_extcmd_t cmd;
5593c8
+static grub_extcmd_t oldcmd;
5593c8
+
5593c8
+GRUB_MOD_INIT(blscfg)
5593c8
+{
5593c8
+  grub_dprintf("blscfg", "%s got here\n", __func__);
5593c8
+  cmd = grub_register_extcmd ("blscfg",
5593c8
+			      grub_cmd_blscfg,
5593c8
+			      0,
5593c8
+			      NULL,
5593c8
+			      N_("Import Boot Loader Specification snippets."),
5593c8
+			      NULL);
5593c8
+  oldcmd = grub_register_extcmd ("bls_import",
5593c8
+				 grub_cmd_blscfg,
5593c8
+				 0,
5593c8
+				 NULL,
5593c8
+				 N_("Import Boot Loader Specification snippets."),
5593c8
+				 NULL);
5593c8
+}
5593c8
+
5593c8
+GRUB_MOD_FINI(blscfg)
5593c8
+{
5593c8
+  grub_unregister_extcmd (cmd);
5593c8
+  grub_unregister_extcmd (oldcmd);
5593c8
+}
5593c8
diff --git a/grub-core/commands/legacycfg.c b/grub-core/commands/legacycfg.c
5593c8
index cc5971f4dbd..782761c31aa 100644
5593c8
--- a/grub-core/commands/legacycfg.c
5593c8
+++ b/grub-core/commands/legacycfg.c
5593c8
@@ -143,7 +143,7 @@ legacy_file (const char *filename)
5593c8
 	    args[0] = oldname;
5593c8
 	    grub_normal_add_menu_entry (1, args, NULL, NULL, "legacy",
5593c8
 					NULL, NULL,
5593c8
-					entrysrc, 0);
5593c8
+					entrysrc, 0, NULL, NULL);
5593c8
 	    grub_free (args);
5593c8
 	    entrysrc[0] = 0;
5593c8
 	    grub_free (oldname);
5593c8
@@ -205,7 +205,8 @@ legacy_file (const char *filename)
5593c8
 	}
5593c8
       args[0] = entryname;
5593c8
       grub_normal_add_menu_entry (1, args, NULL, NULL, NULL,
5593c8
-				  NULL, NULL, entrysrc, 0);
5593c8
+				  NULL, NULL, entrysrc, 0, NULL,
5593c8
+				  NULL);
5593c8
       grub_free (args);
5593c8
     }
5593c8
 
5593c8
diff --git a/grub-core/commands/loadenv.c b/grub-core/commands/loadenv.c
5593c8
index 3fd664aac33..163b9a09042 100644
5593c8
--- a/grub-core/commands/loadenv.c
5593c8
+++ b/grub-core/commands/loadenv.c
5593c8
@@ -28,6 +28,8 @@
5593c8
 #include <grub/extcmd.h>
5593c8
 #include <grub/i18n.h>
5593c8
 
5593c8
+#include "loadenv.h"
5593c8
+
5593c8
 GRUB_MOD_LICENSE ("GPLv3+");
5593c8
 
5593c8
 static const struct grub_arg_option options[] =
5593c8
@@ -79,81 +81,6 @@ open_envblk_file (char *filename,
5593c8
   return file;
5593c8
 }
5593c8
 
5593c8
-static grub_envblk_t
5593c8
-read_envblk_file (grub_file_t file)
5593c8
-{
5593c8
-  grub_off_t offset = 0;
5593c8
-  char *buf;
5593c8
-  grub_size_t size = grub_file_size (file);
5593c8
-  grub_envblk_t envblk;
5593c8
-
5593c8
-  buf = grub_malloc (size);
5593c8
-  if (! buf)
5593c8
-    return 0;
5593c8
-
5593c8
-  while (size > 0)
5593c8
-    {
5593c8
-      grub_ssize_t ret;
5593c8
-
5593c8
-      ret = grub_file_read (file, buf + offset, size);
5593c8
-      if (ret <= 0)
5593c8
-        {
5593c8
-          grub_free (buf);
5593c8
-          return 0;
5593c8
-        }
5593c8
-
5593c8
-      size -= ret;
5593c8
-      offset += ret;
5593c8
-    }
5593c8
-
5593c8
-  envblk = grub_envblk_open (buf, offset);
5593c8
-  if (! envblk)
5593c8
-    {
5593c8
-      grub_free (buf);
5593c8
-      grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
5593c8
-      return 0;
5593c8
-    }
5593c8
-
5593c8
-  return envblk;
5593c8
-}
5593c8
-
5593c8
-struct grub_env_whitelist
5593c8
-{
5593c8
-  grub_size_t len;
5593c8
-  char **list;
5593c8
-};
5593c8
-typedef struct grub_env_whitelist grub_env_whitelist_t;
5593c8
-
5593c8
-static int
5593c8
-test_whitelist_membership (const char* name,
5593c8
-                           const grub_env_whitelist_t* whitelist)
5593c8
-{
5593c8
-  grub_size_t i;
5593c8
-
5593c8
-  for (i = 0; i < whitelist->len; i++)
5593c8
-    if (grub_strcmp (name, whitelist->list[i]) == 0)
5593c8
-      return 1;  /* found it */
5593c8
-
5593c8
-  return 0;  /* not found */
5593c8
-}
5593c8
-
5593c8
-/* Helper for grub_cmd_load_env.  */
5593c8
-static int
5593c8
-set_var (const char *name, const char *value, void *whitelist)
5593c8
-{
5593c8
-  if (! whitelist)
5593c8
-    {
5593c8
-      grub_env_set (name, value);
5593c8
-      return 0;
5593c8
-    }
5593c8
-
5593c8
-  if (test_whitelist_membership (name,
5593c8
-				 (const grub_env_whitelist_t *) whitelist))
5593c8
-    grub_env_set (name, value);
5593c8
-
5593c8
-  return 0;
5593c8
-}
5593c8
-
5593c8
 static grub_err_t
5593c8
 grub_cmd_load_env (grub_extcmd_context_t ctxt, int argc, char **args)
5593c8
 {
5593c8
diff --git a/grub-core/commands/menuentry.c b/grub-core/commands/menuentry.c
5593c8
index 720e6d8ea3b..b194123eb67 100644
5593c8
--- a/grub-core/commands/menuentry.c
5593c8
+++ b/grub-core/commands/menuentry.c
5593c8
@@ -78,7 +78,7 @@ grub_normal_add_menu_entry (int argc, const char **args,
5593c8
 			    char **classes, const char *id,
5593c8
 			    const char *users, const char *hotkey,
5593c8
 			    const char *prefix, const char *sourcecode,
5593c8
-			    int submenu)
5593c8
+			    int submenu, int *index, struct bls_entry *bls)
5593c8
 {
5593c8
   int menu_hotkey = 0;
5593c8
   char **menu_args = NULL;
5593c8
@@ -149,9 +149,12 @@ grub_normal_add_menu_entry (int argc, const char **args,
5593c8
   if (! menu_title)
5593c8
     goto fail;
5593c8
 
5593c8
+  grub_dprintf ("menu", "id:\"%s\"\n", id);
5593c8
+  grub_dprintf ("menu", "title:\"%s\"\n", menu_title);
5593c8
   menu_id = grub_strdup (id ? : menu_title);
5593c8
   if (! menu_id)
5593c8
     goto fail;
5593c8
+  grub_dprintf ("menu", "menu_id:\"%s\"\n", menu_id);
5593c8
 
5593c8
   /* Save argc, args to pass as parameters to block arg later. */
5593c8
   menu_args = grub_calloc (argc + 1, sizeof (char *));
5593c8
@@ -170,8 +173,12 @@ grub_normal_add_menu_entry (int argc, const char **args,
5593c8
   }
5593c8
 
5593c8
   /* Add the menu entry at the end of the list.  */
5593c8
+  int ind=0;
5593c8
   while (*last)
5593c8
-    last = &(*last)->next;
5593c8
+    {
5593c8
+      ind++;
5593c8
+      last = &(*last)->next;
5593c8
+    }
5593c8
 
5593c8
   *last = grub_zalloc (sizeof (**last));
5593c8
   if (! *last)
5593c8
@@ -188,8 +195,11 @@ grub_normal_add_menu_entry (int argc, const char **args,
5593c8
   (*last)->args = menu_args;
5593c8
   (*last)->sourcecode = menu_sourcecode;
5593c8
   (*last)->submenu = submenu;
5593c8
+  (*last)->bls = bls;
5593c8
 
5593c8
   menu->size++;
5593c8
+  if (index)
5593c8
+    *index = ind;
5593c8
   return GRUB_ERR_NONE;
5593c8
 
5593c8
  fail:
5593c8
@@ -286,7 +296,8 @@ grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
5593c8
 				       users,
5593c8
 				       ctxt->state[2].arg, 0,
5593c8
 				       ctxt->state[3].arg,
5593c8
-				       ctxt->extcmd->cmd->name[0] == 's');
5593c8
+				       ctxt->extcmd->cmd->name[0] == 's',
5593c8
+				       NULL, NULL);
5593c8
 
5593c8
   src = args[argc - 1];
5593c8
   args[argc - 1] = NULL;
5593c8
@@ -303,7 +314,8 @@ grub_cmd_menuentry (grub_extcmd_context_t ctxt, int argc, char **args)
5593c8
 				  ctxt->state[0].args, ctxt->state[4].arg,
5593c8
 				  users,
5593c8
 				  ctxt->state[2].arg, prefix, src + 1,
5593c8
-				  ctxt->extcmd->cmd->name[0] == 's');
5593c8
+				  ctxt->extcmd->cmd->name[0] == 's', NULL,
5593c8
+				  NULL);
5593c8
 
5593c8
   src[len - 1] = ch;
5593c8
   args[argc - 1] = src;
5593c8
diff --git a/grub-core/normal/main.c b/grub-core/normal/main.c
5593c8
index 62571e6dfcc..7ca2e5400b1 100644
5593c8
--- a/grub-core/normal/main.c
5593c8
+++ b/grub-core/normal/main.c
5593c8
@@ -21,6 +21,7 @@
5593c8
 #include <grub/net.h>
5593c8
 #include <grub/normal.h>
5593c8
 #include <grub/dl.h>
5593c8
+#include <grub/menu.h>
5593c8
 #include <grub/misc.h>
5593c8
 #include <grub/file.h>
5593c8
 #include <grub/mm.h>
5593c8
@@ -70,6 +71,11 @@ grub_normal_free_menu (grub_menu_t menu)
5593c8
 	  grub_free (entry->args);
5593c8
 	}
5593c8
 
5593c8
+      if (entry->bls)
5593c8
+	{
5593c8
+	  entry->bls->visible = 0;
5593c8
+	}
5593c8
+
5593c8
       grub_free ((void *) entry->id);
5593c8
       grub_free ((void *) entry->users);
5593c8
       grub_free ((void *) entry->title);
5593c8
diff --git a/grub-core/commands/loadenv.h b/grub-core/commands/loadenv.h
5593c8
new file mode 100644
5593c8
index 00000000000..952f46121bd
5593c8
--- /dev/null
5593c8
+++ b/grub-core/commands/loadenv.h
5593c8
@@ -0,0 +1,93 @@
5593c8
+/* loadenv.c - command to load/save environment variable.  */
5593c8
+/*
5593c8
+ *  GRUB  --  GRand Unified Bootloader
5593c8
+ *  Copyright (C) 2008,2009,2010  Free Software Foundation, Inc.
5593c8
+ *
5593c8
+ *  GRUB is free software: you can redistribute it and/or modify
5593c8
+ *  it under the terms of the GNU General Public License as published by
5593c8
+ *  the Free Software Foundation, either version 3 of the License, or
5593c8
+ *  (at your option) any later version.
5593c8
+ *
5593c8
+ *  GRUB is distributed in the hope that it will be useful,
5593c8
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
5593c8
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
5593c8
+ *  GNU General Public License for more details.
5593c8
+ *
5593c8
+ *  You should have received a copy of the GNU General Public License
5593c8
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
5593c8
+ */
5593c8
+
5593c8
+static grub_envblk_t UNUSED
5593c8
+read_envblk_file (grub_file_t file)
5593c8
+{
5593c8
+  grub_off_t offset = 0;
5593c8
+  char *buf;
5593c8
+  grub_size_t size = grub_file_size (file);
5593c8
+  grub_envblk_t envblk;
5593c8
+
5593c8
+  buf = grub_malloc (size);
5593c8
+  if (! buf)
5593c8
+    return 0;
5593c8
+
5593c8
+  while (size > 0)
5593c8
+    {
5593c8
+      grub_ssize_t ret;
5593c8
+
5593c8
+      ret = grub_file_read (file, buf + offset, size);
5593c8
+      if (ret <= 0)
5593c8
+        {
5593c8
+          grub_free (buf);
5593c8
+          return 0;
5593c8
+        }
5593c8
+
5593c8
+      size -= ret;
5593c8
+      offset += ret;
5593c8
+    }
5593c8
+
5593c8
+  envblk = grub_envblk_open (buf, offset);
5593c8
+  if (! envblk)
5593c8
+    {
5593c8
+      grub_free (buf);
5593c8
+      grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
5593c8
+      return 0;
5593c8
+    }
5593c8
+
5593c8
+  return envblk;
5593c8
+}
5593c8
+
5593c8
+struct grub_env_whitelist
5593c8
+{
5593c8
+  grub_size_t len;
5593c8
+  char **list;
5593c8
+};
5593c8
+typedef struct grub_env_whitelist grub_env_whitelist_t;
5593c8
+
5593c8
+static int UNUSED
5593c8
+test_whitelist_membership (const char* name,
5593c8
+                           const grub_env_whitelist_t* whitelist)
5593c8
+{
5593c8
+  grub_size_t i;
5593c8
+
5593c8
+  for (i = 0; i < whitelist->len; i++)
5593c8
+    if (grub_strcmp (name, whitelist->list[i]) == 0)
5593c8
+      return 1;  /* found it */
5593c8
+
5593c8
+  return 0;  /* not found */
5593c8
+}
5593c8
+
5593c8
+/* Helper for grub_cmd_load_env.  */
5593c8
+static int UNUSED
5593c8
+set_var (const char *name, const char *value, void *whitelist)
5593c8
+{
5593c8
+  if (! whitelist)
5593c8
+    {
5593c8
+      grub_env_set (name, value);
5593c8
+      return 0;
5593c8
+    }
5593c8
+
5593c8
+  if (test_whitelist_membership (name,
5593c8
+				 (const grub_env_whitelist_t *) whitelist))
5593c8
+    grub_env_set (name, value);
5593c8
+
5593c8
+  return 0;
5593c8
+}
5593c8
diff --git a/include/grub/compiler.h b/include/grub/compiler.h
5593c8
index 8f3be3ae706..ebafec68957 100644
5593c8
--- a/include/grub/compiler.h
5593c8
+++ b/include/grub/compiler.h
5593c8
@@ -56,4 +56,6 @@
5593c8
 #  define CLANG_PREREQ(maj,min) 0
5593c8
 #endif
5593c8
 
5593c8
+#define UNUSED __attribute__((__unused__))
5593c8
+
5593c8
 #endif /* ! GRUB_COMPILER_HEADER */
5593c8
diff --git a/include/grub/menu.h b/include/grub/menu.h
5593c8
index ee2b5e91045..0acdc2aa6bf 100644
5593c8
--- a/include/grub/menu.h
5593c8
+++ b/include/grub/menu.h
5593c8
@@ -20,6 +20,16 @@
5593c8
 #ifndef GRUB_MENU_HEADER
5593c8
 #define GRUB_MENU_HEADER 1
5593c8
 
5593c8
+struct bls_entry
5593c8
+{
5593c8
+  struct bls_entry *next;
5593c8
+  struct bls_entry *prev;
5593c8
+  struct keyval **keyvals;
5593c8
+  int nkeyvals;
5593c8
+  char *filename;
5593c8
+  int visible;
5593c8
+};
5593c8
+
5593c8
 struct grub_menu_entry_class
5593c8
 {
5593c8
   char *name;
5593c8
@@ -60,6 +70,9 @@ struct grub_menu_entry
5593c8
 
5593c8
   /* The next element.  */
5593c8
   struct grub_menu_entry *next;
5593c8
+
5593c8
+  /* BLS used to populate the entry */
5593c8
+  struct bls_entry *bls;
5593c8
 };
5593c8
 typedef struct grub_menu_entry *grub_menu_entry_t;
5593c8
 
5593c8
diff --git a/include/grub/normal.h b/include/grub/normal.h
5593c8
index 218cbabccaf..8839ad85a19 100644
5593c8
--- a/include/grub/normal.h
5593c8
+++ b/include/grub/normal.h
5593c8
@@ -145,7 +145,7 @@ grub_normal_add_menu_entry (int argc, const char **args, char **classes,
5593c8
 			    const char *id,
5593c8
 			    const char *users, const char *hotkey,
5593c8
 			    const char *prefix, const char *sourcecode,
5593c8
-			    int submenu);
5593c8
+			    int submenu, int *index, struct bls_entry *bls);
5593c8
 
5593c8
 grub_err_t
5593c8
 grub_normal_set_password (const char *user, const char *password);