bootloader_init.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <stdint.h>
  8. #include "sdkconfig.h"
  9. #include "esp_attr.h"
  10. #include "esp_log.h"
  11. #include "bootloader_init.h"
  12. #include "bootloader_flash_priv.h"
  13. #include "bootloader_flash_config.h"
  14. #include "bootloader_random.h"
  15. #include "bootloader_clock.h"
  16. #include "bootloader_common.h"
  17. #include "esp_flash_encrypt.h"
  18. #include "esp_cpu.h"
  19. #include "soc/rtc.h"
  20. #include "hal/wdt_hal.h"
  21. #include "hal/efuse_hal.h"
  22. #include "esp_bootloader_desc.h"
  23. static const char *TAG = "boot";
  24. #if !CONFIG_APP_BUILD_TYPE_RAM
  25. esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
  26. #endif
  27. void bootloader_clear_bss_section(void)
  28. {
  29. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  30. }
  31. esp_err_t bootloader_read_bootloader_header(void)
  32. {
  33. /* load bootloader image header */
  34. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
  35. ESP_EARLY_LOGE(TAG, "failed to load bootloader image header!");
  36. return ESP_FAIL;
  37. }
  38. return ESP_OK;
  39. }
  40. esp_err_t bootloader_check_bootloader_validity(void)
  41. {
  42. unsigned int revision = efuse_hal_chip_revision();
  43. unsigned int major = revision / 100;
  44. unsigned int minor = revision % 100;
  45. ESP_EARLY_LOGI(TAG, "chip revision: v%d.%d", major, minor);
  46. /* compare with the one set in bootloader image header */
  47. if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
  48. return ESP_FAIL;
  49. }
  50. return ESP_OK;
  51. }
  52. void bootloader_config_wdt(void)
  53. {
  54. /*
  55. * At this point, the flashboot protection of RWDT and MWDT0 will have been
  56. * automatically enabled. We can disable flashboot protection as it's not
  57. * needed anymore. If configured to do so, we also initialize the RWDT to
  58. * protect the remainder of the bootloader process.
  59. */
  60. //Disable RWDT flashboot protection.
  61. wdt_hal_context_t rwdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
  62. wdt_hal_write_protect_disable(&rwdt_ctx);
  63. wdt_hal_set_flashboot_en(&rwdt_ctx, false);
  64. wdt_hal_write_protect_enable(&rwdt_ctx);
  65. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  66. //Initialize and start RWDT to protect the for bootloader if configured to do so
  67. ESP_EARLY_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
  68. wdt_hal_init(&rwdt_ctx, WDT_RWDT, 0, false);
  69. uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
  70. wdt_hal_write_protect_disable(&rwdt_ctx);
  71. wdt_hal_config_stage(&rwdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  72. wdt_hal_enable(&rwdt_ctx);
  73. wdt_hal_write_protect_enable(&rwdt_ctx);
  74. #endif
  75. //Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
  76. wdt_hal_context_t mwdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  77. wdt_hal_write_protect_disable(&mwdt_ctx);
  78. wdt_hal_set_flashboot_en(&mwdt_ctx, false);
  79. wdt_hal_write_protect_enable(&mwdt_ctx);
  80. }
  81. void bootloader_enable_random(void)
  82. {
  83. ESP_EARLY_LOGI(TAG, "Enabling RNG early entropy source...");
  84. bootloader_random_enable();
  85. }
  86. void bootloader_print_banner(void)
  87. {
  88. if (CONFIG_BOOTLOADER_LOG_LEVEL >= ESP_LOG_INFO) {
  89. const esp_bootloader_desc_t *desc = esp_bootloader_get_description();
  90. ESP_EARLY_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", desc->idf_ver);
  91. #ifdef CONFIG_BOOTLOADER_COMPILE_TIME_DATE
  92. ESP_EARLY_LOGI(TAG, "compile time %s", desc->date_time);
  93. #endif
  94. }
  95. #if CONFIG_FREERTOS_UNICORE
  96. #if (SOC_CPU_CORES_NUM > 1)
  97. ESP_EARLY_LOGW(TAG, "Unicore bootloader");
  98. #endif
  99. #else
  100. ESP_EARLY_LOGI(TAG, "Multicore bootloader");
  101. #endif
  102. }