ObjectiveThe purpose of this document is to develop a fully compatible bootloader based on the basic requirements for firmware upgrades (local upgrade via USB) for STM32.USB Upgrade Process
Detect if a USB drive is present during power-up;
If a USB drive is detected, check if there is an upgrade file with the corresponding name on the USB drive; if no USB drive is found, directly jump to the application program;
If an upgrade file is present, start reading the file and write it to the flash firmware update area; if not, jump to the application program;
After verifying the firmware, copy it to the flash application run area, indicating a successful upgrade, the LED will blink 3 times before jumping to the application program;
Flowchart

Bootloader Design SchemeTaking the STM32F767ZIT6 chip as an example
The flash size of STM32F767ZIT6 is 2M with an addressing range of 0x0800 0000 – 0x081F FFFF
Flash Partition
First part: Bootloader program area Sector 0 Sector 1 (0x0800 0000 – 0x0800 FFFF) size is 64K Bytes
Second part: Product parameter configuration area Sector 2 (0x0801 0000 – 0x0801 07FFF) size is 1K Bytes
Third part: Application parameter configuration area Sector 3 (0x0801 8000 – 0x0801 FFFF) size is 4K Bytes
Fourth part: Firmware upgrade flag Sector 4 (0x0802 0000 – 0x0803 FFFF) size is 4 Bytes
Fifth part: System run area Sector 5 Sector 6 Sector 7 (0x0804 0000 – 0x080F FFFF) size is 768K Bytes
Sixth part: Firmware update area Sector 8 Sector 9 Sector 10 (0x0810 0000- 0x081B FFFF) size is 768K Bytes
Seventh part: Backup area Sector 11 (0x081C 0000 – 0x081F FFFF) size is 256K Bytes
Bootloader Function Analysis
In the application scenario of this document, the main functions of the Bootloader can be divided into three:
1. Erasing and writing operations for flash
2. For USB drive upgrades, obtaining the upgrade firmware
3. After the upgrade is complete (including platform upgrades), program jump
Creating a Project with CubeMX
1. Core configuration item host USB function, configure low-speed USB option USB_OTG_FS

2. FATFS configuration

3. Serial debugging print, using UART4
USB Drive Upgrade Instructions
1. Pre-agree on the file name and path of the firmware on the USB drive
2. After the firmware download is complete, update the upgrade flag and modify system_stm32f1xx.c, uncomment the USER_VECT_TAB_ADDRESS definition, and modify the offset VECT_TAB_OFFSET
3. After the system restarts, check for the upgrade flag (0x12345678). If the upgrade flag is present, copy the update package storage area to the program run area, reset the upgrade flag to 0xFFFFFFFF, and restart. If there is no upgrade flag, directly jump to the program run area.
4. Consider MD5 encryption for the firmware on the USB drive to prevent file tampering or damage.