bootloader_start.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 esp_err_t select_image (esp_image_metadata_t *image_data);
  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 call_start_cpu0()
  35. {
  36. // 1. Hardware initialization
  37. if(bootloader_init() != ESP_OK){
  38. return;
  39. }
  40. // 2. Select image to boot
  41. esp_image_metadata_t image_data;
  42. if(select_image(&image_data) != ESP_OK){
  43. return;
  44. }
  45. // 3. Loading the selected image
  46. bootloader_utility_load_image(&image_data);
  47. }
  48. // Selects image to boot
  49. static esp_err_t select_image (esp_image_metadata_t *image_data)
  50. {
  51. // 1. Load partition table
  52. bootloader_state_t bs = { 0 };
  53. if (!bootloader_utility_load_partition_table(&bs)) {
  54. ESP_LOGE(TAG, "load partition table error!");
  55. return ESP_FAIL;
  56. }
  57. // 2. Select boot partition
  58. int boot_index = selected_boot_partition(&bs);
  59. if(boot_index == INVALID_INDEX) {
  60. return ESP_FAIL; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
  61. }
  62. // 3. Load the app image for booting
  63. if (!bootloader_utility_load_boot_image(&bs, boot_index, image_data)) {
  64. return ESP_FAIL;
  65. }
  66. return ESP_OK;
  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. } else {
  78. // Factory firmware.
  79. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
  80. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  81. ESP_LOGI(TAG, "Detect a condition of the factory reset");
  82. bool ota_data_erase = false;
  83. #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE
  84. ota_data_erase = true;
  85. #endif
  86. const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET;
  87. ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase);
  88. if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) {
  89. ESP_LOGE(TAG, "Not all partitions were erased");
  90. }
  91. return bootloader_utility_get_selected_boot_partition(bs);
  92. }
  93. #endif
  94. // TEST firmware.
  95. #ifdef CONFIG_BOOTLOADER_APP_TEST
  96. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  97. ESP_LOGI(TAG, "Detect a boot condition of the test firmware");
  98. if (bs->test.offset != 0) {
  99. boot_index = TEST_APP_INDEX;
  100. return boot_index;
  101. } else {
  102. ESP_LOGE(TAG, "Test firmware is not found in partition table");
  103. return INVALID_INDEX;
  104. }
  105. }
  106. #endif
  107. // Customer implementation.
  108. // if (gpio_pin_1 == true && ...){
  109. // boot_index = required_boot_partition;
  110. // } ...
  111. }
  112. return boot_index;
  113. }