From 9caaddb251efdeb980dc0f9456e1a13d70e58de1 Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Fri, 11 Jul 2025 15:17:16 -0300 Subject: [PATCH 1/5] boards/xtensa: fixes SPI Flash for MCUBoot usage Fixes partitions for MCUBoot usage. Signed-off-by: Filipe Cavalcanti --- .../esp32/common/src/esp32_board_spiflash.c | 59 +++------ .../configs/mcuboot_update_agent/defconfig | 33 +++-- .../common/src/esp32s2_board_spiflash.c | 97 +++++++++++++-- .../configs/mcuboot_update_agent/defconfig | 89 ++++++++++++++ .../common/src/esp32s3_board_spiflash.c | 116 ++++++++++++++---- .../configs/mcuboot_update_agent/defconfig | 97 +++++++++++++++ 6 files changed, 410 insertions(+), 81 deletions(-) create mode 100644 boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_update_agent/defconfig create mode 100644 boards/xtensa/esp32s3/esp32s3-devkit/configs/mcuboot_update_agent/defconfig diff --git a/boards/xtensa/esp32/common/src/esp32_board_spiflash.c b/boards/xtensa/esp32/common/src/esp32_board_spiflash.c index 390445a7ee991..9c388b0f6cbf9 100644 --- a/boards/xtensa/esp32/common/src/esp32_board_spiflash.c +++ b/boards/xtensa/esp32/common/src/esp32_board_spiflash.c @@ -39,12 +39,10 @@ #include #include #include +#include #ifdef CONFIG_ESP32_SPIFLASH_NXFFS #include #endif -#ifdef CONFIG_BCH -#include -#endif #include "esp32_spiflash.h" #include "esp32_board_spiflash.h" @@ -69,15 +67,6 @@ * Private Types ****************************************************************************/ -#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION -struct ota_partition_s -{ - uint32_t offset; /* Partition offset from the beginning of MTD */ - uint32_t size; /* Partition size in bytes */ - const char *devpath; /* Partition device path */ -}; -#endif - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -91,22 +80,25 @@ static int init_ota_partitions(void); ****************************************************************************/ #ifdef CONFIG_ESP32_HAVE_OTA_PARTITION -static const struct ota_partition_s g_ota_partition_table[] = +static const struct partition_s g_ota_partition_table[] = { { - .offset = CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET, - .size = CONFIG_ESP32_OTA_SLOT_SIZE, - .devpath = CONFIG_ESP32_OTA_PRIMARY_SLOT_DEVPATH + .name = CONFIG_ESP32_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESP32_OTA_SLOT_SIZE, }, { - .offset = CONFIG_ESP32_OTA_SECONDARY_SLOT_OFFSET, - .size = CONFIG_ESP32_OTA_SLOT_SIZE, - .devpath = CONFIG_ESP32_OTA_SECONDARY_SLOT_DEVPATH + .name = CONFIG_ESP32_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESP32_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESP32_OTA_SLOT_SIZE, }, { - .offset = CONFIG_ESP32_OTA_SCRATCH_OFFSET, - .size = CONFIG_ESP32_OTA_SCRATCH_SIZE, - .devpath = CONFIG_ESP32_OTA_SCRATCH_DEVPATH + .name = CONFIG_ESP32_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESP32_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESP32_OTA_SCRATCH_SIZE, } }; #endif @@ -123,15 +115,15 @@ static int init_ota_partitions(void) for (int i = 0; i < nitems(g_ota_partition_table); ++i) { - const struct ota_partition_s *part = &g_ota_partition_table[i]; - mtd = esp32_spiflash_alloc_mtdpart(part->offset, part->size, + const struct partition_s *part = &g_ota_partition_table[i]; + mtd = esp32_spiflash_alloc_mtdpart(part->firstblock, part->blocksize, OTA_ENCRYPT); - ret = register_mtddriver(part->devpath, mtd, 0755, NULL); + ret = register_mtddriver(part->name, mtd, 0755, NULL); if (ret < 0) { syslog(LOG_ERR, "register_mtddriver %s failed: %d\n", - part->devpath, ret); + part->name, ret); return ret; } } @@ -406,8 +398,6 @@ static int init_storage_partition(void) } #else - int minor; - char path[32]; ret = register_mtddriver("/dev/esp32flash", mtd, 0755, NULL); if (ret < 0) @@ -416,19 +406,6 @@ static int init_storage_partition(void) return ret; } -#ifdef CONFIG_ESP32_HAVE_OTA_PARTITION - minor = nitems(g_ota_partition_table); -#else - minor = 0; -#endif - snprintf(path, sizeof(path), "/dev/mtdblock%d", minor); - ret = register_mtddriver(path, mtd, 0755, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to register the MTD driver %s, \ - ret %d\n", path, ret); - return ret; - } #endif return ret; diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/mcuboot_update_agent/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/mcuboot_update_agent/defconfig index bae8bc89e6718..117d6d055913f 100644 --- a/boards/xtensa/esp32/esp32-devkitc/configs/mcuboot_update_agent/defconfig +++ b/boards/xtensa/esp32/esp32-devkitc/configs/mcuboot_update_agent/defconfig @@ -8,6 +8,7 @@ # CONFIG_ARCH_LEDS is not set # CONFIG_NSH_ARGCAT is not set # CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ALLOW_BSD_COMPONENTS=y CONFIG_ARCH="xtensa" CONFIG_ARCH_BOARD="esp32-devkitc" CONFIG_ARCH_BOARD_COMMON=y @@ -15,23 +16,23 @@ CONFIG_ARCH_BOARD_ESP32_DEVKITC=y CONFIG_ARCH_CHIP="esp32" CONFIG_ARCH_CHIP_ESP32=y CONFIG_ARCH_CHIP_ESP32WROVER=y +CONFIG_ARCH_INTERRUPTSTACK=2048 CONFIG_ARCH_STACKDUMP=y CONFIG_ARCH_XTENSA=y CONFIG_BOARDCTL_RESET=y CONFIG_BOARD_LOOPSPERMSEC=16717 CONFIG_BUILTIN=y -CONFIG_DEBUG_ASSERTIONS=y -CONFIG_DEBUG_FEATURES=y -CONFIG_DEBUG_FULLOPT=y -CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEFAULT_TASK_STACKSIZE=4096 +CONFIG_DEV_URANDOM=y CONFIG_DRIVERS_IEEE80211=y CONFIG_DRIVERS_WIRELESS=y CONFIG_ESP32_APP_FORMAT_MCUBOOT=y CONFIG_ESP32_SPIFLASH=y +CONFIG_ESP32_SPIFLASH_SPIFFS=y CONFIG_ESP32_UART0=y CONFIG_ESPRESSIF_WIFI=y +CONFIG_EXAMPLES_MCUBOOT_SLOT_CONFIRM=y CONFIG_EXAMPLES_MCUBOOT_UPDATE_AGENT=y -CONFIG_EXAMPLES_MCUBOOT_UPDATE_AGENT_DL_BUFFER_SIZE=4096 CONFIG_FS_PROCFS=y CONFIG_HAVE_CXX=y CONFIG_HAVE_CXXINITIALIZE=y @@ -39,24 +40,34 @@ CONFIG_IDLETHREAD_STACKSIZE=3072 CONFIG_INIT_ENTRYPOINT="nsh_main" CONFIG_INIT_STACKSIZE=3072 CONFIG_INTELHEX_BINARY=y -CONFIG_LINE_MAX=300 -CONFIG_MM_REGIONS=3 +CONFIG_LINE_MAX=64 +CONFIG_MM_REGIONS=4 CONFIG_NAME_MAX=48 CONFIG_NETDB_DNSCLIENT=y +CONFIG_NETDB_DNSCLIENT_NAMESIZE=64 CONFIG_NETDEV_LATEINIT=y CONFIG_NETDEV_PHY_IOCTL=y CONFIG_NETDEV_WIRELESS_IOCTL=y +CONFIG_NETUTILS_CJSON=y +CONFIG_NETUTILS_IPERF=y +CONFIG_NETUTILS_TELNETD=y CONFIG_NETUTILS_WEBCLIENT=y CONFIG_NET_BROADCAST=y +CONFIG_NET_ETH_PKTSIZE=1518 CONFIG_NET_ICMP_SOCKET=y +CONFIG_NET_PREALLOC_DEVIF_CALLBACKS=32 +CONFIG_NET_STATISTICS=y CONFIG_NET_TCP=y +CONFIG_NET_TCP_DELAYED_ACK=y +CONFIG_NET_TCP_WRITE_BUFFERS=y CONFIG_NET_UDP=y CONFIG_NSH_ARCHINIT=y CONFIG_NSH_BUILTIN_APPS=y CONFIG_NSH_FILEIOSIZE=512 CONFIG_NSH_MOTD=y -CONFIG_NSH_MOTD_STRING="Welcome to MCUboot support on NuttX!" +CONFIG_NSH_MOTD_STRING="This is MCUBoot Update Agent image" CONFIG_NSH_READLINE=y +CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=2048 CONFIG_PREALLOC_TIMERS=4 CONFIG_PTHREAD_MUTEX_TYPES=y CONFIG_RAM_SIZE=114688 @@ -65,6 +76,10 @@ CONFIG_RR_INTERVAL=200 CONFIG_SCHED_LPWORK=y CONFIG_SCHED_WAITPID=y CONFIG_SIG_DEFAULT=y +CONFIG_SMP=y +CONFIG_SMP_NCPUS=2 +CONFIG_SPI=y +CONFIG_SPIFFS_NAME_MAX=48 CONFIG_START_DAY=6 CONFIG_START_MONTH=12 CONFIG_START_YEAR=2011 @@ -77,4 +92,4 @@ CONFIG_UART0_SERIAL_CONSOLE=y CONFIG_WIRELESS=y CONFIG_WIRELESS_WAPI=y CONFIG_WIRELESS_WAPI_CMDTOOL=y -CONFIG_WIRELESS_WAPI_STACKSIZE=4096 +CONFIG_WIRELESS_WAPI_INITCONF=y diff --git a/boards/xtensa/esp32s2/common/src/esp32s2_board_spiflash.c b/boards/xtensa/esp32s2/common/src/esp32s2_board_spiflash.c index 80fcba4138352..966cb85a4bb5d 100644 --- a/boards/xtensa/esp32s2/common/src/esp32s2_board_spiflash.c +++ b/boards/xtensa/esp32s2/common/src/esp32s2_board_spiflash.c @@ -27,6 +27,7 @@ #include #include +#include #include "inttypes.h" #include @@ -40,9 +41,7 @@ #include #include #include -#ifdef CONFIG_BCH -#include -#endif +#include #include "espressif/esp_spiflash.h" #include "espressif/esp_spiflash_mtd.h" @@ -60,10 +59,88 @@ # define CONFIG_ESP32S2_STORAGE_MTD_SIZE 0x100000 #endif +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static const struct partition_s g_ota_partition_table[] = +{ + { + .name = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESPRESSIF_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SCRATCH_SIZE, + } +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: init_ota_partitions + * + * Description: + * Initialize partitions that are dedicated to firmware OTA update. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void) +{ + struct mtd_dev_s *mtd; + int ret = OK; + + for (int i = 0; i < nitems(g_ota_partition_table); ++i) + { + const struct partition_s *part = &g_ota_partition_table[i]; + mtd = esp_spiflash_alloc_mtdpart(part->firstblock, part->blocksize); + + ret = register_mtddriver(part->name, mtd, 0755, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n", + part->name, ret); + return ret; + } + } + + return ret; +} +#endif + /**************************************************************************** * Name: setup_smartfs * @@ -339,12 +416,6 @@ static int init_storage_partition(void) return ret; } - ret = register_mtddriver("/dev/mtdblock0", mtd, 0755, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to register MTD mtdblock0: %d\n", ret); - return ret; - } #endif return ret; @@ -372,6 +443,14 @@ int board_spiflash_init(void) return ret; } +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + return ret; + } +#endif + ret = init_storage_partition(); if (ret < 0) { diff --git a/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_update_agent/defconfig b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_update_agent/defconfig new file mode 100644 index 0000000000000..203fea28b58d9 --- /dev/null +++ b/boards/xtensa/esp32s2/esp32s2-saola-1/configs/mcuboot_update_agent/defconfig @@ -0,0 +1,89 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_LEDS is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ALLOW_BSD_COMPONENTS=y +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="esp32s2-saola-1" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32S2_SAOLA_1=y +CONFIG_ARCH_CHIP="esp32s2" +CONFIG_ARCH_CHIP_ESP32S2=y +CONFIG_ARCH_CHIP_ESP32S2WROVER=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARDCTL_RESET=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_DEFAULT_TASK_STACKSIZE=4096 +CONFIG_DRIVERS_IEEE80211=y +CONFIG_DRIVERS_WIRELESS=y +CONFIG_ESP32S2_APP_FORMAT_MCUBOOT=y +CONFIG_ESP32S2_RT_TIMER_TASK_STACK_SIZE=4096 +CONFIG_ESP32S2_UART0=y +CONFIG_ESPRESSIF_SPIFLASH=y +CONFIG_ESPRESSIF_WIFI=y +CONFIG_EXAMPLES_MCUBOOT_SLOT_CONFIRM=y +CONFIG_EXAMPLES_MCUBOOT_UPDATE_AGENT=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INIT_STACKSIZE=3072 +CONFIG_INTELHEX_BINARY=y +CONFIG_IOB_NBUFFERS=124 +CONFIG_IOB_THROTTLE=24 +CONFIG_LINE_MAX=64 +CONFIG_NAME_MAX=48 +CONFIG_NETDB_DNSCLIENT=y +CONFIG_NETDEV_LATEINIT=y +CONFIG_NETDEV_PHY_IOCTL=y +CONFIG_NETDEV_WIRELESS_IOCTL=y +CONFIG_NETUTILS_CJSON=y +CONFIG_NETUTILS_IPERF=y +CONFIG_NETUTILS_WEBCLIENT=y +CONFIG_NET_BROADCAST=y +CONFIG_NET_ETH_PKTSIZE=1514 +CONFIG_NET_ICMP_SOCKET=y +CONFIG_NET_TCP=y +CONFIG_NET_TCP_DELAYED_ACK=y +CONFIG_NET_TCP_WRITE_BUFFERS=y +CONFIG_NET_UDP=y +CONFIG_NET_UDP_WRITE_BUFFERS=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_MOTD=y +CONFIG_NSH_MOTD_STRING="This is MCUBoot Update Agent image" +CONFIG_NSH_READLINE=y +CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=2048 +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PTHREAD_MUTEX_TYPES=y +CONFIG_RAM_SIZE=114688 +CONFIG_RAM_START=0x20000000 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SIG_DEFAULT=y +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_SYSLOG_BUFFER=y +CONFIG_SYSTEM_DHCPC_RENEW=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_PING=y +CONFIG_TIMER=y +CONFIG_TLS_TASK_NELEM=4 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_WIRELESS=y +CONFIG_WIRELESS_WAPI=y +CONFIG_WIRELESS_WAPI_CMDTOOL=y +CONFIG_WIRELESS_WAPI_INITCONF=y +CONFIG_WIRELESS_WAPI_STACKSIZE=8192 diff --git a/boards/xtensa/esp32s3/common/src/esp32s3_board_spiflash.c b/boards/xtensa/esp32s3/common/src/esp32s3_board_spiflash.c index fa739f1d195fb..121735dc21944 100644 --- a/boards/xtensa/esp32s3/common/src/esp32s3_board_spiflash.c +++ b/boards/xtensa/esp32s3/common/src/esp32s3_board_spiflash.c @@ -27,6 +27,7 @@ #include #include +#include #include "inttypes.h" #include @@ -40,12 +41,7 @@ #include #include #include -#ifdef CONFIG_MTD_PARTITION #include -#endif -#ifdef CONFIG_BCH -#include -#endif #include "esp32s3_spiflash.h" #include "esp32s3_spiflash_mtd.h" @@ -54,10 +50,95 @@ * Pre-processor Definitions ****************************************************************************/ +#ifdef CONFIG_ESP32S3_OTA_PARTITION_ENCRYPT +# define OTA_ENCRYPT true +#else +# define OTA_ENCRYPT false +#endif + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_ESP32S3_HAVE_OTA_PARTITION +static int init_ota_partitions(void); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#ifdef CONFIG_ESP32S3_HAVE_OTA_PARTITION +static const struct partition_s g_ota_partition_table[] = +{ + { + .name = CONFIG_ESP32S3_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESP32S3_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESP32S3_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESP32S3_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESP32S3_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESP32S3_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESP32S3_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESP32S3_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESP32S3_OTA_SCRATCH_SIZE, + } +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: init_ota_partitions + * + * Description: + * Initialize partitions that are dedicated to firmware OTA update. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + + #ifdef CONFIG_ESP32S3_HAVE_OTA_PARTITION +static int init_ota_partitions(void) +{ + struct mtd_dev_s *mtd; + int ret = OK; + + for (int i = 0; i < nitems(g_ota_partition_table); ++i) + { + const struct partition_s *part = &g_ota_partition_table[i]; + mtd = esp32s3_spiflash_alloc_mtdpart(part->firstblock, part->blocksize, + OTA_ENCRYPT); + + ret = register_mtddriver(part->name, mtd, 0755, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n", + part->name, ret); + return ret; + } + } + + return ret; +} +#endif + /**************************************************************************** * Name: setup_smartfs * @@ -334,22 +415,6 @@ static int init_storage_partition(void) return ret; } - ret = register_mtddriver("/dev/mtdblock0", mtd, 0755, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to register MTD mtdblock0: %d\n", ret); - return ret; - } - -#ifdef CONFIG_MTD_PARTITION - ret = parse_mtd_partition(mtd, NULL, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to parse MTD partition: %d\n", ret); - return ret; - } -#endif /* CONFIG_MTD_PARTITION */ - #endif return ret; @@ -377,6 +442,14 @@ int board_spiflash_init(void) return ret; } +#ifdef CONFIG_ESP32S3_HAVE_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + return ret; + } +#endif + ret = init_storage_partition(); if (ret < 0) { @@ -385,4 +458,3 @@ int board_spiflash_init(void) return ret; } - diff --git a/boards/xtensa/esp32s3/esp32s3-devkit/configs/mcuboot_update_agent/defconfig b/boards/xtensa/esp32s3/esp32s3-devkit/configs/mcuboot_update_agent/defconfig new file mode 100644 index 0000000000000..27c99686b2777 --- /dev/null +++ b/boards/xtensa/esp32s3/esp32s3-devkit/configs/mcuboot_update_agent/defconfig @@ -0,0 +1,97 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_ARCH_LEDS is not set +# CONFIG_NDEBUG is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ALLOW_BSD_COMPONENTS=y +CONFIG_ARCH="xtensa" +CONFIG_ARCH_BOARD="esp32s3-devkit" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32S3_DEVKIT=y +CONFIG_ARCH_CHIP="esp32s3" +CONFIG_ARCH_CHIP_ESP32S3=y +CONFIG_ARCH_CHIP_ESP32S3WROOM1N4=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARCH_XTENSA=y +CONFIG_BOARDCTL_RESET=y +CONFIG_BOARD_LOOPSPERMSEC=16717 +CONFIG_BUILTIN=y +CONFIG_DEFAULT_TASK_STACKSIZE=4096 +CONFIG_DRIVERS_IEEE80211=y +CONFIG_DRIVERS_WIRELESS=y +CONFIG_ESP32S3_APP_FORMAT_MCUBOOT=y +CONFIG_ESP32S3_RT_TIMER_TASK_STACK_SIZE=4096 +CONFIG_ESP32S3_SPIFLASH=y +CONFIG_ESP32S3_SPIFLASH_SPIFFS=y +CONFIG_ESP32S3_STORAGE_MTD_OFFSET=0x180000 +CONFIG_ESP32S3_UART0=y +CONFIG_ESPRESSIF_WIFI=y +CONFIG_EXAMPLES_MCUBOOT_SLOT_CONFIRM=y +CONFIG_EXAMPLES_MCUBOOT_UPDATE_AGENT=y +CONFIG_EXAMPLES_RANDOM=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_IDLETHREAD_STACKSIZE=3072 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INIT_STACKSIZE=3072 +CONFIG_INTELHEX_BINARY=y +CONFIG_IOB_NBUFFERS=124 +CONFIG_IOB_THROTTLE=24 +CONFIG_LINE_MAX=64 +CONFIG_NAME_MAX=48 +CONFIG_NETDB_DNSCLIENT=y +CONFIG_NETDEV_LATEINIT=y +CONFIG_NETDEV_PHY_IOCTL=y +CONFIG_NETDEV_WIRELESS_IOCTL=y +CONFIG_NETUTILS_CJSON=y +CONFIG_NETUTILS_IPERF=y +CONFIG_NETUTILS_WEBCLIENT=y +CONFIG_NET_BROADCAST=y +CONFIG_NET_ETH_PKTSIZE=1514 +CONFIG_NET_ICMP_SOCKET=y +CONFIG_NET_TCP=y +CONFIG_NET_TCP_DELAYED_ACK=y +CONFIG_NET_TCP_WRITE_BUFFERS=y +CONFIG_NET_UDP=y +CONFIG_NET_UDP_WRITE_BUFFERS=y +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_MOTD=y +CONFIG_NSH_MOTD_STRING="This is MCUBoot Update Agent image" +CONFIG_NSH_READLINE=y +CONFIG_POSIX_SPAWN_DEFAULT_STACKSIZE=2048 +CONFIG_PREALLOC_TIMERS=4 +CONFIG_PTHREAD_MUTEX_TYPES=y +CONFIG_RAM_SIZE=114688 +CONFIG_RAM_START=0x20000000 +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SIG_DEFAULT=y +CONFIG_SMP=y +CONFIG_SMP_NCPUS=2 +CONFIG_SPIFFS_NAME_MAX=128 +CONFIG_START_DAY=6 +CONFIG_START_MONTH=12 +CONFIG_START_YEAR=2011 +CONFIG_SYSLOG_BUFFER=y +CONFIG_SYSTEM_DHCPC_RENEW=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_PING=y +CONFIG_TIMER=y +CONFIG_TLS_TASK_NELEM=4 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_WIRELESS=y +CONFIG_WIRELESS_WAPI=y +CONFIG_WIRELESS_WAPI_CMDTOOL=y +CONFIG_WIRELESS_WAPI_INITCONF=y +CONFIG_WIRELESS_WAPI_STACKSIZE=8192 From d66441ed1d9f9df43627eb9e7b0704bf33892212 Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Mon, 14 Jul 2025 09:04:33 -0300 Subject: [PATCH 2/5] arch/xtensa: add E-Fuse support on ESP32S2 Adds support for e-fuse on ESP32S2 and fixes a gitignore issue. Signed-off-by: Filipe Cavalcanti --- arch/xtensa/src/esp32/.gitignore | 1 - arch/xtensa/src/esp32/bootloader/.gitignore | 1 + arch/xtensa/src/esp32s2/hal.mk | 4 ++++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/arch/xtensa/src/esp32/.gitignore b/arch/xtensa/src/esp32/.gitignore index 94940fe623a2e..ab19fe9ea8149 100644 --- a/arch/xtensa/src/esp32/.gitignore +++ b/arch/xtensa/src/esp32/.gitignore @@ -1,4 +1,3 @@ /esp-hal-3rdparty -/esp-hal-3rdparty-mcuboot /esp-wireless-drivers-3rdparty /*.zip diff --git a/arch/xtensa/src/esp32/bootloader/.gitignore b/arch/xtensa/src/esp32/bootloader/.gitignore index 5e47dafb03817..bfbf2a3d49509 100644 --- a/arch/xtensa/src/esp32/bootloader/.gitignore +++ b/arch/xtensa/src/esp32/bootloader/.gitignore @@ -1,3 +1,4 @@ /esp-nuttx-bootloader +/esp-hal-3rdparty-mcuboot /bootloader.conf /mcuboot diff --git a/arch/xtensa/src/esp32s2/hal.mk b/arch/xtensa/src/esp32s2/hal.mk index 695b11e066d56..fa04f06a826e1 100644 --- a/arch/xtensa/src/esp32s2/hal.mk +++ b/arch/xtensa/src/esp32s2/hal.mk @@ -98,8 +98,12 @@ ARCHSCRIPT += $(ARCH_SRCDIR)$(DELIM)chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM) CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_adc$(DELIM)adc_cali.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_adc$(DELIM)$(CHIP_SERIES)$(DELIM)adc_cali_line_fitting.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)src$(DELIM)esp_efuse_api.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)src$(DELIM)esp_efuse_utility.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_utility.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_rtc_calib.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_rtc_table.c +CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)efuse$(DELIM)$(CHIP_SERIES)$(DELIM)esp_efuse_table.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_app_format$(DELIM)esp_app_desc.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_common$(DELIM)src$(DELIM)esp_err_to_name.c CHIP_CSRCS += chip$(DELIM)$(ESP_HAL_3RDPARTY_REPO)$(DELIM)components$(DELIM)esp_hw_support$(DELIM)mspi_timing_tuning$(DELIM)mspi_timing_tuning.c From 0a74b30d1f581315e4d5b8ad9be07f1e6f0d8c99 Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Fri, 11 Jul 2025 15:17:58 -0300 Subject: [PATCH 3/5] boards/risc-v: fixes SPI Flash for MCUBoot usage Adds partitions required for MCUBoot usage and a sample defconfig. Signed-off-by: Filipe Cavalcanti --- .../esp32c3/common/src/esp_board_spiflash.c | 101 +++++++++++++++--- .../configs/mcuboot_update_agent/defconfig | 83 ++++++++++++++ .../esp32c6/common/src/esp_board_spiflash.c | 51 ++++----- .../configs/mcuboot_update_agent/defconfig | 85 +++++++++++++++ .../esp32h2/common/src/esp_board_spiflash.c | 86 +++++++++++++-- 5 files changed, 349 insertions(+), 57 deletions(-) create mode 100644 boards/risc-v/esp32c3/esp32c3-generic/configs/mcuboot_update_agent/defconfig create mode 100644 boards/risc-v/esp32c6/esp32c6-devkitc/configs/mcuboot_update_agent/defconfig diff --git a/boards/risc-v/esp32c3/common/src/esp_board_spiflash.c b/boards/risc-v/esp32c3/common/src/esp_board_spiflash.c index 6c976f3bc5a3e..f19b0a952589b 100644 --- a/boards/risc-v/esp32c3/common/src/esp_board_spiflash.c +++ b/boards/risc-v/esp32c3/common/src/esp_board_spiflash.c @@ -43,9 +43,7 @@ #include #include #include -#ifdef CONFIG_BCH -#include -#endif +#include #include "espressif/esp_spiflash.h" #include "espressif/esp_spiflash_mtd.h" @@ -64,14 +62,82 @@ * Private Function Prototypes ****************************************************************************/ +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void); +#endif + /**************************************************************************** * Private Data ****************************************************************************/ +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static const struct partition_s g_ota_partition_table[] = +{ + { + .name = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESPRESSIF_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SCRATCH_SIZE, + } +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: init_ota_partitions + * + * Description: + * Initialize partitions that are dedicated to firmware OTA update. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void) +{ + struct mtd_dev_s *mtd; + int ret = OK; + int i; + + for (i = 0; i < nitems(g_ota_partition_table); ++i) + { + const struct partition_s *part = &g_ota_partition_table[i]; + mtd = esp_spiflash_alloc_mtdpart(part->firstblock, part->blocksize); + + ret = register_mtddriver(part->name, mtd, 0755, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n", + part->name, ret); + return ret; + } + } + + return ret; +} + +#endif + /**************************************************************************** * Name: setup_smartfs * @@ -125,7 +191,8 @@ static int setup_smartfs(int smartn, struct mtd_dev_s *mtd, ret = nx_mount(path, mnt_pt, "smartfs", 0, NULL); if (ret < 0) { - syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", ret); + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); if (ret == -ENODEV) { syslog(LOG_WARNING, "Smartfs seems unformatted. " @@ -224,7 +291,8 @@ static int setup_spiffs(const char *path, struct mtd_dev_s *mtd, ret = nx_mount(path, mnt_pt, "spiffs", 0, NULL); if (ret < 0) { - syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", ret); + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); return ret; } } @@ -265,7 +333,8 @@ static int setup_nxffs(struct mtd_dev_s *mtd, const char *mnt_pt) ret = nx_mount(NULL, mnt_pt, "nxffs", 0, NULL); if (ret < 0) { - syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", ret); + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); return ret; } } @@ -297,7 +366,7 @@ static int init_storage_partition(void) CONFIG_ESPRESSIF_STORAGE_MTD_SIZE); if (!mtd) { - syslog(LOG_ERR, "ERROR: Failed to alloc MTD partition of SPI Flash\n"); + syslog(LOG_ERR, "ERROR: Fail to alloc MTD partition of SPI Flash\n"); return ERROR; } @@ -333,7 +402,8 @@ static int init_storage_partition(void) #elif defined (CONFIG_ESPRESSIF_SPIFLASH_SPIFFS) const char *path = "/dev/espflash"; - ret = setup_spiffs(path, mtd, CONFIG_ESPRESSIF_SPIFLASH_FS_MOUNT_PT, 0755); + ret = setup_spiffs(path, mtd, CONFIG_ESPRESSIF_SPIFLASH_FS_MOUNT_PT, + 0755); if (ret < 0) { syslog(LOG_ERR, "ERROR: Failed to setup spiffs\n"); @@ -379,12 +449,6 @@ static int init_storage_partition(void) return ret; } - ret = register_mtddriver("/dev/mtdblock0", mtd, 0755, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to register MTD mtdblock0: %d\n", ret); - return ret; - } #endif return ret; @@ -415,6 +479,14 @@ int board_spiflash_init(void) esp_spiflash_init(); +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + return ret; + } +#endif + ret = init_storage_partition(); if (ret < 0) { @@ -423,4 +495,3 @@ int board_spiflash_init(void) return ret; } - diff --git a/boards/risc-v/esp32c3/esp32c3-generic/configs/mcuboot_update_agent/defconfig b/boards/risc-v/esp32c3/esp32c3-generic/configs/mcuboot_update_agent/defconfig new file mode 100644 index 0000000000000..77fc038e37a70 --- /dev/null +++ b/boards/risc-v/esp32c3/esp32c3-generic/configs/mcuboot_update_agent/defconfig @@ -0,0 +1,83 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_NDEBUG is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ALLOW_BSD_COMPONENTS=y +CONFIG_ARCH="risc-v" +CONFIG_ARCH_BOARD="esp32c3-generic" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32C3_GENERIC=y +CONFIG_ARCH_CHIP="esp32c3" +CONFIG_ARCH_CHIP_ESP32C3_GENERIC=y +CONFIG_ARCH_INTERRUPTSTACK=1536 +CONFIG_ARCH_RISCV=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_RESET=y +CONFIG_BOARD_LOOPSPERMSEC=15000 +CONFIG_BUILTIN=y +CONFIG_DRIVERS_IEEE80211=y +CONFIG_DRIVERS_WIRELESS=y +CONFIG_ESPRESSIF_BOOTLOADER_MCUBOOT=y +CONFIG_ESPRESSIF_SPIFLASH=y +CONFIG_ESPRESSIF_SPIFLASH_SPIFFS=y +CONFIG_ESPRESSIF_WIFI=y +CONFIG_EXAMPLES_MCUBOOT_SLOT_CONFIRM=y +CONFIG_EXAMPLES_MCUBOOT_UPDATE_AGENT=y +CONFIG_EXAMPLES_RANDOM=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INIT_STACKSIZE=8192 +CONFIG_INTELHEX_BINARY=y +CONFIG_IOB_THROTTLE=24 +CONFIG_LIBC_PERROR_STDOUT=y +CONFIG_LIBC_STRERROR=y +CONFIG_NETDB_DNSCLIENT=y +CONFIG_NETDEV_LATEINIT=y +CONFIG_NETDEV_PHY_IOCTL=y +CONFIG_NETDEV_WIRELESS_IOCTL=y +CONFIG_NETDEV_WORK_THREAD=y +CONFIG_NETUTILS_CJSON=y +CONFIG_NETUTILS_WEBCLIENT=y +CONFIG_NET_BROADCAST=y +CONFIG_NET_ICMP_SOCKET=y +CONFIG_NET_TCP=y +CONFIG_NET_TCP_DELAYED_ACK=y +CONFIG_NET_TCP_WRITE_BUFFERS=y +CONFIG_NET_UDP=y +CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_MOTD=y +CONFIG_NSH_MOTD_STRING="This is MCUBoot Update Agent image" +CONFIG_NSH_READLINE=y +CONFIG_NSH_STRERROR=y +CONFIG_PREALLOC_TIMERS=0 +CONFIG_PTHREAD_MUTEX_TYPES=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_BACKTRACE=y +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SIG_DEFAULT=y +CONFIG_START_DAY=29 +CONFIG_START_MONTH=11 +CONFIG_START_YEAR=2019 +CONFIG_SYSTEM_DHCPC_RENEW=y +CONFIG_SYSTEM_DUMPSTACK=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_PING=y +CONFIG_TESTING_GETPRIME=y +CONFIG_TESTING_OSTEST=y +CONFIG_TLS_TASK_NELEM=4 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_WIRELESS=y +CONFIG_WIRELESS_WAPI=y +CONFIG_WIRELESS_WAPI_CMDTOOL=y +CONFIG_WIRELESS_WAPI_INITCONF=y diff --git a/boards/risc-v/esp32c6/common/src/esp_board_spiflash.c b/boards/risc-v/esp32c6/common/src/esp_board_spiflash.c index 7f81068c1422a..393dcd29e7a2f 100644 --- a/boards/risc-v/esp32c6/common/src/esp_board_spiflash.c +++ b/boards/risc-v/esp32c6/common/src/esp_board_spiflash.c @@ -43,9 +43,7 @@ #include #include #include -#ifdef CONFIG_BCH -#include -#endif +#include #include "espressif/esp_spiflash.h" #include "espressif/esp_spiflash_mtd.h" @@ -60,13 +58,6 @@ * Private Types ****************************************************************************/ -struct ota_partition_s -{ - uint32_t offset; /* Partition offset from the beginning of MTD */ - uint32_t size; /* Partition size in bytes */ - const char *devpath; /* Partition device path */ -}; - /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -80,22 +71,25 @@ static int init_ota_partitions(void); ****************************************************************************/ #ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION -static const struct ota_partition_s g_ota_partition_table[] = +static const struct partition_s g_ota_partition_table[] = { { - .offset = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET, - .size = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, - .devpath = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_DEVPATH + .name = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, }, { - .offset = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET, - .size = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, - .devpath = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_DEVPATH + .name = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, }, { - .offset = CONFIG_ESPRESSIF_OTA_SCRATCH_OFFSET, - .size = CONFIG_ESPRESSIF_OTA_SCRATCH_SIZE, - .devpath = CONFIG_ESPRESSIF_OTA_SCRATCH_DEVPATH + .name = CONFIG_ESPRESSIF_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESPRESSIF_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SCRATCH_SIZE, } }; #endif @@ -127,14 +121,14 @@ static int init_ota_partitions(void) for (i = 0; i < nitems(g_ota_partition_table); ++i) { - const struct ota_partition_s *part = &g_ota_partition_table[i]; - mtd = esp_spiflash_alloc_mtdpart(part->offset, part->size); + const struct partition_s *part = &g_ota_partition_table[i]; + mtd = esp_spiflash_alloc_mtdpart(part->firstblock, part->blocksize); - ret = register_mtddriver(part->devpath, mtd, 0755, NULL); + ret = register_mtddriver(part->name, mtd, 0755, NULL); if (ret < 0) { - ferr("ERROR: register_mtddriver %s failed: %d\n", - part->devpath, ret); + syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n", + part->name, ret); return ret; } } @@ -451,12 +445,6 @@ static int init_storage_partition(void) return ret; } - ret = register_mtddriver("/dev/mtdblock0", mtd, 0755, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to register MTD mtdblock0: %d\n", ret); - return ret; - } #endif return ret; @@ -503,4 +491,3 @@ int board_spiflash_init(void) return ret; } - diff --git a/boards/risc-v/esp32c6/esp32c6-devkitc/configs/mcuboot_update_agent/defconfig b/boards/risc-v/esp32c6/esp32c6-devkitc/configs/mcuboot_update_agent/defconfig new file mode 100644 index 0000000000000..1d8748e47d7f3 --- /dev/null +++ b/boards/risc-v/esp32c6/esp32c6-devkitc/configs/mcuboot_update_agent/defconfig @@ -0,0 +1,85 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_NDEBUG is not set +# CONFIG_NSH_ARGCAT is not set +# CONFIG_NSH_CMDOPT_HEXDUMP is not set +CONFIG_ALLOW_BSD_COMPONENTS=y +CONFIG_ARCH="risc-v" +CONFIG_ARCH_BOARD="esp32c6-devkitc" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_ESP32C6_DEVKITC=y +CONFIG_ARCH_CHIP="esp32c6" +CONFIG_ARCH_CHIP_ESP32C6=y +CONFIG_ARCH_CHIP_ESP32C6WROOM1=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_RISCV=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_BOARDCTL_RESET=y +CONFIG_BOARD_LOOPSPERMSEC=15000 +CONFIG_BUILTIN=y +CONFIG_DRIVERS_IEEE80211=y +CONFIG_DRIVERS_WIRELESS=y +CONFIG_ESPRESSIF_BOOTLOADER_MCUBOOT=y +CONFIG_ESPRESSIF_ESP32C6=y +CONFIG_ESPRESSIF_SPIFLASH=y +CONFIG_ESPRESSIF_SPIFLASH_SPIFFS=y +CONFIG_ESPRESSIF_WIFI=y +CONFIG_EXAMPLES_MCUBOOT_SLOT_CONFIRM=y +CONFIG_EXAMPLES_MCUBOOT_UPDATE_AGENT=y +CONFIG_EXAMPLES_RANDOM=y +CONFIG_FS_PROCFS=y +CONFIG_IDLETHREAD_STACKSIZE=2048 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INIT_STACKSIZE=8192 +CONFIG_INTELHEX_BINARY=y +CONFIG_IOB_THROTTLE=24 +CONFIG_LIBC_PERROR_STDOUT=y +CONFIG_LIBC_STRERROR=y +CONFIG_NETDB_DNSCLIENT=y +CONFIG_NETDEV_LATEINIT=y +CONFIG_NETDEV_PHY_IOCTL=y +CONFIG_NETDEV_WIRELESS_IOCTL=y +CONFIG_NETDEV_WORK_THREAD=y +CONFIG_NETUTILS_CJSON=y +CONFIG_NETUTILS_WEBCLIENT=y +CONFIG_NET_BROADCAST=y +CONFIG_NET_ICMP_SOCKET=y +CONFIG_NET_TCP=y +CONFIG_NET_TCP_DELAYED_ACK=y +CONFIG_NET_TCP_WRITE_BUFFERS=y +CONFIG_NET_UDP=y +CONFIG_NFILE_DESCRIPTORS_PER_BLOCK=6 +CONFIG_NSH_ARCHINIT=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_MOTD=y +CONFIG_NSH_MOTD_STRING="This is MCUBoot Update Agent image" +CONFIG_NSH_READLINE=y +CONFIG_NSH_STRERROR=y +CONFIG_PREALLOC_TIMERS=0 +CONFIG_PTHREAD_MUTEX_TYPES=y +CONFIG_RR_INTERVAL=200 +CONFIG_SCHED_BACKTRACE=y +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SIG_DEFAULT=y +CONFIG_START_DAY=29 +CONFIG_START_MONTH=11 +CONFIG_START_YEAR=2019 +CONFIG_SYSTEM_DHCPC_RENEW=y +CONFIG_SYSTEM_DUMPSTACK=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_PING=y +CONFIG_TESTING_GETPRIME=y +CONFIG_TESTING_OSTEST=y +CONFIG_TLS_TASK_NELEM=4 +CONFIG_UART0_SERIAL_CONSOLE=y +CONFIG_WIRELESS=y +CONFIG_WIRELESS_WAPI=y +CONFIG_WIRELESS_WAPI_CMDTOOL=y +CONFIG_WIRELESS_WAPI_INITCONF=y diff --git a/boards/risc-v/esp32h2/common/src/esp_board_spiflash.c b/boards/risc-v/esp32h2/common/src/esp_board_spiflash.c index 4395b74dde7cf..40f5567e7adf5 100644 --- a/boards/risc-v/esp32h2/common/src/esp_board_spiflash.c +++ b/boards/risc-v/esp32h2/common/src/esp_board_spiflash.c @@ -43,9 +43,7 @@ #include #include #include -#ifdef CONFIG_BCH -#include -#endif +#include #include "espressif/esp_spiflash.h" #include "espressif/esp_spiflash_mtd.h" @@ -64,14 +62,81 @@ * Private Function Prototypes ****************************************************************************/ +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void); +#endif + /**************************************************************************** * Private Data ****************************************************************************/ +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static const struct partition_s g_ota_partition_table[] = +{ + { + .name = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESPRESSIF_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SCRATCH_SIZE, + } +}; +#endif + /**************************************************************************** * Private Functions ****************************************************************************/ +/**************************************************************************** + * Name: init_ota_partitions + * + * Description: + * Initialize partitions that are dedicated to firmware OTA update. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void) +{ + struct mtd_dev_s *mtd; + int ret = OK; + int i; + + for (i = 0; i < nitems(g_ota_partition_table); ++i) + { + const struct partition_s *part = &g_ota_partition_table[i]; + mtd = esp_spiflash_alloc_mtdpart(part->firstblock, part->blocksize); + + ret = register_mtddriver(part->name, mtd, 0755, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n", + part->name, ret); + return ret; + } + } + + return ret; +} +#endif + /**************************************************************************** * Name: setup_smartfs * @@ -379,12 +444,6 @@ static int init_storage_partition(void) return ret; } - ret = register_mtddriver("/dev/mtdblock0", mtd, 0755, NULL); - if (ret < 0) - { - syslog(LOG_ERR, "ERROR: Failed to register MTD mtdblock0: %d\n", ret); - return ret; - } #endif return ret; @@ -415,6 +474,14 @@ int board_spiflash_init(void) esp_spiflash_init(); +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + return ret; + } +#endif + ret = init_storage_partition(); if (ret < 0) { @@ -423,4 +490,3 @@ int board_spiflash_init(void) return ret; } - From c1e5e520b24efdfdd2af526659beb8d5abc005df Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Tue, 15 Jul 2025 11:28:40 -0300 Subject: [PATCH 4/5] arch/risc-v: change offset for SPI Flash on Espressif devices Signed-off-by: Filipe Cavalcanti --- arch/risc-v/src/common/espressif/Kconfig | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/risc-v/src/common/espressif/Kconfig b/arch/risc-v/src/common/espressif/Kconfig index 2c5316f975cb2..41e4051c9c2f6 100644 --- a/arch/risc-v/src/common/espressif/Kconfig +++ b/arch/risc-v/src/common/espressif/Kconfig @@ -1736,7 +1736,8 @@ config ESPRESSIF_SPI_FLASH_USE_32BIT_ADDRESS config ESPRESSIF_STORAGE_MTD_OFFSET hex "Storage MTD base address in SPI Flash" - default 0x180000 + default 0x180000 if !ESPRESSIF_BOOTLOADER_MCUBOOT + default 0x250000 if ESPRESSIF_BOOTLOADER_MCUBOOT depends on ESPRESSIF_MTD ---help--- MTD base address in SPI Flash. From 463506dcae8b0df3ef1967a54807cd6ab35015d6 Mon Sep 17 00:00:00 2001 From: Filipe Cavalcanti Date: Tue, 15 Jul 2025 11:46:53 -0300 Subject: [PATCH 5/5] documentation: update documentation for MCUBoot on Espressif devices Updates MCUBoot documentation and usage example. Signed-off-by: Filipe Cavalcanti --- .../esp32c3/boards/esp32c3-generic/index.rst | 15 +++ .../platforms/risc-v/esp32c3/index.rst | 99 ++++++++++++++++++- .../esp32c6/boards/esp32c6-devkitc/index.rst | 10 +- .../platforms/risc-v/esp32c6/index.rst | 89 +++++++++++++++++ .../esp32/boards/esp32-devkitc/index.rst | 32 ++---- .../platforms/xtensa/esp32/index.rst | 99 ++++++++++++++++++- .../esp32s2/boards/esp32s2-saola-1/index.rst | 8 ++ .../platforms/xtensa/esp32s2/index.rst | 99 ++++++++++++++++++- .../esp32s3/boards/esp32s3-devkit/index.rst | 8 ++ .../platforms/xtensa/esp32s3/index.rst | 91 ++++++++++++++++- 10 files changed, 510 insertions(+), 40 deletions(-) diff --git a/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst b/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst index cf6b3fa29c7ae..bc10b6112e812 100644 --- a/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst +++ b/Documentation/platforms/risc-v/esp32c3/boards/esp32c3-generic/index.rst @@ -322,6 +322,21 @@ After successfully built and flashed, run on the boards's terminal:: nsh> i2schar +mcuboot_nsh +----------- + +This configuration is the same as the ``nsh`` configuration, but it generates the application +image in a format that can be used by MCUboot. It also makes the ``make bootloader`` command to +build the MCUboot bootloader image using the Espressif HAL. + +mcuboot_update_agent +-------------------- + +This configuration is used to represent an MCUboot image that contains an update agent +to perform over-the-air (OTA) updates. Wi-Fi settings are already enabled and image confirmation program is included. + +Follow the instructions in the :ref:`MCUBoot and OTA Update ` section to execute OTA update. + nimble ------ diff --git a/Documentation/platforms/risc-v/esp32c3/index.rst b/Documentation/platforms/risc-v/esp32c3/index.rst index 208996149a799..9f25e4049b3db 100644 --- a/Documentation/platforms/risc-v/esp32c3/index.rst +++ b/Documentation/platforms/risc-v/esp32c3/index.rst @@ -408,11 +408,100 @@ Then, it can be customized in the menu :menuselection:`System Type --> ADC Confi .. warning:: Maximum measurable voltage may saturate around 2900 mV. +.. _MCUBoot and OTA Update C3: + +MCUBoot and OTA Update +====================== + +The ESP32-C3 supports over-the-air (OTA) updates using MCUBoot. + +Read more about the MCUBoot for Espressif devices `here `__. + +Executing OTA Update +-------------------- + +This section describes how to execute OTA update using MCUBoot. + +1. First build the default ``mcuboot_update_agent`` config. This image defaults to the primary slot and already comes with Wi-Fi settings enabled:: + + ./tools/configure.sh esp32c3-generic:mcuboot_update_agent + +2. Build the MCUBoot bootloader:: + + make bootloader + +3. Finally, build the application image:: + + make + +Flash the image to the board and verify it boots ok. +It should show the message "This is MCUBoot Update Agent image" before NuttShell is ready. + +At this point, the board should be able to connect to Wi-Fi so we can download a new binary from our network:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot Update Agent image + nsh> + nsh> wapi psk wlan0 3 + nsh> wapi essid wlan0 1 + nsh> renew wlan0 + +Now, keep the board as is and execute the following commands to **change the MCUBoot target slot to the 2nd slot** +and modify the message of the day (MOTD) as a mean to verify the new image is being used. + +1. Change the MCUBoot target slot to the 2nd slot:: + + kconfig-tweak -d CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY + kconfig-tweak -e CONFIG_ESPRESSIF_ESPTOOL_TARGET_SECONDARY + kconfig-tweak --set-str CONFIG_NSH_MOTD_STRING "This is MCUBoot UPDATED image!" + make olddefconfig + + .. note:: + The same changes can be accomplished through ``menuconfig`` in :menuselection:`System Type --> Bootloader and Image Configuration --> Target slot for image flashing` + for MCUBoot target slot and in :menuselection:`System Type --> Bootloader and Image Configuration --> Search (motd) --> NSH Library --> Message of the Day` for the MOTD. + +2. Rebuild the application image:: + + make + +At this point the board is already connected to Wi-Fi and has the primary image flashed. +The new image configured for the 2nd slot is ready to be downloaded. + +To execute OTA, create a simple HTTP server on the NuttX directory so we can access the binary remotely:: + + cd nuttxspace/nuttx + python3 -m http.server + Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... + +On the board, execute the update agent, setting the IP address to the one on the host machine. Wait until image is transferred and the board should reboot automatically:: + + nsh> mcuboot_agent http://10.42.0.1:8000/nuttx.bin + MCUboot Update Agent example + Downloading from http://10.42.0.1:8000/nuttx.bin + Firmware Update size: 1048576 bytes + Received: 512 of 1048576 bytes [0%] + Received: 1024 of 1048576 bytes [0%] + Received: 1536 of 1048576 bytes [0%] + [.....] + Received: 1048576 of 1048576 bytes [100%] + Application Image successfully downloaded! + Requested update for next boot. Restarting... + +NuttShell should now show the new MOTD, meaning the new image is being used:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot UPDATED image! + nsh> + +Finally, the image is loaded but not confirmed. +To make sure it won't rollback to the previous image, you must confirm with ``mcuboot_confirm`` and reboot the board. +The OTA is now complete. + Secure Boot and Flash Encryption -================================ +-------------------------------- Secure Boot ------------ +^^^^^^^^^^^ Secure Boot protects a device from running any unauthorized (i.e., unsigned) code by checking that each piece of software that is being booted is signed. On an ESP32-C3, these pieces of software include @@ -440,7 +529,7 @@ The Secure Boot process on the ESP32-C3 involves the following steps performed: by MCUboot rather than the original NuttX port. Flash Encryption ----------------- +^^^^^^^^^^^^^^^^ Flash encryption is intended for encrypting the contents of the ESP32-C3's off-chip flash memory. Once this feature is enabled, firmware is flashed as plaintext, and then the data is encrypted in place on the first boot. As a result, physical readout @@ -452,7 +541,7 @@ of flash will not be sufficient to recover most flash contents. `here `__. Prerequisites -------------- +^^^^^^^^^^^^^ First of all, we need to install ``imgtool`` (the MCUboot utility application to manipulate binary images):: @@ -476,7 +565,7 @@ respectively, of the compiled project:: .. important:: The contents of the key files must be stored securely and kept secret. Enabling Secure Boot and Flash Encryption ------------------------------------------ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To enable Secure Boot for the current project, go to the project's NuttX directory, execute ``make menuconfig`` and the following steps: diff --git a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst index 26f99b53c989a..4d458ccf69ff5 100644 --- a/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst +++ b/Documentation/platforms/risc-v/esp32c6/boards/esp32c6-devkitc/index.rst @@ -323,12 +323,20 @@ There is also support for an optional fault GPIO (defaults to GPIO9), which can for quick motor braking. All GPIOs are configurable in ``menuconfig``. mcuboot_nsh --------------------- +----------- This configuration is the same as the ``nsh`` configuration, but it generates the application image in a format that can be used by MCUboot. It also makes the ``make bootloader`` command to build the MCUboot bootloader image using the Espressif HAL. +mcuboot_update_agent +-------------------- + +This configuration is used to represent an MCUboot image that contains an update agent +to perform over-the-air (OTA) updates. Wi-Fi settings are already enabled and image confirmation program is included. + +Follow the instructions in the :ref:`MCUBoot and OTA Update ` section to execute OTA update. + nsh --- diff --git a/Documentation/platforms/risc-v/esp32c6/index.rst b/Documentation/platforms/risc-v/esp32c6/index.rst index 092ed3da257c6..036a4ec56f2af 100644 --- a/Documentation/platforms/risc-v/esp32c6/index.rst +++ b/Documentation/platforms/risc-v/esp32c6/index.rst @@ -397,6 +397,95 @@ Then, it can be customized in the menu :menuselection:`System Type --> ADC Confi 6 6 ========== =========== +.. _MCUBoot and OTA Update C6: + +MCUBoot and OTA Update +====================== + +The ESP32-C6 supports over-the-air (OTA) updates using MCUBoot. + +Read more about the MCUBoot for Espressif devices `here `__. + +Executing OTA Update +-------------------- + +This section describes how to execute OTA update using MCUBoot. + +1. First build the default ``mcuboot_update_agent`` config. This image defaults to the primary slot and already comes with Wi-Fi settings enabled:: + + ./tools/configure.sh esp32c6-devkitc:mcuboot_update_agent + +2. Build the MCUBoot bootloader:: + + make bootloader + +3. Finally, build the application image:: + + make + +Flash the image to the board and verify it boots ok. +It should show the message "This is MCUBoot Update Agent image" before NuttShell is ready. + +At this point, the board should be able to connect to Wi-Fi so we can download a new binary from our network:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot Update Agent image + nsh> + nsh> wapi psk wlan0 3 + nsh> wapi essid wlan0 1 + nsh> renew wlan0 + +Now, keep the board as is and execute the following commands to **change the MCUBoot target slot to the 2nd slot** +and modify the message of the day (MOTD) as a mean to verify the new image is being used. + +1. Change the MCUBoot target slot to the 2nd slot:: + + kconfig-tweak -d CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY + kconfig-tweak -e CONFIG_ESPRESSIF_ESPTOOL_TARGET_SECONDARY + kconfig-tweak --set-str CONFIG_NSH_MOTD_STRING "This is MCUBoot UPDATED image!" + make olddefconfig + + .. note:: + The same changes can be accomplished through ``menuconfig`` in :menuselection:`System Type --> Bootloader and Image Configuration --> Target slot for image flashing` + for MCUBoot target slot and in :menuselection:`System Type --> Bootloader and Image Configuration --> Search (motd) --> NSH Library --> Message of the Day` for the MOTD. + +2. Rebuild the application image:: + + make + +At this point the board is already connected to Wi-Fi and has the primary image flashed. +The new image configured for the 2nd slot is ready to be downloaded. + +To execute OTA, create a simple HTTP server on the NuttX directory so we can access the binary remotely:: + + cd nuttxspace/nuttx + python3 -m http.server + Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... + +On the board, execute the update agent, setting the IP address to the one on the host machine. Wait until image is transferred and the board should reboot automatically:: + + nsh> mcuboot_agent http://10.42.0.1:8000/nuttx.bin + MCUboot Update Agent example + Downloading from http://10.42.0.1:8000/nuttx.bin + Firmware Update size: 1048576 bytes + Received: 512 of 1048576 bytes [0%] + Received: 1024 of 1048576 bytes [0%] + Received: 1536 of 1048576 bytes [0%] + [.....] + Received: 1048576 of 1048576 bytes [100%] + Application Image successfully downloaded! + Requested update for next boot. Restarting... + +NuttShell should now show the new MOTD, meaning the new image is being used:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot UPDATED image! + nsh> + +Finally, the image is loaded but not confirmed. +To make sure it won't rollback to the previous image, you must confirm with ``mcuboot_confirm`` and reboot the board. +The OTA is now complete. + _`Managing esptool on virtual environment` ========================================== diff --git a/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst b/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst index d8515fd12356a..6aa571a457561 100644 --- a/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst +++ b/Documentation/platforms/xtensa/esp32/boards/esp32-devkitc/index.rst @@ -649,12 +649,20 @@ The MCP2515 interrupt (INT) pin is connected to the pin 22 of the ESP32-Devkit. mcuboot_nsh --------------------- +----------- This configuration is the same as the ``nsh`` configuration, but it generates the application image in a format that can be used by MCUboot. It also makes the ``make bootloader`` command to build the MCUboot bootloader image using the Espressif HAL. +mcuboot_update_agent +-------------------- + +This configuration is used to represent an MCUboot image that contains an update agent +to perform over-the-air (OTA) updates. Wi-Fi settings are already enabled and image confirmation program is included. + +Follow the instructions in the :ref:`MCUBoot and OTA Update ` section to execute OTA update. + mcuboot_slot_confirm -------------------- @@ -666,28 +674,6 @@ after flashing. The image can be confirmed by using the following command:: For more information, check `this demo `_. -mcuboot_update_agent --------------------- - -This configuration is used to represent an MCUboot image that contains an update agent -to perform OTA updates. First, you will have to setup a HTTP server to provide the update -image. To do that, we can run a simple Python server on the same folder that contains our -binary file on the computer:: - - sudo python -m http.server 8080 - -After this, we can use NSH to connect to our network and use the agent to perform the firmware -update:: - - nsh> ifup wlan0 - nsh> wapi mode wlan0 2 - nsh> wapi psk wlan0 mypasswd 3 - nsh> wapi essid wlan0 myssid 1 - nsh> renew wlan0 - nsh> mcuboot_agent http://:8080/nuttx.bin - -For more information, check `this demo `_. - modbus ------ diff --git a/Documentation/platforms/xtensa/esp32/index.rst b/Documentation/platforms/xtensa/esp32/index.rst index 6a1e8c9aff17d..656dc2cfd59c4 100644 --- a/Documentation/platforms/xtensa/esp32/index.rst +++ b/Documentation/platforms/xtensa/esp32/index.rst @@ -752,11 +752,100 @@ Networking is possible using the openeth MAC driver. Enable ``ESP32_OPENETH`` op $ qemu-system-xtensa -nographic -machine esp32 -drive file=nuttx.merged.bin,if=mtd,format=raw -nic user,model=open_eth +.. _MCUBoot and OTA Update ESP32: + +MCUBoot and OTA Update +====================== + +The ESP32 supports over-the-air (OTA) updates using MCUBoot. + +Read more about the MCUBoot for Espressif devices `here `__. + +Executing OTA Update +-------------------- + +This section describes how to execute OTA update using MCUBoot. + +1. First build the default ``mcuboot_update_agent`` config. This image defaults to the primary slot and already comes with Wi-Fi settings enabled:: + + ./tools/configure.sh esp32-devkitc:mcuboot_update_agent + +2. Build the MCUBoot bootloader:: + + make bootloader + +3. Finally, build the application image:: + + make + +Flash the image to the board and verify it boots ok. +It should show the message "This is MCUBoot Update Agent image" before NuttShell is ready. + +At this point, the board should be able to connect to Wi-Fi so we can download a new binary from our network:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot Update Agent image + nsh> + nsh> wapi psk wlan0 3 + nsh> wapi essid wlan0 1 + nsh> renew wlan0 + +Now, keep the board as is and execute the following commands to **change the MCUBoot target slot to the 2nd slot** +and modify the message of the day (MOTD) as a mean to verify the new image is being used. + +1. Change the MCUBoot target slot to the 2nd slot:: + + kconfig-tweak -d CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY + kconfig-tweak -e CONFIG_ESPRESSIF_ESPTOOL_TARGET_SECONDARY + kconfig-tweak --set-str CONFIG_NSH_MOTD_STRING "This is MCUBoot UPDATED image!" + make olddefconfig + + .. note:: + The same changes can be accomplished through ``menuconfig`` in :menuselection:`System Type --> Bootloader and Image Configuration --> Target slot for image flashing` + for MCUBoot target slot and in :menuselection:`System Type --> Bootloader and Image Configuration --> Search (motd) --> NSH Library --> Message of the Day` for the MOTD. + +2. Rebuild the application image:: + + make + +At this point the board is already connected to Wi-Fi and has the primary image flashed. +The new image configured for the 2nd slot is ready to be downloaded. + +To execute OTA, create a simple HTTP server on the NuttX directory so we can access the binary remotely:: + + cd nuttxspace/nuttx + python3 -m http.server + Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... + +On the board, execute the update agent, setting the IP address to the one on the host machine. Wait until image is transferred and the board should reboot automatically:: + + nsh> mcuboot_agent http://10.42.0.1:8000/nuttx.bin + MCUboot Update Agent example + Downloading from http://10.42.0.1:8000/nuttx.bin + Firmware Update size: 1048576 bytes + Received: 512 of 1048576 bytes [0%] + Received: 1024 of 1048576 bytes [0%] + Received: 1536 of 1048576 bytes [0%] + [.....] + Received: 1048576 of 1048576 bytes [100%] + Application Image successfully downloaded! + Requested update for next boot. Restarting... + +NuttShell should now show the new MOTD, meaning the new image is being used:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot UPDATED image! + nsh> + +Finally, the image is loaded but not confirmed. +To make sure it won't rollback to the previous image, you must confirm with ``mcuboot_confirm`` and reboot the board. +The OTA is now complete. + Secure Boot and Flash Encryption -================================ +-------------------------------- Secure Boot ------------ +^^^^^^^^^^^ Secure Boot protects a device from running any unauthorized (i.e., unsigned) code by checking that each piece of software that is being booted is signed. On an ESP32, these pieces of software include @@ -784,7 +873,7 @@ The Secure Boot process on the ESP32 involves the following steps performed: by MCUboot rather than the original NuttX port. Flash Encryption ----------------- +^^^^^^^^^^^^^^^^ Flash encryption is intended for encrypting the contents of the ESP32's off-chip flash memory. Once this feature is enabled, firmware is flashed as plaintext, and then the data is encrypted in place on the first boot. As a result, physical readout @@ -796,7 +885,7 @@ of flash will not be sufficient to recover most flash contents. `here `__. Prerequisites -------------- +^^^^^^^^^^^^^ First of all, we need to install ``imgtool`` (a MCUboot utility application to manipulate binary images) and ``esptool`` (the ESP32 toolkit):: @@ -820,7 +909,7 @@ respectively, of the compiled project:: .. important:: The contents of the key files must be stored securely and kept secret. Enabling Secure Boot and Flash Encryption ------------------------------------------ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To enable Secure Boot for the current project, go to the project's NuttX directory, execute ``make menuconfig`` and the following steps: diff --git a/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst b/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst index 15d6679c86a56..2901da7ee6996 100644 --- a/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst +++ b/Documentation/platforms/xtensa/esp32s2/boards/esp32s2-saola-1/index.rst @@ -334,6 +334,14 @@ This configuration is the same as the ``nsh`` configuration, but it generates th image in a format that can be used by MCUboot. It also makes the ``make bootloader`` command to build the MCUboot bootloader image using the Espressif HAL. +mcuboot_update_agent +-------------------- + +This configuration is used to represent an MCUboot image that contains an update agent +to perform over-the-air (OTA) updates. Wi-Fi settings are already enabled and image confirmation program is included. + +Follow the instructions in the :ref:`MCUBoot and OTA Update ` section to execute OTA update. + nsh --- diff --git a/Documentation/platforms/xtensa/esp32s2/index.rst b/Documentation/platforms/xtensa/esp32s2/index.rst index 82b68012cef9f..f0394902fa2a2 100644 --- a/Documentation/platforms/xtensa/esp32s2/index.rst +++ b/Documentation/platforms/xtensa/esp32s2/index.rst @@ -625,11 +625,100 @@ using WPA2. The ``dhcpd_start`` is necessary to let your board to associate an IP to your smartphone. +.. _MCUBoot and OTA Update S2: + +MCUBoot and OTA Update +====================== + +The ESP32-S2 supports over-the-air (OTA) updates using MCUBoot. + +Read more about the MCUBoot for Espressif devices `here `__. + +Executing OTA Update +-------------------- + +This section describes how to execute OTA update using MCUBoot. + +1. First build the default ``mcuboot_update_agent`` config. This image defaults to the primary slot and already comes with Wi-Fi settings enabled:: + + ./tools/configure.sh esp32s2-saola-1:mcuboot_update_agent + +2. Build the MCUBoot bootloader:: + + make bootloader + +3. Finally, build the application image:: + + make + +Flash the image to the board and verify it boots ok. +It should show the message "This is MCUBoot Update Agent image" before NuttShell is ready. + +At this point, the board should be able to connect to Wi-Fi so we can download a new binary from our network:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot Update Agent image + nsh> + nsh> wapi psk wlan0 3 + nsh> wapi essid wlan0 1 + nsh> renew wlan0 + +Now, keep the board as is and execute the following commands to **change the MCUBoot target slot to the 2nd slot** +and modify the message of the day (MOTD) as a mean to verify the new image is being used. + +1. Change the MCUBoot target slot to the 2nd slot:: + + kconfig-tweak -d CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY + kconfig-tweak -e CONFIG_ESPRESSIF_ESPTOOL_TARGET_SECONDARY + kconfig-tweak --set-str CONFIG_NSH_MOTD_STRING "This is MCUBoot UPDATED image!" + make olddefconfig + + .. note:: + The same changes can be accomplished through ``menuconfig`` in :menuselection:`System Type --> Bootloader and Image Configuration --> Target slot for image flashing` + for MCUBoot target slot and in :menuselection:`System Type --> Bootloader and Image Configuration --> Search (motd) --> NSH Library --> Message of the Day` for the MOTD. + +2. Rebuild the application image:: + + make + +At this point the board is already connected to Wi-Fi and has the primary image flashed. +The new image configured for the 2nd slot is ready to be downloaded. + +To execute OTA, create a simple HTTP server on the NuttX directory so we can access the binary remotely:: + + cd nuttxspace/nuttx + python3 -m http.server + Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... + +On the board, execute the update agent, setting the IP address to the one on the host machine. Wait until image is transferred and the board should reboot automatically:: + + nsh> mcuboot_agent http://10.42.0.1:8000/nuttx.bin + MCUboot Update Agent example + Downloading from http://10.42.0.1:8000/nuttx.bin + Firmware Update size: 1048576 bytes + Received: 512 of 1048576 bytes [0%] + Received: 1024 of 1048576 bytes [0%] + Received: 1536 of 1048576 bytes [0%] + [.....] + Received: 1048576 of 1048576 bytes [100%] + Application Image successfully downloaded! + Requested update for next boot. Restarting... + +NuttShell should now show the new MOTD, meaning the new image is being used:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot UPDATED image! + nsh> + +Finally, the image is loaded but not confirmed. +To make sure it won't rollback to the previous image, you must confirm with ``mcuboot_confirm`` and reboot the board. +The OTA is now complete. + Secure Boot and Flash Encryption -================================ +-------------------------------- Secure Boot ------------ +^^^^^^^^^^^ Secure Boot protects a device from running any unauthorized (i.e., unsigned) code by checking that each piece of software that is being booted is signed. On an ESP32-S2, these pieces of software include @@ -657,7 +746,7 @@ The Secure Boot process on the ESP32-S2 involves the following steps performed: by MCUboot rather than the original NuttX port. Flash Encryption ----------------- +^^^^^^^^^^^^^^^^ Flash encryption is intended for encrypting the contents of the ESP32-S2's off-chip flash memory. Once this feature is enabled, firmware is flashed as plaintext, and then the data is encrypted in place on the first boot. As a result, physical readout @@ -669,7 +758,7 @@ of flash will not be sufficient to recover most flash contents. `here `__. Prerequisites -------------- +^^^^^^^^^^^^^ First of all, we need to install ``imgtool`` (a MCUboot utility application to manipulate binary images) and ``esptool`` (the ESP32-S2 toolkit):: @@ -693,7 +782,7 @@ respectively, of the compiled project:: .. important:: The contents of the key files must be stored securely and kept secret. Enabling Secure Boot and Flash Encryption ------------------------------------------ +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ To enable Secure Boot for the current project, go to the project's NuttX directory, execute ``make menuconfig`` and the following steps: diff --git a/Documentation/platforms/xtensa/esp32s3/boards/esp32s3-devkit/index.rst b/Documentation/platforms/xtensa/esp32s3/boards/esp32s3-devkit/index.rst index d84861996e992..48b49e9561f8a 100644 --- a/Documentation/platforms/xtensa/esp32s3/boards/esp32s3-devkit/index.rst +++ b/Documentation/platforms/xtensa/esp32s3/boards/esp32s3-devkit/index.rst @@ -411,6 +411,14 @@ This configuration is the same as the ``nsh`` configuration, but it generates th image in a format that can be used by MCUboot. It also makes the ``make bootloader`` command to build the MCUboot bootloader image using the Espressif HAL. +mcuboot_update_agent +-------------------- + +This configuration is used to represent an MCUboot image that contains an update agent +to perform over-the-air (OTA) updates. Wi-Fi settings are already enabled and image confirmation program is included. + +Follow the instructions in the :ref:`MCUBoot and OTA Update ` section to execute OTA update. + nsh --- diff --git a/Documentation/platforms/xtensa/esp32s3/index.rst b/Documentation/platforms/xtensa/esp32s3/index.rst index 085d489919a72..8b414f1de2785 100644 --- a/Documentation/platforms/xtensa/esp32s3/index.rst +++ b/Documentation/platforms/xtensa/esp32s3/index.rst @@ -476,8 +476,97 @@ Then, it can be customized in the menu :menuselection:`System Type --> ADC Confi .. warning:: Minimum and maximum measurable voltages may saturate around 100 mV and 3000 mV, respectively. +.. _MCUBoot and OTA Update S3: + +MCUBoot and OTA Update +====================== + +The ESP32-S3 supports over-the-air (OTA) updates using MCUBoot. + +Read more about the MCUBoot for Espressif devices `here `__. + +Executing OTA Update +-------------------- + +This section describes how to execute OTA update using MCUBoot. + +1. First build the default ``mcuboot_update_agent`` config. This image defaults to the primary slot and already comes with Wi-Fi settings enabled:: + + ./tools/configure.sh esp32s3-devkit:mcuboot_update_agent + +2. Build the MCUBoot bootloader:: + + make bootloader + +3. Finally, build the application image:: + + make + +Flash the image to the board and verify it boots ok. +It should show the message "This is MCUBoot Update Agent image" before NuttShell is ready. + +At this point, the board should be able to connect to Wi-Fi so we can download a new binary from our network:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot Update Agent image + nsh> + nsh> wapi psk wlan0 3 + nsh> wapi essid wlan0 1 + nsh> renew wlan0 + +Now, keep the board as is and execute the following commands to **change the MCUBoot target slot to the 2nd slot** +and modify the message of the day (MOTD) as a mean to verify the new image is being used. + +1. Change the MCUBoot target slot to the 2nd slot:: + + kconfig-tweak -d CONFIG_ESPRESSIF_ESPTOOL_TARGET_PRIMARY + kconfig-tweak -e CONFIG_ESPRESSIF_ESPTOOL_TARGET_SECONDARY + kconfig-tweak --set-str CONFIG_NSH_MOTD_STRING "This is MCUBoot UPDATED image!" + make olddefconfig + + .. note:: + The same changes can be accomplished through ``menuconfig`` in :menuselection:`System Type --> Bootloader and Image Configuration --> Target slot for image flashing` + for MCUBoot target slot and in :menuselection:`System Type --> Bootloader and Image Configuration --> Search (motd) --> NSH Library --> Message of the Day` for the MOTD. + +2. Rebuild the application image:: + + make + +At this point the board is already connected to Wi-Fi and has the primary image flashed. +The new image configured for the 2nd slot is ready to be downloaded. + +To execute OTA, create a simple HTTP server on the NuttX directory so we can access the binary remotely:: + + cd nuttxspace/nuttx + python3 -m http.server + Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ... + +On the board, execute the update agent, setting the IP address to the one on the host machine. Wait until image is transferred and the board should reboot automatically:: + + nsh> mcuboot_agent http://10.42.0.1:8000/nuttx.bin + MCUboot Update Agent example + Downloading from http://10.42.0.1:8000/nuttx.bin + Firmware Update size: 1048576 bytes + Received: 512 of 1048576 bytes [0%] + Received: 1024 of 1048576 bytes [0%] + Received: 1536 of 1048576 bytes [0%] + [.....] + Received: 1048576 of 1048576 bytes [100%] + Application Image successfully downloaded! + Requested update for next boot. Restarting... + +NuttShell should now show the new MOTD, meaning the new image is being used:: + + NuttShell (NSH) NuttX-12.4.0 + This is MCUBoot UPDATED image! + nsh> + +Finally, the image is loaded but not confirmed. +To make sure it won't rollback to the previous image, you must confirm with ``mcuboot_confirm`` and reboot the board. +The OTA is now complete. + Wi-Fi ------ +===== .. tip:: Boards usually expose a ``wifi`` defconfig which enables Wi-Fi. On ESP32-S3, SMP is enabled to enhance Wi-Fi performance.