boot/espressif: Add CMakeLists.txt and mcuboot_config.h
Add sources and headers required for build
Signed-off-by: Shubham Kulkarni <shubham.kulkarni@espressif.com>
diff --git a/boot/espressif/CMakeLists.txt b/boot/espressif/CMakeLists.txt
new file mode 100644
index 0000000..8094031
--- /dev/null
+++ b/boot/espressif/CMakeLists.txt
@@ -0,0 +1,158 @@
+cmake_minimum_required(VERSION 3.13)
+
+if (NOT DEFINED MCUBOOT_TARGET)
+ message(FATAL_ERROR "MCUBOOT_TARGET not defined. Please pass -DMCUBOOT_TARGET flag.")
+endif()
+
+project(mcuboot_${MCUBOOT_TARGET})
+
+if (NOT DEFINED IDF_PATH)
+ if (DEFINED ENV{IDF_PATH})
+ set(IDF_PATH $ENV{IDF_PATH})
+ else()
+ message(FATAL_ERROR "IDF_PATH not found. Please set IDF_PATH environment variable or pass -DIDF_PATH flag.")
+ endif()
+endif()
+
+execute_process(
+ COMMAND git describe --tags
+ WORKING_DIRECTORY ${IDF_PATH}
+ OUTPUT_VARIABLE IDF_VER
+ OUTPUT_STRIP_TRAILING_WHITESPACE
+)
+if (NOT "${IDF_VER}" MATCHES "v4.3")
+ message(FATAL_ERROR "Unsupported ESP-IDF version found in IDF_PATH, please checkout to v4.3")
+endif()
+
+if (DEFINED MCUBOOT_CONFIG_FILE)
+ set(mcuboot_config_file ${MCUBOOT_CONFIG_FILE})
+else()
+ set(mcuboot_config_file "${CMAKE_CURRENT_LIST_DIR}/bootloader.conf")
+endif()
+
+if (NOT EXISTS "${mcuboot_config_file}")
+ message(FATAL_ERROR "MCUboot configuration file does not exist at ${mcuboot_config_file}")
+endif()
+
+configure_file(${mcuboot_config_file} dummy.conf)
+file(STRINGS ${mcuboot_config_file} BOOTLOADER_CONF)
+foreach(config ${BOOTLOADER_CONF})
+ if (NOT (${config} MATCHES "#"))
+ string(REGEX REPLACE "^[ ]+" "" config ${config})
+ string(REGEX MATCH "^[^=]+" CONFIG_NAME ${config})
+ string(REPLACE "${CONFIG_NAME}=" "" CONFIG_VALUE ${config})
+ add_definitions(-D${CONFIG_NAME}=${CONFIG_VALUE})
+ endif()
+endforeach()
+
+set(APP_NAME mcuboot_${MCUBOOT_TARGET})
+set(APP_EXECUTABLE ${APP_NAME}.elf)
+
+set(MCUBOOT_ROOT_DIR ${CMAKE_CURRENT_LIST_DIR}/../..)
+set(BOOTUTIL_DIR ${MCUBOOT_ROOT_DIR}/boot/bootutil)
+set(MBEDTLS_DIR ${MCUBOOT_ROOT_DIR}/ext/mbedtls)
+
+set(bootutil_srcs
+ ${BOOTUTIL_DIR}/src/boot_record.c
+ ${BOOTUTIL_DIR}/src/bootutil_misc.c
+ ${BOOTUTIL_DIR}/src/bootutil_public.c
+ ${BOOTUTIL_DIR}/src/caps.c
+ ${BOOTUTIL_DIR}/src/encrypted.c
+ ${BOOTUTIL_DIR}/src/fault_injection_hardening.c
+ ${BOOTUTIL_DIR}/src/fault_injection_hardening_delay_rng_mbedtls.c
+ ${BOOTUTIL_DIR}/src/image_ec.c
+ ${BOOTUTIL_DIR}/src/image_ec256.c
+ ${BOOTUTIL_DIR}/src/image_ed25519.c
+ ${BOOTUTIL_DIR}/src/image_rsa.c
+ ${BOOTUTIL_DIR}/src/image_validate.c
+ ${BOOTUTIL_DIR}/src/loader.c
+ ${BOOTUTIL_DIR}/src/swap_misc.c
+ ${BOOTUTIL_DIR}/src/swap_move.c
+ ${BOOTUTIL_DIR}/src/swap_scratch.c
+ ${BOOTUTIL_DIR}/src/tlv.c
+ )
+
+set(mbedtls_srcs
+ ${MBEDTLS_DIR}/library/sha256.c
+ ${MBEDTLS_DIR}/library/platform_util.c
+ )
+
+set(CFLAGS
+ "-mlongcalls"
+ "-Wno-frame-address"
+ "-Wall"
+ "-Wextra"
+ "-W"
+ "-Wdeclaration-after-statement"
+ "-Wwrite-strings"
+ "-Wlogical-op"
+ "-Wshadow"
+ "-ffunction-sections"
+ "-fdata-sections"
+ "-fstrict-volatile-bitfields"
+ "-Werror=all"
+ "-Wno-error=unused-function"
+ "-Wno-error=unused-but-set-variable"
+ "-Wno-error=unused-variable"
+ "-Wno-error=deprecated-declarations"
+ "-Wno-unused-parameter"
+ "-Wno-sign-compare"
+ "-ggdb"
+ "-Os"
+ "-D_GNU_SOURCE"
+ "-std=gnu99"
+ "-Wno-old-style-declaration"
+ "-Wno-implicit-int"
+ "-Wno-declaration-after-statement"
+ )
+
+set(LDFLAGS
+ "-nostdlib"
+ "-mlongcalls"
+ "-Wno-frame-address"
+ "-Wl,--cref"
+ "-Wl,--Map=${APP_NAME}.map"
+ "-fno-rtti"
+ "-fno-lto"
+ "-Wl,--gc-sections"
+ "-Wl,--undefined=uxTopUsedPriority"
+ "-lm"
+ "-lgcc"
+ "-lgcov"
+ "-lstdc++"
+ "-lc"
+ )
+
+add_executable(
+ ${APP_EXECUTABLE}
+ ${CMAKE_CURRENT_LIST_DIR}/main.c
+ )
+
+target_compile_options(
+ ${APP_EXECUTABLE}
+ PUBLIC
+ ${CFLAGS}
+ )
+
+target_sources(
+ ${APP_EXECUTABLE}
+ PUBLIC
+ ${bootutil_srcs}
+ ${mbedtls_srcs}
+ ${CMAKE_CURRENT_LIST_DIR}/port/esp_mcuboot.c
+ )
+
+target_include_directories(
+ ${APP_EXECUTABLE}
+ PUBLIC
+ ${BOOTUTIL_DIR}/include
+ ${MBEDTLS_DIR}/include
+ ${CMAKE_CURRENT_LIST_DIR}/include
+ )
+
+target_link_libraries(
+ ${APP_EXECUTABLE}
+ PUBLIC
+ -T${CMAKE_CURRENT_LIST_DIR}/port/${MCUBOOT_TARGET}/ld/bootloader.ld
+ ${LDFLAGS}
+ )
diff --git a/boot/espressif/bootloader.conf b/boot/espressif/bootloader.conf
new file mode 100644
index 0000000..a20c904
--- /dev/null
+++ b/boot/espressif/bootloader.conf
@@ -0,0 +1,7 @@
+CONFIG_ESP_BOOTLOADER_SIZE=0xF000
+CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS=0x10000
+CONFIG_ESP_APPLICATION_SIZE=0x100000
+CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS=0x110000
+CONFIG_ESP_MCUBOOT_WDT_ENABLE=y
+CONFIG_ESP_SCRATCH_OFFSET=0x210000
+CONFIG_ESP_SCRATCH_SIZE=0x40000
diff --git a/boot/espressif/include/flash_map_backend/flash_map_backend.h b/boot/espressif/include/flash_map_backend/flash_map_backend.h
new file mode 100644
index 0000000..fcbdc9f
--- /dev/null
+++ b/boot/espressif/include/flash_map_backend/flash_map_backend.h
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#pragma once
+
+#include <inttypes.h>
+
+struct flash_area {
+ uint8_t fa_id; /** The slot/scratch identification */
+ uint8_t fa_device_id; /** The device id (usually there's only one) */
+ uint16_t pad16;
+ uint32_t fa_off; /** The flash offset from the beginning */
+ uint32_t fa_size; /** The size of this sector */
+};
+
+//! Structure describing a sector within a flash area.
+struct flash_sector {
+ //! Offset of this sector, from the start of its flash area (not device).
+ uint32_t fs_off;
+
+ //! Size of this sector, in bytes.
+ uint32_t fs_size;
+};
+
+static inline uint8_t flash_area_get_device_id(const struct flash_area *fa)
+{
+ return (uint8_t)fa->fa_device_id;
+}
+static inline uint32_t flash_area_get_off(const struct flash_area *fa)
+{
+ return (uint32_t)fa->fa_off;
+}
+
+static inline uint32_t flash_area_get_size(const struct flash_area *fa)
+{
+ return (uint32_t)fa->fa_size;
+}
+
+static inline uint8_t flash_area_get_id(const struct flash_area *fa)
+{
+ return fa->fa_id;
+}
+
+static inline uint32_t flash_sector_get_off(const struct flash_sector *fs)
+{
+ return fs->fs_off;
+}
+
+static inline uint32_t flash_sector_get_size(const struct flash_sector *fs)
+{
+ return fs->fs_size;
+}
+
+//! Opens the area for use. id is one of the `fa_id`s */
+int flash_area_open(uint8_t id, const struct flash_area **area_outp);
+void flash_area_close(const struct flash_area *fa);
+
+//! Reads `len` bytes of flash memory at `off` to the buffer at `dst`
+int flash_area_read(const struct flash_area *fa, uint32_t off,
+ void *dst, uint32_t len);
+//! Writes `len` bytes of flash memory at `off` from the buffer at `src`
+int flash_area_write(const struct flash_area *fa, uint32_t off,
+ const void *src, uint32_t len);
+//! Erases `len` bytes of flash memory at `off`
+int flash_area_erase(const struct flash_area *fa,
+ uint32_t off, uint32_t len);
+
+//! Returns this `flash_area`s alignment
+size_t flash_area_align(const struct flash_area *area);
+//! Returns the value read from an erased flash area byte
+uint8_t flash_area_erased_val(const struct flash_area *area);
+
+//! Given flash area ID, return info about sectors within the area
+int flash_area_get_sectors(int fa_id, uint32_t *count,
+ struct flash_sector *sectors);
+
+//! Returns the `fa_id` for slot, where slot is 0 (primary) or 1 (secondary).
+//!
+//! `image_index` (0 or 1) is the index of the image. Image index is
+//! relevant only when multi-image support support is enabled
+int flash_area_id_from_multi_image_slot(int image_index, int slot);
+int flash_area_id_from_image_slot(int slot);
+int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa);
diff --git a/boot/espressif/include/os/os_malloc.h b/boot/espressif/include/os/os_malloc.h
new file mode 100644
index 0000000..1a6a6dc
--- /dev/null
+++ b/boot/espressif/include/os/os_malloc.h
@@ -0,0 +1,9 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#pragma once
+
+#include <stdlib.h>
diff --git a/boot/espressif/include/sysflash/sysflash.h b/boot/espressif/include/sysflash/sysflash.h
new file mode 100644
index 0000000..7f0fb28
--- /dev/null
+++ b/boot/espressif/include/sysflash/sysflash.h
@@ -0,0 +1,25 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+//! A user-defined identifier for different storage mediums
+//! (i.e internal flash, external NOR flash, eMMC, etc)
+#define FLASH_DEVICE_INTERNAL_FLASH 0
+
+//! An arbitrarily high slot ID we will use to indicate that
+//! there is not slot
+#define FLASH_SLOT_DOES_NOT_EXIST 255
+
+//! NB: MCUboot expects this define to exist but it's only used
+//! if MCUBOOT_SWAP_USING_SCRATCH=1 is set
+#define FLASH_AREA_IMAGE_SCRATCH FLASH_SLOT_DOES_NOT_EXIST
+
+//! The slot we will use to track the bootloader allocation
+#define FLASH_AREA_BOOTLOADER 0
+
+//! A mapping to primary and secondary/upgrade slot
+//! given an image_index. We'll plan to use
+#define FLASH_AREA_IMAGE_PRIMARY(i) ((i == 0) ? 1 : 255)
+#define FLASH_AREA_IMAGE_SECONDARY(i) ((i == 0) ? 2 : 255)
diff --git a/boot/espressif/main.c b/boot/espressif/main.c
new file mode 100644
index 0000000..9b98fbf
--- /dev/null
+++ b/boot/espressif/main.c
@@ -0,0 +1,18 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <bootutil/bootutil.h>
+
+int main()
+{
+ struct boot_rsp rsp;
+ int rv = boot_go(&rsp);
+
+ if (rv == 0) {
+ //
+ }
+ while(1);
+}
diff --git a/boot/espressif/port/esp32/ld/bootloader.ld b/boot/espressif/port/esp32/ld/bootloader.ld
new file mode 100644
index 0000000..87f5c56
--- /dev/null
+++ b/boot/espressif/port/esp32/ld/bootloader.ld
@@ -0,0 +1,135 @@
+/*
+ * Linker file used to link the bootloader.
+ */
+
+MEMORY
+{
+ iram_loader_seg (RWX) : org = 0x4009D000, len = 0x3000
+ iram_seg (RWX) : org = 0x40085000, len = 0x8000
+ dram_seg (RW) : org = 0x3FFF5000, len = 0xB000
+}
+
+ENTRY(main)
+
+SECTIONS
+{
+ .iram_loader.text :
+ {
+ . = ALIGN (16);
+ _loader_text_start = ABSOLUTE(.);
+ *(.stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
+ *(.iram1 .iram1.*) /* catch stray IRAM_ATTR */
+ *libhal.a:bootloader_flash.*(.literal .text .literal.* .text.*)
+ *libhal.a:bootloader_flash_config_esp32.*(.literal .text .literal.* .text.*)
+ *esp_mcuboot.*(.literal .text .literal.* .text.*)
+ *esp_loader.*(.literal .text .literal.* .text.*)
+ *(.fini.literal)
+ *(.fini)
+ *(.gnu.version)
+ _loader_text_end = ABSOLUTE(.);
+ } > iram_loader_seg
+
+ .iram.text :
+ {
+ . = ALIGN (16);
+ *(.entry.text)
+ *(.init.literal)
+ *(.init)
+ } > iram_seg
+
+ .dram0.bss (NOLOAD) :
+ {
+ . = ALIGN (8);
+ _dram_start = ABSOLUTE(.);
+ _bss_start = ABSOLUTE(.);
+ *(.dynsbss)
+ *(.sbss)
+ *(.sbss.*)
+ *(.gnu.linkonce.sb.*)
+ *(.scommon)
+ *(.sbss2)
+ *(.sbss2.*)
+ *(.gnu.linkonce.sb2.*)
+ *(.dynbss)
+ *(.bss)
+ *(.bss.*)
+ *(.gnu.linkonce.b.*)
+ *(COMMON)
+ . = ALIGN (8);
+ _bss_end = ABSOLUTE(.);
+ } >dram_seg
+
+ .dram0.data :
+ {
+ _data_start = ABSOLUTE(.);
+ *(.data)
+ *(.data.*)
+ *(.gnu.linkonce.d.*)
+ *(.data1)
+ *(.sdata)
+ *(.sdata.*)
+ *(.gnu.linkonce.s.*)
+ *(.sdata2)
+ *(.sdata2.*)
+ *(.gnu.linkonce.s2.*)
+ *(.jcr)
+ _data_end = ABSOLUTE(.);
+ } >dram_seg
+
+ .dram0.rodata :
+ {
+ _rodata_start = ABSOLUTE(.);
+ *(.rodata)
+ *(.rodata.*)
+ *(.gnu.linkonce.r.*)
+ *(.rodata1)
+ __XT_EXCEPTION_TABLE_ = ABSOLUTE(.);
+ *(.xt_except_table)
+ *(.gcc_except_table)
+ *(.gnu.linkonce.e.*)
+ *(.gnu.version_r)
+ *(.eh_frame)
+ . = (. + 3) & ~ 3;
+ /* C++ constructor and destructor tables, properly ordered: */
+ __init_array_start = ABSOLUTE(.);
+ KEEP (*crtbegin.*(.ctors))
+ KEEP (*(EXCLUDE_FILE (*crtend.*) .ctors))
+ KEEP (*(SORT(.ctors.*)))
+ KEEP (*(.ctors))
+ __init_array_end = ABSOLUTE(.);
+ KEEP (*crtbegin.*(.dtors))
+ KEEP (*(EXCLUDE_FILE (*crtend.*) .dtors))
+ KEEP (*(SORT(.dtors.*)))
+ KEEP (*(.dtors))
+ /* C++ exception handlers table: */
+ __XT_EXCEPTION_DESCS_ = ABSOLUTE(.);
+ *(.xt_except_desc)
+ *(.gnu.linkonce.h.*)
+ __XT_EXCEPTION_DESCS_END__ = ABSOLUTE(.);
+ *(.xt_except_desc_end)
+ *(.dynamic)
+ *(.gnu.version_d)
+ _rodata_end = ABSOLUTE(.);
+ /* Literals are also RO data. */
+ _lit4_start = ABSOLUTE(.);
+ *(*.lit4)
+ *(.lit4.*)
+ *(.gnu.linkonce.lit4.*)
+ _lit4_end = ABSOLUTE(.);
+ . = ALIGN(4);
+ _dram_end = ABSOLUTE(.);
+ } >dram_seg
+
+ .iram.text :
+ {
+ _stext = .;
+ _text_start = ABSOLUTE(.);
+ *(.literal .text .literal.* .text.* .stub .gnu.warning .gnu.linkonce.literal.* .gnu.linkonce.t.*.literal .gnu.linkonce.t.*)
+ *(.iram .iram.*) /* catch stray IRAM_ATTR */
+ *(.fini.literal)
+ *(.fini)
+ *(.gnu.version)
+ _text_end = ABSOLUTE(.);
+ _etext = .;
+ } > iram_seg
+}
diff --git a/boot/espressif/port/esp_mcuboot.c b/boot/espressif/port/esp_mcuboot.c
new file mode 100644
index 0000000..828fc9e
--- /dev/null
+++ b/boot/espressif/port/esp_mcuboot.c
@@ -0,0 +1,216 @@
+/*
+ * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#include "mcuboot_config/mcuboot_logging.h"
+#include "flash_map_backend/flash_map_backend.h"
+#include "sysflash/sysflash.h"
+#include "bootutil/bootutil.h"
+
+#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
+
+#define BOOTLOADER_START_ADDRESS 0x1000
+#define BOOTLOADER_SIZE CONFIG_ESP_BOOTLOADER_SIZE
+#define APPLICATION_PRIMARY_START_ADDRESS CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS
+#define APPLICATION_SECONDARY_START_ADDRESS CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS
+#define APPLICATION_SIZE CONFIG_ESP_APPLICATION_SIZE
+#define SCRATCH_OFFSET CONFIG_ESP_SCRATCH_OFFSET
+#define SCRATCH_SIZE CONFIG_ESP_SCRATCH_SIZE
+
+#define FLASH_SECTOR_SIZE 4096
+
+extern int ets_printf(const char *fmt, ...);
+
+static const struct flash_area bootloader = {
+ .fa_id = FLASH_AREA_BOOTLOADER,
+ .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
+ .fa_off = BOOTLOADER_START_ADDRESS,
+ .fa_size = BOOTLOADER_SIZE,
+};
+
+static const struct flash_area primary_img0 = {
+ .fa_id = FLASH_AREA_IMAGE_PRIMARY(0),
+ .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
+ .fa_off = APPLICATION_PRIMARY_START_ADDRESS,
+ .fa_size = APPLICATION_SIZE,
+};
+
+static const struct flash_area secondary_img0 = {
+ .fa_id = FLASH_AREA_IMAGE_SECONDARY(0),
+ .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
+ .fa_off = APPLICATION_SECONDARY_START_ADDRESS,
+ .fa_size = APPLICATION_SIZE,
+};
+
+static const struct flash_area scratch_img0 = {
+ .fa_id = FLASH_AREA_IMAGE_SCRATCH,
+ .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
+ .fa_off = SCRATCH_OFFSET,
+ .fa_size = SCRATCH_SIZE,
+};
+
+static const struct flash_area *s_flash_areas[] = {
+ &bootloader,
+ &primary_img0,
+ &secondary_img0,
+ &scratch_img0,
+};
+
+static const struct flash_area *prv_lookup_flash_area(uint8_t id) {
+ for (size_t i = 0; i < ARRAY_SIZE(s_flash_areas); i++) {
+ const struct flash_area *area = s_flash_areas[i];
+ if (id == area->fa_id) {
+ return area;
+ }
+ }
+ return NULL;
+}
+
+int flash_area_open(uint8_t id, const struct flash_area **area_outp)
+{
+ MCUBOOT_LOG_DBG("%s: ID=%d", __func__, (int)id);
+ const struct flash_area *area = prv_lookup_flash_area(id);
+ *area_outp = area;
+ return area != NULL ? 0 : -1;
+}
+
+void flash_area_close(const struct flash_area *area)
+{
+
+}
+
+int flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
+ uint32_t len)
+{
+ if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
+ return -1;
+ }
+
+ const uint32_t end_offset = off + len;
+ if (end_offset > fa->fa_size) {
+ MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
+ return -1;
+ }
+
+ return 0;
+}
+
+int flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
+ uint32_t len)
+{
+ if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
+ return -1;
+ }
+
+ const uint32_t end_offset = off + len;
+ if (end_offset > fa->fa_size) {
+ MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
+ return -1;
+ }
+
+ const uint32_t addr = fa->fa_off + off;
+ MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)addr, (int)len);
+
+#if VALIDATE_PROGRAM_OP
+ if (memcmp((void *)addr, src, len) != 0) {
+ MCUBOOT_LOG_ERR("%s: Program Failed", __func__);
+ assert(0);
+ }
+#endif
+
+ return 0;
+}
+
+int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
+{
+ if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
+ return -1;
+ }
+
+ if ((len % FLASH_SECTOR_SIZE) != 0 || (off % FLASH_SECTOR_SIZE) != 0) {
+ MCUBOOT_LOG_ERR("%s: Not aligned on sector Offset: 0x%x Length: 0x%x", __func__,
+ (int)off, (int)len);
+ return -1;
+ }
+
+ const uint32_t start_addr = fa->fa_off + off;
+ MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
+
+#if VALIDATE_PROGRAM_OP
+ for (size_t i = 0; i < len; i++) {
+ uint8_t *val = (void *)(start_addr + i);
+ if (*val != 0xff) {
+ MCUBOOT_LOG_ERR("%s: Erase at 0x%x Failed", __func__, (int)val);
+ assert(0);
+ }
+ }
+#endif
+
+ return 0;
+}
+
+size_t flash_area_align(const struct flash_area *area)
+{
+ return 4;
+}
+
+uint8_t flash_area_erased_val(const struct flash_area *area)
+{
+ return 0xff;
+}
+
+int flash_area_get_sectors(int fa_id, uint32_t *count,
+ struct flash_sector *sectors)
+{
+ const struct flash_area *fa = prv_lookup_flash_area(fa_id);
+ if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
+ return -1;
+ }
+
+ const size_t sector_size = FLASH_SECTOR_SIZE;
+ uint32_t total_count = 0;
+ for (size_t off = 0; off < fa->fa_size; off += sector_size) {
+ // Note: Offset here is relative to flash area, not device
+ sectors[total_count].fs_off = off;
+ sectors[total_count].fs_size = sector_size;
+ total_count++;
+ }
+
+ *count = total_count;
+ return 0;
+}
+
+int flash_area_id_from_multi_image_slot(int image_index, int slot)
+{
+ MCUBOOT_LOG_DBG("%s", __func__);
+ switch (slot) {
+ case 0:
+ return FLASH_AREA_IMAGE_PRIMARY(image_index);
+ case 1:
+ return FLASH_AREA_IMAGE_SECONDARY(image_index);
+ }
+
+ MCUBOOT_LOG_ERR("Unexpected Request: image_index=%d, slot=%d", image_index, slot);
+ return -1; /* flash_area_open will fail on that */
+}
+
+int flash_area_id_from_image_slot(int slot)
+{
+ return flash_area_id_from_multi_image_slot(0, slot);
+}
+
+int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa)
+{
+ return -1;
+}
+
+void mcuboot_assert_handler(const char *file, int line, const char *func)
+{
+ ets_printf("assertion failed: file \"%s\", line %d, func: %s\n", file, line, func);
+ abort();
+}
diff --git a/boot/espressif/tools/toolchain-esp32.cmake b/boot/espressif/tools/toolchain-esp32.cmake
new file mode 100644
index 0000000..5c9c535
--- /dev/null
+++ b/boot/espressif/tools/toolchain-esp32.cmake
@@ -0,0 +1,10 @@
+set(CMAKE_SYSTEM_NAME Generic)
+
+set(CMAKE_C_COMPILER xtensa-esp32-elf-gcc)
+set(CMAKE_CXX_COMPILER xtensa-esp32-elf-g++)
+set(CMAKE_ASM_COMPILER xtensa-esp32-elf-gcc)
+
+set(CMAKE_C_FLAGS "-mlongcalls -Wno-frame-address" CACHE STRING "C Compiler Base Flags")
+set(CMAKE_CXX_FLAGS "-mlongcalls -Wno-frame-address" CACHE STRING "C++ Compiler Base Flags")
+
+set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections" CACHE STRING "Linker Base Flags")