#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>
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,
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;