bootloader_start.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "rom/gpio.h"
  19. #include "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 "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()
  36. {
  37. // 1. Hardware initialization
  38. if (bootloader_init() != ESP_OK) {
  39. bootloader_reset();
  40. }
  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 (rtc_get_reset_reason(0) != DEEPSLEEP_RESET) {
  72. // Factory firmware.
  73. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
  74. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  75. ESP_LOGI(TAG, "Detect a condition of the factory reset");
  76. bool ota_data_erase = false;
  77. #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE
  78. ota_data_erase = true;
  79. #endif
  80. const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET;
  81. ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase);
  82. if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) {
  83. ESP_LOGE(TAG, "Not all partitions were erased");
  84. }
  85. return bootloader_utility_get_selected_boot_partition(bs);
  86. }
  87. #endif
  88. // TEST firmware.
  89. #ifdef CONFIG_BOOTLOADER_APP_TEST
  90. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  91. ESP_LOGI(TAG, "Detect a boot condition of the test firmware");
  92. if (bs->test.offset != 0) {
  93. boot_index = TEST_APP_INDEX;
  94. return boot_index;
  95. } else {
  96. ESP_LOGE(TAG, "Test firmware is not found in partition table");
  97. return INVALID_INDEX;
  98. }
  99. }
  100. #endif
  101. // Customer implementation.
  102. // if (gpio_pin_1 == true && ...){
  103. // boot_index = required_boot_partition;
  104. // } ...
  105. }
  106. return boot_index;
  107. }