bootloader_start.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. static const char* TAG = "boot";
  27. static int select_partition_number (bootloader_state_t *bs);
  28. static int selected_boot_partition(const bootloader_state_t *bs);
  29. /*
  30. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  31. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  32. * We do have a stack, so we can do the initialization in C.
  33. */
  34. void __attribute__((noreturn)) call_start_cpu0()
  35. {
  36. // 1. Hardware initialization
  37. if (bootloader_init() != ESP_OK) {
  38. bootloader_reset();
  39. }
  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. } else {
  70. // Factory firmware.
  71. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
  72. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  73. ESP_LOGI(TAG, "Detect a condition of the factory reset");
  74. bool ota_data_erase = false;
  75. #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE
  76. ota_data_erase = true;
  77. #endif
  78. const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET;
  79. ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase);
  80. if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) {
  81. ESP_LOGE(TAG, "Not all partitions were erased");
  82. }
  83. return bootloader_utility_get_selected_boot_partition(bs);
  84. }
  85. #endif
  86. // TEST firmware.
  87. #ifdef CONFIG_BOOTLOADER_APP_TEST
  88. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  89. ESP_LOGI(TAG, "Detect a boot condition of the test firmware");
  90. if (bs->test.offset != 0) {
  91. boot_index = TEST_APP_INDEX;
  92. return boot_index;
  93. } else {
  94. ESP_LOGE(TAG, "Test firmware is not found in partition table");
  95. return INVALID_INDEX;
  96. }
  97. }
  98. #endif
  99. // Customer implementation.
  100. // if (gpio_pin_1 == true && ...){
  101. // boot_index = required_boot_partition;
  102. // } ...
  103. }
  104. return boot_index;
  105. }