From 131b28314e783cab0049de9a6bd6e64ade588e1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ga=C5=88o?= Date: Tue, 13 Sep 2022 20:09:40 +0200 Subject: [PATCH] fatfsgen.py: fixed missing NULL terminator --- components/fatfs/fatfs_utils/fs_object.py | 2 +- components/fatfs/fatfs_utils/long_filename_utils.py | 7 ++++++- components/fatfs/fatfs_utils/utils.py | 7 ++----- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/components/fatfs/fatfs_utils/fs_object.py b/components/fatfs/fatfs_utils/fs_object.py index 9d68eb27b5..61a0f3a57f 100644 --- a/components/fatfs/fatfs_utils/fs_object.py +++ b/components/fatfs/fatfs_utils/fs_object.py @@ -210,7 +210,7 @@ class Directory: entries_count: int = get_required_lfn_entries_count(lfn_full_name) # entries in long file name entries chain starts with the last entry - split_names_reversed = reversed(list(enumerate(split_name_to_lfn_entries(lfn_full_name, entries_count)))) + split_names_reversed = list(reversed(list(enumerate(split_name_to_lfn_entries(lfn_full_name, entries_count))))) for i, name_split_to_entry in split_names_reversed: order: int = i + 1 blocks_: List[bytes] = split_name_to_lfn_entry_blocks(name_split_to_entry) diff --git a/components/fatfs/fatfs_utils/long_filename_utils.py b/components/fatfs/fatfs_utils/long_filename_utils.py index ccd63b5317..f28816d74c 100644 --- a/components/fatfs/fatfs_utils/long_filename_utils.py +++ b/components/fatfs/fatfs_utils/long_filename_utils.py @@ -85,4 +85,9 @@ def build_lfn_full_name(name: str, extension: str) -> str: The extension is optional, and the long filename entry explicitly specifies it, on the opposite as for short file names. """ - return f'{name}.{extension}' if len(extension) > 0 else name + lfn_record: str = f'{name}.{extension}' if len(extension) > 0 else name + # the name must be terminated with NULL terminator + # if it doesn't fit into the set of long name directory entries + if len(lfn_record) % Entry.CHARS_PER_ENTRY != 0: + return lfn_record + chr(0) + return lfn_record diff --git a/components/fatfs/fatfs_utils/utils.py b/components/fatfs/fatfs_utils/utils.py index 0dec3801cd..72e3e0dee5 100644 --- a/components/fatfs/fatfs_utils/utils.py +++ b/components/fatfs/fatfs_utils/utils.py @@ -119,14 +119,11 @@ def lfn_checksum(short_entry_name: str) -> int: def convert_to_utf16_and_pad(content: str, expected_size: int, - pad: bytes = FULL_BYTE, - terminator: bytes = b'\x00\x00') -> bytes: + pad: bytes = FULL_BYTE) -> bytes: # we need to get rid of the Byte order mark 0xfeff or 0xfffe, fatfs does not use it bom_utf16: bytes = b'\xfe\xff' encoded_content_utf16: bytes = content.encode(LONG_NAMES_ENCODING)[len(bom_utf16):] - terminated_encoded_content_utf16: bytes = (encoded_content_utf16 + terminator) if (2 * expected_size > len( - encoded_content_utf16) > 0) else encoded_content_utf16 - return terminated_encoded_content_utf16.ljust(2 * expected_size, pad) + return encoded_content_utf16.ljust(2 * expected_size, pad) def split_to_name_and_extension(full_name: str) -> Tuple[str, str]: