bootloader_start.c 4.7 KB

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