Blame SOURCES/0209-blscfg-Expand-the-BLS-options-field-instead-of-showi.patch

8631a2
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
8631a2
From: Javier Martinez Canillas <javierm@redhat.com>
8631a2
Date: Wed, 18 Jul 2018 08:08:02 +0200
8631a2
Subject: [PATCH] blscfg: Expand the BLS options field instead of showing its
8631a2
 variables
8631a2
8631a2
The values of the BLS fragment fields can either be string literals or
8631a2
grub2 environment variables, the latter will be expanded by grub2 when
8631a2
the boot entry is selected.
8631a2
8631a2
But from a usability point of view, is much more convenient if the BLS
8631a2
parse code expand any variables that are present in the options field.
8631a2
8631a2
That will allow users to select an entry in the menu by pressing the e
8631a2
key and edit the kernel command line parameters. So for example instead
8631a2
of showing the following:
8631a2
8631a2
kernel /boot/vmlinuz-4.17.0 $kernelopts
8631a2
8631a2
It would show something like the following:
8631a2
8631a2
kernel /boot/vmlinuz-4.17.0 root=UUID=cec677c9-c890-4103-b94a-bcc191642935
8631a2
8631a2
Suggested-by: Hans de Goede <hdegoede@redhat.com>
8631a2
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
8631a2
---
8631a2
 grub-core/commands/blscfg.c | 69 ++++++++++++++++++++++++++++++++++++++++++++-
8631a2
 1 file changed, 68 insertions(+), 1 deletion(-)
8631a2
8631a2
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
f6e916
index c6addc4dc..80d8814fc 100644
8631a2
--- a/grub-core/commands/blscfg.c
8631a2
+++ b/grub-core/commands/blscfg.c
8631a2
@@ -32,6 +32,8 @@
8631a2
 #include <grub/normal.h>
8631a2
 #include <grub/lib/envblk.h>
8631a2
 
8631a2
+#include <stdbool.h>
8631a2
+
8631a2
 GRUB_MOD_LICENSE ("GPLv3+");
8631a2
 
8631a2
 #include "loadenv.h"
8631a2
@@ -506,6 +508,70 @@ static char **bls_make_list (struct bls_entry *entry, const char *key, int *num)
8631a2
   return list;
8631a2
 }
8631a2
 
8631a2
+static char *field_append(bool is_var, char *buffer, char *start, char *end)
8631a2
+{
8631a2
+  char *temp = grub_strndup(start, end - start + 1);
8631a2
+  const char *field = temp;
8631a2
+
8631a2
+  if (is_var) {
8631a2
+    field = grub_env_get (temp);
8631a2
+    if (!field)
8631a2
+      return buffer;
8631a2
+  }
8631a2
+
8631a2
+  if (!buffer) {
8631a2
+    buffer = grub_strdup(field);
8631a2
+    if (!buffer)
8631a2
+      return NULL;
8631a2
+  } else {
8631a2
+    buffer = grub_realloc (buffer, grub_strlen(buffer) + grub_strlen(field));
8631a2
+    if (!buffer)
8631a2
+      return NULL;
8631a2
+
8631a2
+    grub_stpcpy (buffer + grub_strlen(buffer), field);
8631a2
+  }
8631a2
+
8631a2
+  return buffer;
8631a2
+}
8631a2
+
8631a2
+static char *expand_val(char *value)
8631a2
+{
8631a2
+  char *buffer = NULL;
8631a2
+  char *start = value;
8631a2
+  char *end = value;
8631a2
+  bool is_var = false;
8631a2
+
8631a2
+  while (*value) {
8631a2
+    if (*value == '$') {
8631a2
+      if (start != end) {
8631a2
+	buffer = field_append(is_var, buffer, start, end);
8631a2
+	if (!buffer)
8631a2
+	  return NULL;
8631a2
+      }
8631a2
+
8631a2
+      is_var = true;
8631a2
+      start = value + 1;
8631a2
+    } else if (is_var) {
8631a2
+      if (!grub_isalnum(*value) && *value != '_') {
8631a2
+	buffer = field_append(is_var, buffer, start, end);
8631a2
+	is_var = false;
8631a2
+	start = value;
8631a2
+      }
8631a2
+    }
8631a2
+
8631a2
+    end = value;
8631a2
+    value++;
8631a2
+  }
8631a2
+
8631a2
+  if (start != end) {
8631a2
+    buffer = field_append(is_var, buffer, start, end);
8631a2
+    if (!buffer)
8631a2
+      return NULL;
8631a2
+  }
8631a2
+
8631a2
+  return buffer;
8631a2
+}
8631a2
+
8631a2
 static void create_entry (struct bls_entry *entry)
8631a2
 {
8631a2
   int argc = 0;
8631a2
@@ -536,7 +602,7 @@ static void create_entry (struct bls_entry *entry)
8631a2
     }
8631a2
 
8631a2
   title = bls_get_val (entry, "title", NULL);
8631a2
-  options = bls_get_val (entry, "options", NULL);
8631a2
+  options = expand_val (bls_get_val (entry, "options", NULL));
8631a2
   initrds = bls_make_list (entry, "initrd", NULL);
8631a2
 
8631a2
   hotkey = bls_get_val (entry, "grub_hotkey", NULL);
8631a2
@@ -594,6 +660,7 @@ static void create_entry (struct bls_entry *entry)
8631a2
 finish:
8631a2
   grub_free (initrd);
8631a2
   grub_free (initrds);
8631a2
+  grub_free (options);
8631a2
   grub_free (classes);
8631a2
   grub_free (args);
8631a2
   grub_free (argv);