bootloader_init.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "hal/timer_ll.h"
  27. #include "soc/cpu.h"
  28. #include "soc/rtc.h"
  29. #include "soc/rtc_wdt.h"
  30. static const char *TAG = "boot";
  31. esp_image_header_t WORD_ALIGNED_ATTR bootloader_image_hdr;
  32. void bootloader_clear_bss_section(void)
  33. {
  34. memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
  35. }
  36. esp_err_t bootloader_read_bootloader_header(void)
  37. {
  38. /* load bootloader image header */
  39. if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &bootloader_image_hdr, sizeof(esp_image_header_t), true) != ESP_OK) {
  40. ESP_LOGE(TAG, "failed to load bootloader image header!");
  41. return ESP_FAIL;
  42. }
  43. return ESP_OK;
  44. }
  45. esp_err_t bootloader_check_bootloader_validity(void)
  46. {
  47. /* read chip revision from efuse */
  48. uint8_t revision = bootloader_common_get_chip_revision();
  49. ESP_LOGI(TAG, "chip revision: %d", revision);
  50. /* compare with the one set in bootloader image header */
  51. if (bootloader_common_check_chip_validity(&bootloader_image_hdr, ESP_IMAGE_BOOTLOADER) != ESP_OK) {
  52. return ESP_FAIL;
  53. }
  54. return ESP_OK;
  55. }
  56. void bootloader_config_wdt(void)
  57. {
  58. #ifdef CONFIG_BOOTLOADER_WDT_ENABLE
  59. ESP_LOGD(TAG, "Enabling RTCWDT(%d ms)", CONFIG_BOOTLOADER_WDT_TIME_MS);
  60. rtc_wdt_protect_off();
  61. rtc_wdt_disable();
  62. rtc_wdt_set_length_of_reset_signal(RTC_WDT_SYS_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  63. rtc_wdt_set_length_of_reset_signal(RTC_WDT_CPU_RESET_SIG, RTC_WDT_LENGTH_3_2us);
  64. rtc_wdt_set_stage(RTC_WDT_STAGE0, RTC_WDT_STAGE_ACTION_RESET_RTC);
  65. rtc_wdt_set_time(RTC_WDT_STAGE0, CONFIG_BOOTLOADER_WDT_TIME_MS);
  66. rtc_wdt_enable();
  67. rtc_wdt_protect_on();
  68. #else /* disable watch dog */
  69. rtc_wdt_disable();
  70. #endif
  71. timer_ll_wdt_set_protect(&TIMERG0, false);
  72. timer_ll_wdt_flashboot_en(&TIMERG0, false);
  73. }
  74. void bootloader_enable_random(void)
  75. {
  76. ESP_LOGI(TAG, "Enabling RNG early entropy source...");
  77. bootloader_random_enable();
  78. }
  79. void bootloader_print_banner(void)
  80. {
  81. ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
  82. ESP_LOGI(TAG, "compile time " __TIME__);
  83. }
  84. void __assert_func(const char *file, int line, const char *func, const char *expr)
  85. {
  86. ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr);
  87. while (1) {
  88. }
  89. }