Resolves a regression from commit b23816ab9626 where efibootguard would
skip config file probing for an entire device upon encountering a
partition with an indeterminable FAT bit size.

Additionally, simplified the implementation and added documentation for
clarity.

Fixes: b23816ab9626 ("fix: correctly calculate FAT bit size based on cluster 
count")
Reported-by: Felix Moessbauer <[email protected]>
Signed-off-by: Michael Adler <[email protected]>
---
 tools/ebgpart.c | 69 ++++++++++++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 30 deletions(-)

diff --git a/tools/ebgpart.c b/tools/ebgpart.c
index fc754df..9a9e179 100644
--- a/tools/ebgpart.c
+++ b/tools/ebgpart.c
@@ -44,7 +44,7 @@ static void add_block_dev(PedDevice *dev)
        d->next = dev;
 }
 
-static char *GUID_to_str(uint8_t *g)
+static char *GUID_to_str(const uint8_t *g)
 {
        (void)snprintf(buffer, 37,
                       "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-"
@@ -73,26 +73,35 @@ static char *type_to_name(char t)
        return "not supported";
 }
 
-static bool check_GPT_FAT_entry(int fd, struct EFIpartitionentry *e,
-                               PedFileSystemType *pfst, uint32_t i)
+/**
+ * @brief Determines the FAT bit size of a disk or partition.
+ *
+ * @param fd        File descriptor to inspect.
+ * @param e         A pointer to the EFI partition entry.
+ * @param fat_size  A pointer where the detected FAT bit size will be stored.
+ *
+ * @return 0 if the FAT bit size is successfully determined and stored in
+ * `fat_size`; otherwise, returns a non-zero error code.
+ */
+static int check_GPT_FAT_entry(int fd, const struct EFIpartitionentry *e,
+                              int *fat_size)
 {
        char *guid_str = GUID_to_str(e->type_GUID);
        if (strcmp(GPT_PARTITION_GUID_FAT_NTFS, guid_str) != 0 &&
            strcmp(GPT_PARTITION_GUID_ESP, guid_str) != 0) {
-               if (asprintf(&pfst->name, "%s", "not supported") == -1) {
-                       goto error_asprintf;
-               }
                VERBOSE(stderr, "GPT entry has unsupported GUID: %s\n",
                        guid_str);
-               return true;
+               *fat_size = 0;
+               return 0;
        }
-       VERBOSE(stdout, "GPT Partition #%u is FAT/NTFS.\n", i);
+       VERBOSE(stdout, "GPT Partition has a FAT/NTFS GUID\n");
+
        /* Save current file offset */
        off64_t curr = lseek64(fd, 0, SEEK_CUR);
        if (curr == -1) {
                VERBOSE(stderr, "Error getting current seek position: %s\n",
                        strerror(errno));
-               return false;
+               return -1;
        }
 
        /* seek to partition start */
@@ -100,7 +109,7 @@ static bool check_GPT_FAT_entry(int fd, struct 
EFIpartitionentry *e,
        if (lseek64(fd, offset_start, SEEK_SET) == -1) {
                VERBOSE(stderr, "Error seeking to partition start: %s\n",
                        strerror(errno));
-               return false;
+               return -1;
        }
 
        /* read FAT header */
@@ -108,32 +117,17 @@ static bool check_GPT_FAT_entry(int fd, struct 
EFIpartitionentry *e,
        if (read(fd, &header, sizeof(header)) != sizeof(header)) {
                VERBOSE(stderr, "Error reading FAT header: %s\n",
                        strerror(errno));
-               return false;
+               return -1;
        }
 
        /* restore pos */
        if (lseek64(fd, curr, SEEK_SET) == -1) {
                VERBOSE(stderr, "Error restoring seek position (%s)",
                        strerror(errno));
-               return false;
-       }
-
-       int fat_bits = determine_FAT_bits(&header);
-       if (fat_bits <= 0) {
-               /* not a FAT header */
-               return false;
-       }
-       if (asprintf(&pfst->name, "fat%d", fat_bits) == -1) {
-               VERBOSE(stderr,
-                       "Error in asprintf - possibly out of memory.\n");
-               return false;
+               return -1;
        }
-       VERBOSE(stdout, "GPT Partition #%u is %s.\n", i, pfst->name);
-       return true;
-
-error_asprintf:
-       VERBOSE(stderr, "Error in asprintf - possibly out of memory.\n");
-       return false;
+       *fat_size = determine_FAT_bits(&header);
+       return 0;
 }
 
 static void read_GPT_entries(int fd, uint64_t table_LBA, uint32_t num,
@@ -178,13 +172,28 @@ static void read_GPT_entries(int fd, uint64_t table_LBA, 
uint32_t num,
                tmpp->num = i + 1;
                tmpp->fs_type = pfst;
 
-               if (!check_GPT_FAT_entry(fd, &e, pfst, i)) {
+               int fat_size = 0;
+               if (check_GPT_FAT_entry(fd, &e, &fat_size)) {
+                       VERBOSE(stderr,
+                               "%u: error, skipping partitions on device\n",
+                               i);
                        free(pfst->name);
                        free(pfst);
                        free(tmpp);
                        dev->part_list = NULL;
                        continue;
                }
+               if (fat_size > 0) {
+                       VERBOSE(stdout, "GPT Partition #%u is fat%d.\n", i,
+                               fat_size);
+                       if (asprintf(&pfst->name, "fat%d", fat_size) == -1) {
+                               VERBOSE(stderr, "Error in asprintf - possibly "
+                                               "out of memory.\n");
+                               return;
+                       }
+               } else {
+                       VERBOSE(stderr, "%u: not a FAT partition\n", i);
+               }
 
                *list_end = tmpp;
                list_end = &((*list_end)->next);
-- 
2.42.0

-- 
You received this message because you are subscribed to the Google Groups "EFI 
Boot Guard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/efibootguard-dev/20231019094632.232381-2-michael.adler%40siemens.com.

Reply via email to