Started fleshing out the storage API
This commit is contained in:
@ -1,14 +1,14 @@
|
||||
#pragma once
|
||||
#include <stdint.h>
|
||||
#include <stdint-gcc.h>
|
||||
|
||||
struct DirectoryEntry {
|
||||
uint8_t filename [8];
|
||||
uint8_t Extension [3];
|
||||
uint8_t attribute;
|
||||
uint8_t Reserved;
|
||||
uint8_t creation;
|
||||
uint16_t CreationTime;
|
||||
uint16_t CreationDate;
|
||||
uint8_t creation; // Creation in tenths of a second
|
||||
uint16_t CreationTime; // Time Created NOTE: Multiply the seconds by 2
|
||||
uint16_t CreationDate; // Date Created
|
||||
uint16_t LastAccessDate;
|
||||
uint16_t ReservedFAT32;
|
||||
uint16_t LastWriteTime;
|
||||
@ -16,4 +16,22 @@ struct DirectoryEntry {
|
||||
uint16_t StartingCluster;
|
||||
uint32_t FilesizeInBytes;
|
||||
|
||||
}__attribute__((packed));
|
||||
}__attribute__((packed));
|
||||
|
||||
|
||||
|
||||
typedef struct _DIRECTORY{
|
||||
uint8_t Filename[8];
|
||||
uint8_t Ext[3];
|
||||
uint8_t Attrib;
|
||||
uint8_t Reserved;
|
||||
uint8_t TimeCreatedMs;
|
||||
uint16_t TimeCreated;
|
||||
uint16_t DateCreated;
|
||||
uint16_t DateLastAccessed;
|
||||
uint16_t FirstClusterHiBytes;
|
||||
uint16_t LastModTime;
|
||||
uint16_t LastModDate;
|
||||
uint16_t FirstCluster;
|
||||
uint32_t FileSize;
|
||||
}__attribute__((packed)) DIRECTORY, *PDIRECTORY;
|
267
kernel/filesystem/FAT/FAT.cpp
Normal file
267
kernel/filesystem/FAT/FAT.cpp
Normal file
@ -0,0 +1,267 @@
|
||||
//
|
||||
// Created by nigel on 21/02/23.
|
||||
//
|
||||
|
||||
#include "FAT.h"
|
||||
|
||||
void FAT::Read()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void FAT::Open()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void FAT::Write()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void ParseDateInteger(unsigned int date){
|
||||
printf("Date (hex) 0x%x\n", date);
|
||||
unsigned int year = (date >> 9 )+ 1980;
|
||||
unsigned int month = (date & 0xf0 ) >> 4;
|
||||
unsigned int day = date & 0xf ;
|
||||
printf("Date: (D,M,Y) %d, %d ,%d\n", day , month, year );
|
||||
|
||||
}
|
||||
|
||||
BiosParameterBlock* getBPB(MBR* mbr, bool DEBUG =false ){
|
||||
BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock));
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, (uint16_t*) bpb);
|
||||
|
||||
if(DEBUG)
|
||||
{
|
||||
printf("OEM ID: %s\n", bpb->OEM_id);
|
||||
printf("Bytes per sector: %d\n", bpb->BytesPerSector);
|
||||
printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster);
|
||||
printf("Reserved sectors: %d\n", bpb->ReservedSectors);
|
||||
printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables);
|
||||
printf("Number of Dir entries: %d\n", bpb->NumberOfDirectoryEntries);
|
||||
printf("Total Sectors in volume: %d\n", bpb->TotalSectorsInLogicalVolume);
|
||||
printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT);
|
||||
}
|
||||
|
||||
|
||||
|
||||
return bpb;
|
||||
}
|
||||
|
||||
uint16_t* ReadFAT (BiosParameterBlock& bpb , MBR& mbr, bool DEBUG = false ) {
|
||||
uint32_t FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors ;
|
||||
uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256);
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT );
|
||||
|
||||
// Show data in terminal
|
||||
if(DEBUG){
|
||||
for( unsigned int i =0 ; i < 256 ; i++) {
|
||||
printf("0x%x ", (unsigned short)FAT[i]);
|
||||
}
|
||||
kterm_put('\n');
|
||||
}
|
||||
|
||||
return FAT;
|
||||
}
|
||||
|
||||
void readFile(uint32_t DataRegion, DirectoryEntry* entry, uint16_t FATentry, BiosParameterBlock& bpb ){
|
||||
printf("Show contents");
|
||||
|
||||
printf("Start cluster of the file: 0x%x\n", entry->StartingCluster);
|
||||
|
||||
printf("IS it only 1 cluster? %s\n", FATentry == 0xFFFF ? "Yes" : "No");
|
||||
|
||||
uint32_t sector = DataRegion + ((entry->StartingCluster - 0x02) * bpb.SectorsPerCluster);
|
||||
|
||||
|
||||
uint16_t dataBlob[256];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, sector, dataBlob);
|
||||
for (unsigned short n: dataBlob) {
|
||||
kterm_put(n & 0x00ff);
|
||||
|
||||
kterm_put(n >> 8);
|
||||
}
|
||||
kterm_put('\n');
|
||||
}
|
||||
|
||||
void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){
|
||||
auto FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors;
|
||||
uint32_t RootDirectoryRegion = FATAddress + ( bpb.NumberOfFileAllocationTables * bpb.NumberOfSectorsPerFAT );
|
||||
uint32_t DataRegion = RootDirectoryRegion + ((bpb.NumberOfDirectoryEntries * 32) / bpb.BytesPerSector );
|
||||
uint16_t* FAT = ReadFAT(bpb, mbr);
|
||||
|
||||
uint16_t data2 [256];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 );
|
||||
auto* RootDirectory = (DirectoryEntry*) data2;
|
||||
|
||||
// List files in root
|
||||
for(int i= 0; i < sizeof (data2) / sizeof (DirectoryEntry); i++ ) {
|
||||
auto *entry = (DirectoryEntry * )((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry)));
|
||||
|
||||
if (entry->filename[0] == (uint8_t) 0x00)
|
||||
continue; // There are no more entries in this directory or the entry is free
|
||||
|
||||
if (entry->attribute & ATTRIBUTES::ATT_HIDDEN)
|
||||
continue;
|
||||
if(entry->attribute & ATTRIBUTES::ATTR_SYSTEM)
|
||||
continue;
|
||||
if(entry->attribute & ATTRIBUTES::ATTR_VOLUME_ID)
|
||||
continue;
|
||||
|
||||
// Print the filename;
|
||||
for (char n: entry->filename) {
|
||||
if (n == 0x20)
|
||||
break;
|
||||
kterm_put(n);
|
||||
}
|
||||
for (unsigned char n: entry->Extension) {
|
||||
kterm_put(n);
|
||||
}
|
||||
kterm_put('\n');
|
||||
|
||||
|
||||
printf("Attribute: %x \n", entry->attribute);
|
||||
printf("FileSize: %d Bytes\n", entry->FilesizeInBytes);
|
||||
|
||||
if (entry->FilesizeInBytes != 0x0 && (entry->attribute != 0x10)) {
|
||||
readFile(DataRegion,entry, FAT[i], bpb);
|
||||
}
|
||||
|
||||
}
|
||||
free(FAT);
|
||||
|
||||
}
|
||||
|
||||
FILE fsysFatDirectory (const char* DirectoryName){
|
||||
FILE file;
|
||||
unsigned char* buf;
|
||||
PDIRECTORY directory;
|
||||
|
||||
char DosFileName[11];
|
||||
ToDosFileName(DirectoryName, DosFileName, 11);
|
||||
DosFileName[11] =0;
|
||||
|
||||
for (int sector=0; sector <14 ; sector++){
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mountInfo.rootOffset + sector, (uint16_t*)buf);
|
||||
directory = (PDIRECTORY) buf;
|
||||
|
||||
for (int i =0; i < 16; i++){
|
||||
char name[11];
|
||||
memcpy(name, directory->Filename, 11);
|
||||
name[11]=0;
|
||||
|
||||
if(strncmp(DosFileName, name, 11) == 0){
|
||||
strcpy(file.name, DirectoryName);
|
||||
file.id = 0;
|
||||
file.currentCluster = directory->FirstCluster;
|
||||
file.eof = 0;
|
||||
file.filelength = directory->FileSize;
|
||||
|
||||
if(directory->Attrib == 0x10){
|
||||
file.flags = FS_DIRECTORY;
|
||||
} else {
|
||||
file.flags = FS_FILE;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
directory++;
|
||||
}
|
||||
}
|
||||
|
||||
// Can't find file
|
||||
file.flags = FS_INVALID;
|
||||
return file;
|
||||
|
||||
}
|
||||
|
||||
|
||||
void fsysFATRead(PFILE file, unsigned char* buffer, unsigned int length){
|
||||
if(file){
|
||||
unsigned int physSector = 32 + (file->currentCluster - 1);
|
||||
const unsigned int SECTOR_SIZE = 512;
|
||||
// read sector
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, physSector, (uint16_t*) buffer );
|
||||
|
||||
unsigned int FAT_Offset = file->currentCluster + (file->currentCluster /2);
|
||||
unsigned int FAT_Sector = 1 + (FAT_Offset / SECTOR_SIZE);
|
||||
unsigned int entryOffset =FAT_Offset % SECTOR_SIZE;
|
||||
|
||||
uint8_t FAT[SECTOR_SIZE*2];
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FAT_Sector,(uint16_t*) FAT); // Read 1st FAT sector
|
||||
|
||||
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FAT_Sector +1, (uint16_t*)FAT+SECTOR_SIZE);
|
||||
|
||||
// read entry for next cluster
|
||||
uint16_t nextCluster = *(uint16_t*) &FAT[entryOffset];
|
||||
|
||||
// test if entry is odd or even
|
||||
if(file->currentCluster & 0x0001){
|
||||
nextCluster>>= 4; // grab the high 12 bits
|
||||
}else{
|
||||
nextCluster &= 0x0FFF; // grab the low 12 bits
|
||||
}
|
||||
|
||||
// test for end of file
|
||||
if(nextCluster >= 0xff8){
|
||||
file->eof -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// test for file corruption
|
||||
if(nextCluster == 0){
|
||||
file->eof =1;
|
||||
return;
|
||||
}
|
||||
|
||||
// set next cluster
|
||||
file->currentCluster = nextCluster;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
FILE fsysFatOpenSubDir(FILE kFile, const char* filename){
|
||||
FILE file;
|
||||
|
||||
char DosFileName[11];
|
||||
ToDosFileName(filename, DosFileName, 11);
|
||||
DosFileName[11] = 0;
|
||||
|
||||
while(!kFile.eof){
|
||||
//read directory
|
||||
unsigned char buf[512];
|
||||
fsysFATRead(&file, buf, 512);
|
||||
|
||||
PDIRECTORY pkDir = (PDIRECTORY) buf;
|
||||
|
||||
for (unsigned int i = 0; i < 16; i++){
|
||||
// get current filename
|
||||
char name[11];
|
||||
memcpy(name, pkDir->Filename, 11);
|
||||
name[11] = 0;
|
||||
|
||||
if(strncmp(name, DosFileName, 11) == 0){
|
||||
strcpy(file.name, filename);
|
||||
file.id = 0;
|
||||
file.currentCluster = pkDir->FirstCluster;
|
||||
file.filelength = pkDir->FileSize;
|
||||
file.eof = 0;
|
||||
|
||||
// set file type;
|
||||
if(pkDir->Attrib == 0x10){
|
||||
file.flags = FS_DIRECTORY;
|
||||
} else{
|
||||
file.flags = FS_FILE;
|
||||
}
|
||||
|
||||
return file;
|
||||
}
|
||||
// go to next entry
|
||||
pkDir++;
|
||||
}
|
||||
}
|
||||
// unable to find file
|
||||
file.flags = FS_INVALID;
|
||||
return file;
|
||||
}
|
41
kernel/filesystem/FAT/FAT.h
Normal file
41
kernel/filesystem/FAT/FAT.h
Normal file
@ -0,0 +1,41 @@
|
||||
//
|
||||
// Created by nigel on 21/02/23.
|
||||
//
|
||||
#pragma once
|
||||
#include "ExtendBootRecord.h"
|
||||
#include "BiosParameterBlock.h"
|
||||
#include "DirectoryEntry.h"
|
||||
|
||||
|
||||
// Date Format
|
||||
// [0..4] Day
|
||||
// [5..8] Month
|
||||
// [9..15] Year
|
||||
|
||||
// Time Format
|
||||
// [0..4] Seconds
|
||||
// [5..10] Minute
|
||||
// [11..15] Hour
|
||||
|
||||
|
||||
class FAT {
|
||||
public:
|
||||
void Open();
|
||||
void Read();
|
||||
void Write();
|
||||
private:
|
||||
enum struct TYPE{
|
||||
FAT,
|
||||
FAT16,
|
||||
FAT32,
|
||||
VFAT
|
||||
};
|
||||
enum ATTRIBUTES {
|
||||
ATTR_READ_ONLY = 0x01,
|
||||
ATT_HIDDEN = 0x02,
|
||||
ATTR_SYSTEM = 0x04,
|
||||
ATTR_VOLUME_ID = 0x08,
|
||||
ATTR_DIRECTORY = 0x10,
|
||||
ATTR_ARCHIVE = 0x20
|
||||
};
|
||||
};
|
Reference in New Issue
Block a user