chiark / gitweb /
efi: unify BootXXXX reading
authorKay Sievers <kay@vrfy.org>
Wed, 13 Feb 2013 16:34:57 +0000 (17:34 +0100)
committerKay Sievers <kay@vrfy.org>
Wed, 13 Feb 2013 16:35:20 +0000 (17:35 +0100)
src/boot/boot-efi.c
src/boot/boot-loader.c
src/shared/efivars.c

index ac5f9c12a76e76755f1488db8b3e9233c9a0edfa..a0305ba77dc1afae99abe2af55ed3048dfd6b92a 100644 (file)
 #include "conf-files.h"
 
 static int get_boot_entries(struct boot_info *info) {
-        DIR *d = NULL;
-        struct dirent *dent;
+        uint16_t *list;
+        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;
 
@@ -66,15 +60,15 @@ static int get_boot_entries(struct boot_info *info) {
                 memset(e, 0, sizeof(struct boot_info_entry));
                 e->order = -1;
 
-                err = efi_get_boot_option(id, &e->title, &e->part_uuid, &e->path);
+                err = efi_get_boot_option(list[i], &e->title, &e->part_uuid, &e->path);
                 if (err < 0)
-                        break;
-                e->id = id;
+                        continue;
 
+                e->id = list[i];
                 info->fw_entries_count++;
         }
-        closedir(d);
 
+        free(list);
         return err;
 }
 
index bd563da226f1415ad5c7b310ea14b0571ed2cf01..d1a8b0320b511eec322891df7b03aa39fc0929a2 100644 (file)
@@ -104,6 +104,7 @@ int boot_loader_read_entries(struct boot_info *info) {
                 }
                 info->loader_entries_count++;
         }
+
         return 0;
 }
 
index 70b6ce1cc707c7b8c7768ca4bd6e35dd4d9b1301..7918c59613cda0682d6633a50a6cdc459cec2e4c 100644 (file)
@@ -288,6 +288,21 @@ int efi_get_boot_order(uint16_t **order) {
         return (int) (l / sizeof(uint16_t));
 }
 
+static int boot_id_hex(const char s[4]) {
+        int i;
+        int id = 0;
+
+        for (i = 0; i < 4; i++)
+                if (s[i] >= '0' && s[i] <= '9')
+                        id |= (s[i] - '0') << (3 - i) * 4;
+                else if (s[i] >= 'A' && s[i] <= 'F')
+                        id |= (s[i] - 'A' + 10) << (3 - i) * 4;
+                else
+                        return -1;
+
+        return id;
+}
+
 int efi_get_boot_options(uint16_t **options) {
         _cleanup_closedir_ DIR *dir = NULL;
         struct dirent *de;
@@ -301,26 +316,20 @@ int efi_get_boot_options(uint16_t **options) {
                 return -errno;
 
         while ((de = readdir(dir))) {
-                size_t n;
-                int a, b, c, d;
+                int id;
                 uint16_t *t;
 
                 if (strncmp(de->d_name, "Boot", 4) != 0)
                         continue;
 
-                n = strlen(de->d_name);
-                if (n != 45)
+                if (strlen(de->d_name) != 45)
                         continue;
 
                 if (strcmp(de->d_name + 8, "-8be4df61-93ca-11d2-aa0d-00e098032b8c") != 0)
                         continue;
 
-                a = de->d_name[4];
-                b = de->d_name[5];
-                c = de->d_name[6];
-                d = de->d_name[7];
-
-                if (!isdigit(a) || !isdigit(b) || !isdigit(c) || !isdigit(d))
+                id = boot_id_hex(de->d_name + 4);
+                if (id < 0)
                         continue;
 
                 t = realloc(list, (count + 1) * sizeof(uint16_t));
@@ -330,7 +339,7 @@ int efi_get_boot_options(uint16_t **options) {
                 }
 
                 list = t;
-                list[count ++] = (a - '0') * 1000 + (b - '0') * 100 + (c - '0') * 10 + (d - '0');
+                list[count ++] = id;
 
         }