flash_partitions.c 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "esp32/rom/spi_flash.h"
  17. #include "esp32/rom/md5_hash.h"
  18. static const char *TAG = "flash_parts";
  19. esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions)
  20. {
  21. int md5_found = 0;
  22. int num_parts;
  23. uint32_t chip_size = g_rom_flashchip.chip_size;
  24. *num_partitions = 0;
  25. for (num_parts = 0; num_parts < ESP_PARTITION_TABLE_MAX_ENTRIES; num_parts++) {
  26. const esp_partition_info_t *part = &partition_table[num_parts];
  27. if (part->magic == ESP_PARTITION_MAGIC) {
  28. const esp_partition_pos_t *pos = &part->pos;
  29. if (pos->offset > chip_size || pos->offset + pos->size > chip_size) {
  30. if (log_errors) {
  31. ESP_LOGE(TAG, "partition %d invalid - offset 0x%x size 0x%x exceeds flash chip size 0x%x",
  32. num_parts, pos->offset, pos->size, chip_size);
  33. }
  34. return ESP_ERR_INVALID_SIZE;
  35. }
  36. } else if (part->magic == ESP_PARTITION_MAGIC_MD5) {
  37. if (md5_found) {
  38. if (log_errors) {
  39. ESP_LOGE(TAG, "Only one MD5 checksum is allowed");
  40. }
  41. return ESP_ERR_INVALID_STATE;
  42. }
  43. struct MD5Context context;
  44. unsigned char digest[16];
  45. MD5Init(&context);
  46. MD5Update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t));
  47. MD5Final(digest, &context);
  48. unsigned char *md5sum = ((unsigned char *) part) + 16; // skip the 2B magic number and the 14B fillup bytes
  49. if (memcmp(md5sum, digest, sizeof(digest)) != 0) {
  50. if (log_errors) {
  51. ESP_LOGE(TAG, "Incorrect MD5 checksum");
  52. }
  53. return ESP_ERR_INVALID_STATE;
  54. }
  55. //MD5 checksum matches and we continue with the next interation in
  56. //order to detect the end of the partition table
  57. md5_found = 1;
  58. } else if (part->magic == 0xFFFF
  59. && part->type == PART_TYPE_END
  60. && part->subtype == PART_SUBTYPE_END) {
  61. ESP_LOGD(TAG, "partition table verified, %d entries", num_parts);
  62. *num_partitions = num_parts - md5_found; //do not count the partition where the MD5 checksum is held
  63. return ESP_OK;
  64. } else {
  65. if (log_errors) {
  66. ESP_LOGE(TAG, "partition %d invalid magic number 0x%x", num_parts, part->magic);
  67. }
  68. return ESP_ERR_INVALID_STATE;
  69. }
  70. }
  71. if (log_errors) {
  72. ESP_LOGE(TAG, "partition table has no terminating entry, not valid");
  73. }
  74. return ESP_ERR_INVALID_STATE;
  75. }