From b3d77e3ea5d6a8592291872c7e9f1738d74c9a13 Mon Sep 17 00:00:00 2001 From: kohait00 Date: Mon, 30 Oct 2023 13:40:56 +0100 Subject: [PATCH] fix(app_update): avoid erasing an extra sector than the actual required size OTA update used to fail if `firmware_size == partition_size`, because the code was trying to erase one additional sector beyond the space reserved for the firmware partition. This commit fixes the problem and OTA update can work if the firmware size exactly matches the allocated partition size. Closes https://github.com/espressif/esp-idf/pull/12460 --- components/app_update/esp_ota_ops.c | 9 +++++++-- components/app_update/include/esp_ota_ops.h | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/components/app_update/esp_ota_ops.c b/components/app_update/esp_ota_ops.c index 6b80abab30..e15e06f70e 100644 --- a/components/app_update/esp_ota_ops.c +++ b/components/app_update/esp_ota_ops.c @@ -195,13 +195,18 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size) return ESP_ERR_INVALID_ARG; } + if (size == 0) { + ESP_LOGD(TAG, "write data size is 0"); + return ESP_OK; + } + // find ota handle in linked list for (it = LIST_FIRST(&s_ota_ops_entries_head); it != NULL; it = LIST_NEXT(it, entries)) { if (it->handle == handle) { if (it->need_erase) { // must erase the partition before writing to it - uint32_t first_sector = it->wrote_size / SPI_FLASH_SEC_SIZE; - uint32_t last_sector = (it->wrote_size + size) / SPI_FLASH_SEC_SIZE; + uint32_t first_sector = it->wrote_size / SPI_FLASH_SEC_SIZE; // first affected sector + uint32_t last_sector = (it->wrote_size + size - 1) / SPI_FLASH_SEC_SIZE; // last affected sector ret = ESP_OK; if ((it->wrote_size % SPI_FLASH_SEC_SIZE) == 0) { diff --git a/components/app_update/include/esp_ota_ops.h b/components/app_update/include/esp_ota_ops.h index 1364b543a8..9aa83552e9 100644 --- a/components/app_update/include/esp_ota_ops.h +++ b/components/app_update/include/esp_ota_ops.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -110,7 +110,7 @@ esp_err_t esp_ota_begin(const esp_partition_t* partition, size_t image_size, esp * @param size Size of data buffer in bytes. * * @return - * - ESP_OK: Data was written to flash successfully. + * - ESP_OK: Data was written to flash successfully, or size = 0 * - ESP_ERR_INVALID_ARG: handle is invalid. * - ESP_ERR_OTA_VALIDATE_FAILED: First byte of image contains invalid app image magic byte. * - ESP_ERR_FLASH_OP_TIMEOUT or ESP_ERR_FLASH_OP_FAIL: Flash write failed.