From 5b344610c90aba27de449770e51c73a2b2edf0e2 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 29 Sep 2020 12:00:41 +0530 Subject: [PATCH 1/3] bootloader_support: fix issue in memory mapping for getting app descriptor For getting secure_version field in anti rollback case, bootloader tries to map whole firmware partition but fails for cases where partition size is beyond available MMU free pages capacity. Fix here insures to map only required length upto application descriptor size in firmware partition. Closes https://github.com/espressif/esp-idf/issues/5911 --- components/bootloader_support/src/bootloader_common.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index 729da47b2b..5236aa0a54 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -183,13 +183,15 @@ esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t return ESP_ERR_INVALID_ARG; } - const uint8_t *image = bootloader_mmap(partition->offset, partition->size); + const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t); + const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t); + const uint8_t *image = bootloader_mmap(partition->offset, mmap_size); if (image == NULL) { - ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, partition->size); + ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size); return ESP_FAIL; } - memcpy(app_desc, image + sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t), sizeof(esp_app_desc_t)); + memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t)); bootloader_munmap(image); if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) { From ab988ab5caf2637ef2f4377c0e87fbcee325ecf6 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Tue, 29 Sep 2020 12:28:17 +0530 Subject: [PATCH 2/3] bootloader_support: move anti rollback API to common loader section API `bootloader_common_get_partition_description` is required for anti-rollback feature and should be part of common loader code. --- .../src/bootloader_common.c | 24 ------------ .../src/bootloader_common_loader.c | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/components/bootloader_support/src/bootloader_common.c b/components/bootloader_support/src/bootloader_common.c index 5236aa0a54..94faff986a 100644 --- a/components/bootloader_support/src/bootloader_common.c +++ b/components/bootloader_support/src/bootloader_common.c @@ -177,30 +177,6 @@ esp_err_t bootloader_common_get_sha256_of_partition (uint32_t address, uint32_t return bootloader_sha256_flash_contents(address, size, out_sha_256); } -esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc) -{ - if (partition == NULL || app_desc == NULL || partition->offset == 0) { - return ESP_ERR_INVALID_ARG; - } - - const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t); - const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t); - const uint8_t *image = bootloader_mmap(partition->offset, mmap_size); - if (image == NULL) { - ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size); - return ESP_FAIL; - } - - memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t)); - bootloader_munmap(image); - - if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) { - return ESP_ERR_NOT_FOUND; - } - - return ESP_OK; -} - void bootloader_common_vddsdio_configure(void) { #if CONFIG_BOOTLOADER_VDDSDIO_BOOST_1_9V diff --git a/components/bootloader_support/src/bootloader_common_loader.c b/components/bootloader_support/src/bootloader_common_loader.c index f0c65fadca..cc02754ea7 100644 --- a/components/bootloader_support/src/bootloader_common_loader.c +++ b/components/bootloader_support/src/bootloader_common_loader.c @@ -1,3 +1,17 @@ +// Copyright 2020 Espressif Systems (Shanghai) Co., Ltd. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + #include "string.h" #include "sdkconfig.h" #include "esp_err.h" @@ -20,6 +34,7 @@ #include "esp_image_format.h" #include "bootloader_sha.h" #include "sys/param.h" +#include "bootloader_flash_priv.h" #define ESP_PARTITION_HASH_LEN 32 /* SHA-256 digest length */ @@ -97,6 +112,29 @@ int bootloader_common_select_otadata(const esp_ota_select_entry_t *two_otadata, return active_otadata; } +esp_err_t bootloader_common_get_partition_description(const esp_partition_pos_t *partition, esp_app_desc_t *app_desc) +{ + if (partition == NULL || app_desc == NULL || partition->offset == 0) { + return ESP_ERR_INVALID_ARG; + } + + const uint32_t app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t); + const uint32_t mmap_size = app_desc_offset + sizeof(esp_app_desc_t); + const uint8_t *image = bootloader_mmap(partition->offset, mmap_size); + if (image == NULL) { + ESP_LOGE(TAG, "bootloader_mmap(0x%x, 0x%x) failed", partition->offset, mmap_size); + return ESP_FAIL; + } + + memcpy(app_desc, image + app_desc_offset, sizeof(esp_app_desc_t)); + bootloader_munmap(image); + + if (app_desc->magic_word != ESP_APP_DESC_MAGIC_WORD) { + return ESP_ERR_NOT_FOUND; + } + + return ESP_OK; +} #if defined( CONFIG_BOOTLOADER_SKIP_VALIDATE_IN_DEEP_SLEEP ) || defined( CONFIG_BOOTLOADER_CUSTOM_RESERVE_RTC ) From e08a3e1d63ee78d8f6e6eac07534fcc00bdaae18 Mon Sep 17 00:00:00 2001 From: Mahavir Jain Date: Wed, 30 Sep 2020 11:23:50 +0530 Subject: [PATCH 3/3] test_apps: add anti rollback configuration for bootloader build --- .../system/bootloader_sections/partitions_example.csv | 7 +++++++ .../system/bootloader_sections/sdkconfig.ci.anti_rollback | 5 +++++ 2 files changed, 12 insertions(+) create mode 100644 tools/test_apps/system/bootloader_sections/partitions_example.csv create mode 100644 tools/test_apps/system/bootloader_sections/sdkconfig.ci.anti_rollback diff --git a/tools/test_apps/system/bootloader_sections/partitions_example.csv b/tools/test_apps/system/bootloader_sections/partitions_example.csv new file mode 100644 index 0000000000..42b0227c71 --- /dev/null +++ b/tools/test_apps/system/bootloader_sections/partitions_example.csv @@ -0,0 +1,7 @@ +# Name, Type, SubType, Offset, Size, Flags +# Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap +nvs, data, nvs, , 0x4000, +otadata, data, ota, , 0x2000, +phy_init, data, phy, , 0x1000, +ota_0, app, ota_0, , 1M, +ota_1, app, ota_1, , 1M, diff --git a/tools/test_apps/system/bootloader_sections/sdkconfig.ci.anti_rollback b/tools/test_apps/system/bootloader_sections/sdkconfig.ci.anti_rollback new file mode 100644 index 0000000000..e2914f2e94 --- /dev/null +++ b/tools/test_apps/system/bootloader_sections/sdkconfig.ci.anti_rollback @@ -0,0 +1,5 @@ +CONFIG_BOOTLOADER_APP_ROLLBACK_ENABLE=y +CONFIG_BOOTLOADER_APP_ANTI_ROLLBACK=y +CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +CONFIG_PARTITION_TABLE_CUSTOM=y +CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions_example.csv"