usbboot
usbboot copied to clipboard
Fix compatibility with big endian hosts
The Pi bootcode expects the boot_message.length field to be sent in little endian, and sends the message.command in little endian. Make sure they are converted from whatever the host is using.
Out of curiosity, what is the use case that triggered this PR? You don't see much big-endian hardware these days.
@pelwell My main desktop computer is a Talos II (POWER9). But I also happen to run my Raspberry Pi 3 in big endian. :-) https://github.com/zeldin/linux-1/releases Now that I have a CM4 IO board with a PCIe slot, I hope to be able to debug the PCIe driver so that I can run Pi 4 in big endian mode as well...
Having grown up on a 68000, little-endian was an adjustment, but I prefer it now.
usbboot isn't a paragon of programming style, but I think a better fix would be to declare the length field of bootmsg as an array of 4 uint8_t
/unsigned chars
and add another global variable - bootmsg_length
, say - that holds the value in the natural endianness. You then only need to serialise just after receiving or just before sending, which doesn't need the union since you are working with an array.
Sure. That also covers the case if int
has a different size than 32 bits. I initially assumed you wanted to stay with C90 because I didn't see an include of <stdint.h>
and the length/command fields were int
rather than int32_t
, but I do see now that C99 types are used in other places of main.c
. I'll add the include line while I'm at it. :smile: