diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-02 08:37:13 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-02 08:37:13 +0100 |
| commit | 95a887d120b7955bb02f582339d0536696a4cc79 (patch) | |
| tree | 1a240736a98566fce758b57108a1e623a378c89f /Private/NewBoot | |
| parent | 7ceadad6f8d24e98098a00531b24fa2c89fb76ad (diff) | |
Kernel & Bootloader: Improvements and Fix ATA Read and Write problem.
Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Private/NewBoot')
| -rw-r--r-- | Private/NewBoot/BootKit/Arch/ATA.hxx | 18 | ||||
| -rw-r--r-- | Private/NewBoot/BootKit/BootKit.hxx | 16 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/ATA.cxx | 165 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx | 2 |
4 files changed, 134 insertions, 67 deletions
diff --git a/Private/NewBoot/BootKit/Arch/ATA.hxx b/Private/NewBoot/BootKit/Arch/ATA.hxx index 6e692c3d..db92e290 100644 --- a/Private/NewBoot/BootKit/Arch/ATA.hxx +++ b/Private/NewBoot/BootKit/Arch/ATA.hxx @@ -113,8 +113,10 @@ using namespace HCore; Boolean ATAInitDriver(UInt8 Bus, UInt8 Drv); Void ATAWait(UInt16 IO); -UInt16 ATAReadLba(UInt32 Lba, UInt8 Bus, Boolean Master); -Void ATAWriteLba(UInt16 Byte, UInt32 Lba, UInt8 Bus, Boolean Master); +Void ATAReadLba(UInt32 Lba, UInt8 Bus, Boolean Master, wchar_t* Buf, + SizeT Offset); +Void ATAWriteLba(UInt16 Byte, UInt32 Lba, UInt8 Bus, Boolean Master, + wchar_t* Buf, SizeT Offset); Boolean ATAIsDetected(Void); class BATADevice final { @@ -134,11 +136,21 @@ class BATADevice final { Boolean mMaster{false}; }; + operator bool() { return ATAIsDetected(); } + BATADevice& Read(WideChar*, const SizeT&); BATADevice& Write(WideChar*, const SizeT&); - ATATraits& Traits(); + ATATraits& Leak(); private: ATATraits mTraits; }; + +enum { + kATADevicePATA, + kATADeviceSATA, + kATADevicePATA_PI, + kATADeviceSATA_PI, + kATADeviceCount, +}; diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index edd8015b..2b57ec56 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -93,35 +93,39 @@ class BFileReader final { #include <BootKit/Platform.hxx> #include <BootKit/Protocol.hxx> +/***********************************************************************************/ +/// Provide some useful processor features. +/***********************************************************************************/ + #ifdef __EFI_x86_64__ -inline void out8(UInt16 port, UInt8 value) { +inline void Out8(UInt16 port, UInt8 value) { asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); } -inline void out16(UInt16 port, UInt16 value) { +inline void Out16(UInt16 port, UInt16 value) { asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); } -inline void out32(UInt16 port, UInt32 value) { +inline void Out32(UInt16 port, UInt32 value) { asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); } -inline UInt8 in8(UInt16 port) { +inline UInt8 In8(UInt16 port) { UInt8 value = 0UL; asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); return value; } -inline UInt16 in16(UInt16 port) { +inline UInt16 In16(UInt16 port) { UInt16 value = 0UL; asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); return value; } -inline UInt32 in32(UInt16 port) { +inline UInt32 In32(UInt16 port) { UInt32 value = 0UL; asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); diff --git a/Private/NewBoot/Source/HEL/AMD64/ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx index 0f8d9170..fb11f409 100644 --- a/Private/NewBoot/Source/HEL/AMD64/ATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx @@ -7,21 +7,31 @@ * ======================================================== */ +/** + * @file ATA.cxx + * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) + * @brief ATA driver. + * @version 0.1 + * @date 2024-02-02 + * + * @copyright Copyright (c) 2024 + * + */ + #include <BootKit/Arch/ATA.hxx> #include <BootKit/BootKit.hxx> -/***********************************************************************************/ -/// Provide some useful processor features. -/***********************************************************************************/ +/// bugs: 0 static Boolean kATADetected = false; +static Int32 kATADeviceType = kATADeviceCount; void ATASelect(UInt8 Bus, Boolean isMaster) { if (Bus == ATA_PRIMARY) - out8(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, + Out8(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); else - out8(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, + Out8(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); } @@ -33,14 +43,14 @@ Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive, UInt16& OutBus, ATASelect(Bus, Drive); - out8(IO + ATA_REG_SEC_COUNT0, 0); - out8(IO + ATA_REG_LBA0, 0); - out8(IO + ATA_REG_LBA1, 0); - out8(IO + ATA_REG_LBA2, 0); + Out8(IO + ATA_REG_SEC_COUNT0, 0); + Out8(IO + ATA_REG_LBA0, 0); + Out8(IO + ATA_REG_LBA1, 0); + Out8(IO + ATA_REG_LBA2, 0); - out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - UInt8 status = in8(IO + ATA_REG_STATUS); + UInt8 status = In8(IO + ATA_REG_STATUS); if (status & ATA_SR_ERR) { #ifdef __DEBUG__ @@ -52,17 +62,26 @@ Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive, UInt16& OutBus, OutBus = (Bus == ATA_PRIMARY) ? BATADevice::kPrimary : BATADevice::kSecondary; OutMaster = (Bus == ATA_PRIMARY); - unsigned cl = in16(Bus + ATA_CYL_LOW); /* get the "signature bytes" */ - unsigned ch = in16(Bus + ATA_CYL_HIGH); + unsigned cl = In16(Bus + ATA_CYL_LOW); /* get the "signature bytes" */ + unsigned ch = In16(Bus + ATA_CYL_HIGH); /* differentiate ATA, ATAPI, SATA and SATAPI */ - if (cl == 0x14 && ch == 0xEB) - writer.WriteString(L"HCoreLdr: PATAPI detected.\r\n"); - if (cl == 0x69 && ch == 0x96) - writer.WriteString(L"HCoreLdr: SATAPI detected.\r\n"); - if (cl == 0 && ch == 0) writer.WriteString(L"HCoreLdr: PATA detected.\r\n"); - if (cl == 0x3c && ch == 0xc3) - writer.WriteString(L"HCoreLdr: SATA detected.\r\n"); + if (cl == 0x14 && ch == 0xEB) { + writer.WriteString(L"HCoreLdr: PATAPI drive detected.\r\n"); + kATADeviceType = kATADevicePATA_PI; + } + if (cl == 0x69 && ch == 0x96) { + writer.WriteString(L"HCoreLdr: SATAPI drive detected.\r\n"); + kATADeviceType = kATADeviceSATA_PI; + } + if (cl == 0 && ch == 0) { + writer.WriteString(L"HCoreLdr: PATA drive detected.\r\n"); + kATADeviceType = kATADevicePATA; + } + if (cl == 0x3c && ch == 0xc3) { + writer.WriteString(L"HCoreLdr: SATA drive detected.\r\n"); + kATADeviceType = kATADeviceSATA; + } return true; } @@ -71,50 +90,71 @@ Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive, UInt16& OutBus, * This polls the ATA drive. */ void ATAWait(UInt16 IO) { - for (int i = 0; i < 4000; i++) in8(IO + ATA_REG_ALT_STATUS); + for (int i = 0; i < 4000; i++) In8(IO + ATA_REG_ALT_STATUS); } void ATAPoll(UInt16 IO) { ATAWait(IO); } -UInt16 ATAReadLba(UInt32 Lba, UInt8 Bus, Boolean Master) { +Void ATAReadLba(UInt32 Lba, UInt8 Bus, Boolean Master, CharacterType* Buf, + SizeT Offset) { UInt16 IO = Bus; - ATASelect(IO, Master ? ATA_MASTER : ATA_SLAVE); - out8(IO + ATA_REG_SEC_COUNT0, Lba / 512); + ATASelect(IO + ATA_REG_HDDEVSEL, + (Master ? ATA_MASTER : ATA_SLAVE) | Lba >> 24 & 0xF); + + ATASelect(IO + 1, 0); - out8(IO + ATA_REG_LBA0, (UInt8)Lba); - out8(IO + ATA_REG_LBA1, (UInt8)(Lba >> 8)); - out8(IO + ATA_REG_LBA2, (UInt8)(Lba >> 16)); + Out8(IO + ATA_REG_SEC_COUNT0, 1); - out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); + Out8(IO + ATA_REG_LBA0, (UInt8)Lba); + Out8(IO + ATA_REG_LBA1, (UInt8)(Lba >> 8)); + Out8(IO + ATA_REG_LBA2, (UInt8)(Lba >> 16)); - while ((in8(ATA_COMMAND(IO))) & 0x88) ATAWait(IO); + Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - auto chr = in8(IO + ATA_REG_DATA); + ATAPoll(IO); + + for (SizeT index = 0UL; index < 256; ++index) { + Buf[index + Offset] = In16(IO + ATA_REG_DATA); + } - while ((in8(ATA_COMMAND(IO))) & 0x88) ATAWait(IO); - return chr; + ATAWait(IO); } -Void ATAWriteLba(UInt16 Byte, UInt32 Lba, UInt8 Bus, Boolean Master) { +Void ATAWriteLba(UInt16 Byte, UInt32 Lba, UInt8 Bus, Boolean Master, + wchar_t* Buf, SizeT Offset) { UInt16 IO = Bus; - ATASelect(IO, Master ? ATA_MASTER : ATA_SLAVE); - out8(IO + ATA_REG_SEC_COUNT0, Lba / 512); + ATASelect(IO + ATA_REG_HDDEVSEL, + (Master ? ATA_MASTER : ATA_SLAVE) | Lba >> 24 & 0xF); - out8(IO + ATA_REG_LBA0, (UInt8)Lba); - out8(IO + ATA_REG_LBA1, (UInt8)(Lba >> 8)); - out8(IO + ATA_REG_LBA2, (UInt8)(Lba >> 16)); + ATASelect(IO + 1, 0); - out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); // TODO: support DMA + Out8(IO + ATA_REG_SEC_COUNT0, 1); - while ((in8(ATA_COMMAND(IO))) & 0x88) ATAWait(IO); + Out8(IO + ATA_REG_LBA0, (UInt8)Lba); + Out8(IO + ATA_REG_LBA1, (UInt8)(Lba >> 8)); + Out8(IO + ATA_REG_LBA2, (UInt8)(Lba >> 16)); - out32(IO + ATA_REG_DATA, Byte); + Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); + + ATAPoll(IO); + + for (SizeT index = 0UL; index < 256; ++index) { + Out16(IO + ATA_REG_DATA, Buf[index + Offset]); + } - while ((in8(ATA_COMMAND(IO))) & 0x88) ATAWait(IO); + ATAWait(IO); } +/*** + * + * + * ATA Device class. + * + * + */ + Boolean ATAIsDetected(Void) { return kATADetected; } /** @@ -124,14 +164,14 @@ Boolean ATAIsDetected(Void) { return kATADetected; } BATADevice::BATADevice() noexcept { if (ATAIsDetected()) return; - if (ATAInitDriver(ATA_PRIMARY, true, this->Traits().mBus, - this->Traits().mMaster) || - ATAInitDriver(ATA_PRIMARY, false, this->Traits().mBus, - this->Traits().mMaster) || - ATAInitDriver(ATA_SECONDARY, true, this->Traits().mBus, - this->Traits().mMaster) || - ATAInitDriver(ATA_SECONDARY, false, this->Traits().mBus, - this->Traits().mMaster)) { + if (ATAInitDriver(ATA_PRIMARY, true, this->Leak().mBus, + this->Leak().mMaster) || + ATAInitDriver(ATA_PRIMARY, false, this->Leak().mBus, + this->Leak().mMaster) || + ATAInitDriver(ATA_SECONDARY, true, this->Leak().mBus, + this->Leak().mMaster) || + ATAInitDriver(ATA_SECONDARY, false, this->Leak().mBus, + this->Leak().mMaster)) { kATADetected = true; BTextWriter writer; @@ -144,10 +184,14 @@ BATADevice& BATADevice::Read(CharacterType* Buf, const SizeT& Sz) { if (!Buf || Sz < 1) return *this; - for (SizeT i = 0UL; i < Sz; ++i) { - Buf[i] = ATAReadLba(this->Traits().mBase + i, this->Traits().mBus, - this->Traits().mMaster); + SizeT Off = 0; + + for (SizeT i = 0UL; i < (Sz / 512); ++i) { + ATAReadLba(this->Leak().mBase + i, this->Leak().mBus, this->Leak().mMaster, + Buf, Off); + Off += 256; } + return *this; } @@ -156,11 +200,20 @@ BATADevice& BATADevice::Write(CharacterType* Buf, const SizeT& Sz) { if (!Buf || Sz < 1) return *this; + SizeT Off = 0UL; + for (SizeT i = 0UL; i < Sz; ++i) { - ATAWriteLba(Buf[i], this->Traits().mBase + i, this->Traits().mBus, - this->Traits().mMaster); + ATAWriteLba(Buf[i], this->Leak().mBase + i, this->Leak().mBus, + this->Leak().mMaster, Buf, Off); + + Off += 256; } + return *this; } -BATADevice::ATATraits& BATADevice::Traits() { return mTraits; } +/** + * @brief ATA Config getter. + * @return BATADevice::ATATraits& the drive config. + */ +BATADevice::ATATraits& BATADevice::Leak() { return mTraits; } diff --git a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx index 31ec449f..841a1f40 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx @@ -27,8 +27,6 @@ EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, UInt64 mapKey = 0; - CharacterType bytes[1024]; - BFileReader reader(L"\\Root\\System\\HCoreKrnl.exe\0"); auto blob = reader.ReadAll(); |
