bootloader_start.c 4.5 KB

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