diff --git a/components/bootloader/subproject/main/ld/esp32/bootloader.ld b/components/bootloader/subproject/main/ld/esp32/bootloader.ld index 22a872ea37..45b0e78930 100644 --- a/components/bootloader/subproject/main/ld/esp32/bootloader.ld +++ b/components/bootloader/subproject/main/ld/esp32/bootloader.ld @@ -38,14 +38,15 @@ SECTIONS *(.iram1 .iram1.*) /* catch stray IRAM_ATTR */ *liblog.a:(.literal .text .literal.* .text.*) *libgcc.a:(.literal .text .literal.* .text.*) - *libbootloader_support.a:bootloader_clock.*(.literal .text .literal.* .text.*) - *libbootloader_support.a:bootloader_common.*(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_random.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_efuse_esp32.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_sha.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_console_loader.*(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_panic.*(.literal .text .literal.* .text.*) *libbootloader_support.a:esp_image_format.*(.literal .text .literal.* .text.*) *libbootloader_support.a:flash_encrypt.*(.literal .text .literal.* .text.*) *libbootloader_support.a:flash_partitions.*(.literal .text .literal.* .text.*) @@ -55,6 +56,7 @@ SECTIONS *libspi_flash.a:*.*(.literal .text .literal.* .text.*) *libsoc.a:wdt_hal_iram.*(.literal .text .literal.* .text.*) *libsoc.a:rtc_clk.*(.literal .text .literal.* .text.*) + *libsoc.a:rtc_time.*(.literal .text .literal.* .text.*) *libefuse.a:*.*(.literal .text .literal.* .text.*) *(.fini.literal) *(.fini) diff --git a/components/bootloader/subproject/main/ld/esp32s2/bootloader.ld b/components/bootloader/subproject/main/ld/esp32s2/bootloader.ld index c23258298b..94945974f3 100644 --- a/components/bootloader/subproject/main/ld/esp32s2/bootloader.ld +++ b/components/bootloader/subproject/main/ld/esp32s2/bootloader.ld @@ -25,13 +25,15 @@ SECTIONS *(.iram1 .iram1.*) /* catch stray IRAM_ATTR */ *liblog.a:(.literal .text .literal.* .text.*) *libgcc.a:(.literal .text .literal.* .text.*) - *libbootloader_support.a:bootloader_common.*(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_clock_loader.*(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_common_loader.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_flash.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_random.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_efuse_esp32s2.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_utility.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_sha.*(.literal .text .literal.* .text.*) *libbootloader_support.a:bootloader_console_loader.*(.literal .text .literal.* .text.*) + *libbootloader_support.a:bootloader_panic.*(.literal .text .literal.* .text.*) *libbootloader_support.a:esp_image_format.*(.literal .text .literal.* .text.*) *libbootloader_support.a:flash_encrypt.*(.literal .text .literal.* .text.*) *libbootloader_support.a:flash_partitions.*(.literal .text .literal.* .text.*) @@ -40,6 +42,8 @@ SECTIONS *libmicro-ecc.a:*.*(.literal .text .literal.* .text.*) *libspi_flash.a:*.*(.literal .text .literal.* .text.*) *libsoc.a:wdt_hal_iram.*(.literal .text .literal.* .text.*) + *libsoc.a:rtc_clk.*(.literal .text .literal.* .text.*) + *libsoc.a:rtc_time.*(.literal .text .literal.* .text.*) *libefuse.a:*.*(.literal .text .literal.* .text.*) *(.fini.literal) *(.fini) diff --git a/components/bootloader_support/CMakeLists.txt b/components/bootloader_support/CMakeLists.txt index 1f16c3152e..f61c6081e6 100644 --- a/components/bootloader_support/CMakeLists.txt +++ b/components/bootloader_support/CMakeLists.txt @@ -1,6 +1,7 @@ set(srcs - "src/bootloader_clock.c" "src/bootloader_common.c" + "src/bootloader_common_loader.c" + "src/bootloader_clock_init.c" "src/bootloader_flash.c" "src/bootloader_mem.c" "src/bootloader_random.c" @@ -18,8 +19,10 @@ if(BOOTLOADER_BUILD) set(priv_requires micro-ecc spi_flash efuse) list(APPEND srcs "src/bootloader_init.c" + "src/bootloader_clock_loader.c" "src/bootloader_console.c" "src/bootloader_console_loader.c" + "src/bootloader_panic.c" "src/${IDF_TARGET}/bootloader_sha.c" "src/${IDF_TARGET}/flash_encrypt.c" "src/${IDF_TARGET}/bootloader_${IDF_TARGET}.c" @@ -114,3 +117,7 @@ if(CONFIG_SECURE_SIGNED_APPS AND (CONFIG_SECURE_BOOT_V1_ENABLED OR CONFIG_SECURE APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES "${secure_boot_verification_key}") endif() + +if(BOOTLOADER_BUILD) + target_link_libraries(${COMPONENT_LIB} INTERFACE "-u abort") +endif() diff --git a/components/bootloader_support/component.mk b/components/bootloader_support/component.mk index aab6890ec2..dd1c16ceb0 100644 --- a/components/bootloader_support/component.mk +++ b/components/bootloader_support/component.mk @@ -16,7 +16,11 @@ COMPONENT_SRCDIRS += src/$(IDF_TARGET) # one sub-dir per chip endif ifndef IS_BOOTLOADER_BUILD -COMPONENT_OBJEXCLUDE := src/bootloader_init.o +COMPONENT_OBJEXCLUDE := src/bootloader_init.o \ + src/bootloader_panic.o \ + src/bootloader_clock_loader.o \ + src/bootloader_console.o \ + src/bootloader_console_loader.o endif COMPONENT_OBJEXCLUDE += src/bootloader_flash_config_esp32s2.o \ diff --git a/components/bootloader_support/src/bootloader_clock.c b/components/bootloader_support/src/bootloader_clock_init.c similarity index 96% rename from components/bootloader_support/src/bootloader_clock.c rename to components/bootloader_support/src/bootloader_clock_init.c index 44ccff8ce0..9d641ddd63 100644 --- a/components/bootloader_support/src/bootloader_clock.c +++ b/components/bootloader_support/src/bootloader_clock_init.c @@ -75,11 +75,4 @@ void bootloader_clock_configure(void) #endif // CONFIG_ESP_SYSTEM_RTC_EXT_XTAL } -#ifdef BOOTLOADER_BUILD -int esp_clk_apb_freq(void) -{ - return rtc_clk_apb_freq_get(); -} - -#endif // BOOTLOADER_BUILD diff --git a/components/bootloader_support/src/bootloader_clock_loader.c b/components/bootloader_support/src/bootloader_clock_loader.c new file mode 100644 index 0000000000..f8082a95b2 --- /dev/null +++ b/components/bootloader_support/src/bootloader_clock_loader.c @@ -0,0 +1,24 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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 "soc/rtc.h" + +#ifdef BOOTLOADER_BUILD + +int esp_clk_apb_freq(void) +{ + return rtc_clk_apb_freq_get(); +} + +#endif // BOOTLOADER_BUILD diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index 78bad8dd12..641d07b3c3 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -41,21 +41,6 @@ static const char* TAG = "boot_comm"; -uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s) -{ - return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4); -} - -bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s) -{ - return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED; -} - -bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s) -{ - return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s); -} - esp_comm_gpio_hold_t bootloader_common_check_long_hold_gpio(uint32_t num_pin, uint32_t delay_sec) { esp_rom_gpio_pad_select_gpio(num_pin); @@ -190,43 +175,6 @@ esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t return bootloader_sha256_flash_contents(address, size, out_sha_256); } -int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max) -{ - if (two_otadata == NULL || valid_two_otadata == NULL) { - return -1; - } - int active_otadata = -1; - if (valid_two_otadata[0] && valid_two_otadata[1]) { - int condition = (max == true) ? MAX(two_otadata[0].ota_seq, two_otadata[1].ota_seq) : MIN(two_otadata[0].ota_seq, two_otadata[1].ota_seq); - if (condition == two_otadata[0].ota_seq) { - active_otadata = 0; - } else { - active_otadata = 1; - } - ESP_LOGD(TAG, "Both OTA copies are valid"); - } else { - for (int i = 0; i < 2; ++i) { - if (valid_two_otadata[i]) { - active_otadata = i; - ESP_LOGD(TAG, "Only otadata[%d] is valid", i); - break; - } - } - } - return active_otadata; -} - -int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata) -{ - if (two_otadata == NULL) { - return -1; - } - bool valid_two_otadata[2]; - valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]); - valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]); - return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true); -} - esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc) { if (partition == NULL || app_desc == NULL || partition->offset == 0) { @@ -265,26 +213,6 @@ void bootloader_common_vddsdio_configure(void) } -esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type) -{ - esp_err_t err = ESP_OK; - esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID; - if (chip_id != img_hdr->chip_id) { - ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id); - err = ESP_FAIL; - } - uint8_t revision = bootloader_common_get_chip_revision(); - if (revision < img_hdr->min_chip_rev) { - ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev); - err = ESP_FAIL; - } else if (revision != img_hdr->min_chip_rev) { -#ifdef BOOTLOADER_BUILD - ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev); -#endif - } - return err; -} - RESET_REASON bootloader_common_get_reset_reason(int cpu_no) { return rtc_get_reset_reason(cpu_no); diff --git a/components/bootloader_support/src/bootloader_common_loader.c b/components/bootloader_support/src/bootloader_common_loader.c new file mode 100644 index 0000000000..ea5f406895 --- /dev/null +++ b/components/bootloader_support/src/bootloader_common_loader.c @@ -0,0 +1,99 @@ +#include "string.h" +#include "sdkconfig.h" +#include "esp_err.h" +#include "esp_log.h" +#if CONFIG_IDF_TARGET_ESP32 +#include "esp32/rom/spi_flash.h" +#elif CONFIG_IDF_TARGET_ESP32S2 +#include "esp32s2/rom/spi_flash.h" +#include "esp32s2/rom/ets_sys.h" +#endif +#include "esp_rom_crc.h" +#include "esp_rom_gpio.h" +#include "esp_flash_partitions.h" +#include "bootloader_flash.h" +#include "bootloader_common.h" +#include "soc/gpio_periph.h" +#include "soc/rtc.h" +#include "soc/efuse_reg.h" +#include "hal/gpio_ll.h" +#include "esp_image_format.h" +#include "bootloader_sha.h" +#include "sys/param.h" + +#define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */ + +static const char* TAG = "boot_comm"; + +uint32_t bootloader_common_ota_select_crc(const esp_ota_select_entry_t *s) +{ + return esp_rom_crc32_le(UINT32_MAX, (uint8_t*)&s->ota_seq, 4); +} + +bool bootloader_common_ota_select_invalid(const esp_ota_select_entry_t *s) +{ + return s->ota_seq == UINT32_MAX || s->ota_state == ESP_OTA_IMG_INVALID || s->ota_state == ESP_OTA_IMG_ABORTED; +} + +bool bootloader_common_ota_select_valid(const esp_ota_select_entry_t *s) +{ + return bootloader_common_ota_select_invalid(s) == false && s->crc == bootloader_common_ota_select_crc(s); +} + +int bootloader_common_get_active_otadata(esp_ota_select_entry_t *two_otadata) +{ + if (two_otadata == NULL) { + return -1; + } + bool valid_two_otadata[2]; + valid_two_otadata[0] = bootloader_common_ota_select_valid(&two_otadata[0]); + valid_two_otadata[1] = bootloader_common_ota_select_valid(&two_otadata[1]); + return bootloader_common_select_otadata(two_otadata, valid_two_otadata, true); +} + +esp_err_t bootloader_common_check_chip_validity(const esp_image_header_t* img_hdr, esp_image_type type) +{ + esp_err_t err = ESP_OK; + esp_chip_id_t chip_id = CONFIG_IDF_FIRMWARE_CHIP_ID; + if (chip_id != img_hdr->chip_id) { + ESP_LOGE(TAG, "mismatch chip ID, expected %d, found %d", chip_id, img_hdr->chip_id); + err = ESP_FAIL; + } + uint8_t revision = bootloader_common_get_chip_revision(); + if (revision < img_hdr->min_chip_rev) { + ESP_LOGE(TAG, "can't run on lower chip revision, expected %d, found %d", revision, img_hdr->min_chip_rev); + err = ESP_FAIL; + } else if (revision != img_hdr->min_chip_rev) { +#ifdef BOOTLOADER_BUILD + ESP_LOGI(TAG, "chip revision: %d, min. %s chip revision: %d", revision, type == ESP_IMAGE_BOOTLOADER ? "bootloader" : "application", img_hdr->min_chip_rev); +#endif + } + return err; +} + +int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, bool *valid_two_otadata, bool max) +{ + if (two_otadata == NULL || valid_two_otadata == NULL) { + return -1; + } + int active_otadata = -1; + if (valid_two_otadata[0] && valid_two_otadata[1]) { + int condition = (max == true) ? MAX(two_otadata[0].ota_seq, two_otadata[1].ota_seq) : MIN(two_otadata[0].ota_seq, two_otadata[1].ota_seq); + if (condition == two_otadata[0].ota_seq) { + active_otadata = 0; + } else { + active_otadata = 1; + } + ESP_LOGD(TAG, "Both OTA copies are valid"); + } else { + for (int i = 0; i < 2; ++i) { + if (valid_two_otadata[i]) { + active_otadata = i; + ESP_LOGD(TAG, "Only otadata[%d] is valid", i); + break; + } + } + } + return active_otadata; +} + diff --git a/components/bootloader_support/src/bootloader_init.c b/components/bootloader_support/src/bootloader_init.c index 4c57e848ea..8938dc7571 100644 --- a/components/bootloader_support/src/bootloader_init.c +++ b/components/bootloader_support/src/bootloader_init.c @@ -101,10 +101,3 @@ void bootloader_print_banner(void) ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER); ESP_LOGI(TAG, "compile time " __TIME__); } - -void __assert_func(const char *file, int line, const char *func, const char *expr) -{ - ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr); - while (1) { - } -} diff --git a/components/bootloader_support/src/bootloader_panic.c b/components/bootloader_support/src/bootloader_panic.c new file mode 100644 index 0000000000..637f2c968c --- /dev/null +++ b/components/bootloader_support/src/bootloader_panic.c @@ -0,0 +1,38 @@ +// Copyright 2020 Espressif Systems (Shanghai) PTE LTD +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// 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 "esp_log.h" +#include "bootloader_common.h" +#include "soc/cpu.h" +#include "esp_rom_sys.h" + + +void __assert_func(const char *file, int line, const char *func, const char *expr) +{ + esp_rom_printf("Assert failed in %s, %s:%d (%s)\r\n", func, file, line, expr); + while (1) { + } +} + +void abort(void) +{ +#if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT + esp_rom_printf("abort() was called at PC 0x%08x\r\n", (intptr_t)__builtin_return_address(0) - 3); +#endif + if (esp_cpu_in_ocd_debug_mode()) { + __asm__("break 0,0"); + } + while (1) { + } +} diff --git a/components/bootloader_support/src/esp32/bootloader_esp32.c b/components/bootloader_support/src/esp32/bootloader_esp32.c index abf2b52956..4a2b501cd7 100644 --- a/components/bootloader_support/src/esp32/bootloader_esp32.c +++ b/components/bootloader_support/src/esp32/bootloader_esp32.c @@ -351,18 +351,6 @@ static void bootloader_check_wdt_reset(void) wdt_reset_cpu0_info_enable(); } -void abort(void) -{ -#if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT - esp_rom_printf("abort() was called at PC 0x%08x\r\n", (intptr_t)__builtin_return_address(0) - 3); -#endif - if (esp_cpu_in_ocd_debug_mode()) { - __asm__("break 0,0"); - } - while (1) { - } -} - esp_err_t bootloader_init(void) { esp_err_t ret = ESP_OK; diff --git a/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c b/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c index 9b08921d41..29f6d2f82b 100644 --- a/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c +++ b/components/bootloader_support/src/esp32s2/bootloader_esp32s2.c @@ -46,7 +46,7 @@ #include static const char *TAG = "boot.esp32s2"; -void bootloader_configure_spi_pins(int drv) +void IRAM_ATTR bootloader_configure_spi_pins(int drv) { const uint32_t spiconfig = esp_rom_efuse_get_flash_gpio_info(); uint8_t wp_pin = esp_rom_efuse_get_flash_wp_gpio(); @@ -276,18 +276,6 @@ static void bootloader_check_wdt_reset(void) wdt_reset_cpu0_info_enable(); } -void abort(void) -{ -#if !CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT - esp_rom_printf("abort() was called at PC 0x%08x\r\n", (intptr_t)__builtin_return_address(0) - 3); -#endif - if (esp_cpu_in_ocd_debug_mode()) { - __asm__("break 0,0"); - } - while (1) { - } -} - static void bootloader_super_wdt_auto_feed(void) { REG_SET_BIT(RTC_CNTL_SWD_CONF_REG, RTC_CNTL_SWD_AUTO_FEED_EN);