chiark / gitweb /
nspawn: add --image= switch to boot GPT disk images that follow the Discoverable...
[elogind.git] / src / boot / boot-efi.c
index 32cb973cf662eff2c1cf5589dc162fff6869a1a9..bd0c59bd5aef936d95507eff0b77c7d6acc31c47 100644 (file)
 #include "efivars.h"
 #include "conf-files.h"
 
+static char *tilt_slashes(char *s) {
+        char *p;
+
+        if (!s)
+                return NULL;
+
+        for (p = s; *p; p++)
+                if (*p == '\\')
+                        *p = '/';
+        return s;
+}
+
 static int get_boot_entries(struct boot_info *info) {
-        DIR *d = NULL;
-        struct dirent *dent;
+        uint16_t *list = NULL;
+        int i, n;
         int err = 0;
 
-        d = opendir("/sys/firmware/efi/efivars");
-        if (!d)
-                return -errno;
+        n = efi_get_boot_options(&list);
+        if (n < 0)
+                return n;
 
-        for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
-                unsigned int id;
+        for (i = 0; i < n; i++) {
                 struct boot_info_entry *e;
 
-                if (dent->d_name[0] == '.')
-                        continue;
-                if (sscanf(dent->d_name, "Boot%04X-8be4df61-93ca-11d2-aa0d-00e098032b8c", &id) != 1)
-                        continue;
-
                 e = realloc(info->fw_entries, (info->fw_entries_count+1) * sizeof(struct boot_info_entry));
                 if (!e) {
                         err = -ENOMEM;
-                        break;
+                                break;
                 }
                 info->fw_entries = e;
 
                 e = &info->fw_entries[info->fw_entries_count];
-                memset(e, 0, sizeof(struct boot_info_entry));
+                memzero(e, sizeof(struct boot_info_entry));
                 e->order = -1;
 
-                err = efi_get_boot_option(id, NULL, &e->title, &e->part_uuid, &e->path, &e->data, &e->data_size);
+                err = efi_get_boot_option(list[i], &e->title, &e->part_uuid, &e->path);
                 if (err < 0)
-                        break;
-                e->id = id;
+                        continue;
 
+                if (isempty(e->title)) {
+                        free(e->title);
+                        e->title = NULL;
+                }
+                tilt_slashes(e->path);
+
+                e->id = list[i];
                 info->fw_entries_count++;
         }
-        closedir(d);
 
+        free(list);
         return err;
 }
 
@@ -83,7 +95,7 @@ static int find_active_entry(struct boot_info *info) {
         void *buf;
         size_t l;
         size_t i;
-        int err = -ENOENT;
+        int err;
 
         err = efi_get_variable(EFI_VENDOR_GLOBAL, "BootCurrent", NULL, &buf, &l);
         if (err < 0)
@@ -103,11 +115,13 @@ static int find_active_entry(struct boot_info *info) {
 
 static int get_boot_order(struct boot_info *info) {
         size_t i, k;
-        int err;
+        int r;
 
-        err = efi_get_boot_order(&info->fw_entries_order, &info->fw_entries_order_count);
-        if (err < 0)
-                return err;
+        r = efi_get_boot_order(&info->fw_entries_order);
+        if (r < 0)
+                return r;
+
+        info->fw_entries_order_count = r;
 
         for (i = 0; i < info->fw_entries_order_count; i++) {
                 for (k = 0; k < info->fw_entries_count; k++) {
@@ -142,9 +156,12 @@ static int entry_cmp(const void *a, const void *b) {
 int boot_info_query(struct boot_info *info) {
         char str[64];
         char buf[64];
-        char *loader_active;
+        char *loader_active = NULL;
 
-        info->loader = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo");
+        info->fw_secure_boot = is_efi_secure_boot();
+        info->fw_secure_boot_setup_mode = is_efi_secure_boot_setup_mode();
+
+        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderInfo", &info->loader);
 
         get_boot_entries(info);
         if (info->fw_entries_count > 0) {
@@ -153,19 +170,21 @@ int boot_info_query(struct boot_info *info) {
                 find_active_entry(info);
         }
 
-        info->fw_type = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareType");
-        info->fw_info = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareInfo");
-        info->loader_image_path = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderImageIdentifier");
-        efi_get_loader_device_part_uuid(&info->loader_part_uuid);
+        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareType", &info->fw_type);
+        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderFirmwareInfo", &info->fw_info);
+        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderImageIdentifier", &info->loader_image_path);
+        tilt_slashes(info->loader_image_path);
+        efi_loader_get_device_part_uuid(&info->loader_part_uuid);
 
         boot_loader_read_entries(info);
-        loader_active = efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntrySelected");
+        efi_get_variable_string(EFI_VENDOR_LOADER, "LoaderEntrySelected", &loader_active);
         if (loader_active) {
                 boot_loader_find_active_entry(info, loader_active);
                 free(loader_active);
         }
 
         snprintf(str, sizeof(str), "LoaderEntryOptions-%s", sd_id128_to_string(info->machine_id, buf));
-        info->loader_options_added = efi_get_variable_string(EFI_VENDOR_LOADER, str);
+        efi_get_variable_string(EFI_VENDOR_LOADER, str, &info->loader_options_added);
+
         return 0;
 }