Skip to content

Commit fcf72e0

Browse files
mattia-moffadanielinux
authored andcommitted
Remove NO_DIRECT_READ_OF_ERASED_SECTOR option; write after erase instead
1 parent 3ef8c60 commit fcf72e0

File tree

8 files changed

+91
-170
lines changed

8 files changed

+91
-170
lines changed

config/examples/lpc55s69-tz.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ RAM_CODE?=1
2828
DUALBANK_SWAP?=0
2929
PKA?=1
3030
FLASH_MULTI_SECTOR_ERASE?=1
31-
NO_DIRECT_READ_OF_ERASED_SECTOR?=1
3231
WOLFCRYPT_TZ?=1
3332
WOLFCRYPT_TZ_PKCS11?=1
3433

config/examples/lpc55s69.config

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ RAM_CODE?=1
2828
DUALBANK_SWAP?=0
2929
PKA?=1
3030
FLASH_MULTI_SECTOR_ERASE?=1
31-
NO_DIRECT_READ_OF_ERASED_SECTOR?=1
3231

3332
# 512-byte pages erasable/writeable
3433
WOLFBOOT_SECTOR_SIZE?=0x200

hal/lpc55s69.c

Lines changed: 69 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,23 @@ static void periph_unsecure(void)
7676
}
7777
#endif
7878

79+
static void hal_flash_fix_ecc(void)
80+
{
81+
uint8_t page_buf[512];
82+
uint32_t addr;
83+
uint32_t start = WOLFBOOT_PARTITION_BOOT_ADDRESS;
84+
uint32_t end = WOLFBOOT_PARTITION_SWAP_ADDRESS + WOLFBOOT_SECTOR_SIZE;
85+
86+
memset(page_buf, 0xFF, sizeof(page_buf));
87+
88+
for (addr = start; addr < end; addr += pflash_page_size) {
89+
if (FLASH_VerifyErase(&pflash, addr, pflash_page_size)
90+
== kStatus_FLASH_Success) {
91+
FLASH_Program(&pflash, addr, page_buf, pflash_page_size);
92+
}
93+
}
94+
}
95+
7996
void hal_init(void)
8097
{
8198
#ifdef __WOLFBOOT
@@ -91,6 +108,7 @@ void hal_init(void)
91108
#if defined(__WOLFBOOT) || !defined(TZEN)
92109
memset(&pflash, 0, sizeof(pflash));
93110
FLASH_Init(&pflash);
111+
hal_flash_fix_ecc();
94112
#endif
95113

96114
#if defined(TZEN) && !defined(NONSECURE_APP)
@@ -120,16 +138,41 @@ void hal_prepare_boot(void)
120138

121139
int RAMFUNCTION hal_flash_write(uint32_t address, const uint8_t *data, int len)
122140
{
123-
if (
124-
address % pflash_page_size == 0 &&
125-
len % pflash_page_size == 0 &&
126-
FLASH_Program(&pflash, address, data, len) == kStatus_FLASH_Success
127-
)
128-
{
129-
return 0;
141+
uint8_t page_buf[512];
142+
uint32_t page_addr;
143+
uint32_t offset;
144+
uint32_t chunk;
145+
146+
while (len > 0) {
147+
page_addr = address & ~(pflash_page_size - 1);
148+
offset = address - page_addr;
149+
chunk = pflash_page_size - offset;
150+
if ((uint32_t)len < chunk)
151+
chunk = (uint32_t)len;
152+
153+
if (FLASH_VerifyErase(&pflash, page_addr, pflash_page_size)
154+
== kStatus_FLASH_Success) {
155+
memset(page_buf, 0xFF, pflash_page_size);
156+
} else {
157+
memcpy(page_buf, (void *)page_addr, pflash_page_size);
158+
159+
if (FLASH_Erase(&pflash, page_addr, pflash_page_size,
160+
kFLASH_ApiEraseKey) != kStatus_FLASH_Success)
161+
return -1;
162+
}
163+
164+
memcpy(page_buf + offset, data, chunk);
165+
166+
if (FLASH_Program(&pflash, page_addr, page_buf, pflash_page_size)
167+
!= kStatus_FLASH_Success)
168+
return -1;
169+
170+
address += chunk;
171+
data += chunk;
172+
len -= (int)chunk;
130173
}
131174

132-
return -1;
175+
return 0;
133176
}
134177

135178
void RAMFUNCTION hal_flash_unlock(void)
@@ -142,26 +185,26 @@ void RAMFUNCTION hal_flash_lock(void)
142185

143186
int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
144187
{
145-
if (
146-
address % pflash_page_size == 0 &&
147-
len % pflash_page_size == 0 &&
148-
FLASH_Erase(&pflash, address, len, kFLASH_ApiEraseKey)
149-
== kStatus_FLASH_Success
150-
)
151-
{
152-
return 0;
153-
}
188+
uint8_t page_buf[512];
189+
uint32_t pos;
154190

155-
return -1;
156-
}
191+
if (address % pflash_page_size != 0 || len % pflash_page_size != 0)
192+
return -1;
157193

158-
#ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
159-
int RAMFUNCTION hal_flash_is_erased_at(uint32_t address)
160-
{
161-
address &= ~(WOLFBOOT_SECTOR_SIZE - 1);
162-
return FLASH_VerifyErase(&pflash, address, WOLFBOOT_SECTOR_SIZE) == kStatus_FLASH_Success;
194+
memset(page_buf, 0xFF, sizeof(page_buf));
195+
196+
for (pos = address; pos < address + (uint32_t)len; pos += pflash_page_size) {
197+
if (FLASH_Erase(&pflash, pos, pflash_page_size, kFLASH_ApiEraseKey)
198+
!= kStatus_FLASH_Success)
199+
return -1;
200+
201+
if (FLASH_Program(&pflash, pos, page_buf, pflash_page_size)
202+
!= kStatus_FLASH_Success)
203+
return -1;
204+
}
205+
206+
return 0;
163207
}
164-
#endif
165208

166209
#ifdef WOLFCRYPT_SECURE_MODE
167210
void hal_trng_init(void)
@@ -262,4 +305,4 @@ void uart_write(const char *buf, unsigned int sz)
262305
sz -= line_sz + 1U;
263306
}
264307
}
265-
#endif
308+
#endif

include/hal.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,6 @@ uint64_t hal_get_timer_us(void);
8888
void hal_flash_unlock(void);
8989
void hal_flash_lock(void);
9090
void hal_prepare_boot(void);
91-
#ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
92-
int hal_flash_is_erased_at(uint32_t address);
93-
#endif
9491

9592
#ifdef DUALBANK_SWAP
9693
void hal_flash_dualbank_swap(void);

options.mk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,10 +713,6 @@ ifeq ($(NVM_FLASH_WRITEONCE),1)
713713
CFLAGS+= -D"NVM_FLASH_WRITEONCE"
714714
endif
715715

716-
ifeq ($(NO_DIRECT_READ_OF_ERASED_SECTOR),1)
717-
CFLAGS+= -D"NO_DIRECT_READ_OF_ERASED_SECTOR"
718-
endif
719-
720716
ifeq ($(DISABLE_BACKUP),1)
721717
CFLAGS+= -D"DISABLE_BACKUP"
722718
endif

src/image.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,17 +1298,10 @@ uint32_t wolfBoot_image_size(uint8_t *image)
12981298
*/
12991299
int wolfBoot_open_image_address(struct wolfBoot_image *img, uint8_t *image)
13001300
{
1301-
uint32_t *pmagic = (uint32_t *)(image);
1302-
uint32_t magic = FLASH_WORD_ERASED;
1303-
if (
1304-
#ifdef NO_DIRECT_READ_OF_ERASED_SECTOR
1305-
hal_flash_is_erased_at((uintptr_t)pmagic) ||
1306-
#endif
1307-
(magic = *pmagic) != WOLFBOOT_MAGIC
1308-
)
1309-
{
1301+
uint32_t *magic = (uint32_t *)(image);
1302+
if (*magic != WOLFBOOT_MAGIC) {
13101303
wolfBoot_printf("Partition %d header magic 0x%08x invalid at %p\n",
1311-
img->part, (unsigned int)magic, img->hdr);
1304+
img->part, (unsigned int)*magic, img->hdr);
13121305
return -1;
13131306
}
13141307
img->fw_size = wolfBoot_image_size(image);

0 commit comments

Comments
 (0)