STM32 Bootloader
Customizable Bootloader for STM32 microcontrollers
bootloader.c File Reference

This file contains the functions of the bootloader. The bootloader implementation uses the official HAL library of ST. More...

#include "bootloader.h"

Go to the source code of this file.

Macros

#define BOOTLOADER_VERSION_MAJOR   1
 
#define BOOTLOADER_VERSION_MINOR   1
 
#define BOOTLOADER_VERSION_PATCH   3
 
#define BOOTLOADER_VERSION_RC   0
 

Typedefs

typedef void(* pFunction) (void)
 

Functions

uint8_t Bootloader_Init (void)
 This function initializes bootloader and flash. More...
 
uint8_t Bootloader_Erase (void)
 This function erases the user application area in flash. More...
 
uint8_t Bootloader_FlashBegin (void)
 Begin flash programming: this function unlocks the flash and sets the data pointer to the start of application flash area. More...
 
uint8_t Bootloader_FlashNext (uint64_t data)
 Program 64bit data into flash: this function writes an 8byte (64bit) data chunk into the flash and increments the data pointer. More...
 
uint8_t Bootloader_FlashEnd (void)
 Finish flash programming: this function finalizes the flash programming by locking the flash. More...
 
uint8_t Bootloader_GetProtectionStatus (void)
 This function returns the protection status of flash. More...
 
uint8_t Bootloader_ConfigProtection (uint32_t protection)
 This function configures the wirte protection of flash. More...
 
uint8_t Bootloader_CheckSize (uint32_t appsize)
 This function checks whether the new application fits into flash. More...
 
uint8_t Bootloader_VerifyChecksum (void)
 This function verifies the checksum of application located in flash. If USE_CHECKSUM configuration parameter is disabled then the function always returns an error code. More...
 
uint8_t Bootloader_CheckForApplication (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. More...
 
void Bootloader_JumpToApplication (void)
 This function performs the jump to the user application in flash. More...
 
void Bootloader_JumpToSysMem (void)
 This function performs the jump to the MCU System Memory (ST Bootloader). More...
 
uint32_t Bootloader_GetVersion (void)
 This function returns the version number of the bootloader library. Semantic versioning is used for numbering. More...
 

Variables

static uint32_t flash_ptr = APP_ADDRESS
 

Detailed Description

This file contains the functions of the bootloader. The bootloader implementation uses the official HAL library of ST.

STM32 Bootloader Source

Author
Akos Pasztor
See also
Please refer to README for detailed information.

Definition in file bootloader.c.

Macro Definition Documentation

◆ BOOTLOADER_VERSION_MAJOR

#define BOOTLOADER_VERSION_MAJOR   1

Major version

Definition at line 20 of file bootloader.c.

◆ BOOTLOADER_VERSION_MINOR

#define BOOTLOADER_VERSION_MINOR   1

Minor version

Definition at line 21 of file bootloader.c.

◆ BOOTLOADER_VERSION_PATCH

#define BOOTLOADER_VERSION_PATCH   3

Patch version

Definition at line 22 of file bootloader.c.

◆ BOOTLOADER_VERSION_RC

#define BOOTLOADER_VERSION_RC   0

Release candidate version

Definition at line 23 of file bootloader.c.

Typedef Documentation

◆ pFunction

typedef void(* pFunction) (void)

Function pointer definition

Definition at line 26 of file bootloader.c.

Function Documentation

◆ Bootloader_CheckForApplication()

uint8_t Bootloader_CheckForApplication ( 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
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKif first DWORD represents a valid stack pointer location
BL_NO_APPfirst DWORD value is out of RAM boundaries

Definition at line 395 of file bootloader.c.

◆ Bootloader_CheckSize()

uint8_t Bootloader_CheckSize ( uint32_t  appsize)

This function checks whether the new application fits into flash.

Parameters
appsizesize of application
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKif application fits into flash
BL_SIZE_ERRORif application does not fit into flash

Definition at line 338 of file bootloader.c.

◆ Bootloader_ConfigProtection()

uint8_t Bootloader_ConfigProtection ( uint32_t  protection)

This function configures the wirte protection of flash.

Parameters
protectionprotection type eFlashProtectionTypes
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKupon success
BL_OBP_ERRORupon failure

Definition at line 263 of file bootloader.c.

◆ Bootloader_Erase()

uint8_t Bootloader_Erase ( void  )

This function erases the user application area in flash.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKupon success
BL_ERRupon failure

Definition at line 56 of file bootloader.c.

◆ Bootloader_FlashBegin()

uint8_t Bootloader_FlashBegin ( void  )

Begin flash programming: this function unlocks the flash and sets the data pointer to the start of application flash area.

See also
README for futher information
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKis returned in every case

Definition at line 100 of file bootloader.c.

◆ Bootloader_FlashEnd()

uint8_t Bootloader_FlashEnd ( void  )

Finish flash programming: this function finalizes the flash programming by locking the flash.

See also
README for futher information
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKis returned in every case

Definition at line 159 of file bootloader.c.

◆ Bootloader_FlashNext()

uint8_t Bootloader_FlashNext ( uint64_t  data)

Program 64bit data into flash: this function writes an 8byte (64bit) data chunk into the flash and increments the data pointer.

See also
README for futher information
Parameters
data64bit data chunk to be written into flash
Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKupon success
BL_WRITE_ERRORupon failure

Definition at line 120 of file bootloader.c.

◆ Bootloader_GetProtectionStatus()

uint8_t Bootloader_GetProtectionStatus ( void  )

This function returns the protection status of flash.

Returns
Flash protection status eFlashProtectionTypes

Definition at line 171 of file bootloader.c.

◆ Bootloader_GetVersion()

uint32_t Bootloader_GetVersion ( void  )

This function returns the version number of the bootloader library. Semantic versioning is used for numbering.

See also
Semantic versioning: https://semver.org
Returns
Bootloader version number combined into an uint32_t:
  • [31:24] Major version
  • [23:16] Minor version
  • [15:8] Patch version
  • [7:0] Release candidate version

Definition at line 471 of file bootloader.c.

◆ Bootloader_Init()

uint8_t Bootloader_Init ( void  )

This function initializes bootloader and flash.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKis returned in every case

Definition at line 37 of file bootloader.c.

◆ Bootloader_JumpToApplication()

void Bootloader_JumpToApplication ( void  )

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

The function carries out the following operations:

  • De-initialize the clock and peripheral configuration
  • Stop the systick
  • Set the vector table location (if SET_VECTOR_TABLE is enabled)
  • Sets the stack pointer location
  • Perform the jump

Definition at line 410 of file bootloader.c.

◆ Bootloader_JumpToSysMem()

void Bootloader_JumpToSysMem ( void  )

This function performs the jump to the MCU System Memory (ST Bootloader).

The function carries out the following operations:

  • De-initialize the clock and peripheral configuration
  • Stop the systick
  • Remap the system flash memory
  • Perform the jump

Definition at line 439 of file bootloader.c.

◆ Bootloader_VerifyChecksum()

uint8_t Bootloader_VerifyChecksum ( void  )

This function verifies the checksum of application located in flash. If USE_CHECKSUM configuration parameter is disabled then the function always returns an error code.

Returns
Bootloader error code eBootloaderErrorCodes
Return values
BL_OKif calculated checksum matches the application checksum
BL_CHKS_ERRORupon checksum mismatch or when USE_CHECKSUM is disabled

Definition at line 353 of file bootloader.c.

Variable Documentation

◆ flash_ptr

uint32_t flash_ptr = APP_ADDRESS
static

Private variable for tracking flashing progress

Definition at line 30 of file bootloader.c.