From 1197a8074144f74ceed416cedf7ca0b13c24e802 Mon Sep 17 00:00:00 2001 From: Tiago Medicci Serrano Date: Mon, 31 Jul 2023 16:41:43 -0300 Subject: [PATCH] esp32s3: Enhance protected build linker scripts and memory layout Instead of setting kernel/user space instruction and data ROM as hard-coded values on linker, set them according to the max size of the kernel image set by CONFIG_ESP32S3_KERNEL_IMAGE_SIZE. This is done by making KIROM, UIROM, KDROM and UDROM dependent on the kernel size value. Also, override CONFIG_NUTTX_USERSPACE config according to CONFIG_ESP32S3_KERNEL_IMAGE_SIZE by using a custom PREBUILD definition. --- arch/xtensa/src/esp32s3/Kconfig | 31 ++++++++++++++++--- arch/xtensa/src/esp32s3/esp32s3_userspace.c | 2 +- .../common/scripts/protected_memory.ld | 16 +++++----- .../esp32s3-devkit/configs/knsh/defconfig | 3 +- tools/esp32s3/Config.mk | 14 +++++++-- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/arch/xtensa/src/esp32s3/Kconfig b/arch/xtensa/src/esp32s3/Kconfig index caafe4be11..c7d5beba49 100644 --- a/arch/xtensa/src/esp32s3/Kconfig +++ b/arch/xtensa/src/esp32s3/Kconfig @@ -1355,13 +1355,36 @@ config ESP32S3_PARTITION_TABLE_OFFSET default 0x8000 depends on ESP32S3_APP_FORMAT_LEGACY +config ESP32S3_KERNEL_OFFSET + hex + default 0x10000 + depends on ESP32S3_APP_FORMAT_LEGACY + if BUILD_PROTECTED -config ESP32S3_USER_IMAGE_OFFSET - hex "User image offset" - default 0x90000 +config ESP32S3_KERNEL_IMAGE_SIZE + hex "Kernel Image Size" + default 0x100000 + depends on ESP32S3_APP_FORMAT_LEGACY ---help--- - Offset in SPI Flash for flashing the User application firmware image. + Max size of the kernel image. This parameter is used to 1) write to + the SPI flash, 2) calculate the offset for the user image and 3) set + the KIROM, KDROM, UIROM and UDROM parameters in the linker. Please + check boards/xtensa/esp32s3/common/scripts/protected_memory.ld for + more information. + +config ESP32S3_KERNEL_RAM_SIZE + hex "Kernel Allocated RAM" + default 0x29000 + range 0x10000 0x54700 + depends on ESP32S3_APP_FORMAT_LEGACY + ---help--- + Selects the amount of RAM available to the kernel from the total + available amount (337,75KiB). The rest of the memory will be + allocated to the user space. Respectively, this config will set the + KDRAM and UDRAM parameters of the linker. Please check + boards/xtensa/esp32s3/common/scripts/protected_memory.ld for more + information. endif # BUILD_PROTECTED diff --git a/arch/xtensa/src/esp32s3/esp32s3_userspace.c b/arch/xtensa/src/esp32s3/esp32s3_userspace.c index fb52f186a8..ca94a1eef5 100644 --- a/arch/xtensa/src/esp32s3/esp32s3_userspace.c +++ b/arch/xtensa/src/esp32s3/esp32s3_userspace.c @@ -52,7 +52,7 @@ * Pre-processor Definitions ****************************************************************************/ -#define USER_IMAGE_OFFSET CONFIG_ESP32S3_USER_IMAGE_OFFSET +#define USER_IMAGE_OFFSET CONFIG_ESP32S3_KERNEL_OFFSET + CONFIG_ESP32S3_KERNEL_IMAGE_SIZE #define MMU_BLOCK0_VADDR SOC_DROM_LOW #define MMU_SIZE 0x3f0000 diff --git a/boards/xtensa/esp32s3/common/scripts/protected_memory.ld b/boards/xtensa/esp32s3/common/scripts/protected_memory.ld index 4c7e9f2e0a..10a73d0ea4 100644 --- a/boards/xtensa/esp32s3/common/scripts/protected_memory.ld +++ b/boards/xtensa/esp32s3/common/scripts/protected_memory.ld @@ -42,7 +42,7 @@ * Application Image properly. * * For more information, refer to the bootloader linker scripts: - * https://github.com/espressif/esp-idf/blob/v4.4.2/components/bootloader/subproject/main/ld/esp32s3/bootloader.ld#L41-L47 + * https://github.com/espressif/esp-idf/blob/dbb64db552068d440c2659294dcf2a5544fe3b6b/components/bootloader/subproject/main/ld/esp32s3/bootloader.ld#L52 */ #define SRAM_IRAM_END 0x403cc700 @@ -73,7 +73,7 @@ MEMORY { metadata (RX) : org = 0x0, len = 0x30 ROM (RX) : org = ORIGIN(metadata) + LENGTH(metadata), - len = 0x100000 - ORIGIN(ROM) + len = FLASH_SIZE - ORIGIN(ROM) /* Instruction RAM */ @@ -90,13 +90,13 @@ MEMORY * constraint that (paddr % 64KB == vaddr % 64KB). */ - KIROM (RX) : org = 0x42000020, len = 0x80000 - 0x20 - UIROM (RX) : org = 0x42080000, len = 0x180000 + KIROM (RX) : org = 0x42000020, len = CONFIG_ESP32S3_KERNEL_IMAGE_SIZE - 0x20 + UIROM (RX) : org = ORIGIN(KIROM) + LENGTH(KIROM), len = FLASH_SIZE - LENGTH(KIROM) /* Shared data RAM, excluding memory reserved for ROM bss/data/stack. */ - KDRAM (RW) : org = ORIGIN(KIRAM) + LENGTH(KIRAM) - I_D_SRAM_OFFSET, len = 64K - UDRAM (RW) : org = ORIGIN(KDRAM) + LENGTH(KDRAM), len = 256K + KDRAM (RW) : org = ORIGIN(KIRAM) + LENGTH(KIRAM) - I_D_SRAM_OFFSET, len = CONFIG_ESP32S3_KERNEL_RAM_SIZE + UDRAM (RW) : org = ORIGIN(KDRAM) + LENGTH(KDRAM), len = I_D_SRAM_SIZE - LENGTH(KDRAM) /* Flash mapped constant data */ @@ -109,7 +109,7 @@ MEMORY * SRAM contents when loading the User application image. */ - KDROM (R) : org = 0x3c000020, len = 0x80000 - 0x20 - UDROM (R) : org = 0x3c080030, len = 0x180000 - ORIGIN(ROM) + KDROM (R) : org = 0x3c000020, len = CONFIG_ESP32S3_KERNEL_IMAGE_SIZE - 0x20 + UDROM (R) : org = ORIGIN(KDROM) + LENGTH(KDROM) + ORIGIN(ROM), len = FLASH_SIZE - LENGTH(KDROM) - ORIGIN(ROM) } diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/knsh/defconfig b/boards/xtensa/esp32s3/esp32s3-devkit/configs/knsh/defconfig index f0f9726de1..18ddbdb6c7 100644 --- a/boards/xtensa/esp32s3/esp32s3-devkit/configs/knsh/defconfig +++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/knsh/defconfig @@ -23,6 +23,8 @@ CONFIG_BUILD_PROTECTED=y CONFIG_BUILTIN=y CONFIG_DEBUG_FULLOPT=y CONFIG_DEBUG_SYMBOLS=y +CONFIG_ESP32S3_KERNEL_IMAGE_SIZE=0x80000 +CONFIG_ESP32S3_KERNEL_RAM_SIZE=0x10000 CONFIG_ESP32S3_UART0=y CONFIG_ESP32S3_WCL=y CONFIG_FS_PROCFS=y @@ -37,7 +39,6 @@ CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_LINELEN=64 CONFIG_NSH_READLINE=y -CONFIG_NUTTX_USERSPACE=0x3c080030 CONFIG_PASS1_BUILDIR="boards/xtensa/esp32s3/common/kernel" CONFIG_PREALLOC_TIMERS=4 CONFIG_RAM_SIZE=114688 diff --git a/tools/esp32s3/Config.mk b/tools/esp32s3/Config.mk index 65d1ea38d0..039f1e7c54 100644 --- a/tools/esp32s3/Config.mk +++ b/tools/esp32s3/Config.mk @@ -80,7 +80,7 @@ ifdef ESPTOOL_BINDIR endif ifeq ($(CONFIG_ESP32S3_APP_FORMAT_LEGACY),y) - APP_OFFSET := 0x10000 + APP_OFFSET := $(CONFIG_ESP32S3_KERNEL_OFFSET) APP_IMAGE := nuttx.bin FLASH_APP := $(APP_OFFSET) $(APP_IMAGE) else ifeq ($(CONFIG_ESP32S3_APP_FORMAT_MCUBOOT),y) @@ -103,7 +103,7 @@ endif ESPTOOL_BINS += $(FLASH_APP) ifeq ($(CONFIG_BUILD_PROTECTED),y) - ESPTOOL_BINS += $(CONFIG_ESP32S3_USER_IMAGE_OFFSET) nuttx_user.bin + ESPTOOL_BINS += $(shell printf "%#x\n" $$(( $(CONFIG_ESP32S3_KERNEL_OFFSET) + $(CONFIG_ESP32S3_KERNEL_IMAGE_SIZE) ))) nuttx_user.bin endif # MERGEBIN -- Merge raw binary files into a single file @@ -159,6 +159,16 @@ define MKIMAGE endef endif +# PREBUILD -- Perform pre build operations + +ifeq ($(CONFIG_BUILD_PROTECTED),y) +define PREBUILD + $(Q) echo "CONFIG_NUTTX_USERSPACE=$(shell printf "%#x\n" $$(( 0x3c000030 + $(CONFIG_ESP32S3_KERNEL_IMAGE_SIZE) )))" > $(1)/.config.tmp + $(Q) kconfig-merge -m $(1)/.config $(1)/.config.tmp + $(Q)rm -f $(1)/.config.tmp +endef +endif + # POSTBUILD -- Perform post build operations define POSTBUILD