flash_partitions.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "esp_flash_partitions.h"
  8. #include "esp_log.h"
  9. #include "esp_rom_md5.h"
  10. #include "esp_rom_spiflash.h"
  11. static const char *TAG = "flash_parts";
  12. esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions)
  13. {
  14. int md5_found = 0;
  15. size_t num_parts;
  16. uint32_t chip_size = g_rom_flashchip.chip_size;
  17. *num_partitions = 0;
  18. for (num_parts = 0; num_parts < ESP_PARTITION_TABLE_MAX_ENTRIES; num_parts++) {
  19. const esp_partition_info_t *part = &partition_table[num_parts];
  20. if (part->magic == ESP_PARTITION_MAGIC) {
  21. const esp_partition_pos_t *pos = &part->pos;
  22. if (pos->offset > chip_size || pos->offset + pos->size > chip_size) {
  23. if (log_errors) {
  24. ESP_LOGE(TAG, "partition %d invalid - offset 0x%"PRIx32" size 0x%"PRIx32" exceeds flash chip size 0x%"PRIx32,
  25. num_parts, pos->offset, pos->size, chip_size);
  26. }
  27. return ESP_ERR_INVALID_SIZE;
  28. }
  29. } else if (part->magic == ESP_PARTITION_MAGIC_MD5) {
  30. if (md5_found) {
  31. if (log_errors) {
  32. ESP_LOGE(TAG, "Only one MD5 checksum is allowed");
  33. }
  34. return ESP_ERR_INVALID_STATE;
  35. }
  36. md5_context_t context;
  37. unsigned char digest[16];
  38. esp_rom_md5_init(&context);
  39. esp_rom_md5_update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t));
  40. esp_rom_md5_final(digest, &context);
  41. unsigned char *md5sum = ((unsigned char *) part) + ESP_PARTITION_MD5_OFFSET;
  42. if (memcmp(md5sum, digest, sizeof(digest)) != 0) {
  43. if (log_errors) {
  44. ESP_LOGE(TAG, "Incorrect MD5 checksum");
  45. }
  46. return ESP_ERR_INVALID_STATE;
  47. }
  48. //MD5 checksum matches and we continue with the next interation in
  49. //order to detect the end of the partition table
  50. md5_found = 1;
  51. } else if (part->magic == 0xFFFF
  52. && part->type == PART_TYPE_END
  53. && part->subtype == PART_SUBTYPE_END) {
  54. ESP_LOGD(TAG, "partition table verified, %d entries", num_parts);
  55. *num_partitions = num_parts - md5_found; //do not count the partition where the MD5 checksum is held
  56. return ESP_OK;
  57. } else {
  58. if (log_errors) {
  59. ESP_LOGE(TAG, "partition %d invalid magic number 0x%x", num_parts, part->magic);
  60. }
  61. return ESP_ERR_INVALID_STATE;
  62. }
  63. }
  64. if (log_errors) {
  65. ESP_LOGE(TAG, "partition table has no terminating entry, not valid");
  66. }
  67. return ESP_ERR_INVALID_STATE;
  68. }