2021-09-21 18:32:54 -04:00
|
|
|
#!/usr/bin/env python
|
2024-03-13 07:36:28 -04:00
|
|
|
# SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
|
2021-09-21 18:32:54 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2024-03-13 07:36:28 -04:00
|
|
|
from typing import List
|
|
|
|
from typing import Optional
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2024-03-13 07:36:28 -04:00
|
|
|
from construct import Const
|
|
|
|
from construct import Int32ul
|
|
|
|
from construct import Struct
|
2022-04-04 09:33:00 -04:00
|
|
|
from fatfs_utils.exceptions import WLNotInitialized
|
2024-03-13 07:36:28 -04:00
|
|
|
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
|
2021-09-21 18:32:54 -04:00
|
|
|
from fatfsgen import FATFS
|
|
|
|
|
|
|
|
|
2022-06-29 09:37:06 -04:00
|
|
|
def remove_wl(binary_image: bytes) -> bytes:
|
|
|
|
partition_size: int = len(binary_image)
|
|
|
|
total_sectors: int = partition_size // FATDefaults.WL_SECTOR_SIZE
|
|
|
|
wl_state_size: int = WLFATFS.WL_STATE_HEADER_SIZE + WLFATFS.WL_STATE_RECORD_SIZE * total_sectors
|
|
|
|
wl_state_sectors_cnt: int = (wl_state_size + FATDefaults.WL_SECTOR_SIZE - 1) // FATDefaults.WL_SECTOR_SIZE
|
|
|
|
wl_state_total_size: int = wl_state_sectors_cnt * FATDefaults.WL_SECTOR_SIZE
|
|
|
|
wl_sectors_size: int = (wl_state_sectors_cnt
|
|
|
|
* FATDefaults.WL_SECTOR_SIZE
|
|
|
|
* WLFATFS.WL_STATE_COPY_COUNT
|
|
|
|
+ FATDefaults.WL_SECTOR_SIZE)
|
|
|
|
|
|
|
|
correct_wl_configuration = binary_image[-wl_sectors_size:]
|
|
|
|
|
|
|
|
data_ = WLFATFS.WL_STATE_T_DATA.parse(correct_wl_configuration[:WLFATFS.WL_STATE_HEADER_SIZE])
|
|
|
|
|
|
|
|
total_records = 0
|
|
|
|
# iterating over records field of the first copy of the state sector
|
|
|
|
for i in range(WLFATFS.WL_STATE_HEADER_SIZE, wl_state_total_size, WLFATFS.WL_STATE_RECORD_SIZE):
|
|
|
|
if correct_wl_configuration[i:i + WLFATFS.WL_STATE_RECORD_SIZE] != WLFATFS.WL_STATE_RECORD_SIZE * b'\xff':
|
|
|
|
total_records += 1
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
before_dummy = binary_image[:total_records * FATDefaults.WL_SECTOR_SIZE]
|
|
|
|
after_dummy = binary_image[total_records * FATDefaults.WL_SECTOR_SIZE + FATDefaults.WL_SECTOR_SIZE:]
|
|
|
|
new_image: bytes = before_dummy + after_dummy
|
|
|
|
|
|
|
|
# remove wl sectors
|
|
|
|
new_image = new_image[:len(new_image) - (FATDefaults.WL_SECTOR_SIZE + 2 * wl_state_total_size)]
|
|
|
|
|
|
|
|
# reorder to preserve original order
|
|
|
|
new_image = (new_image[-data_['move_count'] * FATDefaults.WL_SECTOR_SIZE:]
|
|
|
|
+ new_image[:-data_['move_count'] * FATDefaults.WL_SECTOR_SIZE])
|
|
|
|
return new_image
|
|
|
|
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
class WLFATFS:
|
|
|
|
# pylint: disable=too-many-instance-attributes
|
2022-04-04 09:33:00 -04:00
|
|
|
WL_CFG_SECTORS_COUNT = 1
|
|
|
|
WL_DUMMY_SECTORS_COUNT = 1
|
2021-09-21 18:32:54 -04:00
|
|
|
WL_CONFIG_HEADER_SIZE = 48
|
|
|
|
WL_STATE_RECORD_SIZE = 16
|
|
|
|
WL_STATE_HEADER_SIZE = 64
|
2022-06-29 09:37:06 -04:00
|
|
|
WL_STATE_COPY_COUNT = 2 # always 2 copies for power failure safety
|
2021-09-21 18:32:54 -04:00
|
|
|
WL_SECTOR_SIZE = 0x1000
|
2024-03-13 07:36:28 -04:00
|
|
|
WL_SAFE_MODE_DUMP_SECTORS = 2
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
WL_STATE_T_DATA = Struct(
|
|
|
|
'pos' / Int32ul,
|
|
|
|
'max_pos' / Int32ul,
|
|
|
|
'move_count' / Int32ul,
|
|
|
|
'access_count' / Int32ul,
|
|
|
|
'max_count' / Int32ul,
|
|
|
|
'block_size' / Int32ul,
|
|
|
|
'version' / Int32ul,
|
|
|
|
'device_id' / Int32ul,
|
|
|
|
'reserved' / Const(28 * b'\x00')
|
|
|
|
)
|
|
|
|
|
|
|
|
WL_CONFIG_T_DATA = Struct(
|
|
|
|
'start_addr' / Int32ul,
|
|
|
|
'full_mem_size' / Int32ul,
|
|
|
|
'page_size' / Int32ul,
|
2022-06-29 09:37:06 -04:00
|
|
|
'sector_size' / Int32ul, # always 4096 for the types of NOR flash supported by ESP-IDF!
|
2021-09-21 18:32:54 -04:00
|
|
|
'updaterate' / Int32ul,
|
|
|
|
'wr_size' / Int32ul,
|
|
|
|
'version' / Int32ul,
|
|
|
|
'temp_buff_size' / Int32ul
|
|
|
|
)
|
|
|
|
WL_CONFIG_T_HEADER_SIZE = 48
|
|
|
|
|
|
|
|
def __init__(self,
|
2022-04-04 09:33:00 -04:00
|
|
|
size: int = FATDefaults.SIZE,
|
2022-09-26 07:23:59 -04:00
|
|
|
sector_size: int = FATDefaults.SECTOR_SIZE,
|
2022-04-04 09:33:00 -04:00
|
|
|
reserved_sectors_cnt: int = FATDefaults.RESERVED_SECTORS_COUNT,
|
|
|
|
fat_tables_cnt: int = FATDefaults.FAT_TABLES_COUNT,
|
|
|
|
sectors_per_cluster: int = FATDefaults.SECTORS_PER_CLUSTER,
|
2021-12-06 12:17:20 -05:00
|
|
|
explicit_fat_type: int = None,
|
2022-04-04 09:33:00 -04:00
|
|
|
hidden_sectors: int = FATDefaults.HIDDEN_SECTORS,
|
2021-09-21 18:32:54 -04:00
|
|
|
long_names_enabled: bool = False,
|
2022-04-04 09:33:00 -04:00
|
|
|
num_heads: int = FATDefaults.NUM_HEADS,
|
|
|
|
oem_name: str = FATDefaults.OEM_NAME,
|
|
|
|
sec_per_track: int = FATDefaults.SEC_PER_TRACK,
|
|
|
|
volume_label: str = FATDefaults.VOLUME_LABEL,
|
|
|
|
file_sys_type: str = FATDefaults.FILE_SYS_TYPE,
|
2022-03-17 13:02:16 -04:00
|
|
|
use_default_datetime: bool = True,
|
2022-04-04 09:33:00 -04:00
|
|
|
version: int = FATDefaults.VERSION,
|
|
|
|
temp_buff_size: int = FATDefaults.TEMP_BUFFER_SIZE,
|
2021-09-21 18:32:54 -04:00
|
|
|
device_id: int = None,
|
2022-04-04 09:33:00 -04:00
|
|
|
root_entry_count: int = FATDefaults.ROOT_ENTRIES_COUNT,
|
2024-03-13 07:36:28 -04:00
|
|
|
media_type: int = FATDefaults.MEDIA_TYPE,
|
|
|
|
wl_mode: Optional[str] = None) -> None:
|
2021-09-21 18:32:54 -04:00
|
|
|
self._initialized = False
|
|
|
|
self._version = version
|
|
|
|
self._temp_buff_size = temp_buff_size
|
|
|
|
self._device_id = device_id
|
|
|
|
self.partition_size = size
|
2022-06-29 09:37:06 -04:00
|
|
|
self.total_sectors = self.partition_size // FATDefaults.WL_SECTOR_SIZE
|
2021-09-21 18:32:54 -04:00
|
|
|
self.wl_state_size = WLFATFS.WL_STATE_HEADER_SIZE + WLFATFS.WL_STATE_RECORD_SIZE * self.total_sectors
|
2024-03-13 07:36:28 -04:00
|
|
|
self.wl_mode = wl_mode
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
# determine the number of required sectors (roundup to sector size)
|
2022-06-29 09:37:06 -04:00
|
|
|
self.wl_state_sectors = (self.wl_state_size + FATDefaults.WL_SECTOR_SIZE - 1) // FATDefaults.WL_SECTOR_SIZE
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2022-06-29 09:37:06 -04:00
|
|
|
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
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2022-04-04 09:33:00 -04:00
|
|
|
wl_sectors = (WLFATFS.WL_DUMMY_SECTORS_COUNT + WLFATFS.WL_CFG_SECTORS_COUNT +
|
|
|
|
self.wl_state_sectors * WLFATFS.WL_STATE_COPY_COUNT)
|
2024-03-13 07:36:28 -04:00
|
|
|
if self.wl_mode is not None and self.wl_mode == 'safe':
|
|
|
|
wl_sectors += WLFATFS.WL_SAFE_MODE_DUMP_SECTORS
|
|
|
|
|
2021-09-21 18:32:54 -04:00
|
|
|
self.plain_fat_sectors = self.total_sectors - wl_sectors
|
|
|
|
self.plain_fatfs = FATFS(
|
2021-12-06 12:17:20 -05:00
|
|
|
explicit_fat_type=explicit_fat_type,
|
2022-06-29 09:37:06 -04:00
|
|
|
size=self.plain_fat_sectors * FATDefaults.WL_SECTOR_SIZE,
|
2021-09-21 18:32:54 -04:00
|
|
|
reserved_sectors_cnt=reserved_sectors_cnt,
|
|
|
|
fat_tables_cnt=fat_tables_cnt,
|
|
|
|
sectors_per_cluster=sectors_per_cluster,
|
2022-09-26 07:23:59 -04:00
|
|
|
sector_size=sector_size,
|
2021-12-06 12:17:20 -05:00
|
|
|
root_entry_count=root_entry_count,
|
2021-09-21 18:32:54 -04:00
|
|
|
hidden_sectors=hidden_sectors,
|
|
|
|
long_names_enabled=long_names_enabled,
|
|
|
|
num_heads=num_heads,
|
2022-03-17 13:02:16 -04:00
|
|
|
use_default_datetime=use_default_datetime,
|
2021-09-21 18:32:54 -04:00
|
|
|
oem_name=oem_name,
|
|
|
|
sec_per_track=sec_per_track,
|
|
|
|
volume_label=volume_label,
|
|
|
|
file_sys_type=file_sys_type,
|
|
|
|
media_type=media_type
|
|
|
|
)
|
|
|
|
|
|
|
|
self.fatfs_binary_image = self.plain_fatfs.state.binary_image
|
|
|
|
|
|
|
|
def init_wl(self) -> None:
|
|
|
|
self.fatfs_binary_image = self.plain_fatfs.state.binary_image
|
|
|
|
self._add_dummy_sector()
|
|
|
|
# config must be added after state, do not change the order of these two calls!
|
|
|
|
self._add_state_sectors()
|
|
|
|
self._add_config_sector()
|
|
|
|
self._initialized = True
|
|
|
|
|
|
|
|
def _add_dummy_sector(self) -> None:
|
2022-06-29 09:37:06 -04:00
|
|
|
self.fatfs_binary_image = FATDefaults.WL_SECTOR_SIZE * FULL_BYTE + self.fatfs_binary_image
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
def _add_config_sector(self) -> None:
|
|
|
|
wl_config_data = WLFATFS.WL_CONFIG_T_DATA.build(
|
|
|
|
dict(
|
|
|
|
start_addr=0,
|
|
|
|
full_mem_size=self.partition_size,
|
2022-06-29 09:37:06 -04:00
|
|
|
page_size=FATDefaults.WL_SECTOR_SIZE, # equal to sector size (always 4096)
|
|
|
|
sector_size=FATDefaults.WL_SECTOR_SIZE,
|
|
|
|
updaterate=FATDefaults.UPDATE_RATE,
|
|
|
|
wr_size=FATDefaults.WR_SIZE,
|
2021-09-21 18:32:54 -04:00
|
|
|
version=self._version,
|
|
|
|
temp_buff_size=self._temp_buff_size
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-02-09 10:09:09 -05:00
|
|
|
crc = crc32(list(wl_config_data), UINT32_MAX)
|
2021-09-21 18:32:54 -04:00
|
|
|
wl_config_crc = Int32ul.build(crc)
|
|
|
|
|
|
|
|
# adding three 4 byte zeros to align the structure
|
|
|
|
wl_config = wl_config_data + wl_config_crc + Int32ul.build(0) + Int32ul.build(0) + Int32ul.build(0)
|
|
|
|
|
2022-06-29 09:37:06 -04:00
|
|
|
self.fatfs_binary_image += (
|
|
|
|
wl_config + (FATDefaults.WL_SECTOR_SIZE - WLFATFS.WL_CONFIG_HEADER_SIZE) * FULL_BYTE)
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
def _add_state_sectors(self) -> None:
|
|
|
|
wl_state_data = WLFATFS.WL_STATE_T_DATA.build(
|
|
|
|
dict(
|
|
|
|
pos=0,
|
2022-04-04 09:33:00 -04:00
|
|
|
max_pos=self.plain_fat_sectors + WLFATFS.WL_DUMMY_SECTORS_COUNT,
|
2021-09-21 18:32:54 -04:00
|
|
|
move_count=0,
|
|
|
|
access_count=0,
|
2022-06-29 09:37:06 -04:00
|
|
|
max_count=FATDefaults.UPDATE_RATE,
|
|
|
|
block_size=FATDefaults.WL_SECTOR_SIZE, # equal to page size, thus equal to wl sector size (4096)
|
2021-09-21 18:32:54 -04:00
|
|
|
version=self._version,
|
|
|
|
device_id=self._device_id or generate_4bytes_random(),
|
|
|
|
)
|
|
|
|
)
|
2022-02-09 10:09:09 -05:00
|
|
|
crc = crc32(list(wl_state_data), UINT32_MAX)
|
2021-09-21 18:32:54 -04:00
|
|
|
wl_state_crc = Int32ul.build(crc)
|
|
|
|
wl_state = wl_state_data + wl_state_crc
|
2022-06-29 09:37:06 -04:00
|
|
|
wl_state_sector_padding: bytes = (FATDefaults.WL_SECTOR_SIZE - WLFATFS.WL_STATE_HEADER_SIZE) * FULL_BYTE
|
2022-04-04 09:33:00 -04:00
|
|
|
wl_state_sector: bytes = (
|
2022-06-29 09:37:06 -04:00
|
|
|
wl_state + wl_state_sector_padding + (self.wl_state_sectors - 1) * FATDefaults.WL_SECTOR_SIZE * FULL_BYTE
|
2022-04-04 09:33:00 -04:00
|
|
|
)
|
|
|
|
self.fatfs_binary_image += (WLFATFS.WL_STATE_COPY_COUNT * wl_state_sector)
|
2021-09-21 18:32:54 -04:00
|
|
|
|
|
|
|
def wl_write_filesystem(self, output_path: str) -> None:
|
|
|
|
if not self._initialized:
|
|
|
|
raise WLNotInitialized('FATFS is not initialized with WL. First call method WLFATFS.init_wl!')
|
|
|
|
with open(output_path, 'wb') as output:
|
|
|
|
output.write(bytearray(self.fatfs_binary_image))
|
|
|
|
|
|
|
|
def wl_generate(self, input_directory: str) -> None:
|
|
|
|
"""
|
|
|
|
Normalize path to folder and recursively encode folder to binary image
|
|
|
|
"""
|
|
|
|
self.plain_fatfs.generate(input_directory=input_directory)
|
|
|
|
|
|
|
|
def wl_create_file(self, name: str, extension: str = '', path_from_root: Optional[List[str]] = None) -> None:
|
|
|
|
self.plain_fatfs.create_file(name, extension, path_from_root)
|
|
|
|
|
|
|
|
def wl_create_directory(self, name: str, path_from_root: Optional[List[str]] = None) -> None:
|
|
|
|
self.plain_fatfs.create_directory(name, path_from_root)
|
|
|
|
|
2022-01-02 19:02:16 -05:00
|
|
|
def wl_write_content(self, path_from_root: List[str], content: bytes) -> None:
|
2021-09-21 18:32:54 -04:00
|
|
|
self.plain_fatfs.write_content(path_from_root, content)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
desc = 'Create a FAT filesystem with support for wear levelling and populate it with directory content'
|
|
|
|
args = get_args_for_partition_generator(desc)
|
|
|
|
|
2022-06-29 09:37:06 -04:00
|
|
|
wl_fatfs = WLFATFS(sectors_per_cluster=args.sectors_per_cluster,
|
2021-12-06 12:17:20 -05:00
|
|
|
size=args.partition_size,
|
2022-09-26 07:23:59 -04:00
|
|
|
sector_size=args.sector_size,
|
2021-12-06 12:17:20 -05:00
|
|
|
root_entry_count=args.root_entry_count,
|
2022-02-09 10:09:09 -05:00
|
|
|
explicit_fat_type=args.fat_type,
|
2022-03-17 13:02:16 -04:00
|
|
|
long_names_enabled=args.long_name_support,
|
2024-03-13 07:36:28 -04:00
|
|
|
use_default_datetime=args.use_default_datetime,
|
|
|
|
wl_mode=args.wl_mode)
|
2021-09-21 18:32:54 -04:00
|
|
|
|
2021-12-06 12:17:20 -05:00
|
|
|
wl_fatfs.wl_generate(args.input_directory)
|
2021-09-21 18:32:54 -04:00
|
|
|
wl_fatfs.init_wl()
|
|
|
|
wl_fatfs.wl_write_filesystem(args.output_file)
|