xtensa/esp32: Enable boot from Espressif's port of MCUboot

Signed-off-by: Gustavo Henrique Nihei <gustavo.nihei@espressif.com>
This commit is contained in:
Gustavo Henrique Nihei
2021-06-23 15:26:33 -03:00
committed by Alan Carvalho de Assis
parent 1a1b1cc2b4
commit 1dfcc6ab49
12 changed files with 1077 additions and 57 deletions
+121 -10
View File
@@ -254,14 +254,6 @@ config ESP32_RT_TIMER
default n
select ESP32_TIMER0
config ESP32_PARTITION
bool "ESP32 Partition"
default n
select ESP32_SPIFLASH
---help---
Decode esp-idf's partition file and initialize
partition by nuttx MTD.
config ESP32_RUN_IRAM
bool "Run from IRAM"
default n
@@ -834,9 +826,50 @@ endmenu # ESP32_SPI
menu "SPI Flash configuration"
depends on ESP32_SPIFLASH
if ESP32_HAVE_OTA_PARTITION
comment "Application Image OTA Update support"
config ESP32_OTA_PRIMARY_SLOT_OFFSET
hex "Application image primary slot offset"
default "0x10000"
config ESP32_OTA_PRIMARY_SLOT_DEVPATH
string "Application image primary slot device path"
default "/dev/ota0"
config ESP32_OTA_SECONDARY_SLOT_OFFSET
hex "Application image secondary slot offset"
default "0x110000"
config ESP32_OTA_SECONDARY_SLOT_DEVPATH
string "Application image secondary slot device path"
default "/dev/ota1"
config ESP32_OTA_SLOT_SIZE
hex "Application image slot size (in bytes)"
default "0x100000"
config ESP32_OTA_SCRATCH_OFFSET
hex "Scratch partition offset"
default "0x210000"
config ESP32_OTA_SCRATCH_SIZE
hex "Scratch partition size"
default "0x40000"
config ESP32_OTA_SCRATCH_DEVPATH
string "Scratch partition device path"
default "/dev/otascratch"
endif
comment "General MTD configuration"
config ESP32_MTD_OFFSET
hex "MTD base address in SPI Flash"
default 0x180000
default 0x180000 if !ESP32_HAVE_OTA_PARTITION
default 0x250000 if ESP32_HAVE_OTA_PARTITION
---help---
MTD base address in SPI Flash.
@@ -1153,6 +1186,82 @@ config ESP32_FREERUN
endmenu # Timer/counter Configuration
endif # ESP32_TIMER
config ESP32_HAVE_OTA_PARTITION
bool
default n
menu "Application Image Configuration"
choice
prompt "Application Image Format"
default ESP32_APP_FORMAT_LEGACY
---help---
Depending on the chosen 2nd stage bootloader, the application may
be required to be perform a specific startup routine. Furthermore,
the image binary must be formatted according to the definition from
the 2nd stage bootloader.
config ESP32_APP_FORMAT_LEGACY
bool "Legacy format"
---help---
This is the legacy application image format, as supported by the ESP-IDF
2nd stage bootloader.
config ESP32_APP_FORMAT_MCUBOOT
bool "MCUboot-bootable format"
select ESP32_HAVE_OTA_PARTITION
depends on EXPERIMENTAL
---help---
The ESP32 port of MCUboot supports the loading of unsegmented firmware
images.
comment "MCUboot support depends on CONFIG_EXPERIMENTAL"
depends on !EXPERIMENTAL
endchoice # Application Image Format
choice
prompt "Target slot for image flashing"
default ESP32_ESPTOOL_TARGET_PRIMARY
depends on ESP32_HAVE_OTA_PARTITION
---help---
Slot to which ESPTOOL will flash the generated binary image.
config ESP32_ESPTOOL_TARGET_PRIMARY
bool "Application image primary slot"
---help---
This assumes that the generated image is already pre-validated.
This is the recommended option for the initial stages of the
application firmware image development.
config ESP32_ESPTOOL_TARGET_SECONDARY
bool "Application image secondary slot"
---help---
The application needs to confirm the generated image as valid,
otherwise the bootloader may consider it invalid and perform the
rollback of the update after a reset.
This is the choice most suitable for the development and verification
of a secure firmware update workflow.
endchoice
config ESP32_APP_MCUBOOT_HEADER_SIZE
int "Application image header size (in bytes)"
default 32
depends on ESP32_APP_FORMAT_MCUBOOT
endmenu # Application Image Configuration
if ESP32_APP_FORMAT_LEGACY
config ESP32_PARTITION
bool "ESP32 Partition"
default n
select ESP32_SPIFLASH
---help---
Decode esp-idf's partition file and initialize
partition by nuttx MTD.
menu "Partition Configuration"
depends on ESP32_PARTITION
@@ -1164,7 +1273,9 @@ config ESP32_PARTITION_MOUNT
string "Partition mount point"
default "/dev/esp/partition/"
endmenu # ESP32_PARTITION
endmenu # Partition Configuration
endif
menu "AES accelerate"
depends on ESP32_AES_ACCELERATOR
+206 -20
View File
@@ -47,11 +47,81 @@
****************************************************************************/
#ifdef CONFIG_DEBUG_FEATURES
# define showprogress(c) up_puts(c)
# define showprogress(c) up_puts(c)
#else
# define showprogress(c)
#endif
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
#define PRIMARY_SLOT_OFFSET CONFIG_ESP32_OTA_PRIMARY_SLOT_OFFSET
#define HDR_ATTR __attribute__((section(".entry_addr"))) \
__attribute__((used))
/* Cache MMU block size */
#define MMU_BLOCK_SIZE 0x00010000 /* 64 KB */
/* Cache MMU address mask (MMU tables ignore bits which are zero) */
#define MMU_FLASH_MASK (~(MMU_BLOCK_SIZE - 1))
#endif
/****************************************************************************
* Private Types
****************************************************************************/
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
extern uint32_t _image_irom_vma;
extern uint32_t _image_irom_lma;
extern uint32_t _image_irom_size;
extern uint32_t _image_drom_vma;
extern uint32_t _image_drom_lma;
extern uint32_t _image_drom_size;
#endif
/****************************************************************************
* ROM Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
extern int ets_printf(const char *fmt, ...);
extern void cache_read_enable(int cpu);
extern void cache_read_disable(int cpu);
extern void cache_flush(int cpu);
extern unsigned int cache_flash_mmu_set(int cpu_no, int pid,
unsigned int vaddr,
unsigned int paddr,
int psize, int num);
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
noreturn_function void __start(void);
#endif
/****************************************************************************
* Public Functions Prototypes
****************************************************************************/
#ifndef CONFIG_SUPPRESS_UART_CONFIG
extern void esp32_lowsetup(void);
#endif
/****************************************************************************
* Private Data
****************************************************************************/
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
HDR_ATTR static void (*_entry_point)(void) = &__start;
#endif
/****************************************************************************
* Public Data
****************************************************************************/
@@ -62,27 +132,10 @@ uint32_t g_idlestack[IDLETHREAD_STACKWORDS]
aligned_data(16) locate_data(".noinit");
/****************************************************************************
* Public Functions
* Private Functions
****************************************************************************/
#ifndef CONFIG_SUPPRESS_UART_CONFIG
extern void esp32_lowsetup(void);
#endif
/****************************************************************************
* Name: __start
*
* Description:
* We arrive here after the bootloader finished loading the program from
* flash. The hardware is mostly uninitialized, and the app CPU is in
* reset. We do have a stack, so we can do the initialization in C.
*
* The app CPU will remain in reset unless CONFIG_SMP is selected and
* up_cpu_start() is called later in the bring-up sequeuence.
*
****************************************************************************/
void IRAM_ATTR __start(void)
static noreturn_function void __esp32_start(void)
{
uint32_t regval;
uint32_t sp;
@@ -178,3 +231,136 @@ void IRAM_ATTR __start(void)
nx_start();
for (; ; ); /* Should not return */
}
/****************************************************************************
* Name: calc_mmu_pages
*
* Description:
* Calculate the number of cache pages to map.
*
* Input Parameters:
* size - Size of data to map
* vaddr - Virtual address where data will be mapped
*
* Returned Value:
* Number of cache MMU pages required to do the mapping.
*
****************************************************************************/
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
static inline uint32_t calc_mmu_pages(uint32_t size, uint32_t vaddr)
{
return (size + (vaddr - (vaddr & MMU_FLASH_MASK)) + MMU_BLOCK_SIZE - 1) /
MMU_BLOCK_SIZE;
}
#endif
/****************************************************************************
* Name: map_rom_segments
*
* Description:
* Configure the MMU and Cache peripherals for accessing ROM code and data.
*
* Input Parameters:
* None.
*
* Returned Value:
* None.
*
****************************************************************************/
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
static int map_rom_segments(void)
{
uint32_t rc = 0;
uint32_t regval;
uint32_t drom_lma_aligned;
uint32_t drom_vma_aligned;
uint32_t drom_page_count;
uint32_t irom_lma_aligned;
uint32_t irom_vma_aligned;
uint32_t irom_page_count;
size_t partition_offset = PRIMARY_SLOT_OFFSET;
uint32_t app_irom_lma = partition_offset + (uint32_t)&_image_irom_lma;
uint32_t app_irom_size = (uint32_t)&_image_irom_size;
uint32_t app_irom_vma = (uint32_t)&_image_irom_vma;
uint32_t app_drom_lma = partition_offset + (uint32_t)&_image_drom_lma;
uint32_t app_drom_size = (uint32_t)&_image_drom_size;
uint32_t app_drom_vma = (uint32_t)&_image_drom_vma;
volatile uint32_t *pro_flash_mmu_table =
(volatile uint32_t *)DPORT_PRO_FLASH_MMU_TABLE_REG;
cache_read_disable(0);
cache_flush(0);
/* Clear the MMU entries that are already set up, so the new app only has
* the mappings it creates.
*/
for (int i = 0; i < DPORT_FLASH_MMU_TABLE_SIZE; i++)
{
putreg32(DPORT_FLASH_MMU_TABLE_INVALID_VAL, pro_flash_mmu_table++);
}
drom_lma_aligned = app_drom_lma & MMU_FLASH_MASK;
drom_vma_aligned = app_drom_vma & MMU_FLASH_MASK;
drom_page_count = calc_mmu_pages(app_drom_size, app_drom_vma);
rc = cache_flash_mmu_set(0, 0, drom_vma_aligned, drom_lma_aligned, 64,
(int)drom_page_count);
rc |= cache_flash_mmu_set(1, 0, drom_vma_aligned, drom_lma_aligned, 64,
(int)drom_page_count);
irom_lma_aligned = app_irom_lma & MMU_FLASH_MASK;
irom_vma_aligned = app_irom_vma & MMU_FLASH_MASK;
irom_page_count = calc_mmu_pages(app_irom_size, app_irom_vma);
rc |= cache_flash_mmu_set(0, 0, irom_vma_aligned, irom_lma_aligned, 64,
(int)irom_page_count);
rc |= cache_flash_mmu_set(1, 0, irom_vma_aligned, irom_lma_aligned, 64,
(int)irom_page_count);
regval = getreg32(DPORT_PRO_CACHE_CTRL1_REG);
regval &= ~(DPORT_PRO_CACHE_MASK_IRAM0 | DPORT_PRO_CACHE_MASK_DROM0 |
DPORT_PRO_CACHE_MASK_DRAM1);
putreg32(regval, DPORT_PRO_CACHE_CTRL1_REG);
regval = getreg32(DPORT_APP_CACHE_CTRL1_REG);
regval &= ~(DPORT_APP_CACHE_MASK_IRAM0 | DPORT_APP_CACHE_MASK_DROM0 |
DPORT_APP_CACHE_MASK_DRAM1);
putreg32(regval, DPORT_APP_CACHE_CTRL1_REG);
cache_read_enable(0);
return (int)rc;
}
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: __start
*
* Description:
* We arrive here after the bootloader finished loading the program from
* flash. The hardware is mostly uninitialized, and the app CPU is in
* reset. We do have a stack, so we can do the initialization in C.
*
* The app CPU will remain in reset unless CONFIG_SMP is selected and
* up_cpu_start() is called later in the bring-up sequence.
*
****************************************************************************/
void __start(void)
{
#ifdef CONFIG_ESP32_APP_FORMAT_MCUBOOT
if (map_rom_segments() != 0)
{
ets_printf("Failed to setup XIP, aborting\n");
while (true);
}
#endif
__esp32_start();
}
@@ -4398,4 +4398,10 @@
#define DPORT_APP_FLASH_MMU_TABLE_REG (DR_REG_DPORT_BASE + 0x12000)
#define DPORT_FLASH_MMU_TABLE_SIZE 0x100
#define DPORT_FLASH_MMU_TABLE_INVALID_VAL 0x100
#define DPORT_MMU_ADDRESS_MASK 0xff
#endif /* __ARCH_XTENSA_SRC_ESP32_HARDWARE_ESP32_DPORT_H */