bootloader_start.c 4.5 KB

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