Bizarre: Flash content not as expected after flashing with CubeIDE, depending on compiled code
I have a project using the STM32L072CZU6, with a custom bootloader that computes CRCs over blocks of the App part in the flash and compares it with block-CRCs in the app header.
This has worked for this project for 2 years or so, from time to time adding a bit of functionality to the app code.
... ah, forget about the long story of what I did so far, I narrowed this down a lot ...
My current firmware image is logically divided into 7 blocks of 16K, the last block is only ~14K here.
Now, extracting, via debugger, that last block, which has the bad CRC problem, and compared it to the same block from my source image. At a certain offset, the hex editor reveals that 4 extra bytes are "inserted" in what I read back from flash, compared to the original image, sucth that the rest of the image is shifted, in memory, 4 bytes forwards.The offset where that happens is, from app image start: 6*16K + 864 bytes. The debug config is set up to flash the bootloader of 32K first, but IIRC the loader vs. app are handled separately, as it has 2 .ELF files for debugging.
So, it's not broken flash or so.
I deleted the 4 bytes in the editor, and the contents then all matched, save missing 4 byts at the end - and all the CRCs on bootloader and hader generator ends check out here, so it's not that my debugger had a hiccup when extracting the memory - this really was what caused the (exact same value) bad CRC.
I see only 2 possible branches of how this can happen:
- when my app header generator program calculates the CRC for the last block, the data this is based on differs from the saved .ELF file that CubeIDE uses to flash: I use an .elf read/write library to get the data, compute CRC, and enter the CRC values into a header section, which I added in the linkerscript.
- or, the CubeIDE flash algorithm does weird stuff
What's bizarre to me is that:
- this happens only for a version of my app code that has some 650 bytes of change in them, where particular code is linked in, because the new module gets called in the code
- when I comment out the call, so the linker throws it out, and add nonsense code to inflate the image artificially, to the exact same size as the bad image - the CRC values are all good and all is fine - so it's not an issue with the app image size, but the content... which I find rather puzzling.
Anyone seen such effect before?
I will, of course, examin the "bad elf handling" part on my side, but the 3rd party ELF library, which I did not update ever so far, has not given me any trouble so far, and I just load the original .ELF as output by CubeIDE, write something into my header section, then save an .ELF again... which has all worked for 34 versions of my app software, without changing this process in between.
