cancel
Showing results for 
Search instead for 
Did you mean: 

Custom UART Bootloader code - need to Verify application code before jump and execute it.

Ibra
Associate II

hello,

I already have developed a custom uart bootloader program on STMcube IDE for STM32f446 (Nucleo board) and an host C# app to fully functionality of bootloader services and i functional well.

The problem is that i need to add feature which make sure or verify the integrity of the user app code before run it (i.e. after it receive Go_to_User_App command from C# app/tool). the implementation i made is that is to append the 32-bits CRC code of the user app at the begining of the .bin file. so, the 1st word of app is now CRC code of app. and 2nd word is the MSP. and 3rd word is the REST handler. after doing that the user app is not running as expected!

so, need more help for identify the issue up there please.

below is screenshots illustrations.

*this is the memory layout after flashing the user app code with appending its CRC at the 1st word at sector 2 (address of 0x08008000)*

0693W00000Y93qiQAB.png 

*this is the C function in bootloader code that perform the jump to user app without checking the CRC of app code yet*

0693W00000Y93ukQAB.png

8 REPLIES 8
gbm
Lead III

Put the CRC at the end of your code, not at the beginning.

Or:

Read, save into a variable but do not program into Flash the CRC value, then program the 2nd word of data to the first Flash location, then, after programming the whole content, check the CRC.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Ibra
Associate II

I need to make the CRC check at every time i send the go_to_user_app command.

# by doing your 1st solution: then i can not know which flash address holds the CRC of the current user app flashed. (so, i appended the CRC at the beginning of app code to be flashed)

# by doing your 2nd solution: then i can not perform the required goal. the checking is just when only the MCU is power on. (so, i stored the CRC in flash memory not RAM)

gbm
Lead III

You may put the CRC at 0x08008020 - it's not used by F4.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
Ibra
Associate II

can't do that as then it become hard coded for only this specific simple flasher user app code!

i need it to be generic to any app the user want to flash at mcu so,i put it in generic fixed address that is at the start address of the user app code sector. i.e sector 2 here

Ibra
Associate II

can't do that as then it become hard coded for only this specific simple flasher user app code!

i need it to be generic to any app the user want to flash at mcu so,i put it in generic fixed address that is at the start address of the user app code sector. i.e sector 2 here

Ibra
Associate II

I need to make the CRC check at every time i send the go_to_user_app command.

# by doing your 1st solution: then i can not know which flash address holds the CRC of the current user app flashed. (so, i appended the CRC at the beginning of app code to be flashed)

# by doing your 2nd solution: then i can not perform the required goal. the checking is just when only the MCU is power on. (so, i stored the CRC in flash memory not RAM)

gbm
Lead III

It's quite generic. Look at ARMv8-M architecture definition and find the unused exception vector in the vector table (first 16 words of memory), then use it for CRC. The exception vector table must start at aligned address, like multiple of 256 - you can't locate it at address 4.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Is this part of the vector table or in front of it?

How do you compute the CRC on the PC side?

Do you need to know the length of the image?

I would tend to put the length in an unused vector, and put the CRC at the end, and then repeat the computation as it was done on the PC side to verify they are correct.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..