flash_partitions.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 "esp_flash_partitions.h"
  14. #include "esp_log.h"
  15. #include "rom/spi_flash.h"
  16. static const char *TAG = "flash_parts";
  17. esp_err_t esp_partition_table_basic_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions)
  18. {
  19. int num_parts;
  20. uint32_t chip_size = g_rom_flashchip.chip_size;
  21. *num_partitions = 0;
  22. for(num_parts = 0; num_parts < ESP_PARTITION_TABLE_MAX_ENTRIES; num_parts++) {
  23. const esp_partition_info_t *part = &partition_table[num_parts];
  24. if (part->magic == 0xFFFF
  25. && part->type == PART_TYPE_END
  26. && part->subtype == PART_SUBTYPE_END) {
  27. /* TODO: check md5 */
  28. ESP_LOGD(TAG, "partition table verified, %d entries", num_parts);
  29. *num_partitions = num_parts;
  30. return ESP_OK;
  31. }
  32. if (part->magic != ESP_PARTITION_MAGIC) {
  33. if (log_errors) {
  34. ESP_LOGE(TAG, "partition %d invalid magic number 0x%x", num_parts, part->magic);
  35. }
  36. return ESP_ERR_INVALID_STATE;
  37. }
  38. const esp_partition_pos_t *pos = &part->pos;
  39. if (pos->offset > chip_size || pos->offset + pos->size > chip_size) {
  40. if (log_errors) {
  41. ESP_LOGE(TAG, "partition %d invalid - offset 0x%x size 0x%x exceeds flash chip size 0x%x",
  42. num_parts, pos->offset, pos->size, chip_size);
  43. }
  44. return ESP_ERR_INVALID_SIZE;
  45. }
  46. }
  47. if (log_errors) {
  48. ESP_LOGE(TAG, "partition table has no terminating entry, not valid");
  49. }
  50. return ESP_ERR_INVALID_STATE;
  51. }