]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
disk: efi: Move logic to get a GPT entry into a helper function
authorJavier Martinez Canillas <javierm@redhat.com>
Thu, 19 Jun 2025 08:33:59 +0000 (10:33 +0200)
committerIlias Apalodimas <ilias.apalodimas@linaro.org>
Thu, 3 Jul 2025 08:32:49 +0000 (11:32 +0300)
Factor out the logic to get the Partition Table Entry (PTE) of a given
partition into a helper function, since it could be used by other code.

Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Reviewed-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
Signed-off-by: Ilias Apalodimas <ilias.apalodimas@linaro.org>
disk/part_efi.c

index 68ba1d11e7b67ffe76bbc5e1bec03b649bf927e3..d0b1f96adff2094ac164a1118fc27c08294af0f7 100644 (file)
@@ -215,6 +215,34 @@ int get_disk_guid(struct blk_desc *desc, char *guid)
        return 0;
 }
 
+static int part_get_gpt_pte(struct blk_desc *desc, int part, gpt_entry *gpt_e)
+{
+       ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
+       gpt_entry *gpt_pte = NULL;
+
+       /* "part" argument must be at least 1 */
+       if (part < 1) {
+               log_debug("Invalid Argument(s)\n");
+               return -EINVAL;
+       }
+
+       /* This function validates AND fills in the GPT header and PTE */
+       if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
+               return -EINVAL;
+
+       if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
+           !is_pte_valid(&gpt_pte[part - 1])) {
+               log_debug("Invalid partition number %d\n", part);
+               free(gpt_pte);
+               return -EPERM;
+       }
+
+       memcpy(gpt_e, &gpt_pte[part - 1], sizeof(*gpt_e));
+
+       free(gpt_pte);
+       return 0;
+}
+
 static void __maybe_unused part_print_efi(struct blk_desc *desc)
 {
        ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
@@ -260,45 +288,32 @@ static void __maybe_unused part_print_efi(struct blk_desc *desc)
 static int __maybe_unused part_get_info_efi(struct blk_desc *desc, int part,
                                            struct disk_partition *info)
 {
-       ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
-       gpt_entry *gpt_pte = NULL;
-
-       /* "part" argument must be at least 1 */
-       if (part < 1) {
-               log_debug("Invalid Argument(s)\n");
-               return -EINVAL;
-       }
-
-       /* This function validates AND fills in the GPT header and PTE */
-       if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
-               return -EINVAL;
+       gpt_entry gpt_pte = {};
+       int ret;
 
-       if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
-           !is_pte_valid(&gpt_pte[part - 1])) {
-               log_debug("Invalid partition number %d\n", part);
-               free(gpt_pte);
-               return -EPERM;
-       }
+       ret = part_get_gpt_pte(desc, part, &gpt_pte);
+       if (ret)
+               return ret;
 
        /* The 'lbaint_t' casting may limit the maximum disk size to 2 TB */
-       info->start = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].starting_lba);
+       info->start = (lbaint_t)le64_to_cpu(gpt_pte.starting_lba);
        /* The ending LBA is inclusive, to calculate size, add 1 to it */
-       info->size = (lbaint_t)le64_to_cpu(gpt_pte[part - 1].ending_lba) + 1
+       info->size = (lbaint_t)le64_to_cpu(gpt_pte.ending_lba) + 1
                     - info->start;
        info->blksz = desc->blksz;
 
        snprintf((char *)info->name, sizeof(info->name), "%s",
-                print_efiname(&gpt_pte[part - 1]));
+                print_efiname(&gpt_pte));
        strcpy((char *)info->type, "U-Boot");
-       info->bootable = get_bootable(&gpt_pte[part - 1]);
-       info->type_flags = gpt_pte[part - 1].attributes.fields.type_guid_specific;
+       info->bootable = get_bootable(&gpt_pte);
+       info->type_flags = gpt_pte.attributes.fields.type_guid_specific;
        if (CONFIG_IS_ENABLED(PARTITION_UUIDS)) {
-               uuid_bin_to_str(gpt_pte[part - 1].unique_partition_guid.b,
+               uuid_bin_to_str(gpt_pte.unique_partition_guid.b,
                                (char *)disk_partition_uuid(info),
                                UUID_STR_FORMAT_GUID);
        }
        if (IS_ENABLED(CONFIG_PARTITION_TYPE_GUID)) {
-               uuid_bin_to_str(gpt_pte[part - 1].partition_type_guid.b,
+               uuid_bin_to_str(gpt_pte.partition_type_guid.b,
                                (char *)disk_partition_type_guid(info),
                                UUID_STR_FORMAT_GUID);
        }
@@ -306,8 +321,6 @@ static int __maybe_unused part_get_info_efi(struct blk_desc *desc, int part,
        log_debug("start 0x" LBAF ", size 0x" LBAF ", name %s\n", info->start,
                  info->size, info->name);
 
-       /* Remember to free pte */
-       free(gpt_pte);
        return 0;
 }