]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - disk/part_efi.c
disk: part_efi: fix regression due to incorrect buffer usage
[karo-tx-uboot.git] / disk / part_efi.c
index cc188ee8991f35f1d731cccf49578d2fa88ab9d4..ddf80a73d2f1bf27dc4ad75dc46396a5a2d2ed40 100644 (file)
 #include <ide.h>
 #include <malloc.h>
 #include "part_efi.h"
+#include <linux/ctype.h>
 
-#if (defined(CONFIG_CMD_IDE) || \
-     defined(CONFIG_CMD_SATA) || \
-     defined(CONFIG_CMD_SCSI) || \
-     defined(CONFIG_CMD_USB) || \
-     defined(CONFIG_MMC) || \
-     defined(CONFIG_SYSTEMACE) ) && defined(CONFIG_EFI_PARTITION)
+#if defined(CONFIG_CMD_IDE) || \
+    defined(CONFIG_CMD_MG_DISK) || \
+    defined(CONFIG_CMD_SATA) || \
+    defined(CONFIG_CMD_SCSI) || \
+    defined(CONFIG_CMD_USB) || \
+    defined(CONFIG_MMC) || \
+    defined(CONFIG_SYSTEMACE)
 
 /* Convert char[2] in little endian format to the host format integer
  */
@@ -98,95 +100,106 @@ static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
 
 static int is_pte_valid(gpt_entry * pte);
 
+static char *print_efiname(gpt_entry *pte)
+{
+       static char name[PARTNAME_SZ + 1];
+       int i;
+       for (i = 0; i < PARTNAME_SZ; i++) {
+               u8 c;
+               c = pte->partition_name[i] & 0xff;
+               c = (c && !isprint(c)) ? '.' : c;
+               name[i] = c;
+       }
+       name[PARTNAME_SZ] = 0;
+       return name;
+}
+
 /*
  * Public Functions (include/part.h)
  */
 
 void print_part_efi(block_dev_desc_t * dev_desc)
 {
-       gpt_header gpt_head;
-       gpt_entry **pgpt_pte = NULL;
+       ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
+       gpt_entry *gpt_pte = NULL;
        int i = 0;
 
        if (!dev_desc) {
-               printf("%s: Invalid Argument(s)\n", __FUNCTION__);
+               printf("%s: Invalid Argument(s)\n", __func__);
                return;
        }
        /* This function validates AND fills in the GPT header and PTE */
        if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
-                        &(gpt_head), pgpt_pte) != 1) {
-               printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
+                        gpt_head, &gpt_pte) != 1) {
+               printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
                return;
        }
 
-       debug("%s: gpt-entry at 0x%08X\n", __FUNCTION__, (unsigned int)*pgpt_pte);
+       debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
 
-       printf("Part  Start LBA  End LBA\n");
-       for (i = 0; i < le32_to_int(gpt_head.num_partition_entries); i++) {
+       printf("Part\tName\t\t\tStart LBA\tEnd LBA\n");
+       for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) {
 
-               if (is_pte_valid(&(*pgpt_pte)[i])) {
-                       printf("%s%d  0x%llX    0x%llX\n", GPT_ENTRY_NAME,
-                               (i + 1),
-                               le64_to_int((*pgpt_pte)[i].starting_lba),
-                               le64_to_int((*pgpt_pte)[i].ending_lba));
+               if (is_pte_valid(&gpt_pte[i])) {
+                       printf("%3d\t%-18s\t0x%08llX\t0x%08llX\n", (i + 1),
+                               print_efiname(&gpt_pte[i]),
+                               le64_to_int(gpt_pte[i].starting_lba),
+                               le64_to_int(gpt_pte[i].ending_lba));
                } else {
                        break;  /* Stop at the first non valid PTE */
                }
        }
 
        /* Remember to free pte */
-       if (*pgpt_pte != NULL) {
-               debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
-               free(*pgpt_pte);
-       }
+       free(gpt_pte);
        return;
 }
 
 int get_partition_info_efi(block_dev_desc_t * dev_desc, int part,
                                disk_partition_t * info)
 {
-       gpt_header gpt_head;
-       gpt_entry **pgpt_pte = NULL;
+       ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1);
+       gpt_entry *gpt_pte = NULL;
 
        /* "part" argument must be at least 1 */
        if (!dev_desc || !info || part < 1) {
-               printf("%s: Invalid Argument(s)\n", __FUNCTION__);
+               printf("%s: Invalid Argument(s)\n", __func__);
                return -1;
        }
 
        /* This function validates AND fills in the GPT header and PTE */
        if (is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
-                       &(gpt_head), pgpt_pte) != 1) {
-               printf("%s: *** ERROR: Invalid GPT ***\n", __FUNCTION__);
+                       gpt_head, &gpt_pte) != 1) {
+               printf("%s: *** ERROR: Invalid GPT ***\n", __func__);
                return -1;
        }
 
        /* The ulong casting limits the maximum disk size to 2 TB */
-       info->start = (ulong) le64_to_int((*pgpt_pte)[part - 1].starting_lba);
-       info->size = (ulong) le64_to_int((*pgpt_pte)[part - 1].ending_lba) - info->start;
+       info->start = (ulong) le64_to_int(gpt_pte[part - 1].starting_lba);
+       /* The ending LBA is inclusive, to calculate size, add 1 to it */
+       info->size = ((ulong)le64_to_int(gpt_pte[part - 1].ending_lba) + 1)
+                    - info->start;
        info->blksz = GPT_BLOCK_SIZE;
 
-       sprintf((char *)info->name, "%s%d\n", GPT_ENTRY_NAME, part);
+       sprintf((char *)info->name, "%s",
+                       print_efiname(&gpt_pte[part - 1]));
        sprintf((char *)info->type, "U-Boot");
 
-       debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__,
+       debug("%s: start 0x%lX, size 0x%lX, name %s", __func__,
                info->start, info->size, info->name);
 
        /* Remember to free pte */
-       if (*pgpt_pte != NULL) {
-               debug("%s: Freeing pgpt_pte\n", __FUNCTION__);
-               free(*pgpt_pte);
-       }
+       free(gpt_pte);
        return 0;
 }
 
 int test_part_efi(block_dev_desc_t * dev_desc)
 {
-       legacy_mbr legacymbr;
+       ALLOC_CACHE_ALIGN_BUFFER(legacy_mbr, legacymbr, 1);
 
        /* Read legacy MBR from block 0 and validate it */
-       if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *) & legacymbr) != 1)
-               || (is_pmbr_valid(&legacymbr) != 1)) {
+       if ((dev_desc->block_read(dev_desc->dev, 0, 1, (ulong *)legacymbr) != 1)
+               || (is_pmbr_valid(legacymbr) != 1)) {
                return -1;
        }
        return 0;
@@ -252,7 +265,7 @@ static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
        unsigned long long lastlba;
 
        if (!dev_desc || !pgpt_head) {
-               printf("%s: Invalid Argument(s)\n", __FUNCTION__);
+               printf("%s: Invalid Argument(s)\n", __func__);
                return 0;
        }
 
@@ -330,9 +343,7 @@ static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba,
                        le32_to_int(pgpt_head->partition_entry_array_crc32),
                        calc_crc32);
 
-               if (*pgpt_pte != NULL) {
-                       free(*pgpt_pte);
-               }
+               free(*pgpt_pte);
                return 0;
        }
 
@@ -356,25 +367,25 @@ static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc,
        gpt_entry *pte = NULL;
 
        if (!dev_desc || !pgpt_head) {
-               printf("%s: Invalid Argument(s)\n", __FUNCTION__);
+               printf("%s: Invalid Argument(s)\n", __func__);
                return NULL;
        }
 
        count = le32_to_int(pgpt_head->num_partition_entries) *
                le32_to_int(pgpt_head->sizeof_partition_entry);
 
-       debug("%s: count = %lu * %lu = %u\n", __FUNCTION__,
+       debug("%s: count = %lu * %lu = %u\n", __func__,
                le32_to_int(pgpt_head->num_partition_entries),
                le32_to_int(pgpt_head->sizeof_partition_entry), count);
 
        /* Allocate memory for PTE, remember to FREE */
        if (count != 0) {
-               pte = malloc(count);
+               pte = memalign(CONFIG_SYS_CACHELINE_SIZE, count);
        }
 
        if (count == 0 || pte == NULL) {
                printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n",
-                       __FUNCTION__, count);
+                       __func__, count);
                return NULL;
        }
 
@@ -402,7 +413,7 @@ static int is_pte_valid(gpt_entry * pte)
        efi_guid_t unused_guid;
 
        if (!pte) {
-               printf("%s: Invalid Argument(s)\n", __FUNCTION__);
+               printf("%s: Invalid Argument(s)\n", __func__);
                return 0;
        }
 
@@ -414,7 +425,7 @@ static int is_pte_valid(gpt_entry * pte)
        if (memcmp(pte->partition_type_guid.b, unused_guid.b,
                sizeof(unused_guid.b)) == 0) {
 
-               debug("%s: Found an unused PTE GUID at 0x%08X\n", __FUNCTION__,
+               debug("%s: Found an unused PTE GUID at 0x%08X\n", __func__,
                (unsigned int)pte);
 
                return 0;