From d5da858cd9af0adc6c9ce7a64cd699af93b21d38 Mon Sep 17 00:00:00 2001 From: Sonika Rathi Date: Thu, 26 Sep 2024 13:01:24 +0800 Subject: [PATCH] refactor: remove dependency on spi_flash include for sector size --- components/app_update/esp_ota_ops.c | 22 ++++++------ .../test_app_update/main/test_switch_ota.c | 8 ++--- .../test_apps/master/main/test_spi_bus_lock.c | 7 ++-- .../esp_partition/include/esp_partition.h | 10 +++++- components/esp_partition/partition_linux.c | 5 +++ components/esp_partition/partition_target.c | 5 +++ components/nvs_flash/CMakeLists.txt | 1 - .../nvs_host_test/main/CMakeLists.txt | 3 +- .../nvs_host_test/main/test_fixtures.hpp | 21 +++++++----- .../host_test/nvs_host_test/main/test_nvs.cpp | 1 + .../main/test_nvs_initialization.cpp | 9 +++-- .../nvs_page_test/main/CMakeLists.txt | 3 +- .../nvs_page_test/main/nvs_page_test.cpp | 1 + .../nvs_page_test/main/test_fixtures.hpp | 7 ++-- components/nvs_flash/src/nvs_api.cpp | 3 +- components/nvs_flash/src/nvs_page.cpp | 7 ++-- components/nvs_flash/src/nvs_page.hpp | 3 +- .../nvs_flash/src/nvs_partition_manager.cpp | 10 +++--- components/nvs_flash/src/nvs_storage.cpp | 1 + .../nvs_flash/test_apps/main/CMakeLists.txt | 2 +- .../nvs_flash/test_apps/main/test_nvs.c | 19 +++++------ components/nvs_flash/test_nvs_host/Makefile | 1 + .../test_nvs_host/spi_flash_emulation.cpp | 12 ++++--- .../test_nvs_host/spi_flash_emulation.h | 34 ++++++++++++------- .../nvs_flash/test_nvs_host/test_fixtures.hpp | 19 ++++++----- .../nvs_flash/test_nvs_host/test_nvs.cpp | 8 +++-- .../test_nvs_host/test_partition_linux.cpp | 12 +++++++ .../test_nvs_host/test_partition_manager.cpp | 8 +++-- .../test_spi_flash_emulation.cpp | 13 ++++--- .../esp_flash/main/test_esp_flash_drv.c | 19 +++++------ .../mspi_test/main/test_large_flash_writes.c | 7 ++-- .../mspi_test/main/test_read_write.c | 10 +++--- components/spiffs/CMakeLists.txt | 2 +- components/spiffs/esp_spiffs.c | 1 - components/wear_levelling/Partition.cpp | 4 +-- components/wear_levelling/SPI_Flash.cpp | 14 ++++---- .../private_include/Partition.h | 1 - .../test_apps/main/CMakeLists.txt | 2 +- .../wear_levelling/test_apps/main/test_wl.c | 9 +++-- components/wear_levelling/wear_levelling.cpp | 4 +-- .../partition_ops/main/CMakeLists.txt | 3 +- .../partition_api/partition_ops/main/main.c | 3 +- 42 files changed, 198 insertions(+), 136 deletions(-) create mode 100644 components/nvs_flash/test_nvs_host/test_partition_linux.cpp diff --git a/components/app_update/esp_ota_ops.c b/components/app_update/esp_ota_ops.c index 7d204d99ae..fdea76bddd 100644 --- a/components/app_update/esp_ota_ops.c +++ b/components/app_update/esp_ota_ops.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -80,7 +80,7 @@ static const esp_partition_t *read_otadata(esp_ota_select_entry_t *two_otadata) return NULL; } else { memcpy(&two_otadata[0], result, sizeof(esp_ota_select_entry_t)); - memcpy(&two_otadata[1], result + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t)); + memcpy(&two_otadata[1], result + otadata_partition->erase_size, sizeof(esp_ota_select_entry_t)); esp_partition_munmap(ota_data_map); } return otadata_partition; @@ -149,7 +149,7 @@ esp_err_t esp_ota_begin(const esp_partition_t *partition, size_t image_size, esp if ((image_size == 0) || (image_size == OTA_SIZE_UNKNOWN)) { ret = esp_partition_erase_range(partition, 0, partition->size); } else { - const int aligned_erase_size = (image_size + SPI_FLASH_SEC_SIZE - 1) & ~(SPI_FLASH_SEC_SIZE - 1); + const int aligned_erase_size = (image_size + partition->erase_size - 1) & ~(partition->erase_size - 1); ret = esp_partition_erase_range(partition, 0, aligned_erase_size); } if (ret != ESP_OK) { @@ -192,14 +192,14 @@ esp_err_t esp_ota_write(esp_ota_handle_t handle, const void *data, size_t size) 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; // first affected sector - uint32_t last_sector = (it->wrote_size + size - 1) / SPI_FLASH_SEC_SIZE; // last affected sector + uint32_t first_sector = it->wrote_size / it->part->erase_size; // first affected sector + uint32_t last_sector = (it->wrote_size + size - 1) / it->part->erase_size; // last affected sector ret = ESP_OK; - if ((it->wrote_size % SPI_FLASH_SEC_SIZE) == 0) { - ret = esp_partition_erase_range(it->part, it->wrote_size, ((last_sector - first_sector) + 1) * SPI_FLASH_SEC_SIZE); + if ((it->wrote_size % it->part->erase_size) == 0) { + ret = esp_partition_erase_range(it->part, it->wrote_size, ((last_sector - first_sector) + 1) * it->part->erase_size); } else if (first_sector != last_sector) { - ret = esp_partition_erase_range(it->part, (first_sector + 1) * SPI_FLASH_SEC_SIZE, (last_sector - first_sector) * SPI_FLASH_SEC_SIZE); + ret = esp_partition_erase_range(it->part, (first_sector + 1) * it->part->erase_size, (last_sector - first_sector) * it->part->erase_size); } if (ret != ESP_OK) { return ret; @@ -369,11 +369,11 @@ static esp_err_t rewrite_ota_seq(esp_ota_select_entry_t *two_otadata, uint32_t s two_otadata[sec_id].ota_seq = seq; two_otadata[sec_id].crc = bootloader_common_ota_select_crc(&two_otadata[sec_id]); - esp_err_t ret = esp_partition_erase_range(ota_data_partition, sec_id * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE); + esp_err_t ret = esp_partition_erase_range(ota_data_partition, sec_id * ota_data_partition->erase_size, ota_data_partition->erase_size); if (ret != ESP_OK) { return ret; } else { - return esp_partition_write(ota_data_partition, SPI_FLASH_SEC_SIZE * sec_id, &two_otadata[sec_id], sizeof(esp_ota_select_entry_t)); + return esp_partition_write(ota_data_partition, ota_data_partition->erase_size * sec_id, &two_otadata[sec_id], sizeof(esp_ota_select_entry_t)); } } @@ -906,7 +906,7 @@ esp_err_t esp_ota_erase_last_boot_app_partition(void) } int sec_id = inactive_otadata; - err = esp_partition_erase_range(ota_data_partition, sec_id * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE); + err = esp_partition_erase_range(ota_data_partition, sec_id * ota_data_partition->erase_size, ota_data_partition->erase_size); if (err != ESP_OK) { return err; } diff --git a/components/app_update/test_apps/test_app_update/main/test_switch_ota.c b/components/app_update/test_apps/test_app_update/main/test_switch_ota.c index 24c038e7e7..f71276668d 100644 --- a/components/app_update/test_apps/test_app_update/main/test_switch_ota.c +++ b/components/app_update/test_apps/test_app_update/main/test_switch_ota.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -177,7 +177,7 @@ static void erase_ota_data(void) { const esp_partition_t *data_partition = esp_partition_find_first(ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_OTA, NULL); TEST_ASSERT_NOT_EQUAL(NULL, data_partition); - TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * SPI_FLASH_SEC_SIZE)); + TEST_ESP_OK(esp_partition_erase_range(data_partition, 0, 2 * data_partition->erase_size)); } /* @brief Reboots ESP using mode deep sleep. This mode guaranty that RTC_DATA_ATTR variables is not reset. @@ -251,7 +251,7 @@ static void get_ota_data(const esp_partition_t *otadata_partition, esp_ota_selec TEST_ASSERT_NOT_EQUAL(NULL, ota_select_map); memcpy(ota_data_0, ota_select_map, sizeof(esp_ota_select_entry_t)); - memcpy(ota_data_1, (uint8_t *)ota_select_map + SPI_FLASH_SEC_SIZE, sizeof(esp_ota_select_entry_t)); + memcpy(ota_data_1, (uint8_t *)ota_select_map + otadata_partition->erase_size, sizeof(esp_ota_select_entry_t)); bootloader_munmap(ota_select_map); } } @@ -264,7 +264,7 @@ static void get_ota_data(const esp_partition_t *otadata_partition, esp_ota_selec */ static void write_ota_data(const esp_partition_t *otadata_partition, esp_ota_select_entry_t *ota_data, int sec_id) { - esp_partition_write(otadata_partition, SPI_FLASH_SEC_SIZE * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t)); + esp_partition_write(otadata_partition, otadata_partition->erase_size * sec_id, &ota_data[sec_id], sizeof(esp_ota_select_entry_t)); } /* @brief Makes a corrupt of ota_data. diff --git a/components/esp_driver_spi/test_apps/master/main/test_spi_bus_lock.c b/components/esp_driver_spi/test_apps/master/main/test_spi_bus_lock.c index 4b7276b6f0..9c2d9ccc5e 100644 --- a/components/esp_driver_spi/test_apps/master/main/test_spi_bus_lock.c +++ b/components/esp_driver_spi/test_apps/master/main/test_spi_bus_lock.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,7 +10,6 @@ #include "esp_flash_spi_init.h" #include "test_utils.h" #include "test_spi_utils.h" -#include "spi_flash_mmap.h" #include "unity.h" #if CONFIG_IDF_TARGET_ESP32 @@ -146,7 +145,7 @@ static void write_large_buffer(esp_flash_t *chip, const esp_partition_t *part, c { printf("Erasing chip %p, %d bytes\n", chip, length); - TEST_ESP_OK(esp_flash_erase_region(chip, part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE - 1))); + TEST_ESP_OK(esp_flash_erase_region(chip, part->address, (length + part->erase_size) & ~(part->erase_size - 1))); printf("Writing chip %p, %d bytes from source %p\n", chip, length, source); // note writing to unaligned address @@ -195,7 +194,7 @@ void spi_task4(void* arg) ESP_LOGI(TAG, "Testing chip %p...", chip); const esp_partition_t *part = get_test_data_partition(); - TEST_ASSERT(part->size > test_len + 2 + SPI_FLASH_SEC_SIZE); + TEST_ASSERT(part->size > test_len + 2 + part->erase_size); write_large_buffer(chip, part, source_buf, test_len); read_and_check(chip, part, source_buf, test_len); diff --git a/components/esp_partition/include/esp_partition.h b/components/esp_partition/include/esp_partition.h index f52d146945..972fbded9e 100644 --- a/components/esp_partition/include/esp_partition.h +++ b/components/esp_partition/include/esp_partition.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -464,6 +464,14 @@ esp_err_t esp_partition_deregister_external(const esp_partition_t* partition); */ void esp_partition_unload_all(void); +/** + * @brief Get the main flash sector size + * @return + * - SPI_FLASH_SEC_SIZE - For esp32xx target + * - ESP_PARTITION_EMULATED_SECTOR_SIZE - For linux target + */ +uint32_t esp_partition_get_main_flash_sector_size(void); + #ifdef __cplusplus } #endif diff --git a/components/esp_partition/partition_linux.c b/components/esp_partition/partition_linux.c index cf6d6ccc9f..c85ac0ced8 100644 --- a/components/esp_partition/partition_linux.c +++ b/components/esp_partition/partition_linux.c @@ -547,6 +547,11 @@ esp_partition_file_mmap_ctrl_t *esp_partition_get_file_mmap_ctrl_act(void) return &s_esp_partition_file_mmap_ctrl_act; } +uint32_t esp_partition_get_main_flash_sector_size(void) +{ + return ESP_PARTITION_EMULATED_SECTOR_SIZE; +} + #ifdef CONFIG_ESP_PARTITION_ENABLE_STATS // timing data for ESP8266, 160MHz CPU frequency, 80MHz flash frequency // all values in microseconds diff --git a/components/esp_partition/partition_target.c b/components/esp_partition/partition_target.c index ab04e7f9da..04f8fdf06c 100644 --- a/components/esp_partition/partition_target.c +++ b/components/esp_partition/partition_target.c @@ -233,3 +233,8 @@ bool esp_partition_main_flash_region_safe(size_t addr, size_t size) } return true; } + +uint32_t esp_partition_get_main_flash_sector_size(void) +{ + return SPI_FLASH_SEC_SIZE; +} diff --git a/components/nvs_flash/CMakeLists.txt b/components/nvs_flash/CMakeLists.txt index 6e7c6544b4..bba0f5e42e 100644 --- a/components/nvs_flash/CMakeLists.txt +++ b/components/nvs_flash/CMakeLists.txt @@ -25,7 +25,6 @@ idf_component_register(SRCS "${srcs}" REQUIRES "${requires}" PRIV_REQUIRES "${priv_requires}" INCLUDE_DIRS "include" - "../spi_flash/include" PRIV_INCLUDE_DIRS "private_include") # If we use the linux target, we need to redirect the crc functions to the linux diff --git a/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt b/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt index 534f968c53..27959af8ee 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt +++ b/components/nvs_flash/host_test/nvs_host_test/main/CMakeLists.txt @@ -9,7 +9,8 @@ idf_component_register(SRCS "test_nvs.cpp" "../../../private_include" "../../../../mbedtls/mbedtls/include" WHOLE_ARCHIVE - REQUIRES nvs_flash) + REQUIRES nvs_flash + PRIV_REQUIRES spi_flash) if(CMAKE_C_COMPILER_ID MATCHES "Clang") target_compile_options(${COMPONENT_LIB} PRIVATE -std=gnu++20) diff --git a/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp b/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp index 50f92b4dcf..48b565ecd7 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp +++ b/components/nvs_flash/host_test/nvs_host_test/main/test_fixtures.hpp @@ -9,6 +9,7 @@ #include #include #include +#include "esp_partition.h" class PartitionEmulationFixture { public: @@ -22,8 +23,9 @@ public: FAIL("Failed to initialize esp_partition_file_mmap"); } - esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE; - esp_partition.size = (start_sector + sector_size) * SPI_FLASH_SEC_SIZE; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + esp_partition.address = start_sector * sec_size; + esp_partition.size = (start_sector + sector_size) * sec_size; esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE; esp_partition.type = ESP_PARTITION_TYPE_DATA; esp_partition.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS; @@ -42,6 +44,7 @@ public: off_t size = -1; void *p_buff = nullptr; char const *fail_msg = nullptr; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); do { // get file size @@ -57,7 +60,7 @@ public: } // check if file fits into the partitiion - if (size > sector_size * SPI_FLASH_SEC_SIZE) { + if (size > sector_size * sec_size) { fail_msg = "file with partition content doesn't fit into the partition"; break; } @@ -82,7 +85,7 @@ public: } // erase whole partition - if (ESP_OK != esp_partition_erase_range(&esp_partition, 0, sector_size * SPI_FLASH_SEC_SIZE)) { + if (ESP_OK != esp_partition_erase_range(&esp_partition, 0, sector_size * sec_size)) { fail_msg = "cannot erase partition prior to write partition binary from file"; break; } @@ -123,7 +126,8 @@ public: // absolute sectorNumber is used here bool erase(size_t sectorNumber) { - size_t offset = sectorNumber * SPI_FLASH_SEC_SIZE; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + size_t offset = sectorNumber * sec_size; // check the upper bound esp_partition_file_mmap_ctrl_t *p_ctrl = esp_partition_get_file_mmap_ctrl_act(); @@ -135,7 +139,7 @@ public: // esp_partition_erase_range uses offset relative to the beginning of partition return (esp_partition_erase_range(&esp_partition, offset - esp_partition.address, - SPI_FLASH_SEC_SIZE) == ESP_OK); + sec_size) == ESP_OK); } ~PartitionEmulationFixture() @@ -175,9 +179,10 @@ public: ) : PartitionEmulationFixture(start_sector1, sector_size1, partition_name1), esp_partition2() { + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); // for 2nd partition - esp_partition2.address = start_sector2 * SPI_FLASH_SEC_SIZE; - esp_partition2.size = (start_sector2 + sector_size2) * SPI_FLASH_SEC_SIZE; + esp_partition2.address = start_sector2 * sec_size; + esp_partition2.size = (start_sector2 + sector_size2) * sec_size; esp_partition2.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE; esp_partition2.type = ESP_PARTITION_TYPE_DATA; esp_partition2.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS; diff --git a/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp b/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp index dbca6e0843..afc3dc5a74 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp +++ b/components/nvs_flash/host_test/nvs_host_test/main/test_nvs.cpp @@ -18,6 +18,7 @@ #include #include #include "test_fixtures.hpp" +#include "spi_flash_mmap.h" #define TEST_ESP_ERR(rc, res) CHECK((rc) == (res)) #define TEST_ESP_OK(rc) CHECK((rc) == ESP_OK) diff --git a/components/nvs_flash/host_test/nvs_host_test/main/test_nvs_initialization.cpp b/components/nvs_flash/host_test/nvs_host_test/main/test_nvs_initialization.cpp index cd52f19a81..5250f8cde7 100644 --- a/components/nvs_flash/host_test/nvs_host_test/main/test_nvs_initialization.cpp +++ b/components/nvs_flash/host_test/nvs_host_test/main/test_nvs_initialization.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -9,6 +9,7 @@ #include "nvs_partition.hpp" #include "test_fixtures.hpp" #include +#include "esp_partition.h" TEST_CASE("nvs_flash_init_partition_ptr fails due to nullptr arg", "[nvs_custom_part]") { @@ -22,13 +23,15 @@ TEST_CASE("nvs_flash_init_partition_ptr inits one partition", "[nvs_custom_part] { const uint32_t NVS_FLASH_SECTOR = 6; const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + uint8_t *p_part_desc_addr_start; CHECK(esp_partition_file_mmap((const uint8_t **)&p_part_desc_addr_start) == ESP_OK); esp_partition_t partition = {}; strcpy(partition.label, "test"); - partition.address = NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE; - partition.size = NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE; + partition.address = NVS_FLASH_SECTOR * sec_size; + partition.size = NVS_FLASH_SECTOR_COUNT_MIN * sec_size; CHECK(nvs_flash_init_partition_ptr(&partition) == ESP_OK); CHECK(nvs::NVSPartitionManager::get_instance()->lookup_storage_from_name("test") != nullptr); diff --git a/components/nvs_flash/host_test/nvs_page_test/main/CMakeLists.txt b/components/nvs_flash/host_test/nvs_page_test/main/CMakeLists.txt index 6fc75e63d6..ef19b49589 100644 --- a/components/nvs_flash/host_test/nvs_page_test/main/CMakeLists.txt +++ b/components/nvs_flash/host_test/nvs_page_test/main/CMakeLists.txt @@ -4,7 +4,8 @@ idf_component_register(SRCS "nvs_page_test.cpp" "${CMAKE_CURRENT_SOURCE_DIR}/../../../src" PRIV_INCLUDE_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/../../../private_include" - REQUIRES nvs_flash) + REQUIRES nvs_flash + PRIV_REQUIRES spi_flash) target_compile_options(${COMPONENT_LIB} PUBLIC --coverage) target_link_libraries(${COMPONENT_LIB} --coverage) diff --git a/components/nvs_flash/host_test/nvs_page_test/main/nvs_page_test.cpp b/components/nvs_flash/host_test/nvs_page_test/main/nvs_page_test.cpp index f3bfcf70fc..88d38a512e 100644 --- a/components/nvs_flash/host_test/nvs_page_test/main/nvs_page_test.cpp +++ b/components/nvs_flash/host_test/nvs_page_test/main/nvs_page_test.cpp @@ -9,6 +9,7 @@ static const char* TAG = "nvs_page_host_test"; #include "unity.h" #include "test_fixtures.hpp" #include "esp_log.h" +#include "spi_flash_mmap.h" #if defined(SEGGER_H) && defined(GLOBAL_H) NVS_GUARD_SYSVIEW_MACRO_EXPANSION_PUSH(); diff --git a/components/nvs_flash/host_test/nvs_page_test/main/test_fixtures.hpp b/components/nvs_flash/host_test/nvs_page_test/main/test_fixtures.hpp index 647711745b..acdc89c6df 100644 --- a/components/nvs_flash/host_test/nvs_page_test/main/test_fixtures.hpp +++ b/components/nvs_flash/host_test/nvs_page_test/main/test_fixtures.hpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -37,8 +37,9 @@ public: TEST_FAIL_MESSAGE("Failed to initialize esp_partition_file_mmap"); } - esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE; - esp_partition.size = (start_sector + sector_count) * SPI_FLASH_SEC_SIZE; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + esp_partition.address = start_sector * sec_size; + esp_partition.size = (start_sector + sector_count) * sec_size; esp_partition.erase_size = ESP_PARTITION_EMULATED_SECTOR_SIZE; esp_partition.type = ESP_PARTITION_TYPE_DATA; esp_partition.subtype = ESP_PARTITION_SUBTYPE_DATA_NVS; diff --git a/components/nvs_flash/src/nvs_api.cpp b/components/nvs_flash/src/nvs_api.cpp index effed329dd..2bbb460e06 100644 --- a/components/nvs_flash/src/nvs_api.cpp +++ b/components/nvs_flash/src/nvs_api.cpp @@ -108,9 +108,10 @@ extern "C" esp_err_t nvs_flash_init_partition_ptr(const esp_partition_t *partiti return ESP_ERR_NO_MEM; } + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); esp_err_t init_res = NVSPartitionManager::get_instance()->init_custom(part, 0, - partition->size / SPI_FLASH_SEC_SIZE); + partition->size / sec_size); if (init_res != ESP_OK) { delete part; diff --git a/components/nvs_flash/src/nvs_page.cpp b/components/nvs_flash/src/nvs_page.cpp index d9d6662616..f818ddb56f 100644 --- a/components/nvs_flash/src/nvs_page.cpp +++ b/components/nvs_flash/src/nvs_page.cpp @@ -9,11 +9,14 @@ #include #include #include "nvs_internal.h" +#include "esp_partition.h" namespace nvs { Page::Page() : mPartition(nullptr) { } +const uint32_t nvs::Page::SEC_SIZE = esp_partition_get_main_flash_sector_size(); + uint32_t Page::Header::calculateCrc32() { return esp_rom_crc32_le(0xffffffff, @@ -49,7 +52,7 @@ esp_err_t Page::load(Partition *partition, uint32_t sectorNumber) return ESP_ERR_NO_MEM; } - for (uint32_t i = 0; i < SPI_FLASH_SEC_SIZE; i += 4 * BLOCK_SIZE) { + for (uint32_t i = 0; i < SEC_SIZE; i += 4 * BLOCK_SIZE) { rc = mPartition->read_raw(mBaseAddress + i, block, 4 * BLOCK_SIZE); if (rc != ESP_OK) { mState = PageState::INVALID; @@ -1020,7 +1023,7 @@ esp_err_t Page::setVersion(uint8_t ver) esp_err_t Page::erase() { - auto rc = mPartition->erase_range(mBaseAddress, SPI_FLASH_SEC_SIZE); + auto rc = mPartition->erase_range(mBaseAddress, SEC_SIZE); if (rc != ESP_OK) { mState = PageState::INVALID; return rc; diff --git a/components/nvs_flash/src/nvs_page.hpp b/components/nvs_flash/src/nvs_page.hpp index 4aff32ad01..8d9c11b002 100644 --- a/components/nvs_flash/src/nvs_page.hpp +++ b/components/nvs_flash/src/nvs_page.hpp @@ -12,7 +12,6 @@ #include #include #include -#include "spi_flash_mmap.h" #include "compressed_enum_table.hpp" #include "intrusive_list.h" #include "nvs_item_hash_list.hpp" @@ -34,7 +33,7 @@ public: static const uint32_t ESB_WRITTEN = 0x1; static const uint32_t ESB_ERASED = 0x2; - static const uint32_t SEC_SIZE = SPI_FLASH_SEC_SIZE; + static const uint32_t SEC_SIZE; static const size_t ENTRY_SIZE = 32; static const size_t ENTRY_COUNT = 126; diff --git a/components/nvs_flash/src/nvs_partition_manager.cpp b/components/nvs_flash/src/nvs_partition_manager.cpp index eecde60ea5..f267aa668f 100644 --- a/components/nvs_flash/src/nvs_partition_manager.cpp +++ b/components/nvs_flash/src/nvs_partition_manager.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -33,6 +33,7 @@ esp_err_t NVSPartitionManager::init_partition(const char *partition_label) } uint32_t size; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); Storage* mStorage; mStorage = lookup_storage_from_name(partition_label); @@ -40,7 +41,7 @@ esp_err_t NVSPartitionManager::init_partition(const char *partition_label) return ESP_OK; } - NVS_ASSERT_OR_RETURN(SPI_FLASH_SEC_SIZE != 0, ESP_FAIL); + NVS_ASSERT_OR_RETURN(sec_size != 0, ESP_FAIL); NVSPartition *p = nullptr; esp_err_t result = partition_lookup::lookup_nvs_partition(partition_label, &p); @@ -51,7 +52,7 @@ esp_err_t NVSPartitionManager::init_partition(const char *partition_label) size = p->get_size(); - result = init_custom(p, 0, size / SPI_FLASH_SEC_SIZE); + result = init_custom(p, 0, size / sec_size); if (result != ESP_OK) { goto error; } @@ -127,8 +128,9 @@ esp_err_t NVSPartitionManager::secure_init_partition(const char *part_name, nvs_ } uint32_t size = p->get_size(); + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); - result = init_custom(p, 0, size / SPI_FLASH_SEC_SIZE); + result = init_custom(p, 0, size / sec_size); if (result != ESP_OK) { delete p; return result; diff --git a/components/nvs_flash/src/nvs_storage.cpp b/components/nvs_flash/src/nvs_storage.cpp index 0cf28149ab..e5db0caefe 100644 --- a/components/nvs_flash/src/nvs_storage.cpp +++ b/components/nvs_flash/src/nvs_storage.cpp @@ -21,6 +21,7 @@ #endif // !ESP_PLATFORM #include "esp_log.h" +#include "spi_flash_mmap.h" #define TAG "nvs_storage" #if defined(SEGGER_H) && defined(GLOBAL_H) diff --git a/components/nvs_flash/test_apps/main/CMakeLists.txt b/components/nvs_flash/test_apps/main/CMakeLists.txt index 22e8d8fbd0..59b17ad7f1 100644 --- a/components/nvs_flash/test_apps/main/CMakeLists.txt +++ b/components/nvs_flash/test_apps/main/CMakeLists.txt @@ -1,6 +1,6 @@ idf_component_register(SRC_DIRS "." PRIV_REQUIRES cmock test_utils nvs_flash nvs_sec_provider - bootloader_support spi_flash esp_psram + bootloader_support esp_psram EMBED_TXTFILES encryption_keys.bin partition_encrypted.bin partition_encrypted_hmac.bin sample.bin WHOLE_ARCHIVE) diff --git a/components/nvs_flash/test_apps/main/test_nvs.c b/components/nvs_flash/test_apps/main/test_nvs.c index 9e7958c6ac..e16691057d 100644 --- a/components/nvs_flash/test_apps/main/test_nvs.c +++ b/components/nvs_flash/test_apps/main/test_nvs.c @@ -17,7 +17,6 @@ #include "esp_log.h" #include "esp_partition.h" #include "esp_system.h" -#include "spi_flash_mmap.h" #include "nvs.h" #include "nvs_flash.h" @@ -664,20 +663,20 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena ESP_PARTITION_TYPE_DATA, ESP_PARTITION_SUBTYPE_DATA_NVS_KEYS, NULL); assert(key_part && "partition table must have a KEY partition"); - TEST_ASSERT_TRUE((nvs_key_end - nvs_key_start - 1) == SPI_FLASH_SEC_SIZE); + TEST_ASSERT_TRUE((nvs_key_end - nvs_key_start - 1) == key_part->erase_size); ESP_ERROR_CHECK(esp_partition_erase_range(key_part, 0, key_part->size)); - for (int i = 0; i < key_part->size; i+= SPI_FLASH_SEC_SIZE) { - ESP_ERROR_CHECK( esp_partition_write(key_part, i, nvs_key_start + i, SPI_FLASH_SEC_SIZE) ); + for (int i = 0; i < key_part->size; i+= key_part->erase_size) { + ESP_ERROR_CHECK( esp_partition_write(key_part, i, nvs_key_start + i, key_part->erase_size) ); } const int content_size = nvs_data_sch0_end - nvs_data_sch0_start - 1; - TEST_ASSERT_TRUE((content_size % SPI_FLASH_SEC_SIZE) == 0); + TEST_ASSERT_TRUE((content_size % key_part->erase_size) == 0); const int size_to_write = MIN(content_size, nvs_part->size); - for (int i = 0; i < size_to_write; i+= SPI_FLASH_SEC_SIZE) { - ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_sch0_start + i, SPI_FLASH_SEC_SIZE) ); + for (int i = 0; i < size_to_write; i+= nvs_part->erase_size) { + ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_sch0_start + i, nvs_part->erase_size) ); } err = nvs_flash_read_security_cfg(key_part, &xts_cfg); @@ -686,11 +685,11 @@ TEST_CASE("test nvs apis for nvs partition generator utility with encryption ena extern const char nvs_data_sch1_end[] asm("_binary_partition_encrypted_hmac_bin_end"); const int content_size = nvs_data_sch1_end - nvs_data_sch1_start - 1; - TEST_ASSERT_TRUE((content_size % SPI_FLASH_SEC_SIZE) == 0); + TEST_ASSERT_TRUE((content_size % nvs_part->erase_size) == 0); const int size_to_write = MIN(content_size, nvs_part->size); - for (int i = 0; i < size_to_write; i+= SPI_FLASH_SEC_SIZE) { - ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_sch1_start + i, SPI_FLASH_SEC_SIZE) ); + for (int i = 0; i < size_to_write; i+= nvs_part->erase_size) { + ESP_ERROR_CHECK( esp_partition_write(nvs_part, i, nvs_data_sch1_start + i, nvs_part->erase_size) ); } nvs_sec_scheme_t *scheme_cfg = nvs_flash_get_default_security_scheme(); diff --git a/components/nvs_flash/test_nvs_host/Makefile b/components/nvs_flash/test_nvs_host/Makefile index e1849a6cb9..3ddb65185f 100644 --- a/components/nvs_flash/test_nvs_host/Makefile +++ b/components/nvs_flash/test_nvs_host/Makefile @@ -24,6 +24,7 @@ SOURCE_FILES = \ test_nvs.cpp \ test_nvs_partition.cpp \ test_partition_manager.cpp \ + test_partition_linux.cpp \ main.cpp SOURCE_FILES_C = ../../esp_rom/linux/esp_rom_crc.c esp_err_check_mock.c diff --git a/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp b/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp index 297572bc2d..fc153edf30 100644 --- a/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp +++ b/components/nvs_flash/test_nvs_host/spi_flash_emulation.cpp @@ -1,10 +1,11 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include "esp_partition.h" #include "spi_flash_emulation.h" +#include "spi_flash_mmap.h" static SpiFlashEmulator* s_emulator = nullptr; @@ -20,16 +21,17 @@ esp_err_t esp_partition_erase_range(const esp_partition_t* partition, return ESP_ERR_FLASH_OP_TIMEOUT; } - if (size % SPI_FLASH_SEC_SIZE != 0) { + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + if (size % sec_size != 0) { return ESP_ERR_INVALID_SIZE; } - if (offset % SPI_FLASH_SEC_SIZE != 0) { + if (offset % sec_size != 0) { return ESP_ERR_INVALID_ARG; } - size_t start_sector = offset / SPI_FLASH_SEC_SIZE; - size_t num_sectors = size / SPI_FLASH_SEC_SIZE; + size_t start_sector = offset / sec_size; + size_t num_sectors = size / sec_size; for (size_t sector = start_sector; sector < (start_sector + num_sectors); sector++) { if (!s_emulator->erase(sector)) { return ESP_ERR_FLASH_OP_FAIL; diff --git a/components/nvs_flash/test_nvs_host/spi_flash_emulation.h b/components/nvs_flash/test_nvs_host/spi_flash_emulation.h index 58942ec29c..13db5ef17e 100644 --- a/components/nvs_flash/test_nvs_host/spi_flash_emulation.h +++ b/components/nvs_flash/test_nvs_host/spi_flash_emulation.h @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -10,7 +10,7 @@ #include #include #include -#include "spi_flash_mmap.h" +#include "esp_partition.h" #include "catch.hpp" class SpiFlashEmulator; @@ -22,7 +22,8 @@ class SpiFlashEmulator public: SpiFlashEmulator(size_t sectorCount) : mUpperSectorBound(sectorCount) { - mData.resize(sectorCount * SPI_FLASH_SEC_SIZE / 4, 0xffffffff); + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + mData.resize(sectorCount * sec_size / 4, 0xffffffff); mEraseCnt.resize(sectorCount); spi_flash_emulator_set(this); } @@ -30,9 +31,10 @@ public: SpiFlashEmulator(const char *filename) { load(filename); + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); // At least one page should be free, hence we create mData of size of 2 sectors. - mData.resize(mData.size() + SPI_FLASH_SEC_SIZE / 4, 0xffffffff); - mUpperSectorBound = mData.size() * 4 / SPI_FLASH_SEC_SIZE; + mData.resize(mData.size() + sec_size / 4, 0xffffffff); + mUpperSectorBound = mData.size() * 4 / sec_size; spi_flash_emulator_set(this); } @@ -59,7 +61,8 @@ public: bool write(size_t dstAddr, const uint32_t* src, size_t size) { - uint32_t sectorNumber = dstAddr/SPI_FLASH_SEC_SIZE; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + uint32_t sectorNumber = dstAddr/sec_size; if (sectorNumber < mLowerSectorBound || sectorNumber >= mUpperSectorBound) { WARN("invalid flash operation detected: erase sector=" << sectorNumber); return false; @@ -95,7 +98,8 @@ public: bool erase(size_t sectorNumber) { - size_t offset = sectorNumber * SPI_FLASH_SEC_SIZE / 4; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + size_t offset = sectorNumber * sec_size / 4; if (offset > mData.size()) { return false; } @@ -109,7 +113,7 @@ public: return false; } - std::fill_n(begin(mData) + offset, SPI_FLASH_SEC_SIZE / 4, 0xffffffff); + std::fill_n(begin(mData) + offset, sec_size / 4, 0xffffffff); ++mEraseOps; mEraseCnt[sectorNumber]++; @@ -142,22 +146,26 @@ public: void load(const char* filename) { + const uint32_t sector_size = esp_partition_get_main_flash_sector_size(); + size_t sec_size = sector_size; FILE* f = fopen(filename, "rb"); fseek(f, 0, SEEK_END); off_t size = ftell(f); - assert(size % SPI_FLASH_SEC_SIZE == 0); + assert(size % sec_size == 0); mData.resize(size / sizeof(uint32_t)); fseek(f, 0, SEEK_SET); - auto s = fread(mData.data(), SPI_FLASH_SEC_SIZE, size / SPI_FLASH_SEC_SIZE, f); - assert(s == static_cast(size / SPI_FLASH_SEC_SIZE)); + auto s = fread(mData.data(), sec_size, size / sec_size, f); + assert(s == static_cast(size / sec_size)); fclose(f); } void save(const char* filename) { + const uint32_t sector_size = esp_partition_get_main_flash_sector_size(); + size_t sec_size = sector_size; FILE* f = fopen(filename, "wb"); - auto n_sectors = mData.size() * sizeof(uint32_t) / SPI_FLASH_SEC_SIZE; - auto s = fwrite(mData.data(), SPI_FLASH_SEC_SIZE, n_sectors, f); + auto n_sectors = mData.size() * sizeof(uint32_t) / sec_size; + auto s = fwrite(mData.data(), sec_size, n_sectors, f); assert(s == n_sectors); fclose(f); } diff --git a/components/nvs_flash/test_nvs_host/test_fixtures.hpp b/components/nvs_flash/test_nvs_host/test_fixtures.hpp index 4a50a1daad..4ca5720dc5 100644 --- a/components/nvs_flash/test_nvs_host/test_fixtures.hpp +++ b/components/nvs_flash/test_nvs_host/test_fixtures.hpp @@ -1,11 +1,12 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include "nvs_partition.hpp" #include "nvs_encrypted_partition.hpp" #include "spi_flash_emulation.h" +#include "spi_flash_mmap.h" #include "nvs.h" class PartitionEmulation : public nvs::Partition { @@ -65,16 +66,17 @@ public: esp_err_t erase_range(size_t dst_offset, size_t size) override { - if (size % SPI_FLASH_SEC_SIZE != 0) { + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + if (size % sec_size != 0) { return ESP_ERR_INVALID_SIZE; } - if (dst_offset % SPI_FLASH_SEC_SIZE != 0) { + if (dst_offset % sec_size != 0) { return ESP_ERR_INVALID_ARG; } - size_t start_sector = dst_offset / SPI_FLASH_SEC_SIZE; - size_t num_sectors = size / SPI_FLASH_SEC_SIZE; + size_t start_sector = dst_offset / sec_size; + size_t num_sectors = size / sec_size; for (size_t sector = start_sector; sector < (start_sector + num_sectors); sector++) { if (!flash_emu->erase(sector)) { return ESP_ERR_FLASH_OP_FAIL; @@ -116,7 +118,7 @@ struct PartitionEmulationFixture { uint32_t sector_size = 1, const char *partition_name = NVS_DEFAULT_PART_NAME) : emu(start_sector + sector_size), - part(&emu, start_sector * SPI_FLASH_SEC_SIZE, sector_size * SPI_FLASH_SEC_SIZE, partition_name) { + part(&emu, start_sector * esp_partition_get_main_flash_sector_size(), sector_size * esp_partition_get_main_flash_sector_size(), partition_name) { } ~PartitionEmulationFixture() { } @@ -133,8 +135,9 @@ struct EncryptedPartitionFixture { const char *partition_name = NVS_DEFAULT_PART_NAME) : esp_partition(), emu(start_sector + sector_size), part(&esp_partition) { - esp_partition.address = start_sector * SPI_FLASH_SEC_SIZE; - esp_partition.size = sector_size * SPI_FLASH_SEC_SIZE; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + esp_partition.address = start_sector * sec_size; + esp_partition.size = sector_size * sec_size; strncpy(esp_partition.label, partition_name, PART_NAME_MAX_SIZE); assert(part.init(cfg) == ESP_OK); } diff --git a/components/nvs_flash/test_nvs_host/test_nvs.cpp b/components/nvs_flash/test_nvs_host/test_nvs.cpp index 0738e92f7d..06d312e483 100644 --- a/components/nvs_flash/test_nvs_host/test_nvs.cpp +++ b/components/nvs_flash/test_nvs_host/test_nvs.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -44,10 +44,11 @@ static void check_nvs_part_gen_args(SpiFlashEmulator *spi_flash_emulator, { nvs_handle_t handle; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); esp_partition_t esp_part; esp_part.encrypted = false; // we're not testing generic flash encryption here, only the legacy NVS encryption esp_part.address = 0; - esp_part.size = size * SPI_FLASH_SEC_SIZE; + esp_part.size = size * sec_size; strncpy(esp_part.label, part_name, PART_NAME_MAX_SIZE); unique_ptr part; @@ -136,10 +137,11 @@ static void check_nvs_part_gen_args_mfg(SpiFlashEmulator *spi_flash_emulator, { nvs_handle_t handle; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); esp_partition_t esp_part; esp_part.encrypted = false; // we're not testing generic flash encryption here, only the legacy NVS encryption esp_part.address = 0; - esp_part.size = size * SPI_FLASH_SEC_SIZE; + esp_part.size = size * sec_size; strncpy(esp_part.label, part_name, PART_NAME_MAX_SIZE); unique_ptr part; diff --git a/components/nvs_flash/test_nvs_host/test_partition_linux.cpp b/components/nvs_flash/test_nvs_host/test_partition_linux.cpp new file mode 100644 index 0000000000..f0e15e1367 --- /dev/null +++ b/components/nvs_flash/test_nvs_host/test_partition_linux.cpp @@ -0,0 +1,12 @@ +/* + * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "esp_private/partition_linux.h" +#include "esp_partition.h" + +uint32_t esp_partition_get_main_flash_sector_size(void) +{ + return ESP_PARTITION_EMULATED_SECTOR_SIZE; +} diff --git a/components/nvs_flash/test_nvs_host/test_partition_manager.cpp b/components/nvs_flash/test_nvs_host/test_partition_manager.cpp index 61f2f4aefb..cc86e5182c 100644 --- a/components/nvs_flash/test_nvs_host/test_partition_manager.cpp +++ b/components/nvs_flash/test_nvs_host/test_partition_manager.cpp @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -12,6 +12,7 @@ #include "spi_flash_emulation.h" #include "nvs_test_api.h" #include "test_fixtures.hpp" +#include "esp_partition.h" /* TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]") @@ -19,8 +20,9 @@ TEST_CASE("Partition manager initializes multiple partitions", "[partition_mgr]" const uint32_t NVS_FLASH_SECTOR = 6; const uint32_t NVS_FLASH_SECTOR_COUNT_MIN = 3; SpiFlashEmulator emu(10); - PartitionEmulation part_0(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test1"); - PartitionEmulation part_1(&emu, NVS_FLASH_SECTOR * SPI_FLASH_SEC_SIZE, NVS_FLASH_SECTOR_COUNT_MIN * SPI_FLASH_SEC_SIZE, "test2"); + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + PartitionEmulation part_0(&emu, NVS_FLASH_SECTOR * sec_size, NVS_FLASH_SECTOR_COUNT_MIN * sec_size, "test1"); + PartitionEmulation part_1(&emu, NVS_FLASH_SECTOR * sec_size, NVS_FLASH_SECTOR_COUNT_MIN * sec_size, "test2"); REQUIRE(nvs::NVSPartitionManager::get_instance()->init_custom(&part_0, NVS_FLASH_SECTOR, NVS_FLASH_SECTOR_COUNT_MIN) == ESP_OK); diff --git a/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp b/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp index ddfdbed094..46fae9f3c0 100644 --- a/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp +++ b/components/nvs_flash/test_nvs_host/test_spi_flash_emulation.cpp @@ -1,12 +1,12 @@ /* - * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include "catch.hpp" -#include "spi_flash_mmap.h" #include "esp_partition.h" #include "spi_flash_emulation.h" +#include "spi_flash_mmap.h" #include template @@ -26,7 +26,8 @@ TEST_CASE("flash starts with all bytes == 0xff", "[spi_flash_emu]") { FlashEmuFixture f(4); - uint8_t sector[SPI_FLASH_SEC_SIZE]; + uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + uint8_t sector[sec_size]; for (int i = 0; i < 4; ++i) { CHECK(esp_partition_read(&f.esp_part, 0, sector, sizeof(sector)) == ESP_OK); @@ -71,7 +72,8 @@ TEST_CASE("after erase the sector is set to 0xff", "[spi_flash_emu]") CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2)); CHECK(f.emu.words()[4096 / 4 - 1] == val2); - CHECK(esp_partition_erase_range(&f.esp_part, 0, SPI_FLASH_SEC_SIZE) == ESP_OK); + uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + CHECK(esp_partition_erase_range(&f.esp_part, 0, sec_size) == ESP_OK); CHECK(f.emu.words()[0] == 0xffffffff); CHECK(range_empty_n(f.emu.words() + 1, 4096 / 4 - 2)); @@ -158,7 +160,8 @@ TEST_CASE("read/write/erase operation times are calculated correctly", "[spi_fla CHECK(f.emu.getTotalTime() == (205+417)/2); f.emu.clearStats(); - esp_partition_erase_range(&f.esp_part, 0, SPI_FLASH_SEC_SIZE); + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + esp_partition_erase_range(&f.esp_part, 0, sec_size); CHECK(f.emu.getEraseOps() == 1); CHECK(f.emu.getTotalTime() == 37142); } diff --git a/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c b/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c index b9e2071244..032c053add 100644 --- a/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c +++ b/components/spi_flash/test_apps/esp_flash/main/test_esp_flash_drv.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Unlicense OR CC0-1.0 */ @@ -15,7 +15,6 @@ #include "esp_private/spi_common_internal.h" #include "esp_flash_spi_init.h" #include "memspi_host_driver.h" -#include "spi_flash_mmap.h" #include #include "esp_log.h" #include "test_utils.h" @@ -24,12 +23,12 @@ #include "soc/io_mux_reg.h" #include "sdkconfig.h" +#include "esp_spi_flash_counters.h" #include "esp_partition.h" #include "esp_rom_gpio.h" #include "esp_rom_sys.h" #include "esp_timer.h" #include "test_esp_flash_def.h" -#include "spi_flash_mmap.h" #include "esp_private/spi_flash_os.h" #include "ccomp_timer.h" @@ -656,7 +655,7 @@ void test_permutations_chip(const flashtest_config_t* config) // Get test partition, and locate temporary partitions according to the default one const esp_partition_t* test_part = get_test_data_partition(); const int length = sizeof(large_const_buffer); - TEST_ASSERT(test_part->size > length + 2 + SPI_FLASH_SEC_SIZE); + TEST_ASSERT(test_part->size > length + 2 + test_part->erase_size); esp_partition_t part[2] = {}; part[0] = *test_part; @@ -738,7 +737,7 @@ static void write_large_buffer(const esp_partition_t *part, const uint8_t *sourc esp_flash_t* chip = part->flash_chip; printf("Writing chip %p %p, %u bytes from source %p\n", chip, (void*)part->address, length, source); - ESP_ERROR_CHECK( esp_flash_erase_region(chip, part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE - 1)) ); + ESP_ERROR_CHECK( esp_flash_erase_region(chip, part->address, (length + part->erase_size) & ~(part->erase_size - 1)) ); // note writing to unaligned address ESP_ERROR_CHECK( esp_flash_write(chip, source, part->address + 1, length) ); @@ -771,7 +770,7 @@ static void read_and_check(const esp_partition_t *part, const uint8_t *source, s static void test_write_large_buffer(const esp_partition_t* part, const uint8_t *source, size_t length) { - TEST_ASSERT(part->size > length + 2 + SPI_FLASH_SEC_SIZE); + TEST_ASSERT(part->size > length + 2 + part->erase_size); write_large_buffer(part, source, length); read_and_check(part, source, length); @@ -804,7 +803,7 @@ static uint32_t time_measure_end(time_meas_ctx_t* ctx) static uint32_t measure_erase(const esp_partition_t* part) { - const int total_len = SPI_FLASH_SEC_SIZE * TEST_SECTORS; + const int total_len = part->erase_size * TEST_SECTORS; time_meas_ctx_t time_ctx = {.name = "erase", .len = total_len}; time_measure_start(&time_ctx); @@ -816,7 +815,7 @@ static uint32_t measure_erase(const esp_partition_t* part) // should called after measure_erase static uint32_t measure_write(const char* name, const esp_partition_t* part, const uint8_t* data_to_write, int seg_len) { - const int total_len = SPI_FLASH_SEC_SIZE; + const int total_len = part->erase_size; time_meas_ctx_t time_ctx = {.name = name, .len = total_len * TEST_TIMES}; time_measure_start(&time_ctx); @@ -839,7 +838,7 @@ static uint32_t measure_write(const char* name, const esp_partition_t* part, con static uint32_t measure_read(const char* name, const esp_partition_t* part, uint8_t* data_read, int seg_len) { - const int total_len = SPI_FLASH_SEC_SIZE; + const int total_len = part->erase_size; time_meas_ctx_t time_ctx = {.name = name, .len = total_len * TEST_TIMES}; time_measure_start(&time_ctx); @@ -896,7 +895,7 @@ static const char* get_chip_vendor(uint32_t id) static void test_flash_read_write_performance(const esp_partition_t *part) { esp_flash_t* chip = part->flash_chip; - const int total_len = SPI_FLASH_SEC_SIZE; + const int total_len = part->erase_size; uint8_t *data_to_write = heap_caps_malloc(total_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); uint8_t *data_read = heap_caps_malloc(total_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); diff --git a/components/spi_flash/test_apps/mspi_test/main/test_large_flash_writes.c b/components/spi_flash/test_apps/mspi_test/main/test_large_flash_writes.c index a23438e9ec..48ea79381d 100644 --- a/components/spi_flash/test_apps/mspi_test/main/test_large_flash_writes.c +++ b/components/spi_flash/test_apps/mspi_test/main/test_large_flash_writes.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2010-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -14,7 +14,6 @@ #include #include "unity.h" -#include "spi_flash_mmap.h" #include "esp_log.h" #include "esp_rom_spiflash.h" #include "esp_private/cache_utils.h" @@ -63,14 +62,14 @@ TEST_CASE("Test flash write large RAM buffer", "[spi_flash][esp_flash]") static void test_write_large_buffer(const uint8_t *source, size_t length) { const esp_partition_t *part = get_test_data_partition(); - TEST_ASSERT(part->size > length + 2 + SPI_FLASH_SEC_SIZE); + TEST_ASSERT(part->size > length + 2 + part->erase_size); printf("Writing %d bytes from source %p\n", length, source); uint8_t *buf = malloc(length); TEST_ASSERT_NOT_NULL(buf); - TEST_ESP_OK( esp_flash_erase_region(NULL, part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE-1)) ); + TEST_ESP_OK( esp_flash_erase_region(NULL, part->address, (length + part->erase_size) & ~(part->erase_size-1)) ); // note writing to unaligned address TEST_ESP_OK( esp_flash_write(NULL, source, part->address + 1, length) ); diff --git a/components/spi_flash/test_apps/mspi_test/main/test_read_write.c b/components/spi_flash/test_apps/mspi_test/main/test_read_write.c index d7b6f6997e..8711e1d885 100644 --- a/components/spi_flash/test_apps/mspi_test/main/test_read_write.c +++ b/components/spi_flash/test_apps/mspi_test/main/test_read_write.c @@ -342,12 +342,12 @@ TEST_CASE("esp_flash_write can write from external RAM buffer", "[spi_flash]") /* Write to flash from buf_ext */ const esp_partition_t *part = get_test_data_partition(); - TEST_ESP_OK(esp_flash_erase_region(NULL, part->address & ~(0x10000 - 1), SPI_FLASH_SEC_SIZE)); - TEST_ESP_OK(esp_flash_write(NULL, buf_ext, part->address, SPI_FLASH_SEC_SIZE)); + TEST_ESP_OK(esp_flash_erase_region(NULL, part->address & ~(0x10000 - 1), part->erase_size)); + TEST_ESP_OK(esp_flash_write(NULL, buf_ext, part->address, part->erase_size)); /* Read back to buf_int and compare */ - TEST_ESP_OK(esp_flash_read(NULL, buf_int, part->address, SPI_FLASH_SEC_SIZE)); - TEST_ASSERT_EQUAL(0, memcmp(buf_ext, buf_int, SPI_FLASH_SEC_SIZE)); + TEST_ESP_OK(esp_flash_read(NULL, buf_int, part->address, part->erase_size)); + TEST_ASSERT_EQUAL(0, memcmp(buf_ext, buf_int, part->erase_size)); free(buf_ext); free(buf_int); @@ -367,7 +367,7 @@ TEST_CASE("spi_flash_read less than 16 bytes into buffer in external RAM", "[spi } const esp_partition_t *part = get_test_data_partition(); - TEST_ESP_OK(esp_flash_erase_region(NULL, part->address & ~(0x10000 - 1), SPI_FLASH_SEC_SIZE)); + TEST_ESP_OK(esp_flash_erase_region(NULL, part->address & ~(0x10000 - 1), part->erase_size)); TEST_ESP_OK(esp_flash_write(NULL, data_8, part->address, MIN_BLOCK_SIZE)); TEST_ESP_OK(esp_flash_read(NULL, buf_ext_8, part->address, MIN_BLOCK_SIZE)); TEST_ESP_OK(esp_flash_read(NULL, buf_int_8, part->address, MIN_BLOCK_SIZE)); diff --git a/components/spiffs/CMakeLists.txt b/components/spiffs/CMakeLists.txt index 142ad9ec29..ed85c16ac1 100644 --- a/components/spiffs/CMakeLists.txt +++ b/components/spiffs/CMakeLists.txt @@ -17,7 +17,7 @@ idf_component_register(SRCS ${srcs} INCLUDE_DIRS "include" PRIV_INCLUDE_DIRS "." "spiffs/src" REQUIRES esp_partition - PRIV_REQUIRES ${pr} spi_flash) + PRIV_REQUIRES ${pr}) if(CMAKE_C_COMPILER_ID MATCHES "GNU") set_source_files_properties(spiffs/src/spiffs_nucleus.c PROPERTIES COMPILE_FLAGS -Wno-stringop-truncation) diff --git a/components/spiffs/esp_spiffs.c b/components/spiffs/esp_spiffs.c index 2d34a19894..e1cffc6a5f 100644 --- a/components/spiffs/esp_spiffs.c +++ b/components/spiffs/esp_spiffs.c @@ -9,7 +9,6 @@ #include "spiffs_nucleus.h" #include "esp_log.h" #include "esp_partition.h" -#include "spi_flash_mmap.h" #include "esp_image_format.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" diff --git a/components/wear_levelling/Partition.cpp b/components/wear_levelling/Partition.cpp index 210ef56d9f..f99dee43be 100644 --- a/components/wear_levelling/Partition.cpp +++ b/components/wear_levelling/Partition.cpp @@ -23,7 +23,7 @@ size_t Partition::get_flash_size() esp_err_t Partition::erase_sector(size_t sector) { esp_err_t result = ESP_OK; - result = erase_range(sector * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE); + result = erase_range(sector * this->partition->erase_size, this->partition->erase_size); return result; } @@ -54,7 +54,7 @@ esp_err_t Partition::read(size_t src_addr, void *dest, size_t size) size_t Partition::get_sector_size() { - return SPI_FLASH_SEC_SIZE; + return this->partition->erase_size; } bool Partition::is_readonly() diff --git a/components/wear_levelling/SPI_Flash.cpp b/components/wear_levelling/SPI_Flash.cpp index 7f0d2d045d..4435f0e0df 100644 --- a/components/wear_levelling/SPI_Flash.cpp +++ b/components/wear_levelling/SPI_Flash.cpp @@ -1,14 +1,14 @@ /* - * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ #include "esp_log.h" #include "SPI_Flash.h" -#include "spi_flash_mmap.h" #include "esp_flash.h" #include +#include "esp_partition.h" static const char *TAG = "spi_flash"; @@ -25,7 +25,8 @@ size_t SPI_Flash::get_flash_size() esp_err_t SPI_Flash::erase_sector(size_t sector) { - esp_err_t result = esp_flash_erase_region(NULL, sector * SPI_FLASH_SEC_SIZE, SPI_FLASH_SEC_SIZE);; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + esp_err_t result = esp_flash_erase_region(NULL, sector * sec_size, sec_size);; if (result == ESP_OK) { ESP_LOGV(TAG, "erase_sector - sector=0x%08" PRIx32 ", result=0x%08x", (uint32_t) sector, result); } else { @@ -35,8 +36,9 @@ esp_err_t SPI_Flash::erase_sector(size_t sector) } esp_err_t SPI_Flash::erase_range(size_t start_address, size_t size) { - size = (size + SPI_FLASH_SEC_SIZE - 1) / SPI_FLASH_SEC_SIZE; - size = size * SPI_FLASH_SEC_SIZE; + const uint32_t sec_size = esp_partition_get_main_flash_sector_size(); + size = (size + sec_size - 1) / sec_size; + size = size * sec_size; esp_err_t result = esp_flash_erase_region(NULL, start_address, size); if (result == ESP_OK) { ESP_LOGV(TAG, "erase_range - start_address=0x%08" PRIx32 ", size=0x%08" PRIx32 ", result=0x%08x", (uint32_t) start_address, (uint32_t) size, result); @@ -70,7 +72,7 @@ esp_err_t SPI_Flash::read(size_t src_addr, void *dest, size_t size) size_t SPI_Flash::get_sector_size() { - return SPI_FLASH_SEC_SIZE; + return esp_partition_get_main_flash_sector_size();; } SPI_Flash::~SPI_Flash() diff --git a/components/wear_levelling/private_include/Partition.h b/components/wear_levelling/private_include/Partition.h index 695bfd6fa4..48cf2f5cc4 100644 --- a/components/wear_levelling/private_include/Partition.h +++ b/components/wear_levelling/private_include/Partition.h @@ -11,7 +11,6 @@ #include "Flash_Access.h" #include "esp_partition.h" -#include "spi_flash_mmap.h" // for SPI_FLASH_SEC_SIZE /** * @brief This class is used to access partition. Class implements Flash_Access interface diff --git a/components/wear_levelling/test_apps/main/CMakeLists.txt b/components/wear_levelling/test_apps/main/CMakeLists.txt index 9273ee133d..e82672a921 100644 --- a/components/wear_levelling/test_apps/main/CMakeLists.txt +++ b/components/wear_levelling/test_apps/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register(SRCS test_wl.c PRIV_INCLUDE_DIRS . - PRIV_REQUIRES wear_levelling unity spi_flash + PRIV_REQUIRES wear_levelling unity EMBED_FILES test_partition_v1.bin ) diff --git a/components/wear_levelling/test_apps/main/test_wl.c b/components/wear_levelling/test_apps/main/test_wl.c index 1d150c724b..fa402f01c0 100644 --- a/components/wear_levelling/test_apps/main/test_wl.c +++ b/components/wear_levelling/test_apps/main/test_wl.c @@ -1,5 +1,5 @@ /* - * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD + * SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 */ @@ -15,7 +15,6 @@ #include "sdkconfig.h" #include "esp_cpu.h" #include "esp_system.h" -#include "spi_flash_mmap.h" TEST_GROUP(wear_levelling); @@ -65,7 +64,7 @@ TEST(wear_levelling, wl_mount_checks_partition_params) esp_partition_erase_range(test_partition, 0, test_partition->size); // test small partition: result should be error for (int i = 0; i < 5; i++) { - fake_partition.size = SPI_FLASH_SEC_SIZE * (i); + fake_partition.size = test_partition->erase_size * (i); size_before = esp_get_free_heap_size(); TEST_ESP_ERR(ESP_ERR_INVALID_ARG, wl_mount(&fake_partition, &handle)); // test that we didn't leak any memory @@ -74,7 +73,7 @@ TEST(wear_levelling, wl_mount_checks_partition_params) } // test minimum size partition: result should be OK - fake_partition.size = SPI_FLASH_SEC_SIZE * 5; + fake_partition.size = test_partition->erase_size * 5; size_before = esp_get_free_heap_size(); TEST_ESP_OK(wl_mount(&fake_partition, &handle)); wl_unmount(handle); @@ -217,7 +216,7 @@ TEST(wear_levelling, write_doesnt_touch_other_sectors) esp_partition_t fake_partition; memcpy(&fake_partition, partition, sizeof(fake_partition)); - fake_partition.size = SPI_FLASH_SEC_SIZE * (4 + TEST_SECTORS_COUNT); + fake_partition.size = partition->erase_size * (4 + TEST_SECTORS_COUNT); wl_handle_t handle; TEST_ESP_OK(wl_mount(&fake_partition, &handle)); diff --git a/components/wear_levelling/wear_levelling.cpp b/components/wear_levelling/wear_levelling.cpp index d44e1b484c..c3500d4875 100644 --- a/components/wear_levelling/wear_levelling.cpp +++ b/components/wear_levelling/wear_levelling.cpp @@ -81,8 +81,8 @@ esp_err_t wl_mount(const esp_partition_t *partition, wl_handle_t *out_handle) cfg.wl_partition_start_addr = WL_DEFAULT_START_ADDR; cfg.wl_partition_size = partition->size; - cfg.wl_page_size = SPI_FLASH_SEC_SIZE; - cfg.flash_sector_size = SPI_FLASH_SEC_SIZE; //default size is 4096 + cfg.wl_page_size = partition->erase_size; + cfg.flash_sector_size = partition->erase_size; //default size is 4096 cfg.wl_update_rate = WL_DEFAULT_UPDATERATE; cfg.wl_pos_update_record_size = WL_DEFAULT_WRITE_SIZE; //16 bytes per pos update will be stored cfg.version = WL_CURRENT_VERSION; diff --git a/examples/storage/partition_api/partition_ops/main/CMakeLists.txt b/examples/storage/partition_api/partition_ops/main/CMakeLists.txt index 29f3e8d2d5..1e6ff73720 100644 --- a/examples/storage/partition_api/partition_ops/main/CMakeLists.txt +++ b/examples/storage/partition_api/partition_ops/main/CMakeLists.txt @@ -1,4 +1,3 @@ idf_component_register(SRCS "main.c" INCLUDE_DIRS "." - REQUIRES esp_partition - PRIV_REQUIRES spi_flash) + REQUIRES esp_partition) diff --git a/examples/storage/partition_api/partition_ops/main/main.c b/examples/storage/partition_api/partition_ops/main/main.c index c705a415b9..6521dbf1bd 100644 --- a/examples/storage/partition_api/partition_ops/main/main.c +++ b/examples/storage/partition_api/partition_ops/main/main.c @@ -8,7 +8,6 @@ #include #include #include "esp_partition.h" -#include "spi_flash_mmap.h" #include "esp_log.h" static const char *TAG = "example"; @@ -46,7 +45,7 @@ void app_main(void) // Erase the area where the data was written. Erase size should be a multiple of SPI_FLASH_SEC_SIZE // and also be SPI_FLASH_SEC_SIZE aligned - ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, SPI_FLASH_SEC_SIZE)); + ESP_ERROR_CHECK(esp_partition_erase_range(partition, 0, partition->erase_size)); // Read back the data (should all now be 0xFF's) memset(store_data, 0xFF, sizeof(read_data));