From 35e52f49687474bf44c93937111e0c1704776ba7 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 22 Aug 2019 12:52:50 +0200 Subject: [PATCH] Initial modification to move bootloader data after the application --- targets/stm32l432/bootloader/bootloader.c | 5 ---- .../stm32l432/linker/bootloader_stm32l4xx.ld | 6 ++--- targets/stm32l432/linker/stm32l4xx.ld | 12 ++++++--- targets/stm32l432/src/memory_layout.h | 25 +++++++++++++++++-- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/targets/stm32l432/bootloader/bootloader.c b/targets/stm32l432/bootloader/bootloader.c index dc9a407..c5b29ef 100644 --- a/targets/stm32l432/bootloader/bootloader.c +++ b/targets/stm32l432/bootloader/bootloader.c @@ -62,11 +62,6 @@ static void erase_application() } } -#define LAST_ADDR (APPLICATION_END_ADDR-2048 + 8) -#define VERSION_ADDR (AUTH_WORD_ADDR-8) -#define BOOT_VERSION_PAGE (APPLICATION_START_PAGE-1) -#define BOOT_VERSION_ADDR (0x08000000 + BOOT_VERSION_PAGE*FLASH_PAGE_SIZE) -#define LAST_PAGE (APPLICATION_END_PAGE-1) static void disable_bootloader() { // Clear last 4 bytes of the last application page-1, which is 108th diff --git a/targets/stm32l432/linker/bootloader_stm32l4xx.ld b/targets/stm32l432/linker/bootloader_stm32l4xx.ld index 53b87cf..6b5e6d8 100644 --- a/targets/stm32l432/linker/bootloader_stm32l4xx.ld +++ b/targets/stm32l432/linker/bootloader_stm32l4xx.ld @@ -14,15 +14,15 @@ _MIN_STACK_SIZE = 0x400; /* flash2 is for storing bootloader data, like last used firmware version. -_bconfig_start should be equal to (APPLICATION_START_PAGE-1) page address, from targets/stm32l432/src/memory_layout.h:30; and equal to flash2 origin +_bconfig_start should be equal to (APPLICATION_END_PAGE) page address, from targets/stm32l432/src/memory_layout.h:30; and equal to flash2 origin */ -_bconfig_start = 0x08000000 + 10*2048; +_bconfig_start = 0x08000000 + 216*1024; MEMORY { flash (rx) : ORIGIN = 0x08000000, LENGTH = 20K - flash2 (rx) : ORIGIN = 0x08000000 + 10*2048, LENGTH = 2K + flash2 (rx) : ORIGIN = 0x08000000 + 216*1024, LENGTH = 2K ram (xrw) : ORIGIN = 0x20000000, LENGTH = 48K sram2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K } diff --git a/targets/stm32l432/linker/stm32l4xx.ld b/targets/stm32l432/linker/stm32l4xx.ld index d5c53f2..e53c246 100644 --- a/targets/stm32l432/linker/stm32l4xx.ld +++ b/targets/stm32l432/linker/stm32l4xx.ld @@ -16,21 +16,27 @@ _MIN_STACK_SIZE = 0x400; Memory layout of device: 20+2 KB 198KB-2KB -8 38 KB | bootloader | application | secrets/data | + -----------> +len | 20 KB/10p| 196KB-8-8/98p | 8B | 2kB/1p | 38 KB/19p | +pos | 0->20 KB | 20->216KB-8-8 | 216KB-8-8->216KB-8 | 216kB -> 218 kB | 218->256 KB | +posp | 0-10 | 10-113 | 113-113 | 113-114 | 113-128 | +desc | bootloader | application | firmware version | bootloader data | secrets/data | Last 8 bytes in application space are occupied by bootloader flags - app authorization and bootloader activation flag. + Previous 8 bytes are application version. */ /* Current firmware version number Should be equal to (APPLICATION_END_ADDR-8) from targets/stm32l432/src/memory_layout.h:40 */ -_version_start = 0x08000000 + (128-19)*2048-8-8; +_version_start = 0x08000000 + 216*1024-8-8; /* flash length is (APPLICATION_END_PAGE-20*1024), where 20K is bootloader */ MEMORY { - flash (rx) : ORIGIN = 0x08005000 + 2K, LENGTH = 198K - 8 - 8 - 2K - flash_v (rx) : ORIGIN = 0x08000000 + (128-19)*2048 - 8 - 8, LENGTH = 8 + flash (rx) : ORIGIN = 0x08000000 + 20K, LENGTH = 196K - 8 - 8 + flash_v (r) : ORIGIN = 0x08000000 + 196K - 8 - 8, LENGTH = 8 ram (xrw) : ORIGIN = 0x20000000, LENGTH = 48K sram2 (rw) : ORIGIN = 0x10000000, LENGTH = 16K } diff --git a/targets/stm32l432/src/memory_layout.h b/targets/stm32l432/src/memory_layout.h index c9c832b..41ff02c 100644 --- a/targets/stm32l432/src/memory_layout.h +++ b/targets/stm32l432/src/memory_layout.h @@ -27,7 +27,7 @@ // Start of application code #ifndef APPLICATION_START_PAGE -#define APPLICATION_START_PAGE (11) +#define APPLICATION_START_PAGE (10) #endif #define APPLICATION_START_ADDR (0x08000000 + ((APPLICATION_START_PAGE)*PAGE_SIZE)) @@ -37,10 +37,31 @@ // End of application code. Leave some extra room for future data storage. // NOT included in application -#define APPLICATION_END_PAGE ((PAGES - 19)) +#define APPLICATION_END_PAGE ((PAGES - 20)) #define APPLICATION_END_ADDR ((0x08000000 + ((APPLICATION_END_PAGE)*PAGE_SIZE))-8) // Bootloader state. #define AUTH_WORD_ADDR (APPLICATION_END_ADDR) +#define LAST_ADDR (APPLICATION_END_ADDR-2048 + 8) +#define VERSION_ADDR (AUTH_WORD_ADDR-8) +#define BOOT_VERSION_PAGE (APPLICATION_END_PAGE) +#define BOOT_VERSION_ADDR (0x08000000 + BOOT_VERSION_PAGE*FLASH_PAGE_SIZE) +#define LAST_PAGE (APPLICATION_END_PAGE-1) + + +struct flash_memory_st{ + uint8_t bootloader[20*1024]; + uint8_t application[196*1024-16]; + uint8_t app_version[8]; + uint8_t auth_word[8]; + uint8_t bootloader_data[2*1024]; + uint8_t user_data[38*1024]; +} __attribute__((packed)); + +typedef struct flash_memory_st flash_memory_st; + +static_assert(sizeof(flash_memory_st) == 256*1024, "Data structure doesn't match flash size"); + + #endif