diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-01 20:05:58 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-01 20:05:58 +0100 |
| commit | 9700c31e4958856ea9e4fa755a43d196fcf50614 (patch) | |
| tree | 262cb175fb3af842563436f5d92b084da27fd05f /Private/NewBoot/Source | |
| parent | 5c59cd35a2fa3e620542b73e8c3f66f0dccd241c (diff) | |
NewBoot: Add ATA driver for NewFS/EPM.
Also added a ATAHelper class.
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 | 188 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootKit.cxx | 4 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx | 3 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 4 |
4 files changed, 195 insertions, 4 deletions
diff --git a/Private/NewBoot/Source/HEL/AMD64/ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx new file mode 100644 index 00000000..6b45b4af --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx @@ -0,0 +1,188 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include <BootKit/Arch/ATA.hxx> +#include <BootKit/BootKit.hxx> + +static Boolean kATADetected = false; + +extern "C" void out8(UInt16 port, UInt8 value) { + asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +extern "C" void out16(UInt16 port, UInt16 value) { + asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +extern "C" void out32(UInt16 port, UInt32 value) { + asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +extern "C" UInt8 in8(UInt16 port) { + UInt8 value = 0UL; + asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +extern "C" UInt16 in16(UInt16 port) { + UInt16 value = 0UL; + asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +extern "C" UInt32 in32(UInt16 port) { + UInt32 value = 0UL; + asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +void ATASelect(UInt8 Bus, Boolean isMaster) { + if (Bus == ATA_PRIMARY) + out8(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, + isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); + else + out8(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, + isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); +} + +Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive) { + BTextWriter writer; + + UInt16 IO = (Bus == ATA_PRIMARY) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO; + + 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_COMMAND, ATA_CMD_IDENTIFY); + + UInt8 status = in8(IO + ATA_REG_STATUS); + writer.WriteString(L"HCoreLdr: Init ATA: Checking status...\r\n"); + + if (status) { + while ((status = in8(IO + ATA_REG_STATUS) & ATA_SR_BSY)) + ; + + if (status & ATA_REG_ERROR) { +#ifdef __DEBUG__ + writer.WriteString(L"HCoreLdr: Init ATA: Bad Drive!\r\n"); +#endif // ifdef __DEBUG__ + return false; + } + +#ifdef __DEBUG__ + writer.WriteString(L"HCoreLdr: Init ATA: OnLine!\r\n"); +#endif // ifdef __DEBUG__ + + kATADetected = true; + return status; + } + +#ifdef __DEBUG__ + writer.WriteString(L"HCoreLdr: Init ATA: Not detected!\r\n"); +#endif // ifdef __DEBUG__ + return false; +} + +void ATAWait(UInt16 IO) { + 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) { + UInt16 IO = Bus; + ATASelect(IO, Master ? ATA_MASTER : ATA_SLAVE); + + out8(IO + ATA_REG_LBA5, (UInt8)(Lba >> 24) & 0xF); + + out8(IO + ATA_REG_SEC_COUNT0, Lba / 512); + + 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_COMMAND, ATA_CMD_READ_PIO); + + ATAPoll(IO); + + UInt16 data = in16(IO + ATA_REG_DATA); + + while ((in8(ATA_COMMAND(IO))) & 0x88) ATAWait(IO); + + return data; +} + +Void ATAWriteLba(UInt16 Byte, UInt32 Lba, UInt8 Bus, Boolean Master) { + UInt16 IO = Bus; + ATASelect(IO, Master ? ATA_MASTER : ATA_SLAVE); + + out8(IO + ATA_REG_LBA5, (UInt8)(Lba >> 24) & 0xF); + + out8(IO + ATA_REG_SEC_COUNT0, Lba / 512); + + 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_COMMAND, ATA_CMD_WRITE_PIO); // TODO: support DMA + + ATAPoll(IO); + + out32(IO + ATA_REG_DATA, Byte); + + while ((in8(ATA_COMMAND(IO))) & 0x88) ATAWait(IO); +} + +Boolean ATAIsDetected(Void) { return kATADetected; } + +/** + * @brief Init ATA driver. + * @param void none. + */ +ATAHelper::ATAHelper() noexcept { + if (ATAIsDetected()) return; + + ATAInitDriver(ATA_PRIMARY, true); + ATAInitDriver(ATA_PRIMARY, false); + + ATAInitDriver(ATA_SECONDARY, true); + ATAInitDriver(ATA_SECONDARY, false); + + BTextWriter writer; + writer.WriteString(L"KeInitATA: Init: Done\r\n"); +} + +ATAHelper& ATAHelper::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); + } + return *this; +} + +ATAHelper& ATAHelper::Write(CharacterType* Buf, const SizeT& Sz) { + if (!Buf || Sz < 1) return *this; + + for (SizeT i = 0UL; i < Sz; ++i) { + ATAWriteLba(Buf[i], this->Traits().mBase + i, this->Traits().mBus, + this->Traits().mMaster); + } + return *this; +} + +ATAHelper::ATATraits& ATAHelper::Traits() { return mTraits; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx b/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx index 43a79b41..33e69d05 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx @@ -10,6 +10,8 @@ #include <BootKit/BootKit.hxx> #include <EFIKit/EFILib.hxx> +#include "BootKit/Arch/ATA.hxx" + /// bugs 0 HCore::SizeT BStrLen(const CharacterType *ptr) { @@ -85,7 +87,7 @@ BFileReader::BFileReader(const CharacterType *path) { */ HCore::VoidPtr BFileReader::ReadAll() { BTextWriter writer; - writer.WriteString(L"*** PE/COFF: Reading ") + writer.WriteString(L"*** BFileReader::ReadAll: Reading ") .WriteString(mPath) .WriteString(L" *** \r\n"); diff --git a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx index d06338b5..e2c96d19 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx @@ -9,6 +9,7 @@ #define __BOOTLOADER__ 1 +#include <BootKit/Arch/ATA.hxx> #include <BootKit/BootKit.hxx> #include <EFIKit/EFILib.hxx> @@ -19,9 +20,9 @@ EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, KeInitEFI(SystemTable); BTextWriter writer; + writer.WriteString(L"HCoreLdr: Firmware: ") .WriteString(SystemTable->FirmwareVendor) - .WriteString(L"\r\nHCoreLdr: Booting on \\Volume0\\ (FAT32)") .WriteString(L"\r\n"); UInt64 mapKey = 0; diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index f10a4194..acf5f201 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -6,7 +6,7 @@ CC_GNU=x86_64-w64-mingw32-g++ LD_GNU=x86_64-w64-mingw32-ld -FLAG_GNU=-fshort-wchar -fPIC -D__DBG__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I../../efiSDK/inc -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ +FLAG_GNU=-fshort-wchar -fPIC -D__DEBUG__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I../../efiSDK/inc -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ .PHONY: invalid-recipe invalid-recipe: @@ -14,7 +14,7 @@ invalid-recipe: .PHONY: bootloader-amd64 bootloader-amd64: - $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx + $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx $(LD_GNU) *.o -e efi_main -filealign:16 -shared --subsystem=10 -ffreestanding -o HCoreLdr.exe cp HCoreLdr.exe CDROM/EFI/BOOT/BOOTX64.EFI cp ../../HCoreKrnl.exe CDROM/EFI/BOOT/HCoreKrnl.exe |
