Skip to content

Commit 9b61f89

Browse files
danielinuxdgarske
authored andcommitted
Fixed update + write to the right bank
1 parent 4f50f42 commit 9b61f89

5 files changed

Lines changed: 129 additions & 39 deletions

File tree

hal/stm32_tz.c

Lines changed: 60 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ static void RAMFUNCTION hal_flash_nonsecure_unlock(void)
4848
DMB();
4949
FLASH_NS_KEYR = FLASH_KEY2;
5050
DMB();
51-
while ((FLASH_NS_CR & FLASH_CR_LOCK) != 0)
52-
;
5351
}
5452
}
5553

@@ -97,37 +95,42 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
9795
int page_n, reg_idx;
9896
uint32_t reg;
9997
uint32_t end = address + len;
98+
uint32_t start_address = address;
10099
uint32_t bank = 0;
101100
int pos;
102101

103102
if (!is_range_nonsecure(address, len))
104103
return;
105-
while (address < end) {
106-
if (address < FLASH_BANK2_BASE) {
107-
page_n = (address - ARCH_FLASH_OFFSET) / FLASH_PAGE_SIZE;
108-
bank = 0;
109-
} else {
110-
page_n = (address - FLASH_BANK2_BASE) / FLASH_PAGE_SIZE;
111-
bank = 1;
112-
}
113104

105+
if (address < FLASH_BANK2_BASE) {
106+
page_n = (address - ARCH_FLASH_OFFSET) / FLASH_PAGE_SIZE;
107+
bank = 0;
108+
} else {
109+
page_n = (address - FLASH_BANK2_BASE) / FLASH_PAGE_SIZE;
110+
bank = 1;
111+
}
114112
#ifdef PLATFORM_stm32h5
115-
/* Take into account current swap configuration */
116-
if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31)
117-
bank = !bank;
113+
/* Take into account current swap configuration */
114+
if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31)
115+
bank = !bank;
118116
#endif
117+
while (address < end) {
119118
reg_idx = page_n / 32;
120119
pos = page_n % 32;
121120
hal_flash_wait_complete(bank);
122121
hal_flash_clear_errors(bank);
123-
hal_flash_nonsecure_unlock();
124122
if (bank == 0)
125123
FLASH_SECBB1[reg_idx] |= ( 1 << pos);
126124
else
127125
FLASH_SECBB2[reg_idx] |= ( 1 << pos);
128126
ISB();
129127
hal_flash_wait_complete(bank);
130-
hal_flash_nonsecure_lock();
128+
address += FLASH_PAGE_SIZE;
129+
page_n++;
130+
}
131+
132+
address = start_address;
133+
while (address < end) {
131134
/* Erase claimed non-secure page, in secure mode */
132135
#ifndef PLATFORM_stm32h5
133136
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_PER | FLASH_CR_BKER | FLASH_CR_PG | FLASH_CR_MER1 | FLASH_CR_MER2));
@@ -156,9 +159,48 @@ void hal_tz_claim_nonsecure_area(uint32_t address, int len)
156159
#if defined (__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
157160
void hal_tz_release_nonsecure_area(void)
158161
{
162+
#ifndef DUALBANK_SWAP
159163
int i;
160164
for (i = 0; i < FLASH_SECBB_NREGS; i++)
161165
FLASH_SECBB2[i] = 0;
166+
#else
167+
uint32_t addr;
168+
int bank_swp = 0;
169+
/* Take into account current swap configuration */
170+
if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31)
171+
bank_swp = 1;
172+
173+
/* Bank 1 */
174+
for(addr = WOLFBOOT_PARTITION_BOOT_ADDRESS;
175+
addr < FLASH_BANK2_BASE; addr += FLASH_PAGE_SIZE) {
176+
uint32_t page_n = (addr - FLASHMEM_ADDRESS_SPACE) / FLASH_PAGE_SIZE;
177+
uint32_t reg_idx = page_n / 32;
178+
uint32_t pos = page_n % 32;
179+
hal_flash_wait_complete(0);
180+
hal_flash_clear_errors(0);
181+
if (!bank_swp)
182+
FLASH_SECBB1[reg_idx] &= ~( 1 << pos);
183+
else
184+
FLASH_SECBB2[reg_idx] &= ~( 1 << pos);
185+
ISB();
186+
hal_flash_wait_complete(0);
187+
}
188+
/* Bank 2 */
189+
for(addr = WOLFBOOT_PARTITION_UPDATE_ADDRESS;
190+
addr < FLASH_TOP; addr += FLASH_PAGE_SIZE) {
191+
uint32_t page_n = (addr - FLASH_BANK2_BASE) / FLASH_PAGE_SIZE;
192+
uint32_t reg_idx = page_n / 32;
193+
uint32_t pos = page_n % 32;
194+
hal_flash_wait_complete(1);
195+
hal_flash_clear_errors(1);
196+
if (!bank_swp)
197+
FLASH_SECBB2[reg_idx] &= ~( 1 << pos);
198+
else
199+
FLASH_SECBB1[reg_idx] &= ~( 1 << pos);
200+
ISB();
201+
hal_flash_wait_complete(1);
202+
}
203+
#endif
162204
}
163205
#else
164206
#define release_nonsecure_area(...) do{}while(0)
@@ -243,13 +285,14 @@ void hal_tz_sau_init(void)
243285
{
244286
uint32_t page_n = 0;
245287
/* SAU is set up before staging. Set up all areas as secure. */
288+
246289
/* Non-secure callable: NSC functions area */
247290
sau_init_region(0, 0x0C038000, 0x0C040000, 1);
248291

249-
/* Non-Secure: application flash area (first bank) */
292+
/* Secure: application flash area (first bank) */
250293
sau_init_region(1, WOLFBOOT_PARTITION_BOOT_ADDRESS, FLASH_BANK2_BASE - 1, 0);
251294

252-
/* Non-Secure: application flash area (second bank) */
295+
/* Secure: application flash area (second bank) */
253296
sau_init_region(2, WOLFBOOT_PARTITION_UPDATE_ADDRESS, FLASH_TOP -1, 0);
254297

255298
/* Secure RAM regions in SRAM1/SRAM2 */

hal/stm32h5.c

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
9494
uint32_t *src, *dst;
9595
uint32_t dword[2];
9696
volatile uint32_t *sr, *cr;
97+
uint32_t off = 0;
98+
uint32_t una_len = 0;
9799

98100
cr = &FLASH_CR;
99101
sr = &FLASH_SR;
@@ -105,28 +107,32 @@ int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
105107
#if (TZ_SECURE())
106108
if (is_flash_nonsecure(address)) {
107109
hal_tz_claim_nonsecure_area(address, len);
108-
}
109-
/* Convert into secure address space */
110-
if (((uint32_t)dst & 0x0F000000) == 0x08000000) {
110+
} else if (((uint32_t)dst & 0x0F000000) == 0x08000000) {
111+
/* Convert into secure address space */
111112
dst = (uint32_t *)((address & (~FLASHMEM_ADDRESS_SPACE)) | FLASH_SECURE_MMAP_BASE);
112113
}
113114
#endif
114-
115115
while (i < len) {
116116
dword[0] = src[i >> 2];
117-
dword[1] = src[(i >> 2) + 1];
117+
if (len > i + 1)
118+
dword[1] = src[(i >> 2) + 1];
119+
else
120+
dword[1] = 0xFFFFFFFF;
118121
*cr |= FLASH_CR_PG;
119122
dst[i >> 2] = dword[0];
120123
ISB();
121124
dst[(i >> 2) + 1] = dword[1];
125+
ISB();
122126
hal_flash_wait_complete(0);
123127
if ((*sr & FLASH_SR_EOP) != 0)
124128
*sr |= FLASH_SR_EOP;
125129
*cr &= ~FLASH_CR_PG;
126130
i+=8;
127131
}
128132
#if (TZ_SECURE())
129-
hal_tz_release_nonsecure_area();
133+
if (is_flash_nonsecure(address)) {
134+
hal_tz_release_nonsecure_area();
135+
}
130136
#endif
131137
return 0;
132138
}
@@ -194,29 +200,29 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
194200
uint32_t bnksel = 0;
195201
base = FLASHMEM_ADDRESS_SPACE;
196202
reg = FLASH_CR & (~((FLASH_CR_PNB_MASK << FLASH_CR_PNB_SHIFT) | FLASH_CR_BER));
203+
if(p >= (FLASH_BANK2_BASE) && (p <= (FLASH_TOP) ))
204+
{
205+
base = FLASH_BANK2_BASE;
206+
bnksel = 1;
207+
}
197208

198209
#if TZ_SECURE()
199210
/* When in secure mode, skip erasing non-secure pages: will be erased upon claim */
200211
if (is_flash_nonsecure(address)) {
201212
return 0;
202213
}
203214
#endif
204-
if(p >= (FLASH_BANK2_BASE) && (p <= (FLASH_TOP) ))
205-
{
206-
base = FLASH_BANK2_BASE;
207-
bnksel = 1;
208-
} else {
209-
FLASH_CR &= ~FLASH_CR_SER ;
210-
return 0; /* Address out of range */
211-
}
212-
213215
/* Check for swapped banks to invert bnksel */
214216
if ((FLASH_OPTSR_CUR & FLASH_OPTSR_SWAP_BANK) >> 31)
215217
bnksel = !bnksel;
216218

219+
#if !TZ_SECURE() && !defined(__FLASH_OTP_PRIMER)
220+
printf("Erasing bank %d, page %d\r\n", bnksel, (p - base) >> 13);
221+
#endif
222+
217223
reg |= ((((p - base) >> 13) << FLASH_CR_PNB_SHIFT) | FLASH_CR_SER | (bnksel << 31));
218224
FLASH_CR = reg;
219-
DMB();
225+
ISB();
220226
FLASH_CR |= FLASH_CR_STRT;
221227
hal_flash_wait_complete(0);
222228
}
@@ -547,21 +553,21 @@ static void fork_bootloader(void)
547553
#include "uart_drv.h"
548554
void hal_init(void)
549555
{
556+
clock_pll_on();
550557
#if TZ_SECURE()
551-
hal_tz_sau_init();
552558
hal_gtzc_init();
559+
hal_tz_sau_init();
553560
#endif
554-
clock_pll_on();
555561

556562
#if defined(DUALBANK_SWAP) && defined(__WOLFBOOT)
557-
if ((FLASH_OPTSR_CUR & (FLASH_OPTSR_SWAP_BANK)) == 0)
558-
fork_bootloader();
563+
fork_bootloader();
559564
#endif
560565

561566

562567
}
563568

564569

570+
565571
void hal_prepare_boot(void)
566572
{
567573

include/wolfboot/wolfboot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ int wolfBoot_fallback_is_possible(void);
301301
int wolfBoot_dualboot_candidate(void);
302302

303303
int wolfBoot_dualboot_candidate_addr(void**);
304+
int wolfBoot_get_partition_state(uint8_t part, uint8_t *st);
305+
304306

305307
#ifdef EXT_ENCRYPTED
306308
/* Encryption support */

test-app/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ ifeq ($(TARGET),stm32h5)
162162
LSCRIPT_TEMPLATE=ARM-stm32h5.ld
163163
endif
164164
CFLAGS+=-DAPP_HAS_SYSTICK
165+
CFLAGS+=-DRAMFUNCTION='__attribute__((used,section(".ramcode")))'
165166
CFLAGS+=-mcpu=cortex-m33 -ffunction-sections -fdata-sections -fno-common
166167
LDFLAGS+=-mcpu=cortex-m33
167168
LDFLAGS+=-Wl,-gc-sections -Wl,-Map=image.map

test-app/app_stm32h5.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,9 @@ static int cmd_update_xmodem(const char *args)
368368
update_ver = wolfBoot_update_firmware_version();
369369
if (update_ver != 0) {
370370
printf("New firmware version: 0x%lx\r\n", update_ver);
371+
printf("Triggering update...\r\n");
372+
wolfBoot_update_trigger();
373+
printf("Update completed successfully.\r\n");
371374
} else {
372375
printf("No valid image in update partition\r\n");
373376
}
@@ -387,25 +390,60 @@ static int cmd_help(const char *args)
387390
return 0;
388391
}
389392

393+
const char part_state_names[6][16] = {
394+
"NEW",
395+
"UPDATING",
396+
"FFLAGS",
397+
"TESTING",
398+
"CONFIRMED",
399+
"[Invalid state]"
400+
};
401+
402+
static const char *part_state_name(uint8_t state)
403+
{
404+
switch(state) {
405+
case IMG_STATE_NEW:
406+
return part_state_names[0];
407+
case IMG_STATE_UPDATING:
408+
return part_state_names[1];
409+
case IMG_STATE_FINAL_FLAGS:
410+
return part_state_names[2];
411+
case IMG_STATE_TESTING:
412+
return part_state_names[3];
413+
case IMG_STATE_SUCCESS:
414+
return part_state_names[4];
415+
default:
416+
return part_state_names[5];
417+
}
418+
}
419+
390420
static int cmd_info(const char *args)
391421
{
392422
int i, j;
393423
uint32_t cur_fw_version, update_fw_version;
394424
uint32_t n_keys;
395425
uint16_t hdrSz;
426+
uint8_t boot_part_state = IMG_STATE_NEW, update_part_state = IMG_STATE_NEW;
396427

397428
cur_fw_version = wolfBoot_current_firmware_version();
398429
update_fw_version = wolfBoot_update_firmware_version();
399430

431+
wolfBoot_get_partition_state(PART_BOOT, &boot_part_state);
432+
wolfBoot_get_partition_state(PART_UPDATE, &update_part_state);
433+
400434
printf("\r\n");
401435
printf("System information\r\n");
402436
printf("====================================\r\n");
403437
printf("Flash banks are %sswapped.\r\n", ((FLASH_OPTSR_CUR & (FLASH_OPTSR_SWAP_BANK)) == 0)?"not ":"");
404438
printf("Firmware version : 0x%lx\r\n", wolfBoot_current_firmware_version());
439+
printf("Current firmware state: %s\r\n", part_state_name(boot_part_state));
405440
if (update_fw_version != 0) {
406-
printf("Candidate firmware version : 0x%lx\r\n", update_fw_version);
441+
if (update_part_state == IMG_STATE_UPDATING)
442+
printf("Candidate firmware version : 0x%lx\r\n", update_fw_version);
443+
else
444+
printf("Backup firmware version : 0x%lx\r\n", update_fw_version);
445+
printf("Update state: %s\r\n", part_state_name(update_part_state));
407446
if (update_fw_version > cur_fw_version) {
408-
wolfBoot_update_trigger();
409447
printf("'reboot' to initiate update.\r\n");
410448
} else {
411449
printf("Update image older than current.\r\n");

0 commit comments

Comments
 (0)