bootloader_start.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "bootloader_config.h"
  20. #include "bootloader_init.h"
  21. #include "bootloader_utility.h"
  22. #include "bootloader_common.h"
  23. #include "sdkconfig.h"
  24. #include "esp_image_format.h"
  25. static const char* TAG = "boot";
  26. static esp_err_t select_image (esp_image_metadata_t *image_data);
  27. static int selected_boot_partition(const bootloader_state_t *bs);
  28. /*
  29. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  30. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  31. * We do have a stack, so we can do the initialization in C.
  32. */
  33. void call_start_cpu0()
  34. {
  35. // 1. Hardware initialization
  36. if(bootloader_init() != ESP_OK){
  37. return;
  38. }
  39. // 2. Select image to boot
  40. esp_image_metadata_t image_data;
  41. if(select_image(&image_data) != ESP_OK){
  42. return;
  43. }
  44. // 3. Loading the selected image
  45. bootloader_utility_load_image(&image_data);
  46. }
  47. // Selects image to boot
  48. static esp_err_t select_image (esp_image_metadata_t *image_data)
  49. {
  50. // 1. Load partition table
  51. bootloader_state_t bs = { 0 };
  52. if (!bootloader_utility_load_partition_table(&bs)) {
  53. ESP_LOGE(TAG, "load partition table error!");
  54. return ESP_FAIL;
  55. }
  56. // 2. Select boot partition
  57. int boot_index = selected_boot_partition(&bs);
  58. if(boot_index == INVALID_INDEX) {
  59. return ESP_FAIL; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
  60. }
  61. // 3. Load the app image for booting
  62. if (!bootloader_utility_load_boot_image(&bs, boot_index, image_data)) {
  63. return ESP_FAIL;
  64. }
  65. return ESP_OK;
  66. }
  67. /*
  68. * Selects a boot partition.
  69. * The conditions for switching to another firmware are checked.
  70. */
  71. static int selected_boot_partition(const bootloader_state_t *bs)
  72. {
  73. int boot_index = bootloader_utility_get_selected_boot_partition(bs);
  74. if (boot_index == INVALID_INDEX) {
  75. return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
  76. } else {
  77. // Check for reset to the factory firmware or for launch OTA[x] firmware.
  78. // Customer implementation.
  79. // if (gpio_pin_1 == true && ...){
  80. // boot_index = required_boot_partition;
  81. // } ...
  82. }
  83. return boot_index;
  84. }