13using namespace MaxOS::common;
14using namespace MaxOS::drivers;
15using namespace MaxOS::drivers::disk;
16using namespace MaxOS::filesystem;
17using namespace MaxOS::filesystem::format;
18using namespace MaxOS::memory;
35 bpb.total_sectors_32 - (
bpb.reserved_sectors + (
bpb.table_copies *
bpb.table_size_32));
37 fat_lba = partition_offset +
bpb.reserved_sectors;
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";
55Fat32Volume::~Fat32Volume() =
default;
70 uint32_t entry_index = offset %
bpb.bytes_per_sector;
77 auto entry = (
uint32_t*) (&(
fat.raw()[entry_index]));
78 return *entry & 0x0FFFFFFF;
98 uint32_t entry_index = offset %
bpb.bytes_per_sector;
105 auto entry = (
uint32_t*) (&(
fat.raw()[entry_index]));
131 ASSERT(
false,
"No free clusters found in the FAT table");
161 for(
size_t i = 0; i <
amount; i++) {
166 fsinfo.free_cluster_count -= 1;
209 for(
size_t i = 0; i <
amount; i++) {
216 fsinfo.free_cluster_count += 1;
241 m_parent_directory(
parent),
243 m_first_cluster((info->first_cluster_high << 16) | info->first_cluster_low) {
250Fat32File::~Fat32File() =
default;
262 size_t buffer_space = m_volume->
bpb.bytes_per_sector * m_volume->
bpb.sectors_per_cluster;
348 m_entry->first_cluster_high = (m_first_cluster >> 16) & 0xFFFF;
349 m_entry->first_cluster_low = m_first_cluster & 0xFFFF;
362 size_t buffer_space = m_volume->
bpb.bytes_per_sector * m_volume->
bpb.sectors_per_cluster;
430Fat32Directory::~Fat32Directory() =
default;
450 for(
size_t i = 0; i < 8; i++)
452 for(
size_t i = 0; i < 3; i++)
461 entry.first_cluster_high = (
cluster >> 16) & 0xFFFF;
462 entry.first_cluster_low =
cluster & 0xFFFF;
467 if(entry_index == -1)
475 for(
size_t index = entry_index +
lfn_entries.size() - 1; index >= entry_index; index--)
480 return &m_entries[entry_index];
497 uint32_t bytes_per_sector = m_volume->
bpb.bytes_per_sector;
506 return &m_entries[entry_index];
529 m_entries[i].
name[0] = (
uint8_t) DirectoryEntryType::FREE;
538 next_cluster < (
lba_t) ClusterState::BAD; next_cluster = m_volume->
next_cluster(next_cluster))
548void Fat32Directory::read_all_entries() {
551 size_t buffer_space = m_volume->
bpb.bytes_per_sector * m_volume->
bpb.sectors_per_cluster;
561 m_current_cluster_length++;
576 if(entry->name[0] == (
uint8_t) DirectoryEntryType::LAST)
592 for(
auto& m_entry : m_entries) {
593 if(&m_entry == entry)
608 auto entry = m_entries[index];
611 uint32_t bytes_per_sector = m_volume->
bpb.bytes_per_sector;
641 for(; entry_index < m_entries.
size(); entry_index++) {
642 auto& entry = m_entries[entry_index];
645 if(entry.name[0] == (
uint8_t) DirectoryEntryType::LAST)
649 if(entry.name[0] == (
uint8_t) DirectoryEntryType::FREE)
659 if(entry_index >= m_entries.
size())
675 for(
uint32_t entry_index = 0; entry_index < m_entries.
size(); entry_index++) {
678 for(
size_t j = 0; j <
amount; j++)
679 if(m_entries[entry_index + j].
name[0] != (
uint8_t) DirectoryEntryType::FREE)
700int Fat32Directory::expand_directory(
size_t amount) {
709 if(m_entries[i].
name[0] == (
uint8_t) DirectoryEntryType::FREE)
725 (m_volume->
bpb.bytes_per_sector * m_volume->
bpb.sectors_per_cluster);
736 free.name[0] = (
uint8_t) DirectoryEntryType::FREE;
741 m_entries[m_entries.
size() - 1].name[0] = (
uint8_t) DirectoryEntryType::LAST;
774 for(
int j = 0; j < 13; j++) {
806 for(
int i = 0; i < 13; i++) {
811 c = entry->name1[i] & 0xFF;
813 c = entry->name2[i - 5] & 0xFF;
815 c = entry->name3[i - 11] & 0xFF;
818 if(c == (
char) 0xFF || c ==
'\0')
844 for(
auto& entry : m_entries) {
847 if(entry.name[0] == (
uint8_t) DirectoryEntryType::FREE
848 || entry.attributes == (
uint8_t) DirectoryEntryAttributes::FREE
849 || entry.attributes == (
uint8_t) DirectoryEntryAttributes::VOLUME_ID)
853 if(entry.attributes == (
uint8_t) DirectoryEntryAttributes::LONG_NAME) {
894 if(file->name() ==
name)
915 if(file->name() ==
name) {
945 auto entry = create_entry(
name,
true);
946 uint32_t cluster = ((entry->first_cluster_high << 16) | entry->first_cluster_low);
990 : m_volume(disk, partition_offset) {
998Fat32FileSystem::~Fat32FileSystem() =
default;
static Logger ERROR()
Gets active logger set to ERROR level.
Wrapper class for a region of bytes in memor in an attempt to add some memory safety....
void set_offset(size_t offset)
Set the offset for where operations should begin from.
void copy_from(const Buffer *buffer)
Copies all the bytes from another buffer into this buffer.
void clear()
Fulls the buffer with 0's and resets the offset.
Stores the left, top, width and height of a rectangle.
uint32_t size() const
Returns the number of elements in the Vector.
void clear()
Removes all elements from the Vector.
iterator push_back(Type)
Adds an element to the end of the vector and returns the iterator of the element.
Generic Disk, handles the reading and writing of data to the hard drive.
void write(uint32_t sector, common::buffer_t *data)
write data to the disk from a buffer (max capacity 512 bytes)
void read(uint32_t sector, common::buffer_t *data_buffer)
read data from the disk into a buffer (max capacity 512 bytes)
Handles a group of files (directory)
common::Vector< Directory * > m_subdirectories
The subdirectories in this directory.
string m_name
The name of this directory.
common::Vector< File * > m_files
The files in this directory.
virtual void read_from_disk()
read the directory from the disk
string name()
Get the name of the directory.
Directory * m_root_directory
The fist directory in the file system (not be confused with the system root)
Handles file operations and information.
size_t m_size
The size of the file.
string m_name
The name of the file.
uint32_t m_offset
The current offset in the file.
string name()
Get the name of the file.
virtual void flush()
Flush the file to the disk.
Defines structures and constants for the File Allocation Table 32 (FAT32) filesystem format and the r...
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.
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.
struct PACKED MaxOS::filesystem::format::DirectoryEntry dir_entry_t
Alias for DirectoryEntry struct.
uint32_t lba_t
Logical Block Addressing type.
#define ASSERT(condition, format,...)
If the specified condition is not met then the kernel will crash with the specified message.
Defines classes for memory input and output operations of various bit widths (8, 16,...
void * memcpy(void *destination, const void *source, uint64_t num)
Copies a block of memory from one location to another.
class MaxOS::String string
Typedef for String.
Stores information about a file or directory.