Skip to content

Commit

Permalink
soc: esp32: MCUboot support
Browse files Browse the repository at this point in the history
This make MCUboot build as Zephyr application.
Providing optinal 2nd stage bootloader to the
IDF bootloader, which is used by default.
This provides more flexibility when building
and loading multiple images and aims to
brings better DX to users by using the sysbuild.
MCUboot and applications has now separate
linker scripts.

Signed-off-by: Marek Matej <[email protected]>
  • Loading branch information
Marek Matej authored and carlescufi committed May 25, 2023
1 parent 2354cc9 commit 4796746
Show file tree
Hide file tree
Showing 52 changed files with 4,304 additions and 2,415 deletions.
4 changes: 2 additions & 2 deletions Kconfig.zephyr
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,8 @@ config BOOTLOADER_SRAM_SIZE

config BOOTLOADER_ESP_IDF
bool "ESP-IDF bootloader support"
depends on (SOC_ESP32 || SOC_ESP32S2 || SOC_ESP32C3 || SOC_ESP32S3) && !BOOTLOADER_MCUBOOT
default y
depends on SOC_FAMILY_ESP32 && !BOOTLOADER_MCUBOOT && !MCUBOOT
default y
help
This option will trigger the compilation of the ESP-IDF bootloader
inside the build folder.
Expand Down
2 changes: 2 additions & 0 deletions arch/riscv/core/isr.S
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ check_reschedule:
addi sp, sp, -16
sr a1, 0(sp)
mv a0, zero
#ifdef CONFIG_MULTITHREADING
call z_get_next_switch_handle
#endif
lr a1, 0(sp)
addi sp, sp, 16
beqz a0, no_reschedule
Expand Down
4 changes: 4 additions & 0 deletions arch/xtensa/core/xtensa-asm2.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ static ALWAYS_INLINE void usage_stop(void)

static inline void *return_to(void *interrupted)
{
#ifdef CONFIG_MULTITHREADING
return _current_cpu->nested <= 1 ?
z_get_next_switch_handle(interrupted) : interrupted;
#else
return interrupted;
#endif /* CONFIG_MULTITHREADING */
}

/* The wrapper code lives here instead of in the python script that
Expand Down
1 change: 0 additions & 1 deletion boards/riscv/esp32c3_devkitm/Kconfig.board
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@
config BOARD_ESP32C3_DEVKITM
bool "ESP32C3 Devkit-M Board"
depends on SOC_ESP32C3
select CONSOLE_HAS_DRIVER
10 changes: 10 additions & 0 deletions boards/riscv/esp32c3_devkitm/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/riscv/icev_wireless/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/riscv/stamp_c3/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/riscv/xiao_esp32c3/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/esp32/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/esp32_ethernet_kit/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/esp32s2_franzininho/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ CONFIG_GEN_ISR_TABLES=y
CONFIG_GEN_IRQ_VECTOR_TABLE=n

CONFIG_CLOCK_CONTROL=y

CONFIG_BOOTLOADER_ESP_IDF=y
10 changes: 10 additions & 0 deletions boards/xtensa/esp32s2_saola/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
2 changes: 0 additions & 2 deletions boards/xtensa/esp32s2_saola/esp32s2_saola_defconfig
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,3 @@ CONFIG_GEN_ISR_TABLES=y
CONFIG_GEN_IRQ_VECTOR_TABLE=n

CONFIG_CLOCK_CONTROL=y

CONFIG_BOOTLOADER_ESP_IDF=y
10 changes: 10 additions & 0 deletions boards/xtensa/esp32s3_devkitm/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/esp_wrover_kit/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/heltec_wifi_lora32_v2/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/m5stickc_plus/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/odroid_go/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
10 changes: 10 additions & 0 deletions boards/xtensa/olimex_esp32_evb/Kconfig.sysbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2023 Espressif Systems (Shanghai) Co., Ltd.
# SPDX-License-Identifier: Apache-2.0

choice BOOTLOADER
default BOOTLOADER_MCUBOOT
endchoice

choice BOOT_SIGNATURE_TYPE
default BOOT_SIGNATURE_TYPE_NONE
endchoice
11 changes: 11 additions & 0 deletions drivers/flash/flash_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,17 @@ struct flash_esp32_dev_config {
};

struct flash_esp32_dev_data {
#ifdef CONFIG_MULTITHREADING
struct k_sem sem;
#endif
};

static const struct flash_parameters flash_esp32_parameters = {
.write_block_size = FLASH_WRITE_BLK_SZ,
.erase_value = 0xff,
};

#ifdef CONFIG_MULTITHREADING
static inline void flash_esp32_sem_take(const struct device *dev)
{
struct flash_esp32_dev_data *data = dev->data;
Expand All @@ -83,6 +86,12 @@ static inline void flash_esp32_sem_give(const struct device *dev)

k_sem_give(&data->sem);
}
#else

#define flash_esp32_sem_take(dev) do {} while (0)
#define flash_esp32_sem_give(dev) do {} while (0)

#endif /* CONFIG_MULTITHREADING */

static int flash_esp32_read(const struct device *dev, off_t address, void *buffer, size_t length)
{
Expand Down Expand Up @@ -150,7 +159,9 @@ static int flash_esp32_init(const struct device *dev)
{
struct flash_esp32_dev_data *const dev_data = dev->data;

#ifdef CONFIG_MULTITHREADING
k_sem_init(&dev_data->sem, 1, 1);
#endif /* CONFIG_MULTITHREADING */

return 0;
}
Expand Down
24 changes: 16 additions & 8 deletions soc/riscv/esp32c3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,6 @@ if(CONFIG_BOOTLOADER_ESP_IDF)
INSTALL_COMMAND ""
)

if(CONFIG_BUILD_OUTPUT_BIN)
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
COMMAND ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/esptool_py/esptool/esptool.py
ARGS --chip ${CONFIG_SOC} elf2image --flash_mode dio --flash_freq 40m --flash_size ${esptoolpy_flashsize}MB
-o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin
${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf)
endif()

set_property(TARGET bintools PROPERTY disassembly_flag_inline_source)

add_dependencies(app EspIdfBootloader EspPartitionTable)
Expand All @@ -82,6 +74,22 @@ if(CONFIG_BOOTLOADER_ESP_IDF)

endif()

if(CONFIG_MCUBOOT OR CONFIG_BOOTLOADER_ESP_IDF)

if(CONFIG_BUILD_OUTPUT_BIN)
set_property(GLOBAL APPEND PROPERTY extra_post_build_commands
COMMAND ${PYTHON_EXECUTABLE} ${ESP_IDF_PATH}/components/esptool_py/esptool/esptool.py
ARGS --chip esp32c3 elf2image --flash_mode dio --flash_freq 40m --flash_size ${esptoolpy_flashsize}MB
-o ${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin
${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.elf)
endif()

if(CONFIG_MCUBOOT)
board_finalize_runner_args(esp32 "--esp-flash-bootloader=${CMAKE_BINARY_DIR}/zephyr/${CONFIG_KERNEL_BIN_NAME}.bin")
endif()

endif()

board_finalize_runner_args(esp32 "--esp-boot-address=${boot_off}")

board_finalize_runner_args(esp32 "--esp-app-address=${img_0_off}")
2 changes: 1 addition & 1 deletion soc/riscv/esp32c3/Kconfig.soc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ config SOC_ESP32C3
select DYNAMIC_INTERRUPTS
select CLOCK_CONTROL
select PINCTRL
select XIP
select XIP if !MCUBOOT
select HAS_ESPRESSIF_HAL
select RISCV_ISA_RV32I
select RISCV_ISA_EXT_M
Expand Down
Loading

0 comments on commit 4796746

Please sign in to comment.