bootloader_start.c 4.8 KB

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