bootloader_init.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. static const char *TAG = "boot";
  23. esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
  24. void bootloader_clear_bss_section(void)
  25. {
  26. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  27. }
  28. esp_err_t bootloader_read_bootloader_header(void)
  29. {
  30. /* load bootloader image header */
  31. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
  32. ESP_EARLY_LOGE(TAG, "failed to load bootloader image header!");
  33. return ESP_FAIL;
  34. }
  35. return ESP_OK;
  36. }
  37. esp_err_t bootloader_check_bootloader_validity(void)
  38. {
  39. unsigned int revision = efuse_hal_chip_revision();
  40. unsigned int major = revision / 100;
  41. unsigned int minor = revision % 100;
  42. ESP_EARLY_LOGI(TAG, "chip revision: v%d.%d", major, minor);
  43. /* compare with the one set in bootloader image header */
  44. if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
  45. return ESP_FAIL;
  46. }
  47. return ESP_OK;
  48. }
  49. void bootloader_config_wdt(void)
  50. {
  51. /*
  52. * At this point, the flashboot protection of RWDT and MWDT0 will have been
  53. * automatically enabled. We can disable flashboot protection as it's not
  54. * needed anymore. If configured to do so, we also initialize the RWDT to
  55. * protect the remainder of the bootloader process.
  56. */
  57. //Disable RWDT flashboot protection.
  58. wdt_hal_context_t rwdt_ctx = RWDT_HAL_CONTEXT_DEFAULT();
  59. wdt_hal_write_protect_disable(&rwdt_ctx);
  60. wdt_hal_set_flashboot_en(&rwdt_ctx, false);
  61. wdt_hal_write_protect_enable(&rwdt_ctx);
  62. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  63. //Initialize and start RWDT to protect the for bootloader if configured to do so
  64. ESP_EARLY_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
  65. wdt_hal_init(&rwdt_ctx, WDT_RWDT, 0, false);
  66. uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
  67. wdt_hal_write_protect_disable(&rwdt_ctx);
  68. wdt_hal_config_stage(&rwdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  69. wdt_hal_enable(&rwdt_ctx);
  70. wdt_hal_write_protect_enable(&rwdt_ctx);
  71. #endif
  72. //Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
  73. wdt_hal_context_t mwdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  74. wdt_hal_write_protect_disable(&mwdt_ctx);
  75. wdt_hal_set_flashboot_en(&mwdt_ctx, false);
  76. wdt_hal_write_protect_enable(&mwdt_ctx);
  77. }
  78. void bootloader_enable_random(void)
  79. {
  80. ESP_EARLY_LOGI(TAG, "Enabling RNG early entropy source...");
  81. bootloader_random_enable();
  82. }
  83. void bootloader_print_banner(void)
  84. {
  85. ESP_EARLY_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
  86. #ifndef CONFIG_APP_REPRODUCIBLE_BUILD
  87. ESP_EARLY_LOGI(TAG, "compile time " __DATE__ " " __TIME__);
  88. #endif
  89. #if CONFIG_FREERTOS_UNICORE
  90. #if (SOC_CPU_CORES_NUM > 1)
  91. ESP_EARLY_LOGW(TAG, "Unicore bootloader");
  92. #endif
  93. #else
  94. ESP_EARLY_LOGI(TAG, "Multicore bootloader");
  95. #endif
  96. }