Search code examples
stm32microcontroller

STM32 hex file, determine if it needs a custom bootloader


it is possible to determine somehow if a hex file (that I know is compiled/intended to work on a STM32F405) can be flashed to a blank STM32F405 chip, or if this hex is just a "firmware update" that needs a custom bootloader to run on, hence can only be flashed on a chip that already has that particular bootloader in it?

I could just try to flash it on a blank chip, but at the moment I don't have a STM32F405 prototype board so if possible I'd like to understand that before building a PCB for this task.

this is the start of the hex file (posted as suggested in the comment below)

:020000040800F2
:10000000F8F40120690200089528000897280008E4
:100010009B2800089F280008A32800080000000073
:10002000000000000000000000000000A9280008F7
:10003000A728000800000000AB280008AD28000831
:10004000830200088302000883020008830200087C
:100050008302000883020008DD280008EF2800085A
:100060000129000813290008252900088302000837
:10007000830200088302000883020008CD280008DC
:10008000830200088302000883020008830200083C
:100090008302000883020008830200083729000851
:1000A000830200088302000883020008830200081C
:1000B000830200088302000883020008830200080C
:1000C00083020008830200088302000883020008FC
:1000D000C5280008D528000883020008830200080C
:1000E00089290008830200088302000883020008AF
:1000F00083020008830200088302000883020008CC
:1001000083020008B5280008830200088302000863
:1001100083020008830200088302000883020008AB
:10012000830200088302000883020008BD2800083B
:10013000830200088302000883020008830200088B

Solution

  • The Intel HEX format is explained in depth for example on this Wikipedia page.

    Let's start the analysis with the first record.

    :020000040800F2: The record type is '04', designating an extended linear address. The data field contains the upper 16 bits of the 32 bit address, '0800'. Citing the Wikipedia page:

    The absolute address for a type 00 record is formed by combining the upper 16 address bits of the most recent 04 record with the low 16 address bits of the 00 record.

    Therefore, the following code bytes are located at 0x0800xxxx, xxxx being the addresses of the data records. According to the data sheet, this is the first 64 KiB page of the flash. The first following data record has the address 0x0000, resulting in the absolute address 0x08000000.

    The regular pattern of the following bytes looks like a vector table.

    Side note: Please be aware that the records in a Intel HEX file don't need to be in order.

    If your hex file is larger than about 130 KiB (two times 64 KiB because of two hex characters per byte), we can expect another extended linear address record with '0801', and so on.

    Depending on your boot configuration, we can assume that this code needs no specific bootloader to run.