abcminiuser / lufa

LUFA - the Lightweight USB Framework for AVRs.
http://www.lufa-lib.org
1.03k stars 321 forks source link

Fix BootloaderCDC flashing over 32k #170

Closed exp closed 3 years ago

exp commented 3 years ago

When loading the bytes from the USB serial stream that represent the current position in words, BootloaderCDC uses a trick which is too clever for GCC in order to populate a u32 and double the integer value.

In my testing, when the size of the flashed item exceeds 32k, the asm generated by avr-gcc (both v5 and v10) incorrectly manipulates the u32, leading to its upper 2 bytes being set to 0xFF. This then fails the Address < BOOT_START_ADDR test in IsPageAddressValid.

This PR adds an explicit u32 cast to the function calls, leading to avr-gcc treating the u32 correctly and populating all 4 bytes.

Disclaimer: I am not familiar with AVR ASM and have based this on functional testing as well as an approximate analysis.

NicoHood commented 3 years ago

From my experience this cast looks correct and required. I guess that most USB MCUs tested simply do not have that much space, so the issue never popped up.

abcminiuser commented 3 years ago

Oops - yes, with the shift it will implicitly up-cast the uint8_t to a uint16_t, but no further. Thanks!