bootloader_start.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdbool.h>
  7. #include "esp_log.h"
  8. #include "esp_rom_sys.h"
  9. #include "bootloader_init.h"
  10. #include "bootloader_utility.h"
  11. #include "bootloader_common.h"
  12. #include "bootloader_hooks.h"
  13. static const char *TAG = "boot";
  14. static int select_partition_number(bootloader_state_t *bs);
  15. static int selected_boot_partition(const bootloader_state_t *bs);
  16. /*
  17. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  18. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  19. * We do have a stack, so we can do the initialization in C.
  20. */
  21. void __attribute__((noreturn)) call_start_cpu0(void)
  22. {
  23. // (0. Call the before-init hook, if available)
  24. if (bootloader_before_init) {
  25. bootloader_before_init();
  26. }
  27. // 1. Hardware initialization
  28. if (bootloader_init() != ESP_OK) {
  29. bootloader_reset();
  30. }
  31. // (1.1 Call the after-init hook, if available)
  32. if (bootloader_after_init) {
  33. bootloader_after_init();
  34. }
  35. #ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP
  36. // If this boot is a wake up from the deep sleep then go to the short way,
  37. // try to load the application which worked before deep sleep.
  38. // It skips a lot of checks due to it was done before (while first boot).
  39. bootloader_utility_load_boot_image_from_deep_sleep();
  40. // If it is not successful try to load an application as usual.
  41. #endif
  42. // 2. Select the number of boot partition
  43. bootloader_state_t bs = {0};
  44. int boot_index = select_partition_number(&bs);
  45. if (boot_index == INVALID_INDEX) {
  46. bootloader_reset();
  47. }
  48. // 3. Load the app image for booting
  49. bootloader_utility_load_boot_image(&bs, boot_index);
  50. }
  51. // Select the number of boot partition
  52. static int select_partition_number(bootloader_state_t *bs)
  53. {
  54. // 1. Load partition table
  55. if (!bootloader_utility_load_partition_table(bs)) {
  56. ESP_LOGE(TAG, "load partition table error!");
  57. return INVALID_INDEX;
  58. }
  59. // 2. Select the number of boot partition
  60. return selected_boot_partition(bs);
  61. }
  62. /*
  63. * Selects a boot partition.
  64. * The conditions for switching to another firmware are checked.
  65. */
  66. static int selected_boot_partition(const bootloader_state_t *bs)
  67. {
  68. int boot_index = bootloader_utility_get_selected_boot_partition(bs);
  69. if (boot_index == INVALID_INDEX) {
  70. return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
  71. }
  72. if (esp_rom_get_reset_reason(0) != RESET_REASON_CORE_DEEP_SLEEP) {
  73. // Factory firmware.
  74. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
  75. bool reset_level = false;
  76. #if CONFIG_BOOTLOADER_FACTORY_RESET_PIN_HIGH
  77. reset_level = true;
  78. #endif
  79. if (bootloader_common_check_long_hold_gpio_level(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO, reset_level) == GPIO_LONG_HOLD) {
  80. ESP_LOGI(TAG, "Detect a condition of the factory reset");
  81. bool ota_data_erase = false;
  82. #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE
  83. ota_data_erase = true;
  84. #endif
  85. const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET;
  86. ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase);
  87. if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) {
  88. ESP_LOGE(TAG, "Not all partitions were erased");
  89. }
  90. return bootloader_utility_get_selected_boot_partition(bs);
  91. }
  92. #endif
  93. // TEST firmware.
  94. #ifdef CONFIG_BOOTLOADER_APP_TEST
  95. bool app_test_level = false;
  96. #if CONFIG_BOOTLOADER_APP_TEST_PIN_HIGH
  97. app_test_level = true;
  98. #endif
  99. if (bootloader_common_check_long_hold_gpio_level(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO, app_test_level) == GPIO_LONG_HOLD) {
  100. ESP_LOGI(TAG, "Detect a boot condition of the test firmware");
  101. if (bs->test.offset != 0) {
  102. boot_index = TEST_APP_INDEX;
  103. return boot_index;
  104. } else {
  105. ESP_LOGE(TAG, "Test firmware is not found in partition table");
  106. return INVALID_INDEX;
  107. }
  108. }
  109. #endif
  110. // Customer implementation.
  111. // if (gpio_pin_1 == true && ...){
  112. // boot_index = required_boot_partition;
  113. // } ...
  114. }
  115. return boot_index;
  116. }
  117. // Return global reent struct if any newlib functions are linked to bootloader
  118. struct _reent *__getreent(void)
  119. {
  120. return _GLOBAL_REENT;
  121. }