From 5f39ce826ff376430a81e439a391898ca607e595 Mon Sep 17 00:00:00 2001
From: aaron <aaron@6dbc8c32-bb84-406f-8558-d1cf31a0ab0c>
Date: Wed, 11 Apr 2018 19:34:08 +0000
Subject: [PATCH] git-svn-id: https://spexeah.com:8443/svn/Asuro@498
 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

---
 src/driver/storage/IDE.pas                  |  10 +-
 src/driver/storage/{FAT32.pas => fat32.pas} | 117 ++++++++++++--------
 src/driver/storage/storagemanagement.pas    |  10 +-
 3 files changed, 80 insertions(+), 57 deletions(-)
 rename src/driver/storage/{FAT32.pas => fat32.pas} (57%)

diff --git a/src/driver/storage/IDE.pas b/src/driver/storage/IDE.pas
index c5b10f44..17e06ba9 100644
--- a/src/driver/storage/IDE.pas
+++ b/src/driver/storage/IDE.pas
@@ -136,7 +136,7 @@ var
     secotrs : uint32;
     cpacityMB : uint32;
     buffer : puint32;
-    i : uint8;
+    i : uint32;
     d : uint8;
 begin
 
@@ -153,26 +153,26 @@ begin
         console.writeint(cpacityMB);
         console.writestringln('MB');
 
-        buffer := puint32(kalloc(1024));
+        buffer := puint32(kalloc(1024 * 2000));
         //buffer^:= secotrs;
 
         for i:=0 to 20 do begin
             puint32(buffer + (i div 2))^:= $10010110;
         end;
 
-        writePIO28(d, 2, 1, buffer);
+        writePIO28(d, 2, 2, buffer);
         //buffer^:= $FFFF;
         for i:=0 to 20 do begin
             puint32(buffer + (i div 2))^:= $FFFFFFFF;
         end;
         
-        readPIO28(d, 2, 1, buffer);
+        readPIO28(d, 2, 2, buffer);
 
         for i:=0 to 20 do begin
             if puint32(buffer + (i div 2))^ <> $10010110 then begin
                 console.writestringln('Tests failed!');
                 exit;
-        end;
+            end;
         end;
 
     // if uint32(buffer^) = secotrs then begin
diff --git a/src/driver/storage/FAT32.pas b/src/driver/storage/fat32.pas
similarity index 57%
rename from src/driver/storage/FAT32.pas
rename to src/driver/storage/fat32.pas
index 04049d41..8ed7b5f4 100644
--- a/src/driver/storage/FAT32.pas
+++ b/src/driver/storage/fat32.pas
@@ -8,7 +8,7 @@
   * Contributors: 
   ************************************************ }
 
-unit fat32;
+unit FAT32;
 
 interface
 
@@ -17,24 +17,21 @@ uses
 
 type 
 
-    TBootRecord = bitpacked record 
+    TBootRecord = bitpacked record
         jmp2boot        : ubit24;
-        OEMName         : uint64;
+        OEMName         : array[0..7] of char;
         sectorSize      : uint16;
         spc             : uint8;
         rsvSectors      : uint16;
         numFats         : uint8;
         numDirEnt       : uint16;
-        numSecotrs      : uint16;
+        numSectors      : uint16;
         mediaDescp      : uint8;
         sectorsPerFat   : uint16;
         sectorsPerTrack : uint16;
         heads           : uint16;
-        hiddenSecotrs   : uint32;
+        hiddenSectors   : uint32;
         manySectors     : uint32;
-    end;
-    
-    TExtendedBootRecord = bitpacked record
         FATSize       : uint32;
         flags         : uint16;
         signature     : uint8;
@@ -45,12 +42,15 @@ type
         reserved0     : array[0..11] of uint8;
         driveNumber   : uint8;
         reserved1     : uint8;
-        signature     : uint8 = $28;
+        bsignature     : uint8;// = $28;
         volumeID      : uint32;
         volumeLabel   : array[0..10] of uint8;
-        identString   : pchar = 'FAT32 ';
+        identString   : array[0..7] of char;// = 'FAT32 ';
     end;
 
+    byteArray8 = array[0..7] of char;
+    byteArray12 = array[0..11] of uint8;
+
     TDirectory = bitpacked record
         fileName      : uint64;
         fileExtension : ubit24;
@@ -75,10 +75,11 @@ type
     TFatVolumeInfo = record
         sectorsPerCluster : uint8; // must be power of 2 and mult by sectorsize to max 32k
     end;
+    PFatVolumeInfo = ^TFatVolumeInfo;
 
 procedure init;
-procedure create_volume(disk : PStorage_Device; sectors : uint32; start : uint32);
-function detect_volumes(disk : PStorage_Device) : APStorage_volume;
+procedure create_volume(disk : PStorage_Device; sectors : uint32; start : uint32; config : puint32);
+procedure detect_volumes(disk : PStorage_Device; volumes : puint32);
 
 implementation
 
@@ -87,18 +88,6 @@ begin
     console.outputln('DUMMY DRIVER', 'LOADED.')
 end;
 
-procedure init;
-var
-    filesystem : TFilesystem;
-begin
-    filesystem.sName:= 'FAT32'; 
-    filesystem.writecallback:= write;
-    filesystem.readcallback:= read;
-    filesystem.createcallback:= create_volume;
-    filesystem.detectcallback:= detect_volumes; 
-    storagemanagement.register_filesystem(filesystem);
-end;
-
 procedure read(volume : PStorage_volume; directory : pchar; byteCount : uint32; buffer : puint32);
 begin
 
@@ -109,24 +98,54 @@ begin
 
 end;
 
-procedure create_volume(disk : PStorage_Device; sectors : uint32; start : uint32, config : puint32);
+procedure init;
+var
+    filesystem : TFilesystem;
+begin
+    filesystem.sName:= 'FAT32'; 
+    filesystem.writecallback:= @write;
+    filesystem.readcallback:= @read;
+    filesystem.createcallback:= @create_volume;
+    filesystem.detectcallback:= @detect_volumes; 
+    storagemanagement.register_filesystem(filesystem);
+end;
+
+procedure create_volume(disk : PStorage_Device; sectors : uint32; start : uint32; config : puint32);
 var
     i : uint8;
     bootRecord : TBootRecord;
-    exBootRecord : TExtendedBootRecord;
+    buffer : puint32;
+    asuroArray : byteArray8;// = byteArray8(['A','S','U','R','O',' ','V','1']);
+    fatArray : byteArray8;// = byteArray8(['F','A','T','3','2',' ',' ',' ']);
 begin
-    bootrecord.jmp2boot:= $00 // TODO what ahppens here???
-    bootRecord.OEMName:= 'ASURO V1';
+
+    asuroArray[0] := 'A';
+    asuroArray[1] := 's';
+    asuroArray[2] := 'u';
+    asuroArray[3] := 'r';
+    asuroArray[4] := 'o';
+    asuroArray[5] := ' ';
+    asuroArray[6] := 'V';
+    asuroArray[7] := '1';
+
+    fatArray[0] := 'F';
+    fatArray[1] := 'A';
+    fatArray[2] := 'T';
+    fatArray[3] := '3';
+    fatArray[4] := '2';
+    fatArray[5] := ' ';
+    fatArray[6] := ' ';
+    fatArray[7] := ' ';
+
+
+    bootrecord.jmp2boot:= $00; // TODO what ahppens here???
+    bootRecord.OEMName:= asuroArray;
     bootrecord.sectorsize:= disk^.sectorSize;
-    bootrecord.spc:= TFatVolumeInfo(config).sectorsPerCluster;
+    bootrecord.spc:= PFatVolumeInfo(config)^.sectorsPerCluster;
     bootrecord.rsvSectors:= 32; //Is this acceptable?
     bootrecord.numFats:= 2;
     bootrecord.numDirEnt:= 0;
-    
-    if sectors < $10000 then begin
-        bootrecord.numSectors:= sectors; //maybe always needs to be 0?
-    end else bootRecord.numSectors:= 0;
-
+    bootRecord.numSectors:= 0;
     bootrecord.mediaDescp:= $F8;
     bootrecord.sectorsPerFat:= 0;
     bootRecord.sectorsPerTrack:= 0;
@@ -134,25 +153,29 @@ begin
     bootRecord.hiddenSectors:= start;
     bootRecord.manySectors:= sectors;
 
-    exBootRecord.FATSize:= ((sectors DIV TFatVolumeInfo(config).sectorsPerCluster) * 16 DIV disk^.sectorSize);
-    exBootRecord.flags:= 1 shl 7;
-    exBootRecord.FATVersion:= 0;
-    exBootRecord.rootCluster:= 2; // can be changed if needed.
-    exBootRecord.FSInfoCluster:=1; //TODO need FSINFO
-    exBootRecord.driveNumber:= $80;
-    exBootRecord.reserved0:=0;
-    exBootRecord.reserved1:=0;
-    exBootRecord.volumeID := 53424; //need random number generator
-    exBootRecord.volumeLabel[0] := 0; //needs to be set later !!!
-    exBootRecord.bsignature:= $29
-    exBootRecord.identString:= 'FAT32   ';
+    BootRecord.FATSize:= ((sectors DIV PFatVolumeInfo(config)^.sectorsPerCluster) * 16 DIV disk^.sectorSize);
+    BootRecord.flags:= 1 shl 7;
+    BootRecord.FATVersion:= 0;
+    BootRecord.rootCluster:= 2; // can be changed if needed.
+    BootRecord.FSInfoCluster:=1; //TODO need FSINFO
+    BootRecord.driveNumber:= $80;
+    //BootRecord.reserved0:=0;
+    //BootRecord.reserved1:=0;
+    BootRecord.volumeID := 53424; //need random number generator
+    //BootRecord.volumeLabel[0] := 0; //needs to be set later !!!
+    BootRecord.bsignature:= $29;
+    BootRecord.identString:= fatArray;
+
+    buffer:= @bootrecord;
+    disk^.writeCallback(disk, 1, sizeof(TBootRecord), buffer);
+
 
     //448bytes bootstrap
     //2bytes end mark 55AA
 
 end;
 
-function detect_volumes(disk : PStorage_Device) : APStorage_volume;
+procedure detect_volumes(disk : PStorage_Device; volumes : puint32);
 begin
     
 end;
diff --git a/src/driver/storage/storagemanagement.pas b/src/driver/storage/storagemanagement.pas
index 2dfe1df5..fd7c3c0a 100644
--- a/src/driver/storage/storagemanagement.pas
+++ b/src/driver/storage/storagemanagement.pas
@@ -25,11 +25,12 @@ type
     TControllerType = (ControllerIDE, ControllerUSB, ControllerAHCI, ControllerNET);
     PStorage_volume = ^TStorage_Volume;
     PStorage_device = ^TStorage_Device;
+    APStorage_Volume = array[0..10] of PStorage_volume;
 
     PPIOHook = procedure(volume : PStorage_volume; directory : pchar; byteCount : uint32; buffer : puint32);
     PPHIOHook = procedure(volume : PStorage_device; addr : uint32; sectors : uint32; buffer : puint32);
-    PPCreateHook = procedure(disk : PStorage_Device; sectors : uint32; start : uint32);
-    PPDetectHook = procedure(disk : PStorage_Device);
+    PPCreateHook = procedure(disk : PStorage_Device; sectors : uint32; start : uint32; config : puint32);
+    PPDetectHook = procedure(disk : PStorage_Device; volumes : Puint32);
 
     PPHIOHook_ = procedure;
 
@@ -37,8 +38,8 @@ type
         sName : pchar;
         writeCallback  : PPIOHook;
         readCallback   : PPIOHook;
-        createCallback : uint32;
-        detectCallback : uint32;
+        createCallback : PPCreateHook;
+        detectCallback : PPDetectHook;
     end;
 
     TStorage_Volume = record
@@ -49,7 +50,6 @@ type
         filesystemInfo  : Puint32; // type dependant on filesystem. can be null if not creating volume now
         directory       : PLinkedListBase; // type dependant on filesytem?
     end;
-    APStorage_Volume = array[0..10] of PStorage_volume;
 
     TStorage_Device = record
         idx              : uint8;