Max OS 0.3
Loading...
Searching...
No Matches
fat32.h
Go to the documentation of this file.
1
13#ifndef MAXOS_FILESYSTEM_FAT32_H
14#define MAXOS_FILESYSTEM_FAT32_H
15
16#include <cstdint>
17#include <common/macros.h>
18#include <drivers/disk/disk.h>
20
21
22namespace MaxOS::filesystem::format {
23
31 typedef struct PACKED BiosParameterBlock32 {
32
33 uint8_t jump[3];
34 uint8_t OEM_name[8];
38 uint8_t table_copies;
39 uint16_t root_entries;
41 uint8_t media_type;
44 uint16_t head_count;
45 uint32_t hidden_sectors;
47
48 uint32_t table_size_32;
49 uint16_t extended_flags;
50 uint16_t fat_version;
51 uint32_t root_cluster;
52 uint16_t fat_info;
53 uint16_t backup_sector;
54 uint8_t reserved0[12];
55 uint8_t drive_number;
56 uint8_t reserved_1;
58 uint32_t volume_id;
59 uint8_t volume_label[11];
60 uint8_t file_system_type[8];
61
62 } bpb32_t;
63
71 typedef struct PACKED FSInfo {
72 uint32_t lead_signature;
73 uint8_t reserved1[480];
77 uint8_t reserved2[12];
78 uint32_t trail_signature;
79 } fs_info_t;
80
88 typedef struct PACKED DirectoryEntry {
89
90 uint8_t name[8];
91 uint8_t extension[3];
92 uint8_t attributes;
93 uint8_t reserved;
94
96 uint16_t creation_time;
97 uint16_t creation_date;
99
101
104
106
107 uint32_t size;
108
109 } dir_entry_t;
110
116 FREE = 0x00,
117 READ_ONLY = 0x01,
118 HIDDEN = 0x02,
119 SYSTEM = 0x04,
120 VOLUME_ID = 0x08,
121 DIRECTORY = 0x10,
122 ARCHIVE = 0x20,
123 LONG_NAME = READ_ONLY | HIDDEN | SYSTEM | VOLUME_ID,
124 };
125
131 LAST = 0x00,
132 FREE = 0xE5,
133 };
134
142 typedef struct PACKED LongFileNameEntry {
143 uint8_t order;
144 uint16_t name1[5];
145 uint8_t attributes;
146 uint8_t type;
147 uint8_t checksum;
148 uint16_t name2[6];
149 uint16_t zero;
150 uint16_t name3[2];
151
152 } long_file_name_entry_t;
153
158 enum class ClusterState : uint32_t {
159 FREE = 0x00000000,
160 BAD = 0x0FFFFFF7,
161 END_OF_CHAIN = 0xFFFFFFFF,
162 };
163
165 constexpr uint16_t MAX_NAME_LENGTH = 255;
166
172 public:
173 Fat32Volume(drivers::disk::Disk* disk, lba_t partition_offset);
174 ~Fat32Volume();
175
176 bpb32_t bpb = { };
178
183
186
188
189 [[nodiscard]] uint32_t next_cluster(uint32_t cluster) const;
190 uint32_t set_next_cluster(uint32_t cluster, uint32_t next_cluster) const;
191 uint32_t find_free_cluster() const;
192
193 uint32_t allocate_cluster(uint32_t cluster);
194 uint32_t allocate_cluster(uint32_t cluster, size_t amount);
195
196 void free_cluster(uint32_t cluster);
197 void free_cluster(uint32_t cluster, size_t amount);
198 };
199
200 // Forward def
201 class Fat32Directory;
202
207 class Fat32File final : public File {
208
209 private:
210 Fat32Volume* m_volume;
211 Fat32Directory* m_parent_directory;
212
213 dir_entry_t* m_entry;
214 uint32_t m_first_cluster;
215
216 public:
217 Fat32File(Fat32Volume* volume, Fat32Directory* parent, dir_entry_t* info, const string& name);
218 ~Fat32File() final;
219
220 void write(common::buffer_t* data, size_t amount) final;
221 void read(common::buffer_t* data, size_t amount) final;
222 void flush() final;
223
228 [[nodiscard]] uint32_t first_cluster() const { return m_first_cluster; }
229 };
230
235 class Fat32Directory final : public Directory {
236 friend class Fat32File;
237
238 private:
239 Fat32Volume* m_volume;
240
241 lba_t m_first_cluster;
242 lba_t m_last_cluster = 0;
243 size_t m_current_cluster_length = 0;
244
246
247 dir_entry_t* create_entry(const string& name, bool is_directory);
248 void remove_entry(lba_t cluster);
249 void read_all_entries();
250
251 uint32_t entry_index(lba_t cluster);
252 int64_t find_free_entries(size_t amount);
253 int expand_directory(size_t amount);
254
255 static common::Vector<long_file_name_entry_t> to_long_filenames(string name);
256 static string parse_long_filename(long_file_name_entry_t* entry, const string& current);
257
258 protected:
259
260 void save_entry_to_disk(dir_entry_t* entry);
261 void update_entry_on_disk(size_t index);
262
263 public:
264 Fat32Directory(Fat32Volume* volume, lba_t cluster, const string& name);
265 ~Fat32Directory() final;
266
267 void read_from_disk() final;
268
269 File* create_file(const string& name) final;
270 void remove_file(const string& name) final;
271
272 Directory* create_subdirectory(const string& name) final;
273 void remove_subdirectory(const string& name) final;
274
279 [[nodiscard]] lba_t first_cluster() const { return m_first_cluster; }
280 };
281
286 class Fat32FileSystem final : public FileSystem {
287 private:
288 Fat32Volume m_volume;
289
290 public:
291 Fat32FileSystem(drivers::disk::Disk* disk, uint32_t partition_offset);
292 ~Fat32FileSystem() final;
293 };
294
295}
296
297
298#endif //MAXOS_FILESYSTEM_FAT32_H
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
Definition buffer.h:25
Stores the left, top, width and height of a rectangle.
Definition rectangle.h:22
Generic Disk, handles the reading and writing of data to the hard drive.
Definition disk.h:24
Handles a group of files (directory)
Definition filesystem.h:57
string name()
Get the name of the directory.
Handles the disk operations and file system information.
Definition filesystem.h:96
Handles file operations and information.
Definition filesystem.h:31
string name()
Get the name of the file.
Handles the directory operations on the FAT32 filesystem.
Definition fat32.h:235
void remove_file(const string &name) final
Delete a file from the subdirectory.
Definition fat32.cpp:912
File * create_file(const string &name) final
Create a new file in the directory.
Definition fat32.cpp:890
void update_entry_on_disk(size_t index)
Save the directory entry at the given index to the disk.
Definition fat32.cpp:605
Directory * create_subdirectory(const string &name) final
Create a new directory in the directory.
Definition fat32.cpp:933
void read_from_disk() final
read the directory from the disk
Definition fat32.cpp:829
void remove_subdirectory(const string &name) final
Remove a directory entry from the directory.
Definition fat32.cpp:959
void save_entry_to_disk(dir_entry_t *entry)
Writes an updated directory entry to the disk.
Definition fat32.cpp:589
lba_t first_cluster() const
Get the first cluster of the directory.
Definition fat32.h:279
Handles the FAT32 filesystem operations.
Definition fat32.h:286
Handles the file operations on the FAT32 filesystem.
Definition fat32.h:207
void read(common::buffer_t *data, size_t amount) final
read data from the file (at the current seek position, updated to be += amount)
Definition fat32.cpp:361
void write(common::buffer_t *data, size_t amount) final
write data to the file (at the current seek position, updated to be += amount)
Definition fat32.cpp:260
uint32_t first_cluster() const
Get the first cluster of the directory.
Definition fat32.h:228
void flush() final
Flush the file to the disk.
Definition fat32.cpp:413
Handles the FAT table that stores the information about the files on the disk and operations on the d...
Definition fat32.h:171
uint32_t next_cluster(uint32_t cluster) const
Take the cluster and gets the next cluster in the chain.
Definition fat32.cpp:65
uint32_t find_free_cluster() const
Searches the fat table for a free cluster starting from the first free cluster in the fsinfo,...
Definition fat32.cpp:119
uint32_t allocate_cluster(uint32_t cluster)
Allocate a cluster in the FAT table.
Definition fat32.cpp:141
fs_info_t fsinfo
The FSInfo structure for the FAT32 volume.
Definition fat32.h:177
void free_cluster(uint32_t cluster)
Free a cluster in the FAT table.
Definition fat32.cpp:189
lba_t fat_lba
The starting LBA of the FAT table.
Definition fat32.h:180
drivers::disk::Disk * disk
The disk that this volume is on.
Definition fat32.h:187
bpb32_t bpb
The BIOS Parameter Block for the FAT32 volume.
Definition fat32.h:176
lba_t fat_info_lba
The LBA of the FSInfo structure.
Definition fat32.h:181
lba_t root_lba
The starting LBA of the root directory.
Definition fat32.h:185
lba_t fat_copies
How many FAT tables are present.
Definition fat32.h:182
lba_t data_lba
The starting LBA of the data region.
Definition fat32.h:184
size_t fat_total_clusters
How many clusters are in the FAT table.
Definition fat32.h:179
uint32_t set_next_cluster(uint32_t cluster, uint32_t next_cluster) const
Sets the next cluster in the chain (where the base cluster should point)
Definition fat32.cpp:90
Defines a generic Disk driver for reading and writing data to disk drives.
ClusterState
The state of a cluster in the FAT32 filesystem.
Definition fat32.h:158
struct PACKED MaxOS::filesystem::format::BiosParameterBlock32 bpb32_t
Alias for BiosParameterBlock32 struct.
struct PACKED MaxOS::filesystem::format::LongFileNameEntry long_file_name_entry_t
Alias for LongFileNameEntry struct.
DirectoryEntryAttributes
Flags for a directory entry in a FAT32 System.
Definition fat32.h:115
struct PACKED MaxOS::filesystem::format::FSInfo fs_info_t
Alias for FSInfo struct.
constexpr uint16_t MAX_NAME_LENGTH
Highest number of characters in a file/directory name.
Definition fat32.h:165
struct PACKED MaxOS::filesystem::format::DirectoryEntry dir_entry_t
Alias for DirectoryEntry struct.
DirectoryEntryType
The type of a directory entry.
Definition fat32.h:130
Defines a generic API for a file system, including classes for File, Directory, and FileSystem.
uint32_t lba_t
Logical Block Addressing type.
Definition filesystem.h:24
Stores information about the FAT32 filesystem.
Definition fat32.h:31
uint16_t reserved_sectors
Number of reserved sectors.
Definition fat32.h:37
uint16_t bytes_per_sector
Bytes per sector.
Definition fat32.h:35
uint32_t table_size_32
Sectors per FAT (FAT32 only)
Definition fat32.h:48
uint8_t sectors_per_cluster
Sectors per cluster.
Definition fat32.h:36
uint16_t head_count
Number of heads/sides.
Definition fat32.h:44
uint8_t boot_signature
Extended boot signature (0x28 or 0x29)
Definition fat32.h:57
uint8_t reserved_1
Reserved (used by Windows NT)
Definition fat32.h:56
uint16_t extended_flags
Extended flags.
Definition fat32.h:49
uint16_t total_sectors_16
Total number of sectors (if zero, use total_sectors_32)
Definition fat32.h:40
uint8_t drive_number
Drive number (must be identical to BIOS drive number) (0x00 = floppy, 0x80 = first hard disk)
Definition fat32.h:55
uint16_t fat_info
Sector number of FSInfo structure.
Definition fat32.h:52
uint8_t media_type
Media descriptor.
Definition fat32.h:41
uint8_t table_copies
Number of FAT tables.
Definition fat32.h:38
uint32_t total_sectors_32
Total number of sectors (if total_sectors_16 is zero)
Definition fat32.h:46
uint32_t root_cluster
Cluster number of root directory start.
Definition fat32.h:51
uint16_t fat_sector_count
Sectors per FAT (FAT12/FAT16 only)
Definition fat32.h:42
uint32_t hidden_sectors
Number of hidden sectors (LBA of start of partition)
Definition fat32.h:45
uint16_t root_entries
Number of root directory entries.
Definition fat32.h:39
uint32_t volume_id
Volume serial number.
Definition fat32.h:58
uint16_t sectors_per_track
Sectors per track.
Definition fat32.h:43
uint16_t fat_version
FAT version number (high byte major, low byte minor)
Definition fat32.h:50
uint16_t backup_sector
Sector number of backup boot sector.
Definition fat32.h:53
Stores information about a file or directory.
Definition fat32.h:88
uint32_t size
File size in bytes.
Definition fat32.h:107
uint8_t creation_time_tenth
Creation time in tenths of a second (or 100ths of a second) (0-199 on Windows, 0/100 on Linux)
Definition fat32.h:95
uint16_t last_access_date
Last access date (YYYY-MM-DD)
Definition fat32.h:98
uint16_t creation_time
Creation time (HH:MM:SS) (multiply seconds by 2)
Definition fat32.h:96
uint8_t reserved
Reserved for Windows NT.
Definition fat32.h:93
uint16_t last_write_time
Last write time (HH:MM:SS) (multiply seconds by 2)
Definition fat32.h:102
uint16_t creation_date
Creation date (YYYY-MM-DD)
Definition fat32.h:97
uint16_t first_cluster_high
High word of this entry's first cluster number (usually 0 for FAT12/FAT16)
Definition fat32.h:100
uint16_t first_cluster_low
Low word of this entry's first cluster number.
Definition fat32.h:105
uint8_t attributes
File attributes (see DirectoryEntryAttributes)
Definition fat32.h:92
uint16_t last_write_date
Last write date (YYYY-MM-DD)
Definition fat32.h:103
Stores extra information about the FAT32 filesystem.
Definition fat32.h:71
uint32_t free_cluster_count
Number of free clusters (or 0xFFFFFFFF if unknown) (not reliable, must range check)
Definition fat32.h:75
uint32_t next_free_cluster
Cluster number of the next free cluster (or 0xFFFFFFFF if unknown, start searching from cluster 2)
Definition fat32.h:76
uint32_t trail_signature
FSInfo trail signature (0xAA550000)
Definition fat32.h:78
uint32_t lead_signature
FSInfo lead signature (0x41615252)
Definition fat32.h:72
uint32_t structure_signature
FSInfo structure signature (0x61417272)
Definition fat32.h:74
Directory entry for a long file name.
Definition fat32.h:142
uint8_t checksum
checksum of the corresponding short file name
Definition fat32.h:147
uint8_t order
Index of this entry in the sequence of LFN entries.
Definition fat32.h:143
uint8_t attributes
Attributes (always 0x0F for LFN entries)
Definition fat32.h:145
uint8_t type
Type (always 0 for LFN entries)
Definition fat32.h:146