Skip to content

Instantly share code, notes, and snippets.

@iProgramMC
Created April 24, 2019 16:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iProgramMC/8d92f901213a26722af5db52e7b76b31 to your computer and use it in GitHub Desktop.
Save iProgramMC/8d92f901213a26722af5db52e7b76b31 to your computer and use it in GitHub Desktop.
extern void AtaLbaRead(uint32_t lba, void* dest, uint8_t sectorsToRead);
extern void AtaLbaWrite(uint32_t lba, void* dest, uint8_t sectorsToRead);
/*
NanoShell File System
*/
#define NSFSBootSector struct nsfs_boot_sector
NSFSBootSector
{
uint8_t jmpInstruction;
uint16_t jmpAddress;
char OemName[8];
uint16_t BytesPerSector;
uint8_t SectorsPerCluster;
uint16_t ReservedSectorCount;
uint8_t Reserved_00;
uint16_t RootEntryCount;
uint16_t TotalSectors16;
uint8_t Media;
uint16_t Reserved_01;
uint16_t SectorsPerTrack;
uint16_t HeadCount;
uint32_t HiddenSectors;
uint32_t TotalSectors32;
// At offset 36, the BPB/boot sector for FAT12 and FAT16 differs from the
// BPB/Boot Sector for FAT32. Thus, we needed separate structs.
uint8_t DriveNumber; // Unused since BIOS interrupts are not available
uint8_t Reserved_02; // Used by Windows NT, ALWAYS set to 0
uint8_t ExtBootSig;
uint32_t VolumeID; // Volume Serial Number
char VolumeLabel[11]; // Volume label
char FileSystemType[8]; // File system type string (unsure)
uint8_t RootDirFileCount;
}__attribute__((packed));
#define NSFSRootEntry struct nsfs_root_entry
NSFSRootEntry
{
char FileName[8];
char Extension[3];
#define ATTRIB_READONLY 0x01
#define ATTRIB_HIDDEN 0x02
#define ATTRIB_SYSTEM 0x04
#define ATTRIB_SUBDIR 0x10
#define ATTRIB_ARCHIVE 0x20
#define ATTRIB_DEVICE 0x40
uint8_t Attributes;
uint8_t Reserved[10];
uint16_t ModifyTime;
uint16_t ModifyDate;
uint16_t StartingSector;
uint32_t FileSize;
}__attribute__((packed));
NSFSBootSector nsfsBootSector;
NSFSRootEntry nsfsRootEntries[256];
#define NSFSFile struct nsfs_file
NSFSFile
{
uint32_t length;
uint8_t* contents;
};
void InitFilesystem()
{
AtaLbaRead(0, &nsfsBootSector, 1);
AtaLbaRead(1, &nsfsRootEntries, 16);
}
char* GetExtension(char* filename)
{
for(int i = 0; i < 12; i++)
{
if(filename[i] == '.')
{
filename[i] = 0;
return filename + i + 1;
break;
}
}
return filename;
}
char* FileNameOpen = " ";
char* ExtensionOpen = " ";
#define DiskbufferSize 262144
uint8_t DiskBuffer[DiskbufferSize] = {0x0, };
void FileOpen(char* filename, uint8_t* dest)
{
char* extension = GetExtension(filename);
for(int i = 0; i < 12; i++)
{
if(filename[i] == '.')
{
filename[i] = 0;
break;
}
}
memset(FileNameOpen, 0x20, 8);
memset(ExtensionOpen, 0x20, 3);
memcpy(FileNameOpen, filename, strlen(filename));
memcpy(ExtensionOpen, extension, strlen(extension));
int index = -1;
for(int i = 0; i < 256; i++)
{
if (memcmp(FileNameOpen, nsfsRootEntries[i].FileName, 8) == 0 &&
memcmp(ExtensionOpen, nsfsRootEntries[i].Extension, 3) == 0)
{
// found the file
index = i;
break;
}
}
printf("Found file at: %x\r\n", index);
if(index == -1) return;
NSFSRootEntry* entry = &nsfsRootEntries[index];
int sectorSize = entry->FileSize / 512 + 1;
if(sectorSize < 512)
{
AtaLbaRead(entry->StartingSector, &DiskBuffer, sectorSize);
fast_memcpy(dest, &DiskBuffer, entry->FileSize);
}
}
void DumpFs()
{
for(int i = 0; i < 16; i++)
{
printf("File ");
TerminalWrite(nsfsRootEntries[i].FileName, 8);
putch('.');
TerminalWrite(nsfsRootEntries[i].Extension, 3);
printf(", size %d\n", nsfsRootEntries[i].FileSize);
}
}
void printf(const char* format, ...);
void TerminalWrite(char* data, size_t count);
void putch(char c);
int main()
{
InitFilesystem();
DumpFs();
char* t = malloc(512);
FileOpen("file1.txt", t);
printf(t);
while(true){}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment