15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Javier Martinez Canillas <javierm@redhat.com>
15a207
Date: Tue, 26 Jun 2018 14:01:26 +0200
15a207
Subject: [PATCH] Use BLS fragment filename as menu entry id and for criteria
15a207
 to sort
15a207
15a207
The BLS config filenames are guaranteed to be unique, so they can be
15a207
used as GRUB2 entry id and can also be used to sort the menu entries.
15a207
15a207
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
15a207
---
15a207
 grub-core/commands/blscfg.c | 62 +++++++++------------------------------------
15a207
 1 file changed, 12 insertions(+), 50 deletions(-)
15a207
15a207
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
15a207
index 70939a81826..cd8659384e4 100644
15a207
--- a/grub-core/commands/blscfg.c
15a207
+++ b/grub-core/commands/blscfg.c
15a207
@@ -70,6 +70,7 @@ struct bls_entry
15a207
 {
15a207
   struct keyval **keyvals;
15a207
   int nkeyvals;
15a207
+  char *filename;
15a207
 };
15a207
 
15a207
 static struct bls_entry **entries;
15a207
@@ -166,6 +167,7 @@ static void bls_free_entry(struct bls_entry *entry)
15a207
 
15a207
   grub_free (entry->keyvals);
15a207
   grub_memset (entry, 0, sizeof (*entry));
15a207
+  grub_free (entry->filename);
15a207
   grub_free (entry);
15a207
 }
15a207
 
15a207
@@ -327,58 +329,12 @@ finish:
15a207
 
15a207
 typedef int (*void_cmp_t)(void *, void *);
15a207
 
15a207
-static int nulcmp(char *s0, char *s1, void_cmp_t cmp)
15a207
-{
15a207
-  grub_dprintf("blscfg", "%s got here\n", __func__);
15a207
-  if (s1 && !s0)
15a207
-    return 1;
15a207
-  if (s0 && !s1)
15a207
-    return -1;
15a207
-  if (!s0 && !s1)
15a207
-    return 0;
15a207
-  if (cmp)
15a207
-    return cmp(s0, s1);
15a207
-  return grub_strcmp(s0, s1);
15a207
-}
15a207
-
15a207
-static int
15a207
-bls_keyval_cmp(struct bls_entry *e0, struct bls_entry *e1, const char *keyname)
15a207
-{
15a207
-  char *val0, *val1;
15a207
-
15a207
-  val0 = bls_get_val (e0, keyname, NULL);
15a207
-  val1 = bls_get_val (e1, keyname, NULL);
15a207
-
15a207
-  if (val1 && !val0)
15a207
-    return 1;
15a207
-
15a207
-  if (val0 && !val1)
15a207
-    return -1;
15a207
-
15a207
-  if (!val0 && !val1)
15a207
-    return 0;
15a207
-
15a207
-  return nulcmp(val0, val1, (void_cmp_t)vercmp);
15a207
-}
15a207
-
15a207
 static int bls_cmp(const void *p0, const void *p1, void *state UNUSED)
15a207
 {
15a207
   struct bls_entry * e0 = *(struct bls_entry **)p0;
15a207
   struct bls_entry * e1 = *(struct bls_entry **)p1;
15a207
-  int rc = 0;
15a207
 
15a207
-  rc = bls_keyval_cmp (e0, e1, "id");
15a207
-
15a207
-  if (rc == 0)
15a207
-    rc = bls_keyval_cmp (e0, e1, "version");
15a207
-
15a207
-  if (rc == 0)
15a207
-    rc = bls_keyval_cmp (e0, e1, "title");
15a207
-
15a207
-  if (rc == 0)
15a207
-    rc = bls_keyval_cmp (e0, e1, "linux");
15a207
-
15a207
-  return rc;
15a207
+  return vercmp(e0->filename, e1->filename);
15a207
 }
15a207
 
15a207
 struct read_entry_info {
15a207
@@ -424,6 +380,12 @@ static int read_entry (
15a207
   if (!entry)
15a207
     goto finish;
15a207
 
15a207
+  entry->filename = grub_strndup(filename, n - 5);
15a207
+  if (!entry->filename)
15a207
+    goto finish;
15a207
+
15a207
+  entry->filename[n - 5] = '\0';
15a207
+
15a207
   for (;;)
15a207
     {
15a207
       char *buf;
15a207
@@ -548,7 +510,7 @@ static void create_entry (struct bls_entry *entry, const char *cfgfile)
15a207
   char *options = NULL;
15a207
   char **initrds = NULL;
15a207
   char *initrd = NULL;
15a207
-  char *id = NULL;
15a207
+  char *id = entry->filename;
15a207
   char *hotkey = NULL;
15a207
 
15a207
   char *users = NULL;
15a207
@@ -570,7 +532,6 @@ static void create_entry (struct bls_entry *entry, const char *cfgfile)
15a207
   title = bls_get_val (entry, "title", NULL);
15a207
   options = bls_get_val (entry, "options", NULL);
15a207
   initrds = bls_make_list (entry, "initrd", NULL);
15a207
-  id = bls_get_val (entry, "id", NULL);
15a207
 
15a207
   hotkey = bls_get_val (entry, "grub_hotkey", NULL);
15a207
   users = bls_get_val (entry, "grub_users", NULL);
15a207
@@ -584,7 +545,8 @@ static void create_entry (struct bls_entry *entry, const char *cfgfile)
15a207
     argv[i] = args[i-1];
15a207
   argv[argc] = NULL;
15a207
 
15a207
-  grub_dprintf ("blscfg", "adding menu entry for \"%s\"\n", title);
15a207
+  grub_dprintf ("blscfg", "adding menu entry for \"%s\" with id \"%s\"\n",
15a207
+		title, id);
15a207
   if (initrds)
15a207
     {
15a207
       int initrd_size = sizeof (GRUB_INITRD_CMD);