From ccc85e6150cbb10cbda09ccc5ecc4ba12fe65373 Mon Sep 17 00:00:00 2001 From: Michael Thomas Date: Wed, 13 Oct 2021 16:25:44 +0000 Subject: [PATCH] add magic alignment fix --- boot/bootutil/src/bootutil_public.c | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/boot/bootutil/src/bootutil_public.c b/boot/bootutil/src/bootutil_public.c index 15cdb0ecf..eb38da1a3 100644 --- a/boot/bootutil/src/bootutil_public.c +++ b/boot/bootutil/src/bootutil_public.c @@ -316,14 +316,30 @@ int boot_write_magic(const struct flash_area *fap) { uint32_t off; + uint32_t pad_off; int rc; + uint8_t magic[BOOT_MAGIC_ALIGN_SIZE]; + uint8_t erased_val; off = boot_magic_off(fap); + /* image_trailer structure was modified with additional padding such that + * the pad+magic ends up in a flash minimum write region. The address + * returned by boot_magic_off() is the start of magic which is not the + * start of the flash write boundary and thus writes to the magic will fail. + * To account for this change, write to magic is first padded with 0xFF + * before writing to the trailer. */ + pad_off = off & ~(BOOT_MAX_ALIGN - 1); + + erased_val = flash_area_erased_val(fap); + + memset(&magic[0], erased_val, sizeof(magic)); + memcpy(&magic[BOOT_MAGIC_ALIGN_SIZE - BOOT_MAGIC_SZ], boot_img_magic, BOOT_MAGIC_SZ); BOOT_LOG_DBG("writing magic; fa_id=%d off=0x%lx (0x%lx)", - flash_area_get_id(fap), (unsigned long)off, - (unsigned long)(flash_area_get_off(fap) + off)); - rc = flash_area_write(fap, off, boot_img_magic, BOOT_MAGIC_SZ); + fap->fa_id, (unsigned long)off, + (unsigned long)(fap->fa_off + off)); + rc = flash_area_write(fap, pad_off, &magic[0], BOOT_MAGIC_ALIGN_SIZE); + if (rc != 0) { return BOOT_EFLASH; }