dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0111-Add-blscfg-command-support-to-parse-BootLoaderSpec-c.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Peter Jones <pjones@redhat.com>
8631a2
Date: Fri, 9 Dec 2016 15:40:58 -0500
8631a2
Subject: [PATCH] Add blscfg command support to parse BootLoaderSpec config
8631a2
 fragments
8631a2
8631a2
The BootLoaderSpec (BLS) defines a scheme where different bootloaders can
8631a2
share a format for boot items and a configuration directory that accepts
8631a2
these common configurations as drop-in files.
8631a2
8631a2
GRUB2 already has a blscfg modle that can parse the config snippets using
8631a2
the bls_import command, change it to blscfg and improve the BLS support.
8631a2
---
8631a2
 grub-core/Makefile.core.def  |   4 +-
8631a2
 grub-core/commands/blscfg.c  | 796 ++++++++++++++++++++++++++++++++++++++++---
8631a2
 grub-core/commands/loadenv.c |  77 +----
8631a2
 grub-core/commands/loadenv.h |  93 +++++
8631a2
 include/grub/compiler.h      |   2 +
8631a2
 5 files changed, 840 insertions(+), 132 deletions(-)
8631a2
 create mode 100644 grub-core/commands/loadenv.h
8631a2
8631a2
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
8631a2
index 27563743ba9..96ccb402125 100644
8631a2
--- a/grub-core/Makefile.core.def
8631a2
+++ b/grub-core/Makefile.core.def
8631a2
@@ -129,6 +129,7 @@ kernel = {
8631a2
   common = kern/rescue_parser.c;
8631a2
   common = kern/rescue_reader.c;
8631a2
   common = kern/term.c;
8631a2
+  common = kern/qsort.c;
8631a2
 
8631a2
   noemu = kern/compiler-rt.c;
8631a2
   noemu = kern/mm.c;
8631a2
@@ -774,8 +775,7 @@ module = {
8631a2
 module = {
8631a2
   name = blscfg;
8631a2
   common = commands/blscfg.c;
8631a2
-  enable = i386_efi;
8631a2
-  enable = x86_64_efi;
8631a2
+  enable = efi;
8631a2
   enable = i386_pc;
8631a2
 };
8631a2
 
8631a2
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
8631a2
index 4274aca5a9d..86796c8cd83 100644
8631a2
--- a/grub-core/commands/blscfg.c
8631a2
+++ b/grub-core/commands/blscfg.c
8631a2
@@ -30,32 +30,405 @@
8631a2
 #include <grub/env.h>
8631a2
 #include <grub/file.h>
8631a2
 #include <grub/normal.h>
8631a2
+#include <grub/lib/envblk.h>
8631a2
 
8631a2
 GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 
8631a2
+#include "loadenv.h"
8631a2
+
8631a2
+#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
8631a2
+#define GRUB_BOOT_DEVICE "($root)"
8631a2
 #ifdef GRUB_MACHINE_EFI
8631a2
 #define GRUB_LINUX_CMD "linuxefi"
8631a2
 #define GRUB_INITRD_CMD "initrdefi"
8631a2
-#define GRUB_BLS_CONFIG_PATH "/EFI/fedora/loader/entries/"
8631a2
-#define GRUB_BOOT_DEVICE "($boot)"
8631a2
 #else
8631a2
 #define GRUB_LINUX_CMD "linux"
8631a2
 #define GRUB_INITRD_CMD "initrd"
8631a2
-#define GRUB_BLS_CONFIG_PATH "/loader/entries/"
8631a2
-#define GRUB_BOOT_DEVICE "($root)"
8631a2
 #endif
8631a2
 
8631a2
-static int parse_entry (
8631a2
+#define grub_free(x) ({grub_dprintf("blscfg", "%s freeing %p\n", __func__, x); grub_free(x); })
8631a2
+
8631a2
+struct keyval
8631a2
+{
8631a2
+  const char *key;
8631a2
+  char *val;
8631a2
+};
8631a2
+
8631a2
+struct bls_entry
8631a2
+{
8631a2
+  struct keyval **keyvals;
8631a2
+  int nkeyvals;
8631a2
+};
8631a2
+
8631a2
+static struct bls_entry **entries;
8631a2
+static int nentries;
8631a2
+
8631a2
+static struct bls_entry *bls_new_entry(void)
8631a2
+{
8631a2
+  struct bls_entry **new_entries;
8631a2
+  struct bls_entry *entry;
8631a2
+  int new_n = nentries + 1;
8631a2
+
8631a2
+  new_entries = grub_realloc (entries,  new_n * sizeof (struct bls_entry *));
8631a2
+  if (!new_entries)
8631a2
+    {
8631a2
+      grub_error (GRUB_ERR_OUT_OF_MEMORY,
8631a2
+		  "couldn't find space for BLS entry list");
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  entries = new_entries;
8631a2
+
8631a2
+  entry = grub_malloc (sizeof (*entry));
8631a2
+  if (!entry)
8631a2
+    {
8631a2
+      grub_error (GRUB_ERR_OUT_OF_MEMORY,
8631a2
+		  "couldn't find space for BLS entry list");
8631a2
+      return NULL;
8631a2
+    }
8631a2
+
8631a2
+  grub_memset (entry, 0, sizeof (*entry));
8631a2
+  entries[nentries] = entry;
8631a2
+
8631a2
+  nentries = new_n;
8631a2
+
8631a2
+  return entry;
8631a2
+}
8631a2
+
8631a2
+static int bls_add_keyval(struct bls_entry *entry, char *key, char *val)
8631a2
+{
8631a2
+  char *k, *v;
8631a2
+  struct keyval **kvs, *kv;
8631a2
+  int new_n = entry->nkeyvals + 1;
8631a2
+
8631a2
+  kvs = grub_realloc (entry->keyvals, new_n * sizeof (struct keyval *));
8631a2
+  if (!kvs)
8631a2
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
8631a2
+		       "couldn't find space for BLS entry");
8631a2
+  entry->keyvals = kvs;
8631a2
+
8631a2
+  kv = grub_malloc (sizeof (struct keyval));
8631a2
+  if (!kv)
8631a2
+    return grub_error (GRUB_ERR_OUT_OF_MEMORY,
8631a2
+		       "couldn't find space for BLS entry");
8631a2
+
8631a2
+  k = grub_strdup (key);
8631a2
+  if (!k)
8631a2
+    {
8631a2
+      grub_free (kv);
8631a2
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
8631a2
+			 "couldn't find space for BLS entry");
8631a2
+    }
8631a2
+
8631a2
+  v = grub_strdup (val);
8631a2
+  if (!v)
8631a2
+    {
8631a2
+      grub_free (k);
8631a2
+      grub_free (kv);
8631a2
+      return grub_error (GRUB_ERR_OUT_OF_MEMORY,
8631a2
+			 "couldn't find space for BLS entry");
8631a2
+    }
8631a2
+
8631a2
+  kv->key = k;
8631a2
+  kv->val = v;
8631a2
+
8631a2
+  entry->keyvals[entry->nkeyvals] = kv;
8631a2
+  grub_dprintf("blscfg", "new keyval at %p:%p:%p\n", entry->keyvals[entry->nkeyvals], k, v);
8631a2
+  entry->nkeyvals = new_n;
8631a2
+
8631a2
+  return 0;
8631a2
+}
8631a2
+
8631a2
+static void bls_free_entry(struct bls_entry *entry)
8631a2
+{
8631a2
+  int i;
8631a2
+
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+  for (i = 0; i < entry->nkeyvals; i++)
8631a2
+    {
8631a2
+      struct keyval *kv = entry->keyvals[i];
8631a2
+      grub_free ((void *)kv->key);
8631a2
+      grub_free (kv->val);
8631a2
+      grub_free (kv);
8631a2
+    }
8631a2
+
8631a2
+  grub_free (entry->keyvals);
8631a2
+  grub_memset (entry, 0, sizeof (*entry));
8631a2
+  grub_free (entry);
8631a2
+}
8631a2
+
8631a2
+static int keyval_cmp (const void *p0, const void *p1,
8631a2
+		       void *state UNUSED)
8631a2
+{
8631a2
+  const struct keyval *kv0 = *(struct keyval * const *)p0;
8631a2
+  const struct keyval *kv1 = *(struct keyval * const *)p1;
8631a2
+  int rc;
8631a2
+
8631a2
+  rc = grub_strcmp(kv0->key, kv1->key);
8631a2
+
8631a2
+  return rc;
8631a2
+}
8631a2
+
8631a2
+/* Find they value of the key named by keyname.  If there are allowed to be
8631a2
+ * more than one, pass a pointer to an int set to -1 the first time, and pass
8631a2
+ * the same pointer through each time after, and it'll return them in sorted
8631a2
+ * order. */
8631a2
+static char *bls_get_val(struct bls_entry *entry, const char *keyname, int *last)
8631a2
+{
8631a2
+  char *foo = (char *)"";
8631a2
+  struct keyval *kv = NULL, **kvp, key = {keyname, foo}, *keyp = &ke;;
8631a2
+
8631a2
+  /* if we've already found an entry that matches, just iterate */
8631a2
+  if (last && *last >= 0)
8631a2
+    {
8631a2
+      int next = ++last[0];
8631a2
+
8631a2
+      if (next == entry->nkeyvals)
8631a2
+	{
8631a2
+done:
8631a2
+	  *last = -1;
8631a2
+	  return NULL;
8631a2
+	}
8631a2
+
8631a2
+      kv = entry->keyvals[next];
8631a2
+      if (grub_strcmp (keyname, kv->key))
8631a2
+	goto done;
8631a2
+
8631a2
+      return kv->val;
8631a2
+    }
8631a2
+
8631a2
+  kvp = grub_bsearch(&keyp, &entry->keyvals[0], entry->nkeyvals,
8631a2
+		    sizeof (struct keyval *), keyval_cmp, NULL);
8631a2
+  if (kvp)
8631a2
+    kv = *kvp;
8631a2
+
8631a2
+  if (kv)
8631a2
+    {
8631a2
+      /* if we've got uninitialized but present state, track back until we find
8631a2
+       * the first match */
8631a2
+      if (last)
8631a2
+	{
8631a2
+	  grub_dprintf("blscfg", "%s trying to find another entry because last was set\n", __func__);
8631a2
+	  /* figure out the position of this entry in the array */
8631a2
+	  int idx;
8631a2
+	  for (idx = 0 ; idx < entry->nkeyvals; idx++)
8631a2
+	    if (entry->keyvals[idx] == kv)
8631a2
+	      break;
8631a2
+	  *last = idx;
8631a2
+
8631a2
+	  while (idx > 0)
8631a2
+	    {
8631a2
+	      struct keyval *kvtmp = entry->keyvals[idx-1];
8631a2
+	      if (idx == 0 || grub_strcmp (keyname, kvtmp->key))
8631a2
+		{
8631a2
+		  /* if we're at the start, or if the previous entry doesn't
8631a2
+		   * match, then we're done */
8631a2
+		  *last = idx;
8631a2
+		  break;
8631a2
+		}
8631a2
+	      else
8631a2
+		/* but if it does match, keep going backwards */
8631a2
+		idx--;
8631a2
+	    }
8631a2
+	}
8631a2
+
8631a2
+      return kv->val;
8631a2
+    }
8631a2
+  return NULL;
8631a2
+}
8631a2
+
8631a2
+#define goto_return(x) ({ ret = (x); goto finish; })
8631a2
+
8631a2
+/* compare alpha and numeric segments of two versions */
8631a2
+/* return 1: a is newer than b */
8631a2
+/*        0: a and b are the same version */
8631a2
+/*       -1: b is newer than a */
8631a2
+static int vercmp(const char * a, const char * b)
8631a2
+{
8631a2
+    char oldch1, oldch2;
8631a2
+    char *abuf, *bbuf;
8631a2
+    char *str1, *str2;
8631a2
+    char * one, * two;
8631a2
+    int rc;
8631a2
+    int isnum;
8631a2
+    int ret = 0;
8631a2
+
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+    if (!grub_strcmp(a, b))
8631a2
+	    return 0;
8631a2
+
8631a2
+    abuf = grub_malloc(grub_strlen(a) + 1);
8631a2
+    bbuf = grub_malloc(grub_strlen(b) + 1);
8631a2
+    str1 = abuf;
8631a2
+    str2 = bbuf;
8631a2
+    grub_strcpy(str1, a);
8631a2
+    grub_strcpy(str2, b);
8631a2
+
8631a2
+    one = str1;
8631a2
+    two = str2;
8631a2
+
8631a2
+    /* loop through each version segment of str1 and str2 and compare them */
8631a2
+    while (*one || *two) {
8631a2
+	while (*one && !grub_isalnum(*one) && *one != '~') one++;
8631a2
+	while (*two && !grub_isalnum(*two) && *two != '~') two++;
8631a2
+
8631a2
+	/* handle the tilde separator, it sorts before everything else */
8631a2
+	if (*one == '~' || *two == '~') {
8631a2
+	    if (*one != '~') goto_return (1);
8631a2
+	    if (*two != '~') goto_return (-1);
8631a2
+	    one++;
8631a2
+	    two++;
8631a2
+	    continue;
8631a2
+	}
8631a2
+
8631a2
+	/* If we ran to the end of either, we are finished with the loop */
8631a2
+	if (!(*one && *two)) break;
8631a2
+
8631a2
+	str1 = one;
8631a2
+	str2 = two;
8631a2
+
8631a2
+	/* grab first completely alpha or completely numeric segment */
8631a2
+	/* leave one and two pointing to the start of the alpha or numeric */
8631a2
+	/* segment and walk str1 and str2 to end of segment */
8631a2
+	if (grub_isdigit(*str1)) {
8631a2
+	    while (*str1 && grub_isdigit(*str1)) str1++;
8631a2
+	    while (*str2 && grub_isdigit(*str2)) str2++;
8631a2
+	    isnum = 1;
8631a2
+	} else {
8631a2
+	    while (*str1 && grub_isalpha(*str1)) str1++;
8631a2
+	    while (*str2 && grub_isalpha(*str2)) str2++;
8631a2
+	    isnum = 0;
8631a2
+	}
8631a2
+
8631a2
+	/* save character at the end of the alpha or numeric segment */
8631a2
+	/* so that they can be restored after the comparison */
8631a2
+	oldch1 = *str1;
8631a2
+	*str1 = '\0';
8631a2
+	oldch2 = *str2;
8631a2
+	*str2 = '\0';
8631a2
+
8631a2
+	/* this cannot happen, as we previously tested to make sure that */
8631a2
+	/* the first string has a non-null segment */
8631a2
+	if (one == str1) goto_return(-1);	/* arbitrary */
8631a2
+
8631a2
+	/* take care of the case where the two version segments are */
8631a2
+	/* different types: one numeric, the other alpha (i.e. empty) */
8631a2
+	/* numeric segments are always newer than alpha segments */
8631a2
+	/* XXX See patch #60884 (and details) from bugzilla #50977. */
8631a2
+	if (two == str2) goto_return (isnum ? 1 : -1);
8631a2
+
8631a2
+	if (isnum) {
8631a2
+	    grub_size_t onelen, twolen;
8631a2
+	    /* this used to be done by converting the digit segments */
8631a2
+	    /* to ints using atoi() - it's changed because long  */
8631a2
+	    /* digit segments can overflow an int - this should fix that. */
8631a2
+
8631a2
+	    /* throw away any leading zeros - it's a number, right? */
8631a2
+	    while (*one == '0') one++;
8631a2
+	    while (*two == '0') two++;
8631a2
+
8631a2
+	    /* whichever number has more digits wins */
8631a2
+	    onelen = grub_strlen(one);
8631a2
+	    twolen = grub_strlen(two);
8631a2
+	    if (onelen > twolen) goto_return (1);
8631a2
+	    if (twolen > onelen) goto_return (-1);
8631a2
+	}
8631a2
+
8631a2
+	/* grub_strcmp will return which one is greater - even if the two */
8631a2
+	/* segments are alpha or if they are numeric.  don't return  */
8631a2
+	/* if they are equal because there might be more segments to */
8631a2
+	/* compare */
8631a2
+	rc = grub_strcmp(one, two);
8631a2
+	if (rc) goto_return (rc < 1 ? -1 : 1);
8631a2
+
8631a2
+	/* restore character that was replaced by null above */
8631a2
+	*str1 = oldch1;
8631a2
+	one = str1;
8631a2
+	*str2 = oldch2;
8631a2
+	two = str2;
8631a2
+    }
8631a2
+
8631a2
+    /* this catches the case where all numeric and alpha segments have */
8631a2
+    /* compared identically but the segment sepparating characters were */
8631a2
+    /* different */
8631a2
+    if ((!*one) && (!*two)) goto_return (0);
8631a2
+
8631a2
+    /* whichever version still has characters left over wins */
8631a2
+    if (!*one) goto_return (-1); else goto_return (1);
8631a2
+
8631a2
+finish:
8631a2
+    grub_free (abuf);
8631a2
+    grub_free (bbuf);
8631a2
+    return ret;
8631a2
+}
8631a2
+
8631a2
+typedef int (*void_cmp_t)(void *, void *);
8631a2
+
8631a2
+static int nulcmp(char *s0, char *s1, void_cmp_t cmp)
8631a2
+{
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+  if (s1 && !s0)
8631a2
+    return 1;
8631a2
+  if (s0 && !s1)
8631a2
+    return -1;
8631a2
+  if (!s0 && !s1)
8631a2
+    return 0;
8631a2
+  if (cmp)
8631a2
+    return cmp(s0, s1);
8631a2
+  return grub_strcmp(s0, s1);
8631a2
+}
8631a2
+
8631a2
+static int
8631a2
+bls_keyval_cmp(struct bls_entry *e0, struct bls_entry *e1, const char *keyname)
8631a2
+{
8631a2
+  char *val0, *val1;
8631a2
+
8631a2
+  val0 = bls_get_val (e0, keyname, NULL);
8631a2
+  val1 = bls_get_val (e1, keyname, NULL);
8631a2
+
8631a2
+  if (val1 && !val0)
8631a2
+    return 1;
8631a2
+
8631a2
+  if (val0 && !val1)
8631a2
+    return -1;
8631a2
+
8631a2
+  if (!val0 && !val1)
8631a2
+    return 0;
8631a2
+
8631a2
+  return nulcmp(val0, val1, (void_cmp_t)vercmp);
8631a2
+}
8631a2
+
8631a2
+static int bls_cmp(const void *p0, const void *p1, void *state UNUSED)
8631a2
+{
8631a2
+  struct bls_entry * e0 = *(struct bls_entry **)p0;
8631a2
+  struct bls_entry * e1 = *(struct bls_entry **)p1;
8631a2
+  int rc = 0;
8631a2
+
8631a2
+  rc = bls_keyval_cmp (e0, e1, "id");
8631a2
+
8631a2
+  if (rc == 0)
8631a2
+    rc = bls_keyval_cmp (e0, e1, "title");
8631a2
+
8631a2
+  if (rc == 0)
8631a2
+    rc = bls_keyval_cmp (e0, e1, "linux");
8631a2
+
8631a2
+  return rc;
8631a2
+}
8631a2
+
8631a2
+static int read_entry (
8631a2
     const char *filename,
8631a2
-    const struct grub_dirhook_info *info __attribute__ ((unused)),
8631a2
-    void *data __attribute__ ((unused)))
8631a2
+    const struct grub_dirhook_info *info UNUSED,
8631a2
+    void *data)
8631a2
 {
8631a2
   grub_size_t n;
8631a2
   char *p;
8631a2
   grub_file_t f = NULL;
8631a2
   grub_off_t sz;
8631a2
-  char *title = NULL, *options = NULL, *clinux = NULL, *initrd = NULL, *src = NULL;
8631a2
-  const char *args[2] = { NULL, NULL };
8631a2
+  struct bls_entry *entry;
8631a2
+  const char *dirname= (const char *)data;
8631a2
+  const char *devid = grub_env_get ("boot");
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "filename: \"%s\"\n", filename);
8631a2
 
8631a2
   if (filename[0] == '.')
8631a2
     return 0;
8631a2
@@ -67,7 +440,7 @@ static int parse_entry (
8631a2
   if (grub_strcmp (filename + n - 5, ".conf") != 0)
8631a2
     return 0;
8631a2
 
8631a2
-  p = grub_xasprintf (GRUB_BLS_CONFIG_PATH "%s", filename);
8631a2
+  p = grub_xasprintf ("(%s)%s/%s", devid, dirname, filename);
8631a2
 
8631a2
   f = grub_file_open (p);
8631a2
   if (!f)
8631a2
@@ -77,54 +450,169 @@ static int parse_entry (
8631a2
   if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024)
8631a2
     goto finish;
8631a2
 
8631a2
+  entry = bls_new_entry();
8631a2
+  if (!entry)
8631a2
+    goto finish;
8631a2
+
8631a2
   for (;;)
8631a2
     {
8631a2
       char *buf;
8631a2
+      char *separator;
8631a2
+      int rc;
8631a2
 
8631a2
       buf = grub_file_getline (f);
8631a2
       if (!buf)
8631a2
 	break;
8631a2
 
8631a2
-      if (grub_strncmp (buf, "title ", 6) == 0)
8631a2
-	{
8631a2
-	  grub_free (title);
8631a2
-	  title = grub_strdup (buf + 6);
8631a2
-	  if (!title)
8631a2
-	    goto finish;
8631a2
-	}
8631a2
-      else if (grub_strncmp (buf, "options ", 8) == 0)
8631a2
-	{
8631a2
-	  grub_free (options);
8631a2
-	  options = grub_strdup (buf + 8);
8631a2
-	  if (!options)
8631a2
-	    goto finish;
8631a2
-	}
8631a2
-      else if (grub_strncmp (buf, "linux ", 6) == 0)
8631a2
-	{
8631a2
-	  grub_free (clinux);
8631a2
-	  clinux = grub_strdup (buf + 6);
8631a2
-	  if (!clinux)
8631a2
-	    goto finish;
8631a2
-	}
8631a2
-      else if (grub_strncmp (buf, "initrd ", 7) == 0)
8631a2
+      while (buf && buf[0] && (buf[0] == ' ' || buf[0] == '\t'))
8631a2
+	buf++;
8631a2
+      if (buf[0] == '#')
8631a2
+	continue;
8631a2
+
8631a2
+      separator = grub_strchr (buf, ' ');
8631a2
+
8631a2
+      if (!separator)
8631a2
+	separator = grub_strchr (buf, '\t');
8631a2
+
8631a2
+      if (!separator || separator[1] == '\0')
8631a2
 	{
8631a2
-	  grub_free (initrd);
8631a2
-	  initrd = grub_strdup (buf + 7);
8631a2
-	  if (!initrd)
8631a2
-	    goto finish;
8631a2
+	  grub_free (buf);
8631a2
+	  break;
8631a2
 	}
8631a2
 
8631a2
-      grub_free(buf);
8631a2
+      separator[0] = '\0';
8631a2
+
8631a2
+      rc = bls_add_keyval (entry, buf, separator+1);
8631a2
+      grub_free (buf);
8631a2
+      if (rc < 0)
8631a2
+	break;
8631a2
+    }
8631a2
+
8631a2
+  grub_qsort(&entry->keyvals[0], entry->nkeyvals, sizeof (struct keyval *),
8631a2
+	     keyval_cmp, NULL);
8631a2
+
8631a2
+finish:
8631a2
+  grub_free (p);
8631a2
+
8631a2
+  if (f)
8631a2
+    grub_file_close (f);
8631a2
+
8631a2
+  return 0;
8631a2
+}
8631a2
+
8631a2
+static grub_envblk_t saved_env = NULL;
8631a2
+
8631a2
+static int
8631a2
+save_var (const char *name, const char *value, void *whitelist UNUSED)
8631a2
+{
8631a2
+  const char *val = grub_env_get (name);
8631a2
+  grub_dprintf("blscfg", "saving \"%s\"\n", name);
8631a2
+
8631a2
+  if (val)
8631a2
+    grub_envblk_set (saved_env, name, value);
8631a2
+
8631a2
+  return 0;
8631a2
+}
8631a2
+
8631a2
+static int
8631a2
+unset_var (const char *name, const char *value UNUSED, void *whitelist)
8631a2
+{
8631a2
+  grub_dprintf("blscfg", "restoring \"%s\"\n", name);
8631a2
+  if (! whitelist)
8631a2
+    {
8631a2
+      grub_env_unset (name);
8631a2
+      return 0;
8631a2
     }
8631a2
 
8631a2
-  if (!linux)
8631a2
+  if (test_whitelist_membership (name,
8631a2
+				 (const grub_env_whitelist_t *) whitelist))
8631a2
+    grub_env_unset (name);
8631a2
+
8631a2
+  return 0;
8631a2
+}
8631a2
+
8631a2
+static char **bls_make_list (struct bls_entry *entry, const char *key, int *num)
8631a2
+{
8631a2
+  int last = -1;
8631a2
+  char *val;
8631a2
+
8631a2
+  int nlist = 0;
8631a2
+  char **list = NULL;
8631a2
+
8631a2
+  list = grub_malloc (sizeof (char *));
8631a2
+  if (!list)
8631a2
+    return NULL;
8631a2
+  list[0] = NULL;
8631a2
+
8631a2
+  while (1)
8631a2
+    {
8631a2
+      char **new;
8631a2
+
8631a2
+      val = bls_get_val (entry, key, &last);
8631a2
+      if (!val)
8631a2
+	break;
8631a2
+
8631a2
+      new = grub_realloc (list, (nlist + 2) * sizeof (char *));
8631a2
+      if (!new)
8631a2
+	break;
8631a2
+
8631a2
+      list = new;
8631a2
+      list[nlist++] = val;
8631a2
+      list[nlist] = NULL;
8631a2
+  }
8631a2
+
8631a2
+  if (num)
8631a2
+    *num = nlist;
8631a2
+
8631a2
+  return list;
8631a2
+}
8631a2
+
8631a2
+static void create_entry (struct bls_entry *entry, const char *cfgfile)
8631a2
+{
8631a2
+  int argc = 0;
8631a2
+  const char **argv = NULL;
8631a2
+
8631a2
+  char *title = NULL;
8631a2
+  char *clinux = NULL;
8631a2
+  char *options = NULL;
8631a2
+  char *initrd = NULL;
8631a2
+  char *id = NULL;
8631a2
+  char *hotkey = NULL;
8631a2
+
8631a2
+  char *users = NULL;
8631a2
+  char **classes = NULL;
8631a2
+
8631a2
+  char **args = NULL;
8631a2
+
8631a2
+  char *src = NULL;
8631a2
+  int i;
8631a2
+
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+  clinux = bls_get_val (entry, "linux", NULL);
8631a2
+  if (!clinux)
8631a2
     {
8631a2
-      grub_printf ("Skipping file %s with no 'linux' key.", p);
8631a2
+      grub_dprintf ("blscfg", "Skipping file %s with no 'linux' key.\n", cfgfile);
8631a2
       goto finish;
8631a2
     }
8631a2
 
8631a2
-  args[0] = title ? title : filename;
8631a2
+  title = bls_get_val (entry, "title", NULL);
8631a2
+  options = bls_get_val (entry, "options", NULL);
8631a2
+  initrd = bls_get_val (entry, "initrd", NULL);
8631a2
+  id = bls_get_val (entry, "id", NULL);
8631a2
 
8631a2
+  hotkey = bls_get_val (entry, "grub_hotkey", NULL);
8631a2
+  users = bls_get_val (entry, "grub_users", NULL);
8631a2
+  classes = bls_make_list (entry, "grub_class", NULL);
8631a2
+  args = bls_make_list (entry, "grub_arg", &argc);
8631a2
+
8631a2
+  argc += 1;
8631a2
+  argv = grub_malloc ((argc + 1) * sizeof (char *));
8631a2
+  argv[0] = title ? title : clinux;
8631a2
+  for (i = 1; i < argc; i++)
8631a2
+    argv[i] = args[i-1];
8631a2
+  argv[argc] = NULL;
8631a2
+
8631a2
+  grub_dprintf("blscfg", "adding menu entry for \"%s\"\n", title);
8631a2
   src = grub_xasprintf ("load_video\n"
8631a2
 			"set gfx_payload=keep\n"
8631a2
 			"insmod gzio\n"
8631a2
@@ -133,40 +621,219 @@ static int parse_entry (
8631a2
 			GRUB_BOOT_DEVICE, clinux, options ? " " : "", options ? options : "",
8631a2
 			initrd ? GRUB_INITRD_CMD " " : "", initrd ? GRUB_BOOT_DEVICE : "", initrd ? initrd : "", initrd ? "\n" : "");
8631a2
 
8631a2
-  grub_normal_add_menu_entry (1, args, NULL, NULL, "bls", NULL, NULL, src, 0);
8631a2
+  grub_normal_add_menu_entry (argc, argv, classes, id, users, hotkey, NULL, src, 0);
8631a2
 
8631a2
 finish:
8631a2
-  grub_free (p);
8631a2
-  grub_free (title);
8631a2
-  grub_free (options);
8631a2
-  grub_free (clinux);
8631a2
-  grub_free (initrd);
8631a2
-  grub_free (src);
8631a2
+  if (classes)
8631a2
+      grub_free (classes);
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+  if (args)
8631a2
+      grub_free (args);
8631a2
+
8631a2
+  if (argv)
8631a2
+      grub_free (argv);
8631a2
+
8631a2
+  if (src)
8631a2
+      grub_free (src);
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+}
8631a2
+
8631a2
+struct find_entry_info {
8631a2
+	grub_device_t dev;
8631a2
+	grub_fs_t fs;
8631a2
+	int efi;
8631a2
+};
8631a2
+
8631a2
+/*
8631a2
+ * filename: if the directory is /EFI/something/ , filename is "something"
8631a2
+ * info: unused
8631a2
+ * data: the filesystem object the file is on.
8631a2
+ */
8631a2
+static int find_entry (const char *filename,
8631a2
+		       const struct grub_dirhook_info *dirhook_info UNUSED,
8631a2
+		       void *data)
8631a2
+{
8631a2
+  struct find_entry_info *info = (struct find_entry_info *)data;
8631a2
+  grub_file_t f = NULL;
8631a2
+  char *grubenv_path = NULL;
8631a2
+  grub_envblk_t env = NULL;
8631a2
+  char *default_blsdir = NULL;
8631a2
+  const char *blsdir = NULL;
8631a2
+  char *saved_env_buf = NULL;
8631a2
+  int r = 0;
8631a2
+  const char *devid = grub_env_get ("boot");
8631a2
+
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+  if (!grub_strcmp (filename, ".") ||
8631a2
+      !grub_strcmp (filename, ".."))
8631a2
+    return 0;
8631a2
+
8631a2
+  if (info->efi && !grub_strcasecmp (filename, "boot"))
8631a2
+    return 0;
8631a2
+
8631a2
+  saved_env_buf = grub_malloc (512);
8631a2
+
8631a2
+  // set a default blsdir
8631a2
+  if (info->efi)
8631a2
+    default_blsdir = grub_xasprintf ("/EFI/%s%s", filename,
8631a2
+				     GRUB_BLS_CONFIG_PATH);
8631a2
+  else
8631a2
+    default_blsdir = grub_xasprintf ("%s", GRUB_BLS_CONFIG_PATH);
8631a2
+
8631a2
+  grub_env_set ("blsdir", default_blsdir);
8631a2
+  grub_dprintf ("blscfg", "default_blsdir: \"%s\"\n", default_blsdir);
8631a2
+
8631a2
+  /*
8631a2
+   * try to load a grubenv from /EFI/wherever/grubenv
8631a2
+   */
8631a2
+  if (info->efi)
8631a2
+    grubenv_path = grub_xasprintf ("(%s)/EFI/%s/grubenv", devid, filename);
8631a2
+  else
8631a2
+    grubenv_path = grub_xasprintf ("(%s)/grub2/grubenv", devid);
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "looking for \"%s\"\n", grubenv_path);
8631a2
+  f = grub_file_open (grubenv_path);
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "%s it\n", f ? "found" : "did not find");
8631a2
+  grub_free (grubenv_path);
8631a2
+  if (f)
8631a2
+    {
8631a2
+      grub_off_t sz;
8631a2
+
8631a2
+      grub_dprintf ("blscfg", "getting size\n");
8631a2
+      sz = grub_file_size (f);
8631a2
+      if (sz == GRUB_FILE_SIZE_UNKNOWN || sz > 1024*1024)
8631a2
+	goto finish;
8631a2
+
8631a2
+      grub_dprintf ("blscfg", "reading env\n");
8631a2
+      env = read_envblk_file (f);
8631a2
+      if (!env)
8631a2
+	goto finish;
8631a2
+      grub_dprintf ("blscfg", "read env file\n");
8631a2
+
8631a2
+      grub_memset (saved_env_buf, '#', 512);
8631a2
+      grub_memcpy (saved_env_buf, GRUB_ENVBLK_SIGNATURE,
8631a2
+		   sizeof (GRUB_ENVBLK_SIGNATURE));
8631a2
+      grub_dprintf ("blscfg", "saving env\n");
8631a2
+      saved_env = grub_envblk_open (saved_env_buf, 512);
8631a2
+      if (!saved_env)
8631a2
+	goto finish;
8631a2
+
8631a2
+      // save everything listed in "env" with values from our existing grub env
8631a2
+      grub_envblk_iterate (env, NULL, save_var);
8631a2
+      // set everything from our loaded grubenv into the real grub env
8631a2
+      grub_envblk_iterate (env, NULL, set_var);
8631a2
+    }
8631a2
+  else
8631a2
+    {
8631a2
+      grub_err_t e;
8631a2
+      grub_dprintf ("blscfg", "no such file\n");
8631a2
+      do
8631a2
+	{
8631a2
+	  e = grub_error_pop();
8631a2
+	} while (e);
8631a2
+
8631a2
+    }
8631a2
+
8631a2
+  blsdir = grub_env_get ("blsdir");
8631a2
+  if (!blsdir)
8631a2
+    goto finish;
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "blsdir: \"%s\"\n", blsdir);
8631a2
+  if (blsdir[0] != '/' && info->efi)
8631a2
+    blsdir = grub_xasprintf ("/EFI/%s/%s/", filename, blsdir);
8631a2
+  else
8631a2
+    blsdir = grub_strdup (blsdir);
8631a2
+
8631a2
+  if (!blsdir)
8631a2
+    goto finish;
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "blsdir: \"%s\"\n", blsdir);
8631a2
+  r = info->fs->dir (info->dev, blsdir, read_entry, (char *)blsdir);
8631a2
+  if (r != 0) {
8631a2
+      grub_dprintf ("blscfg", "read_entry returned error\n");
8631a2
+      grub_err_t e;
8631a2
+      do
8631a2
+	{
8631a2
+	  e = grub_error_pop();
8631a2
+	} while (e);
8631a2
+  }
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "Sorting %d entries\n", nentries);
8631a2
+  grub_qsort(&entries[0], nentries, sizeof (struct bls_entry *), bls_cmp, NULL);
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "%s Creating %d entries from bls\n", __func__, nentries);
8631a2
+  for (r = nentries - 1; r >= 0; r--)
8631a2
+      create_entry(entries[r], filename);
8631a2
+
8631a2
+  for (r = 0; r < nentries; r++)
8631a2
+      bls_free_entry (entries[r]);
8631a2
+finish:
8631a2
+  nentries = 0;
8631a2
+
8631a2
+  grub_free (entries);
8631a2
+  entries = NULL;
8631a2
+
8631a2
+  grub_free ((char *)blsdir);
8631a2
+
8631a2
+  grub_env_unset ("blsdir");
8631a2
+
8631a2
+  if (saved_env)
8631a2
+    {
8631a2
+      // remove everything from the real environment that's defined in env
8631a2
+      grub_envblk_iterate (env, NULL, unset_var);
8631a2
+
8631a2
+      // re-set the things from our original environment
8631a2
+      grub_envblk_iterate (saved_env, NULL, set_var);
8631a2
+      grub_envblk_close (saved_env);
8631a2
+      saved_env = NULL;
8631a2
+    }
8631a2
+  else if (saved_env_buf)
8631a2
+    {
8631a2
+      // if we have a saved environment, grub_envblk_close() freed this.
8631a2
+      grub_free (saved_env_buf);
8631a2
+    }
8631a2
+
8631a2
+  if (env)
8631a2
+    grub_envblk_close (env);
8631a2
 
8631a2
   if (f)
8631a2
     grub_file_close (f);
8631a2
 
8631a2
+  grub_free (default_blsdir);
8631a2
+
8631a2
   return 0;
8631a2
 }
8631a2
 
8631a2
 static grub_err_t
8631a2
-grub_cmd_bls_import (grub_extcmd_context_t ctxt __attribute__ ((unused)),
8631a2
-		     int argc __attribute__ ((unused)),
8631a2
-		     char **args __attribute__ ((unused)))
8631a2
+grub_cmd_blscfg (grub_extcmd_context_t ctxt UNUSED,
8631a2
+		     int argc UNUSED,
8631a2
+		     char **args UNUSED)
8631a2
 {
8631a2
   grub_fs_t fs;
8631a2
   grub_device_t dev;
8631a2
   static grub_err_t r;
8631a2
   const char *devid;
8631a2
+  struct find_entry_info info =
8631a2
+    {
8631a2
+      .dev = NULL,
8631a2
+      .fs = NULL,
8631a2
+      .efi = 0,
8631a2
+    };
8631a2
 
8631a2
-  devid = grub_env_get ("root");
8631a2
+
8631a2
+  grub_dprintf ("blscfg", "finding boot\n");
8631a2
+  devid = grub_env_get ("boot");
8631a2
   if (!devid)
8631a2
-    return grub_error (GRUB_ERR_FILE_NOT_FOUND, N_("variable `%s' isn't set"), "root");
8631a2
+    return grub_error (GRUB_ERR_FILE_NOT_FOUND,
8631a2
+		       N_("variable `%s' isn't set"), "boot");
8631a2
 
8631a2
+  grub_dprintf ("blscfg", "opening %s\n", devid);
8631a2
   dev = grub_device_open (devid);
8631a2
   if (!dev)
8631a2
     return grub_errno;
8631a2
 
8631a2
+  grub_dprintf ("blscfg", "probing fs\n");
8631a2
   fs = grub_fs_probe (dev);
8631a2
   if (!fs)
8631a2
     {
8631a2
@@ -174,7 +841,17 @@ grub_cmd_bls_import (grub_extcmd_context_t ctxt __attribute__ ((unused)),
8631a2
       goto finish;
8631a2
     }
8631a2
 
8631a2
-  r = fs->dir (dev, GRUB_BLS_CONFIG_PATH, parse_entry, NULL);
8631a2
+  info.dev = dev;
8631a2
+  info.fs = fs;
8631a2
+#ifdef GRUB_MACHINE_EFI
8631a2
+  info.efi = 1;
8631a2
+  grub_dprintf ("blscfg", "scanning /EFI/\n");
8631a2
+  r = fs->dir (dev, "/EFI/", find_entry, &info;;
8631a2
+#else
8631a2
+  info.efi = 0;
8631a2
+  grub_dprintf ("blscfg", "scanning %s\n", GRUB_BLS_CONFIG_PATH);
8631a2
+  r = fs->dir (dev, "/", find_entry, &info;;
8631a2
+#endif
8631a2
 
8631a2
 finish:
8631a2
   if (dev)
8631a2
@@ -184,18 +861,27 @@ finish:
8631a2
 }
8631a2
 
8631a2
 static grub_extcmd_t cmd;
8631a2
+static grub_extcmd_t oldcmd;
8631a2
 
8631a2
 GRUB_MOD_INIT(bls)
8631a2
 {
8631a2
-  cmd = grub_register_extcmd ("bls_import",
8631a2
-			      grub_cmd_bls_import,
8631a2
+  grub_dprintf("blscfg", "%s got here\n", __func__);
8631a2
+  cmd = grub_register_extcmd ("blscfg",
8631a2
+			      grub_cmd_blscfg,
8631a2
 			      0,
8631a2
 			      NULL,
8631a2
 			      N_("Import Boot Loader Specification snippets."),
8631a2
 			      NULL);
8631a2
+  oldcmd = grub_register_extcmd ("bls_import",
8631a2
+				 grub_cmd_blscfg,
8631a2
+				 0,
8631a2
+				 NULL,
8631a2
+				 N_("Import Boot Loader Specification snippets."),
8631a2
+				 NULL);
8631a2
 }
8631a2
 
8631a2
 GRUB_MOD_FINI(bls)
8631a2
 {
8631a2
   grub_unregister_extcmd (cmd);
8631a2
+  grub_unregister_extcmd (oldcmd);
8631a2
 }
8631a2
diff --git a/grub-core/commands/loadenv.c b/grub-core/commands/loadenv.c
8631a2
index acd93d123ed..91c99456091 100644
8631a2
--- a/grub-core/commands/loadenv.c
8631a2
+++ b/grub-core/commands/loadenv.c
8631a2
@@ -28,6 +28,8 @@
8631a2
 #include <grub/extcmd.h>
8631a2
 #include <grub/i18n.h>
8631a2
 
8631a2
+#include "loadenv.h"
8631a2
+
8631a2
 GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 
8631a2
 static const struct grub_arg_option options[] =
8631a2
@@ -84,81 +86,6 @@ open_envblk_file (char *filename, int untrusted)
8631a2
   return file;
8631a2
 }
8631a2
 
8631a2
-static grub_envblk_t
8631a2
-read_envblk_file (grub_file_t file)
8631a2
-{
8631a2
-  grub_off_t offset = 0;
8631a2
-  char *buf;
8631a2
-  grub_size_t size = grub_file_size (file);
8631a2
-  grub_envblk_t envblk;
8631a2
-
8631a2
-  buf = grub_malloc (size);
8631a2
-  if (! buf)
8631a2
-    return 0;
8631a2
-
8631a2
-  while (size > 0)
8631a2
-    {
8631a2
-      grub_ssize_t ret;
8631a2
-
8631a2
-      ret = grub_file_read (file, buf + offset, size);
8631a2
-      if (ret <= 0)
8631a2
-        {
8631a2
-          grub_free (buf);
8631a2
-          return 0;
8631a2
-        }
8631a2
-
8631a2
-      size -= ret;
8631a2
-      offset += ret;
8631a2
-    }
8631a2
-
8631a2
-  envblk = grub_envblk_open (buf, offset);
8631a2
-  if (! envblk)
8631a2
-    {
8631a2
-      grub_free (buf);
8631a2
-      grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
8631a2
-      return 0;
8631a2
-    }
8631a2
-
8631a2
-  return envblk;
8631a2
-}
8631a2
-
8631a2
-struct grub_env_whitelist
8631a2
-{
8631a2
-  grub_size_t len;
8631a2
-  char **list;
8631a2
-};
8631a2
-typedef struct grub_env_whitelist grub_env_whitelist_t;
8631a2
-
8631a2
-static int
8631a2
-test_whitelist_membership (const char* name,
8631a2
-                           const grub_env_whitelist_t* whitelist)
8631a2
-{
8631a2
-  grub_size_t i;
8631a2
-
8631a2
-  for (i = 0; i < whitelist->len; i++)
8631a2
-    if (grub_strcmp (name, whitelist->list[i]) == 0)
8631a2
-      return 1;  /* found it */
8631a2
-
8631a2
-  return 0;  /* not found */
8631a2
-}
8631a2
-
8631a2
-/* Helper for grub_cmd_load_env.  */
8631a2
-static int
8631a2
-set_var (const char *name, const char *value, void *whitelist)
8631a2
-{
8631a2
-  if (! whitelist)
8631a2
-    {
8631a2
-      grub_env_set (name, value);
8631a2
-      return 0;
8631a2
-    }
8631a2
-
8631a2
-  if (test_whitelist_membership (name,
8631a2
-				 (const grub_env_whitelist_t *) whitelist))
8631a2
-    grub_env_set (name, value);
8631a2
-
8631a2
-  return 0;
8631a2
-}
8631a2
-
8631a2
 static grub_err_t
8631a2
 grub_cmd_load_env (grub_extcmd_context_t ctxt, int argc, char **args)
8631a2
 {
8631a2
diff --git a/grub-core/commands/loadenv.h b/grub-core/commands/loadenv.h
8631a2
new file mode 100644
8631a2
index 00000000000..952f46121bd
8631a2
--- /dev/null
8631a2
+++ b/grub-core/commands/loadenv.h
8631a2
@@ -0,0 +1,93 @@
8631a2
+/* loadenv.c - command to load/save environment variable.  */
8631a2
+/*
8631a2
+ *  GRUB  --  GRand Unified Bootloader
8631a2
+ *  Copyright (C) 2008,2009,2010  Free Software Foundation, Inc.
8631a2
+ *
8631a2
+ *  GRUB is free software: you can redistribute it and/or modify
8631a2
+ *  it under the terms of the GNU General Public License as published by
8631a2
+ *  the Free Software Foundation, either version 3 of the License, or
8631a2
+ *  (at your option) any later version.
8631a2
+ *
8631a2
+ *  GRUB is distributed in the hope that it will be useful,
8631a2
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
8631a2
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8631a2
+ *  GNU General Public License for more details.
8631a2
+ *
8631a2
+ *  You should have received a copy of the GNU General Public License
8631a2
+ *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
8631a2
+ */
8631a2
+
8631a2
+static grub_envblk_t UNUSED
8631a2
+read_envblk_file (grub_file_t file)
8631a2
+{
8631a2
+  grub_off_t offset = 0;
8631a2
+  char *buf;
8631a2
+  grub_size_t size = grub_file_size (file);
8631a2
+  grub_envblk_t envblk;
8631a2
+
8631a2
+  buf = grub_malloc (size);
8631a2
+  if (! buf)
8631a2
+    return 0;
8631a2
+
8631a2
+  while (size > 0)
8631a2
+    {
8631a2
+      grub_ssize_t ret;
8631a2
+
8631a2
+      ret = grub_file_read (file, buf + offset, size);
8631a2
+      if (ret <= 0)
8631a2
+        {
8631a2
+          grub_free (buf);
8631a2
+          return 0;
8631a2
+        }
8631a2
+
8631a2
+      size -= ret;
8631a2
+      offset += ret;
8631a2
+    }
8631a2
+
8631a2
+  envblk = grub_envblk_open (buf, offset);
8631a2
+  if (! envblk)
8631a2
+    {
8631a2
+      grub_free (buf);
8631a2
+      grub_error (GRUB_ERR_BAD_FILE_TYPE, "invalid environment block");
8631a2
+      return 0;
8631a2
+    }
8631a2
+
8631a2
+  return envblk;
8631a2
+}
8631a2
+
8631a2
+struct grub_env_whitelist
8631a2
+{
8631a2
+  grub_size_t len;
8631a2
+  char **list;
8631a2
+};
8631a2
+typedef struct grub_env_whitelist grub_env_whitelist_t;
8631a2
+
8631a2
+static int UNUSED
8631a2
+test_whitelist_membership (const char* name,
8631a2
+                           const grub_env_whitelist_t* whitelist)
8631a2
+{
8631a2
+  grub_size_t i;
8631a2
+
8631a2
+  for (i = 0; i < whitelist->len; i++)
8631a2
+    if (grub_strcmp (name, whitelist->list[i]) == 0)
8631a2
+      return 1;  /* found it */
8631a2
+
8631a2
+  return 0;  /* not found */
8631a2
+}
8631a2
+
8631a2
+/* Helper for grub_cmd_load_env.  */
8631a2
+static int UNUSED
8631a2
+set_var (const char *name, const char *value, void *whitelist)
8631a2
+{
8631a2
+  if (! whitelist)
8631a2
+    {
8631a2
+      grub_env_set (name, value);
8631a2
+      return 0;
8631a2
+    }
8631a2
+
8631a2
+  if (test_whitelist_membership (name,
8631a2
+				 (const grub_env_whitelist_t *) whitelist))
8631a2
+    grub_env_set (name, value);
8631a2
+
8631a2
+  return 0;
8631a2
+}
8631a2
diff --git a/include/grub/compiler.h b/include/grub/compiler.h
8631a2
index c9e1d7a73dc..9859ff4cc79 100644
8631a2
--- a/include/grub/compiler.h
8631a2
+++ b/include/grub/compiler.h
8631a2
@@ -48,4 +48,6 @@
8631a2
 #  define WARN_UNUSED_RESULT
8631a2
 #endif
8631a2
 
8631a2
+#define UNUSED __attribute__((__unused__))
8631a2
+
8631a2
 #endif /* ! GRUB_COMPILER_HEADER */