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/Source | |
| 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/Source')
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/ATA.cxx | 165 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx | 2 |
2 files changed, 109 insertions, 58 deletions
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(); |
