vdudouyt / stm8flash

program your stm8 devices with SWIM/stlinkv(1,2)
GNU General Public License v2.0
401 stars 182 forks source link

stlinkv2: fix the flash programming mode selection #170

Closed martin-kaiser closed 8 months ago

martin-kaiser commented 9 months ago

For writing a flash block, we can select fast block programming or standard block programming.

Fast block programming works only if the block has been erased before, i.e. if all of its bytes are 0x00.

The current implementation checks the bytes in the "buffer" array. If all bytes of our block are 0x00, it uses fast mode. Otherwise, standard mode is selected. This is wrong: buffer contains the data that we want to write, not the data that is currently in the flash block.

I spotted this problem when I uploaded a file with large areas of 0x00 bytes. For a block within such a 0x00 area, fast block programming was selected although the block in flash was not empty. The block with 0x00s was not written, the resulting flash image was corrupt.

Reproducing the issue is as simple as

stm8flash -c stlinkv2 -p stm8l151c8 -s flash -w dd if=/dev/zero of=./only00 bs=1k count=32 stm8flash -c stlinkv2 -p stm8l151c8 -s flash -w ./only00 stm8flash -c stlinkv2 -p stm8l151c8 -s flash -v ./only00 Verification returns an error.

Fix this by checking the current content of the flash block instead of the data that we want to upload. This works only if we have read the block from flash before. If we haven't read the block, we have default to standard block programming.

With this patch applied, the test above results in a successful verification.

martin-kaiser commented 8 months ago

gentle ping - is there any chance that this can be reviewed and merged?