Skip to content

Commit

Permalink
Merge branch 'fix/wl_fatfsgen_safe_mode_v5.2' into 'release/v5.2'
Browse files Browse the repository at this point in the history
fix(storage/fatfs): make wl_fatfsgen.py safe mode aware (v5.2)

See merge request espressif/esp-idf!29728
  • Loading branch information
pacucha42 committed Mar 20, 2024
2 parents 7e36e97 + c51b2fb commit 4005c24
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
17 changes: 14 additions & 3 deletions components/fatfs/fatfs_utils/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0

import argparse
import binascii
import os
import re
import uuid
from datetime import datetime
from typing import List, Optional, Tuple
from typing import List
from typing import Optional
from typing import Tuple

from construct import BitsInteger, BitStruct, Int16ul
from construct import BitsInteger
from construct import BitStruct
from construct import Int16ul

# the regex pattern defines symbols that are allowed by long file names but not by short file names
INVALID_SFN_CHARS_PATTERN = re.compile(r'[.+,;=\[\]]')
Expand Down Expand Up @@ -206,6 +209,11 @@ def get_args_for_partition_generator(desc: str, wl: bool) -> argparse.Namespace:
Type of fat. Select 12 for fat12, 16 for fat16. Don't set, or set to 0 for automatic
calculation using cluster size and partition size.
""")
parser.add_argument('--wl_mode',
default=None,
type=str,
choices=['safe', 'perf'],
help='Wear levelling mode to use. Safe or performance. Only for sector size of 512')

args = parser.parse_args()
if args.fat_type == 0:
Expand All @@ -215,6 +223,9 @@ def get_args_for_partition_generator(desc: str, wl: bool) -> argparse.Namespace:
args.partition_size = int(str(args.partition_size), 0)
if not os.path.isdir(args.input_directory):
raise NotADirectoryError(f'The target directory `{args.input_directory}` does not exist!')
if args.wl_mode is not None:
if args.sector_size != 512:
raise ValueError('Wear levelling mode can be set only for sector size 512')
return args


Expand Down
24 changes: 19 additions & 5 deletions components/fatfs/wl_fatfsgen.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#!/usr/bin/env python
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

from construct import Const, Int32ul, Struct
from construct import Const
from construct import Int32ul
from construct import Struct
from fatfs_utils.exceptions import WLNotInitialized
from fatfs_utils.utils import (FULL_BYTE, UINT32_MAX, FATDefaults, crc32, generate_4bytes_random,
get_args_for_partition_generator)
from fatfs_utils.utils import crc32
from fatfs_utils.utils import FATDefaults
from fatfs_utils.utils import FULL_BYTE
from fatfs_utils.utils import generate_4bytes_random
from fatfs_utils.utils import get_args_for_partition_generator
from fatfs_utils.utils import UINT32_MAX
from fatfsgen import FATFS


Expand Down Expand Up @@ -53,6 +60,7 @@ class WLFATFS:
WL_STATE_HEADER_SIZE = 64
WL_STATE_COPY_COUNT = 2 # always 2 copies for power failure safety
WL_SECTOR_SIZE = 0x1000
WL_SAFE_MODE_DUMP_SECTORS = 2

WL_STATE_T_DATA = Struct(
'pos' / Int32ul,
Expand Down Expand Up @@ -97,14 +105,16 @@ def __init__(self,
temp_buff_size: int = FATDefaults.TEMP_BUFFER_SIZE,
device_id: int = None,
root_entry_count: int = FATDefaults.ROOT_ENTRIES_COUNT,
media_type: int = FATDefaults.MEDIA_TYPE) -> None:
media_type: int = FATDefaults.MEDIA_TYPE,
wl_mode: Optional[str] = None) -> None:
self._initialized = False
self._version = version
self._temp_buff_size = temp_buff_size
self._device_id = device_id
self.partition_size = size
self.total_sectors = self.partition_size // FATDefaults.WL_SECTOR_SIZE
self.wl_state_size = WLFATFS.WL_STATE_HEADER_SIZE + WLFATFS.WL_STATE_RECORD_SIZE * self.total_sectors
self.wl_mode = wl_mode

# 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
Expand All @@ -114,6 +124,9 @@ def __init__(self,

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.plain_fat_sectors = self.total_sectors - wl_sectors
self.plain_fatfs = FATFS(
explicit_fat_type=explicit_fat_type,
Expand Down Expand Up @@ -208,7 +221,8 @@ def wl_write_filesystem(self, output_path: str) -> None:
root_entry_count=args.root_entry_count,
explicit_fat_type=args.fat_type,
long_names_enabled=args.long_name_support,
use_default_datetime=args.use_default_datetime)
use_default_datetime=args.use_default_datetime,
wl_mode=args.wl_mode)

wl_fatfs.plain_fatfs.generate(args.input_directory)
wl_fatfs.init_wl()
Expand Down

0 comments on commit 4005c24

Please sign in to comment.