bootloader_start.c 2.1 KB

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