Skip to content

Commit

Permalink
fixup! bootutil: Add support for devices without erase and reduced er…
Browse files Browse the repository at this point in the history
…ases
  • Loading branch information
de-nordic committed Dec 18, 2024
1 parent 9afbf09 commit 174ad52
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion boot/bootutil/src/loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,8 +1259,54 @@ int boot_scramble_region_backwards(const struct flash_area *fa, uint32_t off, ui
return 0;
}

if (flash_area_get_size(fa) < size || (flash_area_get_size(fa) - size) < off) {
return -EINVAL;
}

if (device_requires_erase(fa)) {
return flash_area_erase(fa, off, size);
struct flash_sector sector;
uint32_t first_offset = 0;

/* Set boundary condition, the highest probable offset to erase, within
* last sector to erase
*/
off += size - 1;

/* Get the lowest erased page offset first */
ret = flash_area_get_sector(fa, off, &sector);
if (ret < 0) {
return ret;
}
first_offset = flash_sector_get_off(&sector);

while (true) {
size_t cs;

/* Get current sector and, also, correct offset */
ret = flash_area_get_sector(fa, off, &sector);
if (ret < 0) {
return ret;
}

/* Corrected offset and size of current sector to erase */
off = flash_sector_get_off(&sector);
cs = flash_sector_get_size(&sector);

ret = flash_area_erase(fa, off, cs);
if (ret < 0) {
return ret;
}

if (first_offset == off) {
/* Reached the first offsset in range and already erased it */
break;
}

/* Move down to previous sector, the flash_area_get_sector will
* correct the value to real page offset
*/
off -= 1;
}
} else {
uint8_t buf[BOOT_MAX_ALIGN];
const size_t write_block = flash_area_align(fa);
Expand Down

0 comments on commit 174ad52

Please sign in to comment.