From af8eb560585897ec5488d29da24d89e9ed22e699 Mon Sep 17 00:00:00 2001
From: aaron <aaron@6dbc8c32-bb84-406f-8558-d1cf31a0ab0c>
Date: Thu, 12 Apr 2018 15:17:59 +0000
Subject: [PATCH] git-svn-id: https://spexeah.com:8443/svn/Asuro@508
 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

---
 src/driver/storage/IDE.pas               |  9 ++---
 src/driver/storage/fat32.pas             | 41 +++++++++++++++++---
 src/driver/storage/storagemanagement.pas | 49 +++++++++++++++++-------
 src/kernel.pas                           |  3 ++
 4 files changed, 79 insertions(+), 23 deletions(-)

diff --git a/src/driver/storage/IDE.pas b/src/driver/storage/IDE.pas
index 79121b44..d12b2958 100644
--- a/src/driver/storage/IDE.pas
+++ b/src/driver/storage/IDE.pas
@@ -16,7 +16,6 @@ uses
     drivertypes,
     console,
     terminal,
-    isr76,
     drivermanagement,
     vmemorymanager,
     lmemorymanager,
@@ -350,11 +349,11 @@ begin
         end;
 
         for ii:=0 to 127 do begin //read data
-            outw($1F0, Puint32(buffer + ((i * 512) + (ii * 16) DIV 32) )^);
+            outw($1F0, Puint32(buffer + ((i * 512) + (ii * 32) DIV 32) )^);
             while true do if (inw($1f7) and (1 shl 7)) = 0 then break; //Wait until drive not busy 
             outb($1F7, $E7);
             if ii <> 127 then begin
-                outw($1F0, Puint32(buffer + ((i * 512) + (ii * 16) DIV 32) )^ shr 16);
+                outw($1F0, Puint32(buffer + ((i * 512) + (ii * 32) DIV 32) )^ shr 16);
                 while true do if (inw($1f7) and (1 shl 7)) = 0 then break; //Wait until drive not busy 
                 outb($1F7, $E7);
             end;
@@ -404,10 +403,10 @@ begin
         end;
 
         for ii:=0 to 127 do begin //read data
-            Puint32(buffer + ((i * 512) + (ii * 16) DIV 32))^ := uint32(inw($1F0)); //wrong
+            Puint32(buffer + ((i * 512) + (ii * 32) DIV 32))^ := uint32(inw($1F0)); //wrong
             for iii:=0 to 1000 do if(ii = iii) then begin  end;
             if ii <> 127 then begin
-                Puint32(buffer + ((i * 512) + (ii * 16) DIV 32))^ := ((uint32(inw($1F0)) shl 16) or Puint32(buffer + ((i * 512) + (ii * 16) DIV 32))^); //wrong
+                Puint32(buffer + ((i * 512) + (ii * 32) DIV 32))^ := ((uint32(inw($1F0)) shl 16) or Puint32(buffer + ((i * 512) + (ii * 16) DIV 32))^); //wrong
                 for iii:=0 to 1000 do if(ii = iii) then begin  end
             end;
         end;
diff --git a/src/driver/storage/fat32.pas b/src/driver/storage/fat32.pas
index cbb4b664..28db4b51 100644
--- a/src/driver/storage/fat32.pas
+++ b/src/driver/storage/fat32.pas
@@ -51,7 +51,7 @@ type
     byteArray8 = array[0..7] of char;
 
     TDirectory = bitpacked record
-        fileName      : uint64;
+        fileName      : array[0..7] of char;
         fileExtension : ubit24;
         attributes    : uint8;
         reserved0     : uint8;
@@ -107,7 +107,7 @@ begin
     filesystem.readcallback:= @read;
     filesystem.createcallback:= @create_volume;
     filesystem.detectcallback:= @detect_volumes; 
-    storagemanagement.register_filesystem(filesystem);
+    storagemanagement.register_filesystem(@filesystem);
 end;
 
 procedure create_volume(disk : PStorage_Device; sectors : uint32; start : uint32; config : puint32);
@@ -117,6 +117,10 @@ var
     buffer : puint32;
     asuroArray : byteArray8;// = byteArray8(['A','S','U','R','O',' ','V','1']);
     fatArray : byteArray8;// = byteArray8(['F','A','T','3','2',' ',' ',' ']);
+    tmpArray : byteArray8;
+
+    fatStart : uint32;
+    dataStart: uint32;
 begin
 
     asuroArray[0] := 'A';
@@ -137,6 +141,7 @@ begin
     fatArray[6] := ' ';
     fatArray[7] := ' ';
 
+    buffer:= puint32(kalloc(512));
 
     bootrecord.jmp2boot:= $00; // TODO what ahppens here???
     bootRecord.OEMName:= asuroArray;
@@ -153,7 +158,8 @@ begin
     bootRecord.hiddenSectors:= start;
     bootRecord.manySectors:= sectors;
 
-    BootRecord.FATSize:= ((sectors DIV PFatVolumeInfo(config)^.sectorsPerCluster) * 16 DIV disk^.sectorSize);
+    //BootRecord.FATSize:= ((sectors DIV PFatVolumeInfo(config)^.sectorsPerCluster) * 16 DIV disk^.sectorSize);
+    BootRecord.FATSize:= ((sectors DIV 4) * 16 DIV disk^.sectorSize);
     BootRecord.flags:= 0; //1 shl 7 for mirroring
     BootRecord.FATVersion:= 0;
     BootRecord.rootCluster:= 2; // can be changed if needed.
@@ -166,21 +172,46 @@ begin
     BootRecord.bsignature:= $29;
     BootRecord.identString:= fatArray;
 
+    buffer:= @bootrecord;
     puint32(buffer + (127))^:= $55AA; //end marker
 
-    buffer:= @bootrecord;
     disk^.writeCallback(disk, start + 1, 1, buffer);
 
+    dataStart:= (sectors DIV bootrecord.spc) + 2;
+
     //TODO FSINFO struct
 
     //write fat
     buffer := puint32(kalloc((sectors DIV bootrecord.spc) * 4));
     memset(uint32(buffer), 0, sectors DIV bootrecord.spc);
     puint32(buffer + 1)^:= $FFF8; //root directory table cluster, currently root is only 1 cluster long
-    disk^.writeCallback(disk, start + 2, sectors DIV bootrecord.spc, buffer);
+    disk^.writeCallback(disk, start + 2, sectors DIV bootrecord.spc, buffer); 
     //disk^.writeCallback(disk, start + 2 + (sectors Div bootrecord.sectorsPerCluster DIV 512), sectors DIV bootrecord.sectorsPerCluster, buffer);
+    kfree(buffer);
 
+    //setup root directory
+    buffer:= puint32(kalloc(512));
+    memset(uint32(buffer), 0, 512);
 
+    tmpArray[0]:= '.';
+    tmpArray[1]:= ' ';
+    tmpArray[2]:= ' ';
+    tmpArray[3]:= ' ';
+    tmpArray[4]:= ' ';
+    tmpArray[5]:= ' ';
+    tmpArray[6]:= ' ';
+    tmpArray[7]:= ' ';
+
+    PDirectory(buffer)^.fileName:= tmpArray;
+    PDirectory(buffer)^.attributes:= $10; // is directory
+    PDirectory(buffer)^.clusterLow:= 2; //my cluster location
+
+    tmpArray[1]:= '.';
+    PDirectory(buffer + (sizeof(TDirectory) DIV 4 ) )^.fileName:= tmpArray;
+    PDirectory(buffer + (sizeof(TDirectory) DIV 4) )^.attributes:= $08; // volume id
+    PDirectory(buffer + (sizeof(TDirectory) DIV 4) )^.clusterLow:= 2; //my cluster location
+
+    disk^.writeCallback(disk, dataStart + (bootRecord.spc), 1, buffer);
 
 end;
 
diff --git a/src/driver/storage/storagemanagement.pas b/src/driver/storage/storagemanagement.pas
index d8a2f205..d75a4a5a 100644
--- a/src/driver/storage/storagemanagement.pas
+++ b/src/driver/storage/storagemanagement.pas
@@ -17,8 +17,10 @@ uses
     console,
     terminal,
     drivermanagement,
+    lmemorymanager,
     strings,
-    lists;
+    lists,
+    tracer;
 
 type 
 
@@ -41,12 +43,13 @@ type
         createCallback : PPCreateHook;
         detectCallback : PPDetectHook;
     end;
+    PFileSystem = ^TFilesystem;
 
     TStorage_Volume = record
         sectorStart     : uint32;
         sectorSize      : uint32;
         freeSectors     : uint32;
-        filesystem      : TFilesystem; 
+        filesystem      : PFilesystem; 
         filesystemInfo  : Puint32; // type dependant on filesystem. can be null if not creating volume now
         directory       : PLinkedListBase; // type dependant on filesytem?
     end;
@@ -67,14 +70,14 @@ type
 
 var 
     storageDevices : PLinkedListBase; //index in this array is global drive id
-    fileSystems : array[0..7] of TFilesystem;
+    fileSystems : PLinkedListBase;
 
 procedure init();
 
 procedure register_device(device : PStorage_Device);
 function get_device_list() : PLinkedListBase;
 
-procedure register_filesystem(filesystem : TFilesystem);
+procedure register_filesystem(filesystem : PFilesystem);
 
 //procedure register_volume(volume : TStorage_Volume);
 
@@ -85,8 +88,12 @@ implementation
 procedure disk_command(params : PParamList);
 var
     i : uint8;
+    spc : puint32;
+    drive : uint32;
 begin
-
+    push_trace('storagemanagement.diskcommand');
+    spc:= puint32(kalloc(4));
+    spc^:= 4;
 
     if stringEquals(getParam(0, params), 'ls') and (LL_Size(storageDevices) > 0) then begin
         for i:=0 to LL_Size(storageDevices) - 1 do begin
@@ -103,20 +110,39 @@ begin
             console.writeint((( PStorage_Device(LL_Get(storageDevices, i))^.maxSectorCount * PStorage_Device(LL_Get(storageDevices, i))^.sectorSize) DIV 1024) DIV 1024);
             console.writestringln('MB');
         end;
+    end else if stringEquals(getParam(0, params), 'format') then begin //disk format 0 fat32
+        //check param count!
+        drive :=  stringToInt(getParam(1, params));
+        console.writeintln(drive); // works
+        if stringEquals(getParam(2, params), 'fat32') then begin
+                PFilesystem(LL_Get(filesystems, 0))^.createCallback((PStorage_Device(LL_Get(storageDevices, drive))), 100000, 1, @spc); //todo check fs
+                console.writestring('Drive ');
+                //console.writeint(drive); // page faults
+                console.writestringln(' formatted.');
+        end;
+
+    end else if stringEquals(getParam(0, params), 'lsfs') then begin
+        for i:=0 to LL_Size(filesystems)-1 do begin
+            //print file systems
+            console.writestringln(PFilesystem(LL_Get(filesystems, i))^.sName);
+        end;
     end;
+    pop_trace;
 end;
 
 procedure init();
 begin
+    push_trace('storagemanagement.init');
     storageDevices:= ll_New(sizeof(TStorage_Device));
+    fileSystems:= ll_New(sizeof(TFilesystem));
     terminal.registerCommand('DISK', @disk_command, 'List physical storage devices');
+    pop_trace();
 end;
 
 procedure register_device(device : PStorage_device);
 var 
     i   : uint8;
     elm : void;
-    dev : PStorage_device;
 begin
     elm:= LL_Add(storageDevices);
     PStorage_device(elm)^ := device^;
@@ -129,16 +155,13 @@ begin
     get_device_list:= storageDevices;
 end;
 
-procedure register_filesystem(filesystem : TFilesystem);
+procedure register_filesystem(filesystem : PFilesystem);
 var
     i : uint8;
+    elm : void;    
 begin
-    for i:= 0 to 31 do begin
-        if fileSystems[i].sName = nil then begin
-            fileSystems[i]:= filesystem;
-            break;
-        end;
-    end; 
+    elm:= LL_Add(fileSystems);
+    PFileSystem(elm)^ := filesystem^;
 end;
 
 end.
\ No newline at end of file
diff --git a/src/kernel.pas b/src/kernel.pas
index 419f8497..63b364be 100644
--- a/src/kernel.pas
+++ b/src/kernel.pas
@@ -167,6 +167,9 @@ begin
      TMR_0_ISR.hook(uint32(@bios_data_area.tick_update));
      tracer.pop_trace;
 
+     { Filsystems }
+     fat32.init();
+
      { Device Drivers }
      tracer.push_trace('kmain.DEVDRV');
      console.outputln('KERNEL', 'DEVICE DRIVERS: INIT BEGIN.');