genimage icon indicating copy to clipboard operation
genimage copied to clipboard

4Kn storage device support

Open learmj opened this issue 2 months ago • 7 comments

Hi @michaelolbrich , all

Are there recommendations about how to support creating images for a 4Kn storage device using genimage?

We've been testing with NVMe devices which have been set to operate with a 4096 byte logical and physical block size (i.e. 4Kn), so a sector is actually 4096 (native) bytes rather than 512. We're building GPT hd images with genimage where the vfat boot filesystem is created with -S 4096 (sets the number of bytes per logical sector) and ext4 created with -b -4096 (specifies a minimum block size of 4096). All partitions are aligned correctly (we use 8M as we've seen improved performance on large capacity SD cards).

The GPT primary header is expected at LBA1. When genimage creates a GPT hdimage, it inserts the GPT primary header at offset +512 bytes into the image file: https://github.com/pengutronix/genimage/blob/master/image-hd.c#L534. This is fine for an LBA size of 512 (512e or 512n) but not 4Kn where it should be +4096 bytes.

To handle 4Kn devices, it appears as though genimage needs to know the logical sector size so it can factor it into it's calculations, e.g. have some abstraction for positioning of blobs that require an LBA offset rather than assuming one LBA unit is 512 bytes.

Boot firmware that is able to transparently support 512n, 512e and 4Kn will likely inspect attributes of the block device and use both its logical and physical block sizes in alignments checks and calculations. For example, a 4Kn device would need to have its GPT primary header offset 4K byte aligned. If it's always at offset +512 bytes, it's probably fair to say the firmware should assume a 512 byte IO unit size.

I'm not sure yet, but I suspect this only impacts GPT images. Lack of this support appears to indicate users only format disks like this on-device.

Do you think genimage needs to gain a logical-sector-size (defaults to 512) param which is used in the appropriate calculations, or are there other ways we can create 4Kn compatible images with GPT using genimage? -- Matt

learmj avatar Oct 13 '25 14:10 learmj

genimage doesn't have support for 4Kn images yet. A logical-sector-size parameter sounds good.

Is 4Kn with MBR used in the real world? If not, we could limit that support to GPT.

jluebbe avatar Oct 13 '25 15:10 jluebbe

As it happens, I've been experimenting with an implementation of this. @jluebbe are you interested in a PR once I get it done? I called the value lba-size and added it to hdimage_opts.

nrclark avatar Oct 13 '25 16:10 nrclark

lba-size would mean the size of the logical block addresses (8 bytes), which I'd find a bit confusing...

@michaelolbrich?

jluebbe avatar Oct 14 '25 07:10 jluebbe

Hmm, I would call it sector-size or something like that. Which one is relevant here anyways, the physical or the logical sector size?

michaelolbrich avatar Oct 14 '25 07:10 michaelolbrich

Just sector-size is ambiguous. In this case, the logical sector size is relevant. The physical sector size may be different and isn't used for addressing.

jluebbe avatar Oct 14 '25 07:10 jluebbe

logical-sector-size then.

michaelolbrich avatar Oct 14 '25 08:10 michaelolbrich

Is 4Kn with MBR used in the real world? If not, we could limit that support to GPT.

I think we ought to be able to support creation of 4Kn compatible images for both MBR and GPT. MBR is still a thing and is not going away any time soon. It's perfectly fine to use it for simple partitioning layouts.

There are issues with MBR, too. For example, if the genimage.cfg specifies:

   hdimage {
      align = 8M
   }

...the value at offset 0x08 in the first Partition Table Entry (offset 0x1be in the MBR) is 16384.

This value should be 2048 for a logical sector size of 4096 because we want the alignment to be 8M. The value of 16384 is only correct for a logical sector size of 512 with 8M alignment.

learmj avatar Oct 14 '25 13:10 learmj