1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "esp_flash_partitions.h"
16 #include "rom/spi_flash.h"
18 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
)
23 uint32_t chip_size
= g_rom_flashchip
.chip_size
;
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
];
29 if (part
->magic
== 0xFFFF
30 && part
->type
== PART_TYPE_END
31 && part
->subtype
== PART_SUBTYPE_END
) {
33 ESP_LOGD(TAG
, "partition table verified, %d entries", num_parts
);
34 *num_partitions
= num_parts
;
38 if (part
->magic
!= ESP_PARTITION_MAGIC
) {
40 ESP_LOGE(TAG
, "partition %d invalid magic number 0x%x", num_parts
, part
->magic
);
42 return ESP_ERR_INVALID_STATE
;
45 const esp_partition_pos_t
*pos
= &part
->pos
;
46 if (pos
->offset
> chip_size
|| pos
->offset
+ pos
->size
> chip_size
) {
48 ESP_LOGE(TAG
, "partition %d invalid - offset 0x%x size 0x%x exceeds flash chip size 0x%x",
49 num_parts
, pos
->offset
, pos
->size
, chip_size
);
51 return ESP_ERR_INVALID_SIZE
;
56 ESP_LOGE(TAG
, "partition table has no terminating entry, not valid");
58 return ESP_ERR_INVALID_STATE
;