Skip to content

image-hd: fix endianness of MBR and EBR partition entries on big-endian hosts - #347

Open
pranavkaruvally wants to merge 2 commits into
pengutronix:masterfrom
pranavkaruvally:s390x-fix
Open

pranavkaruvally wants to merge 2 commits into
pengutronix:masterfrom
pranavkaruvally:s390x-fix

Conversation

@pranavkaruvally

Copy link
Copy Markdown

Write MBR/EBR partition entries in little-endian byte order. It was being written in native byteorder in big endian machines.

Fixes s390x in #335
Addresses: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1149583

@pranavkaruvally

Copy link
Copy Markdown
Author

Hi @michaelolbrich
Can you please take a look at this?

@giritrivedi giritrivedi left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Check other code paths that read relative_sectors/total_sectors from an in-memory MBR struct that originated from disk.

Comment thread image-hd.c Outdated

memset(&mbr, 0, sizeof(mbr));
memcpy(&mbr.disk_signature, &hd->disksig, sizeof(hd->disksig));
uint32_t tmp_disksig = htole32(hd->disksig);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

sizeof(hd->disksig) lets memcpy know how many bytes to copy.
No endianness conversion is needed.

@pranavkaruvally pranavkaruvally Oct 29, 2026 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

hd->disksig is also being used as the source argument in that memcpy. Without the endianness conversion, this will lead to failure in big endian machines as the MBR entries are supposed to be in little-endian format.

Comment thread image-hd.c
lba_to_chs(le32toh(entry->relative_sectors) + offset_sectors,
entry->first_chs);
lba_to_chs(entry->relative_sectors + entry->total_sectors - 1 + offset_sectors,
lba_to_chs(le32toh(entry->relative_sectors) + le32toh(entry->total_sectors) - 1 + offset_sectors,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

le32toh is called on entry->relative_sectors twice in two separate expressions. Looks to be slightly wasteful and inconsistent, instead use a local variable

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, I would expect the compiler to optimize this anyway. And I don't think another variable will make the code cleaner in this case.
So I don't mind either way.

Comment thread image-hd.c
entry->partition_type = 0xee;
entry->relative_sectors = 1;
entry->total_sectors = hd->gpt_location / 512 + GPT_SECTORS - 2;
entry->relative_sectors = htole32(1);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is not needed. htole32(1) is 1 on all platforms.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

htole32(1) is not 1 on big endian platforms

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The point i wanted to make was, we need to keep the constant 1 as is on all platforms so that relative_sectors will remain 1 on all platforms.
With htole32(1) relative_sectors will have some other values stored in it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

htole32(1) is correct here.

@pranavkaruvally

Copy link
Copy Markdown
Author

Hi @sudipm-mukherjee
Looping you in.

Thanks

@michaelolbrich michaelolbrich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks mostly good. Take a look at the failing test. It's a formatting issue.

And we really need a CI check for this. Maybe an action that builds and runs the whole thing in an x390 qemu? It would be fine if some of the tests fail for now, but I want to see it if we break things.

Comment thread image-hd.c
lba_to_chs(le32toh(entry->relative_sectors) + offset_sectors,
entry->first_chs);
lba_to_chs(entry->relative_sectors + entry->total_sectors - 1 + offset_sectors,
lba_to_chs(le32toh(entry->relative_sectors) + le32toh(entry->total_sectors) - 1 + offset_sectors,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Actually, I would expect the compiler to optimize this anyway. And I don't think another variable will make the code cleaner in this case.
So I don't mind either way.

Comment thread image-hd.c
entry->partition_type = 0xee;
entry->relative_sectors = 1;
entry->total_sectors = hd->gpt_location / 512 + GPT_SECTORS - 2;
entry->relative_sectors = htole32(1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

htole32(1) is correct here.

…an hosts

Signed-off-by: Pranav P <pranavsdreams@gmail.com>
@pranavkaruvally

Copy link
Copy Markdown
Author

Thanks for the reviews @michaelolbrich
Would the IBM LinuxOne Community cloud be enough to set up a testing infrastructure? (https://linuxone.cloud.marist.edu/#/login)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants