[FREELDR] Optimize and refactor the FAT driver.
[reactos.git] / boot / freeldr / freeldr / include / fs / fat.h
1 /*
2 * FreeLoader
3 * Copyright (C) 1998-2003 Brian Palmer <brianp@sginet.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <pshpack1.h>
23 typedef struct _FAT_BOOTSECTOR
24 {
25 UCHAR JumpBoot[3]; // Jump instruction to boot code
26 CHAR OemName[8]; // "MSWIN4.1" for MS formatted volumes
27 USHORT BytesPerSector; // Bytes per sector
28 UCHAR SectorsPerCluster; // Number of sectors in a cluster
29 USHORT ReservedSectors; // Reserved sectors, usually 1 (the bootsector)
30 UCHAR NumberOfFats; // Number of FAT tables
31 USHORT RootDirEntries; // Number of root directory entries (fat12/16)
32 USHORT TotalSectors; // Number of total sectors on the drive, 16-bit
33 UCHAR MediaDescriptor; // Media descriptor byte
34 USHORT SectorsPerFat; // Sectors per FAT table (fat12/16)
35 USHORT SectorsPerTrack; // Number of sectors in a track
36 USHORT NumberOfHeads; // Number of heads on the disk
37 ULONG HiddenSectors; // Hidden sectors (sectors before the partition start like the partition table)
38 ULONG TotalSectorsBig; // This field is the new 32-bit total count of sectors on the volume
39 UCHAR DriveNumber; // Int 0x13 drive number (e.g. 0x80)
40 UCHAR Reserved1; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
41 UCHAR BootSignature; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
42 ULONG VolumeSerialNumber; // Volume serial number
43 CHAR VolumeLabel[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
44 CHAR FileSystemType[8]; // One of the strings "FAT12 ", "FAT16 ", or "FAT "
45
46 UCHAR BootCodeAndData[448]; // The remainder of the boot sector
47
48 USHORT BootSectorMagic; // 0xAA55
49
50 } FAT_BOOTSECTOR, *PFAT_BOOTSECTOR;
51
52 typedef struct _FAT32_BOOTSECTOR
53 {
54 UCHAR JumpBoot[3]; // Jump instruction to boot code
55 CHAR OemName[8]; // "MSWIN4.1" for MS formatted volumes
56 USHORT BytesPerSector; // Bytes per sector
57 UCHAR SectorsPerCluster; // Number of sectors in a cluster
58 USHORT ReservedSectors; // Reserved sectors, usually 1 (the bootsector)
59 UCHAR NumberOfFats; // Number of FAT tables
60 USHORT RootDirEntries; // Number of root directory entries (fat12/16)
61 USHORT TotalSectors; // Number of total sectors on the drive, 16-bit
62 UCHAR MediaDescriptor; // Media descriptor byte
63 USHORT SectorsPerFat; // Sectors per FAT table (fat12/16)
64 USHORT SectorsPerTrack; // Number of sectors in a track
65 USHORT NumberOfHeads; // Number of heads on the disk
66 ULONG HiddenSectors; // Hidden sectors (sectors before the partition start like the partition table)
67 ULONG TotalSectorsBig; // This field is the new 32-bit total count of sectors on the volume
68 ULONG SectorsPerFatBig; // This field is the FAT32 32-bit count of sectors occupied by ONE FAT. BPB_FATSz16 must be 0
69 USHORT ExtendedFlags; // Extended flags (fat32)
70 USHORT FileSystemVersion; // File system version (fat32)
71 ULONG RootDirStartCluster; // Starting cluster of the root directory (fat32)
72 USHORT FsInfo; // Sector number of FSINFO structure in the reserved area of the FAT32 volume. Usually 1.
73 USHORT BackupBootSector; // If non-zero, indicates the sector number in the reserved area of the volume of a copy of the boot record. Usually 6.
74 UCHAR Reserved[12]; // Reserved for future expansion
75 UCHAR DriveNumber; // Int 0x13 drive number (e.g. 0x80)
76 UCHAR Reserved1; // Reserved (used by Windows NT). Code that formats FAT volumes should always set this byte to 0.
77 UCHAR BootSignature; // Extended boot signature (0x29). This is a signature byte that indicates that the following three fields in the boot sector are present.
78 ULONG VolumeSerialNumber; // Volume serial number
79 CHAR VolumeLabel[11]; // Volume label. This field matches the 11-byte volume label recorded in the root directory
80 CHAR FileSystemType[8]; // Always set to the string "FAT32 "
81
82 UCHAR BootCodeAndData[420]; // The remainder of the boot sector
83
84 USHORT BootSectorMagic; // 0xAA55
85
86 } FAT32_BOOTSECTOR, *PFAT32_BOOTSECTOR;
87
88 typedef struct _FATX_BOOTSECTOR
89 {
90 CHAR FileSystemType[4]; /* String "FATX" */
91 ULONG VolumeSerialNumber; /* Volume serial number */
92 ULONG SectorsPerCluster; /* Number of sectors in a cluster */
93 USHORT NumberOfFats; /* Number of FAT tables */
94 ULONG Unknown; /* Always 0? */
95 UCHAR Unused[494]; /* Actually size should be 4078 (boot block is 4096 bytes) */
96
97 } FATX_BOOTSECTOR, *PFATX_BOOTSECTOR;
98
99 /*
100 * Structure of MSDOS directory entry
101 */
102 typedef struct //_DIRENTRY
103 {
104 CHAR FileName[11]; /* Filename + extension */
105 UCHAR Attr; /* File attributes */
106 UCHAR ReservedNT; /* Reserved for use by Windows NT */
107 UCHAR TimeInTenths; /* Millisecond stamp at file creation */
108 USHORT CreateTime; /* Time file was created */
109 USHORT CreateDate; /* Date file was created */
110 USHORT LastAccessDate; /* Date file was last accessed */
111 USHORT ClusterHigh; /* High word of this entry's start cluster */
112 USHORT Time; /* Time last modified */
113 USHORT Date; /* Date last modified */
114 USHORT ClusterLow; /* First cluster number low word */
115 ULONG Size; /* File size */
116 } DIRENTRY, * PDIRENTRY;
117
118 typedef struct
119 {
120 UCHAR SequenceNumber; /* Sequence number for slot */
121 WCHAR Name0_4[5]; /* First 5 characters in name */
122 UCHAR EntryAttributes; /* Attribute byte */
123 UCHAR Reserved; /* Always 0 */
124 UCHAR AliasChecksum; /* Checksum for 8.3 alias */
125 WCHAR Name5_10[6]; /* 6 more characters in name */
126 USHORT StartCluster; /* Starting cluster number */
127 WCHAR Name11_12[2]; /* Last 2 characters in name */
128 } LFN_DIRENTRY, * PLFN_DIRENTRY;
129
130 typedef struct
131 {
132 UCHAR FileNameSize; /* Size of filename (max 42) */
133 UCHAR Attr; /* File attributes */
134 CHAR FileName[42]; /* Filename in ASCII, padded with 0xff (not zero-terminated) */
135 ULONG StartCluster; /* Starting cluster number */
136 ULONG Size; /* File size */
137 USHORT Time; /* Time last modified */
138 USHORT Date; /* Date last modified */
139 USHORT CreateTime; /* Time file was created */
140 USHORT CreateDate; /* Date file was created */
141 USHORT LastAccessTime; /* Time file was last accessed */
142 USHORT LastAccessDate; /* Date file was last accessed */
143 } FATX_DIRENTRY, * PFATX_DIRENTRY;
144 #include <poppack.h>
145
146 typedef struct _FAT_VOLUME_INFO *PFAT_VOLUME_INFO;
147
148 typedef struct
149 {
150 PFAT_VOLUME_INFO Volume;
151 ULONG FileSize; /* File size */
152 ULONG FilePointer; /* File pointer */
153 ULONG CurrentCluster; /* The cluster for file pointer */
154 ULONG StartCluster; /* The first cluster for file */
155 UCHAR Attributes; /* File attributes */
156 } FAT_FILE_INFO, * PFAT_FILE_INFO;
157
158 #define ATTR_NORMAL 0x00
159 #define ATTR_READONLY 0x01
160 #define ATTR_HIDDEN 0x02
161 #define ATTR_SYSTEM 0x04
162 #define ATTR_VOLUMENAME 0x08
163 #define ATTR_DIRECTORY 0x10
164 #define ATTR_ARCHIVE 0x20
165 #define ATTR_LONG_NAME (ATTR_READONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUMENAME)
166
167 #define FAT12 1
168 #define FAT16 2
169 #define FAT32 3
170 #define FATX16 4
171 #define FATX32 5
172
173 #define ISFATX(FT) ((FT) == FATX16 || (FT) == FATX32)
174
175 const DEVVTBL* FatMount(ULONG DeviceId);