cluster.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. # SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  2. # SPDX-License-Identifier: Apache-2.0
  3. from typing import Dict, Optional
  4. from construct import Int16ul
  5. from .fatfs_state import BootSectorState
  6. from .utils import (EMPTY_BYTE, FAT12, FAT16, build_byte, merge_by_half_byte_12_bit_little_endian,
  7. split_by_half_byte_12_bit_little_endian)
  8. def get_dir_size(is_root: bool, boot_sector: BootSectorState) -> int:
  9. dir_size_: int = boot_sector.root_dir_sectors_cnt * boot_sector.sector_size if is_root else boot_sector.sector_size
  10. return dir_size_
  11. class Cluster:
  12. """
  13. class Cluster handles values in FAT table and allocates sectors in data region.
  14. """
  15. RESERVED_BLOCK_ID: int = 0
  16. ROOT_BLOCK_ID: int = 1
  17. ALLOCATED_BLOCK_FAT12: int = 0xFFF
  18. ALLOCATED_BLOCK_FAT16: int = 0xFFFF
  19. ALLOCATED_BLOCK_SWITCH = {FAT12: ALLOCATED_BLOCK_FAT12, FAT16: ALLOCATED_BLOCK_FAT16}
  20. INITIAL_BLOCK_SWITCH: Dict[int, int] = {FAT12: 0xFF8, FAT16: 0xFFF8}
  21. def __init__(self,
  22. cluster_id: int,
  23. boot_sector_state: BootSectorState,
  24. init_: bool) -> None:
  25. """
  26. Initially, if init_ is False, the cluster is virtual and is not allocated (doesn't do changes in the FAT).
  27. :param cluster_id: the cluster ID - a key value linking the file's cluster,
  28. the corresponding physical cluster (data region) and the FAT table cluster.
  29. :param boot_sector_state: auxiliary structure holding the file-system's metadata
  30. :param init_: True for allocation the cluster on instantiation, otherwise False.
  31. :returns: None
  32. """
  33. self.id: int = cluster_id
  34. self.boot_sector_state: BootSectorState = boot_sector_state
  35. self._next_cluster = None # type: Optional[Cluster]
  36. # First cluster in FAT is reserved, low 8 bits contains BPB_Media and the rest is filled with 1
  37. # e.g. the esp32 media type is 0xF8 thus the FAT[0] = 0xFF8 for FAT12, 0xFFF8 for FAT16
  38. if self.id == Cluster.RESERVED_BLOCK_ID and init_:
  39. self.set_in_fat(self.INITIAL_BLOCK_SWITCH[self.boot_sector_state.fatfs_type])
  40. return
  41. self.cluster_data_address: int = self._compute_cluster_data_address()
  42. assert self.cluster_data_address
  43. @property
  44. def next_cluster(self): # type: () -> Optional[Cluster]
  45. return self._next_cluster
  46. @next_cluster.setter
  47. def next_cluster(self, value): # type: (Optional[Cluster]) -> None
  48. self._next_cluster = value
  49. def _cluster_id_to_fat_position_in_bits(self, _id: int) -> int:
  50. """
  51. This private method calculates the position of the memory block (cluster) in the FAT table.
  52. :param _id: the cluster ID - a key value linking the file's cluster,
  53. the corresponding physical cluster (data region) and the FAT table cluster.
  54. :returns: bit offset of the cluster in FAT
  55. e.g.:
  56. 00003000: 42 65 00 2E 00 74 00 78 00 74 00 0F 00 43 FF FF
  57. For FAT12 the third cluster has value = 0x02E and ID = 2.
  58. Its bit-address is 24 (24 bits preceding, 0-indexed), because 0x2E starts at the bit-offset 24.
  59. """
  60. logical_position_: int = self.boot_sector_state.fatfs_type * _id
  61. return logical_position_
  62. @staticmethod
  63. def compute_cluster_data_address(boot_sector_state: BootSectorState, id_: int) -> int:
  64. """
  65. This method translates the id of the cluster to the address in data region.
  66. :param boot_sector_state: the class with FS shared data
  67. :param id_: id of the cluster
  68. :returns: integer denoting the address of the cluster in the data region
  69. """
  70. data_address_: int = boot_sector_state.root_directory_start
  71. if not id_ == Cluster.ROOT_BLOCK_ID:
  72. # the first data cluster id is 2 (we have to subtract reserved cluster and cluster for root)
  73. data_address_ = boot_sector_state.sector_size * (id_ - 2) + boot_sector_state.data_region_start
  74. return data_address_
  75. def _compute_cluster_data_address(self) -> int:
  76. return self.compute_cluster_data_address(self.boot_sector_state, self.id)
  77. @property
  78. def fat_cluster_address(self) -> int:
  79. """Determines how many bits precede the first bit of the cluster in FAT"""
  80. return self._cluster_id_to_fat_position_in_bits(self.id)
  81. @property
  82. def real_cluster_address(self) -> int:
  83. """
  84. The property method computes the real address of the cluster in the FAT region. Result is simply
  85. address of the cluster in fat + fat table address.
  86. """
  87. cluster_address: int = self.boot_sector_state.fat_table_start_address + self.fat_cluster_address // 8
  88. return cluster_address
  89. def get_from_fat(self) -> int:
  90. """
  91. Calculating the value in the FAT block, that denotes if the block is full, empty, or chained to other block.
  92. For FAT12 is the block stored in one and half byte. If the order of the block is even the first byte and second
  93. half of the second byte belongs to the block. First half of the second byte and the third byte belongs to
  94. the second block.
  95. e.g. b'\xff\x0f\x00' stores two blocks. First of them is evenly ordered (index 0) and is set to 0xfff,
  96. that means full block that is final in chain of blocks
  97. and second block is set to 0x000 that means empty block.
  98. three bytes - AB XC YZ - stores two blocks - CAB YZX
  99. """
  100. address_: int = self.real_cluster_address
  101. bin_img_: bytearray = self.boot_sector_state.binary_image
  102. if self.boot_sector_state.fatfs_type == FAT12:
  103. if self.fat_cluster_address % 8 == 0:
  104. # even block
  105. return bin_img_[self.real_cluster_address] | ((bin_img_[self.real_cluster_address + 1] & 0x0F) << 8)
  106. # odd block
  107. return ((bin_img_[self.real_cluster_address] & 0xF0) >> 4) | (bin_img_[self.real_cluster_address + 1] << 4)
  108. if self.boot_sector_state.fatfs_type == FAT16:
  109. return int.from_bytes(bin_img_[address_:address_ + 2], byteorder='little')
  110. raise NotImplementedError('Only valid fatfs types are FAT12 and FAT16.')
  111. @property
  112. def is_empty(self) -> bool:
  113. """
  114. The property method takes a look into the binary array and checks if the bytes ordered by little endian
  115. and relates to the current cluster are all zeros (which denotes they are empty).
  116. """
  117. return self.get_from_fat() == 0x00
  118. def set_in_fat(self, value: int) -> None:
  119. """
  120. Sets cluster in FAT to certain value.
  121. Firstly, we split the target value into 3 half bytes (max value is 0xfff).
  122. Then we could encounter two situations:
  123. 1. if the cluster index (indexed from zero) is even, we set the full byte computed by
  124. self.cluster_id_to_logical_position_in_bits and the second half of the consequent byte.
  125. Order of half bytes is 2, 1, 3.
  126. 2. if the cluster index is odd, we set the first half of the computed byte and the full consequent byte.
  127. Order of half bytes is 1, 3, 2.
  128. """
  129. def _set_msb_half_byte(address: int, value_: int) -> None:
  130. """
  131. Sets 4 most significant bits (msb half-byte) of 'boot_sector_state.binary_image' at given
  132. 'address' to 'value_' (size of variable 'value_' is half byte)
  133. If a byte contents is 0b11110000, the msb half-byte would be 0b1111
  134. """
  135. self.boot_sector_state.binary_image[address] &= 0x0f
  136. self.boot_sector_state.binary_image[address] |= value_ << 4
  137. def _set_lsb_half_byte(address: int, value_: int) -> None:
  138. """
  139. Sets 4 least significant bits (lsb half-byte) of 'boot_sector_state.binary_image' at given
  140. 'address' to 'value_' (size of variable 'value_' is half byte)
  141. If a byte contents is 0b11110000, the lsb half-byte would be 0b0000
  142. """
  143. self.boot_sector_state.binary_image[address] &= 0xf0
  144. self.boot_sector_state.binary_image[address] |= value_
  145. # value must fit into number of bits of the fat (12, 16 or 32)
  146. assert value <= (1 << self.boot_sector_state.fatfs_type) - 1
  147. half_bytes = split_by_half_byte_12_bit_little_endian(value)
  148. bin_img_: bytearray = self.boot_sector_state.binary_image
  149. if self.boot_sector_state.fatfs_type == FAT12:
  150. assert merge_by_half_byte_12_bit_little_endian(*half_bytes) == value
  151. if self.fat_cluster_address % 8 == 0:
  152. # even block
  153. bin_img_[self.real_cluster_address] = build_byte(half_bytes[1], half_bytes[0])
  154. _set_lsb_half_byte(self.real_cluster_address + 1, half_bytes[2])
  155. elif self.fat_cluster_address % 8 != 0:
  156. # odd block
  157. _set_msb_half_byte(self.real_cluster_address, half_bytes[0])
  158. bin_img_[self.real_cluster_address + 1] = build_byte(half_bytes[2], half_bytes[1])
  159. elif self.boot_sector_state.fatfs_type == FAT16:
  160. bin_img_[self.real_cluster_address:self.real_cluster_address + 2] = Int16ul.build(value)
  161. assert self.get_from_fat() == value
  162. @property
  163. def is_root(self) -> bool:
  164. """
  165. The FAT12/FAT16 contains only one root directory,
  166. the root directory allocates the first cluster with the ID `ROOT_BLOCK_ID`.
  167. The method checks if the cluster belongs to the root directory.
  168. """
  169. return self.id == Cluster.ROOT_BLOCK_ID
  170. def allocate_cluster(self) -> None:
  171. """
  172. This method sets bits in FAT table to `allocated` and clean the corresponding sector(s)
  173. """
  174. self.set_in_fat(self.ALLOCATED_BLOCK_SWITCH[self.boot_sector_state.fatfs_type])
  175. cluster_start = self.cluster_data_address
  176. dir_size = get_dir_size(self.is_root, self.boot_sector_state)
  177. cluster_end = cluster_start + dir_size
  178. self.boot_sector_state.binary_image[cluster_start:cluster_end] = dir_size * EMPTY_BYTE