bootloader_start.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdbool.h>
  7. #include "esp_log.h"
  8. #include "bootloader_init.h"
  9. #include "bootloader_utility.h"
  10. #include "bootloader_common.h"
  11. static const char* TAG = "boot";
  12. static int select_partition_number(bootloader_state_t *bs);
  13. /*
  14. * We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
  15. * The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
  16. * We do have a stack, so we can do the initialization in C.
  17. */
  18. void __attribute__((noreturn)) call_start_cpu0(void)
  19. {
  20. // 1. Hardware initialization
  21. if (bootloader_init() != ESP_OK) {
  22. bootloader_reset();
  23. }
  24. #ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP
  25. // If this boot is a wake up from the deep sleep then go to the short way,
  26. // try to load the application which worked before deep sleep.
  27. // It skips a lot of checks due to it was done before (while first boot).
  28. bootloader_utility_load_boot_image_from_deep_sleep();
  29. // If it is not successful try to load an application as usual.
  30. #endif
  31. // 2. Select the number of boot partition
  32. bootloader_state_t bs = {0};
  33. int boot_index = select_partition_number(&bs);
  34. if (boot_index == INVALID_INDEX) {
  35. bootloader_reset();
  36. }
  37. // 2.1 Print a custom message!
  38. esp_rom_printf("[%s] Custom bootloader has been initialized correctly.\n", TAG);
  39. // 3. Load the app image for booting
  40. bootloader_utility_load_boot_image(&bs, boot_index);
  41. }
  42. // Select the number of boot partition
  43. static int select_partition_number(bootloader_state_t *bs)
  44. {
  45. // 1. Load partition table
  46. if (!bootloader_utility_load_partition_table(bs)) {
  47. ESP_LOGE(TAG, "load partition table error!");
  48. return INVALID_INDEX;
  49. }
  50. // 2. Select the number of boot partition
  51. return bootloader_utility_get_selected_boot_partition(bs);
  52. }
  53. // Return global reent struct if any newlib functions are linked to bootloader
  54. struct _reent *__getreent(void)
  55. {
  56. return _GLOBAL_REENT;
  57. }