diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-03 14:52:52 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-03 14:52:52 +0100 |
| commit | fcccf780db4cdc23858c108c6cde1d08360ee88f (patch) | |
| tree | 6e9d871860fe4a6f415b95f7d77d2ac5bf9275a2 /Private/NewBoot | |
| parent | 1f0cdb9f4ad64623ae8434a81fcbe8d37a5c8164 (diff) | |
Kernel: Got stuck at the way I do things, trying another approach see
hcore ticket HCR-11 in Jira.
Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Private/NewBoot')
| -rw-r--r-- | Private/NewBoot/BootKit/BootKit.hxx | 5 | ||||
| -rw-r--r-- | Private/NewBoot/Source/Entrypoint.cxx (renamed from Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx) | 7 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx | 74 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/AMD64-CoreSync-ATA.asm | 24 | ||||
| -rw-r--r-- | Private/NewBoot/Source/ImageReader.cxx | 70 | ||||
| -rw-r--r-- | Private/NewBoot/Source/String.cxx (renamed from Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx) | 56 | ||||
| -rw-r--r-- | Private/NewBoot/Source/Utils.cxx | 162 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 5 |
8 files changed, 156 insertions, 247 deletions
diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 0cbddfab..766749bd 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -84,7 +84,8 @@ class BImageReader final { private: Int32 mErrorCode{kOperationOkay}; CharacterType mPath[kPathLen]; - VoidPtr mHandle{nullptr}; + BTextWriter mWriter; + BATADevice mDevice; }; /***********************************************************************************/ @@ -121,7 +122,7 @@ inline UInt8 In8(UInt16 port) { inline UInt16 In16(UInt16 port) { UInt16 value = 0UL; - asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); + asm volatile("in %%dx, %%ax" : "=a"(value) : "d"(port)); return value; } diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx b/Private/NewBoot/Source/Entrypoint.cxx index c2631a6f..6a778143 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx +++ b/Private/NewBoot/Source/Entrypoint.cxx @@ -12,6 +12,7 @@ #include <BootKit/BootKit.hxx> #include <EFIKit/Api.hxx> #include <KernelKit/PE.hpp> +#include <NewKit/Ref.hpp> // don't remove EfiGUID, it will call initializer_list! @@ -33,7 +34,8 @@ EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, PEImagePtr blob = (PEImagePtr)img.Fetch(sz); if (!blob || sz < 1) - KeRuntimeStop(L"HCoreLdr_NoSuchKernel", L"Couldn't find HCoreKrnl.exe!"); + EFI::RaiseHardError(L"HCoreLdr_NoSuchKernel", + L"Couldn't find HCoreKrnl.exe!"); ExecHeaderPtr headerPtr = (ExecHeaderPtr)blob; @@ -41,10 +43,11 @@ EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle, writer.WriteString(L"HCoreLdr: Running HCoreKrnl.exe...\r\n"); /// Load Image here } else { - KeRuntimeStop(L"HCoreLdr_NotPE", L"Not a PE file! Aborting..."); + EFI::RaiseHardError(L"HCoreLdr_NotPE", L"Not a PE file! Aborting..."); } EFI::Stop(); + EFI::ExitBootServices(mapKey, ImageHandle); return kEfiOk; } diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx index c289bcf5..d6b6e0a9 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx @@ -23,6 +23,8 @@ /// bugs: 0 +extern "C" Void ATAWaitForIO(Void); + static Boolean kATADetected = false; static Int32 kATADeviceType = kATADeviceCount; @@ -67,19 +69,19 @@ Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive, UInt16& OutBus, /* differentiate ATA, ATAPI, SATA and SATAPI */ if (cl == 0x14 && ch == 0xEB) { - writer.WriteString(L"HCoreLdr: PATAPI drive detected.\r\n"); + 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"); + 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"); + 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"); + writer.WriteString(L"HCoreLdr: SATA: drive detected.\r\n"); kATADeviceType = kATADeviceSATA; } @@ -95,52 +97,50 @@ void ATAWait(UInt16 IO) { void ATAPoll(UInt16 IO) { ATAWait(IO); } -Void ATAReadLba(UInt32 Lba, UInt8 Bus, Boolean Master, CharacterType* Buf, +Void ATAReadLba(UInt32 Lba, UInt8 IO, Boolean Master, CharacterType* Buf, SizeT Offset) { - UInt16 IO = Bus; + rt_cli(); + ATAWaitForIO(); - ATASelect(IO + ATA_REG_HDDEVSEL, - (Master ? ATA_MASTER : ATA_SLAVE) | Lba >> 24 & 0xF); + Lba &= 0x00FFFFFF; - ATASelect(IO + 1, 0); + UInt8 Command = (Master ? 0xE0 : 0xF0); - Out8(IO + ATA_REG_SEC_COUNT0, 1); + Out8(IO + ATA_REG_HDDEVSEL, Command | ((Lba >> 24))); + Out8(IO + ATA_REG_SEC_COUNT0, 0x1); 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_LBA1, (UInt8)Lba >> 8); + Out8(IO + ATA_REG_LBA2, (UInt8)Lba >> 16); Out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - ATAPoll(IO); - - Buf[Offset] = In16(IO + ATA_REG_DATA); + Buf[Offset] = In16(IO); - ATAWait(IO); + ATAWaitForIO(); + rt_sti(); } -Void ATAWriteLba(UInt32 Lba, UInt8 Bus, Boolean Master, wchar_t* Buf, +Void ATAWriteLba(UInt32 Lba, UInt8 IO, Boolean Master, wchar_t* Buf, SizeT Offset) { - UInt16 IO = Bus; - - ATASelect(IO + ATA_REG_HDDEVSEL, - (Master ? ATA_MASTER : ATA_SLAVE) | Lba >> 24 & 0xF); + rt_cli(); + ATAWaitForIO(); + Lba &= 0x00FFFFFF; - ATASelect(IO + 1, 0); + UInt8 Command = (Master ? 0xE0 : 0xF0); - Out8(IO + ATA_REG_SEC_COUNT0, 1); + Out8(IO + ATA_REG_HDDEVSEL, Command | ((Lba >> 24))); + Out8(IO + ATA_REG_SEC_COUNT0, 0x1); 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_LBA1, (UInt8)Lba >> 8); + Out8(IO + ATA_REG_LBA2, (UInt8)Lba >> 16); Out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); - ATAPoll(IO); - - Out16(IO + ATA_REG_DATA, Buf[Offset]); - - ATAWait(IO); + Out16(IO, Buf[Offset]); + ATAWaitForIO(); + rt_sti(); } Boolean ATAIsDetected(Void) { return kATADetected; } @@ -171,7 +171,7 @@ BATADevice::BATADevice() noexcept { kATADetected = true; BTextWriter writer; - writer.WriteString(L"BATADevice::BATADevice: OnLine\r\n"); + writer.WriteString(L"HCoreLdr: Driver: OnLine.\r\n"); } } @@ -180,12 +180,12 @@ BATADevice::BATADevice() noexcept { @param Sz Sector size @param Buf buffer */ -BATADevice& BATADevice::Read(CharacterType* Buf, const SizeT& Sz) { +BATADevice& BATADevice::Read(CharacterType* Buf, const SizeT& SectorSz) { if (!ATAIsDetected()) return *this; - if (!Buf || Sz < 1) return *this; + if (!Buf || SectorSz > kATASectorSz || SectorSz < 1) return *this; - for (SizeT i = 0UL; i < Sz; ++i) { + for (SizeT i = 0UL; i < SectorSz; ++i) { ATAReadLba(this->Leak().mBase + i, this->Leak().mBus, this->Leak().mMaster, Buf, i); } @@ -198,14 +198,14 @@ BATADevice& BATADevice::Read(CharacterType* Buf, const SizeT& Sz) { @param Sz Sector size @param Buf buffer */ -BATADevice& BATADevice::Write(CharacterType* Buf, const SizeT& Sz) { +BATADevice& BATADevice::Write(CharacterType* Buf, const SizeT& SectorSz) { if (!ATAIsDetected()) return *this; - if (!Buf || Sz < 1) return *this; + if (!Buf || SectorSz > kATASectorSz || SectorSz < 1) return *this; SizeT Off = 0UL; - for (SizeT i = 0UL; i < Sz; ++i) { + for (SizeT i = 0UL; i < SectorSz; ++i) { ATAWriteLba(this->Leak().mBase + i, this->Leak().mBus, this->Leak().mMaster, Buf, Off); diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-CoreSync-ATA.asm b/Private/NewBoot/Source/HEL/AMD64/AMD64-CoreSync-ATA.asm new file mode 100644 index 00000000..6ad07a59 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-CoreSync-ATA.asm @@ -0,0 +1,24 @@ +[bits 64] + + +section .text +global ATAWaitForIO + +ATAWaitForIO: + push ax + push dx + + xor ax, ax + mov dx, 01F7h + +ATAWaitForIO_Loop: + in al, dx + and al, 0C0h + cmp al, 040h + jne ATAWaitForIO_Loop + +ATAWaitForIO_End: + pop dx + pop ax + + ret diff --git a/Private/NewBoot/Source/ImageReader.cxx b/Private/NewBoot/Source/ImageReader.cxx new file mode 100644 index 00000000..b714c21f --- /dev/null +++ b/Private/NewBoot/Source/ImageReader.cxx @@ -0,0 +1,70 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include <BootKit/BootKit.hxx> +#include <EFIKit/Api.hxx> +#include <FSKit/NewFS.hxx> + +#include "NewKit/Defines.hpp" + +//////////////////////////////////////////////////////////////////////////////////////////////////// +// +// +// @brief BImageReader class +// +// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/*** + @brief File Reader constructor. +*/ +BImageReader::BImageReader(const CharacterType *path) { + if (path != nullptr) { + SizeT index = 0UL; + for (; path[index] != L'\0'; ++index) { + mPath[index] = path[index]; + } + + mPath[index] = 0; + } +} + +/** + @brief this reads all of the buffer. + @param size, new buffer size. +*/ +HCore::VoidPtr BImageReader::Fetch(SizeT &size) { + mWriter.WriteString(L"HCoreLdr: Fetch-Image: ") + .WriteString(mPath) + .WriteString(L"\r\n"); + + CharacterType bootBlockBytes[kATASectorSz] = {0}; + + mDevice.Leak().mBase = kNewFSAddressAsLba; + mDevice.Leak().mMaster = true; + + mDevice.Read(bootBlockBytes, kATASectorSz); + + NewBootBlock *bootBlock = (NewBootBlock *)bootBlockBytes; + + for (SizeT index = 0UL; index < kNewFSIdentLen; ++index) { + if (bootBlock->Ident[index] != kNewFSIdent[index]) { + mWriter.WriteString(L"HCoreLdr: NewFS: Not found.\r\n"); + return nullptr; + } + } + + /// get file catalog with mPath inside it. + + return nullptr; +} diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx b/Private/NewBoot/Source/String.cxx index 8db6ae16..a2d19f1f 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx +++ b/Private/NewBoot/Source/String.cxx @@ -1,11 +1,15 @@ -/* - * ======================================================== - * - * NewBoot - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ +/* ------------------------------------------- + + Copyright Mahrouss Logic + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ #include <BootKit/BootKit.hxx> #include <EFIKit/Api.hxx> @@ -82,39 +86,3 @@ BTextWriter &BTextWriter::WriteCharacter(CharacterType c) { return *this; } - -//////////////////////////////////////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -/*** - @brief File Reader constructor. -*/ -BImageReader::BImageReader(const CharacterType *path) { - if (path != nullptr) { - SizeT index = 0UL; - for (; path[index] != L'\0'; ++index) { - mPath[index] = path[index]; - } - - mPath[index] = 0; - } -} - -/** -@brief this reads all of the buffer. -@param size, new buffer size. -*/ -HCore::VoidPtr BImageReader::Fetch(SizeT &size) { - BTextWriter writer; - writer.WriteString(L"*** BImageReader::Fetch: Fetching... ") - .WriteString(mPath) - .WriteString(L" *** \r\n"); - - EfiLoadImageProtocol *proto = nullptr; - EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); - - if (BS->LocateProtocol(&guidImg, nullptr, (VoidPtr *)&proto) == kEfiOk) { - } - - return nullptr; -} diff --git a/Private/NewBoot/Source/Utils.cxx b/Private/NewBoot/Source/Utils.cxx index c51a87d6..8d2242cd 100644 --- a/Private/NewBoot/Source/Utils.cxx +++ b/Private/NewBoot/Source/Utils.cxx @@ -11,164 +11,4 @@ #include <EFIKit/Api.hxx> #include <EFIKit/EFI.hxx> -///////////////////////////////////////////////////////////////////////////////////////////////////////// -// -// @brief External EFI code for completeness. -// -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -namespace Detail { -const EfiDevicePathProtocol mUefiDevicePathLibEndDevicePath = { - END_DEVICE_PATH_TYPE, - END_ENTIRE_DEVICE_PATH_SUBTYPE, - {sizeof(EfiDevicePathProtocol), 0}}; - -EfiDevicePathProtocol *UefiDevicePathLibAppendDevicePath( - const EfiDevicePathProtocol *FirstDevicePath, - const EfiDevicePathProtocol *SecondDevicePath) { - SizeT Size; - SizeT Size1; - SizeT Size2; - EfiDevicePathProtocol *NewDevicePath; - EfiDevicePathProtocol *DevicePath2; - - // - // If there's only 1 path, just duplicate it. - // - if (FirstDevicePath == nullptr) { - return DuplicateDevicePath((SecondDevicePath != nullptr) - ? SecondDevicePath - : &mUefiDevicePathLibEndDevicePath); - } - - if (SecondDevicePath == nullptr) { - return DuplicateDevicePath(FirstDevicePath); - } - - if (!IsDevicePathValid(FirstDevicePath, 0) || - !IsDevicePathValid(SecondDevicePath, 0)) { - return nullptr; - } - - // - // Allocate space for the combined device path. It only has one end node of - // length EFI_DEVICE_PATH_PROTOCOL. - // - Size1 = GetDevicePathSize(FirstDevicePath); - Size2 = GetDevicePathSize(SecondDevicePath); - Size = Size1 + Size2 - sizeof(EfiDevicePathProtocol); - - BS->AllocatePool(EfiLoaderData, Size, (VoidPtr *)&NewDevicePath); - - if (NewDevicePath != NULL) { - BCopyMem((CharacterType *)NewDevicePath, (CharacterType *)FirstDevicePath, - Size1); - // - // Over write FirstDevicePath EndNode and do the copy - // - DevicePath2 = - (EfiDevicePathProtocol *)((Char *)NewDevicePath + - (Size1 - sizeof(EfiDevicePathProtocol))); - BCopyMem((CharacterType *)DevicePath2, (CharacterType *)SecondDevicePath, - Size2); - } - - return NewDevicePath; -} - -EfiDevicePathProtocol *AppendDevicePath( - EfiDevicePathProtocol *FirstDevicePath, - EfiDevicePathProtocol *SecondDevicePath) { - return UefiDevicePathLibAppendDevicePath(FirstDevicePath, SecondDevicePath); -} - -EfiDevicePathProtocol *EFI_API DevicePathFromHandle(EfiHandlePtr Handle) { - EfiDevicePathProtocol *DevicePath; - UInt64 Status; - - EfiGUID guid = EfiGUID(EFI_DEVICE_PATH_PROTOCOL_GUID); - - Status = BS->HandleProtocol(Handle, &guid, (VoidPtr *)&DevicePath); - if (Status != kEfiOk) { - DevicePath = nullptr; - } - - return DevicePath; -} - -UInt16 ReadUnaligned16(UInt16 *Buffer) { - // ASSERT (Buffer != NULL); - return *Buffer; -} - -UInt16 WriteUnaligned16(UInt16 *Buffer, UInt16 Value) { - // ASSERT (Buffer != NULL); - - return *Buffer = Value; -} - -UInt16 EFI_API SetDevicePathNodeLength(Void *Node, UInt32 Length) { - // ASSERT(Node != nullptr); - // ASSERT((Length >= sizeof(EfiDevicePathProtocol)) && (Length < KIB(64))); - return WriteUnaligned16( - (UInt16 *)&((EfiDevicePathProtocol *)(Node))->Length[0], - (UInt16)(Length)); -} - -Void EFI_API SetDevicePathEndNode(Void *Node) { - // ASSERT(Node != NULL); - BCopyMem((CharacterType *)Node, - (CharacterType *)&mUefiDevicePathLibEndDevicePath, - sizeof(mUefiDevicePathLibEndDevicePath)); -} - -UInt32 EFI_API DevicePathNodeLength(const Void *Node) { - // ASSERT(Node != NULL); - return ReadUnaligned16( - (UInt16 *)&((EfiDevicePathProtocol *)(Node))->Length[0]); -} - -EfiDevicePathProtocol *EFI_API NextDevicePathNode(Void *Node) { - // ASSERT (Node != NULL); - return (EfiDevicePathProtocol *)((UInt8 *)(Node) + - DevicePathNodeLength(Node)); -} - -EfiDevicePathProtocol *EFI_API FileDevicePath(EfiHandlePtr Device, - CharacterType *FileName) { - SizeT Size; - EfiFileDevicePathProtocol *FilePath; - EfiDevicePathProtocol *DevicePath; - EfiDevicePathProtocol *FileDevicePath; - - DevicePath = nullptr; - - Size = BStrLen(FileName); - BS->AllocatePool( - EfiLoaderData, - Size + sizeof(EfiFileDevicePathProtocol) + sizeof(EfiDevicePathProtocol), - (VoidPtr *)&FileDevicePath); - - if (FileDevicePath != nullptr) { - FilePath = (EfiFileDevicePathProtocol *)FileDevicePath; - FilePath->Proto.Type = kEFIMediaDevicePath; - FilePath->Proto.SubType = kEFIMediaDevicePath; - - BCopyMem(FilePath->Path, FileName, Size); - - SetDevicePathNodeLength(&FilePath->Proto, - Size + sizeof(EfiFileDevicePathProtocol)); - - SetDevicePathEndNode(NextDevicePathNode(&FilePath->Proto)); - - if (Device != nullptr) { - DevicePath = DevicePathFromHandle(Device); - } - - DevicePath = AppendDevicePath(DevicePath, FileDevicePath); - BS->FreePool(FileDevicePath); - } - - return DevicePath; -} -} // namespace Detail +/// @brief EFI API diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 04b39516..eb836632 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -5,7 +5,9 @@ CC_GNU=x86_64-w64-mingw32-g++ LD_GNU=x86_64-w64-mingw32-ld +ASM=nasm +FLAG_ASM=-f win64 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 @@ -14,8 +16,9 @@ invalid-recipe: .PHONY: bootloader-amd64 bootloader-amd64: + $(ASM) $(FLAG_ASM) HEL/AMD64/AMD64-CoreSync-ATA.asm $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx - $(LD_GNU) *.o -e efi_main -filealign:16 -shared --subsystem=10 -ffreestanding -o HCoreLdr.exe + $(LD_GNU) *.o HEL/AMD64/*.obj -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 |
