From 3a777927080c3d1d06f486c9a1913ba2f523b80c Mon Sep 17 00:00:00 2001 From: Koen Zandberg Date: Tue, 10 Nov 2020 21:26:13 +0100 Subject: [PATCH] riotboot/flashwrite: support non-uniform flash page sizes --- sys/riotboot/flashwrite.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sys/riotboot/flashwrite.c b/sys/riotboot/flashwrite.c index 743676e32edd..7441d8d83eda 100644 --- a/sys/riotboot/flashwrite.c +++ b/sys/riotboot/flashwrite.c @@ -43,9 +43,7 @@ size_t riotboot_flashwrite_slotsize( int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot, size_t offset) { - assert(offset <= FLASHPAGE_SIZE); /* the flashpage size must be a multiple of the riotboot flashpage buffer */ - static_assert(!(FLASHPAGE_SIZE % RIOTBOOT_FLASHPAGE_BUFFER_SIZE)); LOG_INFO(LOG_PREFIX "initializing update to target slot %i\n", target_slot); @@ -56,6 +54,8 @@ int riotboot_flashwrite_init_raw(riotboot_flashwrite_t *state, int target_slot, state->target_slot = target_slot; state->flashpage = flashpage_page((void *)riotboot_slot_get_hdr(target_slot)); + assert(offset <= flashpage_size(state->flashpage)); + if (CONFIG_RIOTBOOT_FLASHWRITE_RAW && offset) { /* Erase the first page only if the offset (!=0) specifies that there is * a checksum or other mechanism at the start of the page. */ @@ -97,13 +97,15 @@ int riotboot_flashwrite_putbytes(riotboot_flashwrite_t *state, LOG_DEBUG(LOG_PREFIX "processing bytes %u-%u\n", state->offset, state->offset + len - 1); while (len) { - size_t flashpage_pos = state->offset % FLASHPAGE_SIZE; size_t flashwrite_buffer_pos = state->offset % RIOTBOOT_FLASHPAGE_BUFFER_SIZE; size_t flashpage_avail = RIOTBOOT_FLASHPAGE_BUFFER_SIZE - flashwrite_buffer_pos; size_t to_copy = min(flashpage_avail, len); - if (CONFIG_RIOTBOOT_FLASHWRITE_RAW && flashpage_pos == 0) { + void * target_addr = (uint8_t*)riotboot_slot_get_hdr(state->target_slot) + state->offset; + + if (CONFIG_RIOTBOOT_FLASHWRITE_RAW && + (target_addr == (flashpage_addr(state->flashpage) + flashpage_size(state->flashpage)))) { /* Erase the next page */ state->flashpage++; flashpage_erase(state->flashpage); @@ -131,9 +133,9 @@ int riotboot_flashwrite_putbytes(riotboot_flashwrite_t *state, state->flashpage_buf, RIOTBOOT_FLASHPAGE_BUFFER_SIZE); } else { - flashpage_write((uint8_t*)addr + flashpage_pos, - state->flashpage_buf, - RIOTBOOT_FLASHPAGE_BUFFER_SIZE); + flashpage_write(target_addr, + state->flashpage_buf, + RIOTBOOT_FLASHPAGE_BUFFER_SIZE); } #else int res = flashpage_write_and_verify(state->flashpage,