fatfsgen.py: enabled extension of the FAT table

Closes IDF-5688
This commit is contained in:
Martin Gaňo
2022-07-29 15:19:30 +02:00
parent 06b31d487b
commit 2d173c0777
6 changed files with 25 additions and 25 deletions

View File

@@ -107,14 +107,11 @@ class BootSector:
# uncomment for FAT32 implementation
# sectors_count_ = self._parsed_header['BPB_TotSec32']
# possible_fat_types = [FAT32]
assert self._parsed_header['BPB_FATSz16'] == 0
assert self._parsed_header['BPB_TotSec16'] == 0
raise NotImplementedError('FAT32 not implemented!')
else:
raise InconsistentFATAttributes('The number of FS sectors cannot be zero!')
# in the current code assigning self._parsed_header['BPB_TotSec32'] is not reachable
# the option to assign it is kept for possibility to implement FAT32
sectors_per_fat_cnt_ = self._parsed_header['BPB_FATSz16'] or self._parsed_header['BPB_TotSec32']
if self._parsed_header['BPB_BytsPerSec'] not in ALLOWED_SECTOR_SIZES:
raise InconsistentFATAttributes(f'The number of bytes '
f"per sector is {self._parsed_header['BPB_BytsPerSec']}! "
@@ -134,7 +131,6 @@ class BootSector:
root_dir_sectors_cnt=root_dir_sectors_cnt_,
sectors_count=sectors_count_,
media_type=self._parsed_header['BPB_Media'],
sectors_per_fat_cnt=sectors_per_fat_cnt_,
sec_per_track=self._parsed_header['BPB_SecPerTrk'],
num_heads=self._parsed_header['BPB_NumHeads'],
hidden_sectors=self._parsed_header['BPB_HiddSec'],

View File

@@ -6,7 +6,7 @@ from typing import Optional
from .exceptions import InconsistentFATAttributes
from .utils import (ALLOWED_SECTOR_SIZES, FAT12, FAT12_MAX_CLUSTERS, FAT16, FAT16_MAX_CLUSTERS, FATDefaults,
get_fatfs_type, get_non_data_sectors_cnt, number_of_clusters)
get_fat_sectors_count, get_fatfs_type, get_non_data_sectors_cnt, number_of_clusters)
class FATFSState:
@@ -20,7 +20,6 @@ class FATFSState:
root_dir_sectors_cnt: int,
size: int,
media_type: int,
sectors_per_fat: int,
sectors_per_cluster: int,
volume_label: str,
oem_name: str,
@@ -40,18 +39,18 @@ class FATFSState:
root_dir_sectors_cnt=root_dir_sectors_cnt,
sectors_count=size // sector_size,
media_type=media_type,
sectors_per_fat_cnt=sectors_per_fat,
sec_per_track=sec_per_track,
num_heads=num_heads,
hidden_sectors=hidden_sectors,
volume_label=volume_label,
file_sys_type=file_sys_type,
volume_uuid=-1)
self._explicit_fat_type: Optional[int] = explicit_fat_type
self.long_names_enabled: bool = long_names_enabled
self.use_default_datetime: bool = use_default_datetime
if self.boot_sector_state.clusters in (FAT12_MAX_CLUSTERS, FAT16_MAX_CLUSTERS):
if (size // sector_size) * sectors_per_cluster in (FAT12_MAX_CLUSTERS, FAT16_MAX_CLUSTERS):
print('WARNING: It is not recommended to create FATFS with bounding '
f'count of clusters: {FAT12_MAX_CLUSTERS} or {FAT16_MAX_CLUSTERS}')
self.check_fat_type()
@@ -86,7 +85,6 @@ class BootSectorState:
root_dir_sectors_cnt: int,
sectors_count: int,
media_type: int,
sectors_per_fat_cnt: int,
sec_per_track: int,
num_heads: int,
hidden_sectors: int,
@@ -102,7 +100,7 @@ class BootSectorState:
self.root_dir_sectors_cnt: int = root_dir_sectors_cnt
self.sectors_count: int = sectors_count
self.media_type: int = media_type
self.sectors_per_fat_cnt: int = sectors_per_fat_cnt
self.sectors_per_fat_cnt = get_fat_sectors_count(self.size // self.sector_size, self.sector_size)
self.sec_per_track: int = sec_per_track
self.num_heads: int = num_heads
self.hidden_sectors: int = hidden_sectors
@@ -161,4 +159,5 @@ class BootSectorState:
@property
def root_directory_start(self) -> int:
return (self.reserved_sectors_cnt + self.sectors_per_fat_cnt) * self.sector_size
root_dir_start: int = (self.reserved_sectors_cnt + self.sectors_per_fat_cnt) * self.sector_size
return root_dir_start

View File

@@ -70,6 +70,17 @@ def get_fatfs_type(clusters_count: int) -> int:
return FAT32
def get_fat_sectors_count(clusters_count: int, sector_size: int) -> int:
fatfs_type_ = get_fatfs_type(clusters_count)
if fatfs_type_ == FAT32:
raise NotImplementedError('FAT32 is not supported!')
# number of byte halves
cluster_s: int = fatfs_type_ // 4
fat_size_bytes: int = (
clusters_count * 2 + cluster_s) if fatfs_type_ == FAT16 else (clusters_count * 3 + 1) // 2 + cluster_s
return (fat_size_bytes + sector_size - 1) // sector_size
def required_clusters_count(cluster_size: int, content: bytes) -> int:
# compute number of required clusters for file text
return (len(content) + cluster_size - 1) // cluster_size
@@ -253,7 +264,6 @@ class FATDefaults:
FAT_TABLES_COUNT: int = 1
SECTORS_PER_CLUSTER: int = 1
SECTOR_SIZE: int = 0x1000
SECTORS_PER_FAT: int = 1
HIDDEN_SECTORS: int = 0
ENTRY_SIZE: int = 32
NUM_HEADS: int = 0xff