jacobrosenthal / hf2-rs

Microsoft HF2 HID Flashing Format for UF2 Bootloaders
49 stars 14 forks source link

Fix an incorrect number of zero bytes being inserted between sections #44

Closed yvt closed 2 years ago

yvt commented 2 years ago

This PR fixes a bug in the elf_to_bin function.

When processing the i-th section (where i > 0), start[i] - (start[0] + len[i - 1]) zero bytes are inserted after that section. This doesn't make any sense and causes sections to be written at incorrect locations under certain circumstances.

This PR updates the code so that start[i] - (start[i - 1] + len[i - 1]) = start[i] - end[i - 1] zero bytes are inserted before each section.

jacobrosenthal commented 2 years ago

Man that loop has all the bugs. Do you have a binary showing the problem we can add to those test cases?

jacobrosenthal commented 2 years ago

And does this by any chance close https://github.com/jacobrosenthal/hf2-rs/issues/38 can we get @sinyo-matu to confirm if so?

yvt commented 2 years ago

Do you have a binary showing the problem we can add to those test cases?

The problem is exhibited by an example from my RTOS, but I think I can whip up a smaller test case.

And does this by any chance close #38 can we get @sinyo-matu to confirm if so?

No, it's unrelated.

jacobrosenthal commented 2 years ago

I threw a commit on unifying the if statements that I think is an improvement.

Is this done? It tested a pygamer and it still works with this patch so seems ok

yvt commented 2 years ago

Yep, it's ready to merge.

Rahix commented 2 years ago

Just as a datapoint: The linker script changes in cortex-m-rt 0.7 seem to reliably trigger this bug. Glad it's fixed!