[PATCH 1/1] efivar: check that efivarfs is writeable
authorHeinrich Schuchardt <heinrich.schuchardt@canonical.com>
Fri, 18 Mar 2022 14:21:33 +0000 (15:21 +0100)
committerPeter Michael Green <plugwash@raspbian.org>
Sun, 28 Jul 2024 22:42:11 +0000 (22:42 +0000)
Some UEFI implementations (notably U-Boot) don't implement the
SetVariable() runtime service. On these systems the GRUB installation
must be completed manually. Write a warning in this case but avoid
throwing an error.  (LP: #1965288)

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
Gbp-Pq: Name efivar-check-that-efivarfs-is-writeable.patch

grub-core/osdep/unix/efivar.c

index 4a58328b42a5d331255a218004a71335fbede8d5..b53f32d76909f89d457ac462cb60cb3b6ce24006 100644 (file)
 #include <grub/list.h>
 #include <grub/misc.h>
 #include <grub/emu/exec.h>
+#include <linux/magic.h>
 #include <sys/types.h>
+#include <sys/vfs.h>
+#include <sys/statvfs.h>
 #include <ctype.h>
 #include <errno.h>
 #include <stdlib.h>
@@ -398,6 +401,29 @@ err:
   return NULL;
 }
 
+/**
+ * efivar_is_rw - detect if the efivar file system exists and is writeable
+ *
+ * Return:     true if efivarfs is writeable
+ */
+static bool
+efivar_is_rw (void)
+{
+  int ret;
+  const char *mount = "/sys/firmware/efi/efivars";
+  struct statfs stat;
+
+  ret = statfs(mount, &stat);
+  if (ret == -1)
+    return false;
+  if (stat.f_type != EFIVARFS_MAGIC)
+      return false;
+  if (stat.f_flags & ST_RDONLY)
+    return false;
+
+  return true;
+}
+
 int
 grub_install_efivar_register_efi (grub_device_t efidir_grub_dev,
                                  const char *efifile_path,
@@ -410,6 +436,14 @@ grub_install_efivar_register_efi (grub_device_t efidir_grub_dev,
   int entry_num = -1;
   int rc;
 
+  /* Check if EFI variable can be written */
+  if (!efivar_is_rw ())
+    {
+      grub_util_warn ("EFI variables cannot be set on this system");
+      grub_util_warn ("You will have to complete the GRUB setup manually");
+      return 0;
+    }
+
   efidir_disk = grub_util_biosdisk_get_osdev (efidir_grub_dev->disk);
   efidir_part = efidir_grub_dev->disk->partition ? efidir_grub_dev->disk->partition->number + 1 : 1;