From e4ed0cf1cf3d021f1371e2c640058db5be08e0f7 Mon Sep 17 00:00:00 2001
From: aaron <aaron@6dbc8c32-bb84-406f-8558-d1cf31a0ab0c>
Date: Sat, 7 Apr 2018 22:46:14 +0000
Subject: [PATCH] git-svn-id: https://spexeah.com:8443/svn/Asuro@393
 6dbc8c32-bb84-406f-8558-d1cf31a0ab0c

---
 src/driver/PCI.pas                       |  1 -
 src/driver/storage/IDE.pas               | 16 ++++++-----
 src/driver/storage/storagemanagement.pas | 35 ++++++++++++++++--------
 src/kernel.pas                           |  4 ++-
 4 files changed, 35 insertions(+), 21 deletions(-)

diff --git a/src/driver/PCI.pas b/src/driver/PCI.pas
index 6e44234a..eb1aabbb 100644
--- a/src/driver/PCI.pas
+++ b/src/driver/PCI.pas
@@ -130,7 +130,6 @@ begin
             if devices[device_count - 1].header_type and $80 <> 0 then begin
                 for func := 1 to 8 do begin
                     loadDeviceConfig(bus, slot, func);
-                    psleep(10);
                 end;
             end;
         end;
diff --git a/src/driver/storage/IDE.pas b/src/driver/storage/IDE.pas
index e7a3a743..40d021dc 100644
--- a/src/driver/storage/IDE.pas
+++ b/src/driver/storage/IDE.pas
@@ -19,7 +19,8 @@ uses
     isr76,
     drivermanagement,
     vmemorymanager,
-    lmemorymanager;
+    lmemorymanager,
+    storagemanagement;
 
 type 
     TIdentResponse = array[0..255] of uint16;
@@ -170,24 +171,27 @@ begin
     devID.id4:= idANY;
     devID.ex:= nil;
     drivermanagement.register_driver('IDE ATA Driver', @devID, @load);
-    terminal.registerCommand('IDE', @test_command, 'TEST IDE DRIVER');
+    //terminal.registerCommand('IDE', @test_command, 'TEST IDE DRIVER');
     buffer := Puint32(kalloc(1024*2));
 end;
 
 function load(ptr : void) : boolean;
+var
+    storageDevice : TStorage_Device;
 begin
     //controller := PPCI_Device(ptr);
 
 
     //check if bus is floating and identify device
     if inb($1F7) <> $FF then begin
-                console.writeint(1);
         outb($3F6, inb($3f6) or (1 shl 1)); // disable interrupts
-                    console.writeint(1);
-
         IDEDevices[0].exists:= true;
         IDEDevices[0].isMaster:= true;
         IDEDevices[0].info := identify_device(0, $A0);
+
+        storageDevice.controller := ControllerIDE;
+        storageDevice.maxSectorCount:= (IDEDevices[0].info[60] or (IDEDevices[0].info[61] shl 16) ); //LBA28
+        storagemanagement.register_device(storageDevice);
     end 
     //identify_device(0, $B0);
 
@@ -206,7 +210,6 @@ begin
         outw($1F3, 0);
         outw($1F4, 0);
         outw($1F5, 0); 
-                    console.writeint(1);
 
         outw($1F7, ATA_CMD_IDENTIFY); //send identify command
             console.writeint(1);
@@ -214,7 +217,6 @@ begin
         while true do begin
             if (inw($1f7) and (1 shl 7)) = 0 then break; //Wait until drive not busy 
         end;
-                    console.writeint(1);
 
         while true do begin
             if (inw($1f7) and (1 shl 3)) <> 0 then break;
diff --git a/src/driver/storage/storagemanagement.pas b/src/driver/storage/storagemanagement.pas
index 0ae2389b..140c19e0 100644
--- a/src/driver/storage/storagemanagement.pas
+++ b/src/driver/storage/storagemanagement.pas
@@ -16,7 +16,6 @@ uses
     drivertypes,
     console,
     terminal,
-    IDE,
     drivermanagement,
     vmemorymanager,
     lmemorymanager;
@@ -34,17 +33,16 @@ type
         usedSectorCount  : uint32;
     end;
 
-const
-    
 
 var 
     storageDevices : array[0..255] of TStorage_Device;
 
+//TODO need callback things for when new devices are connected
 procedure init();
 procedure register_device(device : TStorage_Device);
-function get_all_devices() : APStorage_Device;
-function read(device : uint16; address : uint32; byteCount : uint32) : PuInt32;
-procedure write(device : uint16; address : uint32; byteCount : uint32; data : PuInt32);
+//function get_all_devices() : APStorage_Device;
+//function read(device : uint16; address : uint32; byteCount : uint32) : PuInt32;
+//procedure write(device : uint16; address : uint32; byteCount : uint32; data : PuInt32);
 
 implementation 
 
@@ -53,17 +51,17 @@ var
     i : uint8;
 begin
     for i:=0 to 255 do begin
-        if storageDevices[i].controller = nil then break;
+        if storageDevices[i].maxSectorCount = 0 then break;
         console.writeint(i);
         console.writestring(') Device_Type: ');
         case storageDevices[i].controller of
-            ControllerIDE:console.writestring('IDE ');
-            ControllerUSB:console.writestring('USB ');
-            ControllerAHCI:console.writestring('AHCI ');
-            ControllerNET:console.writestring('NET ');
+            ControllerIDE  : console.writestring('IDE, ');
+            ControllerUSB  : console.writestring('USB, ');
+            ControllerAHCI : console.writestring('AHCI, ');
+            ControllerNET  : console.writestring('NET, ');
         end;
         console.writestring('Capacity: ');
-        console.writeint(((sectors * 512) / 1000) / 1000);
+        console.writeint(((storageDevices[i].maxSectorCount * 512) DIV 1000) DIV 1000);
         console.writestringln('MB');
     end;
 end;
@@ -72,4 +70,17 @@ procedure init();
 begin
     terminal.registerCommand('disks', @list_drives_command, 'List storage devices');
 end;
+
+procedure register_device(device : TStorage_Device);
+var 
+    i : uint8;
+begin
+    for i:=0 to 255 do begin 
+        if storageDevices[i].maxSectorCount = 0 then begin
+            storageDevices[i]:= device;
+            break;
+        end;
+    end;
+end;
+
 end.
\ No newline at end of file
diff --git a/src/kernel.pas b/src/kernel.pas
index d066c131..22ccdfa9 100644
--- a/src/kernel.pas
+++ b/src/kernel.pas
@@ -36,7 +36,8 @@ uses
      testdriver,
      E1000,
      AHCI_OLD,
-     IDE;
+     IDE,
+     storagemanagement;
  
 procedure kmain(mbinfo: Pmultiboot_info_t; mbmagic: uint32); stdcall;
  
@@ -94,6 +95,7 @@ begin
      terminal.registerCommand('BSOD', @terminal_command_bsod, 'Force a Panic Screen.');
 
      drivermanagement.init();
+     storagemanagement.init();
 
      console.init();