bootloader_utility.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * SPDX-FileCopyrightText: 2018-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #pragma once
  7. #include "bootloader_config.h"
  8. #include "esp_image_format.h"
  9. #include "bootloader_config.h"
  10. /**
  11. * @brief Load partition table.
  12. *
  13. * Parse partition table, get useful data such as location of
  14. * OTA data partition, factory app partition, and test app partition.
  15. *
  16. * @param[out] bs Bootloader state structure used to save read data.
  17. * @return Return true if the partition table was succesfully loaded and MD5 checksum is valid.
  18. */
  19. bool bootloader_utility_load_partition_table(bootloader_state_t* bs);
  20. /**
  21. * @brief Return the index of the selected boot partition.
  22. *
  23. * This is the preferred boot partition, as determined by the partition table &
  24. * any OTA sequence number found in OTA data.
  25. * This partition will only be booted if it contains a valid app image, otherwise load_boot_image() will search
  26. * for a valid partition using this selection as the starting point.
  27. *
  28. * @param[in] bs Bootloader state structure.
  29. * @return Returns the index on success, INVALID_INDEX otherwise.
  30. */
  31. int bootloader_utility_get_selected_boot_partition(const bootloader_state_t *bs);
  32. /**
  33. * @brief Load the selected partition and start application.
  34. *
  35. * Start from partition 'start_index', if not bootable then work backwards to FACTORY_INDEX
  36. * (ie try any OTA slots in descending order and then the factory partition).
  37. * If still nothing, start from 'start_index + 1' and work up to highest numbered OTA partition.
  38. * If still nothing, try TEST_APP_INDEX.
  39. * Everything this function calls must be located in the iram_loader_seg segment.
  40. *
  41. * @param[in] bs Bootloader state structure.
  42. * @param[in] start_index The index from which the search for images begins.
  43. */
  44. __attribute__((noreturn)) void bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index);
  45. #ifdef CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP
  46. /**
  47. * @brief Load that application which was worked before we go to the deep sleep.
  48. *
  49. * Checks the reboot reason if it is the deep sleep and has a valid partition in the RTC memory
  50. * then try to load the application which was worked before we go to the deep sleep.
  51. *
  52. */
  53. void bootloader_utility_load_boot_image_from_deep_sleep(void);
  54. #endif
  55. /**
  56. * @brief Software reset the ESP32
  57. *
  58. * Bootloader code should call this in the case that it cannot proceed.
  59. *
  60. * It is not recommended to call this function from an app (if called, the app will abort).
  61. */
  62. __attribute__((noreturn)) void bootloader_reset(void);
  63. /**
  64. * @brief Do any cleanup before exiting the bootloader, before starting the app or resetting
  65. */
  66. void bootloader_atexit(void);
  67. /**
  68. * @brief Converts an array to a printable string.
  69. *
  70. * This function is useful for printing SHA-256 digest.
  71. * \code{c}
  72. * // Example of using. image_hash will be printed
  73. * #define HASH_LEN 32 // SHA-256 digest length
  74. * ...
  75. * char hash_print[HASH_LEN * 2 + 1];
  76. * hash_print[HASH_LEN * 2] = 0;
  77. * bootloader_sha256_hex_to_str(hash_print, image_hash, HASH_LEN);
  78. * ESP_LOGI(TAG, %s", hash_print);
  79. * \endcode
  80. * @param[out] out_str Output string
  81. * @param[in] in_array_hex Pointer to input array
  82. * @param[in] len Length of input array
  83. *
  84. * @return ESP_OK: Successful
  85. * ESP_ERR_INVALID_ARG: Error in the passed arguments
  86. */
  87. esp_err_t bootloader_sha256_hex_to_str(char *out_str, const uint8_t *in_array_hex, size_t len);
  88. /**
  89. * @brief Debug log contents of a buffer as hexadecimal.
  90. *
  91. * @note - Only works if component log level is DEBUG or higher.
  92. * - It will print at most 128 bytes from @c buffer.
  93. *
  94. * @param buffer Buffer to log
  95. * @param length Length of buffer in bytes. Maximum length 128 bytes.
  96. * @param label Label to print at beginning of log line.
  97. */
  98. void bootloader_debug_buffer(const void *buffer, size_t length, const char *label);
  99. /** @brief Generates the digest of the data between offset & offset+length.
  100. *
  101. * This function should be used when the size of the data is larger than 3.2MB.
  102. * The MMU capacity is 3.2MB (50 pages - 64KB each). This function generates the SHA-256
  103. * of the data in chunks of 3.2MB, considering the MMU capacity.
  104. *
  105. * @param[in] flash_offset Offset of the data in flash.
  106. * @param[in] len Length of data in bytes.
  107. * @param[out] digest Pointer to buffer where the digest is written, if ESP_OK is returned.
  108. *
  109. * @return ESP_OK if secure boot digest is generated successfully.
  110. */
  111. esp_err_t bootloader_sha256_flash_contents(uint32_t flash_offset, uint32_t len, uint8_t *digest);