nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

Blame SOURCES/0233-Move-quicksort-function-from-kernel.exec-to-the-blsc.patch

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