FatFileSystem.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*++
  2. Copyright (c) 2005, Intel Corporation. All rights reserved.<BR>
  3. This program and the accompanying materials are licensed and made available
  4. under the terms and conditions of the BSD License which accompanies this
  5. distribution. The full text of the license may be found at
  6. http://opensource.org/licenses/bsd-license.php
  7. THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  9. Module Name:
  10. FatFileSystem.h
  11. Abstract:
  12. Definitions for on-disk FAT structures
  13. Revision History
  14. --*/
  15. #ifndef _FATFILESYSTEM_H_
  16. #define _FATFILESYSTEM_H_
  17. #pragma pack(1)
  18. //
  19. // FAT info signature
  20. //
  21. #define FAT_INFO_SIGNATURE 0x41615252
  22. #define FAT_INFO_BEGIN_SIGNATURE 0x61417272
  23. #define FAT_INFO_END_SIGNATURE 0xAA550000
  24. //
  25. // FAT entry values
  26. //
  27. #define FAT_CLUSTER_SPECIAL_EXT (-1 & (~0xF))
  28. #define FAT_CLUSTER_SPECIAL ((FAT_CLUSTER_SPECIAL_EXT) | 0x07)
  29. #define FAT_CLUSTER_FREE 0
  30. #define FAT_CLUSTER_RESERVED (FAT_CLUSTER_SPECIAL)
  31. #define FAT_CLUSTER_BAD (FAT_CLUSTER_SPECIAL)
  32. #define FAT_CLUSTER_LAST (-1)
  33. #define FAT_END_OF_FAT_CHAIN(Cluster) ((Cluster) > (FAT_CLUSTER_SPECIAL))
  34. #define FAT_MIN_CLUSTER 2
  35. #define FAT_MAX_FAT12_CLUSTER 0xFF5
  36. #define FAT_MAX_FAT16_CLUSTER 0xFFF5
  37. #define FAT_CLUSTER_SPECIAL_FAT12 0xFF7
  38. #define FAT_CLUSTER_SPECIAL_FAT16 0xFFF7
  39. #define FAT_CLUSTER_SPECIAL_FAT32 0x0FFFFFF7
  40. #define FAT_CLUSTER_MASK_FAT12 0xFFF
  41. #define FAT_CLUSTER_UNMASK_FAT12 0xF000
  42. #define FAT_CLUSTER_MASK_FAT32 0x0FFFFFFF
  43. #define FAT_CLUSTER_UNMASK_FAT32 0xF0000000
  44. #define FAT_POS_FAT12(a) ((a) * 3 / 2)
  45. #define FAT_POS_FAT16(a) ((a) * 2)
  46. #define FAT_POS_FAT32(a) ((a) * 4)
  47. #define FAT_ODD_CLUSTER_FAT12(a) (((a) & 1) != 0)
  48. //
  49. // FAT attribute define
  50. //
  51. #define FAT_ATTRIBUTE_READ_ONLY 0x01
  52. #define FAT_ATTRIBUTE_HIDDEN 0x02
  53. #define FAT_ATTRIBUTE_SYSTEM 0x04
  54. #define FAT_ATTRIBUTE_VOLUME_ID 0x08
  55. #define FAT_ATTRIBUTE_DIRECTORY 0x10
  56. #define FAT_ATTRIBUTE_ARCHIVE 0x20
  57. #define FAT_ATTRIBUTE_DEVICE 0x40
  58. #define FAT_ATTRIBUTE_LFN 0x0F
  59. //
  60. // Some Long File Name definitions
  61. //
  62. #define FAT_LFN_LAST 0x40 // Ordinal field
  63. #define MAX_LFN_ENTRIES 20
  64. #define LFN_CHAR1_LEN 5
  65. #define LFN_CHAR2_LEN 6
  66. #define LFN_CHAR3_LEN 2
  67. #define LFN_CHAR_TOTAL (LFN_CHAR1_LEN + LFN_CHAR2_LEN + LFN_CHAR3_LEN)
  68. #define LFN_ENTRY_NUMBER(a) (((a) + LFN_CHAR_TOTAL - 1) / LFN_CHAR_TOTAL)
  69. //
  70. // Some 8.3 File Name definitions
  71. //
  72. #define FAT_MAIN_NAME_LEN 8
  73. #define FAT_EXTEND_NAME_LEN 3
  74. #define FAT_NAME_LEN (FAT_MAIN_NAME_LEN + FAT_EXTEND_NAME_LEN)
  75. //
  76. // Some directory entry information
  77. //
  78. #define FAT_ENTRY_INFO_OFFSET 13
  79. #define DELETE_ENTRY_MARK 0xE5
  80. #define EMPTY_ENTRY_MARK 0x00
  81. //
  82. // Volume dirty Mask
  83. //
  84. #define FAT16_DIRTY_MASK 0x7fff
  85. #define FAT32_DIRTY_MASK 0xf7ffffff
  86. //
  87. // internal flag
  88. //
  89. #define FAT_CASE_MIXED 0x01
  90. #define FAT_CASE_NAME_LOWER 0x08
  91. #define FAT_CASE_EXT_LOWER 0x10
  92. typedef struct {
  93. UINT8 Ia32Jump[3];
  94. CHAR8 OemId[8];
  95. UINT16 SectorSize;
  96. UINT8 SectorsPerCluster;
  97. UINT16 ReservedSectors;
  98. UINT8 NumFats;
  99. UINT16 RootEntries; // < FAT32, root dir is fixed size
  100. UINT16 Sectors;
  101. UINT8 Media;
  102. UINT16 SectorsPerFat; // < FAT32
  103. UINT16 SectorsPerTrack; // (ignored)
  104. UINT16 Heads; // (ignored)
  105. UINT32 HiddenSectors; // (ignored)
  106. UINT32 LargeSectors; // Used if Sectors==0
  107. } FAT_BOOT_SECTOR_BASIC;
  108. typedef struct {
  109. UINT8 PhysicalDriveNumber; // (ignored)
  110. UINT8 CurrentHead; // holds boot_sector_dirty bit
  111. UINT8 Signature; // (ignored)
  112. CHAR8 Id[4];
  113. CHAR8 FatLabel[11];
  114. CHAR8 SystemId[8];
  115. } FAT_BOOT_SECTOR_EXT;
  116. typedef struct {
  117. UINT32 LargeSectorsPerFat; // FAT32
  118. UINT16 ExtendedFlags; // FAT32 (ignored)
  119. UINT16 FsVersion; // FAT32 (ignored)
  120. UINT32 RootDirFirstCluster; // FAT32
  121. UINT16 FsInfoSector; // FAT32
  122. UINT16 BackupBootSector; // FAT32
  123. UINT8 Reserved[12]; // FAT32 (ignored)
  124. UINT8 PhysicalDriveNumber; // (ignored)
  125. UINT8 CurrentHead; // holds boot_sector_dirty bit
  126. UINT8 Signature; // (ignored)
  127. CHAR8 Id[4];
  128. CHAR8 FatLabel[11];
  129. CHAR8 SystemId[8];
  130. } FAT32_BOOT_SECTOR_EXT;
  131. typedef struct {
  132. FAT_BOOT_SECTOR_BASIC FatBsb;
  133. union {
  134. FAT_BOOT_SECTOR_EXT FatBse;
  135. FAT32_BOOT_SECTOR_EXT Fat32Bse;
  136. } FatBse;
  137. } FAT_BOOT_SECTOR;
  138. //
  139. // FAT Info Structure
  140. //
  141. typedef struct {
  142. UINT32 ClusterCount;
  143. UINT32 NextCluster;
  144. } FAT_FREE_INFO;
  145. typedef struct {
  146. UINT32 Signature;
  147. UINT8 ExtraBootCode[480];
  148. UINT32 InfoBeginSignature;
  149. FAT_FREE_INFO FreeInfo;
  150. UINT8 Reserved[12];
  151. UINT32 InfoEndSignature;
  152. } FAT_INFO_SECTOR;
  153. //
  154. // Directory Entry
  155. //
  156. #define FAT_MAX_YEAR_FROM_1980 0x7f
  157. typedef struct {
  158. UINT16 Day : 5;
  159. UINT16 Month : 4;
  160. UINT16 Year : 7; // From 1980
  161. } FAT_DATE;
  162. typedef struct {
  163. UINT16 DoubleSecond : 5;
  164. UINT16 Minute : 6;
  165. UINT16 Hour : 5;
  166. } FAT_TIME;
  167. typedef struct {
  168. FAT_TIME Time;
  169. FAT_DATE Date;
  170. } FAT_DATE_TIME;
  171. typedef struct {
  172. CHAR8 FileName[11]; // 8.3 filename
  173. UINT8 Attributes;
  174. UINT8 CaseFlag;
  175. UINT8 CreateMillisecond; // (creation milliseconds - ignored)
  176. FAT_DATE_TIME FileCreateTime;
  177. FAT_DATE FileLastAccess;
  178. UINT16 FileClusterHigh; // >= FAT32
  179. FAT_DATE_TIME FileModificationTime;
  180. UINT16 FileCluster;
  181. UINT32 FileSize;
  182. } FAT_DIRECTORY_ENTRY;
  183. typedef struct {
  184. UINT8 Ordinal;
  185. CHAR8 Name1[10]; // (Really 5 chars, but not WCHAR aligned)
  186. UINT8 Attributes;
  187. UINT8 Type;
  188. UINT8 Checksum;
  189. CHAR16 Name2[6];
  190. UINT16 MustBeZero;
  191. CHAR16 Name3[2];
  192. } FAT_DIRECTORY_LFN;
  193. #pragma pack()
  194. #endif