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