Primitie listing rootdir of FAT16 filesystem

This commit is contained in:
Nigel Barink 2023-02-19 14:17:47 +01:00
parent 37542b736f
commit dbb147e110
8 changed files with 86 additions and 74 deletions

View File

@ -1,14 +1,17 @@
# TODO list
## Basics
<input type="checkbox" checked/> Setup Cross-Compiler \
<input type="checkbox" checked/> Multiboot to kernel \
<input type="checkbox" checked/> Printing string to the screen \
<label>
<input type="checkbox" checked/>
</label> Setup Cross-Compiler
<input type="checkbox" checked> Multiboot to kernel \
<input type="checkbox" checked/> Printing string to the screen \
<input type="checkbox" checked/> Printing values/numbers to the screen \
<input type="checkbox" checked/> Basic Terminal \
<input type="checkbox" checked/> Extend Multiboot implementation \
<input type="checkbox" checked/> Output to serial port \
<input type="checkbox" checked/> Move to protected mode \
<input type="checkbox" checked/> Enabel CMOS clock \
<input type="checkbox" checked/> Enable CMOS clock \
<input type="checkbox" checked/> Time measurement (PIC &| PIT) \
<input type="checkbox" /> Detect CPU speed \
<input type="checkbox" checked/> Interrupt / exception system (API) \
@ -22,16 +25,13 @@
<input type="checkbox" checked/> Virtual memory management \
<input type="checkbox" checked/> The heap: allocating memory at runtime (malloc and free) is almost impossible to go without. \
<input type="checkbox" /> Enable SIMD Extensions (SSE)
## Other features I am thinking of:
<input type="checkbox" checked/> PCI support \
<input type="checkbox" /> ATA PIO Mode support \
<input type="checkbox" checked/> ATA PIO Mode support \
<input type="checkbox" /> USTAR Filesystem ( For its simplicity this is very likely the first filesystem the OS is going to support) \
<input type="checkbox" /> ACPI support ( Or some other basic way to support shutdown, reboot and possibly hibernation ) \
<input type="checkbox" /> ATAPI support \
<input type="checkbox" checked/> Memory Management (MMU)
<input type="checkbox" /> Hardware Management system
<input type="checkbox" /> Preemptive multi tasking \
<input type="checkbox" /> Processes \
<input type="checkbox" /> Threads
@ -44,7 +44,6 @@
<input type="checkbox" /> ACPI support \
<input type="checkbox" /> ATAPI support \
## Optional
<input type="checkbox" /> Basic Window server/client \
<input type="checkbox" /> EXT2 Filesystem
<input type="checkbox" /> USTAR Filesystem \

View File

@ -128,7 +128,7 @@ void ATA_DEVICE::Read(uint16_t DEVICE_CHANNEL, DEVICE_DRIVE drive, uint32_t LBA
return ;
}
printf("Read LBA: 0x%x\n", LBA);
//printf("Read LBA: 0x%x\n", LBA);
// Send 0xE0 for the "master" or 0xF0 for the "slave", ORed with the highest 4 bits of the LBA to port 0x1F6: outb(0x1F6, 0xE0 | (slavebit << 4) | ((LBA >> 24) & 0x0F))
outb(DEVICE_CHANNEL | 6 , ( 0xE0 | (LBA >>28) ) );
// Send a NULL byte to port 0x1F1, if you like (it is ignored and wastes lots of CPU time): outb(0x1F1, 0x00)
@ -156,7 +156,7 @@ void ATA_DEVICE::Read(uint16_t DEVICE_CHANNEL, DEVICE_DRIVE drive, uint32_t LBA
return;
}
printf("Status: %x\n", status);
//printf("Status: %x\n", status);
// Check if busy!
while((status & 0x80) == 0x80){
printf("Reading....\r");

View File

@ -10,7 +10,6 @@
* This first driver wil make use of IO ports.
* Doing so means reading or writing from disk is going
* to be very cpu intensive.
*
*/
enum DEVICE_DRIVE{
@ -18,9 +17,6 @@ enum DEVICE_DRIVE{
SLAVE = 0xB0
};
namespace ATA_DEVICE{
void Identify(uint16_t, DEVICE_DRIVE);
void Read (uint16_t, DEVICE_DRIVE, uint32_t, uint16_t*);

View File

@ -308,8 +308,8 @@ void irq_handler (registers regs) {
break;
default:
printf("Interrupt happened!");
printf("Received INT: 0x%x\n", regs.int_no);
//printf("Interrupt happened!");
//printf("Received INT: 0x%x\n", regs.int_no);
break;
}

View File

@ -1,7 +1,7 @@
#include "KernelHeap.h"
#include "VirtualMemoryManager.h"
extern "C" const uint32_t kernel_end;
// Size of heap meta data is 5 bytes
// Size of heap metadata is 5 bytes
struct heap_block{
uint8_t Used;
uint32_t Size;
@ -12,7 +12,7 @@ heap_block* start ;
void* malloc(size_t size)
{
printf("Received request for %d bytes of memory\n", size);
//printf("Received request for %d bytes of memory\n", size);
heap_block* current = start;
// look for a free block
@ -21,14 +21,14 @@ void* malloc(size_t size)
if(current->Size >= size && current->Used == false )
{
// We found a spot
printf("Block found!\n");
// printf("Block found!\n");
// Set the spot to in-use
current->Used = true;
// split the block
printf("Split block.\n");
//printf("Split block.\n");
uint32_t oldSize = current->Size;
current->Size = size;
@ -57,7 +57,7 @@ void free(void* addr)
{
// clear the free boolean that corresponds to this adddress
// This should be fairly simple
heap_block* allocatedBlock = (heap_block*)(addr - sizeof(heap_block));
heap_block* allocatedBlock = (heap_block*)((uint32_t)addr - sizeof(heap_block));
allocatedBlock->Used = false;
}

View File

@ -70,9 +70,17 @@ extern "C" void startSuperVisorTerminal()
kterm_init();
printf("|=== BarinkOS ===|\n");
}
if(strncmp("LIST", command, characterCount) == 0)
if(strncmp("LIST", command, 4) == 0)
{
// slice off the command part
char args[characterCount - 4];
for(int i = 5 ; i < characterCount; i++) {
args[i] = command[i];
}
printf("=============== DIRECTORY LISTING =================\n");
printf("Path to show %s\n", args);
}
if(strncmp("DEVICES", command, characterCount) == 0){

View File

@ -80,7 +80,7 @@ bool driveAvailable(){
return true;
}
MBR* getPartitions(){
MBR* getPartitions(bool DEBUG = false){
const int C = 0;
const int H = 0;
const int HPC = 16;
@ -92,67 +92,91 @@ MBR* getPartitions(){
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr);
printf("BootSector: 0x%x\n", mbr->ValidBootsector );
for( int i = 0 ; i < 4 ; i ++){
PartitionTableEntry PT = mbr->TableEntries[i];
if(DEBUG){
printf("BootSector: 0x%x\n", mbr->ValidBootsector );
for( int i = 0 ; i < 4 ; i ++){
PartitionTableEntry PT = mbr->TableEntries[i];
printf("Partition %d [ %d sectors, PartitionType: 0x%x, 0x%x, \nLBA Start: 0x%x ]\n" ,
i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start );
}
printf("Partition %d [ %d sectors, PartitionType: %x, 0x%x, \nLBA Start: 0x%x ]\n" ,
i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start );
}
return mbr;
}
BiosParameterBlock* getBPB(MBR* mbr){
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);
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);
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) {
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
/*
for(unsigned short i : FAT) {
printf("%x ", i);
if(DEBUG){
for( unsigned int i =0 ; i < 256 ; i++) {
printf("0x%x ", (unsigned short)FAT[i]);
}
kterm_put('\n');
}
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 < bpb.NumberOfDirectoryEntries ; i++ ) {
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)
break; // There are no more entries in this directory or the entry is free
continue; // There are no more entries in this directory or the entry is free
if ((entry->attribute & 0x01) == 0x01 || (entry->attribute & 0x20) == 0x20)
continue; // Skip listing if hidden or Achieve flag is set
@ -163,40 +187,21 @@ void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){
break;
kterm_put(n);
}
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 & 0x8) == 0x0) {
printf("Show contents");
printf("Start cluster of the file: 0x%x\n", entry->StartingCluster);
printf("IS it only 1 cluster? %s\n", FAT[i] == 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');
if (entry->FilesizeInBytes != 0x0 && (entry->attribute == 0x08)) {
readFile(DataRegion,entry, FAT[i], bpb);
}
printf("======================\n");
}
free(FAT);
}
@ -341,6 +346,9 @@ void FileSystem::initialize()
MBR* mbr = getPartitions();
BiosParameterBlock* bootsector = getBPB(mbr);
listFilesInRoot(*mbr, *bootsector);
free(mbr);
free(bootsector);
/*
mountInfo.numSectors = bootsector->NumberOfSectorsPerFAT;
mountInfo.fatOffset = 1;
@ -348,7 +356,8 @@ void FileSystem::initialize()
mountInfo.fatEntrySize = 8;
mountInfo.numRootEntries = bootsector->NumberOfDirectoryEntries;
mountInfo.rootOffset = (bootsector->NumberOfFileAllocationTables * bootsector->NumberOfSectorsPerFAT) + 1;
mountInfo.rootSize = (bootsector->NumberOfDirectoryEntries * 32) / bootsector->BytesPerSector;*/
mountInfo.rootSize = (bootsector->NumberOfDirectoryEntries * 32) / bootsector->BytesPerSector;
*/
}

View File

@ -13,9 +13,9 @@ to do on a more in depth level.
[x] Setup a proper HEAP \
[ ] Setup a proper Stack \
[x] Setup KMalloc and KFree \
[ ] Merge Functioning Feature branches into sandboxKernelDev \
[x] Merge Functioning Feature branches into sandboxKernelDev \
[ ] Remove merged feature branches \
[ ] Merge sandboxKernelDev with dev \
[ ] Remove sandboxKernelDev branch \
[x] Remove sandboxKernelDev branch \
[ ] Implement proper virtual filesystem