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