Writing Your Own Bootloader for STM32, Part 2.
While I was finishing the previous article, I came across a thread on Radiokot discussing what format to use for sending firmware to a bootloader. I had the same question, and at the end of the previous article I described why it is more convenient to send firmware as a hex file than as a binary; those interested can read it here.
So, we need to look at the record types in a hex file and extend the code from the previous article. Everything described below applies to KEIL and is essentially a free translation of this article.
Keil generates firmware in the Intel HEX format, which stores a hexadecimal representation of a binary file encoded with ASCII alphanumeric characters. A file with the hex extension can contain any number of records, each of which has the following format:
The groups consist of identical letters, and each letter corresponds to one hexadecimal digit.
00 — contains data
01 — last record in the file
02 — extended segment address
04 — the record contains an extended address needed to calculate the full address
05 — application start address, namely of the main function (MDK-ARM only)
Data record.
- 10 number of bytes in the record
- 2462 record start address
- 00 record type
- 464C...464C data
- 33 record checksum
Remember that data in a hex file is stored in hexadecimal, so the number of bytes in the record is 16, not 10 as you might think: 0x10 = 16. This applies to all the other fields too.
Last record in the file.
- 00 number of bytes in the record
- 0000 this field is ignored in this record.
- 01 record type
- FC the record checksum is calculated as
01h + NOT(00h + 00h + 00h + 01h).
An Intel HEX file must end with a record of this type.
Extended address.(HEX386)
- 02 number of bytes in the record
- 0000 address field, always 0000 for this record type
- 04 record type
- FFFF upper 16 bits of the address
- FC the record checksum is calculated as
01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).
The point is that a data record specifies only 16 bits of the address, for example 0x2462, while our address space is 32-bit. So to write the full address we are missing another 16 bits, which are exactly what this record contains. To calculate the full address where the data will be written, shift the address from this record 16 bits to the left and add the address from the data record to it.
We won't need the remaining two record types. I didn't encounter a type 02 record in my hex file at all; it is used in the HEX86 format, while ours is apparently HEX386 (I didn't look into how they differ). A type 05 record (application start address, namely of the main function) contains a 32-bit address and can be located anywhere; most importantly, it carries no useful information for writing flash memory, so it can be ignored, according to the documentation.
Now, using what we have learned, let's update the code from the previous article.
So we have developed a template for writing your own bootloader that parses hex files.
It was derived from the bootloader I use myself by replacing the function that reads a certain number of characters from the hex file with the comment /*read n bytes*/. Of course, it can be optimized and it's not a ready-made solution, but it took several weeks to figure all this out, and I'm publishing it to save you time, my dear reader)))
By the way, when compiling the firmware that will be fed to the bootloader, you need to specify that it should be located not at the beginning of memory but after the bootloader.

Since the topic is quite broad, one point was missed, namely relocating the interrupt vectors when jumping to the main program. The thing is, in the bootloader the interrupt vector table is located at one address, and in the main program at another (they can't share an interrupt vector table, since they are two separate programs compiled separately). To be able to work with interrupt vectors after jumping to the main program, they must be relocated either before jumping to the main program or at the beginning of the main program.
So, we need to look at the record types in a hex file and extend the code from the previous article. Everything described below applies to KEIL and is essentially a free translation of this article.
Keil generates firmware in the Intel HEX format, which stores a hexadecimal representation of a binary file encoded with ASCII alphanumeric characters. A file with the hex extension can contain any number of records, each of which has the following format:
:llaaaatt[dd...]ccThe groups consist of identical letters, and each letter corresponds to one hexadecimal digit.
- : start of record
- ll number of data bytes dd in the record
- aaaa record start address
- tt record type:
00 — contains data
01 — last record in the file
02 — extended segment address
04 — the record contains an extended address needed to calculate the full address
05 — application start address, namely of the main function (MDK-ARM only)
- dd — data written to the MCU memory
- cc — checksum
Data record.
:10246200464C5549442050524F46494C4500464C33- 10 number of bytes in the record
- 2462 record start address
- 00 record type
- 464C...464C data
- 33 record checksum
Remember that data in a hex file is stored in hexadecimal, so the number of bytes in the record is 16, not 10 as you might think: 0x10 = 16. This applies to all the other fields too.
Last record in the file.
:00000001FF- 00 number of bytes in the record
- 0000 this field is ignored in this record.
- 01 record type
- FC the record checksum is calculated as
01h + NOT(00h + 00h + 00h + 01h).
An Intel HEX file must end with a record of this type.
Extended address.(HEX386)
:02000004FFFFFC- 02 number of bytes in the record
- 0000 address field, always 0000 for this record type
- 04 record type
- FFFF upper 16 bits of the address
- FC the record checksum is calculated as
01h + NOT(02h + 00h + 00h + 04h + FFh + FFh).
The point is that a data record specifies only 16 bits of the address, for example 0x2462, while our address space is 32-bit. So to write the full address we are missing another 16 bits, which are exactly what this record contains. To calculate the full address where the data will be written, shift the address from this record 16 bits to the left and add the address from the data record to it.
Address from the data record 2462
Address from this record FFFF
-----------
Absolute address FFFF2462We won't need the remaining two record types. I didn't encounter a type 02 record in my hex file at all; it is used in the HEX86 format, while ours is apparently HEX386 (I didn't look into how they differ). A type 05 record (application start address, namely of the main function) contains a 32-bit address and can be located anywhere; most importantly, it carries no useful information for writing flash memory, so it can be ignored, according to the documentation.
Now, using what we have learned, let's update the code from the previous article.
#include "stm32f10x.h"
#define APPLICATION_ADDRESS 0x08008400//program start address
//choose the page size depending on the MCU model
#if defined (STM32F10X_HD) || defined (STM32F10X_HD_VL) || defined (STM32F10X_CL) || defined (STM32F10X_XL)
#define FLASH_PAGE_SIZE ((uint16_t)0x800)
#else
#define FLASH_PAGE_SIZE ((uint16_t)0x400)
#endif
uint8_t buff[8];
/*function that converts ascii to hex*/
void Ascii_To_Hex( uint8_t* buff, uint8_t count)
{
uint8_t i;
for(i=0; i<count;i++)
{
if(buff[i] <= '9' && buff[i] >= '0' )
{
buff[i] -= 0x30;
}
else
{
buff[i] = buff[i] - 0x41 + 10;
}
}
}
void Go_To_User_App(void)
{
uint32_t app_jump_address;
typedef void(*pFunction)(void);//declare a user type
pFunction Jump_To_Application;//and create a variable of this type
__disable_irq();//disable interrupts
app_jump_address = *( uint32_t*) (APPLICATION_ADDRESS + 4); //get the jump address from the Reset vector
Jump_To_Application = (pFunction)app_jump_address; //cast it to the user type
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); //set the application SP
Jump_To_Application(); //start the application
}
int main(void)
{
uint8_t i;
uint16_t count_page = 20;//number of pages to erase
uint16_t erase_counter = 0x00;//counter
uint32_t extented_linear_adress = 0;//extended address
uint8_t size_data, type_data, check_sum;//size, data type and checksum
uint16_t address_data;//lower 16 bits of the address
uint32_t program_data;//word written to flash
uint8_t calculation_check_sum = 0;//checksum
volatile FLASH_Status FLASHStatus = FLASH_COMPLETE;
//if new firmware is found
if(new_firmware)
{
//unlock the flash
FLASH_Unlock();
//clear the flags
FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPRTERR);
//erase the pages
for(erase_counter = 0; (erase_counter <= count_page) && (FLASHStatus == FLASH_COMPLETE); erase_counter++)
{
FLASHStatus = FLASH_ErasePage(APPLICATION_ADDRESS + (FLASH_PAGE_SIZE * erase_counter));
}
//update
while(1)
{
/*read one character at a time until the start of a record is found*/
if(buff[0] == ':')
{
/*read 8 characters of metadata*/
Ascii_To_Hex(buff, 8);//convert them to hex
size_data = 2*(buff[1] + 16*buff[0]);//find the data size
address_data = buff[5] + 16*buff[4] + 256*buff[3] + 4096*buff[2];//address
type_data = buff[7] + 16*buff[6];//and type
calculation_check_sum = size_data/2 + (uint8_t)address_data + (uint8_t)(address_data>>8) + type_data;//calculate the checksum
if(type_data == 0x00)
{
while(size_data>0 && (FLASHStatus == FLASH_COMPLETE))
{
/*read 8 characters*/
Ascii_To_Hex(buff, 8);//convert them to hex
for(i=0; i<8;i=i+2)//build the 32-bit word to write
{
buff[i] <<= 4;
buff[i] = buff[i] | buff[i+1];
program_data |= buff[i] <<(i*4);
}
FLASHStatus = FLASH_ProgramWord(extented_linear_adress + address_data, program_data );
calculation_check_sum += (uint8_t)program_data + (uint8_t)(program_data>>8) + (uint8_t)(program_data>>16) + (uint8_t)(program_data>>24);
size_data -=8;
address_data +=4;
program_data = 0;
}
calculation_check_sum = ~(calculation_check_sum) + 1;
/*read 2 bytes*/
Ascii_To_Hex(buff,2);
check_sum = buff[1] + 16*buff[0];
if(calculation_check_sum != check_sum )
{
//here you can display a message like "Checksum error"
}
calculation_check_sum = 0;//reset the checksum
}
else if(type_data == 0x04)//extended address
{
/*read 4 bytes*/
extented_linear_adress = (uint32_t)(buff[0]<<28 | buff[1]<<24 | buff[2]<<20 | buff[3]<<16 );//calculate the address
calculation_check_sum += buff[0] + buff[1]+ buff[3] + buff[3];
calculation_check_sum = ~(calculation_check_sum) + 1;
/*read 2 bytes*/
Ascii_To_Hex(buff,2);
check_sum = buff[1] + 16*buff[0];
if(calculation_check_sum != check_sum )
{
//here you can display a message like "Checksum error"
}
calculation_check_sum = 0;//reset the checksum
}
else if(type_data == 0x01)//end of file
{
break;
}
}
FLASH_Lock();//lock the flash
}
}
Go_To_User_App();//jump to the main program
}
So we have developed a template for writing your own bootloader that parses hex files.
It was derived from the bootloader I use myself by replacing the function that reads a certain number of characters from the hex file with the comment /*read n bytes*/. Of course, it can be optimized and it's not a ready-made solution, but it took several weeks to figure all this out, and I'm publishing it to save you time, my dear reader)))
By the way, when compiling the firmware that will be fed to the bootloader, you need to specify that it should be located not at the beginning of memory but after the bootloader.

Since the topic is quite broad, one point was missed, namely relocating the interrupt vectors when jumping to the main program. The thing is, in the bootloader the interrupt vector table is located at one address, and in the main program at another (they can't share an interrupt vector table, since they are two separate programs compiled separately). To be able to work with interrupt vectors after jumping to the main program, they must be relocated either before jumping to the main program or at the beginning of the main program.
__set_PRIMASK(1); //disable interrupts
SCB->VTOR = APPLICATION_ADDRESS;//move the start of the vector table to the specified address
__set_PRIMASK(0);//enable interrupts