fatfsparse.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #!/usr/bin/env python
  2. # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
  3. # SPDX-License-Identifier: Apache-2.0
  4. import argparse
  5. import os
  6. import construct
  7. from fatfs_utils.boot_sector import BootSector
  8. from fatfs_utils.entry import Entry
  9. from fatfs_utils.fat import FAT
  10. from fatfs_utils.fatfs_state import BootSectorState
  11. from fatfs_utils.utils import FULL_BYTE, LONG_NAMES_ENCODING, PAD_CHAR, FATDefaults, lfn_checksum, read_filesystem
  12. from wl_fatfsgen import remove_wl
  13. def build_file_name(name1: bytes, name2: bytes, name3: bytes) -> str:
  14. full_name_ = name1 + name2 + name3
  15. # need to strip empty bytes and null-terminating char ('\x00')
  16. return full_name_.rstrip(FULL_BYTE).decode(LONG_NAMES_ENCODING).rstrip('\x00')
  17. def get_obj_name(obj_: dict, directory_bytes_: bytes, entry_position_: int, lfn_checksum_: int) -> str:
  18. obj_ext_ = obj_['DIR_Name_ext'].rstrip(chr(PAD_CHAR))
  19. ext_ = f'.{obj_ext_}' if len(obj_ext_) > 0 else ''
  20. obj_name_: str = obj_['DIR_Name'].rstrip(chr(PAD_CHAR)) + ext_ # short entry name
  21. if not args.long_name_support:
  22. return obj_name_
  23. full_name = {}
  24. for pos in range(entry_position_ - 1, -1, -1): # loop from the current entry back to the start
  25. obj_address_: int = FATDefaults.ENTRY_SIZE * pos
  26. entry_bytes_: bytes = directory_bytes_[obj_address_: obj_address_ + FATDefaults.ENTRY_SIZE]
  27. struct_ = Entry.parse_entry_long(entry_bytes_, lfn_checksum_)
  28. if len(struct_.items()) > 0:
  29. full_name[struct_['order']] = build_file_name(struct_['name1'], struct_['name2'], struct_['name3'])
  30. if struct_['is_last']:
  31. break
  32. return ''.join(map(lambda x: x[1], sorted(full_name.items()))) or obj_name_
  33. def traverse_folder_tree(directory_bytes_: bytes,
  34. name: str,
  35. state_: BootSectorState,
  36. fat_: FAT,
  37. binary_array_: bytes) -> None:
  38. os.makedirs(name)
  39. assert len(directory_bytes_) % FATDefaults.ENTRY_SIZE == 0
  40. entries_count_: int = len(directory_bytes_) // FATDefaults.ENTRY_SIZE
  41. for i in range(entries_count_):
  42. obj_address_: int = FATDefaults.ENTRY_SIZE * i
  43. try:
  44. obj_: dict = Entry.ENTRY_FORMAT_SHORT_NAME.parse(
  45. directory_bytes_[obj_address_: obj_address_ + FATDefaults.ENTRY_SIZE])
  46. except (construct.core.ConstError, UnicodeDecodeError) as e:
  47. if not args.long_name_support:
  48. raise e
  49. continue
  50. if obj_['DIR_Attr'] == 0: # empty entry
  51. continue
  52. obj_name_: str = get_obj_name(obj_,
  53. directory_bytes_,
  54. entry_position_=i,
  55. lfn_checksum_=lfn_checksum(obj_['DIR_Name'] + obj_['DIR_Name_ext']))
  56. if obj_['DIR_Attr'] == Entry.ATTR_ARCHIVE:
  57. content_ = fat_.chain_content(cluster_id_=Entry.get_cluster_id(obj_)).rstrip(chr(0x00).encode())
  58. with open(os.path.join(name, obj_name_), 'wb') as new_file:
  59. new_file.write(content_)
  60. elif obj_['DIR_Attr'] == Entry.ATTR_DIRECTORY:
  61. # avoid creating symlinks to itself and parent folder
  62. if obj_name_ in ('.', '..'):
  63. continue
  64. child_directory_bytes_ = fat_.chain_content(cluster_id_=obj_['DIR_FstClusLO'])
  65. traverse_folder_tree(directory_bytes_=child_directory_bytes_,
  66. name=os.path.join(name, obj_name_),
  67. state_=state_,
  68. fat_=fat_,
  69. binary_array_=binary_array_)
  70. if __name__ == '__main__':
  71. desc = 'Tool for parsing fatfs image and extracting directory structure on host.'
  72. argument_parser: argparse.ArgumentParser = argparse.ArgumentParser(description=desc)
  73. argument_parser.add_argument('input_image',
  74. help='Path to the image that will be parsed and extracted.')
  75. argument_parser.add_argument('--long-name-support',
  76. action='store_true',
  77. help='Set flag to enable long names support.')
  78. argument_parser.add_argument('--wear-leveling',
  79. action='store_true',
  80. help='Set flag to parse an image encoded using wear levelling.')
  81. args = argument_parser.parse_args()
  82. fs = read_filesystem(args.input_image)
  83. # An algorithm for removing wear levelling:
  84. # 1. find an remove dummy sector:
  85. # a) dummy sector is at the position defined by the number of records in the state sector
  86. # b) dummy may not be placed in state nor cfg sectors
  87. # c) first (boot) sector position (boot_s_pos) is calculated using value of move count
  88. # boot_s_pos = - mc
  89. # 2. remove state sectors (trivial)
  90. # 3. remove cfg sector (trivial)
  91. # 4. valid fs is then old_fs[-mc:] + old_fs[:-mc]
  92. if args.wear_leveling:
  93. fs = remove_wl(fs)
  94. boot_sector_ = BootSector()
  95. boot_sector_.parse_boot_sector(fs)
  96. fat = FAT(boot_sector_.boot_sector_state, init_=False)
  97. boot_dir_start_ = boot_sector_.boot_sector_state.root_directory_start
  98. boot_dir_sectors = boot_sector_.boot_sector_state.root_dir_sectors_cnt
  99. full_ = fs[boot_dir_start_: boot_dir_start_ + boot_dir_sectors * boot_sector_.boot_sector_state.sector_size]
  100. traverse_folder_tree(full_,
  101. boot_sector_.boot_sector_state.volume_label.rstrip(chr(PAD_CHAR)),
  102. boot_sector_.boot_sector_state, fat, fs)