From 003e049ba882e04919c58d7c8880c30c641cb3ef Mon Sep 17 00:00:00 2001 From: Frantisek Hrbata Date: Tue, 12 May 2026 11:32:01 +0200 Subject: [PATCH] fix(esp_hw_support): guard SPIRAM-dependent code with !BOOTLOADER_BUILD The bootloader subproject's full Kconfig discovery resolves CONFIG_SPIRAM=y when the parent app has it enabled, even though esp_psram is not linked into the bootloader (the CMake gate is 'if(NOT non_os_build) if(CONFIG_SPIRAM) idf_component_optional_requires(PRIVATE esp_psram)'). Shared sources in esp_hw_support that #include esp_psram private headers or call esp_psram functions guarded only by '#if CONFIG_SPIRAM' then fail to compile in the bootloader with "fatal error: esp_private/esp_psram_extram.h: No such file or directory". Mirror the CMake gate in source guards: every '#if CONFIG_SPIRAM' block in a bootloader-compiled source that touches esp_psram becomes '#if !BOOTLOADER_BUILD && CONFIG_SPIRAM'. The leaked CONFIG_SPIRAM value in the bootloader's sdkconfig.h is then harmless because every dependent block evaluates to false. Sites updated: - esp_memory_utils.c: include of esp_psram_extram.h and all esp_psram_check_ptr_addr() call sites - port/esp32{c5,c61,p4}/cpu_region_protect.c: include of esp_psram_extram.h (inner SPIRAM_FETCH/RODATA/PRE_CONFIGURE blocks are already inside outer !BOOTLOADER_BUILD guards) Signed-off-by: Frantisek Hrbata --- components/esp_hw_support/esp_memory_utils.c | 14 +++++++------- .../port/esp32c5/cpu_region_protect.c | 4 ++-- .../port/esp32c61/cpu_region_protect.c | 4 ++-- .../port/esp32p4/cpu_region_protect.c | 8 ++++---- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/components/esp_hw_support/esp_memory_utils.c b/components/esp_hw_support/esp_memory_utils.c index fd79ef09ce9..b543b23265e 100644 --- a/components/esp_hw_support/esp_memory_utils.c +++ b/components/esp_hw_support/esp_memory_utils.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2025 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2026 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,7 +12,7 @@ #include "soc/soc_caps.h" #include "esp_attr.h" #include "esp_memory_utils.h" -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM #include "esp_private/esp_psram_extram.h" #endif @@ -22,7 +22,7 @@ bool esp_ptr_dma_ext_capable(const void *p) #if !SOC_PSRAM_DMA_CAPABLE return false; #endif //!SOC_PSRAM_DMA_CAPABLE -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM return esp_psram_check_ptr_addr(p); #else return false; @@ -35,7 +35,7 @@ bool esp_ptr_executable(const void *p) return (ip >= SOC_IROM_LOW && ip < SOC_IROM_HIGH) || (ip >= SOC_IRAM_LOW && ip < SOC_IRAM_HIGH) || (ip >= SOC_IROM_MASK_LOW && ip < SOC_IROM_MASK_HIGH) -#if SOC_SPIRAM_SUPPORTED && CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && SOC_SPIRAM_SUPPORTED && CONFIG_SPIRAM || esp_ptr_external_ram(p) #endif #if defined(SOC_CACHE_APP_LOW) && defined(CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE) @@ -61,7 +61,7 @@ bool esp_ptr_byte_accessible(const void *p) * additional check is required */ r |= (ip >= SOC_RTC_DRAM_LOW && ip < SOC_RTC_DRAM_HIGH); #endif -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM r |= esp_psram_check_ptr_addr(p); #endif #if CONFIG_ESP32S3_DATA_CACHE_16KB @@ -81,14 +81,14 @@ bool esp_ptr_external_ram(const void *p) #if !SOC_SPIRAM_SUPPORTED return false; #endif //!SOC_SPIRAM_SUPPORTED -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM return esp_psram_check_ptr_addr(p); #else return false; #endif //CONFIG_SPIRAM } -#if CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM +#if !BOOTLOADER_BUILD && CONFIG_FREERTOS_TASK_CREATE_ALLOW_EXT_MEM bool esp_stack_ptr_in_extram(uint32_t sp) { //Check if stack ptr is on PSRAM, and 16 byte aligned. diff --git a/components/esp_hw_support/port/esp32c5/cpu_region_protect.c b/components/esp_hw_support/port/esp32c5/cpu_region_protect.c index 8b8e5e6002c..d8f5bd38473 100644 --- a/components/esp_hw_support/port/esp32c5/cpu_region_protect.c +++ b/components/esp_hw_support/port/esp32c5/cpu_region_protect.c @@ -9,9 +9,9 @@ #include "esp_cpu.h" #include "esp_fault.h" #include "esp32c5/rom/rom_layout.h" -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM #include "esp_private/esp_psram_extram.h" -#endif /* CONFIG_SPIRAM */ +#endif /* !BOOTLOADER_BUILD && CONFIG_SPIRAM */ #ifdef BOOTLOADER_BUILD // Without L bit set diff --git a/components/esp_hw_support/port/esp32c61/cpu_region_protect.c b/components/esp_hw_support/port/esp32c61/cpu_region_protect.c index cc62bade13b..07ad769289e 100644 --- a/components/esp_hw_support/port/esp32c61/cpu_region_protect.c +++ b/components/esp_hw_support/port/esp32c61/cpu_region_protect.c @@ -10,9 +10,9 @@ #include "esp_cpu.h" #include "esp_fault.h" #include "esp32c61/rom/rom_layout.h" -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM #include "esp_private/esp_psram_extram.h" -#endif /* CONFIG_SPIRAM */ +#endif /* !BOOTLOADER_BUILD && CONFIG_SPIRAM */ #ifdef BOOTLOADER_BUILD // Without L bit set diff --git a/components/esp_hw_support/port/esp32p4/cpu_region_protect.c b/components/esp_hw_support/port/esp32p4/cpu_region_protect.c index 54ee7637f5c..ab682877734 100644 --- a/components/esp_hw_support/port/esp32p4/cpu_region_protect.c +++ b/components/esp_hw_support/port/esp32p4/cpu_region_protect.c @@ -11,9 +11,9 @@ #include "esp_fault.h" #include "hal/cache_ll.h" #include "riscv/csr.h" -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM #include "esp_private/esp_psram_extram.h" -#endif /* CONFIG_SPIRAM */ +#endif /* !BOOTLOADER_BUILD && CONFIG_SPIRAM */ #include "soc/chip_revision.h" #include "hal/config.h" @@ -198,14 +198,14 @@ static void esp_cpu_configure_region_protection_rev_v3(void) PMP_ENTRY_SET_CACHED_AND_UNCACHED(22, 26, page_aligned_drom_resv_end, PMP_TOR | R); #else -#if CONFIG_SPIRAM +#if !BOOTLOADER_BUILD && CONFIG_SPIRAM const uint32_t pmpaddr10 = PMPADDR_NAPOT(SOC_EXTRAM_LOW, SOC_EXTRAM_HIGH); PMP_RESET_AND_ENTRY_SET(10, pmpaddr10, PMP_NAPOT | CONDITIONAL_RWX); const uint32_t pmpaddr11 = PMPADDR_NAPOT(CACHE_LL_L2MEM_NON_CACHE_ADDR(SOC_EXTRAM_LOW), CACHE_LL_L2MEM_NON_CACHE_ADDR(SOC_EXTRAM_HIGH)); PMP_RESET_AND_ENTRY_SET(11, pmpaddr11, PMP_NAPOT | CONDITIONAL_RWX); _Static_assert(SOC_EXTRAM_LOW < SOC_EXTRAM_HIGH, "Invalid I/D_EXTRAM region"); -#endif /* CONFIG_SPIRAM */ +#endif /* !BOOTLOADER_BUILD && CONFIG_SPIRAM */ const uint32_t pmpaddr12 = PMPADDR_NAPOT(SOC_IROM_LOW, SOC_IROM_HIGH); PMP_RESET_AND_ENTRY_SET(12, pmpaddr12, PMP_NAPOT | CONDITIONAL_RX);