diff --git a/image-hd.c b/image-hd.c index d1fe303..cec0f7d 100644 --- a/image-hd.c +++ b/image-hd.c @@ -125,13 +125,20 @@ static void lba_to_chs(unsigned int lba, unsigned char *chs) chs[2] = (c & 0xff); } -static void hdimage_setup_chs(struct mbr_partition_entry *entry) +static void hdimage_setup_chs_with_offset(struct mbr_partition_entry *entry, + unsigned long long offset_sectors) { - lba_to_chs(entry->relative_sectors, entry->first_chs); - lba_to_chs(entry->relative_sectors + entry->total_sectors - 1, + lba_to_chs(entry->relative_sectors + offset_sectors, + entry->first_chs); + lba_to_chs(entry->relative_sectors + entry->total_sectors - 1 + offset_sectors, entry->last_chs); } +static void hdimage_setup_chs(struct mbr_partition_entry *entry) +{ + hdimage_setup_chs_with_offset(entry, 0); +} + static int hdimage_insert_mbr(struct image *image, struct list_head *partitions) { struct hdimage *hd = image->handler_priv; @@ -226,7 +233,9 @@ static int hdimage_insert_ebr(struct image *image, struct partition *part) entry->partition_type = part->partition_type; entry->relative_sectors = hd->align/512; entry->total_sectors = part->size/512; - hdimage_setup_chs(entry); + // absolute CHS address of the logical partition + // equals to absolute partition offset + hdimage_setup_chs_with_offset(entry, (part->offset - hd->align) / 512); struct partition *p = part; list_for_each_entry_continue(p, &image->partitions, list) { if (!p->extended) @@ -236,7 +245,9 @@ static int hdimage_insert_ebr(struct image *image, struct partition *part) entry->partition_type = 0x0F; entry->relative_sectors = (p->offset - hd->align - hd->extended_lba)/512; entry->total_sectors = (p->size + hd->align)/512; - hdimage_setup_chs(entry); + // absolute CHS address of the next EBR + // equals to relative address within extended partition + partition start + hdimage_setup_chs_with_offset(entry, hd->extended_lba / 512); break; }