bootloader_init.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <string.h>
  15. #include <stdint.h>
  16. #include "sdkconfig.h"
  17. #include "esp_attr.h"
  18. #include "esp_log.h"
  19. #include "bootloader_init.h"
  20. #include "bootloader_flash.h"
  21. #include "bootloader_flash_config.h"
  22. #include "bootloader_random.h"
  23. #include "bootloader_clock.h"
  24. #include "bootloader_common.h"
  25. #include "esp_flash_encrypt.h"
  26. #include "soc/cpu.h"
  27. #include "soc/rtc.h"
  28. #include "hal/wdt_hal.h"
  29. static const char *TAG = "boot";
  30. esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
  31. void bootloader_clear_bss_section(void)
  32. {
  33. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  34. }
  35. esp_err_t bootloader_read_bootloader_header(void)
  36. {
  37. /* load bootloader image header */
  38. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
  39. ESP_LOGE(TAG, "failed to load bootloader image header!");
  40. return ESP_FAIL;
  41. }
  42. return ESP_OK;
  43. }
  44. esp_err_t bootloader_check_bootloader_validity(void)
  45. {
  46. /* read chip revision from efuse */
  47. uint8_t revision = bootloader_common_get_chip_revision();
  48. ESP_LOGI(TAG, "chip revision: %d", revision);
  49. /* compare with the one set in bootloader image header */
  50. if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
  51. return ESP_FAIL;
  52. }
  53. return ESP_OK;
  54. }
  55. void bootloader_config_wdt(void)
  56. {
  57. /*
  58. * At this point, the flashboot protection of RWDT and MWDT0 will have been
  59. * automatically enabled. We can disable flashboot protection as it's not
  60. * needed anymore. If configured to do so, we also initialize the RWDT to
  61. * protect the remainder of the bootloader process.
  62. */
  63. //Disable RWDT flashboot protection.
  64. wdt_hal_context_t rtc_wdt_ctx = {.inst = WDT_RWDT, .rwdt_dev = &RTCCNTL};
  65. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  66. wdt_hal_set_flashboot_en(&rtc_wdt_ctx, false);
  67. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  68. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  69. //Initialize and start RWDT to protect the for bootloader if configured to do so
  70. ESP_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
  71. wdt_hal_init(&rtc_wdt_ctx, WDT_RWDT, 0, false);
  72. uint32_t stage_timeout_ticks = (uint32_t)((uint64_t)CONFIG_BOOTLOADER_WDT_TIME_MS * rtc_clk_slow_freq_get_hz() / 1000);
  73. wdt_hal_write_protect_disable(&rtc_wdt_ctx);
  74. wdt_hal_config_stage(&rtc_wdt_ctx, WDT_STAGE0, stage_timeout_ticks, WDT_STAGE_ACTION_RESET_RTC);
  75. wdt_hal_enable(&rtc_wdt_ctx);
  76. wdt_hal_write_protect_enable(&rtc_wdt_ctx);
  77. #endif
  78. //Disable MWDT0 flashboot protection. But only after we've enabled the RWDT first so that there's not gap in WDT protection.
  79. wdt_hal_context_t wdt_ctx = {.inst = WDT_MWDT0, .mwdt_dev = &TIMERG0};
  80. wdt_hal_write_protect_disable(&wdt_ctx);
  81. wdt_hal_set_flashboot_en(&wdt_ctx, false);
  82. wdt_hal_write_protect_enable(&wdt_ctx);
  83. }
  84. void bootloader_enable_random(void)
  85. {
  86. ESP_LOGI(TAG, "Enabling RNG early entropy source...");
  87. bootloader_random_enable();
  88. }
  89. void bootloader_print_banner(void)
  90. {
  91. ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
  92. ESP_LOGI(TAG, "compile time " __TIME__);
  93. }
  94. void __assert_func(const char *file, int line, const char *func, const char *expr)
  95. {
  96. ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr);
  97. while (1) {
  98. }
  99. }