bootloader_start.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. 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. #define LWS_MAGIC_REBOOT_TYPE_ADS 0x50001ffc
  30. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY 0xb00bcafe
  31. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY 0xfaceb00b
  32. #define LWS_MAGIC_REBOOT_TYPE_FORCED_FACTORY_BUTTON 0xf0cedfac
  33. #define LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY_ERASE_OTA 0xfac0eeee
  34. /*
  35. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  36. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  37. * We do have a stack, so we can do the initialization in C.
  38. */
  39. void __attribute__((noreturn)) call_start_cpu0()
  40. {
  41. // 1. Hardware initialization
  42. if (bootloader_init() != ESP_OK) {
  43. bootloader_reset();
  44. }
  45. // 2. Select the number of boot partition
  46. bootloader_state_t bs = { 0 };
  47. int boot_index = select_partition_number(&bs);
  48. if (boot_index == INVALID_INDEX) {
  49. bootloader_reset();
  50. }
  51. // 3. Load the app image for booting
  52. bootloader_utility_load_boot_image(&bs, boot_index);
  53. }
  54. // Select the number of boot partition
  55. static int select_partition_number (bootloader_state_t *bs)
  56. {
  57. // 1. Load partition table
  58. if (!bootloader_utility_load_partition_table(bs)) {
  59. ESP_LOGE(TAG, "load partition table error!");
  60. return INVALID_INDEX;
  61. }
  62. // 2. Select the number of boot partition
  63. return selected_boot_partition(bs);
  64. }
  65. /*
  66. * Selects a boot partition.
  67. * The conditions for switching to another firmware are checked.
  68. */
  69. static int selected_boot_partition(const bootloader_state_t *bs)
  70. {
  71. int boot_index = bootloader_utility_get_selected_boot_partition(bs);
  72. if (boot_index == INVALID_INDEX) {
  73. return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
  74. } else {
  75. // Factory firmware.
  76. #ifdef CONFIG_BOOTLOADER_FACTORY_RESET
  77. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_FACTORY_RESET, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  78. ESP_LOGI(TAG, "Detect a condition of the factory reset");
  79. bool ota_data_erase = false;
  80. #ifdef CONFIG_BOOTLOADER_OTA_DATA_ERASE
  81. ota_data_erase = true;
  82. #endif
  83. const char *list_erase = CONFIG_BOOTLOADER_DATA_FACTORY_RESET;
  84. ESP_LOGI(TAG, "Data partitions to erase: %s", list_erase);
  85. if (bootloader_common_erase_part_type_data(list_erase, ota_data_erase) == false) {
  86. ESP_LOGE(TAG, "Not all partitions were erased");
  87. }
  88. return bootloader_utility_get_selected_boot_partition(bs);
  89. }
  90. #endif
  91. // TEST firmware.
  92. #ifdef CONFIG_BOOTLOADER_APP_TEST
  93. if (bootloader_common_check_long_hold_gpio(CONFIG_BOOTLOADER_NUM_PIN_APP_TEST, CONFIG_BOOTLOADER_HOLD_TIME_GPIO) == 1) {
  94. ESP_LOGI(TAG, "Detect a boot condition of the test firmware");
  95. if (bs->test.offset != 0) {
  96. boot_index = TEST_APP_INDEX;
  97. return boot_index;
  98. } else {
  99. ESP_LOGE(TAG, "Test firmware is not found in partition table");
  100. return INVALID_INDEX;
  101. }
  102. }
  103. #endif
  104. uint32_t *p_force_factory_magic = (uint32_t *)LWS_MAGIC_REBOOT_TYPE_ADS;
  105. if(*p_force_factory_magic == LWS_MAGIC_REBOOT_TYPE_REQ_FACTORY){
  106. boot_index=FACTORY_INDEX;
  107. }
  108. }
  109. return boot_index;
  110. }
  111. // Return global reent struct if any newlib functions are linked to bootloader
  112. struct _reent* __getreent() {
  113. return _GLOBAL_REENT;
  114. }