bootloader_utility.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 "esp_image_format.h"
  16. #include "bootloader_config.h"
  17. /**
  18. * @brief Load partition table.
  19. *
  20. * Parse partition table, get useful data such as location of
  21. * OTA data partition, factory app partition, and test app partition.
  22. *
  23. * @param[out] bs Bootloader state structure used to save read data.
  24. * @return Return true if the partition table was succesfully loaded and MD5 checksum is valid.
  25. */
  26. bool bootloader_utility_load_partition_table(bootloader_state_t* bs);
  27. /**
  28. * @brief Return the index of the selected boot partition.
  29. *
  30. * This is the preferred boot partition, as determined by the partition table &
  31. * any OTA sequence number found in OTA data.
  32. * This partition will only be booted if it contains a valid app image, otherwise load_boot_image() will search
  33. * for a valid partition using this selection as the starting point.
  34. *
  35. * @param[in] bs Bootloader state structure.
  36. * @return Returns the index on success, INVALID_INDEX otherwise.
  37. */
  38. int bootloader_utility_get_selected_boot_partition(const bootloader_state_t *bs);
  39. /**
  40. * @brief Load the selected partition and start application.
  41. *
  42. * Start from partition 'start_index', if not bootable then work backwards to FACTORY_INDEX
  43. * (ie try any OTA slots in descending order and then the factory partition).
  44. * If still nothing, start from 'start_index + 1' and work up to highest numbered OTA partition.
  45. * If still nothing, try TEST_APP_INDEX.
  46. * Everything this function calls must be located in the iram_loader_seg segment.
  47. *
  48. * @param[in] bs Bootloader state structure.
  49. * @param[in] start_index The index from which the search for images begins.
  50. */
  51. __attribute__((noreturn)) void bootloader_utility_load_boot_image(const bootloader_state_t *bs, int start_index);
  52. /**
  53. * @brief Software reset the ESP32
  54. *
  55. * Bootloader code should call this in the case that it cannot proceed.
  56. *
  57. * It is not recommended to call this function from an app (if called, the app will abort).
  58. */
  59. __attribute__((noreturn)) void bootloader_reset(void);
  60. /** @brief Generates the digest of the data between offset & offset+length.
  61. *
  62. * This function should be used when the size of the data is larger than 3.2MB.
  63. * The MMU capacity is 3.2MB (50 pages - 64KB each). This function generates the SHA-256
  64. * of the data in chunks of 3.2MB, considering the MMU capacity.
  65. *
  66. * @param[in] flash_offset Offset of the data in flash.
  67. * @param[in] len Length of data in bytes.
  68. * @param[out] digest Pointer to buffer where the digest is written, if ESP_OK is returned.
  69. *
  70. * @return ESP_OK if secure boot digest is generated successfully.
  71. */
  72. esp_err_t bootloader_sha256_flash_contents(uint32_t flash_offset, uint32_t len, uint8_t *digest);