Skip to content

Commit

Permalink
susbys/dfu/img_util: refined ERASE PROGRESSIVELY implementation
Browse files Browse the repository at this point in the history
Moved MCUboot trailer's status erase ahead any write operation.

Signed-off-by: Andrzej Puzdrowski <[email protected]>
Signed-off-by: Dominik Ermel <[email protected]>
  • Loading branch information
nvlsianpu committed Oct 7, 2024
1 parent e0f5354 commit f21eb74
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions subsys/dfu/img_util/flash_img.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,43 +43,67 @@ BUILD_ASSERT((CONFIG_IMG_BLOCK_BUF_SIZE % FLASH_WRITE_BLOCK_SIZE == 0),
"FLASH_WRITE_BLOCK_SIZE");
#endif

static int scramble_mcuboot_trailer(struct flash_img_context *ctx)
{
int rc = 0;

#ifdef CONFIG_IMG_ERASE_PROGRESSIVELY
if (stream_flash_bytes_written(&ctx->stream) == 0) {
off_t toff = boot_get_trailer_status_offset(ctx->flash_area->fa_size);
off_t offset;
size_t size;
const struct flash_parameters *fparams =
flash_get_parameters(flash_area_get_device(ctx->flash_area));

Check notice on line 57 in subsys/dfu/img_util/flash_img.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/dfu/img_util/flash_img.c:57 - flash_get_parameters(flash_area_get_device(ctx->flash_area)); + flash_get_parameters(flash_area_get_device(ctx->flash_area));
if (flash_params_get_erase_cap(fparams) & FLASH_ERASE_C_EXPLICIT) {
/* On devices with explicit erase we are aligning to page
* layout.
*/
struct flash_pages_info info;

rc = flash_get_page_info_by_offs(flash_area_get_device(ctx->flash_area), toff,

Check warning on line 64 in subsys/dfu/img_util/flash_img.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LONG_LINE

subsys/dfu/img_util/flash_img.c:64 line length of 102 exceeds 100 columns
&info);
if (rc != 0) {

Check notice on line 66 in subsys/dfu/img_util/flash_img.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/dfu/img_util/flash_img.c:66 - rc = flash_get_page_info_by_offs(flash_area_get_device(ctx->flash_area), toff, - &info); + rc = flash_get_page_info_by_offs(flash_area_get_device(ctx->flash_area), + toff, &info);
return rc;
}
offset = info.start_offset;
size = info.size;

} else {
/* On devices with no erase, we are aligning to write block
* size.
*/
offset = (toff + fparams->write_block_size - 1) &
~(fparams->write_block_size - 1);
/* No alignment correction neede here, offset is corrected already */
size = ctx->flash_area->fa_size - offset;
}

rc = flash_area_flatten(ctx->flash_area, offset, size);
}
#endif

return rc;
}


Check notice on line 89 in subsys/dfu/img_util/flash_img.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

You may want to run clang-format on this change

subsys/dfu/img_util/flash_img.c:89 -
int flash_img_buffered_write(struct flash_img_context *ctx, const uint8_t *data,
size_t len, bool flush)
{
int rc;

rc = stream_flash_buffered_write(&ctx->stream, data, len, flush);
if (!flush) {
/* If there is need to erase trailer, that should happen before any
* write is done to partition.
*/
rc = scramble_mcuboot_trailer(ctx);
if (rc != 0) {
return rc;
}

#ifdef CONFIG_IMG_ERASE_PROGRESSIVELY
ssize_t status_offset = boot_get_trailer_status_offset(
ctx->flash_area->fa_size);

#ifdef CONFIG_STREAM_FLASH_ERASE
const struct flash_parameters *fparams = flash_get_parameters(flash_area_get_device(ctx->flash_area));

if ((flash_params_get_erase_cap(fparams) & FLASH_ERASE_C_EXPLICIT)) {
/* use pistine-page-erase procedure for a device which needs it */
rc = stream_flash_erase_page(&ctx->stream,
ctx->flash_area->fa_off +
status_offset);
} else
#endif
{
if (status_offset > stream_flash_bytes_written(&ctx->stream)) {
rc = flash_area_flatten(ctx->flash_area, status_offset,
ctx->flash_area->fa_off - status_offset);
} else {
rc = 0;
}
}

if (rc) {
rc = stream_flash_buffered_write(&ctx->stream, data, len, flush);
if (!flush) {
return rc;
}
#endif

flash_area_close(ctx->flash_area);
ctx->flash_area = NULL;
Expand Down

0 comments on commit f21eb74

Please sign in to comment.