Max OS 0.3
Loading...
Searching...
No Matches
MaxOS::filesystem::format::Fat32Volume Class Reference

Handles the FAT table that stores the information about the files on the disk and operations on the disk. More...

#include <fat32.h>

Public Member Functions

 Fat32Volume (drivers::disk::Disk *disk, lba_t partition_offset)
 Construct a new Fat32 Volume object.
 
uint32_t next_cluster (uint32_t cluster) const
 Take the cluster and gets the next cluster in the chain.
 
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)
 
uint32_t find_free_cluster () const
 Searches the fat table for a free cluster starting from the first free cluster in the fsinfo, will then wrap around.
 
uint32_t allocate_cluster (uint32_t cluster)
 Allocate a cluster in the FAT table.
 
uint32_t allocate_cluster (uint32_t cluster, size_t amount)
 Allocate a number of clusters in the FAT table, updates the fsinfo and the chain.
 
void free_cluster (uint32_t cluster)
 Free a cluster in the FAT table.
 
void free_cluster (uint32_t cluster, size_t amount)
 Free a number of clusters in the FAT table.
 

Public Attributes

bpb32_t bpb = { }
 The BIOS Parameter Block for the FAT32 volume.
 
fs_info_t fsinfo = { }
 The FSInfo structure for the FAT32 volume.
 
size_t fat_total_clusters
 How many clusters are in the FAT table.
 
lba_t fat_lba
 The starting LBA of the FAT table.
 
lba_t fat_info_lba
 The LBA of the FSInfo structure.
 
lba_t fat_copies
 How many FAT tables are present.
 
lba_t data_lba
 The starting LBA of the data region.
 
lba_t root_lba
 The starting LBA of the root directory.
 
drivers::disk::Diskdisk
 The disk that this volume is on.
 

Detailed Description

Handles the FAT table that stores the information about the files on the disk and operations on the disk.

Definition at line 171 of file fat32.h.

Constructor & Destructor Documentation

◆ Fat32Volume()

Fat32Volume::Fat32Volume ( drivers::disk::Disk disk,
lba_t  partition_offset 
)

Construct a new Fat32 Volume object.

Parameters
diskThe disk to read from
partition_offsetThe offset of the partition on the disk

Definition at line 26 of file fat32.cpp.

27 : disk(disk) {
28
29 // Read the BIOS parameter block
30 buffer_t bpb_buffer(&bpb, sizeof(bpb32_t));
31 disk->read(partition_offset, &bpb_buffer);
32
33 // Parse the FAT info
35 bpb.total_sectors_32 - (bpb.reserved_sectors + (bpb.table_copies * bpb.table_size_32));
36 fat_total_clusters = total_data_sectors / bpb.sectors_per_cluster;
37 fat_lba = partition_offset + bpb.reserved_sectors;
38 fat_copies = bpb.table_copies;
39 fat_info_lba = partition_offset + bpb.fat_info;
40 data_lba = fat_lba + (bpb.table_copies * bpb.table_size_32);
41 root_lba = data_lba + bpb.sectors_per_cluster * (bpb.root_cluster - 2);
42
43 // Read the fs info
46
47 // Validate the fat information
48 if(fsinfo.lead_signature != 0x41615252 || fsinfo.structure_signature != 0x61417272 ||
49 fsinfo.trail_signature != 0xAA550000) {
50 Logger::ERROR() << "Invalid FAT32 filesystem information TODO: Handle this\n";
51 return;
52 }
53}
static Logger ERROR()
Gets active logger set to ERROR level.
Definition logger.cpp:216
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
void read(uint32_t sector, common::buffer_t *data_buffer)
read data from the disk into a buffer (max capacity 512 bytes)
Definition disk.cpp:26
fs_info_t fsinfo
The FSInfo structure for the FAT32 volume.
Definition fat32.h:177
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
struct PACKED MaxOS::filesystem::format::BiosParameterBlock32 bpb32_t
Alias for BiosParameterBlock32 struct.
struct PACKED MaxOS::filesystem::format::FSInfo fs_info_t
Alias for FSInfo struct.

References bpb, data_lba, disk, MaxOS::Logger::ERROR(), fat_copies, fat_info_lba, fat_lba, fat_total_clusters, fsinfo, MaxOS::drivers::disk::Disk::read(), and root_lba.

Member Function Documentation

◆ allocate_cluster() [1/2]

uint32_t Fat32Volume::allocate_cluster ( uint32_t  cluster)

Allocate a cluster in the FAT table.

Parameters
clusterThe base cluster to start from or 0 if this is a new chain
Returns
The next cluster in the chain

Definition at line 141 of file fat32.cpp.

141 {
142
143 // Allocate 1 cluster
144 return allocate_cluster(cluster, 1);
145}
uint32_t allocate_cluster(uint32_t cluster)
Allocate a cluster in the FAT table.
Definition fat32.cpp:141

References allocate_cluster().

Referenced by allocate_cluster(), and MaxOS::filesystem::format::Fat32File::write().

◆ allocate_cluster() [2/2]

uint32_t Fat32Volume::allocate_cluster ( uint32_t  cluster,
size_t  amount 
)

Allocate a number of clusters in the FAT table, updates the fsinfo and the chain.

Parameters
clusterThe base cluster to start from or 0 if this is a new chain
amountThe number of clusters to allocate
Returns
The next cluster in the chain

Definition at line 154 of file fat32.cpp.

154 {
155
156 // Make sure within bounds
158 return 0;
159
160 // Go through allocating the clusters
161 for(size_t i = 0; i < amount; i++) {
163
164 // Update the fsinfo
165 fsinfo.next_free_cluster = next_cluster + 1;
166 fsinfo.free_cluster_count -= 1;
167
168 // If there is an existing chain it needs to be updated
169 if(cluster != 0)
171
173 }
174
175 // Once all the updates are done flush the changes to the disk
178
179 // Finish the chain
180 set_next_cluster(cluster, (uint32_t) ClusterState::END_OF_CHAIN);
181 return cluster;
182}
void write(uint32_t sector, common::buffer_t *data)
write data to the disk from a buffer (max capacity 512 bytes)
Definition disk.cpp:50
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 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

References disk, fat_info_lba, fat_total_clusters, find_free_cluster(), fsinfo, next_cluster(), set_next_cluster(), and MaxOS::drivers::disk::Disk::write().

◆ find_free_cluster()

uint32_t Fat32Volume::find_free_cluster ( ) const

Searches the fat table for a free cluster starting from the first free cluster in the fsinfo, will then wrap around.

Returns
The first free cluster in the FAT table

Definition at line 119 of file fat32.cpp.

119 {
120
121 // Get the first free cluster
122 for(uint32_t start = fsinfo.next_free_cluster; start < fat_total_clusters + 1; start++)
123 if(next_cluster(start) == 0)
124 return start;
125
126 // Check any clusters before the first free cluster
127 for(uint32_t start = 2; start < fsinfo.next_free_cluster; start++)
128 if(next_cluster(start) == 0)
129 return start;
130
131 ASSERT(false, "No free clusters found in the FAT table");
132 return 0;
133}
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Definition logger.h:100

References ASSERT, fat_total_clusters, fsinfo, and next_cluster().

Referenced by allocate_cluster().

◆ free_cluster() [1/2]

void Fat32Volume::free_cluster ( uint32_t  cluster)

Free a cluster in the FAT table.

Parameters
clusterThe base cluster to start from

Definition at line 189 of file fat32.cpp.

189 {
190
191 // Free 1 cluster
193
194}
void free_cluster(uint32_t cluster)
Free a cluster in the FAT table.
Definition fat32.cpp:189

References free_cluster().

Referenced by free_cluster().

◆ free_cluster() [2/2]

void Fat32Volume::free_cluster ( uint32_t  cluster,
size_t  amount 
)

Free a number of clusters in the FAT table.

Parameters
clusterThe base cluster to start from
amountThe number of clusters to free

Definition at line 202 of file fat32.cpp.

202 {
203
204 // Make sure within bounds
206 return;
207
208 // Go through freeing the clusters
209 for(size_t i = 0; i < amount; i++) {
210
211 // Find the next cluster before it is removed from the chain
213
214 // Update the fsinfo
215 fsinfo.next_free_cluster = cluster;
216 fsinfo.free_cluster_count += 1;
217
218 // Update the chain
219 set_next_cluster(cluster, (lba_t) ClusterState::FREE);
221 }
222
223 // Save the fsinfo
226
227 // Mark the end of the chain
228 set_next_cluster(cluster, (uint32_t) ClusterState::END_OF_CHAIN);
229}
uint32_t lba_t
Logical Block Addressing type.
Definition filesystem.h:24

References disk, fat_info_lba, fat_total_clusters, fsinfo, next_cluster(), set_next_cluster(), and MaxOS::drivers::disk::Disk::write().

◆ next_cluster()

lba_t Fat32Volume::next_cluster ( uint32_t  cluster) const

Take the cluster and gets the next cluster in the chain.

Parameters
clusterThe base cluster to start from
Returns
The next cluster in the chain
Todo:
The auto entry = uint32_t* '&' is weird, fix it

Definition at line 65 of file fat32.cpp.

65 {
66
67 // Get the location in the FAT table
68 lba_t offset = cluster * sizeof(uint32_t);
69 lba_t sector = fat_lba + (offset / bpb.bytes_per_sector);
70 uint32_t entry_index = offset % bpb.bytes_per_sector;
71
72 // Read the FAT entry
73 buffer_t fat(bpb.bytes_per_sector);
74 disk->read(sector, &fat);
75
76 // Get the next cluster info (mask the upper 4 bits)
77 auto entry = (uint32_t*) (&(fat.raw()[entry_index]));
78 return *entry & 0x0FFFFFFF;
79}

References bpb, disk, fat_lba, and MaxOS::drivers::disk::Disk::read().

Referenced by allocate_cluster(), find_free_cluster(), free_cluster(), MaxOS::filesystem::format::Fat32File::read(), set_next_cluster(), MaxOS::filesystem::format::Fat32Directory::update_entry_on_disk(), and MaxOS::filesystem::format::Fat32File::write().

◆ set_next_cluster()

uint32_t Fat32Volume::set_next_cluster ( uint32_t  cluster,
uint32_t  next_cluster 
) const

Sets the next cluster in the chain (where the base cluster should point)

Parameters
clusterThe base cluster to start from
next_clusterThe next cluster in the chain
Returns
The next cluster in the chain
Todo:
when in userspace: For performance cache fat entirely, cache file data, cache cluster chains

Definition at line 90 of file fat32.cpp.

90 {
91
92 // Get the location in the FAT table
93 lba_t offset = cluster * sizeof(uint32_t);
94
95 for(uint32_t i = 0; i < fat_copies; ++i) {
96
97 lba_t sector = (fat_lba + i * bpb.table_size_32) + (offset / bpb.bytes_per_sector);
98 uint32_t entry_index = offset % bpb.bytes_per_sector;
99
100 // Read the FAT entry
101 buffer_t fat(bpb.bytes_per_sector);
102 disk->read(sector, &fat);
103
104 // Set the next cluster info (mask the upper 4 bits)
105 auto entry = (uint32_t*) (&(fat.raw()[entry_index]));
106 *entry = next_cluster & 0x0FFFFFFF;
107 disk->write(sector, &fat);
108
109 }
110
111 return next_cluster;
112}

References bpb, disk, fat_copies, fat_lba, next_cluster(), MaxOS::drivers::disk::Disk::read(), and MaxOS::drivers::disk::Disk::write().

Referenced by allocate_cluster(), and free_cluster().

Member Data Documentation

◆ bpb

◆ data_lba

lba_t MaxOS::filesystem::format::Fat32Volume::data_lba

◆ disk

◆ fat_copies

lba_t MaxOS::filesystem::format::Fat32Volume::fat_copies

How many FAT tables are present.

Definition at line 182 of file fat32.h.

Referenced by Fat32Volume(), and set_next_cluster().

◆ fat_info_lba

lba_t MaxOS::filesystem::format::Fat32Volume::fat_info_lba

The LBA of the FSInfo structure.

Definition at line 181 of file fat32.h.

Referenced by allocate_cluster(), Fat32Volume(), and free_cluster().

◆ fat_lba

lba_t MaxOS::filesystem::format::Fat32Volume::fat_lba

The starting LBA of the FAT table.

Definition at line 180 of file fat32.h.

Referenced by Fat32Volume(), next_cluster(), and set_next_cluster().

◆ fat_total_clusters

size_t MaxOS::filesystem::format::Fat32Volume::fat_total_clusters

How many clusters are in the FAT table.

Definition at line 179 of file fat32.h.

Referenced by allocate_cluster(), Fat32Volume(), find_free_cluster(), and free_cluster().

◆ fsinfo

fs_info_t MaxOS::filesystem::format::Fat32Volume::fsinfo = { }

The FSInfo structure for the FAT32 volume.

Definition at line 177 of file fat32.h.

177{ };

Referenced by allocate_cluster(), Fat32Volume(), find_free_cluster(), and free_cluster().

◆ root_lba

lba_t MaxOS::filesystem::format::Fat32Volume::root_lba

The starting LBA of the root directory.

Definition at line 185 of file fat32.h.

Referenced by Fat32Volume().


The documentation for this class was generated from the following files: