Primitie listing rootdir of FAT16 filesystem

dev
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 # TODO list
## Basics ## Basics
<input type="checkbox" checked/> Setup Cross-Compiler \
<input type="checkbox" checked/> Multiboot to kernel \ <label>
<input type="checkbox" checked/> Printing string to the screen \ <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/> Printing values/numbers to the screen \
<input type="checkbox" checked/> Basic Terminal \ <input type="checkbox" checked/> Basic Terminal \
<input type="checkbox" checked/> Extend Multiboot implementation \ <input type="checkbox" checked/> Extend Multiboot implementation \
<input type="checkbox" checked/> Output to serial port \ <input type="checkbox" checked/> Output to serial port \
<input type="checkbox" checked/> Move to protected mode \ <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" checked/> Time measurement (PIC &| PIT) \
<input type="checkbox" /> Detect CPU speed \ <input type="checkbox" /> Detect CPU speed \
<input type="checkbox" checked/> Interrupt / exception system (API) \ <input type="checkbox" checked/> Interrupt / exception system (API) \
@ -22,16 +25,13 @@
<input type="checkbox" checked/> Virtual memory management \ <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" checked/> The heap: allocating memory at runtime (malloc and free) is almost impossible to go without. \
<input type="checkbox" /> Enable SIMD Extensions (SSE) <input type="checkbox" /> Enable SIMD Extensions (SSE)
## Other features I am thinking of:
<input type="checkbox" checked/> PCI support \ <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" /> 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" /> ACPI support ( Or some other basic way to support shutdown, reboot and possibly hibernation ) \
<input type="checkbox" /> ATAPI support \ <input type="checkbox" /> ATAPI support \
<input type="checkbox" checked/> Memory Management (MMU) <input type="checkbox" checked/> Memory Management (MMU)
<input type="checkbox" /> Hardware Management system <input type="checkbox" /> Hardware Management system
<input type="checkbox" /> Preemptive multi tasking \ <input type="checkbox" /> Preemptive multi tasking \
<input type="checkbox" /> Processes \ <input type="checkbox" /> Processes \
<input type="checkbox" /> Threads <input type="checkbox" /> Threads
@ -44,7 +44,6 @@
<input type="checkbox" /> ACPI support \ <input type="checkbox" /> ACPI support \
<input type="checkbox" /> ATAPI support \ <input type="checkbox" /> ATAPI support \
## Optional
<input type="checkbox" /> Basic Window server/client \ <input type="checkbox" /> Basic Window server/client \
<input type="checkbox" /> EXT2 Filesystem <input type="checkbox" /> EXT2 Filesystem
<input type="checkbox" /> USTAR 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 ; 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)) // 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) ) ); 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) // 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; return;
} }
printf("Status: %x\n", status); //printf("Status: %x\n", status);
// Check if busy! // Check if busy!
while((status & 0x80) == 0x80){ while((status & 0x80) == 0x80){
printf("Reading....\r"); printf("Reading....\r");

View File

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

View File

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

View File

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

View File

@ -70,9 +70,17 @@ extern "C" void startSuperVisorTerminal()
kterm_init(); kterm_init();
printf("|=== BarinkOS ===|\n"); 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("=============== DIRECTORY LISTING =================\n");
printf("Path to show %s\n", args);
} }
if(strncmp("DEVICES", command, characterCount) == 0){ if(strncmp("DEVICES", command, characterCount) == 0){

View File

@ -80,7 +80,7 @@ bool driveAvailable(){
return true; return true;
} }
MBR* getPartitions(){ MBR* getPartitions(bool DEBUG = false){
const int C = 0; const int C = 0;
const int H = 0; const int H = 0;
const int HPC = 16; const int HPC = 16;
@ -92,67 +92,91 @@ MBR* getPartitions(){
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr); ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, (uint16_t*)mbr);
printf("BootSector: 0x%x\n", mbr->ValidBootsector ); if(DEBUG){
for( int i = 0 ; i < 4 ; i ++){ printf("BootSector: 0x%x\n", mbr->ValidBootsector );
PartitionTableEntry PT = mbr->TableEntries[i]; 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; return mbr;
} }
BiosParameterBlock* getBPB(MBR* mbr){ BiosParameterBlock* getBPB(MBR* mbr, bool DEBUG =false ){
BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock)); BiosParameterBlock* bpb = (BiosParameterBlock*) malloc(sizeof(BiosParameterBlock));
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, (uint16_t*) bpb); 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); if(DEBUG)
printf("Bytes per sector: %d\n", bpb->BytesPerSector); {
printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster); printf("OEM ID: %s\n", bpb->OEM_id);
printf("Reserved sectors: %d\n", bpb->ReservedSectors); printf("Bytes per sector: %d\n", bpb->BytesPerSector);
printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables); printf("Sectors per cluster: %d\n", bpb->SectorsPerCluster);
printf("Number of Dir entries: %d\n", bpb->NumberOfDirectoryEntries); printf("Reserved sectors: %d\n", bpb->ReservedSectors);
printf("Total Sectors in volume: %d\n", bpb->TotalSectorsInLogicalVolume); printf("Number of FAT: %d\n", bpb->NumberOfFileAllocationTables);
printf("Sectors per FAT: %d\n", bpb->NumberOfSectorsPerFAT); 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; 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 ; uint32_t FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors ;
uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256); uint16_t* FAT = (uint16_t*)malloc(sizeof (uint16_t) * 256);
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT ); ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, FATAddress, FAT );
// Show data in terminal // Show data in terminal
/* if(DEBUG){
for(unsigned short i : FAT) { for( unsigned int i =0 ; i < 256 ; i++) {
printf("%x ", i); printf("0x%x ", (unsigned short)FAT[i]);
}
kterm_put('\n');
} }
kterm_put('\n');*/
return FAT; 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 ){ void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){
auto FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors; auto FATAddress = mbr.TableEntries[0].LBA_partition_start + bpb.ReservedSectors;
uint32_t RootDirectoryRegion = FATAddress + ( bpb.NumberOfFileAllocationTables * bpb.NumberOfSectorsPerFAT ); uint32_t RootDirectoryRegion = FATAddress + ( bpb.NumberOfFileAllocationTables * bpb.NumberOfSectorsPerFAT );
uint32_t DataRegion = RootDirectoryRegion + ((bpb.NumberOfDirectoryEntries * 32) / bpb.BytesPerSector ); uint32_t DataRegion = RootDirectoryRegion + ((bpb.NumberOfDirectoryEntries * 32) / bpb.BytesPerSector );
uint16_t* FAT = ReadFAT(bpb, mbr); uint16_t* FAT = ReadFAT(bpb, mbr);
uint16_t data2 [256]; uint16_t data2 [256];
ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 ); ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, RootDirectoryRegion, data2 );
auto* RootDirectory = (DirectoryEntry*) data2; auto* RootDirectory = (DirectoryEntry*) data2;
// List files in root // 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))); auto *entry = (DirectoryEntry * )((uint32_t) RootDirectory + (i * sizeof(DirectoryEntry)));
if (entry->filename[0] == (uint8_t) 0x00) 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) if ((entry->attribute & 0x01) == 0x01 || (entry->attribute & 0x20) == 0x20)
continue; // Skip listing if hidden or Achieve flag is set continue; // Skip listing if hidden or Achieve flag is set
@ -163,40 +187,21 @@ void listFilesInRoot(MBR& mbr, BiosParameterBlock& bpb ){
break; break;
kterm_put(n); kterm_put(n);
} }
kterm_put('\n');
for (unsigned char n: entry->Extension) { for (unsigned char n: entry->Extension) {
kterm_put(n); kterm_put(n);
} }
kterm_put('\n'); kterm_put('\n');
printf("Attribute: %x \n", entry->attribute); printf("Attribute: %x \n", entry->attribute);
printf("FileSize: %d Bytes\n", entry->FilesizeInBytes); printf("FileSize: %d Bytes\n", entry->FilesizeInBytes);
if (entry->FilesizeInBytes != 0x0 || (entry->attribute & 0x8) == 0x0) { if (entry->FilesizeInBytes != 0x0 && (entry->attribute == 0x08)) {
printf("Show contents"); readFile(DataRegion,entry, FAT[i], bpb);
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');
} }
printf("======================\n");
} }
free(FAT);
} }
@ -341,6 +346,9 @@ void FileSystem::initialize()
MBR* mbr = getPartitions(); MBR* mbr = getPartitions();
BiosParameterBlock* bootsector = getBPB(mbr); BiosParameterBlock* bootsector = getBPB(mbr);
listFilesInRoot(*mbr, *bootsector); listFilesInRoot(*mbr, *bootsector);
free(mbr);
free(bootsector);
/* /*
mountInfo.numSectors = bootsector->NumberOfSectorsPerFAT; mountInfo.numSectors = bootsector->NumberOfSectorsPerFAT;
mountInfo.fatOffset = 1; mountInfo.fatOffset = 1;
@ -348,7 +356,8 @@ void FileSystem::initialize()
mountInfo.fatEntrySize = 8; mountInfo.fatEntrySize = 8;
mountInfo.numRootEntries = bootsector->NumberOfDirectoryEntries; mountInfo.numRootEntries = bootsector->NumberOfDirectoryEntries;
mountInfo.rootOffset = (bootsector->NumberOfFileAllocationTables * bootsector->NumberOfSectorsPerFAT) + 1; 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 \ [x] Setup a proper HEAP \
[ ] Setup a proper Stack \ [ ] Setup a proper Stack \
[x] Setup KMalloc and KFree \ [x] Setup KMalloc and KFree \
[ ] Merge Functioning Feature branches into sandboxKernelDev \ [x] Merge Functioning Feature branches into sandboxKernelDev \
[ ] Remove merged feature branches \ [ ] Remove merged feature branches \
[ ] Merge sandboxKernelDev with dev \ [ ] Merge sandboxKernelDev with dev \
[ ] Remove sandboxKernelDev branch \ [x] Remove sandboxKernelDev branch \
[ ] Implement proper virtual filesystem [ ] Implement proper virtual filesystem