diff --git a/components/bootloader/subproject/Makefile b/components/bootloader/subproject/Makefile index ee495c38..edf34e8d 100644 --- a/components/bootloader/subproject/Makefile +++ b/components/bootloader/subproject/Makefile @@ -8,14 +8,14 @@ endif PROJECT_NAME := bootloader -COMPONENTS := esptool_py main log esp8266 +COMPONENTS := esptool_py main bootloader_support spi_flash log esp8266 # Clear C and CXX from top level project CFLAGS = CXXFLAGS = #We cannot include the esp8266 component directly but we need its includes. -CFLAGS += -I $(IDF_PATH)/components/esp8266/include +CFLAGS += -I $(IDF_PATH)/components/esp8266/include -I $(IDF_PATH)/components/util/include # The bootloader pseudo-component is also included in this build, for its Kconfig.projbuild to be included. # diff --git a/components/bootloader/subproject/main/bootloader_start.c b/components/bootloader/subproject/main/bootloader_start.c index 99df7f6d..e8930ab4 100644 --- a/components/bootloader/subproject/main/bootloader_start.c +++ b/components/bootloader/subproject/main/bootloader_start.c @@ -14,301 +14,46 @@ #include "sdkconfig.h" -#include -#include +#include "bootloader_init.h" +#include "bootloader_config.h" +#include "esp_image_format.h" +#include "esp_log.h" -#include "load_flash_bin.h" +static const char* TAG = "boot"; -#include "esp8266/eagle_soc.h" - -//#define BOOT_DEBUG - -#ifdef BOOT_DEBUG -#define BDEBUG ets_printf -#else -#define BDEBUG(...) -#endif - -#define BOOT_VERSION 0x06 - -typedef enum { - SPI_FLASH_QIO_MODE = 0, - SPI_FLASH_QOUT_MODE, - SPI_FLASH_DIO_MODE, - SPI_FLASH_DOUT_MODE, - SPI_FLASH_FASTRD_MODE, - SPI_FLASH_SLOWRD_MODE -} SpiFlashRdMode; - -enum { - SPI_MODE_QIO, - SPI_MODE_QOUT, - SPI_MODE_DIO, - SPI_MODE_DOUT -}; - -enum { - SPI_SPEED_40M, - SPI_SPEED_26M, - SPI_SPEED_20M, - SPI_SPEED_80M = 0xF -}; - -enum { - SPI_SIZE_4M_256_256 = 0, - SPI_SIZE_2M, - SPI_SIZE_8M_512_512, - SPI_SIZE_16M_512_512, - SPI_SIZE_32M_512_512, - SPI_SIZE_16M_1024_1024, - SPI_SIZE_32M_1024_1024 -}; - -enum { - USER_BIN1, - USER_BIN2 -}; - -#define SPI_SEC_SIZE 0x1000 - -struct save_hdr { - char flag; - char pad[3]; -}; - -struct boot_hdr { - char use_bin: 2; // low bit - char boot_status: 1; - char reverse: 5; - char version: 5; // low bit - char test_pass_flag: 1; - char test_start_flag: 1; - char enhance_boot_flag: 1; - char test_bin_addr[3]; - char user_bin_addr[3]; -}; - -struct boot_hdr_1 { - char use_bin: 4; - char flag: 4; - char pad[7]; -}; - -struct boot_hdr_2 { - char use_bin: 4; - char flag: 4; - char version; - char pad[6]; -}; - -struct flash_hdr { - char magic; - char blocks; - char spi_mode; //flag of flash read mode in unpackage and usage in future - char spi_speed: 4; // low bit - char spi_size_map: 4; - unsigned int entry_addr; -} ; - -struct block_hdr { - unsigned int load_addr; - unsigned int data_len; -} ; - -#define WIFI_PARAM_RF 0 -#define WIFI_PARAM_SAVE_0 1 -#define WIFI_PARAM_SAVE_1 2 -#define WIFI_PARAM_FLAG 3 - -typedef enum { - SPI_FLASH_RESULT_OK = 0, - SPI_FLASH_RESULT_ERR = 1, - SPI_FLASH_RESULT_TIMEOUT = 2 -} SpiFlashOpResult; - -extern SpiFlashOpResult SPIRead(uint32_t addr, void *dst, uint32_t size); -extern int ets_printf(const char* fmt, ...); -extern void *ets_memcpy(void *restrict to, const void *restrict from, size_t size); - -signed int get_flash_bin_addr(unsigned int bin_addr) -{ - char buf[16]; - struct flash_hdr* fhdr; - struct block_hdr* bhdr; - - SPIRead(bin_addr, (unsigned int*)buf, 16); - - fhdr = (struct flash_hdr*)buf; - - if (fhdr->magic == 0xE9) { - return 0; - } else if (fhdr->magic == 0xEA && fhdr->blocks == 0x04) { - bhdr = (struct block_hdr*)(buf + sizeof(struct flash_hdr)); - return bhdr->data_len; - } else { - ets_printf("error magic!\n"); - return -1; - } -} - -// 0---OK, 1---FAIL -char jump_to_run_addr(unsigned int bin_addr) -{ - unsigned int flash_addr; - char ret = 1; - char(*jump_to_load_flash_code)(unsigned int addr); - - ets_printf(" @ %x\n\n", bin_addr); - - jump_to_load_flash_code = (void*)(0x4010FC08); - flash_addr = get_flash_bin_addr(bin_addr); - - if (flash_addr != -1) { - if (flash_addr == 0) { - ret = jump_to_load_flash_code(bin_addr); - } else { - ret = jump_to_load_flash_code(bin_addr + 16 + flash_addr); - } - } - - return ret; -} - -unsigned int gen_bin_addr(unsigned char* buf) -{ - unsigned int ret; - - ret = buf[2] << 16 | buf[1] << 8 | buf[0]; - - return ret; -} +static esp_err_t select_image (esp_image_metadata_t *image_data); +static int selected_boot_partition(const bootloader_state_t *bs); void call_start_cpu(void) { - struct save_hdr shdr; - struct boot_hdr bhdr; - struct flash_hdr fhdr; - - unsigned int sys_start; - - ets_printf("\n2nd boot version : 2.0\n"); - - SPIRead(0, (unsigned int*)&fhdr, sizeof(struct flash_hdr)); - - BDEBUG("[D]: magic %02x\n", fhdr.magic); - BDEBUG("[D]: blocks %02x\n", fhdr.blocks); - BDEBUG("[D]: spi_mode %02x\n", fhdr.spi_mode); - BDEBUG("[D]: spi_speed %02x\n", fhdr.spi_speed); - BDEBUG("[D]: spi_size_map %02x\n", fhdr.spi_size_map); - - ets_printf(" SPI Speed : "); - - switch (fhdr.spi_speed) { - case SPI_SPEED_40M: - ets_printf("40MHz\n"); - break; - - case SPI_SPEED_26M: - ets_printf("26.7MHz\n"); - break; - - case SPI_SPEED_20M: - ets_printf("20MHz\n"); - break; - - case SPI_SPEED_80M: - ets_printf("80MHz\n"); - break; + // 1. Hardware initialization + if(bootloader_init() != ESP_OK){ + return; } - ets_printf(" SPI Mode : "); - - switch (fhdr.spi_mode) { - case SPI_MODE_QIO: - ets_printf("QIO\n"); - break; - - case SPI_MODE_QOUT: - ets_printf("QOUT\n"); - break; - - case SPI_MODE_DIO: - ets_printf("DIO\n"); - break; - - case SPI_MODE_DOUT: - ets_printf("DOUT\n"); - break; - - default: - fhdr.spi_mode = SPI_MODE_QIO; - ets_printf("QIO\n"); - break; + // 2. Select image to boot + esp_image_metadata_t image_data; + if(select_image(&image_data) != ESP_OK){ + return; } - ets_printf(" SPI Flash Size & Map: "); - - switch (fhdr.spi_size_map) { - case SPI_SIZE_4M_256_256: - sys_start = 124; - ets_printf("4Mbit(256KB+256KB)\n"); - break; - - case SPI_SIZE_2M: - sys_start = 60; - ets_printf("2Mbit\n"); - break; - - case SPI_SIZE_8M_512_512: - sys_start = 252; - ets_printf("8Mbit(512KB+512KB)\n"); - break; - - case SPI_SIZE_16M_512_512: - sys_start = 508; - ets_printf("16Mbit(512KB+512KB)\n"); - break; - - case SPI_SIZE_32M_512_512: - sys_start = 1020; - ets_printf("32Mbit(512KB+512KB)\n"); - break; - - case SPI_SIZE_16M_1024_1024: - sys_start = 508; - ets_printf("16Mbit(1024KB+1024KB)\n"); - break; - - case SPI_SIZE_32M_1024_1024: - sys_start = 1020; - ets_printf("32Mbit(1024KB+1024KB)\n"); - break; - - default: - sys_start = 124; - ets_printf("4Mbit\n"); - break; - } - - SPIRead((sys_start + WIFI_PARAM_FLAG) * SPI_SEC_SIZE, - (unsigned int*)&shdr, sizeof(struct save_hdr)); - - SPIRead((sys_start + ((shdr.flag == 0) ? WIFI_PARAM_SAVE_0 : WIFI_PARAM_SAVE_1)) * SPI_SEC_SIZE, - (unsigned int*)&bhdr, sizeof(struct boot_hdr)); - - BDEBUG("[D]: use_bin %02x\n", bhdr.use_bin); - BDEBUG("[D]: boot_status %02x\n", bhdr.boot_status); - BDEBUG("[D]: reverse %02x\n", bhdr.reverse); - BDEBUG("[D]: version %02x\n", bhdr.version); - BDEBUG("[D]: test_pass_flag %02x\n", bhdr.test_pass_flag); - BDEBUG("[D]: test_start_flag %02x\n", bhdr.test_start_flag); - BDEBUG("[D]: enhance_boot_flag %02x\n", bhdr.enhance_boot_flag); - BDEBUG("[D]: test_bin_addr %02x %02x %02x\n", bhdr.test_bin_addr[0], bhdr.test_bin_addr[1], bhdr.test_bin_addr[2]); - BDEBUG("[D]: user_bin_addr %02x %02x %02x\n", bhdr.user_bin_addr[0], bhdr.user_bin_addr[1], bhdr.user_bin_addr[2]); - - ets_memcpy((void *)0x4010FC00, load_bin, load_bin_len); - - ets_printf("jump to run"); - - jump_to_run_addr(CONFIG_PARTITION_TABLE_CUSTOM_APP_BIN_OFFSET); + // 3. Loading the selected image +// bootloader_utility_load_image(&image_data); +} + +// Selects image to boot +static esp_err_t select_image (esp_image_metadata_t *image_data) +{ + return ESP_OK; +} + +/* + * Selects a boot partition. + * The conditions for switching to another firmware are checked. + */ +static int selected_boot_partition(const bootloader_state_t *bs) +{ + int boot_index = 1; //bootloader_utility_get_selected_boot_partition(bs); + + return boot_index; } diff --git a/components/bootloader/subproject/main/load_flash_bin.h b/components/bootloader/subproject/main/load_flash_bin.h deleted file mode 100644 index 8317f42f..00000000 --- a/components/bootloader/subproject/main/load_flash_bin.h +++ /dev/null @@ -1,68 +0,0 @@ -unsigned char load_bin[] = { - 0x1c, 0x4b, 0x00, 0x40, 0xb4, 0x18, 0x00, 0x40, 0x1c, 0x04, 0x12, 0xc1, - 0xa0, 0xd9, 0xd1, 0xe9, 0xe1, 0xf9, 0xf1, 0x3d, 0x01, 0xc9, 0xc1, 0x09, - 0xb1, 0x01, 0xf9, 0xff, 0xcd, 0x02, 0xc0, 0x00, 0x00, 0x56, 0x72, 0x1c, - 0x32, 0xa0, 0xe9, 0x22, 0x01, 0x00, 0xf8, 0x11, 0x30, 0x22, 0xc0, 0x32, - 0x01, 0x01, 0x39, 0xa1, 0x56, 0x42, 0x1b, 0x3d, 0x01, 0x1c, 0x04, 0x02, - 0x01, 0x08, 0x22, 0x01, 0x0a, 0xe2, 0x01, 0x0b, 0xd2, 0x01, 0x0f, 0x80, - 0xee, 0x11, 0x80, 0xdd, 0x11, 0x20, 0xee, 0x20, 0x22, 0x01, 0x09, 0x80, - 0xee, 0x11, 0x20, 0xee, 0x20, 0x22, 0x01, 0x0e, 0x80, 0xee, 0x11, 0x00, - 0xee, 0x20, 0x20, 0xdd, 0x20, 0x02, 0x01, 0x0c, 0x22, 0x01, 0x0d, 0x80, - 0xdd, 0x11, 0x20, 0xdd, 0x20, 0x80, 0xdd, 0x11, 0x00, 0xdd, 0x20, 0x01, - 0xe2, 0xff, 0x22, 0xcc, 0x10, 0xc0, 0x00, 0x00, 0x56, 0x82, 0x16, 0xf9, - 0x91, 0xe2, 0x61, 0x13, 0x22, 0xcc, 0x20, 0x08, 0xa1, 0x22, 0x61, 0x12, - 0x16, 0x50, 0x21, 0xfd, 0x01, 0xc2, 0xa0, 0xef, 0x1c, 0x0a, 0xd2, 0x61, - 0x14, 0x2d, 0x0e, 0x0c, 0x07, 0x72, 0x61, 0x11, 0xed, 0x01, 0x0d, 0x02, - 0x0c, 0x17, 0x62, 0x21, 0x11, 0x82, 0x21, 0x14, 0x60, 0x6a, 0xc0, 0x60, - 0x60, 0x74, 0x67, 0xb8, 0x01, 0x0c, 0x07, 0x16, 0x47, 0x1f, 0x0c, 0x39, - 0x67, 0x09, 0x1c, 0xa6, 0x16, 0x4f, 0x0c, 0x0d, 0x1b, 0xdd, 0x22, 0x0f, - 0x00, 0x22, 0x40, 0x00, 0x1b, 0xff, 0x1b, 0x00, 0xc0, 0xc2, 0x30, 0xc0, - 0xc0, 0x74, 0x67, 0x9d, 0xea, 0x46, 0x0d, 0x00, 0xbc, 0x26, 0x2d, 0x00, - 0x60, 0x92, 0x41, 0xa6, 0x19, 0x29, 0x0c, 0x0d, 0x1b, 0xdd, 0x42, 0x0f, - 0x01, 0x02, 0x0f, 0x03, 0x32, 0x0f, 0x00, 0xc0, 0x00, 0x30, 0xc2, 0x0f, - 0x02, 0x40, 0x33, 0x30, 0x30, 0xcc, 0x30, 0x00, 0xcc, 0x30, 0xc0, 0xc0, - 0x74, 0x08, 0x0f, 0x09, 0x02, 0x4b, 0xff, 0x4b, 0x22, 0x97, 0x9d, 0xd7, - 0x0d, 0x02, 0x16, 0x97, 0x19, 0x60, 0x28, 0xc0, 0x20, 0x24, 0x41, 0x22, - 0x61, 0x10, 0x79, 0x71, 0x32, 0x21, 0x10, 0x42, 0x21, 0x11, 0x0c, 0x05, - 0x4a, 0x68, 0x40, 0x40, 0x74, 0x62, 0xc6, 0xf0, 0x60, 0x60, 0x34, 0x70, - 0x45, 0x93, 0x42, 0x61, 0x11, 0x70, 0x68, 0x83, 0x69, 0x81, 0xa6, 0x13, - 0x4c, 0x0c, 0x0d, 0x09, 0x61, 0x22, 0x21, 0x12, 0x3d, 0x01, 0x01, 0xac, - 0xff, 0x1c, 0x04, 0xc0, 0x00, 0x00, 0x56, 0x22, 0x09, 0x22, 0x21, 0x12, - 0x0c, 0x00, 0x22, 0xc2, 0x10, 0x22, 0x61, 0x12, 0x1b, 0x30, 0xea, 0x20, - 0x22, 0x02, 0x00, 0x30, 0x00, 0x74, 0xc0, 0xc2, 0x30, 0xc0, 0xc0, 0x74, - 0x66, 0xb0, 0xec, 0x28, 0x61, 0x3d, 0x01, 0x01, 0xa2, 0xff, 0x1c, 0x04, - 0xc0, 0x00, 0x00, 0x1b, 0xdd, 0x08, 0x61, 0x22, 0x21, 0x10, 0x02, 0xc0, - 0x10, 0x09, 0x61, 0xd7, 0x92, 0xb6, 0x38, 0x71, 0x09, 0x61, 0x9c, 0xb3, - 0x22, 0x21, 0x12, 0x3d, 0x01, 0x01, 0x98, 0xff, 0x1c, 0x04, 0xc0, 0x00, - 0x00, 0x08, 0x61, 0x56, 0x12, 0x04, 0x22, 0x21, 0x12, 0xfd, 0x01, 0x22, - 0xc2, 0x10, 0x22, 0x61, 0x12, 0x68, 0x81, 0x0c, 0x33, 0x60, 0x60, 0x74, - 0x67, 0x83, 0x02, 0x06, 0x3e, 0x00, 0xa6, 0x16, 0x16, 0x0c, 0x0d, 0x1b, - 0xdd, 0x22, 0x0f, 0x00, 0x22, 0x40, 0x00, 0x1b, 0xff, 0x1b, 0x00, 0xc0, - 0xc2, 0x30, 0xc0, 0xc0, 0x74, 0x67, 0x9d, 0xea, 0x38, 0xa1, 0x1c, 0x0a, - 0x66, 0x13, 0x19, 0x42, 0x01, 0x0f, 0xc0, 0x44, 0xc0, 0x16, 0xc4, 0x0b, - 0x0c, 0x12, 0xc8, 0xc1, 0xd8, 0xd1, 0xe8, 0xe1, 0xf8, 0xf1, 0x08, 0xb1, - 0x12, 0xc1, 0x60, 0x0d, 0xf0, 0x02, 0x21, 0x11, 0x0a, 0x06, 0x00, 0xda, - 0xc0, 0xd0, 0xd0, 0x74, 0xb6, 0x8d, 0x46, 0x92, 0x0f, 0x06, 0x32, 0x0f, - 0x02, 0x82, 0x0f, 0x07, 0x22, 0x0f, 0x03, 0x80, 0x88, 0x11, 0x80, 0x22, - 0x11, 0x30, 0x22, 0x20, 0x90, 0x88, 0x20, 0x32, 0x0f, 0x01, 0x80, 0x22, - 0x11, 0x92, 0x0f, 0x05, 0x80, 0x88, 0x11, 0x90, 0x88, 0x20, 0x30, 0x22, - 0x20, 0x92, 0x0f, 0x04, 0x80, 0x88, 0x11, 0x32, 0x0f, 0x00, 0x80, 0x22, - 0x11, 0x30, 0x22, 0x20, 0x90, 0x88, 0x20, 0x8b, 0x30, 0x30, 0x30, 0x74, - 0x32, 0x61, 0x11, 0x06, 0x10, 0x00, 0x22, 0xc1, 0x10, 0xea, 0x30, 0x01, - 0x6b, 0xff, 0x4d, 0x0d, 0xc0, 0x00, 0x00, 0x22, 0x21, 0x12, 0x3d, 0x01, - 0x01, 0x67, 0xff, 0x1c, 0x04, 0xc0, 0x00, 0x00, 0xfc, 0x92, 0x01, 0x65, - 0xff, 0x32, 0x21, 0x12, 0x0c, 0x84, 0x22, 0xc1, 0x10, 0x2a, 0x2d, 0xd0, - 0x44, 0xc0, 0x40, 0x40, 0x74, 0x32, 0xc3, 0x10, 0x32, 0x61, 0x12, 0x42, - 0x61, 0x11, 0x3d, 0x01, 0xc0, 0x00, 0x00, 0x28, 0x41, 0x88, 0x51, 0x1c, - 0x0a, 0x82, 0x61, 0x14, 0xf2, 0x21, 0x11, 0x48, 0xa1, 0xea, 0xff, 0x0b, - 0x04, 0x00, 0x40, 0x74, 0x49, 0xa1, 0x56, 0xc4, 0xdf, 0x08, 0x91, 0xc0, - 0x00, 0x00, 0x0c, 0x02, 0x86, 0xce, 0xff, 0x0c, 0x02, 0x22, 0x61, 0x10, - 0x86, 0x99, 0xff, 0x16, 0xd6, 0xf1, 0x2d, 0x00, 0x60, 0x72, 0x41, 0xe6, - 0x17, 0x02, 0x86, 0xc4, 0xff, 0x0c, 0x0d, 0x1b, 0xdd, 0x42, 0x0f, 0x01, - 0x02, 0x0f, 0x03, 0x32, 0x0f, 0x00, 0xc0, 0x00, 0x30, 0xc2, 0x0f, 0x02, - 0x40, 0x33, 0x30, 0x30, 0xcc, 0x30, 0x00, 0xcc, 0x30, 0xc0, 0xc0, 0x74, - 0x08, 0x0f, 0x09, 0x02, 0x4b, 0xff, 0x4b, 0x22, 0x77, 0x9d, 0xd7, 0x46, - 0xb9, 0xff, 0x00, 0x00 -}; - -unsigned int load_bin_len = 760; diff --git a/components/bootloader_support/include/esp_flash_partitions.h b/components/bootloader_support/include/esp_flash_partitions.h index 843e5a28..ee413101 100644 --- a/components/bootloader_support/include/esp_flash_partitions.h +++ b/components/bootloader_support/include/esp_flash_partitions.h @@ -18,11 +18,21 @@ #include "esp_flash_data_types.h" #include +#include "sdkconfig.h" + /* Pre-partition table fixed flash offsets */ #define ESP_BOOTLOADER_DIGEST_OFFSET 0x0 + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 #define ESP_BOOTLOADER_OFFSET 0x1000 /* Offset of bootloader image. Has matching value in bootloader KConfig.projbuild file. */ +#endif + +#ifdef CONFIG_TARGET_PLATFORM_ESP8266 +#define ESP_BOOTLOADER_OFFSET 0x0000 /* Offset of bootloader image. Has matching value in bootloader KConfig.projbuild file. */ +#endif + #define ESP_BOOTLOADER_SIZE (ESP_PARTITION_TABLE_OFFSET - ESP_BOOTLOADER_OFFSET) -#define ESP_PARTITION_TABLE_OFFSET 0x8000 /* Offset of partition table. Has matching value in partition_table Kconfig.projbuild file. */ +#define ESP_PARTITION_TABLE_OFFSET CONFIG_PARTITION_TABLE_OFFSET /* Offset of partition table. Has matching value in partition_table Kconfig.projbuild file. */ #define ESP_PARTITION_TABLE_MAX_LEN 0xC00 /* Maximum length of partition table data */ #define ESP_PARTITION_TABLE_MAX_ENTRIES (ESP_PARTITION_TABLE_MAX_LEN / sizeof(esp_partition_info_t)) /* Maximum length of partition table data, including terminating entry */ diff --git a/components/bootloader_support/include/esp_image_format.h b/components/bootloader_support/include/esp_image_format.h index d2dcfd31..c8be9f66 100644 --- a/components/bootloader_support/include/esp_image_format.h +++ b/components/bootloader_support/include/esp_image_format.h @@ -16,6 +16,7 @@ #include #include #include "esp_flash_partitions.h" +#include "sdkconfig.h" #define ESP_ERR_IMAGE_BASE 0x2000 #define ESP_ERR_IMAGE_FLASH_FAIL (ESP_ERR_IMAGE_BASE + 1) @@ -43,6 +44,7 @@ enum { ESP_IMAGE_SPI_SPEED_80M = 0xF } esp_image_spi_freq_t; +#ifdef CONFIG_TARGET_PLATFORM_ESP32 /* Supported SPI flash sizes */ typedef enum { ESP_IMAGE_FLASH_SIZE_1MB = 0, @@ -52,6 +54,24 @@ typedef enum { ESP_IMAGE_FLASH_SIZE_16MB, ESP_IMAGE_FLASH_SIZE_MAX } esp_image_flash_size_t; +#endif + +#ifdef CONFIG_TARGET_PLATFORM_ESP8266 +/* Supported SPI flash sizes */ +typedef enum { + ESP_IMAGE_FLASH_SIZE_512KB = 0, + ESP_IMAGE_FLASH_SIZE_256KB, + ESP_IMAGE_FLASH_SIZE_1MB, + ESP_IMAGE_FLASH_SIZE_2MB, + ESP_IMAGE_FLASH_SIZE_4MB, + ESP_IMAGE_FLASH_SIZE_2MB_C1, + ESP_IMAGE_FLASH_SIZE_4MB_C1, + ESP_IMAGE_FLASH_SIZE_8MB = 8, + ESP_IMAGE_FLASH_SIZE_16MB, + ESP_IMAGE_FLASH_SIZE_MAX +} esp_image_flash_size_t; + +#endif #define ESP_IMAGE_HEADER_MAGIC 0xE9 @@ -66,6 +86,8 @@ typedef struct { /* flash chip size (esp_image_flash_size_t as uint8_t) */ uint8_t spi_size: 4; uint32_t entry_addr; + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 /* WP pin when SPI pins set via efuse (read by ROM bootloader, the IDF bootloader uses software to configure the WP * pin and sets this field to 0xEE=disabled) */ uint8_t wp_pin; @@ -77,9 +99,16 @@ typedef struct { * is separate to secure boot and only used for detecting corruption. For secure boot signed images, the signature * is appended after this (and the simple hash is included in the signed data). */ uint8_t hash_appended; +#endif } __attribute__((packed)) esp_image_header_t; +#ifdef CONFIG_TARGET_PLATFORM_ESP32 _Static_assert(sizeof(esp_image_header_t) == 24, "binary image header should be 24 bytes"); +#endif + +#ifdef CONFIG_TARGET_PLATFORM_ESP8266 +_Static_assert(sizeof(esp_image_header_t) == 8, "binary image header should be 8 bytes"); +#endif /* Header of binary image segment */ typedef struct { diff --git a/components/bootloader_support/include_priv/bootloader_config.h b/components/bootloader_support/include_priv/bootloader_config.h index d6ec5da1..dc5c6a6b 100644 --- a/components/bootloader_support/include_priv/bootloader_config.h +++ b/components/bootloader_support/include_priv/bootloader_config.h @@ -15,6 +15,7 @@ #define __BOOT_CONFIG_H__ #include +#include #ifdef __cplusplus extern "C" @@ -22,7 +23,6 @@ extern "C" #endif #include "esp_flash_data_types.h" -#include "soc/soc.h" #define SPI_SEC_SIZE 0x1000 diff --git a/components/bootloader_support/src/bootloader_clock.c b/components/bootloader_support/src/bootloader_clock.c index 64ef9c23..ecc77b40 100644 --- a/components/bootloader_support/src/bootloader_clock.c +++ b/components/bootloader_support/src/bootloader_clock.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include "rom/uart.h" #include "rom/rtc.h" #include "soc/soc.h" @@ -59,3 +64,4 @@ void bootloader_clock_configure() } #endif } +#endif diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index 0d72c3e1..773089a4 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -11,10 +11,14 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include #include "string.h" -#include "sdkconfig.h" #include "esp_err.h" #include "esp_log.h" #include "rom/spi_flash.h" @@ -153,3 +157,5 @@ bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_dat return ret; } + +#endif diff --git a/components/bootloader_support/src/bootloader_flash.c b/components/bootloader_support/src/bootloader_flash.c index a7a6f6e6..dfe15e3b 100644 --- a/components/bootloader_support/src/bootloader_flash.c +++ b/components/bootloader_support/src/bootloader_flash.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include @@ -248,3 +253,80 @@ esp_err_t bootloader_flash_erase_sector(size_t sector) } #endif + +#endif + +#ifdef CONFIG_TARGET_PLATFORM_ESP8266 + +#include +#include + +#include "esp_err.h" +#include "esp_log.h" + +static const char *TAG = "bootloader_flash"; + +typedef enum { SPI_FLASH_RESULT_OK = 0, + SPI_FLASH_RESULT_ERR = 1, + SPI_FLASH_RESULT_TIMEOUT = 2 } SpiFlashOpResult; + +SpiFlashOpResult SPIRead(uint32_t addr, void *dst, uint32_t size); +SpiFlashOpResult SPIWrite(uint32_t addr, const uint8_t *src, uint32_t size); +SpiFlashOpResult SPIEraseSector(uint32_t sector_num); + +static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size) +{ + SPIRead(src_addr, dest, size); + + return ESP_OK; +} + +esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt) +{ + if (src_addr & 3) { + ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr); + return ESP_FAIL; + } + if (size & 3) { + ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size); + return ESP_FAIL; + } + if ((intptr_t)dest & 3) { + ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest); + return ESP_FAIL; + } + + return bootloader_flash_read_no_decrypt(src_addr, dest, size); +} + +esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted) +{ + esp_err_t err; + size_t alignment = write_encrypted ? 32 : 4; + if ((dest_addr % alignment) != 0) { + ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment); + return ESP_FAIL; + } + if ((size % alignment) != 0) { + ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment); + return ESP_FAIL; + } + if (((intptr_t)src % 4) != 0) { + ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src); + return ESP_FAIL; + } + + + SPIWrite(dest_addr, src, size); + + return ESP_OK; +} + +esp_err_t bootloader_flash_erase_sector(size_t sector) +{ + SPIEraseSector(sector); + + return ESP_OK; +} + +#endif diff --git a/components/bootloader_support/src/bootloader_init.c b/components/bootloader_support/src/bootloader_init.c index ab017fae..c867be74 100644 --- a/components/bootloader_support/src/bootloader_init.c +++ b/components/bootloader_support/src/bootloader_init.c @@ -11,10 +11,15 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include -//#include -//#include +#include +#include #include "esp_attr.h" #include "esp_log.h" @@ -528,3 +533,164 @@ void __assert_func(const char *file, int line, const char *func, const char *exp ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr); while(1) {} } + +#endif + +#ifdef CONFIG_TARGET_PLATFORM_ESP8266 + +#include + +#include "esp_err.h" +#include "esp_log.h" + +#include "esp_image_format.h" +#include "esp_flash_partitions.h" + +extern int _bss_start; +extern int _bss_end; +extern int _data_start; +extern int _data_end; + +static const char* TAG = "boot"; + +static esp_err_t bootloader_main(); +static void print_flash_info(const esp_image_header_t* pfhdr); +static void update_flash_config(const esp_image_header_t* pfhdr); + +esp_err_t bootloader_init() +{ + //Clear bss + memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start)); + + if(bootloader_main() != ESP_OK){ + return ESP_FAIL; + } + return ESP_OK; +} + +static esp_err_t bootloader_main() +{ + esp_image_header_t fhdr; + if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &fhdr, sizeof(esp_image_header_t), true) != ESP_OK) { + ESP_LOGE(TAG, "failed to load bootloader header!"); + return ESP_FAIL; + } + + ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER); + + ESP_LOGI(TAG, "compile time " __TIME__ ); + + print_flash_info(&fhdr); + + update_flash_config(&fhdr); + + return ESP_OK; +} + +static void update_flash_config(const esp_image_header_t* pfhdr) +{ + uint32_t size; + switch(pfhdr->spi_size) { + case ESP_IMAGE_FLASH_SIZE_1MB: + size = 1; + break; + case ESP_IMAGE_FLASH_SIZE_2MB: + case ESP_IMAGE_FLASH_SIZE_2MB_C1: + size = 2; + break; + case ESP_IMAGE_FLASH_SIZE_4MB: + case ESP_IMAGE_FLASH_SIZE_4MB_C1: + size = 4; + break; + case ESP_IMAGE_FLASH_SIZE_8MB: + size = 8; + break; + case ESP_IMAGE_FLASH_SIZE_16MB: + size = 16; + break; + default: + size = 2; + } + + // Set flash chip size +// esp_rom_spiflash_config_param(g_rom_flashchip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff); + // TODO: set mode + // TODO: set frequency +} + +static void print_flash_info(const esp_image_header_t* phdr) +{ +#if (BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_NOTICE) + + ESP_LOGD(TAG, "magic %02x", phdr->magic ); + ESP_LOGD(TAG, "segments %02x", phdr->segment_count ); + ESP_LOGD(TAG, "spi_mode %02x", phdr->spi_mode ); + ESP_LOGD(TAG, "spi_speed %02x", phdr->spi_speed ); + ESP_LOGD(TAG, "spi_size %02x", phdr->spi_size ); + + const char* str; + switch ( phdr->spi_speed ) { + case ESP_IMAGE_SPI_SPEED_40M: + str = "40MHz"; + break; + case ESP_IMAGE_SPI_SPEED_26M: + str = "26.7MHz"; + break; + case ESP_IMAGE_SPI_SPEED_20M: + str = "20MHz"; + break; + case ESP_IMAGE_SPI_SPEED_80M: + str = "80MHz"; + break; + default: + str = "20MHz"; + break; + } + ESP_LOGI(TAG, "SPI Speed : %s", str ); + + switch ( phdr->spi_mode ) { + case ESP_IMAGE_SPI_MODE_QIO: + str = "QIO"; + break; + case ESP_IMAGE_SPI_MODE_QOUT: + str = "QOUT"; + break; + case ESP_IMAGE_SPI_MODE_DIO: + str = "DIO"; + break; + case ESP_IMAGE_SPI_MODE_DOUT: + str = "DOUT"; + break; + default: + str = "QIO"; + break; + } + ESP_LOGI(TAG, "SPI Mode : %s", str ); + + switch ( phdr->spi_size ) { + case ESP_IMAGE_FLASH_SIZE_1MB: + str = "1MB"; + break; + case ESP_IMAGE_FLASH_SIZE_2MB: + case ESP_IMAGE_FLASH_SIZE_2MB_C1: + str = "2MB"; + break; + case ESP_IMAGE_FLASH_SIZE_4MB: + case ESP_IMAGE_FLASH_SIZE_4MB_C1: + str = "4MB"; + break; + case ESP_IMAGE_FLASH_SIZE_8MB: + str = "8MB"; + break; + case ESP_IMAGE_FLASH_SIZE_16MB: + str = "16MB"; + break; + default: + str = "2MB"; + break; + } + ESP_LOGI(TAG, "SPI Flash Size : %s", str ); +#endif +} + +#endif diff --git a/components/bootloader_support/src/bootloader_random.c b/components/bootloader_support/src/bootloader_random.c index 36628e6c..bf75c8d7 100644 --- a/components/bootloader_support/src/bootloader_random.c +++ b/components/bootloader_support/src/bootloader_random.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include "bootloader_random.h" #include "soc/cpu.h" #include "soc/wdev_reg.h" @@ -143,3 +148,5 @@ void bootloader_random_disable(void) CLEAR_PERI_REG_MASK(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_ENT_RTC); SET_PERI_REG_BITS(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_DTEST_RTC, 0, RTC_CNTL_DTEST_RTC_S); } + +#endif diff --git a/components/bootloader_support/src/bootloader_sha.c b/components/bootloader_support/src/bootloader_sha.c index be23f7f2..8d7e5dca 100644 --- a/components/bootloader_support/src/bootloader_sha.c +++ b/components/bootloader_support/src/bootloader_sha.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include "bootloader_sha.h" #include #include @@ -164,3 +169,5 @@ void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest } #endif + +#endif diff --git a/components/bootloader_support/src/bootloader_utility.c b/components/bootloader_support/src/bootloader_utility.c index eeaee217..a806d1f8 100644 --- a/components/bootloader_support/src/bootloader_utility.c +++ b/components/bootloader_support/src/bootloader_utility.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include #include @@ -470,3 +475,5 @@ static void set_cache_and_start_app( // use "movsp" instruction to reset stack back to where ROM stack starts. (*entry)(); } + +#endif diff --git a/components/bootloader_support/src/efuse.c b/components/bootloader_support/src/efuse.c index 40bb6d45..c3e23b1b 100644 --- a/components/bootloader_support/src/efuse.c +++ b/components/bootloader_support/src/efuse.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include "esp_efuse.h" #include "esp_log.h" @@ -58,3 +63,5 @@ void esp_efuse_disable_basic_rom_console(void) esp_efuse_burn_new_values(); } } + +#endif diff --git a/components/bootloader_support/src/esp_image_format.c b/components/bootloader_support/src/esp_image_format.c index 92acf3b0..c5345c49 100644 --- a/components/bootloader_support/src/esp_image_format.c +++ b/components/bootloader_support/src/esp_image_format.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include @@ -571,3 +576,5 @@ static void debug_log_hash(const uint8_t *image_hash, const char *label) ESP_LOGD(TAG, "%s: %s", label, hash_print); #endif } + +#endif diff --git a/components/bootloader_support/src/flash_encrypt.c b/components/bootloader_support/src/flash_encrypt.c index 290a02a9..8d21c37d 100644 --- a/components/bootloader_support/src/flash_encrypt.c +++ b/components/bootloader_support/src/flash_encrypt.c @@ -12,6 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include "bootloader_flash.h" @@ -337,3 +341,5 @@ esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length) ESP_LOGE(TAG, "flash operation failed: 0x%x", err); return err; } + +#endif diff --git a/components/bootloader_support/src/flash_partitions.c b/components/bootloader_support/src/flash_partitions.c index f8a24f26..69f7bafe 100644 --- a/components/bootloader_support/src/flash_partitions.c +++ b/components/bootloader_support/src/flash_partitions.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include "esp_flash_partitions.h" #include "esp_log.h" @@ -84,3 +89,5 @@ esp_err_t esp_partition_table_basic_verify(const esp_partition_info_t *partition return ESP_ERR_INVALID_STATE; } +#endif + diff --git a/components/bootloader_support/src/flash_qio_mode.c b/components/bootloader_support/src/flash_qio_mode.c index 361e3d4a..571515b5 100644 --- a/components/bootloader_support/src/flash_qio_mode.c +++ b/components/bootloader_support/src/flash_qio_mode.c @@ -11,6 +11,11 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. + +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include #include "flash_qio_mode.h" @@ -280,3 +285,5 @@ static uint32_t execute_flash_command(uint8_t command, uint32_t mosi_data, uint8 SPIFLASH.ctrl.val = old_ctrl_reg; return SPIFLASH.data_buf[0]; } + +#endif diff --git a/components/bootloader_support/src/secure_boot.c b/components/bootloader_support/src/secure_boot.c index ef9744ff..8eb26ec5 100644 --- a/components/bootloader_support/src/secure_boot.c +++ b/components/bootloader_support/src/secure_boot.c @@ -12,6 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include "sdkconfig.h" + +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include #include "esp_attr.h" @@ -202,3 +206,5 @@ esp_err_t esp_secure_boot_permanently_enable(void) { return ESP_ERR_INVALID_STATE; } } + +#endif diff --git a/components/bootloader_support/src/secure_boot_signatures.c b/components/bootloader_support/src/secure_boot_signatures.c index 988ab793..4a216a36 100644 --- a/components/bootloader_support/src/secure_boot_signatures.c +++ b/components/bootloader_support/src/secure_boot_signatures.c @@ -13,6 +13,8 @@ // limitations under the License. #include "sdkconfig.h" +#ifdef CONFIG_TARGET_PLATFORM_ESP32 + #include "bootloader_flash.h" #include "bootloader_sha.h" #include "esp_log.h" @@ -91,3 +93,5 @@ esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block uECC_secp256r1()); return is_valid ? ESP_OK : ESP_ERR_IMAGE_INVALID; } + +#endif diff --git a/components/spi_flash/component.mk b/components/spi_flash/component.mk index b2dceefc..6ec0f676 100644 --- a/components/spi_flash/component.mk +++ b/components/spi_flash/component.mk @@ -2,6 +2,8 @@ # Component Makefile # +ifndef IS_BOOTLOADER_BUILD COMPONENT_SRCDIRS := src +endif CFLAGS += -DPARTITION_QUEUE_HEADER=\"sys/queue.h\"