[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 1/2] Add helper to load linux kmods
From: |
Ismael Luceno |
Subject: |
[PATCH 1/2] Add helper to load linux kmods |
Date: |
Wed, 24 Aug 2022 18:03:10 +0200 |
The function checks for already loaded (incl. built-in) modules first at
/sys/module/, then tries to load it.
Abort when unable to load efivarfs/efivars.
Signed-off-by: Ismael Luceno <iluceno@suse.de>
---
grub-core/osdep/unix/platform.c | 24 ++++++++++++++++++++----
1 file changed, 20 insertions(+), 4 deletions(-)
diff --git a/grub-core/osdep/unix/platform.c b/grub-core/osdep/unix/platform.c
index de712211cf09..a5bf064b7cf5 100644
--- a/grub-core/osdep/unix/platform.c
+++ b/grub-core/osdep/unix/platform.c
@@ -26,8 +26,25 @@
#include <grub/emu/exec.h>
#include <sys/types.h>
#include <dirent.h>
+#include <stdio.h>
#include <string.h>
#include <errno.h>
+#include <unistd.h>
+
+static int
+linux_kmod_load (const char *name)
+{
+#ifdef __linux__
+ char path[64];
+ if (snprintf (path, sizeof(path), "/sys/module/%s", name) >= sizeof(path))
+ grub_util_error (_("module name `%s' too long"), name);
+ if (!access (path, F_OK))
+ return 0;
+ return grub_util_exec ((const char * []){ "modprobe", "-q", name, NULL });
+#else
+ return 0;
+#endif
+}
static char *
get_ofpathname (const char *dev)
@@ -149,10 +166,9 @@ grub_install_register_efi (grub_device_t efidir_grub_dev,
grub_util_error (_("%s: not found"), "efibootmgr");
}
- /* On Linux, we need the efivars kernel modules. */
-#ifdef __linux__
- grub_util_exec ((const char * []){ "modprobe", "-q", "efivars", NULL });
-#endif
+ /* On Linux, we need efivarfs (or the legacy efivars). */
+ linux_kmod_load ("efivars");
+
/* Delete old entries from the same distributor. */
ret = grub_install_remove_efi_entries_by_distributor (efi_distributor);
if (ret)
--
2.37.1