From 61f1852420f06e840d3cb7f0c40fcaae83d118a1 Mon Sep 17 00:00:00 2001 From: Nigel Date: Sat, 25 Feb 2023 21:03:10 +0100 Subject: [PATCH] Added file reading without cluster chain following --- kernel/kernel.cpp | 2 ++ kernel/storage/filesystems/FAT/FAT.cpp | 43 ++++++++------------------ kernel/storage/filesystems/FAT/FAT.h | 1 + 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/kernel/kernel.cpp b/kernel/kernel.cpp index f10b3cf..48081dc 100644 --- a/kernel/kernel.cpp +++ b/kernel/kernel.cpp @@ -122,6 +122,8 @@ extern "C" void kernel () printf(" [Size: %d bytes, Attributes: %d]\n", entry->ATTR, entry->FileSize); if(entry->ATTR & FAT::ATTRIBUTES::ATTR_DIRECTORY ){ FAT::OpenSubdir(entry, bpb); + } else { + FAT::readFile(entry, bpb); } } diff --git a/kernel/storage/filesystems/FAT/FAT.cpp b/kernel/storage/filesystems/FAT/FAT.cpp index 7a635e4..19619a0 100644 --- a/kernel/storage/filesystems/FAT/FAT.cpp +++ b/kernel/storage/filesystems/FAT/FAT.cpp @@ -146,8 +146,6 @@ uint16_t FAT::GetFATEntry (BiosParameterBlock* bpb, unsigned int cluster){ } - - uint16_t FAT::DetermineFreeSpace() { // Loop through all FAT entries in all FAT's @@ -180,8 +178,6 @@ unsigned int FAT::RootDirSize(BiosParameterBlock* bpb) } - - uint16_t* ReadFAT (BiosParameterBlock& bpb , bool DEBUG = false ) { uint32_t FATAddress = /*StartAddress*/ 0x00 + bpb.RsvdSecCnt ; uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256); @@ -253,42 +249,29 @@ void FAT::OpenSubdir(DIR* directory, BiosParameterBlock* bpb ){ printf("LFN\n"); } - - - - - - - } - - } +void FAT::readFile(DIR* fileEntry , BiosParameterBlock* bpb){ + unsigned int cluster = fileEntry->FstClusLo; + unsigned int FATEntry = FAT::GetFATEntry(bpb, cluster); + unsigned int root_dir_sectors = FAT::RootDirSize(bpb); + unsigned int fat_size = bpb->FATSz16; + unsigned int first_data_sector = bpb->RsvdSecCnt + (bpb->NumFATs * fat_size) + root_dir_sectors; + unsigned int file_data_sector = ((cluster -2) * bpb->SecPerClus) + first_data_sector; + printf("FAT entry = %x\n", FATEntry); + uint16_t data[256]; + ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, file_data_sector, data); -void readFile(uint32_t DataRegion, DIR* entry, uint16_t FATentry, BiosParameterBlock& bpb ){ - printf("Show contents"); - - printf("Start cluster of the file: 0x%x\n", entry->FileSize); - - printf("IS it only 1 cluster? %s\n", FATentry == 0xFFFF ? "Yes" : "No"); - - uint32_t sector = DataRegion + ((entry->FileSize - 0x02) * bpb.SecPerClus); - - - uint16_t dataBlob[256]; - ATAPIO::Read(ATAPIO_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob); - for (unsigned short n: dataBlob) { + for (unsigned short n : data) + { kterm_put(n & 0x00ff); - kterm_put(n >> 8); } - kterm_put('\n'); + } - - /* file fsysFatDirectory (const char* DirectoryName){ file file; diff --git a/kernel/storage/filesystems/FAT/FAT.h b/kernel/storage/filesystems/FAT/FAT.h index 1627aab..4d5ed59 100644 --- a/kernel/storage/filesystems/FAT/FAT.h +++ b/kernel/storage/filesystems/FAT/FAT.h @@ -83,6 +83,7 @@ public: static int GetSectorOfRootDirectory(BiosParameterBlock*); static unsigned int RootDirSize(BiosParameterBlock*); static void OpenSubdir (DIR*, BiosParameterBlock*); + static void readFile(DIR*, BiosParameterBlock*);