Skip to content

Commit 4f50f42

Browse files
danielinuxdgarske
authored andcommitted
Take into account bank swapping on erase.
Fixes update demo.
1 parent 107247f commit 4f50f42

4 files changed

Lines changed: 48 additions & 12 deletions

File tree

hal/stm32_tz.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,23 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
105105
while (address < end) {
106106
if (address < FLASH_BANK2_BASE) {
107107
page_n = (address - ARCH_FLASH_OFFSET) / FLASH_PAGE_SIZE;
108-
bank = 1;
108+
bank = 0;
109109
} else {
110110
page_n = (address - FLASH_BANK2_BASE) / FLASH_PAGE_SIZE;
111-
bank = 2;
111+
bank = 1;
112112
}
113+
114+
#ifdef PLATFORM_stm32h5
115+
/* Take into account current swap configuration */
116+
if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31)
117+
bank = !bank;
118+
#endif
113119
reg_idx = page_n / 32;
114120
pos = page_n % 32;
115121
hal_flash_wait_complete(bank);
116122
hal_flash_clear_errors(bank);
117123
hal_flash_nonsecure_unlock();
118-
if (bank == 1)
124+
if (bank == 0)
119125
FLASH_SECBB1[reg_idx] |= ( 1 << pos);
120126
else
121127
FLASH_SECBB2[reg_idx] |= ( 1 << pos);
@@ -128,7 +134,7 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
128134
FLASH_CR = reg | ((page_n << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER);
129135
#else
130136
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | FLASH_CR_BER | FLASH_CR_PG | FLASH_CR_MER));
131-
FLASH_CR = reg | ((page_n << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER);
137+
FLASH_CR = reg | ((page_n << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | (bank << 31));
132138
#endif
133139

134140
DMB();

hal/stm32h5.c

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,25 @@
2828

2929
#define PLL_SRC_HSE 1
3030

31+
#if TZ_SECURE()
32+
static int is_flash_nonsecure(uint32_t address)
33+
{
34+
uint32_t in_bank_offset = address & 0x000FFFFF;
35+
#ifdef DUALBANK_SWAP
36+
if (in_bank_offset >= (WOLFBOOT_PARTITION_BOOT_ADDRESS - FLASHMEM_ADDRESS_SPACE))
37+
return 1;
38+
else
39+
return 0;
40+
#else
41+
if (address >= WOLFBOOT_PARTITION_BOOT_ADDRESS)
42+
return 1;
43+
else
44+
return 0;
45+
#endif
46+
}
47+
#endif
48+
49+
3150
static void RAMFUNCTION flash_set_waitstates(unsigned int waitstates)
3251
{
3352
uint32_t reg = FLASH_ACR;
@@ -84,11 +103,13 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
84103
dst = (uint32_t *)address;
85104

86105
#if (TZ_SECURE())
87-
if ( ((address < FLASH_BANK2_BASE) && (address >= WOLFBOOT_PARTITION_BOOT_ADDRESS)) ||
88-
(address >= WOLFBOOT_PARTITION_UPDATE_ADDRESS))
106+
if (is_flash_nonsecure(address)) {
89107
hal_tz_claim_nonsecure_area(address, len);
108+
}
90109
/* Convert into secure address space */
91-
dst = (uint32_t *)((address & (~FLASHMEM_ADDRESS_SPACE)) | FLASH_SECURE_MMAP_BASE);
110+
if (((uint32_t)dst & 0x0F000000) == 0x08000000) {
111+
dst = (uint32_t *)((address & (~FLASHMEM_ADDRESS_SPACE)) | FLASH_SECURE_MMAP_BASE);
112+
}
92113
#endif
93114

94115
while (i < len) {
@@ -153,6 +174,7 @@ void RAMFUNCTION hal_flash_opt_lock(void)
153174
}
154175

155176

177+
156178
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
157179
{
158180
uint32_t end_address;
@@ -173,18 +195,25 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
173195
base = FLASHMEM_ADDRESS_SPACE;
174196
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_BER));
175197

176-
if(p >= (FLASH_BANK2_BASE) && (p <= (FLASH_TOP) ))
177-
{
178198
#if TZ_SECURE()
179-
/* When in secure mode, skip erasing non-secure pages: will be erased upon claim */
199+
/* When in secure mode, skip erasing non-secure pages: will be erased upon claim */
200+
if (is_flash_nonsecure(address)) {
180201
return 0;
202+
}
181203
#endif
204+
if(p >= (FLASH_BANK2_BASE) && (p <= (FLASH_TOP) ))
205+
{
182206
base = FLASH_BANK2_BASE;
183207
bnksel = 1;
184208
} else {
185209
FLASH_CR &= ~FLASH_CR_SER ;
186210
return 0; /* Address out of range */
187211
}
212+
213+
/* Check for swapped banks to invert bnksel */
214+
if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31)
215+
bnksel = !bnksel;
216+
188217
reg |= ((((p - base) >> 13) << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | (bnksel << 31));
189218
FLASH_CR = reg;
190219
DMB();

hal/stm32h5.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258

259259

260260

261-
#if defined(DUALBANK_SWAP) && defined (__WOLFBOOT)
261+
#if defined(DUALBANK_SWAP)
262262
#define FLASH_OPTSR_CUR (*(volatile uint32_t *)(FLASH_BASE + 0x50))
263263
#define FLASH_OPTSR_PRG (*(volatile uint32_t *)(FLASH_BASE + 0x54))
264264
#define FLASH_OPTSR_SWAP_BANK (1 << 31)

test-app/app_stm32h5.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ static int cmd_update_xmodem(const char *args)
279279
printf("Erasing update partition...");
280280
fflush(stdout);
281281
hal_flash_unlock();
282-
//hal_flash_erase(dst_flash, WOLFBOOT_PARTITION_SIZE);
282+
hal_flash_erase(dst_flash, WOLFBOOT_PARTITION_SIZE);
283283
printf("Done.\r\n");
284284

285285
printf("Waiting for XMODEM transfer...\r\n");
@@ -400,6 +400,7 @@ static int cmd_info(const char *args)
400400
printf("\r\n");
401401
printf("System information\r\n");
402402
printf("====================================\r\n");
403+
printf("Flash banks are %sswapped.\r\n", ((FLASH_OPTSR_CUR & (FLASH_OPTSR_SWAP_BANK)) == 0)?"not ":"");
403404
printf("Firmware version : 0x%lx\r\n", wolfBoot_current_firmware_version());
404405
if (update_fw_version != 0) {
405406
printf("Candidate firmware version : 0x%lx\r\n", update_fw_version);

0 commit comments

Comments
 (0)