diff --git a/cpu/stm32/include/cpu_conf.h b/cpu/stm32/include/cpu_conf.h index fdc37fbb7f..35e10aaab7 100644 --- a/cpu/stm32/include/cpu_conf.h +++ b/cpu/stm32/include/cpu_conf.h @@ -116,6 +116,11 @@ extern "C" { #define FLASHPAGE_SIZE (128U) #endif +#if defined(CPU_FAM_STM32L0) || defined(CPU_FAM_STM32L1) || \ + defined(CPU_FAM_STM32L4) +#define FLASHPAGE_ERASE_STATE (0x00U) +#endif + #ifdef FLASHPAGE_SIZE #define FLASHPAGE_NUMOF (STM32_FLASHSIZE / FLASHPAGE_SIZE) #endif diff --git a/drivers/include/periph/flashpage.h b/drivers/include/periph/flashpage.h index 2153b84f95..9080e3336c 100644 --- a/drivers/include/periph/flashpage.h +++ b/drivers/include/periph/flashpage.h @@ -85,6 +85,15 @@ extern "C" { #define FLASHPAGE_WRITE_BLOCK_ALIGNMENT #endif +/** + * @def FLASHPAGE_ERASE_STATE + * + * @brief State of an erased byte in memory + */ +#if defined(DOXYGEN) || !defined(FLASHPAGE_ERASE_STATE) +#define FLASHPAGE_ERASE_STATE (0xFFU) +#endif + /** * @def PERIPH_FLASHPAGE_CUSTOM_PAGESIZES * diff --git a/sys/riotboot/flashwrite.c b/sys/riotboot/flashwrite.c index e5ef3134ae..2088f67953 100644 --- a/sys/riotboot/flashwrite.c +++ b/sys/riotboot/flashwrite.c @@ -30,11 +30,6 @@ #define LOG_PREFIX "riotboot_flashwrite: " #include "log.h" -/** - * @brief Magic number used to invalidate a slot - */ -#define INVALIDATE_HDR 0xAA - static inline size_t min(size_t a, size_t b) { return a <= b ? a : b; @@ -178,10 +173,14 @@ int riotboot_flashwrite_invalidate(int slot) return -2; } - uint8_t data_flash[4]; - memset(data_flash, INVALIDATE_HDR, sizeof(data_flash)); + /* invalidate header (checksum and magic number must be invalidated), + write the whole header to avoid running in memory alignment issues + with FLASHPAGE_WRITE_BLOCK_SIZE */ + riotboot_hdr_t tmp_hdr; + memset(&tmp_hdr, (~FLASHPAGE_ERASE_STATE), sizeof(riotboot_hdr_t)); - flashpage_write((void *)riotboot_slot_get_hdr(slot), data_flash, sizeof(data_flash)); + flashpage_write((void *)riotboot_slot_get_hdr(slot), &tmp_hdr, + sizeof(riotboot_hdr_t)); return 0; }