/* Print a partition that is relative to its Extended partition table */ static void print_partition_extended(block_dev_desc_t *dev_desc, int ext_part_sector, int relative, int part_num, unsigned int disksig) { ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz); dos_partition_t *pt; int i; if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { printf ("** Can't read partition table on %d:%d **\n", dev_desc->dev, ext_part_sector); return; } i=test_block_type(buffer); if (i != DOS_MBR) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return; } if (!ext_part_sector) disksig = le32_to_int(&buffer[DOS_PART_DISKSIG_OFFSET]); /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* * fdisk does not show the extended partitions that * are not in the MBR */ if ((pt->sys_ind != 0) && (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { print_one_part(pt, ext_part_sector, part_num, disksig); } /* Reverse engr the fdisk part# assignment rule! */ if ((ext_part_sector == 0) || (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { part_num++; } } /* Follows the extended partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if (is_extended (pt->sys_ind)) { int lba_start = le32_to_int (pt->start4) + relative; print_partition_extended(dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, part_num, disksig); } } return; }
static void print_one_part (dos_partition_t *p, int ext_part_sector, int part_num) { int lba_start = ext_part_sector + le32_to_int (p->start4); int lba_size = le32_to_int (p->size4); printf ("%5d\t\t%10d\t%10d\t%2x%s\n", part_num, lba_start, lba_size, p->sys_ind, (is_extended (p->sys_ind) ? " Extd" : "")); }
int get_partition_info_efi_with_partnum(block_dev_desc_t * dev_desc, int part, disk_partition_t * info, unsigned long total, unsigned long sdidx, int sdpart, disk_partition_t *sdinfo) { gpt_header gpt_head; gpt_entry *pgpt_pte = NULL; /* "part" argument must be at least 1 */ if (!dev_desc || !info || part < 1) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); 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 Main GPT ***\n", __FUNCTION__); if(is_gpt_valid(dev_desc, dev_desc->lba -1, &(gpt_head), &pgpt_pte) != 1){ printf("%s: *** ERROR: Invalid alternate GPT ***\n", __FUNCTION__); 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); /* The ending LBA is inclusive, to calculate size, add 1 to it */ info->size = ((ulong)le64_to_int((pgpt_pte)[part - 1].ending_lba) + 1) - info->start; info->blksz = GPT_BLOCK_SIZE; sprintf((char *)info->name, "%s%d", GPT_ENTRY_NAME, part); sprintf((char *)info->type, "U-Boot"); debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__, info->start, info->size, info->name); /* copy sd info */ if (sdidx < (le32_to_int(gpt_head.num_partition_entries))) { sdinfo->start = (ulong) le64_to_int((pgpt_pte)[sdpart - 1].starting_lba); sdinfo->size = ((ulong)le64_to_int((pgpt_pte)[sdpart - 1].ending_lba) + 1) - sdinfo->start; } /* Remember to free pte */ if (pgpt_pte != NULL) { debug("%s: Freeing pgpt_pte\n", __FUNCTION__); free(pgpt_pte); } if (total == 0) return 0; else if (total != le32_to_int(gpt_head.num_partition_entries)) return -1; else return 0; }
static void print_one_part(dos_partition_t *p, int ext_part_sector, int part_num, unsigned int disksig) { int lba_start = ext_part_sector + le32_to_int (p->start4); int lba_size = le32_to_int (p->size4); printf("%3d\t%-10d\t%-10d\t%08x-%02x\t%02x%s%s\n", part_num, lba_start, lba_size, disksig, part_num, p->sys_ind, (is_extended(p->sys_ind) ? " Extd" : ""), (is_bootable(p) ? " Boot" : "")); }
unsigned int _gen_gpt(gpt_header *g_header,PARTITION_CFG *p_partition_cfg) { printf("write gpt header \n"); int i = 0 ,gpt_partition_number = 0; unsigned long crc = 0; _parser_cfg(&gpt_partition_number,p_partition_cfg); *(unsigned long long int*)g_header->signature = PED_CPU_TO_LE64 (GPT_HEADER_SIGNATURE); *(unsigned long int*)g_header->revision = PED_CPU_TO_LE32 (GPT_HEADER_REVISION_V1); *(unsigned long int*)g_header->header_size = PED_CPU_TO_LE32(92UL); *(unsigned long int*)g_header->reserved1 = PED_CPU_TO_LE32(0); *(unsigned long long int*)g_header->my_lba = PED_CPU_TO_LE64 (1); *(unsigned long long int*)g_header->alternate_lba = PED_CPU_TO_LE64 (emmc_part_device.total_sector-1); *(unsigned long long int*)g_header->first_usable_lba = PED_CPU_TO_LE64 (MAX_PARTITION_INFO/4 + 2); *(unsigned long long int*)g_header->last_usable_lba = PED_CPU_TO_LE64 (emmc_part_device.total_sector-2-MAX_PARTITION_INFO/4); g_header->disk_guid = _gen_guid(0); *(unsigned long long int*)g_header->partition_entry_lba = PED_CPU_TO_LE64 (2); *(unsigned long int*)g_header->num_partition_entries = PED_CPU_TO_LE32(gpt_partition_number); *(unsigned long int*)g_header->sizeof_partition_entry = PED_CPU_TO_LE32(sizeof(gpt_entry)); memset(g_header->reserved2,0,GPT_BLOCK_SIZE - 92); //_cur_lba_num = MAX_PARTITION_INFO/4 + 2; _cur_lba_num = STARTING_LBA_OF_FIRST_PARTITION; printf("write gpt partition \n"); for(i=0;i<gpt_partition_number;i++) { _gen_gpt_entry(i,&g_gpt_entry_block._gpt_entry[i],p_partition_cfg); } //*(unsigned long int*)g_header->partition_entry_array_crc32 = ; crc = uefi_crc32(&g_gpt_entry_block,(le32_to_int(g_header->num_partition_entries)) *(le32_to_int( g_header->sizeof_partition_entry))); *(unsigned long int*)g_header->partition_entry_array_crc32 = PED_CPU_TO_LE32(crc); //CRC32 check crc = uefi_crc32(g_header,le32_to_int(g_header->header_size)); *(unsigned long int*)g_header->header_crc32 = PED_CPU_TO_LE32(crc); return 1; }
/* Print a partition that is relative to its Extended partition table */ static void print_partition_extended (block_dev_desc_t *dev_desc, int ext_part_sector, int relative, int part_num) { unsigned char tmp_buf[DEFAULT_SECTOR_SIZE]; unsigned char *buffer = KSEG1ADDR(&tmp_buf[0]); dos_partition_t *pt; int i; if (dev_desc->block_read(dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { printf ("** Can't read partition table on %d:%d **\n", dev_desc->dev, ext_part_sector); return; } i=test_block_type(buffer); if(i==-1) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return; } if(i==DOS_PBR) { printf (" 1\t\t 0\t%10ld\t%2x\n", dev_desc->lba, buffer[DOS_PBR_MEDIA_TYPE_OFFSET]); return; } /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* * fdisk does not show the extended partitions that * are not in the MBR */ if ((pt->sys_ind != 0) && (ext_part_sector == 0 || !is_extended (pt->sys_ind)) ) { print_one_part (pt, ext_part_sector, part_num); } /* Reverse engr the fdisk part# assignment rule! */ if ((ext_part_sector == 0) || (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { part_num++; } } /* Follows the extended partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if (is_extended (pt->sys_ind)) { int lba_start = le32_to_int (pt->start4) + relative; print_partition_extended (dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, part_num); } } return; }
/** * alloc_read_gpt_entries(): reads partition entries from disk * @dev_desc * @gpt - GPT header * * Description: Returns ptes on success, NULL on error. * Allocates space for PTEs based on information found in @gpt. * Notes: remember to free pte when you're done! */ static gpt_entry *alloc_read_gpt_entries(block_dev_desc_t * dev_desc, gpt_header * pgpt_head) { size_t count = 0; gpt_entry *pte = NULL; if (!dev_desc || !pgpt_head) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); 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__, le32_to_int(pgpt_head->num_partition_entries), le32_to_int(pgpt_head->sizeof_partition_entry), count); if(count % GPT_BLOCK_SIZE){ size_t ntemp; ntemp = count/GPT_BLOCK_SIZE +1; count = ntemp * GPT_BLOCK_SIZE; } /* Allocate memory for PTE, remember to FREE */ if (count != 0) { pte = malloc(count); } if (count == 0 || pte == NULL) { printf("%s: ERROR: Can't allocate 0x%X bytes for GPT Entries\n", __FUNCTION__, count); return NULL; } /* Read GPT Entries from device */ if (dev_desc->block_read (dev_desc->dev, (unsigned long)le64_to_int(pgpt_head->partition_entry_lba), (lbaint_t) (count / GPT_BLOCK_SIZE), pte) != (count / GPT_BLOCK_SIZE)) { printf("*** ERROR: Can't read GPT Entries ***\n"); free(pte); return NULL; } return pte; }
/* * pmbr_part_valid(): Check for EFI partition signature * * Returns: 1 if EFI GPT partition type is found. */ static int pmbr_part_valid(struct partition *part) { if (part->sys_ind == EFI_PMBR_OSTYPE_EFI_GPT && le32_to_int(part->start_sect) == 1UL) { return 1; } return 0; }
unsigned int _gen_backup_gpt(gpt_header *g_header,PARTITION_CFG *p_partition_cfg) { unsigned long crc = 0; *(unsigned long long int*)g_header->my_lba = PED_CPU_TO_LE64 (emmc_part_device.total_sector-1); *(unsigned long long int*)g_header->alternate_lba = PED_CPU_TO_LE64 (1); *(unsigned long long int*)g_header->partition_entry_lba = PED_CPU_TO_LE64 (emmc_part_device.total_sector-1-MAX_PARTITION_INFO/4); *(unsigned long int*)g_header->header_crc32 = 0; crc = uefi_crc32(g_header,le32_to_int(g_header->header_size)); *(unsigned long int*)g_header->header_crc32 = PED_CPU_TO_LE32(crc); return 1; }
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; /* "part" argument must be at least 1 */ if (!dev_desc || !info || part < 1) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); 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__); return -1; } /* valid entry? */ if (part > le32_to_int(gpt_head.num_partition_entries)) return -1; if (!is_pte_valid(&pgpt_pte[part - 1])) 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); /* The ending LBA is inclusive, to calculate size, add 1 to it */ info->size = ((ulong)le64_to_int(pgpt_pte[part - 1].ending_lba) + 1) - info->start; info->blksz = GPT_BLOCK_SIZE; sprintf((char *)info->name, "%s%d", GPT_ENTRY_NAME, part); if (is_pte_env(&pgpt_pte[part - 1])) sprintf((char *)info->type, BOOT_PART_ENV); else sprintf((char *)info->type, BOOT_PART_TYPE); debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__, info->start, info->size, info->name); /* Remember to free pte */ if (pgpt_pte != NULL) { debug("%s: Freeing pgpt_pte\n", __FUNCTION__); free(pgpt_pte); } return 0; }
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; /* "part" argument must be at least 1 */ if (!dev_desc || !info || part < 1) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); 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__); return -1; } /* "part" argument must be less than the number of partition entries */ if (part > le32_to_int(gpt_head.num_partition_entries)) 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); /* The ending LBA is inclusive, to calculate size, add 1 to it */ info->size = ((ulong)le64_to_int(pgpt_pte[part - 1].ending_lba) + 1) - info->start; info->blksz = GPT_BLOCK_SIZE; unicode2asc(pgpt_pte[part - 1].partition_name, info->name, sizeof(info->name)); memcpy(info->type, &pgpt_pte[part - 1].partition_type_guid, sizeof(efi_guid_t)); memcpy(info->type + sizeof(efi_guid_t), &pgpt_pte[part - 1].unique_partition_guid, sizeof(efi_guid_t)); debug("%s: start 0x%lX, size 0x%lX, name %s", __FUNCTION__, info->start, info->size, info->name); /* Remember to free pte */ if (pgpt_pte != NULL) { debug("%s: Freeing pgpt_pte\n", __FUNCTION__); free(pgpt_pte); } return 0; }
int find_part_efi(block_dev_desc_t * dev_desc, char *name, disk_partition_t * info) { ALLOC_CACHE_ALIGN_BUFFER(gpt_header, gpt_head, 1); gpt_entry *gpt_pte = NULL; int i = 0, pos = 0; /* "part" argument must be at least 1 */ if (!dev_desc || !info ) { 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, &gpt_pte) != 1) { printf("%s: *** ERROR: Invalid Primary GPT ***\n", __func__); if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head, &gpt_pte) != 1) { printf("%s: *** ERROR: Invalid Backup GPT ***\n", __func__); return -1; } } for (i = 0; i < le32_to_int(gpt_head->num_partition_entries); i++) { if (is_pte_valid(&gpt_pte[i]) && !(strcmp(name, print_efiname(&gpt_pte[i])))) { /* The ulong casting limits the maximum disk size to 2 TB */ info->start = (ulong) le64_to_int(gpt_pte[i].starting_lba); /* The ending LBA is inclusive, to calculate size, add 1 to it */ info->size = ((ulong)le64_to_int(gpt_pte[i].ending_lba) + 1) - info->start; info->blksz = GPT_BLOCK_SIZE; sprintf((char *)info->name, "%s", print_efiname(&gpt_pte[i])); sprintf((char *)info->type, "U-Boot"); pos = i + 1; } } /* Remember to free pte */ free(gpt_pte); return pos; }
void print_part_efi(block_dev_desc_t * dev_desc) { gpt_header gpt_head; gpt_entry *pgpt_pte; int i = 0; unsigned char name[ARRAY_SIZE(pgpt_pte->partition_name) + 1]; if (!dev_desc) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); 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__); return; } debug("%s: gpt-entry at 0x%p\n", __func__, pgpt_pte); printf("Part Start LBA End LBA Name\n"); for (i = 0; i < le32_to_int(gpt_head.num_partition_entries); i++) { if (is_pte_valid(&pgpt_pte[i])) { unicode2asc(pgpt_pte[i].partition_name, name, sizeof(name)); printf("%s%d 0x%08llX 0x%08llX %s\n", GPT_ENTRY_NAME, (i + 1), le64_to_int(pgpt_pte[i].starting_lba), le64_to_int(pgpt_pte[i].ending_lba), name); } 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); } return; }
void print_part_efi(block_dev_desc_t * dev_desc) { 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", __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, &gpt_pte) != 1) { printf("%s: *** ERROR: Invalid Primary GPT ***\n", __func__); if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head, &gpt_pte) != 1) { printf("%s: *** ERROR: Invalid Backup GPT ***\n", __func__); return -1; } } debug("%s: gpt-entry at %p\n", __func__, gpt_pte); 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(&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 */ free(gpt_pte); return; }
void print_part_efi(block_dev_desc_t * dev_desc) { gpt_header gpt_head; gpt_entry *pgpt_pte = NULL; int i = 0; if (!dev_desc) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); 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__); return; } debug("%s: gpt-entry at 0x%08X\n", __FUNCTION__, (unsigned int)pgpt_pte); printf("Part Start LBA End 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)); } 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); } return; }
/** * is_gpt_valid() - tests one GPT header and PTEs for validity * * lba is the logical block address of the GPT header to test * gpt is a GPT header ptr, filled on return. * ptes is a PTEs ptr, filled on return. * * Description: returns 1 if valid, 0 on error. * If valid, returns pointers to PTEs. */ static int is_gpt_valid(block_dev_desc_t * dev_desc, unsigned long long lba, gpt_header * pgpt_head, gpt_entry ** pgpt_pte) { unsigned char crc32_backup[4] = { 0 }; unsigned long calc_crc32; unsigned long long lastlba; if (!dev_desc || !pgpt_head) { printf("%s: Invalid Argument(s)\n", __FUNCTION__); return 0; } /* Read GPT Header from device */ if (dev_desc->block_read(dev_desc->dev, lba, 1, pgpt_head) != 1) { printf("*** ERROR: Can't read GPT header ***\n"); return 0; } /* Check the GPT header signature */ if (le64_to_int(pgpt_head->signature) != GPT_HEADER_SIGNATURE) { printf("GUID Partition Table Header signature is wrong:" "0x%llX != 0x%llX\n", (unsigned long long)le64_to_int(pgpt_head->signature), (unsigned long long)GPT_HEADER_SIGNATURE); return 0; } /* Check the GUID Partition Table CRC */ memcpy(crc32_backup, pgpt_head->header_crc32, sizeof(crc32_backup)); memset(pgpt_head->header_crc32, 0, sizeof(pgpt_head->header_crc32)); calc_crc32 = efi_crc32((const unsigned char *)pgpt_head, le32_to_int(pgpt_head->header_size)); memcpy(pgpt_head->header_crc32, crc32_backup, sizeof(crc32_backup)); if (calc_crc32 != le32_to_int(crc32_backup)) { printf("GUID Partition Table Header CRC is wrong:" "0x%08lX != 0x%08lX\n", le32_to_int(crc32_backup), calc_crc32); return 0; } /* Check that the my_lba entry points to the LBA that contains the GPT */ if (le64_to_int(pgpt_head->my_lba) != lba) { printf("GPT: my_lba incorrect: %llX != %llX\n", (unsigned long long)le64_to_int(pgpt_head->my_lba), (unsigned long long)lba); return 0; } /* Check the first_usable_lba and last_usable_lba are within the disk. */ lastlba = (unsigned long long)dev_desc->lba; if (le64_to_int(pgpt_head->first_usable_lba) > lastlba) { printf("GPT: first_usable_lba incorrect: %llX > %llX\n", le64_to_int(pgpt_head->first_usable_lba), lastlba); return 0; } if (le64_to_int(pgpt_head->last_usable_lba) > lastlba) { printf("GPT: last_usable_lba incorrect: %llX > %llX\n", le64_to_int(pgpt_head->last_usable_lba), lastlba); return 0; } debug("GPT: first_usable_lba: %llX last_usable_lba %llX last lba %llX\n", le64_to_int(pgpt_head->first_usable_lba), le64_to_int(pgpt_head->last_usable_lba), lastlba); /* Read and allocate Partition Table Entries */ *pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); if (*pgpt_pte == NULL) { printf("GPT: Failed to allocate memory for PTE\n"); return 0; } /* Check the GUID Partition Table Entry Array CRC */ calc_crc32 = efi_crc32((const unsigned char *)*pgpt_pte, le32_to_int(pgpt_head->num_partition_entries) * le32_to_int(pgpt_head->sizeof_partition_entry)); if (calc_crc32 != le32_to_int(pgpt_head->partition_entry_array_crc32)) { printf("GUID Partition Table Entry Array CRC is wrong:" "0x%08lX != 0x%08lX\n", le32_to_int(pgpt_head->partition_entry_array_crc32), calc_crc32); if (*pgpt_pte != NULL) { free(*pgpt_pte); } return 0; } /* We're done, all's well */ return 1; }
/* * re-constructe iptbl from mbr&ebr infos. * memory for iptbl_mbr must be alloced outside. * */ static void _construct_ptbl_by_mbr(struct mmc *mmc, struct _iptbl *iptbl_mbr) { int ret,i; int flag = 0; lbaint_t read_offset = 0; int part_num = 0; int primary_num = 0; uint64_t logic_start = 0; uint64_t externed_start = 0; struct dos_partition *pt; struct partitions *partitions = iptbl_mbr->partitions; apt_info("aml MBR&EBR debug...\n"); ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, 512); for (;;) { apt_info("**%02d: read_offset %016llx\n", part_num, (uint64_t)read_offset<<9); ret = mmc->block_dev.block_read(mmc->block_dev.dev, read_offset, 1, buffer); if (read_offset == 0) flag = 1; else flag = 0; /* debug code */ // print_buffer(0,buffer,1,512,16); if (ret != 1) { apt_err("ret %d fail to read current ebr&mbr from emmc! \n", ret); break; } ret = test_block_type(buffer); if (ret != 0 && ret != 1) { apt_err("invalid magic value: 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); break; } pt = (dos_partition_t *)(&buffer[0] + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if ( (pt->boot_ind == 0x00 || pt->boot_ind == 0x80) && pt->sys_ind == 0x83 ) { //emmc_partition[part_num]->name = NULL; partitions[part_num].offset = ((uint64_t)(le32_to_int(pt->start4)+read_offset) << 9ULL); partitions[part_num].size = (uint64_t)le32_to_int(pt->size4) << 9ULL; partitions[part_num].mask_flags = pt->sys_ind; apt_info("--partition[%d]: %016llx, %016llx, 0x%08x \n", part_num, partitions[part_num].offset, partitions[part_num].size, le32_to_int(pt->size4)); part_num++; if ( flag ) primary_num++; }else{/* get the next externed partition info */ if ( pt->boot_ind == 0x00 && pt->sys_ind == 0x05) { logic_start = (uint64_t)le32_to_int (pt->start4); //logic_size = (uint64_t)le32_to_int (pt->size4); } } } /* mbr & ebr debug infos */ apt_info("******%02d: read_offset=%016llx, logic_start=%016llx\n", part_num,(uint64_t)read_offset*512ULL,logic_start*512ULL); if (part_num == primary_num) { externed_start = logic_start; read_offset = externed_start; }else read_offset = externed_start + logic_start; if (logic_start == 0) break; logic_start = 0; } iptbl_mbr->count = part_num; apt_info("iptbl_mbr->count = %d\n", iptbl_mbr->count); return; }
/* Print a partition that is relative to its Extended partition table */ static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector, int relative, int part_num, int which_part, disk_partition_t *info) { unsigned char tmp_buf[DEFAULT_SECTOR_SIZE]; unsigned char *buffer = KSEG1ADDR(&tmp_buf[0]); dos_partition_t *pt; int i; if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { printf ("** Can't read partition table on %d:%d **\n", dev_desc->dev, ext_part_sector); return -1; } if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return -1; } /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); /* MTK/Ralink 2012/06/28 -- if no partition table in USB storage first sector */ if(pt->boot_ind != 0x80 && pt->boot_ind != 0x0){ boot_sector *bs; printf ("It seems no partition tables existed.\n"); bs = (boot_sector *) (buffer); if(ext_part_sector == 0 && bs->reserved && bs->fats){ info->blksz = 512; info->start = ext_part_sector + 0; //FIXME: how to determine info->size? from FAT struct? //info->size = FAT2CPU32(bs->total_sect); return 0; }else return -1; } for (i = 0; i < 4; i++, pt++) { /* * fdisk does not show the extended partitions that * are not in the MBR */ if ((pt->sys_ind != 0) && (part_num == which_part) && (is_extended(pt->sys_ind) == 0)) { info->blksz = 512; info->start = ext_part_sector + le32_to_int (pt->start4); info->size = le32_to_int (pt->size4); switch(dev_desc->if_type) { case IF_TYPE_IDE: case IF_TYPE_ATAPI: sprintf (info->name, "hd%c%d\n", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_SCSI: sprintf (info->name, "sd%c%d\n", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_USB: sprintf (info->name, "usbd%c%d\n", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_DOC: sprintf (info->name, "docd%c%d\n", 'a' + dev_desc->dev, part_num); break; default: sprintf (info->name, "xx%c%d\n", 'a' + dev_desc->dev, part_num); break; } /* sprintf(info->type, "%d, pt->sys_ind); */ sprintf (info->type, "U-Boot"); return 0; } /* Reverse engr the fdisk part# assignment rule! */ if ((ext_part_sector == 0) || (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { part_num++; } } /* Follows the extended partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if (is_extended (pt->sys_ind)) { int lba_start = le32_to_int (pt->start4) + relative; return get_partition_info_extended (dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, part_num, which_part, info); } } return -1; }
/* Print a partition that is relative to its Extended partition table */ static int get_partition_info_extended (block_dev_desc_t *dev_desc, int ext_part_sector, int relative, int part_num, int which_part, disk_partition_t *info) { unsigned char buffer[DEFAULT_SECTOR_SIZE]; dos_partition_t *pt; int i; if (dev_desc->block_read (dev_desc->dev, ext_part_sector, 1, (ulong *) buffer) != 1) { printf ("** Can't read partition table on %d:%d **\n", dev_desc->dev, ext_part_sector); return -1; } if (buffer[DOS_PART_MAGIC_OFFSET] != 0x55 || buffer[DOS_PART_MAGIC_OFFSET + 1] != 0xaa) { printf ("bad MBR sector signature 0x%02x%02x\n", buffer[DOS_PART_MAGIC_OFFSET], buffer[DOS_PART_MAGIC_OFFSET + 1]); return -1; } /* Print all primary/logical partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { /* * fdisk does not show the extended partitions that * are not in the MBR */ if (((pt->boot_ind & ~0x80) == 0) && (pt->sys_ind != 0) && (part_num == which_part) && (is_extended(pt->sys_ind) == 0)) { info->blksz = 512; info->start = ext_part_sector + le32_to_int (pt->start4); info->size = le32_to_int (pt->size4); switch(dev_desc->if_type) { case IF_TYPE_IDE: case IF_TYPE_SATA: case IF_TYPE_ATAPI: sprintf ((char *)info->name, "hd%c%d", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_SCSI: sprintf ((char *)info->name, "sd%c%d", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_USB: sprintf ((char *)info->name, "usbd%c%d", 'a' + dev_desc->dev, part_num); break; case IF_TYPE_DOC: sprintf ((char *)info->name, "docd%c%d", 'a' + dev_desc->dev, part_num); break; default: sprintf ((char *)info->name, "xx%c%d", 'a' + dev_desc->dev, part_num); break; } /* sprintf(info->type, "%d, pt->sys_ind); */ sprintf ((char *)info->type, "U-Boot"); return 0; } /* Reverse engr the fdisk part# assignment rule! */ if ((ext_part_sector == 0) || (pt->sys_ind != 0 && !is_extended (pt->sys_ind)) ) { part_num++; } } /* Follows the extended partitions */ pt = (dos_partition_t *) (buffer + DOS_PART_TBL_OFFSET); for (i = 0; i < 4; i++, pt++) { if (is_extended (pt->sys_ind)) { int lba_start = le32_to_int (pt->start4) + relative; return get_partition_info_extended (dev_desc, lba_start, ext_part_sector == 0 ? lba_start : relative, part_num, which_part, info); } } return -1; }