summaryrefslogtreecommitdiffhomepage
path: root/dev/kernel/HALKit/AMD64/Storage
diff options
context:
space:
mode:
Diffstat (limited to 'dev/kernel/HALKit/AMD64/Storage')
-rw-r--r--dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc534
-rw-r--r--dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc200
-rw-r--r--dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc314
-rw-r--r--dev/kernel/HALKit/AMD64/Storage/SCSI+Generic.cc6
4 files changed, 484 insertions, 570 deletions
diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc
index 576c151b..aa9ab2cb 100644
--- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc
+++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc
@@ -1,6 +1,6 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss Corporation, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss Corporation, all rights reserved.
------------------------------------------- */
@@ -15,23 +15,23 @@
*
*/
+#include <FirmwareKit/EPM.h>
#include <KernelKit/DeviceMgr.h>
#include <KernelKit/DriveMgr.h>
-#include <KernelKit/ProcessScheduler.h>
#include <KernelKit/KPC.h>
-#include <FirmwareKit/EPM.h>
-#include <StorageKit/AHCI.h>
-#include <modules/ATA/ATA.h>
-#include <modules/AHCI/AHCI.h>
+#include <KernelKit/LockDelegate.h>
#include <KernelKit/PCI/Iterator.h>
+#include <KernelKit/ProcessScheduler.h>
#include <NewKit/Utils.h>
-#include <KernelKit/LockDelegate.h>
+#include <StorageKit/AHCI.h>
+#include <modules/AHCI/AHCI.h>
+#include <modules/ATA/ATA.h>
#define kHBAErrTaskFile (1 << 30)
-#define kHBAPxCmdST (0x0001)
-#define kHBAPxCmdFre (0x0010)
-#define kHBAPxCmdFR (0x4000)
-#define kHBAPxCmdCR (0x8000)
+#define kHBAPxCmdST (0x0001)
+#define kHBAPxCmdFre (0x0010)
+#define kHBAPxCmdFR (0x4000)
+#define kHBAPxCmdCR (0x8000)
#define kSATALBAMode (1 << 6)
@@ -39,87 +39,84 @@
#define kSATASRDrq (0x08)
#define kHBABohcBiosOwned (1 << 0)
-#define kHBABohcOSOwned (1 << 1)
+#define kHBABohcOSOwned (1 << 1)
#define kSATAPortCnt (0x20)
-#define kSATASig (0x00000101)
+#define kSATASig (0x00000101)
#define kSATAPISig (0xEB140101)
#define kSATAProgIfAHCI (0x01)
-#define kSATASubClass (0x06)
-#define kSATABar5 (0x24)
+#define kSATASubClass (0x06)
+#define kSATABar5 (0x24)
using namespace Kernel;
STATIC PCI::Device kSATADev;
STATIC HbaMemRef kSATAHba;
-STATIC Lba kSATASectorCount = 0UL;
-STATIC UInt16 kSATAIndex = 0U;
-STATIC Char kCurrentDiskModel[50] = {"GENERIC SATA"};
-STATIC UInt16 kSATAPortsImplemented = 0U;
+STATIC Lba kSATASectorCount = 0UL;
+STATIC UInt16 kSATAIndex = 0U;
+STATIC Char kCurrentDiskModel[50] = {"GENERIC SATA"};
+STATIC UInt16 kSATAPortsImplemented = 0U;
template <BOOL Write, BOOL CommandOrCTRL, BOOL Identify>
-STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz, SizeT size_buffer) noexcept;
+STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz,
+ SizeT size_buffer) noexcept;
STATIC Int32 drv_find_cmd_slot_ahci(HbaPort* port) noexcept;
STATIC Void drv_compute_disk_ahci() noexcept;
-STATIC Void drv_compute_disk_ahci() noexcept
-{
- kSATASectorCount = 0UL;
+STATIC Void drv_compute_disk_ahci() noexcept {
+ kSATASectorCount = 0UL;
- /// Normally 512 bytes, but add an additional 512 bytes to make 1 KIB.
- const UInt16 kSzIdent = 512;
+ /// Normally 512 bytes, but add an additional 512 bytes to make 1 KIB.
+ const UInt16 kSzIdent = 512;
- /// Push it to the stack
- UInt8* identify_data = new UInt8[kSzIdent];
+ /// Push it to the stack
+ UInt8* identify_data = new UInt8[kSzIdent];
- /// Send AHCI command for identification.
- drv_std_input_output_ahci<NO, YES, YES>(0, identify_data, kAHCISectorSize, kSzIdent);
+ /// Send AHCI command for identification.
+ drv_std_input_output_ahci<NO, YES, YES>(0, identify_data, kAHCISectorSize, kSzIdent);
- /// Extract 48-bit LBA.
- UInt64 lba48_sectors = 0;
- lba48_sectors |= (UInt64)identify_data[100];
- lba48_sectors |= (UInt64)identify_data[101] << 16;
- lba48_sectors |= (UInt64)identify_data[102] << 32;
+ /// Extract 48-bit LBA.
+ UInt64 lba48_sectors = 0;
+ lba48_sectors |= (UInt64) identify_data[100];
+ lba48_sectors |= (UInt64) identify_data[101] << 16;
+ lba48_sectors |= (UInt64) identify_data[102] << 32;
- /// Now verify if lba48
- if (lba48_sectors == 0)
- kSATASectorCount = (identify_data[61] << 16) | identify_data[60];
- else
- kSATASectorCount = lba48_sectors;
+ /// Now verify if lba48
+ if (lba48_sectors == 0)
+ kSATASectorCount = (identify_data[61] << 16) | identify_data[60];
+ else
+ kSATASectorCount = lba48_sectors;
- for (Int32 i = 0; i < 20; i++)
- {
- kCurrentDiskModel[i * 2] = (identify_data[27 + i] >> 8) & 0xFF;
- kCurrentDiskModel[i * 2 + 1] = identify_data[27 + i] & 0xFF;
- }
+ for (Int32 i = 0; i < 20; i++) {
+ kCurrentDiskModel[i * 2] = (identify_data[27 + i] >> 8) & 0xFF;
+ kCurrentDiskModel[i * 2 + 1] = identify_data[27 + i] & 0xFF;
+ }
- kCurrentDiskModel[40] = '\0';
+ kCurrentDiskModel[40] = '\0';
- (Void)(kout << "SATA Sector Count: " << hex_number(kSATASectorCount) << kendl);
- (Void)(kout << "SATA Disk Model: " << kCurrentDiskModel << kendl);
+ (Void)(kout << "SATA Sector Count: " << hex_number(kSATASectorCount) << kendl);
+ (Void)(kout << "SATA Disk Model: " << kCurrentDiskModel << kendl);
- delete[] identify_data;
- identify_data = nullptr;
+ delete[] identify_data;
+ identify_data = nullptr;
}
/// @brief Finds a command slot for a HBA port.
/// @param port The port to search on.
/// @return The slot, or ~0.
-STATIC Int32 drv_find_cmd_slot_ahci(HbaPort* port) noexcept
-{
- UInt32 slots = port->Sact | port->Ci;
+STATIC Int32 drv_find_cmd_slot_ahci(HbaPort* port) noexcept {
+ UInt32 slots = port->Sact | port->Ci;
- for (Int32 i = 0; i < kSATAPortCnt; ++i) // AHCI supports up to 32 slots
- {
- if ((slots & (1U << i)) == 0)
- return i;
- }
+ for (Int32 i = 0; i < kSATAPortCnt; ++i) // AHCI supports up to 32 slots
+ {
+ if ((slots & (1U << i)) == 0) return i;
+ }
- return -1; // no free slot found
+ return -1; // no free slot found
}
/// @brief Send an AHCI command, according to the template parameters.
@@ -128,225 +125,204 @@ STATIC Int32 drv_find_cmd_slot_ahci(HbaPort* port) noexcept
/// @param sector_sz The disk's sector size (unused)
/// @param size_buffer The size of the **buffer** parameter.
template <BOOL Write, BOOL CommandOrCTRL, BOOL Identify>
-STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz, SizeT size_buffer) noexcept
-{
- UIntPtr slot = 0UL;
+STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz,
+ SizeT size_buffer) noexcept {
+ UIntPtr slot = 0UL;
- slot = drv_find_cmd_slot_ahci(&kSATAHba->Ports[kSATAIndex]);
+ slot = drv_find_cmd_slot_ahci(&kSATAHba->Ports[kSATAIndex]);
- if (slot == ~0UL)
- {
- err_global_get() = kErrorDisk;
- return;
- }
+ if (slot == ~0UL) {
+ err_global_get() = kErrorDisk;
+ return;
+ }
- if (size_buffer > mib_cast(4) ||
- sector_sz > kAHCISectorSize)
- return;
+ if (size_buffer > mib_cast(4) || sector_sz > kAHCISectorSize) return;
- if (!Write)
- {
- // Zero-memory the buffer field.
- rt_set_memory(buffer, 0, size_buffer);
- }
+ if (!Write) {
+ // Zero-memory the buffer field.
+ rt_set_memory(buffer, 0, size_buffer);
+ }
- /// prepare command header.
- volatile HbaCmdHeader* command_header = ((volatile HbaCmdHeader*)(((UInt64)kSATAHba->Ports[kSATAIndex].Clb)));
+ /// prepare command header.
+ volatile HbaCmdHeader* command_header =
+ ((volatile HbaCmdHeader*) (((UInt64) kSATAHba->Ports[kSATAIndex].Clb)));
- /// Offset to specific command slot.
- command_header += slot;
+ /// Offset to specific command slot.
+ command_header += slot;
- /// check for command header.
- MUST_PASS(command_header);
+ /// check for command header.
+ MUST_PASS(command_header);
- command_header->Struc.Cfl = sizeof(FisRegH2D) / sizeof(UInt32);
- command_header->Struc.Write = Write;
- command_header->Prdtl = 8;
+ command_header->Struc.Cfl = sizeof(FisRegH2D) / sizeof(UInt32);
+ command_header->Struc.Write = Write;
+ command_header->Prdtl = 8;
- auto ctba_phys = ((UInt64)command_header->Ctbau << 32) | command_header->Ctba;
- auto command_table = reinterpret_cast<volatile HbaCmdTbl*>(ctba_phys);
+ auto ctba_phys = ((UInt64) command_header->Ctbau << 32) | command_header->Ctba;
+ auto command_table = reinterpret_cast<volatile HbaCmdTbl*>(ctba_phys);
- MUST_PASS(command_table);
+ MUST_PASS(command_table);
- UIntPtr buffer_phys = HAL::hal_get_phys_address(buffer);
- SizeT bytes_remaining = size_buffer;
+ UIntPtr buffer_phys = HAL::hal_get_phys_address(buffer);
+ SizeT bytes_remaining = size_buffer;
- command_table->Prdt[0].Dba = (UInt32)(buffer_phys & 0xFFFFFFFF);
- command_table->Prdt[0].Dbau = (UInt32)(buffer_phys >> 32);
- command_table->Prdt[0].Dbc = bytes_remaining - 1;
- command_table->Prdt[0].Ie = NO;
+ command_table->Prdt[0].Dba = (UInt32) (buffer_phys & 0xFFFFFFFF);
+ command_table->Prdt[0].Dbau = (UInt32) (buffer_phys >> 32);
+ command_table->Prdt[0].Dbc = bytes_remaining - 1;
+ command_table->Prdt[0].Ie = NO;
- volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*)(&command_table->Cfis[0]);
+ volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*) (&command_table->Cfis[0]);
- h2d_fis->FisType = kFISTypeRegH2D;
- h2d_fis->CmdOrCtrl = CommandOrCTRL;
- h2d_fis->Command = (Identify ? (kAHCICmdIdentify) : (Write ? kAHCICmdWriteDmaEx : kAHCICmdReadDmaEx));
+ h2d_fis->FisType = kFISTypeRegH2D;
+ h2d_fis->CmdOrCtrl = CommandOrCTRL;
+ h2d_fis->Command =
+ (Identify ? (kAHCICmdIdentify) : (Write ? kAHCICmdWriteDmaEx : kAHCICmdReadDmaEx));
- h2d_fis->Lba0 = (lba)&0xFF;
- h2d_fis->Lba1 = (lba >> 8) & 0xFF;
- h2d_fis->Lba2 = (lba >> 16) & 0xFF;
+ h2d_fis->Lba0 = (lba) & 0xFF;
+ h2d_fis->Lba1 = (lba >> 8) & 0xFF;
+ h2d_fis->Lba2 = (lba >> 16) & 0xFF;
- h2d_fis->Device = kSATALBAMode;
+ h2d_fis->Device = kSATALBAMode;
- h2d_fis->Lba3 = (lba >> 24) & 0xFF;
- h2d_fis->Lba4 = (lba >> 32) & 0xFF;
- h2d_fis->Lba5 = (lba >> 40) & 0xFF;
+ h2d_fis->Lba3 = (lba >> 24) & 0xFF;
+ h2d_fis->Lba4 = (lba >> 32) & 0xFF;
+ h2d_fis->Lba5 = (lba >> 40) & 0xFF;
- h2d_fis->CountLow = (size_buffer)&0xFF;
- h2d_fis->CountHigh = (size_buffer >> 8) & 0xFF;
+ h2d_fis->CountLow = (size_buffer) & 0xFF;
+ h2d_fis->CountHigh = (size_buffer >> 8) & 0xFF;
- kSATAHba->Ports[kSATAIndex].Ci = (1 << slot);
+ kSATAHba->Ports[kSATAIndex].Ci = (1 << slot);
- for (Int32 i = 0; i < 1000000; ++i)
- {
- if (!(kSATAHba->Ports[kSATAIndex].Ci & (1 << slot)))
- break;
- }
+ for (Int32 i = 0; i < 1000000; ++i) {
+ if (!(kSATAHba->Ports[kSATAIndex].Ci & (1 << slot))) break;
+ }
- if (kSATAHba->Is & kHBAErrTaskFile)
- {
- err_global_get() = kErrorDiskIsCorrupted;
- return;
- }
+ if (kSATAHba->Is & kHBAErrTaskFile) {
+ err_global_get() = kErrorDiskIsCorrupted;
+ return;
+ }
- err_global_get() = kErrorSuccess;
+ err_global_get() = kErrorSuccess;
}
/***
- @brief Gets the number of sectors inside the drive.
- @return Sector size in bytes.
+ @brief Gets the number of sectors inside the drive.
+ @return Sector size in bytes.
*/
-SizeT drv_get_sector_count_ahci()
-{
- return kSATASectorCount;
+SizeT drv_get_sector_count_ahci() {
+ return kSATASectorCount;
}
/// @brief Get the drive size.
/// @return Disk size in bytes.
-SizeT drv_get_size_ahci()
-{
- return drv_get_sector_count() * kAHCISectorSize;
+SizeT drv_get_size_ahci() {
+ return drv_get_sector_count() * kAHCISectorSize;
}
/// @brief Enable Host and probe using the IDENTIFY command.
-STATIC BOOL ahci_enable_and_probe()
-{
- if (kSATAHba->Cap == 0x0)
- return NO;
+STATIC BOOL ahci_enable_and_probe() {
+ if (kSATAHba->Cap == 0x0) return NO;
- kSATAHba->Ports[kSATAIndex].Cmd &= ~kHBAPxCmdFre;
- kSATAHba->Ports[kSATAIndex].Cmd &= ~kHBAPxCmdST;
+ kSATAHba->Ports[kSATAIndex].Cmd &= ~kHBAPxCmdFre;
+ kSATAHba->Ports[kSATAIndex].Cmd &= ~kHBAPxCmdST;
- while (YES)
- {
- if (kSATAHba->Ports[kSATAIndex].Cmd & kHBAPxCmdCR)
- continue;
+ while (YES) {
+ if (kSATAHba->Ports[kSATAIndex].Cmd & kHBAPxCmdCR) continue;
- if (kSATAHba->Ports[kSATAIndex].Cmd & kHBAPxCmdFR)
- continue;
+ if (kSATAHba->Ports[kSATAIndex].Cmd & kHBAPxCmdFR) continue;
- break;
- }
+ break;
+ }
- // Now we are ready.
+ // Now we are ready.
- kSATAHba->Ports[kSATAIndex].Cmd |= kHBAPxCmdFre;
- kSATAHba->Ports[kSATAIndex].Cmd |= kHBAPxCmdST;
+ kSATAHba->Ports[kSATAIndex].Cmd |= kHBAPxCmdFre;
+ kSATAHba->Ports[kSATAIndex].Cmd |= kHBAPxCmdST;
- if (kSATAHba->Bohc & kHBABohcBiosOwned)
- {
- kSATAHba->Bohc |= kHBABohcOSOwned;
+ if (kSATAHba->Bohc & kHBABohcBiosOwned) {
+ kSATAHba->Bohc |= kHBABohcOSOwned;
- while (kSATAHba->Bohc & kHBABohcBiosOwned)
- {
- ;
- }
- }
+ while (kSATAHba->Bohc & kHBABohcBiosOwned) {
+ ;
+ }
+ }
- drv_compute_disk_ahci();
+ drv_compute_disk_ahci();
- return YES;
+ return YES;
}
/// @brief Initializes an AHCI disk.
/// @param pi the amount of ports that have been detected.
/// @param atapi reference value, tells whether we should detect ATAPI instead of SATA.
/// @return if the disk was successfully initialized or not.
-STATIC Bool drv_std_init_ahci(UInt16& pi, BOOL& atapi)
-{
- PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController);
+STATIC Bool drv_std_init_ahci(UInt16& pi, BOOL& atapi) {
+ PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController);
- for (SizeT device_index = 0; device_index < NE_BUS_COUNT; ++device_index)
- {
- kSATADev = iterator[device_index].Leak(); // Leak device.
+ for (SizeT device_index = 0; device_index < NE_BUS_COUNT; ++device_index) {
+ kSATADev = iterator[device_index].Leak(); // Leak device.
- if (kSATADev.Subclass() == kSATASubClass &&
- kSATADev.ProgIf() == kSATAProgIfAHCI)
- {
- kSATADev.EnableMmio();
- kSATADev.BecomeBusMaster();
+ if (kSATADev.Subclass() == kSATASubClass && kSATADev.ProgIf() == kSATAProgIfAHCI) {
+ kSATADev.EnableMmio();
+ kSATADev.BecomeBusMaster();
- HbaMem* mem_ahci = (HbaMem*)kSATADev.Bar(kSATABar5);
+ HbaMem* mem_ahci = (HbaMem*) kSATADev.Bar(kSATABar5);
- HAL::mm_map_page((VoidPtr)mem_ahci, (VoidPtr)mem_ahci, HAL::kMMFlagsPresent | HAL::kMMFlagsWr | HAL::kMMFlagsPCD | HAL::kMMFlagsPwt);
+ HAL::mm_map_page(
+ (VoidPtr) mem_ahci, (VoidPtr) mem_ahci,
+ HAL::kMMFlagsPresent | HAL::kMMFlagsWr | HAL::kMMFlagsPCD | HAL::kMMFlagsPwt);
- UInt32 ports_implemented = mem_ahci->Pi;
- UInt16 ahci_index = 0;
+ UInt32 ports_implemented = mem_ahci->Pi;
+ UInt16 ahci_index = 0;
- pi = ports_implemented;
+ pi = ports_implemented;
- const UInt16 kSATAMaxPortsImplemented = ports_implemented;
- const UInt32 kSATASignature = kSATASig;
- const UInt32 kSATAPISignature = kSATAPISig;
- const UInt8 kSATAPresent = 0x03;
- const UInt8 kSATAIPMActive = 0x01;
+ const UInt16 kSATAMaxPortsImplemented = ports_implemented;
+ const UInt32 kSATASignature = kSATASig;
+ const UInt32 kSATAPISignature = kSATAPISig;
+ const UInt8 kSATAPresent = 0x03;
+ const UInt8 kSATAIPMActive = 0x01;
- if (kSATAMaxPortsImplemented < 1)
- continue;
+ if (kSATAMaxPortsImplemented < 1) continue;
- while (ports_implemented)
- {
- UInt8 ipm = (mem_ahci->Ports[ahci_index].Ssts >> 8) & 0x0F;
- UInt8 det = (mem_ahci->Ports[ahci_index].Ssts & 0x0F);
+ while (ports_implemented) {
+ UInt8 ipm = (mem_ahci->Ports[ahci_index].Ssts >> 8) & 0x0F;
+ UInt8 det = (mem_ahci->Ports[ahci_index].Ssts & 0x0F);
- if (det != kSATAPresent || ipm != kSATAIPMActive)
- continue;
+ if (det != kSATAPresent || ipm != kSATAIPMActive) continue;
- if ((mem_ahci->Ports[ahci_index].Sig == kSATASignature) ||
- (atapi && kSATAPISignature == mem_ahci->Ports[ahci_index].Sig))
- {
- kSATAIndex = ahci_index;
- kSATAHba = mem_ahci;
+ if ((mem_ahci->Ports[ahci_index].Sig == kSATASignature) ||
+ (atapi && kSATAPISignature == mem_ahci->Ports[ahci_index].Sig)) {
+ kSATAIndex = ahci_index;
+ kSATAHba = mem_ahci;
- goto success_hba_fetch;
- }
+ goto success_hba_fetch;
+ }
- ports_implemented >>= 1;
- ++ahci_index;
- }
- }
- }
+ ports_implemented >>= 1;
+ ++ahci_index;
+ }
+ }
+ }
- err_global_get() = kErrorDisk;
+ err_global_get() = kErrorDisk;
- return NO;
+ return NO;
success_hba_fetch:
- if (ahci_enable_and_probe())
- {
- err_global_get() = kErrorSuccess;
+ if (ahci_enable_and_probe()) {
+ err_global_get() = kErrorSuccess;
- return YES;
- }
+ return YES;
+ }
- return NO;
+ return NO;
}
/// @brief Checks if an AHCI device is detected.
/// @return Either if detected, or not found.
-Bool drv_std_detected_ahci()
-{
- return kSATADev.DeviceId() != (UShort)PCI::PciConfigKind::Invalid && kSATADev.Bar(kSATABar5) != 0;
+Bool drv_std_detected_ahci() {
+ return kSATADev.DeviceId() != (UShort) PCI::PciConfigKind::Invalid &&
+ kSATADev.Bar(kSATABar5) != 0;
}
// ================================================================================================
@@ -362,133 +338,121 @@ Bool drv_std_detected_ahci()
////////////////////////////////////////////////////
///
////////////////////////////////////////////////////
-Void drv_std_write(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer)
-{
- drv_std_input_output_ahci<YES, YES, NO>(lba, reinterpret_cast<UInt8*>(buffer), sector_sz, size_buffer);
+Void drv_std_write(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer) {
+ drv_std_input_output_ahci<YES, YES, NO>(lba, reinterpret_cast<UInt8*>(buffer), sector_sz,
+ size_buffer);
}
////////////////////////////////////////////////////
///
////////////////////////////////////////////////////
-Void drv_std_read(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer)
-{
- drv_std_input_output_ahci<NO, YES, NO>(lba, reinterpret_cast<UInt8*>(buffer), sector_sz, size_buffer);
+Void drv_std_read(UInt64 lba, Char* buffer, SizeT sector_sz, SizeT size_buffer) {
+ drv_std_input_output_ahci<NO, YES, NO>(lba, reinterpret_cast<UInt8*>(buffer), sector_sz,
+ size_buffer);
}
////////////////////////////////////////////////////
///
////////////////////////////////////////////////////
-Bool drv_std_init(UInt16& pi)
-{
- BOOL atapi = NO;
- return drv_std_init_ahci(pi, atapi);
+Bool drv_std_init(UInt16& pi) {
+ BOOL atapi = NO;
+ return drv_std_init_ahci(pi, atapi);
}
////////////////////////////////////////////////////
///
////////////////////////////////////////////////////
-Bool drv_std_detected(Void)
-{
- return drv_std_detected_ahci();
+Bool drv_std_detected(Void) {
+ return drv_std_detected_ahci();
}
////////////////////////////////////////////////////
/**
- @brief Gets the number of sectors inside the drive.
- @return Sector size in bytes.
+ @brief Gets the number of sectors inside the drive.
+ @return Sector size in bytes.
*/
////////////////////////////////////////////////////
-SizeT drv_get_sector_count()
-{
- return drv_get_sector_count_ahci();
+SizeT drv_get_sector_count() {
+ return drv_get_sector_count_ahci();
}
////////////////////////////////////////////////////
/// @brief Get the drive size.
/// @return Disk size in bytes.
////////////////////////////////////////////////////
-SizeT drv_get_size()
-{
- return drv_get_size_ahci();
+SizeT drv_get_size() {
+ return drv_get_size_ahci();
}
-#endif // ifdef __AHCI__
+#endif // ifdef __AHCI__
-namespace Kernel
-{
- /// @brief Initialize an AHCI device (StorageKit)
- UInt16 sk_init_ahci_device(BOOL atapi)
- {
- UInt16 pi = 0;
+namespace Kernel {
+/// @brief Initialize an AHCI device (StorageKit)
+UInt16 sk_init_ahci_device(BOOL atapi) {
+ UInt16 pi = 0;
- if (drv_std_init_ahci(pi, atapi))
- kSATAPortsImplemented = pi;
+ if (drv_std_init_ahci(pi, atapi)) kSATAPortsImplemented = pi;
- return pi;
- }
+ return pi;
+}
- /// @brief Implementation details namespace.
- namespace Detail
- {
- /// @brief Read AHCI device.
- /// @param self device
- /// @param mnt mounted disk.
- STATIC Void sk_io_read_ahci(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt)
- {
- AHCIDeviceInterface* dev = (AHCIDeviceInterface*)self;
+/// @brief Implementation details namespace.
+namespace Detail {
+ /// @brief Read AHCI device.
+ /// @param self device
+ /// @param mnt mounted disk.
+ STATIC Void sk_io_read_ahci(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) {
+ AHCIDeviceInterface* dev = (AHCIDeviceInterface*) self;
- err_global_get() = kErrorDisk;
+ err_global_get() = kErrorDisk;
- if (!dev)
- return;
+ if (!dev) return;
- auto disk = mnt->GetAddressOf(dev->GetIndex());
+ auto disk = mnt->GetAddressOf(dev->GetIndex());
- if (!disk)
- return;
+ if (!disk) return;
- err_global_get() = kErrorSuccess;
+ err_global_get() = kErrorSuccess;
- drv_std_input_output_ahci<NO, YES, NO>(disk->fPacket.fPacketLba, (UInt8*)disk->fPacket.fPacketContent, kAHCISectorSize, disk->fPacket.fPacketSize);
- }
+ drv_std_input_output_ahci<NO, YES, NO>(disk->fPacket.fPacketLba,
+ (UInt8*) disk->fPacket.fPacketContent, kAHCISectorSize,
+ disk->fPacket.fPacketSize);
+ }
- /// @brief Write AHCI device.
- /// @param self device
- /// @param mnt mounted disk.
- STATIC Void sk_io_write_ahci(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt)
- {
- AHCIDeviceInterface* dev = (AHCIDeviceInterface*)self;
+ /// @brief Write AHCI device.
+ /// @param self device
+ /// @param mnt mounted disk.
+ STATIC Void sk_io_write_ahci(IDeviceObject<MountpointInterface*>* self,
+ MountpointInterface* mnt) {
+ AHCIDeviceInterface* dev = (AHCIDeviceInterface*) self;
- err_global_get() = kErrorDisk;
+ err_global_get() = kErrorDisk;
- if (!dev)
- return;
+ if (!dev) return;
- auto disk = mnt->GetAddressOf(dev->GetIndex());
+ auto disk = mnt->GetAddressOf(dev->GetIndex());
- if (!disk)
- return;
+ if (!disk) return;
- err_global_get() = kErrorSuccess;
+ err_global_get() = kErrorSuccess;
- drv_std_input_output_ahci<YES, YES, NO>(disk->fPacket.fPacketLba, (UInt8*)disk->fPacket.fPacketContent, kAHCISectorSize, disk->fPacket.fPacketSize);
- }
- } // namespace Detail
+ drv_std_input_output_ahci<YES, YES, NO>(disk->fPacket.fPacketLba,
+ (UInt8*) disk->fPacket.fPacketContent, kAHCISectorSize,
+ disk->fPacket.fPacketSize);
+ }
+} // namespace Detail
- /// @brief Acquires a new AHCI device with drv_index in mind.
- /// @param drv_index The drive index to assign.
- /// @return A wrapped device interface if successful, or error code.
- ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index)
- {
- if (!drv_std_detected_ahci())
- return ErrorOr<AHCIDeviceInterface>(kErrorDisk);
+/// @brief Acquires a new AHCI device with drv_index in mind.
+/// @param drv_index The drive index to assign.
+/// @return A wrapped device interface if successful, or error code.
+ErrorOr<AHCIDeviceInterface> sk_acquire_ahci_device(Int32 drv_index) {
+ if (!drv_std_detected_ahci()) return ErrorOr<AHCIDeviceInterface>(kErrorDisk);
- AHCIDeviceInterface device(Detail::sk_io_read_ahci,
- Detail::sk_io_write_ahci);
+ AHCIDeviceInterface device(Detail::sk_io_read_ahci, Detail::sk_io_write_ahci);
- device.SetPortsImplemented(kSATAPortsImplemented);
- device.SetIndex(drv_index);
+ device.SetPortsImplemented(kSATAPortsImplemented);
+ device.SetIndex(drv_index);
- return ErrorOr<AHCIDeviceInterface>(device);
- }
-} // namespace Kernel
+ return ErrorOr<AHCIDeviceInterface>(device);
+}
+} // namespace Kernel
diff --git a/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc
index f04d25eb..a7361778 100644
--- a/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc
+++ b/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc
@@ -1,6 +1,6 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
@@ -15,9 +15,9 @@
*
*/
-#include <modules/ATA/ATA.h>
#include <ArchKit/ArchKit.h>
#include <KernelKit/PCI/Iterator.h>
+#include <modules/ATA/ATA.h>
#if defined(__ATA_DMA__)
@@ -28,185 +28,165 @@ using namespace Kernel::HAL;
/// BUGS: 0
-STATIC Boolean kATADetected = false;
-STATIC Int32 kATADeviceType = kATADeviceCount;
+STATIC Boolean kATADetected = false;
+STATIC Int32 kATADeviceType = kATADeviceCount;
STATIC UInt16 kATAIdentifyData[kATADataLen] = {0};
STATIC Kernel::PCI::Device kATADevice;
-STATIC Char kATADiskModel[50] = {"GENERIC DMA"};
+STATIC Char kATADiskModel[50] = {"GENERIC DMA"};
-Boolean drv_std_wait_io(UInt16 IO)
-{
- for (int i = 0; i < 400; i++)
- rt_in8(IO + ATA_REG_STATUS);
+Boolean drv_std_wait_io(UInt16 IO) {
+ for (int i = 0; i < 400; i++) rt_in8(IO + ATA_REG_STATUS);
ATAWaitForIO_Retry:
- auto status_rdy = rt_in8(IO + ATA_REG_STATUS);
+ auto status_rdy = rt_in8(IO + ATA_REG_STATUS);
- if ((status_rdy & ATA_SR_BSY))
- goto ATAWaitForIO_Retry;
+ if ((status_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry;
ATAWaitForIO_Retry2:
- status_rdy = rt_in8(IO + ATA_REG_STATUS);
+ status_rdy = rt_in8(IO + ATA_REG_STATUS);
- if (status_rdy & ATA_SR_ERR)
- return false;
+ if (status_rdy & ATA_SR_ERR) return false;
- if (!(status_rdy & ATA_SR_DRDY))
- goto ATAWaitForIO_Retry2;
+ if (!(status_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2;
- return true;
+ return true;
}
-Void drv_std_select(UInt16 Bus)
-{
- if (Bus == ATA_PRIMARY_IO)
- rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL);
- else
- rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL);
+Void drv_std_select(UInt16 Bus) {
+ if (Bus == ATA_PRIMARY_IO)
+ rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL);
+ else
+ rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL);
}
-Boolean drv_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster)
-{
- PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController);
-
- for (SizeT device_index = 0; device_index < NE_BUS_COUNT; ++device_index)
- {
- kATADevice = iterator[device_index].Leak(); // And then leak the reference.
+Boolean drv_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) {
+ PCI::Iterator iterator(Types::PciDeviceKind::MassStorageController);
- /// IDE interface
- if (kATADevice.Subclass() == 0x01)
- {
+ for (SizeT device_index = 0; device_index < NE_BUS_COUNT; ++device_index) {
+ kATADevice = iterator[device_index].Leak(); // And then leak the reference.
- break;
- }
- }
+ /// IDE interface
+ if (kATADevice.Subclass() == 0x01) {
+ break;
+ }
+ }
- return NO;
+ return NO;
}
-namespace Kernel::Detail
-{
- struct PRDEntry final
- {
- UInt32 mAddress;
- UInt16 mByteCount;
- UInt16 mFlags; /// @param PRD flags, set to 0x8000 to indicate end of prd.
- };
-} // namespace Kernel::Detail
+namespace Kernel::Detail {
+struct PRDEntry final {
+ UInt32 mAddress;
+ UInt16 mByteCount;
+ UInt16 mFlags; /// @param PRD flags, set to 0x8000 to indicate end of prd.
+};
+} // namespace Kernel::Detail
static UIntPtr kReadAddr = mib_cast(2);
static UIntPtr kWriteAddr = mib_cast(2) + kib_cast(64);
-Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size)
-{
- Lba /= SectorSz;
+Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) {
+ Lba /= SectorSz;
- if (Size > kib_cast(64))
- return;
+ if (Size > kib_cast(64)) return;
- UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
+ UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
- rt_copy_memory((VoidPtr)Buf, (VoidPtr)kReadAddr, Size);
+ rt_copy_memory((VoidPtr) Buf, (VoidPtr) kReadAddr, Size);
- drv_std_select(IO);
+ drv_std_select(IO);
- rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
+ rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
- rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz - 1) / SectorSz));
+ rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz - 1) / SectorSz));
- rt_out8(IO + ATA_REG_LBA0, (Lba)&0xFF);
- rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
- rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
- rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
+ rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF);
+ rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
+ rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
+ rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
- Kernel::Detail::PRDEntry* prd = (Kernel::Detail::PRDEntry*)(kATADevice.Bar(0x20) + 4); // The PRDEntry is not correct.
+ Kernel::Detail::PRDEntry* prd =
+ (Kernel::Detail::PRDEntry*) (kATADevice.Bar(0x20) + 4); // The PRDEntry is not correct.
- prd->mAddress = (UInt32)(UIntPtr)kReadAddr;
- prd->mByteCount = Size - 1;
- prd->mFlags = 0x8000; // indicate the end of prd.
+ prd->mAddress = (UInt32) (UIntPtr) kReadAddr;
+ prd->mByteCount = Size - 1;
+ prd->mFlags = 0x8000; // indicate the end of prd.
- rt_out32(kATADevice.Bar(0x20) + 0x04, (UInt32)(UIntPtr)prd);
+ rt_out32(kATADevice.Bar(0x20) + 0x04, (UInt32) (UIntPtr) prd);
- rt_out8(kATADevice.Bar(0x20) + ATA_REG_COMMAND, ATA_CMD_READ_DMA);
+ rt_out8(kATADevice.Bar(0x20) + ATA_REG_COMMAND, ATA_CMD_READ_DMA);
- rt_out8(kATADevice.Bar(0x20) + 0x00, 0x09); // Start DMA engine
+ rt_out8(kATADevice.Bar(0x20) + 0x00, 0x09); // Start DMA engine
- while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01)
- ;
+ while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01);
- rt_out8(kATADevice.Bar(0x20) + 0x00, 0x00); // Stop DMA engine
+ rt_out8(kATADevice.Bar(0x20) + 0x00, 0x00); // Stop DMA engine
- rt_copy_memory((VoidPtr)kReadAddr, (VoidPtr)Buf, Size);
+ rt_copy_memory((VoidPtr) kReadAddr, (VoidPtr) Buf, Size);
- delete prd;
- prd = nullptr;
+ delete prd;
+ prd = nullptr;
}
-Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size)
-{
- Lba /= SectorSz;
+Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) {
+ Lba /= SectorSz;
- if (Size > kib_cast(64))
- return;
+ if (Size > kib_cast(64)) return;
- UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
+ UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
- rt_copy_memory((VoidPtr)Buf, (VoidPtr)kWriteAddr, Size);
+ rt_copy_memory((VoidPtr) Buf, (VoidPtr) kWriteAddr, Size);
- rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
+ rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
- rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz - 1)) / SectorSz));
+ rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz - 1)) / SectorSz));
- rt_out8(IO + ATA_REG_LBA0, (Lba)&0xFF);
- rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
- rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
- rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
+ rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF);
+ rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
+ rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
+ rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
- Kernel::Detail::PRDEntry* prd = (Kernel::Detail::PRDEntry*)(kATADevice.Bar(0x20) + 4);
+ Kernel::Detail::PRDEntry* prd = (Kernel::Detail::PRDEntry*) (kATADevice.Bar(0x20) + 4);
- prd->mAddress = (UInt32)(UIntPtr)kWriteAddr;
- prd->mByteCount = Size - 1;
- prd->mFlags = 0x8000;
+ prd->mAddress = (UInt32) (UIntPtr) kWriteAddr;
+ prd->mByteCount = Size - 1;
+ prd->mFlags = 0x8000;
- rt_out32(kATADevice.Bar(0x20) + 0x04, (UInt32)(UIntPtr)prd);
- rt_out8(kATADevice.Bar(0x20) + ATA_REG_COMMAND, ATA_CMD_WRITE_DMA);
+ rt_out32(kATADevice.Bar(0x20) + 0x04, (UInt32) (UIntPtr) prd);
+ rt_out8(kATADevice.Bar(0x20) + ATA_REG_COMMAND, ATA_CMD_WRITE_DMA);
- rt_out8(IO + 0x00, 0x09); // Start DMA engine
+ rt_out8(IO + 0x00, 0x09); // Start DMA engine
- while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01)
- ;
+ while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01);
- rt_out8(kATADevice.Bar(0x20) + 0x00, 0x00); // Stop DMA engine
+ rt_out8(kATADevice.Bar(0x20) + 0x00, 0x00); // Stop DMA engine
- delete prd;
- prd = nullptr;
+ delete prd;
+ prd = nullptr;
}
/***********************************************************************************/
/// @brief Is ATA detected?
/***********************************************************************************/
-Boolean drv_std_detected(Void)
-{
- return kATADetected;
+Boolean drv_std_detected(Void) {
+ return kATADetected;
}
/***********************************************************************************/
/***
- @brief Gets the number of sectors inside the drive.
- @return Number of sectors, or zero.
+ @brief Gets the number of sectors inside the drive.
+ @return Number of sectors, or zero.
*/
/***********************************************************************************/
-Kernel::SizeT drv_get_sector_count()
-{
- return (kATAIdentifyData[61] << 16) | kATAIdentifyData[60];
+Kernel::SizeT drv_get_sector_count() {
+ return (kATAIdentifyData[61] << 16) | kATAIdentifyData[60];
}
/***********************************************************************************/
/// @brief Get the size of the current drive.
/***********************************************************************************/
-Kernel::SizeT drv_get_size()
-{
- return (drv_get_sector_count()) * kATASectorSize;
+Kernel::SizeT drv_get_size() {
+ return (drv_get_sector_count()) * kATASectorSize;
}
#endif /* ifdef __ATA_DMA__ */
diff --git a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc
index cd25ab7f..6fb1d8a3 100644
--- a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc
+++ b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc
@@ -1,6 +1,6 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
@@ -15,10 +15,10 @@
*
*/
-#include <modules/ATA/ATA.h>
#include <ArchKit/ArchKit.h>
#include <KernelKit/DriveMgr.h>
#include <StorageKit/ATA.h>
+#include <modules/ATA/ATA.h>
using namespace Kernel;
using namespace Kernel::HAL;
@@ -27,264 +27,234 @@ using namespace Kernel::HAL;
#define kATADataLen 256
-STATIC Boolean kATADetected = false;
+STATIC Boolean kATADetected = false;
STATIC UInt16 kATAIdentifyData[kATADataLen] = {0};
-STATIC Char kATADiskModel[50] = {"GENERIC PIO"};
+STATIC Char kATADiskModel[50] = {"GENERIC PIO"};
-static Boolean drv_pio_std_wait_io(UInt16 IO)
-{
- for (int i = 0; i < 400; i++)
- rt_in8(IO + ATA_REG_STATUS);
+static Boolean drv_pio_std_wait_io(UInt16 IO) {
+ for (int i = 0; i < 400; i++) rt_in8(IO + ATA_REG_STATUS);
ATAWaitForIO_Retry:
- auto stat_rdy = rt_in8(IO + ATA_REG_STATUS);
+ auto stat_rdy = rt_in8(IO + ATA_REG_STATUS);
- if ((stat_rdy & ATA_SR_BSY))
- goto ATAWaitForIO_Retry;
+ if ((stat_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry;
ATAWaitForIO_Retry2:
- stat_rdy = rt_in8(IO + ATA_REG_STATUS);
+ stat_rdy = rt_in8(IO + ATA_REG_STATUS);
- if (stat_rdy & ATA_SR_ERR)
- return false;
+ if (stat_rdy & ATA_SR_ERR) return false;
- if (!(stat_rdy & ATA_SR_DRDY))
- goto ATAWaitForIO_Retry2;
+ if (!(stat_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2;
- return true;
+ return true;
}
-static Void drv_pio_std_select(UInt16 Bus)
-{
- if (Bus == ATA_PRIMARY_IO)
- rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL);
- else
- rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL);
+static Void drv_pio_std_select(UInt16 Bus) {
+ if (Bus == ATA_PRIMARY_IO)
+ rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL);
+ else
+ rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL);
}
-Boolean drv_pio_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster)
-{
- UInt16 IO = Bus;
+Boolean drv_pio_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) {
+ UInt16 IO = Bus;
- NE_UNUSED(Drive);
+ NE_UNUSED(Drive);
- drv_pio_std_select(IO);
+ drv_pio_std_select(IO);
- // Bus init, NEIN bit.
- rt_out8(IO + ATA_REG_NEIN, 1);
+ // Bus init, NEIN bit.
+ rt_out8(IO + ATA_REG_NEIN, 1);
- // identify until it's good.
+ // identify until it's good.
ATAInit_Retry:
- auto stat_rdy = rt_in8(IO + ATA_REG_STATUS);
+ auto stat_rdy = rt_in8(IO + ATA_REG_STATUS);
- if (stat_rdy & ATA_SR_ERR)
- {
- return false;
- }
+ if (stat_rdy & ATA_SR_ERR) {
+ return false;
+ }
- if ((stat_rdy & ATA_SR_BSY))
- goto ATAInit_Retry;
+ if ((stat_rdy & ATA_SR_BSY)) goto ATAInit_Retry;
- OutBus = (Bus == ATA_PRIMARY_IO) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO;
- OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE;
+ OutBus = (Bus == ATA_PRIMARY_IO) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO;
+ OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE;
- rt_out8(OutBus + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
+ rt_out8(OutBus + ATA_REG_COMMAND, ATA_CMD_IDENTIFY);
- drv_pio_std_wait_io(IO);
+ drv_pio_std_wait_io(IO);
- /// fetch serial info
- /// model, speed, number of sectors...
+ /// fetch serial info
+ /// model, speed, number of sectors...
- for (SizeT i = 0ul; i < kATADataLen; ++i)
- {
- kATAIdentifyData[i] = HAL::rt_in16(OutBus + ATA_REG_DATA);
- }
+ for (SizeT i = 0ul; i < kATADataLen; ++i) {
+ kATAIdentifyData[i] = HAL::rt_in16(OutBus + ATA_REG_DATA);
+ }
- for (Int32 i = 0; i < 20; i++)
- {
- kATADiskModel[i * 2] = (kATAIdentifyData[27 + i] >> 8) & 0xFF;
- kATADiskModel[i * 2 + 1] = kATAIdentifyData[27 + i] & 0xFF;
- }
+ for (Int32 i = 0; i < 20; i++) {
+ kATADiskModel[i * 2] = (kATAIdentifyData[27 + i] >> 8) & 0xFF;
+ kATADiskModel[i * 2 + 1] = kATAIdentifyData[27 + i] & 0xFF;
+ }
- kATADiskModel[40] = '\0';
+ kATADiskModel[40] = '\0';
- (Void)(kout << "Drive Model: " << kATADiskModel << kendl);
+ (Void)(kout << "Drive Model: " << kATADiskModel << kendl);
- return true;
+ return true;
}
-Void drv_pio_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size)
-{
- Lba /= SectorSz;
+Void drv_pio_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) {
+ Lba /= SectorSz;
- UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
+ UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
- drv_pio_std_wait_io(IO);
- drv_pio_std_select(IO);
+ drv_pio_std_wait_io(IO);
+ drv_pio_std_select(IO);
- rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
+ rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
- rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz));
+ rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz));
- rt_out8(IO + ATA_REG_LBA0, (Lba)&0xFF);
- rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
- rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
- rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
+ rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF);
+ rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
+ rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
+ rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
- rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
+ rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO);
- for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff)
- {
- drv_pio_std_wait_io(IO);
- Buf[IndexOff] = HAL::rt_in16(IO + ATA_REG_DATA);
- }
+ for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) {
+ drv_pio_std_wait_io(IO);
+ Buf[IndexOff] = HAL::rt_in16(IO + ATA_REG_DATA);
+ }
}
-Void drv_pio_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size)
-{
- Lba /= SectorSz;
+Void drv_pio_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) {
+ Lba /= SectorSz;
- UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
+ UInt8 Command = ((!Master) ? 0xE0 : 0xF0);
- drv_pio_std_wait_io(IO);
- drv_pio_std_select(IO);
+ drv_pio_std_wait_io(IO);
+ drv_pio_std_select(IO);
- rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
+ rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F));
- rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz));
+ rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz));
- rt_out8(IO + ATA_REG_LBA0, (Lba)&0xFF);
- rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
- rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
- rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
+ rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF);
+ rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8);
+ rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16);
+ rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24);
- rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
+ rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO);
- for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff)
- {
- drv_pio_std_wait_io(IO);
- rt_out16(IO + ATA_REG_DATA, Buf[IndexOff]);
- }
+ for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) {
+ drv_pio_std_wait_io(IO);
+ rt_out16(IO + ATA_REG_DATA, Buf[IndexOff]);
+ }
}
/// @brief is ATA detected?
-Boolean drv_pio_std_detected(Void)
-{
- return kATADetected;
+Boolean drv_pio_std_detected(Void) {
+ return kATADetected;
}
/***
- @brief Getter, gets the number of sectors inside the drive.
+ @brief Getter, gets the number of sectors inside the drive.
*/
-SizeT drv_pio_get_sector_count()
-{
- return (kATAIdentifyData[61] << 16) | kATAIdentifyData[60];
+SizeT drv_pio_get_sector_count() {
+ return (kATAIdentifyData[61] << 16) | kATAIdentifyData[60];
}
/// @brief Get the drive size.
-SizeT drv_pio_get_size()
-{
- return (drv_pio_get_sector_count()) * kATASectorSize;
+SizeT drv_pio_get_size() {
+ return (drv_pio_get_sector_count()) * kATASectorSize;
}
-namespace Kernel
-{
- /// @brief Initialize an PIO device (StorageKit function)
- /// @param is_master is the current PIO master?
- /// @return [io:master] for PIO device.
- BOOL sk_init_pio_device(BOOL is_master, UInt16& io, UInt8& master)
- {
- return drv_pio_std_init(ATA_SECONDARY_IO, is_master, io, master);
- }
+namespace Kernel {
+/// @brief Initialize an PIO device (StorageKit function)
+/// @param is_master is the current PIO master?
+/// @return [io:master] for PIO device.
+BOOL sk_init_pio_device(BOOL is_master, UInt16& io, UInt8& master) {
+ return drv_pio_std_init(ATA_SECONDARY_IO, is_master, io, master);
+}
- /// @brief Implementation details namespace.
- namespace Detail
- {
- /// @brief Read PIO device.
- /// @param self device
- /// @param mnt mounted disk.
- STATIC Void sk_io_read_pio(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt)
- {
- ATADeviceInterface* dev = (ATADeviceInterface*)self;
+/// @brief Implementation details namespace.
+namespace Detail {
+ /// @brief Read PIO device.
+ /// @param self device
+ /// @param mnt mounted disk.
+ STATIC Void sk_io_read_pio(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) {
+ ATADeviceInterface* dev = (ATADeviceInterface*) self;
- err_global_get() = kErrorDisk;
+ err_global_get() = kErrorDisk;
- if (!dev)
- return;
+ if (!dev) return;
- auto disk = mnt->GetAddressOf(dev->GetIndex());
+ auto disk = mnt->GetAddressOf(dev->GetIndex());
- if (!disk)
- return;
+ if (!disk) return;
- err_global_get() = kErrorSuccess;
+ err_global_get() = kErrorSuccess;
- drv_pio_std_read(disk->fPacket.fPacketLba, dev->GetIO(), dev->GetMaster(), (Char*)disk->fPacket.fPacketContent, kATASectorSize, disk->fPacket.fPacketSize);
- }
+ drv_pio_std_read(disk->fPacket.fPacketLba, dev->GetIO(), dev->GetMaster(),
+ (Char*) disk->fPacket.fPacketContent, kATASectorSize,
+ disk->fPacket.fPacketSize);
+ }
- /// @brief Write PIO device.
- /// @param self device
- /// @param mnt mounted disk.
- STATIC Void sk_io_write_pio(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt)
- {
- ATADeviceInterface* dev = (ATADeviceInterface*)self;
+ /// @brief Write PIO device.
+ /// @param self device
+ /// @param mnt mounted disk.
+ STATIC Void sk_io_write_pio(IDeviceObject<MountpointInterface*>* self, MountpointInterface* mnt) {
+ ATADeviceInterface* dev = (ATADeviceInterface*) self;
- err_global_get() = kErrorDisk;
+ err_global_get() = kErrorDisk;
- if (!dev)
- return;
+ if (!dev) return;
- auto disk = mnt->GetAddressOf(dev->GetIndex());
+ auto disk = mnt->GetAddressOf(dev->GetIndex());
- if (!disk)
- return;
+ if (!disk) return;
- err_global_get() = kErrorSuccess;
+ err_global_get() = kErrorSuccess;
- drv_pio_std_write(disk->fPacket.fPacketLba, dev->GetIO(), dev->GetMaster(), (Char*)disk->fPacket.fPacketContent, kATASectorSize, disk->fPacket.fPacketSize);
- }
- } // namespace Detail
+ drv_pio_std_write(disk->fPacket.fPacketLba, dev->GetIO(), dev->GetMaster(),
+ (Char*) disk->fPacket.fPacketContent, kATASectorSize,
+ disk->fPacket.fPacketSize);
+ }
+} // namespace Detail
- /// @brief Acquires a new PIO device with drv_index in mind.
- /// @param drv_index The drive index to assign.
- /// @return A wrapped device interface if successful, or error code.
- ErrorOr<ATADeviceInterface> sk_acquire_pio_device(Int32 drv_index)
- {
- /// here we don't check if we probed ATA, since we'd need to grab IO after that.
- ATADeviceInterface device(Detail::sk_io_read_pio,
- Detail::sk_io_write_pio);
+/// @brief Acquires a new PIO device with drv_index in mind.
+/// @param drv_index The drive index to assign.
+/// @return A wrapped device interface if successful, or error code.
+ErrorOr<ATADeviceInterface> sk_acquire_pio_device(Int32 drv_index) {
+ /// here we don't check if we probed ATA, since we'd need to grab IO after that.
+ ATADeviceInterface device(Detail::sk_io_read_pio, Detail::sk_io_write_pio);
- device.SetIndex(drv_index);
+ device.SetIndex(drv_index);
- return ErrorOr<ATADeviceInterface>(device);
- }
-} // namespace Kernel
+ return ErrorOr<ATADeviceInterface>(device);
+}
+} // namespace Kernel
#ifdef __ATA_PIO__
-Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size)
-{
- drv_pio_std_read(Lba, IO, Master, Buf, SectorSz, Size);
+Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) {
+ drv_pio_std_read(Lba, IO, Master, Buf, SectorSz, Size);
}
-Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size)
-{
- drv_pio_std_write(Lba, IO, Master, Buf, SectorSz, Size);
+Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz, SizeT Size) {
+ drv_pio_std_write(Lba, IO, Master, Buf, SectorSz, Size);
}
-SizeT drv_get_size()
-{
- return drv_pio_get_size();
+SizeT drv_get_size() {
+ return drv_pio_get_size();
}
-SizeT drv_get_sector_count()
-{
- return drv_pio_get_sector_count();
+SizeT drv_get_sector_count() {
+ return drv_pio_get_sector_count();
}
-Boolean drv_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster)
-{
- return drv_pio_std_init(Bus, Drive, OutBus, OutMaster);
+Boolean drv_std_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) {
+ return drv_pio_std_init(Bus, Drive, OutBus, OutMaster);
}
#endif \ No newline at end of file
diff --git a/dev/kernel/HALKit/AMD64/Storage/SCSI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/SCSI+Generic.cc
index cb387e73..1cc97cba 100644
--- a/dev/kernel/HALKit/AMD64/Storage/SCSI+Generic.cc
+++ b/dev/kernel/HALKit/AMD64/Storage/SCSI+Generic.cc
@@ -1,6 +1,6 @@
/* -------------------------------------------
- Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
+ Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved.
------------------------------------------- */
@@ -9,5 +9,5 @@
using namespace Kernel;
///! @brief ATAPI SCSI packet.
-const ATTRIBUTE(unused) scsi_packet_type_12 kCDRomPacketTemplate = {0x43, 0, 1, 0, 0, 0,
- 0, 12, 0x40, 0, 0};
+const ATTRIBUTE(unused) scsi_packet_type_12 kCDRomPacketTemplate = {0x43, 0, 1, 0, 0, 0,
+ 0, 12, 0x40, 0, 0};