STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
Bootloader

Macros

#define USE_WRITE_PROTECTION   0
 
#define CLEAR_RESET_FLAGS   1
 
#define APP_FILE_NAME   "app-demo.bin"
 
#define CRC_FILE_NAME   "app-demo.crc"
 
#define APP_ADDRESS   ((uint32_t)&LINKER_APP_AREA_ORIGIN)
 
#define APP_SIZE   ((uint32_t)&LINKER_APP_AREA_LENGTH)
 

Typedefs

typedef void(* Function) (void)
 

Functions

bool BootloaderUpdateFirmware (void)
 This function performs the complete application update procedure. More...
 
bool BootloaderCheckForApplication (void)
 This function checks whether a valid application exists in flash. More...
 
void BootloaderJumpToApplication (void)
 This function performs the jump to the user application in flash. More...
 
void BootloaderJumpToSysMem (void)
 This function performs the jump to the microcontroller System Memory (ST built-in bootloader). More...
 
static bool CheckApplicationSize (void)
 Check whether the application binary on the SD card fits into the available flash memory region designated for the application. More...
 
static bool CheckApplicationCrc (void)
 Validate the integrity of the application binary by comparing a CRC-32 value calculated over the file contents against the reference CRC value read from a separate file on the SD card. More...
 
static bool UpdateApplication (void)
 Erase the application flash region and program it with the contents of the application binary file from the SD card, page by page. More...
 
static bool VerifyApplication (void)
 Verify the programmed flash contents against the original application binary file on the SD card by performing a word-by-word comparison. More...
 
static bool CalculateCrcOfFile (uint32_t *const crc)
 Calculate the CRC-32 checksum of the application binary file on the SD card using the hardware CRC peripheral. More...
 
static bool ReadCrcFromFile (uint32_t *const crc)
 Read the expected CRC-32 value from a dedicated CRC file on the SD card and convert the hexadecimal string representation to an integer. More...
 

Variables

const uint32_t LINKER_APP_AREA_ORIGIN
 
const uint32_t LINKER_APP_AREA_LENGTH
 
static uint8_t readBuffer [FLASH_PAGE_SIZE] = {0}
 

Detailed Description

Macro Definition Documentation

◆ APP_ADDRESS

#define APP_ADDRESS   ((uint32_t)&LINKER_APP_AREA_ORIGIN)

Helper define for the application area start address

Definition at line 70 of file bootloader.h.

◆ APP_FILE_NAME

#define APP_FILE_NAME   "app-demo.bin"

File name of application image located on SD card

Definition at line 46 of file bootloader.h.

◆ APP_SIZE

#define APP_SIZE   ((uint32_t)&LINKER_APP_AREA_LENGTH)

Helper define for the application area size

Definition at line 83 of file bootloader.h.

◆ CLEAR_RESET_FLAGS

#define CLEAR_RESET_FLAGS   1

Clear reset flags

  • If enabled: bootloader clears reset flags. (This occurs only when OBL RST flag is active.)
  • If disabled: bootloader does not clear reset flags, not even when OBL RST is active.

Definition at line 43 of file bootloader.h.

◆ CRC_FILE_NAME

#define CRC_FILE_NAME   "app-demo.crc"

File name that contains the application CRC located on SD card

Definition at line 49 of file bootloader.h.

◆ USE_WRITE_PROTECTION

#define USE_WRITE_PROTECTION   0

Enable write protection after performing in-app-programming

Definition at line 35 of file bootloader.h.

Typedef Documentation

◆ Function

typedef void(* Function) (void)

Function pointer type definition

Definition at line 43 of file bootloader.c.

Function Documentation

◆ BootloaderCheckForApplication()

bool BootloaderCheckForApplication ( void  )

This function checks whether a valid application exists in flash.

The check is performed by checking the very first DWORD (4 bytes) of the application firmware. In case of a valid application, this DWORD must represent the initialization location of stack pointer - which must be within the boundaries of RAM.

Returns
True if there is a valid application found in the microcontroller flash application area.

Definition at line 123 of file bootloader.c.

◆ BootloaderJumpToApplication()

void BootloaderJumpToApplication ( void  )

This function performs the jump to the user application in flash.

The function carries out the following operations:

  • Sets the vector table location
  • Sets the stack pointer location
  • Performs the jump

Definition at line 159 of file bootloader.c.

◆ BootloaderJumpToSysMem()

void BootloaderJumpToSysMem ( void  )

This function performs the jump to the microcontroller System Memory (ST built-in bootloader).

The function carries out the following operations:

  • Remaps the system flash memory
  • Performs the jump

Definition at line 201 of file bootloader.c.

◆ BootloaderUpdateFirmware()

bool BootloaderUpdateFirmware ( void  )

This function performs the complete application update procedure.

The function checks the application size, the application CRC; then carries out the actual firmware update process and finally verifies if the update was successful.

Returns
True if the entire process was successful; otherwise false.

Definition at line 110 of file bootloader.c.

◆ CalculateCrcOfFile()

static bool CalculateCrcOfFile ( uint32_t *const  crc)
static

Calculate the CRC-32 checksum of the application binary file on the SD card using the hardware CRC peripheral.

Parameters
crcPointer to store the calculated CRC-32 value.
Returns
True if the CRC calculation completed successfully; false if the file could not be opened or read.

Definition at line 451 of file bootloader.c.

◆ CheckApplicationCrc()

static bool CheckApplicationCrc ( void  )
static

Validate the integrity of the application binary by comparing a CRC-32 value calculated over the file contents against the reference CRC value read from a separate file on the SD card.

Returns
True if the CRC values match; false on CRC mismatch or if the CRC could not be calculated/read.

Definition at line 269 of file bootloader.c.

◆ CheckApplicationSize()

static bool CheckApplicationSize ( void  )
static

Check whether the application binary on the SD card fits into the available flash memory region designated for the application.

Returns
True if the application size is within the allowed limit; false if the application file cannot be opened or exceeds the maximum size.

Definition at line 243 of file bootloader.c.

◆ ReadCrcFromFile()

static bool ReadCrcFromFile ( uint32_t *const  crc)
static

Read the expected CRC-32 value from a dedicated CRC file on the SD card and convert the hexadecimal string representation to an integer.

Parameters
crcPointer to store the parsed CRC-32 value.
Returns
True if the CRC value was read and converted successfully; false if the file could not be opened, read, or the value could not be converted.

Definition at line 493 of file bootloader.c.

◆ UpdateApplication()

bool UpdateApplication ( void  )
static

Erase the application flash region and program it with the contents of the application binary file from the SD card, page by page.

Returns
True if flash programming completed successfully; false on file read error or flash write error.

Definition at line 297 of file bootloader.c.

◆ VerifyApplication()

static bool VerifyApplication ( void  )
static

Verify the programmed flash contents against the original application binary file on the SD card by performing a word-by-word comparison.

Returns
True if the flash contents match the file; false on verification mismatch or file read error.

Definition at line 376 of file bootloader.c.

Variable Documentation

◆ LINKER_APP_AREA_LENGTH

const uint32_t LINKER_APP_AREA_LENGTH
extern

Application area size address defined in the linker file

◆ LINKER_APP_AREA_ORIGIN

const uint32_t LINKER_APP_AREA_ORIGIN
extern

Application area start address defined in the linker file

◆ readBuffer

uint8_t readBuffer[FLASH_PAGE_SIZE] = {0}
static

Read buffer for reading data from a file on the SD card

Definition at line 47 of file bootloader.c.