From fb2a19e11db9d183f233af3da0cfef4950a0df5c Mon Sep 17 00:00:00 2001 From: Nigel Date: Fri, 24 Dec 2021 21:31:10 +0100 Subject: [PATCH] FAT16 structures read from disk using ATA. The proper reading of folders and files is not yet implemented. Although it is close. --- .../filesytems/FAT32/BiosParameterBlock.h | 21 +++++ .../filesytems/FAT32/ExtendBootRecord.h | 32 ++++++++ src/kernel/kernel.cpp | 76 +++++++++++++++---- src/kernel/kernel.h | 8 +- 4 files changed, 119 insertions(+), 18 deletions(-) create mode 100644 src/kernel/filesytems/FAT32/BiosParameterBlock.h create mode 100644 src/kernel/filesytems/FAT32/ExtendBootRecord.h diff --git a/src/kernel/filesytems/FAT32/BiosParameterBlock.h b/src/kernel/filesytems/FAT32/BiosParameterBlock.h new file mode 100644 index 0000000..3bf2de3 --- /dev/null +++ b/src/kernel/filesytems/FAT32/BiosParameterBlock.h @@ -0,0 +1,21 @@ +#pragma once +#include +#include "./ExtendBootRecord.h" + +struct BiosParameterBlock { + uint8_t BootLoaderCodeSection [3]; + uint8_t OEM_id [8]; + uint16_t BytesPerSector ; // I suspect would be 512 + uint8_t SectorsPerCluster ; + uint16_t ReservedSectors; + uint8_t NumberOfFileAllocationTables; // Probably equals 2 + uint16_t NumberOfDirectoryEntries; // Root directory must contain entire sectors + uint16_t TotalSectorsInLogicalVolume ; // 0 means >65535 sectors in volume , actual count can be found in LargeSectorCount + uint8_t MediaDescriptor ; // Indication the media descriptor type + uint16_t NumberOfSectorsPerFAT;// only in FAT12 / FAT 16 + uint16_t NumberOfSectorsPerTrack; + uint16_t NumberOfHeadsOnMedia; + uint32_t NumberOfHiddenSectors; + uint32_t LargeSectorCount; + ExtendedBootRecord_FAT16 ebpb; +}__attribute__((packed)); \ No newline at end of file diff --git a/src/kernel/filesytems/FAT32/ExtendBootRecord.h b/src/kernel/filesytems/FAT32/ExtendBootRecord.h new file mode 100644 index 0000000..38a40b7 --- /dev/null +++ b/src/kernel/filesytems/FAT32/ExtendBootRecord.h @@ -0,0 +1,32 @@ +#pragma once +#include + +struct ExtendedBootRecord_FAT16{ + uint8_t DriveNumber; + uint8_t Reserved; + uint8_t Signature; + const uint32_t VOLUME_ID_SERIAL_NUMBER; + uint8_t volume_label [11]; + uint8_t Identifier_string [8]; + uint8_t bootCode [448]; + uint16_t partitionSignature; +}__attribute__((packed)); + +struct ExtendedBootRecord_FAT32{ + uint32_t SectorsPerFAT; + uint16_t Flags; + const uint16_t FAT_VERSION_NUMBER; + uint32_t rootDirectory_clusterNumber;// Often set to 2; + uint16_t FSInfo_SectorNumber; + uint16_t backup_bpb_sectorNumber; + uint8_t Reserved [12]; + uint8_t DriveNumber; + uint8_t Reserved2; + uint8_t Signature; // must be 0x28 or 0x29 + uint32_t VOLUME_ID_SERIAL; + uint8_t volume_label[11]; + uint8_t SystemIdentifierString [8]; // ALWAYS "FAT32 " but spec says do not trust + uint8_t BootCode [420]; // NICE + uint16_t PartitionSignature; // 0xAA55 + +}__attribute__((packed)); \ No newline at end of file diff --git a/src/kernel/kernel.cpp b/src/kernel/kernel.cpp index 5e30044..407f637 100644 --- a/src/kernel/kernel.cpp +++ b/src/kernel/kernel.cpp @@ -44,40 +44,84 @@ extern "C" void kernel_main (void); init_serial(); print_serial("Serial port initialized!"); - + RSDPTR* rsd = FindRSD(); + RSDT* rsd_table = getRSDT(rsd); + // Enumerate the PCI bus PCI_Enumerate(); - - - - TestIDEController(); + TestIDEController(); int devNumber = 0 ; for ( auto device : ide_devices){ if (!device.Reserved) continue; - - printf("Device %d\n" , devNumber); printf (" Device on Channel: (0x%x) %s\n" ,device.Channel, device.Channel == 0 ? "Primary" : "Secondary"); printf (" Device drive:(0x%x) %s\n" , device.Drive, device.Drive? "Slave" : "Master"); printf (" Device Type:(0x%x) %s\n" , device.Type, device.Type ? "ATAPI" : "ATA"); devNumber ++; - - - - - } - // ATAPI_DEVICE::isPacketDevice(); - - + enum BUS_PORT { + Primary= 0x1f0, + Secondary = 0x170 + }; - ATAPI_DEVICE::Identify(ATA_SECONDARY, DEVICE_DRIVE::MASTER); + + ATA_DEVICE::Identify((uint16_t) BUS_PORT::Primary, DEVICE_DRIVE::MASTER); + + const int C = 0; + const int H = 0; + const int HPC = 16; + const int SPT = 63; + + int S = 1; + uint32_t LBA = (C*HPC+H) * SPT + (S-1); + printf("LBA: %d\n" , LBA); + uint16_t buffer [256]; + + + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, LBA, buffer); + + MBR* mbr = (MBR*) buffer; + + 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: %x, 0x%x, \nLBA Start: 0x%x ]\n" , + i, PT.Number_sectors_inPartition, PT.PartitionType, mbr->uniqueID, PT.LBA_partition_start ); + } + + // Find the super block + uint16_t superBlock[256]; + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER, mbr->TableEntries[0].LBA_partition_start, superBlock); + + BiosParameterBlock* bpb = (BiosParameterBlock*) superBlock; + + + printf("\nBPB: Bytes per Sector %d\n", bpb->BytesPerSector ); + 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); + + uint32_t PartitionAddress = mbr->TableEntries[0].LBA_partition_start *512 ; + uint32_t RootDirAddress = PartitionAddress + ((bpb->ReservedSectors + bpb->NumberOfSectorsPerFAT * bpb->NumberOfFileAllocationTables ) * bpb->BytesPerSector); + uint32_t RootDirLBA =RootDirAddress/512; + + + + uint16_t RootDir [16]; + ATA_DEVICE::Read(BUS_PORT::Primary, DEVICE_DRIVE::MASTER,RootDirLBA, (uint16_t*) RootDir ); + diff --git a/src/kernel/kernel.h b/src/kernel/kernel.h index 9e84d19..5542a5c 100644 --- a/src/kernel/kernel.h +++ b/src/kernel/kernel.h @@ -20,8 +20,12 @@ extern "C"{ #include "serial.h" #include "pci.h" #include "ide/ide.h" -#include "drivers/atapi/atapiDevice.h" - +//#include "drivers/atapi/atapiDevice.h" +#include "drivers/ata/ataDevice.h" +#include "./PartitionTable/MBR/MasterBootRecord.h" +#include "./filesytems/FAT32/BiosParameterBlock.h" +#include "./filesytems/FAT32/ExtendBootRecord.h" +#include "./drivers/rsdp/rsdp.h" #define CHECK_FLAG(flags, bit) ((flags) & (1 <<(bit)))