2021-05-09 22:35:07 -04:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-01-31 08:45:12 -05:00
|
|
|
#include <string.h>
|
2016-11-11 01:00:34 -05:00
|
|
|
#include "esp_flash_partitions.h"
|
|
|
|
#include "esp_log.h"
|
2020-07-21 05:01:28 -04:00
|
|
|
#include "esp_rom_md5.h"
|
2021-09-28 02:12:56 -04:00
|
|
|
#include "esp_rom_spiflash.h"
|
2016-11-11 01:00:34 -05:00
|
|
|
|
|
|
|
static const char *TAG = "flash_parts";
|
|
|
|
|
2018-07-13 01:23:04 -04:00
|
|
|
esp_err_t esp_partition_table_verify(const esp_partition_info_t *partition_table, bool log_errors, int *num_partitions)
|
2016-11-11 01:00:34 -05:00
|
|
|
{
|
2018-01-31 08:45:12 -05:00
|
|
|
int md5_found = 0;
|
2020-11-16 23:48:35 -05:00
|
|
|
size_t num_parts;
|
2018-01-31 08:45:12 -05:00
|
|
|
uint32_t chip_size = g_rom_flashchip.chip_size;
|
|
|
|
*num_partitions = 0;
|
2016-11-11 01:00:34 -05:00
|
|
|
|
2018-01-31 08:45:12 -05:00
|
|
|
for (num_parts = 0; num_parts < ESP_PARTITION_TABLE_MAX_ENTRIES; num_parts++) {
|
|
|
|
const esp_partition_info_t *part = &partition_table[num_parts];
|
|
|
|
|
|
|
|
if (part->magic == ESP_PARTITION_MAGIC) {
|
|
|
|
const esp_partition_pos_t *pos = &part->pos;
|
|
|
|
if (pos->offset > chip_size || pos->offset + pos->size > chip_size) {
|
|
|
|
if (log_errors) {
|
|
|
|
ESP_LOGE(TAG, "partition %d invalid - offset 0x%x size 0x%x exceeds flash chip size 0x%x",
|
|
|
|
num_parts, pos->offset, pos->size, chip_size);
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_SIZE;
|
|
|
|
}
|
|
|
|
} else if (part->magic == ESP_PARTITION_MAGIC_MD5) {
|
|
|
|
if (md5_found) {
|
|
|
|
if (log_errors) {
|
|
|
|
ESP_LOGE(TAG, "Only one MD5 checksum is allowed");
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
2016-12-29 21:16:22 -05:00
|
|
|
|
2021-11-06 05:21:57 -04:00
|
|
|
md5_context_t context;
|
2018-01-31 08:45:12 -05:00
|
|
|
unsigned char digest[16];
|
2020-07-21 05:01:28 -04:00
|
|
|
esp_rom_md5_init(&context);
|
|
|
|
esp_rom_md5_update(&context, (unsigned char *) partition_table, num_parts * sizeof(esp_partition_info_t));
|
|
|
|
esp_rom_md5_final(digest, &context);
|
2018-01-31 08:45:12 -05:00
|
|
|
|
2021-02-03 19:12:04 -05:00
|
|
|
unsigned char *md5sum = ((unsigned char *) part) + ESP_PARTITION_MD5_OFFSET;
|
2018-01-31 08:45:12 -05:00
|
|
|
|
|
|
|
if (memcmp(md5sum, digest, sizeof(digest)) != 0) {
|
|
|
|
if (log_errors) {
|
|
|
|
ESP_LOGE(TAG, "Incorrect MD5 checksum");
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
//MD5 checksum matches and we continue with the next interation in
|
|
|
|
//order to detect the end of the partition table
|
|
|
|
md5_found = 1;
|
|
|
|
} else if (part->magic == 0xFFFF
|
|
|
|
&& part->type == PART_TYPE_END
|
|
|
|
&& part->subtype == PART_SUBTYPE_END) {
|
|
|
|
ESP_LOGD(TAG, "partition table verified, %d entries", num_parts);
|
|
|
|
*num_partitions = num_parts - md5_found; //do not count the partition where the MD5 checksum is held
|
|
|
|
return ESP_OK;
|
|
|
|
} else {
|
|
|
|
if (log_errors) {
|
|
|
|
ESP_LOGE(TAG, "partition %d invalid magic number 0x%x", num_parts, part->magic);
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
2016-12-29 21:16:22 -05:00
|
|
|
}
|
|
|
|
}
|
2016-11-11 01:00:34 -05:00
|
|
|
|
2018-01-31 08:45:12 -05:00
|
|
|
if (log_errors) {
|
|
|
|
ESP_LOGE(TAG, "partition table has no terminating entry, not valid");
|
|
|
|
}
|
|
|
|
return ESP_ERR_INVALID_STATE;
|
2016-11-11 01:00:34 -05:00
|
|
|
}
|