nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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