part: efi: Use desc instead of dev_desc

The dev_ prefix is a hangover from the pre-driver model days. The device
is now a different thing, with driver model. Update the efi code to
just use 'desc'.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass 2023-08-24 13:55:27 -06:00 committed by Tom Rini
parent cfdcd41e5a
commit 7ed781fb4c

View file

@ -51,12 +51,12 @@ static inline u32 efi_crc32(const void *buf, u32 len)
static int pmbr_part_valid(struct partition *part); static int pmbr_part_valid(struct partition *part);
static int is_pmbr_valid(legacy_mbr * mbr); static int is_pmbr_valid(legacy_mbr * mbr);
static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
gpt_header *pgpt_head, gpt_entry **pgpt_pte); gpt_entry **pgpt_pte);
static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc, static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,
gpt_header *pgpt_head); gpt_header *pgpt_head);
static int is_pte_valid(gpt_entry * pte); static int is_pte_valid(gpt_entry * pte);
static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head, static int find_valid_gpt(struct blk_desc *desc, gpt_header *gpt_head,
gpt_entry **pgpt_pte); gpt_entry **pgpt_pte);
static char *print_efiname(gpt_entry *pte) static char *print_efiname(gpt_entry *pte)
@ -195,14 +195,14 @@ static void prepare_backup_gpt_header(gpt_header *gpt_h)
* UUID is displayed as 32 hexadecimal digits, in 5 groups, * UUID is displayed as 32 hexadecimal digits, in 5 groups,
* separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters * separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters
*/ */
int get_disk_guid(struct blk_desc * dev_desc, char *guid) int get_disk_guid(struct blk_desc *desc, char *guid)
{ {
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
gpt_entry *gpt_pte = NULL; gpt_entry *gpt_pte = NULL;
unsigned char *guid_bin; unsigned char *guid_bin;
/* This function validates AND fills in the GPT header and PTE */ /* This function validates AND fills in the GPT header and PTE */
if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
return -EINVAL; return -EINVAL;
guid_bin = gpt_head->disk_guid.b; guid_bin = gpt_head->disk_guid.b;
@ -213,15 +213,15 @@ int get_disk_guid(struct blk_desc * dev_desc, char *guid)
return 0; return 0;
} }
void part_print_efi(struct blk_desc *dev_desc) void part_print_efi(struct blk_desc *desc)
{ {
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
gpt_entry *gpt_pte = NULL; gpt_entry *gpt_pte = NULL;
int i = 0; int i = 0;
unsigned char *uuid; unsigned char *uuid;
/* This function validates AND fills in the GPT header and PTE */ /* This function validates AND fills in the GPT header and PTE */
if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
return; return;
debug("%s: gpt-entry at %p\n", __func__, gpt_pte); debug("%s: gpt-entry at %p\n", __func__, gpt_pte);
@ -255,10 +255,10 @@ void part_print_efi(struct blk_desc *dev_desc)
return; return;
} }
int part_get_info_efi(struct blk_desc *dev_desc, int part, int part_get_info_efi(struct blk_desc *desc, int part,
struct disk_partition *info) struct disk_partition *info)
{ {
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_head, 1, desc->blksz);
gpt_entry *gpt_pte = NULL; gpt_entry *gpt_pte = NULL;
/* "part" argument must be at least 1 */ /* "part" argument must be at least 1 */
@ -268,7 +268,7 @@ int part_get_info_efi(struct blk_desc *dev_desc, int part,
} }
/* This function validates AND fills in the GPT header and PTE */ /* This function validates AND fills in the GPT header and PTE */
if (find_valid_gpt(dev_desc, gpt_head, &gpt_pte) != 1) if (find_valid_gpt(desc, gpt_head, &gpt_pte) != 1)
return -EINVAL; return -EINVAL;
if (part > le32_to_cpu(gpt_head->num_partition_entries) || if (part > le32_to_cpu(gpt_head->num_partition_entries) ||
@ -283,7 +283,7 @@ int part_get_info_efi(struct blk_desc *dev_desc, int part,
/* The ending LBA is inclusive, to calculate size, add 1 to it */ /* 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[part - 1].ending_lba) + 1
- info->start; - info->start;
info->blksz = dev_desc->blksz; info->blksz = desc->blksz;
snprintf((char *)info->name, sizeof(info->name), "%s", snprintf((char *)info->name, sizeof(info->name), "%s",
print_efiname(&gpt_pte[part - 1])); print_efiname(&gpt_pte[part - 1]));
@ -306,12 +306,12 @@ int part_get_info_efi(struct blk_desc *dev_desc, int part,
return 0; return 0;
} }
static int part_test_efi(struct blk_desc *dev_desc) static int part_test_efi(struct blk_desc *desc)
{ {
ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, legacymbr, 1, desc->blksz);
/* Read legacy MBR from block 0 and validate it */ /* Read legacy MBR from block 0 and validate it */
if ((blk_dread(dev_desc, 0, 1, (ulong *)legacymbr) != 1) if ((blk_dread(desc, 0, 1, (ulong *)legacymbr) != 1)
|| (is_pmbr_valid(legacymbr) != 1)) { || (is_pmbr_valid(legacymbr) != 1)) {
return -1; return -1;
} }
@ -320,23 +320,23 @@ static int part_test_efi(struct blk_desc *dev_desc)
/** /**
* set_protective_mbr(): Set the EFI protective MBR * set_protective_mbr(): Set the EFI protective MBR
* @param dev_desc - block device descriptor * @param desc - block device descriptor
* *
* Return: - zero on success, otherwise error * Return: - zero on success, otherwise error
*/ */
static int set_protective_mbr(struct blk_desc *dev_desc) static int set_protective_mbr(struct blk_desc *desc)
{ {
/* Setup the Protective MBR */ /* Setup the Protective MBR */
ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, p_mbr, 1, desc->blksz);
if (p_mbr == NULL) { if (p_mbr == NULL) {
log_debug("calloc failed!\n"); log_debug("calloc failed!\n");
return -ENOMEM; return -ENOMEM;
} }
/* Read MBR to backup boot code if it exists */ /* Read MBR to backup boot code if it exists */
if (blk_dread(dev_desc, 0, 1, p_mbr) != 1) { if (blk_dread(desc, 0, 1, p_mbr) != 1) {
log_debug("** Can't read from device %d **\n", log_debug("** Can't read from device %d **\n",
dev_desc->devnum); desc->devnum);
return -EIO; return -EIO;
} }
@ -348,27 +348,26 @@ static int set_protective_mbr(struct blk_desc *dev_desc)
p_mbr->signature = MSDOS_MBR_SIGNATURE; p_mbr->signature = MSDOS_MBR_SIGNATURE;
p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT; p_mbr->partition_record[0].sys_ind = EFI_PMBR_OSTYPE_EFI_GPT;
p_mbr->partition_record[0].start_sect = 1; p_mbr->partition_record[0].start_sect = 1;
p_mbr->partition_record[0].nr_sects = (u32) dev_desc->lba - 1; p_mbr->partition_record[0].nr_sects = (u32)desc->lba - 1;
/* Write MBR sector to the MMC device */ /* Write MBR sector to the MMC device */
if (blk_dwrite(dev_desc, 0, 1, p_mbr) != 1) { if (blk_dwrite(desc, 0, 1, p_mbr) != 1) {
log_debug("** Can't write to device %d **\n", dev_desc->devnum); log_debug("** Can't write to device %d **\n", desc->devnum);
return -EIO; return -EIO;
} }
return 0; return 0;
} }
int write_gpt_table(struct blk_desc *dev_desc, int write_gpt_table(struct blk_desc *desc, gpt_header *gpt_h, gpt_entry *gpt_e)
gpt_header *gpt_h, gpt_entry *gpt_e)
{ {
const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
* sizeof(gpt_entry)), dev_desc); * sizeof(gpt_entry)), desc);
u32 calc_crc32; u32 calc_crc32;
debug("max lba: %x\n", (u32) dev_desc->lba); debug("max lba: %x\n", (u32)desc->lba);
/* Setup the Protective MBR */ /* Setup the Protective MBR */
if (set_protective_mbr(dev_desc) < 0) if (set_protective_mbr(desc) < 0)
goto err; goto err;
/* Generate CRC for the Primary GPT Header */ /* Generate CRC for the Primary GPT Header */
@ -382,20 +381,20 @@ int write_gpt_table(struct blk_desc *dev_desc,
gpt_h->header_crc32 = cpu_to_le32(calc_crc32); gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
/* Write the First GPT to the block right after the Legacy MBR */ /* Write the First GPT to the block right after the Legacy MBR */
if (blk_dwrite(dev_desc, 1, 1, gpt_h) != 1) if (blk_dwrite(desc, 1, 1, gpt_h) != 1)
goto err; goto err;
if (blk_dwrite(dev_desc, le64_to_cpu(gpt_h->partition_entry_lba), if (blk_dwrite(desc, le64_to_cpu(gpt_h->partition_entry_lba),
pte_blk_cnt, gpt_e) != pte_blk_cnt) pte_blk_cnt, gpt_e) != pte_blk_cnt)
goto err; goto err;
prepare_backup_gpt_header(gpt_h); prepare_backup_gpt_header(gpt_h);
if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba) if (blk_dwrite(desc, (lbaint_t)le64_to_cpu(gpt_h->last_usable_lba)
+ 1, pte_blk_cnt, gpt_e) != pte_blk_cnt) + 1, pte_blk_cnt, gpt_e) != pte_blk_cnt)
goto err; goto err;
if (blk_dwrite(dev_desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1, if (blk_dwrite(desc, (lbaint_t)le64_to_cpu(gpt_h->my_lba), 1,
gpt_h) != 1) gpt_h) != 1)
goto err; goto err;
@ -403,11 +402,11 @@ int write_gpt_table(struct blk_desc *dev_desc,
return 0; return 0;
err: err:
log_debug("** Can't write to device %d **\n", dev_desc->devnum); log_debug("** Can't write to device %d **\n", desc->devnum);
return -EIO; return -EIO;
} }
int gpt_fill_pte(struct blk_desc *dev_desc, int gpt_fill_pte(struct blk_desc *desc,
gpt_header *gpt_h, gpt_entry *gpt_e, gpt_header *gpt_h, gpt_entry *gpt_e,
struct disk_partition *partitions, int parts) struct disk_partition *partitions, int parts)
{ {
@ -430,7 +429,7 @@ int gpt_fill_pte(struct blk_desc *dev_desc,
size_t pte_start = gpt_h->partition_entry_lba; size_t pte_start = gpt_h->partition_entry_lba;
size_t pte_end = pte_start + size_t pte_end = pte_start +
gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry / gpt_h->num_partition_entries * gpt_h->sizeof_partition_entry /
dev_desc->blksz; desc->blksz;
for (i = 0; i < parts; i++) { for (i = 0; i < parts; i++) {
/* partition starting lba */ /* partition starting lba */
@ -527,7 +526,7 @@ int gpt_fill_pte(struct blk_desc *dev_desc,
return 0; return 0;
} }
static uint32_t partition_entries_offset(struct blk_desc *dev_desc) static uint32_t partition_entries_offset(struct blk_desc *desc)
{ {
uint32_t offset_blks = 2; uint32_t offset_blks = 2;
uint32_t __maybe_unused offset_bytes; uint32_t __maybe_unused offset_bytes;
@ -543,8 +542,8 @@ static uint32_t partition_entries_offset(struct blk_desc *dev_desc)
* CONFIG_EFI_PARTITION_ENTRIES_OFF. * CONFIG_EFI_PARTITION_ENTRIES_OFF.
*/ */
offset_bytes = offset_bytes =
PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, dev_desc); PAD_TO_BLOCKSIZE(CONFIG_EFI_PARTITION_ENTRIES_OFF, desc);
offset_blks = offset_bytes / dev_desc->blksz; offset_blks = offset_bytes / desc->blksz;
#endif #endif
#if defined(CONFIG_OF_CONTROL) #if defined(CONFIG_OF_CONTROL)
@ -556,8 +555,8 @@ static uint32_t partition_entries_offset(struct blk_desc *dev_desc)
config_offset = ofnode_conf_read_int( config_offset = ofnode_conf_read_int(
"u-boot,efi-partition-entries-offset", -EINVAL); "u-boot,efi-partition-entries-offset", -EINVAL);
if (config_offset != -EINVAL) { if (config_offset != -EINVAL) {
offset_bytes = PAD_TO_BLOCKSIZE(config_offset, dev_desc); offset_bytes = PAD_TO_BLOCKSIZE(config_offset, desc);
offset_blks = offset_bytes / dev_desc->blksz; offset_blks = offset_bytes / desc->blksz;
} }
#endif #endif
@ -573,17 +572,17 @@ static uint32_t partition_entries_offset(struct blk_desc *dev_desc)
return offset_blks; return offset_blks;
} }
int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h, int gpt_fill_header(struct blk_desc *desc, gpt_header *gpt_h, char *str_guid,
char *str_guid, int parts_count) int parts_count)
{ {
gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT); gpt_h->signature = cpu_to_le64(GPT_HEADER_SIGNATURE_UBOOT);
gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1); gpt_h->revision = cpu_to_le32(GPT_HEADER_REVISION_V1);
gpt_h->header_size = cpu_to_le32(sizeof(gpt_header)); gpt_h->header_size = cpu_to_le32(sizeof(gpt_header));
gpt_h->my_lba = cpu_to_le64(1); gpt_h->my_lba = cpu_to_le64(1);
gpt_h->alternate_lba = cpu_to_le64(dev_desc->lba - 1); gpt_h->alternate_lba = cpu_to_le64(desc->lba - 1);
gpt_h->last_usable_lba = cpu_to_le64(dev_desc->lba - 34); gpt_h->last_usable_lba = cpu_to_le64(desc->lba - 34);
gpt_h->partition_entry_lba = gpt_h->partition_entry_lba =
cpu_to_le64(partition_entries_offset(dev_desc)); cpu_to_le64(partition_entries_offset(desc));
gpt_h->first_usable_lba = gpt_h->first_usable_lba =
cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32); cpu_to_le64(le64_to_cpu(gpt_h->partition_entry_lba) + 32);
gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS); gpt_h->num_partition_entries = cpu_to_le32(GPT_ENTRY_NUMBERS);
@ -597,14 +596,14 @@ int gpt_fill_header(struct blk_desc *dev_desc, gpt_header *gpt_h,
return 0; return 0;
} }
int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid, int gpt_restore(struct blk_desc *desc, char *str_disk_guid,
struct disk_partition *partitions, int parts_count) struct disk_partition *partitions, int parts_count)
{ {
gpt_header *gpt_h; gpt_header *gpt_h;
gpt_entry *gpt_e; gpt_entry *gpt_e;
int ret, size; int ret, size;
size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), dev_desc); size = PAD_TO_BLOCKSIZE(sizeof(gpt_header), desc);
gpt_h = malloc_cache_aligned(size); gpt_h = malloc_cache_aligned(size);
if (gpt_h == NULL) { if (gpt_h == NULL) {
log_debug("calloc failed!\n"); log_debug("calloc failed!\n");
@ -613,7 +612,7 @@ int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
memset(gpt_h, 0, size); memset(gpt_h, 0, size);
size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry), size = PAD_TO_BLOCKSIZE(GPT_ENTRY_NUMBERS * sizeof(gpt_entry),
dev_desc); desc);
gpt_e = malloc_cache_aligned(size); gpt_e = malloc_cache_aligned(size);
if (gpt_e == NULL) { if (gpt_e == NULL) {
log_debug("calloc failed!\n"); log_debug("calloc failed!\n");
@ -623,17 +622,17 @@ int gpt_restore(struct blk_desc *dev_desc, char *str_disk_guid,
memset(gpt_e, 0, size); memset(gpt_e, 0, size);
/* Generate Primary GPT header (LBA1) */ /* Generate Primary GPT header (LBA1) */
ret = gpt_fill_header(dev_desc, gpt_h, str_disk_guid, parts_count); ret = gpt_fill_header(desc, gpt_h, str_disk_guid, parts_count);
if (ret) if (ret)
goto err; goto err;
/* Generate partition entries */ /* Generate partition entries */
ret = gpt_fill_pte(dev_desc, gpt_h, gpt_e, partitions, parts_count); ret = gpt_fill_pte(desc, gpt_h, gpt_e, partitions, parts_count);
if (ret) if (ret)
goto err; goto err;
/* Write GPT partition table */ /* Write GPT partition table */
ret = write_gpt_table(dev_desc, gpt_h, gpt_e); ret = write_gpt_table(desc, gpt_h, gpt_e);
err: err:
free(gpt_e); free(gpt_e);
@ -664,14 +663,14 @@ static void gpt_convert_efi_name_to_char(char *s, void *es, int n)
} }
} }
int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head, int gpt_verify_headers(struct blk_desc *desc, gpt_header *gpt_head,
gpt_entry **gpt_pte) gpt_entry **gpt_pte)
{ {
/* /*
* This function validates AND * This function validates AND
* fills in the GPT header and PTE * fills in the GPT header and PTE
*/ */
if (is_gpt_valid(dev_desc, if (is_gpt_valid(desc,
GPT_PRIMARY_PARTITION_TABLE_LBA, GPT_PRIMARY_PARTITION_TABLE_LBA,
gpt_head, gpt_pte) != 1) { gpt_head, gpt_pte) != 1) {
log_debug("Invalid GPT\n"); log_debug("Invalid GPT\n");
@ -684,12 +683,12 @@ int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
/* /*
* Check that the alternate_lba entry points to the last LBA * Check that the alternate_lba entry points to the last LBA
*/ */
if (le64_to_cpu(gpt_head->alternate_lba) != (dev_desc->lba - 1)) { if (le64_to_cpu(gpt_head->alternate_lba) != (desc->lba - 1)) {
log_debug("Misplaced Backup GPT\n"); log_debug("Misplaced Backup GPT\n");
return -1; return -1;
} }
if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), if (is_gpt_valid(desc, (desc->lba - 1),
gpt_head, gpt_pte) != 1) { gpt_head, gpt_pte) != 1) {
log_debug("Invalid Backup GPT\n"); log_debug("Invalid Backup GPT\n");
return -1; return -1;
@ -698,7 +697,7 @@ int gpt_verify_headers(struct blk_desc *dev_desc, gpt_header *gpt_head,
return 0; return 0;
} }
static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *dev_desc) static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *desc)
{ {
u32 calc_crc32; u32 calc_crc32;
u64 val; u64 val;
@ -707,7 +706,7 @@ static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *dev_d
val = le64_to_cpu(gpt_h->my_lba); val = le64_to_cpu(gpt_h->my_lba);
gpt_h->my_lba = gpt_h->alternate_lba; gpt_h->my_lba = gpt_h->alternate_lba;
gpt_h->alternate_lba = cpu_to_le64(val); gpt_h->alternate_lba = cpu_to_le64(val);
gpt_h->partition_entry_lba = cpu_to_le64(partition_entries_offset(dev_desc)); gpt_h->partition_entry_lba = cpu_to_le64(partition_entries_offset(desc));
gpt_h->header_crc32 = 0; gpt_h->header_crc32 = 0;
@ -716,22 +715,22 @@ static void restore_primary_gpt_header(gpt_header *gpt_h, struct blk_desc *dev_d
gpt_h->header_crc32 = cpu_to_le32(calc_crc32); gpt_h->header_crc32 = cpu_to_le32(calc_crc32);
} }
static int write_one_gpt_table(struct blk_desc *dev_desc, static int write_one_gpt_table(struct blk_desc *desc, gpt_header *gpt_h,
gpt_header *gpt_h, gpt_entry *gpt_e) gpt_entry *gpt_e)
{ {
const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries const int pte_blk_cnt = BLOCK_CNT((gpt_h->num_partition_entries
* sizeof(gpt_entry)), dev_desc); * sizeof(gpt_entry)), desc);
lbaint_t start; lbaint_t start;
int ret = 0; int ret = 0;
start = le64_to_cpu(gpt_h->my_lba); start = le64_to_cpu(gpt_h->my_lba);
if (blk_dwrite(dev_desc, start, 1, gpt_h) != 1) { if (blk_dwrite(desc, start, 1, gpt_h) != 1) {
ret = -1; ret = -1;
goto out; goto out;
} }
start = le64_to_cpu(gpt_h->partition_entry_lba); start = le64_to_cpu(gpt_h->partition_entry_lba);
if (blk_dwrite(dev_desc, start, pte_blk_cnt, gpt_e) != pte_blk_cnt) { if (blk_dwrite(desc, start, pte_blk_cnt, gpt_e) != pte_blk_cnt) {
ret = -1; ret = -1;
goto out; goto out;
} }
@ -740,17 +739,17 @@ static int write_one_gpt_table(struct blk_desc *dev_desc,
return ret; return ret;
} }
int gpt_repair_headers(struct blk_desc *dev_desc) int gpt_repair_headers(struct blk_desc *desc)
{ {
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h1, 1, desc->blksz);
ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(gpt_header, gpt_h2, 1, desc->blksz);
gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL; gpt_entry *gpt_e1 = NULL, *gpt_e2 = NULL;
int is_gpt1_valid, is_gpt2_valid; int is_gpt1_valid, is_gpt2_valid;
int ret = -1; int ret = -1;
is_gpt1_valid = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, is_gpt1_valid = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA,
gpt_h1, &gpt_e1); gpt_h1, &gpt_e1);
is_gpt2_valid = is_gpt_valid(dev_desc, dev_desc->lba - 1, is_gpt2_valid = is_gpt_valid(desc, desc->lba - 1,
gpt_h2, &gpt_e2); gpt_h2, &gpt_e2);
if (is_gpt1_valid && is_gpt2_valid) { if (is_gpt1_valid && is_gpt2_valid) {
@ -760,13 +759,13 @@ int gpt_repair_headers(struct blk_desc *dev_desc)
if (is_gpt1_valid && !is_gpt2_valid) { if (is_gpt1_valid && !is_gpt2_valid) {
prepare_backup_gpt_header(gpt_h1); prepare_backup_gpt_header(gpt_h1);
ret = write_one_gpt_table(dev_desc, gpt_h1, gpt_e1); ret = write_one_gpt_table(desc, gpt_h1, gpt_e1);
goto out; goto out;
} }
if (!is_gpt1_valid && is_gpt2_valid) { if (!is_gpt1_valid && is_gpt2_valid) {
restore_primary_gpt_header(gpt_h2, dev_desc); restore_primary_gpt_header(gpt_h2, desc);
ret = write_one_gpt_table(dev_desc, gpt_h2, gpt_e2); ret = write_one_gpt_table(desc, gpt_h2, gpt_e2);
goto out; goto out;
} }
@ -784,7 +783,7 @@ int gpt_repair_headers(struct blk_desc *dev_desc)
return ret; return ret;
} }
int gpt_verify_partitions(struct blk_desc *dev_desc, int gpt_verify_partitions(struct blk_desc *desc,
struct disk_partition *partitions, int parts, struct disk_partition *partitions, int parts,
gpt_header *gpt_head, gpt_entry **gpt_pte) gpt_header *gpt_head, gpt_entry **gpt_pte)
{ {
@ -793,7 +792,7 @@ int gpt_verify_partitions(struct blk_desc *dev_desc,
gpt_entry *gpt_e; gpt_entry *gpt_e;
int ret, i; int ret, i;
ret = gpt_verify_headers(dev_desc, gpt_head, gpt_pte); ret = gpt_verify_headers(desc, gpt_head, gpt_pte);
if (ret) if (ret)
return ret; return ret;
@ -862,28 +861,27 @@ int gpt_verify_partitions(struct blk_desc *dev_desc,
return 0; return 0;
} }
int is_valid_gpt_buf(struct blk_desc *dev_desc, void *buf) int is_valid_gpt_buf(struct blk_desc *desc, void *buf)
{ {
gpt_header *gpt_h; gpt_header *gpt_h;
gpt_entry *gpt_e; gpt_entry *gpt_e;
/* determine start of GPT Header in the buffer */ /* determine start of GPT Header in the buffer */
gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * desc->blksz);
dev_desc->blksz);
if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA, if (validate_gpt_header(gpt_h, GPT_PRIMARY_PARTITION_TABLE_LBA,
dev_desc->lba)) desc->lba))
return -1; return -1;
/* determine start of GPT Entries in the buffer */ /* determine start of GPT Entries in the buffer */
gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) *
dev_desc->blksz); desc->blksz);
if (validate_gpt_entries(gpt_h, gpt_e)) if (validate_gpt_entries(gpt_h, gpt_e))
return -1; return -1;
return 0; return 0;
} }
int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf) int write_mbr_and_gpt_partitions(struct blk_desc *desc, void *buf)
{ {
gpt_header *gpt_h; gpt_header *gpt_h;
gpt_entry *gpt_e; gpt_entry *gpt_e;
@ -891,24 +889,22 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
lbaint_t lba; lbaint_t lba;
int cnt; int cnt;
if (is_valid_gpt_buf(dev_desc, buf)) if (is_valid_gpt_buf(desc, buf))
return -1; return -1;
/* determine start of GPT Header in the buffer */ /* determine start of GPT Header in the buffer */
gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * gpt_h = buf + (GPT_PRIMARY_PARTITION_TABLE_LBA * desc->blksz);
dev_desc->blksz);
/* determine start of GPT Entries in the buffer */ /* determine start of GPT Entries in the buffer */
gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * gpt_e = buf + (le64_to_cpu(gpt_h->partition_entry_lba) * desc->blksz);
dev_desc->blksz);
gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) * gpt_e_blk_cnt = BLOCK_CNT((le32_to_cpu(gpt_h->num_partition_entries) *
le32_to_cpu(gpt_h->sizeof_partition_entry)), le32_to_cpu(gpt_h->sizeof_partition_entry)),
dev_desc); desc);
/* write MBR */ /* write MBR */
lba = 0; /* MBR is always at 0 */ lba = 0; /* MBR is always at 0 */
cnt = 1; /* MBR (1 block) */ cnt = 1; /* MBR (1 block) */
if (blk_dwrite(dev_desc, lba, cnt, buf) != cnt) { if (blk_dwrite(desc, lba, cnt, buf) != cnt) {
log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
"MBR", cnt, lba); "MBR", cnt, lba);
return 1; return 1;
@ -917,7 +913,7 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
/* write Primary GPT */ /* write Primary GPT */
lba = GPT_PRIMARY_PARTITION_TABLE_LBA; lba = GPT_PRIMARY_PARTITION_TABLE_LBA;
cnt = 1; /* GPT Header (1 block) */ cnt = 1; /* GPT Header (1 block) */
if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) { if (blk_dwrite(desc, lba, cnt, gpt_h) != cnt) {
log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
"Primary GPT Header", cnt, lba); "Primary GPT Header", cnt, lba);
return 1; return 1;
@ -925,7 +921,7 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
lba = le64_to_cpu(gpt_h->partition_entry_lba); lba = le64_to_cpu(gpt_h->partition_entry_lba);
cnt = gpt_e_blk_cnt; cnt = gpt_e_blk_cnt;
if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) { if (blk_dwrite(desc, lba, cnt, gpt_e) != cnt) {
log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
"Primary GPT Entries", cnt, lba); "Primary GPT Entries", cnt, lba);
return 1; return 1;
@ -936,7 +932,7 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
/* write Backup GPT */ /* write Backup GPT */
lba = le64_to_cpu(gpt_h->partition_entry_lba); lba = le64_to_cpu(gpt_h->partition_entry_lba);
cnt = gpt_e_blk_cnt; cnt = gpt_e_blk_cnt;
if (blk_dwrite(dev_desc, lba, cnt, gpt_e) != cnt) { if (blk_dwrite(desc, lba, cnt, gpt_e) != cnt) {
log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
"Backup GPT Entries", cnt, lba); "Backup GPT Entries", cnt, lba);
return 1; return 1;
@ -944,14 +940,14 @@ int write_mbr_and_gpt_partitions(struct blk_desc *dev_desc, void *buf)
lba = le64_to_cpu(gpt_h->my_lba); lba = le64_to_cpu(gpt_h->my_lba);
cnt = 1; /* GPT Header (1 block) */ cnt = 1; /* GPT Header (1 block) */
if (blk_dwrite(dev_desc, lba, cnt, gpt_h) != cnt) { if (blk_dwrite(desc, lba, cnt, gpt_h) != cnt) {
log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n", log_debug("failed writing '%s' (%d blks at 0x" LBAF ")\n",
"Backup GPT Header", cnt, lba); "Backup GPT Header", cnt, lba);
return 1; return 1;
} }
/* Update the partition table entries*/ /* Update the partition table entries*/
part_init(dev_desc); part_init(desc);
return 0; return 0;
} }
@ -1008,25 +1004,25 @@ static int is_pmbr_valid(legacy_mbr * mbr)
* Description: returns 1 if valid, 0 on error, 2 if ignored header * Description: returns 1 if valid, 0 on error, 2 if ignored header
* If valid, returns pointers to PTEs. * If valid, returns pointers to PTEs.
*/ */
static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba, static int is_gpt_valid(struct blk_desc *desc, u64 lba, gpt_header *pgpt_head,
gpt_header *pgpt_head, gpt_entry **pgpt_pte) gpt_entry **pgpt_pte)
{ {
/* Confirm valid arguments prior to allocation. */ /* Confirm valid arguments prior to allocation. */
if (!dev_desc || !pgpt_head) { if (!desc || !pgpt_head) {
log_debug("Invalid Argument(s)\n"); log_debug("Invalid Argument(s)\n");
return 0; return 0;
} }
ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, mbr, 1, dev_desc->blksz); ALLOC_CACHE_ALIGN_BUFFER_PAD(legacy_mbr, mbr, 1, desc->blksz);
/* Read MBR Header from device */ /* Read MBR Header from device */
if (blk_dread(dev_desc, 0, 1, (ulong *)mbr) != 1) { if (blk_dread(desc, 0, 1, (ulong *)mbr) != 1) {
log_debug("Can't read MBR header\n"); log_debug("Can't read MBR header\n");
return 0; return 0;
} }
/* Read GPT Header from device */ /* Read GPT Header from device */
if (blk_dread(dev_desc, (lbaint_t)lba, 1, pgpt_head) != 1) { if (blk_dread(desc, (lbaint_t)lba, 1, pgpt_head) != 1) {
log_debug("Can't read GPT header\n"); log_debug("Can't read GPT header\n");
return 0; return 0;
} }
@ -1037,23 +1033,23 @@ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
return 2; return 2;
} }
if (validate_gpt_header(pgpt_head, (lbaint_t)lba, dev_desc->lba)) if (validate_gpt_header(pgpt_head, (lbaint_t)lba, desc->lba))
return 0; return 0;
if (dev_desc->sig_type == SIG_TYPE_NONE) { if (desc->sig_type == SIG_TYPE_NONE) {
efi_guid_t empty = {}; efi_guid_t empty = {};
if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) { if (memcmp(&pgpt_head->disk_guid, &empty, sizeof(empty))) {
dev_desc->sig_type = SIG_TYPE_GUID; desc->sig_type = SIG_TYPE_GUID;
memcpy(&dev_desc->guid_sig, &pgpt_head->disk_guid, memcpy(&desc->guid_sig, &pgpt_head->disk_guid,
sizeof(empty)); sizeof(empty));
} else if (mbr->unique_mbr_signature != 0) { } else if (mbr->unique_mbr_signature != 0) {
dev_desc->sig_type = SIG_TYPE_MBR; desc->sig_type = SIG_TYPE_MBR;
dev_desc->mbr_sig = mbr->unique_mbr_signature; desc->mbr_sig = mbr->unique_mbr_signature;
} }
} }
/* Read and allocate Partition Table Entries */ /* Read and allocate Partition Table Entries */
*pgpt_pte = alloc_read_gpt_entries(dev_desc, pgpt_head); *pgpt_pte = alloc_read_gpt_entries(desc, pgpt_head);
if (!*pgpt_pte) if (!*pgpt_pte)
return 0; return 0;
@ -1075,20 +1071,20 @@ static int is_gpt_valid(struct blk_desc *dev_desc, u64 lba,
* Description: returns 1 if found a valid gpt, 0 on error. * Description: returns 1 if found a valid gpt, 0 on error.
* If valid, returns pointers to PTEs. * If valid, returns pointers to PTEs.
*/ */
static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head, static int find_valid_gpt(struct blk_desc *desc, gpt_header *gpt_head,
gpt_entry **pgpt_pte) gpt_entry **pgpt_pte)
{ {
int r; int r;
r = is_gpt_valid(dev_desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head, r = is_gpt_valid(desc, GPT_PRIMARY_PARTITION_TABLE_LBA, gpt_head,
pgpt_pte); pgpt_pte);
if (r != 1) { if (r != 1) {
if (r != 2) if (r != 2)
log_debug("Invalid GPT\n"); log_debug("Invalid GPT\n");
if (is_gpt_valid(dev_desc, (dev_desc->lba - 1), gpt_head, if (is_gpt_valid(desc, desc->lba - 1, gpt_head, pgpt_pte)
pgpt_pte) != 1) { != 1) {
log_debug("Invalid Backup GPT\n"); log_debug("Invalid Backup GPT\n");
return 0; return 0;
} }
@ -1100,21 +1096,21 @@ static int find_valid_gpt(struct blk_desc *dev_desc, gpt_header *gpt_head,
/** /**
* alloc_read_gpt_entries(): reads partition entries from disk * alloc_read_gpt_entries(): reads partition entries from disk
* @dev_desc * @desc
* @gpt - GPT header * @gpt - GPT header
* *
* Description: Returns ptes on success, NULL on error. * Description: Returns ptes on success, NULL on error.
* Allocates space for PTEs based on information found in @gpt. * Allocates space for PTEs based on information found in @gpt.
* Notes: remember to free pte when you're done! * Notes: remember to free pte when you're done!
*/ */
static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc, static gpt_entry *alloc_read_gpt_entries(struct blk_desc *desc,
gpt_header *pgpt_head) gpt_header *pgpt_head)
{ {
size_t count = 0, blk_cnt; size_t count = 0, blk_cnt;
lbaint_t blk; lbaint_t blk;
gpt_entry *pte = NULL; gpt_entry *pte = NULL;
if (!dev_desc || !pgpt_head) { if (!desc || !pgpt_head) {
log_debug("Invalid Argument(s)\n"); log_debug("Invalid Argument(s)\n");
return NULL; return NULL;
} }
@ -1130,7 +1126,7 @@ static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
/* Allocate memory for PTE, remember to FREE */ /* Allocate memory for PTE, remember to FREE */
if (count != 0) { if (count != 0) {
pte = memalign(ARCH_DMA_MINALIGN, pte = memalign(ARCH_DMA_MINALIGN,
PAD_TO_BLOCKSIZE(count, dev_desc)); PAD_TO_BLOCKSIZE(count, desc));
} }
if (count == 0 || pte == NULL) { if (count == 0 || pte == NULL) {
@ -1141,8 +1137,8 @@ static gpt_entry *alloc_read_gpt_entries(struct blk_desc *dev_desc,
/* Read GPT Entries from device */ /* Read GPT Entries from device */
blk = le64_to_cpu(pgpt_head->partition_entry_lba); blk = le64_to_cpu(pgpt_head->partition_entry_lba);
blk_cnt = BLOCK_CNT(count, dev_desc); blk_cnt = BLOCK_CNT(count, desc);
if (blk_dread(dev_desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) { if (blk_dread(desc, blk, (lbaint_t)blk_cnt, pte) != blk_cnt) {
log_debug("Can't read GPT Entries\n"); log_debug("Can't read GPT Entries\n");
free(pte); free(pte);
return NULL; return NULL;