flash_partitions.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2015-2016 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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include "esp_flash_partitions.h"
  15. #include "esp_log.h"
  16. #include "esp_rom_md5.h"
  17. #if CONFIG_IDF_TARGET_ESP32C3
  18. #include "esp32c3/rom/spi_flash.h"
  19. #elif CONFIG_IDF_TARGET_ESP32S2
  20. #include "esp32s2/rom/spi_flash.h"
  21. #elif CONFIG_IDF_TARGET_ESP32S3
  22. #include "esp32s3/rom/spi_flash.h"
  23. #else
  24. #include "esp32/rom/spi_flash.h"
  25. #endif
  26. static const char *TAG = "flash_parts";
  27. esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions)
  28. {
  29. int md5_found = 0;
  30. size_t num_parts;
  31. uint32_t chip_size = g_rom_flashchip.chip_size;
  32. *num_partitions = 0;
  33. for (num_parts = 0; num_parts < ESP_PARTITION_TABLE_MAX_ENTRIES; num_parts++) {
  34. const esp_partition_info_t *part = &partition_table[num_parts];
  35. if (part->magic == ESP_PARTITION_MAGIC) {
  36. const esp_partition_pos_t *pos = &part->pos;
  37. if (pos->offset > chip_size || pos->offset + pos->size > chip_size) {
  38. if (log_errors) {
  39. ESP_LOGE(TAG, "partition %d invalid - offset 0x%x size 0x%x exceeds flash chip size 0x%x",
  40. num_parts, pos->offset, pos->size, chip_size);
  41. }
  42. return ESP_ERR_INVALID_SIZE;
  43. }
  44. } else if (part->magic == ESP_PARTITION_MAGIC_MD5) {
  45. if (md5_found) {
  46. if (log_errors) {
  47. ESP_LOGE(TAG, "Only one MD5 checksum is allowed");
  48. }
  49. return ESP_ERR_INVALID_STATE;
  50. }
  51. struct MD5Context context;
  52. unsigned char digest[16];
  53. esp_rom_md5_init(&context);
  54. esp_rom_md5_update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t));
  55. esp_rom_md5_final(digest, &context);
  56. unsigned char *md5sum = ((unsigned char *) part) + 16; // skip the 2B magic number and the 14B fillup bytes
  57. if (memcmp(md5sum, digest, sizeof(digest)) != 0) {
  58. if (log_errors) {
  59. ESP_LOGE(TAG, "Incorrect MD5 checksum");
  60. }
  61. return ESP_ERR_INVALID_STATE;
  62. }
  63. //MD5 checksum matches and we continue with the next interation in
  64. //order to detect the end of the partition table
  65. md5_found = 1;
  66. } else if (part->magic == 0xFFFF
  67. && part->type == PART_TYPE_END
  68. && part->subtype == PART_SUBTYPE_END) {
  69. ESP_LOGD(TAG, "partition table verified, %d entries", num_parts);
  70. *num_partitions = num_parts - md5_found; //do not count the partition where the MD5 checksum is held
  71. return ESP_OK;
  72. } else {
  73. if (log_errors) {
  74. ESP_LOGE(TAG, "partition %d invalid magic number 0x%x", num_parts, part->magic);
  75. }
  76. return ESP_ERR_INVALID_STATE;
  77. }
  78. }
  79. if (log_errors) {
  80. ESP_LOGE(TAG, "partition table has no terminating entry, not valid");
  81. }
  82. return ESP_ERR_INVALID_STATE;
  83. }