diff --git a/components/bootloader_support/src/bootloader_common_loader.c b/components/bootloader_support/src/bootloader_common_loader.c index f1b902bfd4..cef2e7e996 100644 --- a/components/bootloader_support/src/bootloader_common_loader.c +++ b/components/bootloader_support/src/bootloader_common_loader.c @@ -28,6 +28,7 @@ #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */ #define IS_FIELD_SET(rev_full) (((rev_full) != 65535) && ((rev_full) != 0)) +#define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1)) static const char* TAG = "boot_comm"; @@ -264,7 +265,10 @@ rtc_retain_mem_t* bootloader_common_get_rtc_retain_mem(void) #if ESP_ROM_HAS_LP_ROM #define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_LOW) #else - #define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_HIGH - sizeof(rtc_retain_mem_t)) + /* Since the structure containing the retain_mem_t is aligned on 8 by the linker, make sure we align this + * structure size here too */ + #define RETAIN_MEM_SIZE ALIGN_UP(sizeof(rtc_retain_mem_t), 8) + #define RTC_RETAIN_MEM_ADDR (SOC_RTC_DRAM_HIGH - RETAIN_MEM_SIZE) #endif //ESP_ROM_HAS_LP_ROM static rtc_retain_mem_t *const s_bootloader_retain_mem = (rtc_retain_mem_t *)RTC_RETAIN_MEM_ADDR; return s_bootloader_retain_mem; diff --git a/components/esp_system/ld/ld.common b/components/esp_system/ld/ld.common index a4a9b4e892..04401fd24b 100644 --- a/components/esp_system/ld/ld.common +++ b/components/esp_system/ld/ld.common @@ -6,6 +6,8 @@ #include "sdkconfig.h" +#define ALIGN_UP(SIZE, AL) (((SIZE) + (AL - 1)) & ~(AL - 1)) + /* CPU instruction prefetch padding size for flash mmap scenario */ #define _esp_flash_mmap_prefetch_pad_size 16 @@ -45,10 +47,14 @@ #if CONFIG_SOC_RTC_MEM_SUPPORTED #if CONFIG_BOOTLOADER_RESERVE_RTC_MEM + /** + * The ESP_BOOTLOADER_RESERVE_RTC size must have the same alignment of RTC_TIMER_RESERVE_RTC, else + * the segment will overflow at link time because not enough bytes are allocated for the RTC segment. + */ #ifdef CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC - #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE) + #define ESP_BOOTLOADER_RESERVE_RTC ALIGN_UP(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE + CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC_SIZE, 8) #else - #define ESP_BOOTLOADER_RESERVE_RTC (CONFIG_BOOTLOADER_RESERVE_RTC_SIZE) + #define ESP_BOOTLOADER_RESERVE_RTC ALIGN_UP(CONFIG_BOOTLOADER_RESERVE_RTC_SIZE, 8) #endif // not CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC #else #define ESP_BOOTLOADER_RESERVE_RTC 0