Skip to content

Commit e64c5f0

Browse files
committed
synch with upstream c625da4
Synchronized up to: mcu-tools@c625da4 - Removed the `flash_area_read_is_empty()` port implementation function - Added watchdog feed on nRF dvices. See CONFIG BOOT_WATCHDOG_FEED option. Signed-off-by: Andrzej Puzdrowski <[email protected]>
2 parents e312fa2 + c625da4 commit e64c5f0

File tree

13 files changed

+101
-82
lines changed

13 files changed

+101
-82
lines changed

boot/bootutil/src/bootutil_misc.c

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,27 @@ boot_enc_key_off(const struct flash_area *fap, uint8_t slot)
291291
}
292292
#endif
293293

294+
bool bootutil_buffer_is_erased(const struct flash_area *area,
295+
const void *buffer, size_t len)
296+
{
297+
size_t i;
298+
uint8_t *u8b;
299+
uint8_t erased_val;
300+
301+
if (buffer == NULL || len == 0) {
302+
return false;
303+
}
304+
305+
erased_val = flash_area_erased_val(area);
306+
for (i = 0, u8b = (uint8_t *)buffer; i < len; i++) {
307+
if (u8b[i] != erased_val) {
308+
return false;
309+
}
310+
}
311+
312+
return true;
313+
}
314+
294315
int
295316
boot_read_swap_state(const struct flash_area *fap,
296317
struct boot_swap_state *state)
@@ -301,18 +322,18 @@ boot_read_swap_state(const struct flash_area *fap,
301322
int rc;
302323

303324
off = boot_magic_off(fap);
304-
rc = flash_area_read_is_empty(fap, off, magic, BOOT_MAGIC_SZ);
325+
rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ);
305326
if (rc < 0) {
306327
return BOOT_EFLASH;
307328
}
308-
if (rc == 1) {
329+
if (bootutil_buffer_is_erased(fap, magic, BOOT_MAGIC_SZ)) {
309330
state->magic = BOOT_MAGIC_UNSET;
310331
} else {
311332
state->magic = boot_magic_decode(magic);
312333
}
313334

314335
off = boot_swap_info_off(fap);
315-
rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
336+
rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
316337
if (rc < 0) {
317338
return BOOT_EFLASH;
318339
}
@@ -321,30 +342,31 @@ boot_read_swap_state(const struct flash_area *fap,
321342
state->swap_type = BOOT_GET_SWAP_TYPE(swap_info);
322343
state->image_num = BOOT_GET_IMAGE_NUM(swap_info);
323344

324-
if (rc == 1 || state->swap_type > BOOT_SWAP_TYPE_REVERT) {
345+
if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info) ||
346+
state->swap_type > BOOT_SWAP_TYPE_REVERT) {
325347
state->swap_type = BOOT_SWAP_TYPE_NONE;
326348
state->image_num = 0;
327349
}
328350

329351
off = boot_copy_done_off(fap);
330-
rc = flash_area_read_is_empty(fap, off, &state->copy_done,
331-
sizeof state->copy_done);
352+
rc = flash_area_read(fap, off, &state->copy_done, sizeof state->copy_done);
332353
if (rc < 0) {
333354
return BOOT_EFLASH;
334355
}
335-
if (rc == 1) {
356+
if (bootutil_buffer_is_erased(fap, &state->copy_done,
357+
sizeof state->copy_done)) {
336358
state->copy_done = BOOT_FLAG_UNSET;
337359
} else {
338360
state->copy_done = boot_flag_decode(state->copy_done);
339361
}
340362

341363
off = boot_image_ok_off(fap);
342-
rc = flash_area_read_is_empty(fap, off, &state->image_ok,
343-
sizeof state->image_ok);
364+
rc = flash_area_read(fap, off, &state->image_ok, sizeof state->image_ok);
344365
if (rc < 0) {
345366
return BOOT_EFLASH;
346367
}
347-
if (rc == 1) {
368+
if (bootutil_buffer_is_erased(fap, &state->image_ok,
369+
sizeof state->image_ok)) {
348370
state->image_ok = BOOT_FLAG_UNSET;
349371
} else {
350372
state->image_ok = boot_flag_decode(state->image_ok);

boot/bootutil/src/bootutil_priv.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,16 @@ int boot_write_enc_key(const struct flash_area *fap, uint8_t slot,
325325
int boot_read_enc_key(int image_index, uint8_t slot, struct boot_status *bs);
326326
#endif
327327

328+
/**
329+
* Checks that a buffer is erased according to what the erase value for the
330+
* flash device provided in `flash_area` is.
331+
*
332+
* @returns true if the buffer is erased; false if any of the bytes is not
333+
* erased, or when buffer is NULL, or when len == 0.
334+
*/
335+
bool bootutil_buffer_is_erased(const struct flash_area *area,
336+
const void *buffer, size_t len);
337+
328338
/**
329339
* Safe (non-overflowing) uint32_t addition. Returns true, and stores
330340
* the result in *dest if it can be done without overflow. Otherwise,

boot/bootutil/src/swap_misc.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ swap_read_status(struct boot_loader_state *state, struct boot_status *bs)
164164
rc = swap_read_status_bytes(fap, state, bs);
165165
if (rc == 0) {
166166
off = boot_swap_info_off(fap);
167-
rc = flash_area_read_is_empty(fap, off, &swap_info, sizeof swap_info);
168-
if (rc == 1) {
167+
rc = flash_area_read(fap, off, &swap_info, sizeof swap_info);
168+
if (rc != 0) {
169+
return BOOT_EFLASH;
170+
}
171+
172+
if (bootutil_buffer_is_erased(fap, &swap_info, sizeof swap_info)) {
169173
BOOT_SET_SWAP_INFO(swap_info, 0, BOOT_SWAP_TYPE_NONE);
170174
rc = 0;
171175
}

boot/bootutil/src/swap_move.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,12 @@ swap_read_status_bytes(const struct flash_area *fap,
142142
write_sz = BOOT_WRITE_SZ(state);
143143
off = boot_status_off(fap);
144144
for (i = max_entries; i > 0; i--) {
145-
rc = flash_area_read_is_empty(fap, off + (i - 1) * write_sz, &status, 1);
145+
rc = flash_area_read(fap, off + (i - 1) * write_sz, &status, 1);
146146
if (rc < 0) {
147147
return BOOT_EFLASH;
148148
}
149149

150-
if (rc == 1) {
150+
if (bootutil_buffer_is_erased(fap, &status, 1)) {
151151
if (rc != last_rc) {
152152
erased_sections++;
153153
}

boot/bootutil/src/swap_scratch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ swap_read_status_bytes(const struct flash_area *fap,
110110
found_idx = 0;
111111
invalid = 0;
112112
for (i = 0; i < max_entries; i++) {
113-
rc = flash_area_read_is_empty(fap, off + i * BOOT_WRITE_SZ(state),
113+
rc = flash_area_read(fap, off + i * BOOT_WRITE_SZ(state),
114114
&status, 1);
115115
if (rc < 0) {
116116
return BOOT_EFLASH;
117117
}
118118

119-
if (rc == 1) {
119+
if (bootutil_buffer_is_erased(fap, &status, 1)) {
120120
if (found && !found_idx) {
121121
found_idx = i;
122122
}

boot/zephyr/Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,16 @@ config MCUBOOT_HW_DOWNGRADE_PREVENTION
495495

496496
endchoice
497497

498+
config BOOT_WATCHDOG_FEED
499+
bool "Feed the watchdog while doing swap"
500+
default y if SOC_FAMILY_NRF
501+
imply NRFX_WDT
502+
imply NRFX_WDT0
503+
imply NRFX_WDT1
504+
help
505+
Enables implementation of MCUBOOT_WATCHDOG_FEED() macro which is
506+
used to feed watchdog while doing time consuming operations.
507+
498508
endmenu
499509

500510
config MCUBOOT_DEVICE_SETTINGS

boot/zephyr/flash_map_extended.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -115,24 +115,3 @@ uint8_t flash_area_erased_val(const struct flash_area *fap)
115115
(void)fap;
116116
return ERASED_VAL;
117117
}
118-
119-
int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
120-
void *dst, uint32_t len)
121-
{
122-
uint8_t i;
123-
uint8_t *u8dst;
124-
int rc;
125-
126-
rc = flash_area_read(fa, off, dst, len);
127-
if (rc) {
128-
return -1;
129-
}
130-
131-
for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
132-
if (u8dst[i] != ERASED_VAL) {
133-
return 0;
134-
}
135-
}
136-
137-
return 1;
138-
}

boot/zephyr/include/flash_map_backend/flash_map_backend.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ int flash_area_sector_from_off(off_t off, struct flash_sector *sector);
8282
*/
8383
uint8_t flash_area_erased_val(const struct flash_area *fap);
8484

85-
/*
86-
* Reads len bytes from off, and checks if the read data is erased.
87-
*
88-
* Returns 1 if erased, 0 if non-erased, and -1 on failure.
89-
*/
90-
int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
91-
void *dst, uint32_t len);
92-
9385
#ifdef __cplusplus
9486
}
9587
#endif

boot/zephyr/include/mcuboot_config/mcuboot_config.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,45 @@
154154

155155
#endif /* !__BOOTSIM__ */
156156

157+
#if CONFIG_BOOT_WATCHDOG_FEED
158+
#if CONFIG_NRFX_WDT
159+
#include <nrfx_wdt.h>
160+
161+
#define FEED_WDT_INST(id) \
162+
do { \
163+
nrfx_wdt_t wdt_inst_##id = NRFX_WDT_INSTANCE(id); \
164+
for (uint8_t i = 0; i < NRF_WDT_CHANNEL_NUMBER; i++) \
165+
{ \
166+
nrf_wdt_reload_request_set(wdt_inst_##id.p_reg, \
167+
(nrf_wdt_rr_register_t)(NRF_WDT_RR0 + i)); \
168+
} \
169+
} while (0)
170+
#if defined(CONFIG_NRFX_WDT0) && defined(CONFIG_NRFX_WDT1)
171+
#define MCUBOOT_WATCHDOG_FEED() \
172+
do { \
173+
FEED_WDT_INST(0); \
174+
FEED_WDT_INST(1); \
175+
} while (0)
176+
#elif defined(CONFIG_NRFX_WDT0)
177+
#define MCUBOOT_WATCHDOG_FEED() \
178+
FEED_WDT_INST(0);
179+
#else /* defined(CONFIG_NRFX_WDT0) && defined(CONFIG_NRFX_WDT1) */
180+
#error "No NRFX WDT instances enabled"
181+
#endif /* defined(CONFIG_NRFX_WDT0) && defined(CONFIG_NRFX_WDT1) */
182+
183+
#else /* CONFIG_NRFX_WDT */
184+
#warning "MCUBOOT_WATCHDOG_FEED() is no-op"
185+
/* No vendor implementation, no-op for historical reasons */
186+
#define MCUBOOT_WATCHDOG_FEED() \
187+
do { \
188+
} while (0)
189+
#endif /* CONFIG_NRFX_WDT */
190+
#else /* CONFIG_BOOT_WATCHDOG_FEED */
191+
/* Not enabled, no feed activity */
157192
#define MCUBOOT_WATCHDOG_FEED() \
158193
do { \
159-
/* TODO: to be implemented */ \
160194
} while (0)
161195

196+
#endif /* CONFIG_BOOT_WATCHDOG_FEED */
197+
162198
#endif /* __MCUBOOT_CONFIG_H__ */

boot/zephyr/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ void main(void)
313313
int rc;
314314
fih_int fih_rc = FIH_FAILURE;
315315

316+
MCUBOOT_WATCHDOG_FEED();
317+
316318
BOOT_LOG_INF("Starting bootloader");
317319

318320
os_heap_init();

0 commit comments

Comments
 (0)