Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions image-hd.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,24 @@ static unsigned long long partition_end(const struct partition *part)

static void lba_to_chs(unsigned int lba, unsigned char *chs)
{
const unsigned int hpc = 255;
const unsigned int spt = 63;
unsigned int s, c;

chs[0] = (lba / spt) % hpc;
c = (lba / (spt * hpc));
s = (lba > 0) ? (lba % spt + 1) : 0;
chs[1] = ((c & 0x300) >> 2) | (s & 0xff);
chs[2] = (c & 0xff);
const unsigned int heads_per_cyl = 255;
const unsigned int sect_per_track = 63;

if (lba > (1024 * heads_per_cyl * sect_per_track - 1)) {
chs[0] = 0xFE;
chs[1] = 0xFF;
chs[2] = 0xFF;
return;
}

unsigned int cylinder = lba / (heads_per_cyl * sect_per_track);
unsigned int remainder = lba % (heads_per_cyl * sect_per_track);
unsigned int head = remainder / sect_per_track;
unsigned int sector = (remainder % sect_per_track) + 1;

chs[0] = head & 0xFF;
chs[1] = (sector & 0x3F) + ((cylinder & 0x300) >> 2);
chs[2] = cylinder & 0xFF;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be better readable if you moved the variable renaming to a separate patch so the actual changes in functionality are better visible

}

static void hdimage_setup_chs(struct mbr_partition_entry *entry,
Expand Down Expand Up @@ -185,7 +194,7 @@ static int hdimage_insert_mbr(struct image *image, struct list_head *partitions)

entry->partition_type = 0xee;
entry->relative_sectors = 1;
entry->total_sectors = hd->gpt_location / 512 + GPT_SECTORS - 2;
entry->total_sectors = GPT_SECTORS;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gpt_location is configurable. It doesn't have start at LBA2. We specifically use this on some i.MX SoCs where the
ROM expects a i.MX specific boot header at LBA2, so we move the gpt partition entries somewhere else using gpt_location.


hdimage_setup_chs(entry, 0);
}
Expand Down