bootloader_start.c 4.9 KB

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