Skip to content

Commit 4b2e55f

Browse files
committed
boot: bootutil: drop flash_area_read_is_empty
Removes the current `flash_area_read_is_empty` which lacked a bit of clarity in its naming and error handling, as well as requiring an extra API in the flash map, and switches to using an internal function `bootutil_buffer_is_erased`. Code that was previously using `flash_area_read_is_empty` must now be updated to do a `flash_area_read` followed by a call to `bootutil_buffer_is_erased` with the read buffer. The proposal was previously discussed here: zephyrproject-rtos/zephyr#28519 Signed-off-by: Fabio Utzig <fabio.utzig@nordicsemi.no>
1 parent 296949e commit 4b2e55f

File tree

8 files changed

+52
-55
lines changed

8 files changed

+52
-55
lines changed

boot/bootutil/src/bootutil_misc.c

+32-10
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

+10
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

+6-2
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

+2-2
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

+2-2
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/include/flash_map_backend/flash_map_backend.h

-8
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

sim/mcuboot-sys/csupport/run.c

-23
Original file line numberDiff line numberDiff line change
@@ -334,29 +334,6 @@ int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
334334
return sim_flash_erase(area->fa_device_id, area->fa_off + off, len);
335335
}
336336

337-
int flash_area_read_is_empty(const struct flash_area *area, uint32_t off,
338-
void *dst, uint32_t len)
339-
{
340-
uint8_t i;
341-
uint8_t *u8dst;
342-
int rc;
343-
344-
BOOT_LOG_SIM("%s: area=%d, off=%x, len=%x", __func__, area->fa_id, off, len);
345-
346-
rc = sim_flash_read(area->fa_device_id, area->fa_off + off, dst, len);
347-
if (rc) {
348-
return -1;
349-
}
350-
351-
for (i = 0, u8dst = (uint8_t *)dst; i < len; i++) {
352-
if (u8dst[i] != sim_flash_erased_val(area->fa_device_id)) {
353-
return 0;
354-
}
355-
}
356-
357-
return 1;
358-
}
359-
360337
int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
361338
{
362339
uint32_t i;

sim/mcuboot-sys/csupport/storage/flash_map.h

-8
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,6 @@ uint16_t flash_area_align(const struct flash_area *);
130130
*/
131131
uint8_t flash_area_erased_val(const struct flash_area *);
132132

133-
/*
134-
* Reads len bytes from off, and checks if the read data is erased.
135-
*
136-
* Returns 1 if erased, 0 if non-erased, and -1 on failure.
137-
*/
138-
int flash_area_read_is_empty(const struct flash_area *fa, uint32_t off,
139-
void *dst, uint32_t len);
140-
141133
/*
142134
* Given flash area ID, return info about sectors within the area.
143135
*/

0 commit comments

Comments
 (0)