From e2a26ac6e57da67ba2b7dfe986efbd8824090e01 Mon Sep 17 00:00:00 2001 From: Martin Vychodil Date: Tue, 3 Jun 2025 11:36:42 +0200 Subject: [PATCH] fix(storage/fatfs): Fix generating of FatFS image with WL SAFE mode (wl_fatfsgen.py) FatFS image with Wear-Levelling in SAFE MODE was missing 2 extra sectors necessary for the WL SAFE operations. --- components/fatfs/test_apps/flash_ro/README.md | 4 +-- components/fatfs/test_apps/flash_wl/README.md | 4 +-- components/fatfs/test_apps/sdcard/README.md | 4 +-- components/fatfs/wl_fatfsgen.py | 25 +++++++++++-------- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/components/fatfs/test_apps/flash_ro/README.md b/components/fatfs/test_apps/flash_ro/README.md index b75df091bcf..4d341fbc4d9 100644 --- a/components/fatfs/test_apps/flash_ro/README.md +++ b/components/fatfs/test_apps/flash_ro/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | This test app runs a few FATFS test cases in a read-only FAT partition. diff --git a/components/fatfs/test_apps/flash_wl/README.md b/components/fatfs/test_apps/flash_wl/README.md index 22383db423f..9112db39f3a 100644 --- a/components/fatfs/test_apps/flash_wl/README.md +++ b/components/fatfs/test_apps/flash_wl/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | This test app runs a few FATFS test cases in a wear levelling FAT partition. diff --git a/components/fatfs/test_apps/sdcard/README.md b/components/fatfs/test_apps/sdcard/README.md index f4c21cc47e2..7fb7b83de56 100644 --- a/components/fatfs/test_apps/sdcard/README.md +++ b/components/fatfs/test_apps/sdcard/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-C61 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | --------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | This test app runs a few FATFS test cases in a FAT-formatted SD card. diff --git a/components/fatfs/wl_fatfsgen.py b/components/fatfs/wl_fatfsgen.py index 13ef51689b9..4ae5b44c2d3 100755 --- a/components/fatfs/wl_fatfsgen.py +++ b/components/fatfs/wl_fatfsgen.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD +# SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Apache-2.0 from typing import Optional @@ -92,7 +92,7 @@ class WLFATFS: reserved_sectors_cnt: int = FATDefaults.RESERVED_SECTORS_COUNT, fat_tables_cnt: int = FATDefaults.FAT_TABLES_COUNT, sectors_per_cluster: int = FATDefaults.SECTORS_PER_CLUSTER, - explicit_fat_type: int = None, + explicit_fat_type: Optional[int] = None, hidden_sectors: int = FATDefaults.HIDDEN_SECTORS, long_names_enabled: bool = False, num_heads: int = FATDefaults.NUM_HEADS, @@ -103,7 +103,7 @@ class WLFATFS: use_default_datetime: bool = True, version: int = FATDefaults.VERSION, temp_buff_size: int = FATDefaults.TEMP_BUFFER_SIZE, - device_id: int = None, + device_id: Optional[int] = None, root_entry_count: int = FATDefaults.ROOT_ENTRIES_COUNT, media_type: int = FATDefaults.MEDIA_TYPE, wl_mode: Optional[str] = None) -> None: @@ -119,14 +119,15 @@ class WLFATFS: # determine the number of required sectors (roundup to sector size) self.wl_state_sectors = (self.wl_state_size + FATDefaults.WL_SECTOR_SIZE - 1) // FATDefaults.WL_SECTOR_SIZE - self.boot_sector_start = FATDefaults.WL_SECTOR_SIZE # shift by one "dummy" sector - self.fat_table_start = self.boot_sector_start + reserved_sectors_cnt * FATDefaults.WL_SECTOR_SIZE - wl_sectors = (WLFATFS.WL_DUMMY_SECTORS_COUNT + WLFATFS.WL_CFG_SECTORS_COUNT + self.wl_state_sectors * WLFATFS.WL_STATE_COPY_COUNT) + if self.wl_mode is not None and self.wl_mode == 'safe': wl_sectors += WLFATFS.WL_SAFE_MODE_DUMP_SECTORS + self.boot_sector_start = FATDefaults.WL_SECTOR_SIZE # shift by one "dummy" sector + self.fat_table_start = self.boot_sector_start + reserved_sectors_cnt * FATDefaults.WL_SECTOR_SIZE + self.plain_fat_sectors = self.total_sectors - wl_sectors self.plain_fatfs = FATFS( explicit_fat_type=explicit_fat_type, @@ -200,10 +201,14 @@ class WLFATFS: wl_state_crc = Int32ul.build(crc) wl_state = wl_state_data + wl_state_crc wl_state_sector_padding: bytes = (FATDefaults.WL_SECTOR_SIZE - WLFATFS.WL_STATE_HEADER_SIZE) * FULL_BYTE - wl_state_sector: bytes = ( - wl_state + wl_state_sector_padding + (self.wl_state_sectors - 1) * FATDefaults.WL_SECTOR_SIZE * FULL_BYTE - ) - self.fatfs_binary_image += (WLFATFS.WL_STATE_COPY_COUNT * wl_state_sector) + wl_state_sectors: bytes = (wl_state + wl_state_sector_padding + (self.wl_state_sectors - 1) * FATDefaults.WL_SECTOR_SIZE * FULL_BYTE) + + # add 2 extra state-preservation sectors in 'Safe' mode + if self.wl_mode is not None and self.wl_mode == 'safe': + wl_safe_dummy_sec: bytes = (WLFATFS.WL_SAFE_MODE_DUMP_SECTORS * FATDefaults.WL_SECTOR_SIZE * FULL_BYTE) + self.fatfs_binary_image += wl_safe_dummy_sec + + self.fatfs_binary_image += (WLFATFS.WL_STATE_COPY_COUNT * wl_state_sectors) def wl_write_filesystem(self, output_path: str) -> None: if not self._initialized: