a3bfe3
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
a3bfe3
From: Javier Martinez Canillas <javierm@redhat.com>
a3bfe3
Date: Thu, 27 Sep 2018 10:49:14 +0200
a3bfe3
Subject: [PATCH] Move quicksort function from kernel.exec to the blscfg module
a3bfe3
a3bfe3
The qsort function is defined in the grub2 kernel and exported for modules
a3bfe3
to use. But this prevents the blscfg.mod to be loaded by old grub2 kernels
a3bfe3
that don't export this symbol.
a3bfe3
a3bfe3
Loading the latest blscfg module might be useful on legacy BIOS systems to
a3bfe3
avoid updating the first and second stage grub2 images in the boot device.
a3bfe3
a3bfe3
Since the only caller of the qsort function is the blscfg module, move the
a3bfe3
qsort function out of the grub2 kernel and only have it in the blscfg.mod.
a3bfe3
a3bfe3
While being there, also remove the grub_bsearch() function that is unused.
a3bfe3
a3bfe3
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
a3bfe3
---
a3bfe3
 grub-core/Makefile.core.def                      |  2 +-
a3bfe3
 grub-core/commands/blscfg.c                      |  3 ++-
a3bfe3
 grub-core/{kern/qsort.c => commands/bls_qsort.h} | 30 +++---------------------
a3bfe3
 include/grub/misc.h                              | 15 ------------
a3bfe3
 4 files changed, 6 insertions(+), 44 deletions(-)
a3bfe3
 rename grub-core/{kern/qsort.c => commands/bls_qsort.h} (93%)
a3bfe3
a3bfe3
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
a3bfe3
index f33ff332079..18d4ec20cdf 100644
a3bfe3
--- a/grub-core/Makefile.core.def
a3bfe3
+++ b/grub-core/Makefile.core.def
a3bfe3
@@ -129,7 +129,6 @@ kernel = {
a3bfe3
   common = kern/rescue_parser.c;
a3bfe3
   common = kern/rescue_reader.c;
a3bfe3
   common = kern/term.c;
a3bfe3
-  common = kern/qsort.c;
a3bfe3
   common = kern/backtrace.c;
a3bfe3
   common = kern/tpm.c;
a3bfe3
 
a3bfe3
@@ -786,6 +785,7 @@ module = {
a3bfe3
 module = {
a3bfe3
   name = blscfg;
a3bfe3
   common = commands/blscfg.c;
a3bfe3
+  common = commands/bls_qsort.h;
a3bfe3
   common = commands/loadenv.h;
a3bfe3
   enable = efi;
a3bfe3
   enable = i386_pc;
a3bfe3
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
a3bfe3
index abd6f00d0de..bec5a9ffe3e 100644
a3bfe3
--- a/grub-core/commands/blscfg.c
a3bfe3
+++ b/grub-core/commands/blscfg.c
a3bfe3
@@ -36,6 +36,7 @@
a3bfe3
 
a3bfe3
 GRUB_MOD_LICENSE ("GPLv3+");
a3bfe3
 
a3bfe3
+#include "bls_qsort.h"
a3bfe3
 #include "loadenv.h"
a3bfe3
 
a3bfe3
 #define GRUB_BLS_CONFIG_PATH "/loader/entries/"
a3bfe3
@@ -717,7 +718,7 @@ read_fallback:
a3bfe3
       use_version = false;
a3bfe3
   }
a3bfe3
 
a3bfe3
-  grub_qsort(&entries[0], nentries, sizeof (struct bls_entry *), bls_cmp, &use_version);
a3bfe3
+  bls_qsort(&entries[0], nentries, sizeof (struct bls_entry *), bls_cmp, &use_version);
a3bfe3
 
a3bfe3
   grub_dprintf ("blscfg", "%s Creating %d entries from bls\n", __func__, nentries);
a3bfe3
   for (r = nentries - 1; r >= 0; r--)
a3bfe3
diff --git a/grub-core/kern/qsort.c b/grub-core/commands/bls_qsort.h
a3bfe3
similarity index 93%
a3bfe3
rename from grub-core/kern/qsort.c
a3bfe3
rename to grub-core/commands/bls_qsort.h
a3bfe3
index 7f3fc9ffdae..572765fa3f2 100644
a3bfe3
--- a/grub-core/kern/qsort.c
a3bfe3
+++ b/grub-core/commands/bls_qsort.h
a3bfe3
@@ -64,6 +64,7 @@ typedef struct
a3bfe3
 #define	POP(low, high)	((void) (--top, (low = top->lo), (high = top->hi)))
a3bfe3
 #define	STACK_NOT_EMPTY	(stack < top)
a3bfe3
 
a3bfe3
+typedef int (*grub_compar_d_fn_t) (const void *p0, const void *p1, void *state);
a3bfe3
 
a3bfe3
 /* Order size using quicksort.  This implementation incorporates
a3bfe3
    four optimizations discussed in Sedgewick:
a3bfe3
@@ -89,8 +90,8 @@ typedef struct
a3bfe3
       smaller partition.  This *guarantees* no more than log (total_elems)
a3bfe3
       stack size is needed (actually O(1) in this case)!  */
a3bfe3
 
a3bfe3
-void
a3bfe3
-grub_qsort (void *const pbase, grub_size_t total_elems, grub_size_t size,
a3bfe3
+static inline void UNUSED
a3bfe3
+bls_qsort (void *const pbase, grub_size_t total_elems, grub_size_t size,
a3bfe3
 	    grub_compar_d_fn_t cmp, void *arg)
a3bfe3
 {
a3bfe3
   char *base_ptr = (char *) pbase;
a3bfe3
@@ -252,28 +253,3 @@ grub_qsort (void *const pbase, grub_size_t total_elems, grub_size_t size,
a3bfe3
   }
a3bfe3
 }
a3bfe3
 
a3bfe3
-void *
a3bfe3
-grub_bsearch (const void *key, const void *base, grub_size_t nmemb, grub_size_t size,
a3bfe3
-	 grub_compar_d_fn_t compar, void *state)
a3bfe3
-{
a3bfe3
-  grub_size_t l, u, idx;
a3bfe3
-  const void *p;
a3bfe3
-  int comparison;
a3bfe3
-
a3bfe3
-  l = 0;
a3bfe3
-  u = nmemb;
a3bfe3
-  while (l < u)
a3bfe3
-    {
a3bfe3
-      idx = (l + u) / 2;
a3bfe3
-      p = (void *) (((const char *) base) + (idx * size));
a3bfe3
-      comparison = (*compar) (key, p, state);
a3bfe3
-      if (comparison < 0)
a3bfe3
-	u = idx;
a3bfe3
-      else if (comparison > 0)
a3bfe3
-	l = idx + 1;
a3bfe3
-      else
a3bfe3
-	return (void *) p;
a3bfe3
-    }
a3bfe3
-
a3bfe3
-  return NULL;
a3bfe3
-}
a3bfe3
diff --git a/include/grub/misc.h b/include/grub/misc.h
a3bfe3
index 5f1c1c1be4e..de9016ab709 100644
a3bfe3
--- a/include/grub/misc.h
a3bfe3
+++ b/include/grub/misc.h
a3bfe3
@@ -510,19 +510,4 @@ void EXPORT_FUNC(grub_real_boot_time) (const char *file,
a3bfe3
 #define grub_max(a, b) (((a) > (b)) ? (a) : (b))
a3bfe3
 #define grub_min(a, b) (((a) < (b)) ? (a) : (b))
a3bfe3
 
a3bfe3
-typedef int (*grub_compar_d_fn_t) (const void *p0, const void *p1, void *state);
a3bfe3
-
a3bfe3
-void *EXPORT_FUNC(grub_bsearch) (const void *key,
a3bfe3
-			    const void *base,
a3bfe3
-			    grub_size_t nmemb,
a3bfe3
-			    grub_size_t size,
a3bfe3
-			    grub_compar_d_fn_t compar,
a3bfe3
-			    void *state);
a3bfe3
-
a3bfe3
-void EXPORT_FUNC(grub_qsort) (void *const pbase,
a3bfe3
-			 grub_size_t total_elems,
a3bfe3
-			 grub_size_t size,
a3bfe3
-			 grub_compar_d_fn_t cmp,
a3bfe3
-			 void *state);
a3bfe3
-
a3bfe3
 #endif /* ! GRUB_MISC_HEADER */