diff --git a/boot/bootutil/src/bootutil_public.c b/boot/bootutil/src/bootutil_public.c index 1021188644..dc118031d9 100644 --- a/boot/bootutil/src/bootutil_public.c +++ b/boot/bootutil/src/bootutil_public.c @@ -51,6 +51,11 @@ #include "bootutil_priv.h" #include "bootutil_misc.h" +// TEMP FOR DEBUG - REMOVE +// #include "bootloader_flash_priv.h" + +bool aligned_flash_read_(uintptr_t addr, void *dest, size_t size, bool read_encrypted); + #ifdef CONFIG_MCUBOOT BOOT_LOG_MODULE_DECLARE(mcuboot); #else @@ -223,6 +228,13 @@ boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off) if (rc < 0) { return BOOT_EFLASH; } + // TEMP FOR DEBUG - REMOVE + uint8_t read_buf; + BOOT_LOG_INF("boot_read_flag whats read rc=0x%x addr=0x%lx 0x%x", rc, fap->fa_off+off, *flag); + rc = aligned_flash_read_(fap->fa_off + off, &read_buf, sizeof(read_buf), false); + BOOT_LOG_INF("boot_read_flag whats read - RAW rc=0x%x 0x%x", rc, read_buf); + // TEMP FOR DEBUG - REMOVE + if (bootutil_buffer_is_erased(fap, flag, sizeof *flag)) { *flag = BOOT_FLAG_UNSET; } else { @@ -235,6 +247,8 @@ boot_read_flag(const struct flash_area *fap, uint8_t *flag, uint32_t off) static inline int boot_read_copy_done(const struct flash_area *fap, uint8_t *copy_done) { + BOOT_LOG_INF("boot_read_copy_done"); + return boot_read_flag(fap, copy_done, boot_copy_done_off(fap)); } @@ -248,11 +262,29 @@ boot_read_swap_state(const struct flash_area *fap, uint8_t swap_info; int rc; + off = boot_magic_off(fap); + BOOT_LOG_INF("boot_read_swap_state MAGIC addr 0x%lx", fap->fa_off+off); rc = flash_area_read(fap, off, magic, BOOT_MAGIC_SZ); if (rc < 0) { return BOOT_EFLASH; } + + // TEMP FOR DEBUG - REMOVE + uint8_t read_buf[BOOT_MAGIC_SZ]; + BOOT_LOG_INF("boot_read_swap_state whats read rc=0x%x", rc); + for (uint8_t i=0; ifa_off + off, read_buf, BOOT_MAGIC_SZ, false); + BOOT_LOG_INF("boot_read_swap_state whats read - RAW rc=0x%x", rc); + for (uint8_t i=0; imagic = BOOT_MAGIC_UNSET; } else { @@ -260,11 +292,19 @@ boot_read_swap_state(const struct flash_area *fap, } off = boot_swap_info_off(fap); + BOOT_LOG_INF("boot_read_swap_state SWAP_INFO addr 0x%lx", fap->fa_off+off); rc = flash_area_read(fap, off, &swap_info, sizeof swap_info); if (rc < 0) { return BOOT_EFLASH; } + // TEMP FOR DEBUG - REMOVE + uint8_t read_buf_swap; + BOOT_LOG_INF("boot_read_swap_state whats read rc=0x%x 0x%x", rc, swap_info); + rc = aligned_flash_read_(fap->fa_off + off, &read_buf_swap, sizeof(swap_info), false); + BOOT_LOG_INF("boot_read_swap_state whats read - RAW rc=0x%x 0x%x", rc, read_buf_swap); + // TEMP FOR DEBUG - REMOVE + /* Extract the swap type and image number */ state->swap_type = BOOT_GET_SWAP_TYPE(swap_info); state->image_num = BOOT_GET_IMAGE_NUM(swap_info); @@ -327,12 +367,52 @@ boot_write_magic(const struct flash_area *fap) 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)); + +#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION + BOOT_LOG_INF("boot_write_magic writing MAGIC; fa_id=%d off=0x%lx (0x%lx) size=0%0x", + flash_area_get_id(fap), (unsigned long)pad_off, + (unsigned long)(flash_area_get_off(fap) + pad_off), BOOT_MAGIC_ALIGN_SIZE); + rc = flash_area_erase(fap, pad_off, BOOT_MAGIC_ALIGN_SIZE); + + // TEMP FOR DEBUG - REMOVE + uint8_t read_buf[BOOT_MAGIC_ALIGN_SIZE]; + rc = flash_area_read(fap, pad_off, read_buf, BOOT_MAGIC_ALIGN_SIZE); + BOOT_LOG_INF("boot_write_magic whats read BEFORE writing rc=0x%x", rc); + for (uint8_t i=0; ifa_off + pad_off, read_buf, BOOT_MAGIC_ALIGN_SIZE, false); + BOOT_LOG_INF("boot_write_magic whats read BEFORE writing - RAW rc=0x%x", rc); + for (uint8_t i=0; ifa_off + pad_off, read_buf, BOOT_MAGIC_ALIGN_SIZE, false); + BOOT_LOG_INF("boot_write_magic whats read AFTER writing - RAW rc=0x%x", rc); + for (uint8_t i=0; ifa_off + off + BOOT_MAX_ALIGN, read_buf, BOOT_MAX_ALIGN, false); + BOOT_LOG_INF("boot_write_trailer whats read in addr 0x%lx AFTER ERASE - RAW rc=0x%x", + (unsigned long)(flash_area_get_off(fap) + off + BOOT_MAX_ALIGN), rc); + for (uint8_t i=0; ifa_off + off, read_buf, BOOT_MAX_ALIGN, false); + BOOT_LOG_INF("boot_write_trailer whats read BEFORE writing - RAW rc=0x%x", rc); + for (uint8_t i=0; ifa_off + off, read_buf, BOOT_MAX_ALIGN, false); + BOOT_LOG_INF("boot_write_trailer whats read AFTER writing - RAW rc=0x%x", rc); + for (uint8_t i=0; iih_magic, hdr->ih_flags, hdr->ih_hdr_size, hdr->ih_img_size); + return 0; } diff --git a/boot/bootutil/src/loader.c b/boot/bootutil/src/loader.c index 62f980b69c..295fed1d29 100644 --- a/boot/bootutil/src/loader.c +++ b/boot/bootutil/src/loader.c @@ -50,6 +50,9 @@ #include "bootutil/boot_hooks.h" #include "bootutil/mcuboot_status.h" +// TEMP FOR DEBUG - REMOVE +#include "bootloader_flash_priv.h" + #ifdef MCUBOOT_ENC_IMAGES #include "bootutil/enc_key.h" #endif @@ -751,11 +754,49 @@ boot_write_status(const struct boot_loader_state *state, struct boot_status *bs) flash_area_get_id(fap), (unsigned long)off, (unsigned long)flash_area_get_off(fap) + off); +#ifdef MCUBOOT_FLASH_HAS_HW_ENCRYPTION + BOOT_LOG_INF("loader.c writing SWAP STATUS; 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_erase(fap, off, align); + + // TEMP FOR DEBUG - REMOVE + uint8_t read_buf[BOOT_MAX_ALIGN]; + rc = flash_area_read(fap, off, read_buf, BOOT_MAX_ALIGN); + BOOT_LOG_INF("whats read BEFORE writing rc=0x%x", rc); + for (uint8_t i=0; ifa_off + off, read_buf, BOOT_MAX_ALIGN, false); + BOOT_LOG_INF("whats read BEFORE writing - RAW rc=0x%x", rc); + for (uint8_t i=0; ifa_off + off, read_buf, BOOT_MAX_ALIGN, false); + BOOT_LOG_INF("whats read AFTER writing - RAW rc=0x%x", rc); + for (uint8_t i=0; i 0) { + aligned_flash_write(aligned_addr, write_data, addr_offset); + } + + if(bytes < sizeof(write_data)) { + aligned_flash_write(aligned_addr + bytes, write_data + bytes, sizeof(write_data) - bytes); + } + + bytes_remaining -= bytes_written; + + /* Write remaining data to Flash if any */ + + uint32_t offset = bytes; + + while (bytes_remaining != 0) { + bytes = MIN(bytes_remaining, sizeof(write_data)); + if (bootloader_flash_read(aligned_addr + offset, write_data, ALIGN_UP(bytes, FLASH_SECTOR_SIZE), true) != ESP_OK) { + return false; + } + + if (bootloader_flash_erase_range(aligned_addr + offset, ALIGN_UP(bytes, FLASH_SECTOR_SIZE)) != ESP_OK) { + BOOT_LOG_ERR("%s: Flash erase failed", __func__); + return -1; + } + + if(bytes < sizeof(write_data)) { + aligned_flash_write(aligned_addr + offset + bytes, write_data + bytes, sizeof(write_data) - bytes); + } + + offset += bytes; + bytes_written += bytes; + bytes_remaining -= bytes; + } + + // TEMP FOR DEBUG - REMOVE + if (addr == 0x16ffa0) { + bootloader_flash_read(aligned_addr, write_data, FLASH_SECTOR_SIZE, false); + BOOT_LOG_INF("%s: addr unaligned 0x16ffa0 whats read on 0x%lx sector AFTER erasing - RAW", __func__, aligned_addr); + for (uint32_t i=0; ifa_off + off; BOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len); - bool success = aligned_flash_write(start_addr, src, len); - if (!success) { + + if (!aligned_flash_write(start_addr, src, len)) { BOOT_LOG_ERR("%s: Flash write failed", __func__); return -1; } @@ -308,19 +460,24 @@ int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len) return -1; } + const uint32_t start_addr = fa->fa_off + off; + if ((len % FLASH_SECTOR_SIZE) != 0 || (off % FLASH_SECTOR_SIZE) != 0) { - BOOT_LOG_ERR("%s: Not aligned on sector Offset: 0x%x Length: 0x%x", - __func__, (int)off, (int)len); - return -1; - } + BOOT_LOG_DBG("%s: Not aligned on sector Offset: 0x%x Length: 0x%x", + __func__, (int)start_addr, (int)len); - const uint32_t start_addr = fa->fa_off + off; - BOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len); + if(!aligned_flash_erase(start_addr, len)) { + return -1; + } + } else { + BOOT_LOG_DBG("%s: Aligned Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len); - if (bootloader_flash_erase_range(start_addr, len) != ESP_OK) { - BOOT_LOG_ERR("%s: Flash erase failed", __func__); - return -1; + if (bootloader_flash_erase_range(start_addr, len) != ESP_OK) { + BOOT_LOG_ERR("%s: Flash erase failed", __func__); + return -1; + } } + #if VALIDATE_PROGRAM_OP for (size_t i = 0; i < len; i++) { uint8_t *val = (void *)(start_addr + i);