diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-02 09:13:50 +0000 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-02 09:13:50 +0000 |
| commit | f08c864e988f6f1f01985910955755220b37ecc8 (patch) | |
| tree | 174a79ebbdd869e543df9d2aa97aea0343c84725 /Private/NewBoot/Source | |
| parent | 5c59cd35a2fa3e620542b73e8c3f66f0dccd241c (diff) | |
| parent | df77fd9586cb305a738d5b4dfcdbe67177e3de3f (diff) | |
Merge branch 'HCR-9-add-support-for-file-load-hel-amd64' into 'trunk'
Merge Fixes and WiP Bootloader.
See merge request mahrouss-logic/micro-kernel!3
Diffstat (limited to 'Private/NewBoot/Source')
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/ATA.cxx | 230 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/BootKit.cxx | 4 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx | 6 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/Platform.cxx | 3 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 7 |
5 files changed, 239 insertions, 11 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..4f1b3171 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx @@ -0,0 +1,230 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +/** + * @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> + +/// 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, + 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, UInt16& OutBus, + Boolean& OutMaster) { + 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); + + if (status & ATA_SR_ERR) { +#ifdef __DEBUG__ + writer.WriteString(L"HCoreLdr: Init ATA: Bad Drive!\r\n"); +#endif // ifdef __DEBUG__ + return false; + } + + 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); + + /* differentiate ATA, ATAPI, SATA and SATAPI */ + 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; +} + +/*** @brief + * This polls the ATA drive. + */ +void ATAWait(UInt16 IO) { + for (int i = 0; i < 4000; i++) In8(IO + ATA_REG_ALT_STATUS); +} + +void ATAPoll(UInt16 IO) { ATAWait(IO); } + +Void ATAReadLba(UInt32 Lba, UInt8 Bus, Boolean Master, CharacterType* Buf, + SizeT Offset) { + UInt16 IO = Bus; + + ATASelect(IO + ATA_REG_HDDEVSEL, + (Master ? ATA_MASTER : ATA_SLAVE) | Lba >> 24 & 0xF); + + ATASelect(IO + 1, 0); + + Out8(IO + ATA_REG_SEC_COUNT0, 1); + + 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); + + for (SizeT index = 0UL; index < 256; ++index) { + Buf[index + Offset] = In16(IO + ATA_REG_DATA); + } + + ATAWait(IO); +} + +Void ATAWriteLba(UInt32 Lba, UInt8 Bus, Boolean Master, wchar_t* Buf, + SizeT Offset) { + UInt16 IO = Bus; + + ATASelect(IO + ATA_REG_HDDEVSEL, + (Master ? ATA_MASTER : ATA_SLAVE) | Lba >> 24 & 0xF); + + ATASelect(IO + 1, 0); + + Out8(IO + ATA_REG_SEC_COUNT0, 1); + + 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); + + ATAPoll(IO); + + for (SizeT index = 0UL; index < 256; ++index) { + Out16(IO + ATA_REG_DATA, Buf[index + Offset]); + } + + ATAWait(IO); +} + +/*** + * + * + * ATA Device class. + * + * + */ + +Boolean ATAIsDetected(Void) { return kATADetected; } + +/** + * @brief Init ATA driver. + * @param void none. + */ +BATADevice::BATADevice() noexcept { + if (ATAIsDetected()) return; + + 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; + writer.WriteString(L"BATADevice::BATADevice: OnLine\r\n"); + } +} + +/** + @brief Read Buf from disk + @param Sz Sector size + @param Buf buffer +*/ +BATADevice& BATADevice::Read(CharacterType* Buf, const SizeT& Sz) { + if (!ATAIsDetected()) return *this; + + if (!Buf || Sz < 1) return *this; + + SizeT Off = 0; + + for (SizeT i = 0UL; i < Sz; ++i) { + ATAReadLba(this->Leak().mBase + i, this->Leak().mBus, this->Leak().mMaster, + Buf, Off); + + Off += 512; + } + + return *this; +} + +/** + @brief Write Buf into disk + @param Sz Sector size + @param Buf buffer +*/ +BATADevice& BATADevice::Write(CharacterType* Buf, const SizeT& Sz) { + if (!ATAIsDetected()) return *this; + + if (!Buf || Sz < 1) return *this; + + SizeT Off = 0UL; + + for (SizeT i = 0UL; i < Sz; ++i) { + ATAWriteLba(this->Leak().mBase + i, this->Leak().mBus, this->Leak().mMaster, + Buf, Off); + + Off += 512; + } + + return *this; +} + +/** + * @brief ATA Config getter. + * @return BATADevice::ATATraits& the drive config. + */ +BATADevice::ATATraits& BATADevice::Leak() { return mTraits; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx b/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx index 43a79b41..99f8d41b 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootKit.cxx @@ -8,7 +8,7 @@ */ #include <BootKit/BootKit.hxx> -#include <EFIKit/EFILib.hxx> +#include <EFIKit/Api.hxx> /// bugs 0 @@ -85,7 +85,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..69f235c4 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Entrypoint.cxx @@ -10,7 +10,7 @@ #define __BOOTLOADER__ 1 #include <BootKit/BootKit.hxx> -#include <EFIKit/EFILib.hxx> +#include <EFIKit/Api.hxx> // don't remove EfiGUID, it will call initializer_list! @@ -19,14 +19,14 @@ 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; - BFileReader reader(L"\\MAHROUSS\\Root\\System\\HCoreKrnl.exe\0"); + BFileReader reader(L"\\Root\\System\\HCoreKrnl.exe\0"); auto blob = reader.ReadAll(); if (!blob) diff --git a/Private/NewBoot/Source/HEL/AMD64/Platform.cxx b/Private/NewBoot/Source/HEL/AMD64/Platform.cxx index dbd2b900..f92de33b 100644 --- a/Private/NewBoot/Source/HEL/AMD64/Platform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/Platform.cxx @@ -15,8 +15,7 @@ */ #include <BootKit/Platform.hxx> - -#include "EFIKit/EFILib.hxx" +#include <EFIKit/Api.hxx> extern "C" void rt_halt() { asm volatile("hlt"); } diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index f10a4194..063fa42a 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,15 +14,14 @@ 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 .PHONY: run-efi-debug run-efi-debug: wget https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd - qemu-system-x86_64 -bios OVMF.fd -net none -drive file=fat:rw:CDROM,format=raw -d int + qemu-system-x86_64 -bios OVMF.fd -drive file=fat:rw:CDROM,format=raw -d int .PHONY: clean clean: |
