bootloader_utility.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2018 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. #pragma once
  15. #include "bootloader_config.h"
  16. #include "esp_image_format.h"
  17. #include "bootloader_config.h"
  18. /**
  19. * @brief Load partition table.
  20. *
  21. * Parse partition table, get useful data such as location of
  22. * OTA data partition, factory app partition, and test app partition.
  23. *
  24. * @param[out] bs Bootloader state structure used to save read data.
  25. * @return Return true if the partition table was succesfully loaded and MD5 checksum is valid.
  26. */
  27. bool bootloader_utility_load_partition_table(bootloader_state_t* bs);
  28. /**
  29. * @brief Return the index of the selected boot partition.
  30. *
  31. * This is the preferred boot partition, as determined by the partition table &
  32. * any OTA sequence number found in OTA data.
  33. * This partition will only be booted if it contains a valid app image, otherwise load_boot_image() will search
  34. * for a valid partition using this selection as the starting point.
  35. *
  36. * @param[in] bs Bootloader state structure.
  37. * @return Returns the index on success, INVALID_INDEX otherwise.
  38. */
  39. int bootloader_utility_get_selected_boot_partition(const bootloader_state_t *bs);
  40. /**
  41. * @brief Load the selected partition and start application.
  42. *
  43. * Start from partition 'start_index', if not bootable then work backwards to FACTORY_INDEX
  44. * (ie try any OTA slots in descending order and then the factory partition).
  45. * If still nothing, start from 'start_index + 1' and work up to highest numbered OTA partition.
  46. * If still nothing, try TEST_APP_INDEX.
  47. * Everything this function calls must be located in the iram_loader_seg segment.
  48. *
  49. * @param[in] bs Bootloader state structure.
  50. * @param[in] start_index The index from which the search for images begins.
  51. */
  52. __attribute__((noreturn)) void bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index);
  53. /**
  54. * @brief Software reset the ESP32
  55. *
  56. * Bootloader code should call this in the case that it cannot proceed.
  57. *
  58. * It is not recommended to call this function from an app (if called, the app will abort).
  59. */
  60. __attribute__((noreturn)) void bootloader_reset(void);
  61. /**
  62. * @brief Converts an array to a printable string.
  63. *
  64. * This function is useful for printing SHA-256 digest.
  65. * \code{c}
  66. * // Example of using. image_hash will be printed
  67. * #define HASH_LEN 32 // SHA-256 digest length
  68. * ...
  69. * char hash_print[HASH_LEN * 2 + 1];
  70. * hash_print[HASH_LEN * 2] = 0;
  71. * bootloader_sha256_hex_to_str(hash_print, image_hash, HASH_LEN);
  72. * ESP_LOGI(TAG, %s", hash_print);
  73. * \endcode
  74. * @param[out] out_str Output string
  75. * @param[in] in_array_hex Pointer to input array
  76. * @param[in] len Length of input array
  77. *
  78. * @return ESP_OK: Successful
  79. * ESP_ERR_INVALID_ARG: Error in the passed arguments
  80. */
  81. esp_err_t bootloader_sha256_hex_to_str(char *out_str, const uint8_t *in_array_hex, size_t len);
  82. /**
  83. * @brief Debug log contents of a buffer as hexadecimal
  84. *
  85. * @note Only works if component log level is DEBUG or higher.
  86. *
  87. * @param buffer Buffer to log
  88. * @param length Length of buffer in bytes. Maximum length 128 bytes.
  89. * @param label Label to print at beginning of log line.
  90. */
  91. void bootloader_debug_buffer(const void *buffer, size_t length, const char *label);