mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
bootloader_common: Fix esp_partition_get_sha256(), add unit tests
Regression in 438d513a95a Reported here: https://esp32.com/viewtopic.php?f=13&t=13250&p=52460
This commit is contained in:
parent
2303dfaffc
commit
5139934767
@ -229,18 +229,19 @@ static esp_err_t image_load(esp_image_load_mode_t mode, const esp_partition_pos_
|
||||
if (sha_handle != NULL) {
|
||||
bootloader_sha256_finish(sha_handle, NULL);
|
||||
}
|
||||
|
||||
if (data->image.hash_appended) {
|
||||
const void *hash = bootloader_mmap(data->start_addr + data->image_len - HASH_LEN, HASH_LEN);
|
||||
if (hash == NULL) {
|
||||
err = ESP_FAIL;
|
||||
goto err;
|
||||
}
|
||||
memcpy(data->image_digest, hash, HASH_LEN);
|
||||
bootloader_munmap(hash);
|
||||
}
|
||||
sha_handle = NULL;
|
||||
} // verify_sha
|
||||
} //verify_sha
|
||||
|
||||
// Separately, if there's a hash appended to the image then copy it out to the data->image_digest field
|
||||
if (data->image.hash_appended) {
|
||||
const void *hash = bootloader_mmap(data->start_addr + data->image_len - HASH_LEN, HASH_LEN);
|
||||
if (hash == NULL) {
|
||||
err = ESP_FAIL;
|
||||
goto err;
|
||||
}
|
||||
memcpy(data->image_digest, hash, HASH_LEN);
|
||||
bootloader_munmap(hash);
|
||||
}
|
||||
} // do_verify
|
||||
|
||||
if (err != ESP_OK) {
|
||||
|
@ -22,6 +22,8 @@
|
||||
|
||||
#include <unity.h>
|
||||
#include <test_utils.h>
|
||||
#include <esp_image_format.h>
|
||||
#include <esp_log.h>
|
||||
#include <esp_partition.h>
|
||||
#include <esp_attr.h>
|
||||
|
||||
@ -66,3 +68,57 @@ TEST_CASE("Test erase partition", "[spi_flash][esp_flash]")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static bool s_test_nonzero_sha_of_partition(const esp_partition_t *part, bool allow_invalid_image)
|
||||
{
|
||||
uint8_t sha256[32] = { 0 };
|
||||
|
||||
TEST_ASSERT_NOT_NULL(part);
|
||||
|
||||
esp_err_t err = esp_partition_get_sha256(part, sha256);
|
||||
|
||||
if (allow_invalid_image && err == ESP_ERR_IMAGE_INVALID) {
|
||||
printf("App partition at 0x%x doesn't hold a valid app\n", part->address);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Otherwise, err should be ESP_OK
|
||||
ESP_ERROR_CHECK(err);
|
||||
|
||||
ESP_LOG_BUFFER_HEX("sha", sha256, sizeof(sha256));
|
||||
|
||||
for (int i = 0; i < sizeof(sha256); i++) {
|
||||
if (sha256[i] != 0) {
|
||||
return true; // At least one non-zero byte!
|
||||
}
|
||||
}
|
||||
TEST_FAIL_MESSAGE("SHA-256 of data partition should not be all zeroes");
|
||||
abort(); // unreachable
|
||||
}
|
||||
|
||||
TEST_CASE("Test esp_partition_get_sha256() with data", "[spi_flash]")
|
||||
{
|
||||
const esp_partition_t *part = get_test_data_partition();
|
||||
s_test_nonzero_sha_of_partition(part, false);
|
||||
}
|
||||
|
||||
TEST_CASE("Test esp_partition_get_sha256() with app", "[spi_flash]")
|
||||
{
|
||||
bool found_valid_app = false;
|
||||
esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_APP,
|
||||
ESP_PARTITION_SUBTYPE_ANY,
|
||||
NULL);
|
||||
TEST_ASSERT_NOT_NULL(it); /* has to be at least one app partition */
|
||||
|
||||
while (it != NULL) {
|
||||
const esp_partition_t *part = esp_partition_get(it);
|
||||
printf("Hashing app partition at 0x%x\n", part->address);
|
||||
bool valid = s_test_nonzero_sha_of_partition(part, true);
|
||||
found_valid_app |= valid;
|
||||
|
||||
it = esp_partition_next(it);
|
||||
}
|
||||
|
||||
TEST_ASSERT_MESSAGE(found_valid_app, "At least one app partition should be a valid app partition");
|
||||
}
|
||||
|
||||
|
@ -453,7 +453,7 @@ UT_034:
|
||||
|
||||
UT_035:
|
||||
extends: .unit_test_template
|
||||
parallel: 30
|
||||
parallel: 31
|
||||
tags:
|
||||
- ESP32S2BETA_IDF
|
||||
- UT_T1_1
|
||||
|
Loading…
x
Reference in New Issue
Block a user