flash_partitions.c 3.4 KB

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