bootloader_init.c 3.3 KB

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