diff options
| author | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-05 10:53:58 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlalelmahrouss@icloud.com> | 2024-02-05 10:53:58 +0100 |
| commit | f8c9b81ff120160af60af6e9d44cba338aceb65a (patch) | |
| tree | cb4683589aab4d50470628f189dc366814fd80ad | |
| parent | e03903b98aa0b4d2dc3ed4637863124f28c4e1fe (diff) | |
Kernel: Improved kernel design and Bootloader runs on real hardware!
Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
| -rw-r--r-- | Private/Drivers/ATA/Defines.hxx | 2 | ||||
| -rw-r--r-- | Private/EFIKit/Api.hxx | 4 | ||||
| -rw-r--r-- | Private/EFIKit/EPM.hxx | 2 | ||||
| -rw-r--r-- | Private/KernelKit/HError.hpp | 4 | ||||
| -rw-r--r-- | Private/NewBoot/BootKit/BootKit.hxx | 9 | ||||
| -rw-r--r-- | Private/NewBoot/Source/Entrypoint.cxx | 13 | ||||
| -rw-r--r-- | Private/NewBoot/Source/FileReader.cxx | 45 | ||||
| -rw-r--r-- | Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx | 20 | ||||
| -rw-r--r-- | Private/NewBoot/Source/makefile | 10 | ||||
| -rw-r--r-- | Private/Source/DriveManager.cxx | 12 | ||||
| -rw-r--r-- | Private/makefile | 2 | ||||
| -rw-r--r-- | Public/Kits/SystemKit/CoreAPI.hxx | 5 | ||||
| -rw-r--r-- | Public/Kits/SystemKit/FileAPI.hxx | 3 | ||||
| -rw-r--r-- | Public/Kits/SystemKit/SystemCall.hxx | 18 | ||||
| -rw-r--r-- | Public/Kits/ZipKit/Zip.cxx | 2 |
15 files changed, 78 insertions, 73 deletions
diff --git a/Private/Drivers/ATA/Defines.hxx b/Private/Drivers/ATA/Defines.hxx index f3392ace..058f0758 100644 --- a/Private/Drivers/ATA/Defines.hxx +++ b/Private/Drivers/ATA/Defines.hxx @@ -86,6 +86,8 @@ using namespace HCore; #define ATA_REG_ALT_STATUS 0x0C #define ATA_REG_DEV_ADDRESS 0x0D +#define ATA_REG_NEIN 0x01 + #define ATA_PRIMARY_IO 0x1F0 #define ATA_SECONDARY_IO 0x170 #define ATA_PRIMARY_DCR_AS 0x3F6 diff --git a/Private/EFIKit/Api.hxx b/Private/EFIKit/Api.hxx index 0c667cb2..89e5e0a3 100644 --- a/Private/EFIKit/Api.hxx +++ b/Private/EFIKit/Api.hxx @@ -48,8 +48,12 @@ inline void RaiseHardError(const EfiCharType *ErrorCode, ST->ConOut->OutputString(ST->ConOut, L"*** Error: "); ST->ConOut->OutputString(ST->ConOut, ErrorCode); + +#ifdef __DEBUG__ ST->ConOut->OutputString(ST->ConOut, L", Reason: "); ST->ConOut->OutputString(ST->ConOut, Reason); +#endif // ifdef __DEBUG__ + ST->ConOut->OutputString(ST->ConOut, L" ***\r\n"); EFI::Stop(); diff --git a/Private/EFIKit/EPM.hxx b/Private/EFIKit/EPM.hxx index dba8a9b7..8c8f724f 100644 --- a/Private/EFIKit/EPM.hxx +++ b/Private/EFIKit/EPM.hxx @@ -8,7 +8,7 @@ */ /** - @brief The Explicit Partition Map use is to tell how many NewFS and other + @brief The Explicit Partition Map is used to tell how many NewFS and other EPM compatible Filesystems. We have in this computer. */ diff --git a/Private/KernelKit/HError.hpp b/Private/KernelKit/HError.hpp index c03e8104..d37fe276 100644 --- a/Private/KernelKit/HError.hpp +++ b/Private/KernelKit/HError.hpp @@ -14,6 +14,7 @@ namespace HCore { typedef Int32 HError; +inline constexpr HError kErrorSuccess = 0; inline constexpr HError kErrorExecutable = 33; inline constexpr HError kErrorExecutableLib = 34; inline constexpr HError kErrorFileNotFound = 35; @@ -27,3 +28,6 @@ inline constexpr HError kErrorMath = 42; inline constexpr HError kErrorNoNetwork = 43; inline constexpr HError kErrorHeapOutOfMemory = 44; } // namespace HCore + +#define KernIsOk(HERR) (HERR == HCore::kErrorSuccess) +#define KernHasFailed(HERR) (HERR != HCore::kErrorSuccess) diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 5e0ca135..9a34f568 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -64,7 +64,7 @@ class BFileReader final { explicit BFileReader(const CharacterType *path); ~BFileReader(); - Void Fetch(EfiHandlePtr ImageHandle); + Void ReadAll(EfiHandlePtr ImageHandle); enum { kOperationOkay, @@ -77,6 +77,8 @@ class BFileReader final { Int32 &Error() { return mErrorCode; } VoidPtr Blob() { return mBlob; } + EfiFileProtocol *File() { return mFile; } + UInt32 &Size() { return mSizeFile; } public: BFileReader &operator=(const BFileReader &) = default; @@ -87,7 +89,8 @@ class BFileReader final { VoidPtr mBlob{nullptr}; CharacterType mPath[kPathLen]; BTextWriter mWriter; - bool mCached{false}; + EfiFileProtocol *mFile{nullptr}; + UInt32 mSizeFile{0}; }; /***********************************************************************************/ @@ -172,7 +175,7 @@ inline Void InitQT() noexcept { for (int y = 0; y < kGop->Mode->Info->HorizontalResolution; ++y) { *((UInt32 *)(kGop->Mode->FrameBufferBase + 4 * kGop->Mode->Info->PixelsPerScanLine * x + kStride * y)) = - RGB(10, 10, 10); + RGB(19, 19, 19); } } } diff --git a/Private/NewBoot/Source/Entrypoint.cxx b/Private/NewBoot/Source/Entrypoint.cxx index c6ae4572..2ef057ba 100644 --- a/Private/NewBoot/Source/Entrypoint.cxx +++ b/Private/NewBoot/Source/Entrypoint.cxx @@ -21,19 +21,20 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, BTextWriter writer; - writer.WriteString(L"HCoreLdr: ") - .WriteString(L"Copyright Mahrouss-Logic Corporation.") - .WriteString(L"\r\n"); - - writer.WriteString(L"HCoreLdr: Firmware: ") + writer.WriteString(L"HCoreLdr: Firmware Vendor: ") .WriteString(SystemTable->FirmwareVendor) .WriteString(L"\r\n"); BFileReader img(L"HCOREKRNL.EXE"); - img.Fetch(ImageHandle); + img.ReadAll(ImageHandle); VoidPtr blob = img.Blob(); + if (!blob) { + EFI::RaiseHardError(L"HCoreLdr_NoBlob", L"No Such blob."); + return kEfiFail; + } + UInt64 MapKey = 0; EFI::ExitBootServices(MapKey, ImageHandle); diff --git a/Private/NewBoot/Source/FileReader.cxx b/Private/NewBoot/Source/FileReader.cxx index be0e7746..a18c5093 100644 --- a/Private/NewBoot/Source/FileReader.cxx +++ b/Private/NewBoot/Source/FileReader.cxx @@ -17,8 +17,7 @@ #include <EFIKit/Handover.hxx> #include "EFIKit/EFI.hxx" -#include "KernelKit/PE.hpp" -#include "NewKit/Defines.hpp" +#include "NewKit/Macros.hpp" //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -46,14 +45,21 @@ BFileReader::~BFileReader() { if (this->mBlob) { BS->FreePool(this->mBlob); } + + if (this->mFile) { + this->mFile->Close(this->mFile); + this->mFile = nullptr; + } + + BSetMem(this->mPath, 0, kPathLen); } /** @brief this reads all of the buffer. @param ImageHandle used internally. */ -Void BFileReader::Fetch(EfiHandlePtr ImageHandle) { - mWriter.WriteString(L"HCoreLdr: Fetch-File: ") +Void BFileReader::ReadAll(EfiHandlePtr ImageHandle) { + mWriter.WriteString(L"HCoreLdr: ReadAll: ") .WriteString(mPath) .WriteString(L"\r\n"); @@ -102,13 +108,6 @@ Void BFileReader::Fetch(EfiHandlePtr ImageHandle) { rootFs->Close(rootFs); - if (kernelFile->Revision < EFI_FILE_PROTOCOL_REVISION2) { - mWriter.WriteString(L"HCoreLdr: Fetch-Protocol: Invalid-Revision: ") - .WriteString(mPath) - .WriteString(L"\r\n"); - return; - } - /// File FAT info. UInt32 szInfo = sizeof(EfiFileInfo); @@ -126,8 +125,7 @@ Void BFileReader::Fetch(EfiHandlePtr ImageHandle) { return; } - mWriter.WriteString(L"HCoreLdr: Fetch-Info: In-Progress...") - .WriteString(L"\r\n"); + /// Allocate Handover page. VoidPtr blob = (VoidPtr)kHandoverStartKernel; @@ -136,21 +134,14 @@ Void BFileReader::Fetch(EfiHandlePtr ImageHandle) { EFI::RaiseHardError(L"HCoreLdr_PageError", L"Allocation error."); } - UInt32* sz = nullptr; - - BS->AllocatePool(EfiLoaderData, sizeof(UInt32), (VoidPtr*)&sz); - - *sz = info->FileSize; - - mWriter.WriteString(L"HCoreLdr: Fetch-Info: Read...").WriteString(L"\r\n"); - - kernelFile->Read(kernelFile, sz, blob); - - mWriter.WriteString(L"HCoreLdr: Fetch-Info: Success...").WriteString(L"\r\n"); - - mCached = true; + mSizeFile = info->FileSize; + mFile = kernelFile; mErrorCode = kOperationOkay; mBlob = blob; - // We are done! + this->File()->Read(this->File(), &mSizeFile, this->Blob()); + + mWriter.WriteString(L"HCoreLdr: ReadAll: OK: ") + .WriteString(mPath) + .WriteString(L"\r\n"); } diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx index 2ef2189c..757c294d 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx @@ -57,39 +57,45 @@ Void ATASelect(UInt16 Bus) { Boolean ATAInitDriver(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { + if (IsATADetected()) return false; + BTextWriter writer; UInt16 IO = Bus; - // Bus init, NEIN bit. - Out8(IO + 1, 1); - ATASelect(IO); + // Bus init, NEIN bit. + Out8(IO + ATA_REG_NEIN, 1); + // identify until it's good. ATAInit_Retry: auto statRdy = In8(IO + ATA_REG_STATUS); if (statRdy & ATA_SR_ERR) { writer.WriteString( - L"HCoreLdr: ATA: Hard-drive error, not an IDE drive.\r\n"); + L"HCoreLdr: ATA: Select error, not an IDE based hard-drive.\r\n"); + return false; } + if ((statRdy & ATA_SR_BSY)) goto ATAInit_Retry; Out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + BSetMem(kATAData, 0, kATADataLen); + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { kATAData[indexData] = In16(IO + ATA_REG_DATA); } - writer.WriteString(L"HCoreLdr: Drive: "); + writer.WriteString(L"HCoreLdr: Model: "); /// fetch drive info for (SizeT indexData = 0; indexData < kATADataLen; indexData += 1) { - writer.WriteCharacter(kATAData[indexData + 1]) - .WriteCharacter(kATAData[indexData]); + writer.WriteCharacter(kATAData[indexData + ATA_IDENT_MODEL + 1]) + .WriteCharacter(kATAData[indexData + ATA_IDENT_MODEL]); } writer.WriteString(L"\r\n"); diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 2c08426e..02990084 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -5,10 +5,12 @@ CC_GNU=x86_64-w64-mingw32-g++ LD_GNU=x86_64-w64-mingw32-ld +LD_FLAGS=-e efi_main -shared --subsystem=10 -ffreestanding ASM=nasm +OBJ=$(wildcard *.obj) $(wildcard *.o) $(wildcard HEL/AMD64/*.obj) $(wildcard HEL/AMD64/*.o) FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -D__DEBUG__ -fPIC -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ +FLAG_GNU=-fshort-wchar -O0 -D__DEBUG__ -fPIC -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/ .PHONY: invalid-recipe invalid-recipe: @@ -18,7 +20,7 @@ invalid-recipe: bootloader-amd64: $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx $(ASM) $(FLAG_ASM) HEl/AMD64/AMD64-VirtualMemory.asm - $(LD_GNU) *.o HEL/AMD64/*.obj -e efi_main -shared --subsystem=10 -ffreestanding -o HCoreLdr.exe + $(LD_GNU) $(OBJ) $(LD_FLAGS) -o HCoreLdr.exe cp HCoreLdr.exe CDROM/EFI/BOOT/BOOTX64.EFI cp ../../HCoreKrnl.exe CDROM/HCOREKRNL.EXE @@ -29,8 +31,8 @@ make-disk: .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 -smp 8,maxcpus=16 -m 8G -M q35 -bios OVMF.fd -drive file=os.epm,index=0,if=ide,format=qcow2 -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio + qemu-system-x86_64 -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=os.epm,index=0,if=ide,format=qcow2 -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio .PHONY: clean clean: - rm -f *.o HCoreLdr.exe OVMF.fd + rm -f $(OBJ) HCoreLdr.exe OVMF.fd diff --git a/Private/Source/DriveManager.cxx b/Private/Source/DriveManager.cxx index 9fbba2ec..bc100a1a 100644 --- a/Private/Source/DriveManager.cxx +++ b/Private/Source/DriveManager.cxx @@ -25,11 +25,15 @@ DriveTraits &DriveSelector::GetMounted() { } bool DriveSelector::Mount(DriveTraits *drive) { - if (drive && drive->fReady() && fDrive == nullptr) { + if (drive && drive->fReady()) { + if (fDrive != nullptr) { + this->Unmount(); + } + fDrive = drive; fDrive->fMount(); - kcout << "Mount drive: " << fDrive->fName << "\n"; + kcout << "[Krnl] mounted: " << fDrive->fName << "\n"; return true; } @@ -40,12 +44,12 @@ bool DriveSelector::Mount(DriveTraits *drive) { DriveTraits *DriveSelector::Unmount() { if (!fDrive) return nullptr; - auto drivePointer = fDrive; + DriveTraits *drivePointer = fDrive; fDrive->fUnmount(); fDrive = nullptr; - kcout << "Unmount drive: " << drivePointer->fName << "\n"; + kcout << "[Krnl] unmounted: " << fDrive->fName << "\n"; return drivePointer; } diff --git a/Private/makefile b/Private/makefile index 39e23636..8ea6e7f3 100644 --- a/Private/makefile +++ b/Private/makefile @@ -9,7 +9,7 @@ CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__USE_NEWFS_ ASM = nasm ASMFLAGS = -f win64 LDFLAGS = -e Main -shared --subsystem=10 -LDOBJ = Obj/*.obj +LDOBJ = $(wildcard Obj/*.obj) # This file is the kernel, responsible of task management, memory, drivers and more. KERNEL = HCoreKrnl.exe diff --git a/Public/Kits/SystemKit/CoreAPI.hxx b/Public/Kits/SystemKit/CoreAPI.hxx index fa5e8eef..64373987 100644 --- a/Public/Kits/SystemKit/CoreAPI.hxx +++ b/Public/Kits/SystemKit/CoreAPI.hxx @@ -24,3 +24,8 @@ #define STDCALL __attribute__((stdcall)) #define CDECL __attribute__((cdecl)) +#define MSCALL __attribute__((ms_abi)) + +typedef __UINT16_TYPE__ WORD; +typedef __UINT32_TYPE__ DWORD; +typedef __UINT64_TYPE__ QWORD; diff --git a/Public/Kits/SystemKit/FileAPI.hxx b/Public/Kits/SystemKit/FileAPI.hxx index 350e27f4..7dc3dc74 100644 --- a/Public/Kits/SystemKit/FileAPI.hxx +++ b/Public/Kits/SystemKit/FileAPI.hxx @@ -32,7 +32,8 @@ class HFile final { void Rewind(); public: - void SetMIME(const char *mime); + const char *MIME(); + void MIME(const char *mime); }; typedef HFile *HFilePtr; diff --git a/Public/Kits/SystemKit/SystemCall.hxx b/Public/Kits/SystemKit/SystemCall.hxx deleted file mode 100644 index 64d65d4b..00000000 --- a/Public/Kits/SystemKit/SystemCall.hxx +++ /dev/null @@ -1,18 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - - File: SystemCall.hxx - Purpose: - - Revision History: - - 31/01/24: Added file (amlel) - -------------------------------------------- */ - -#pragma once - -/// @brief System Call Interface - -#include <SystemKit/CoreAPI.hxx> diff --git a/Public/Kits/ZipKit/Zip.cxx b/Public/Kits/ZipKit/Zip.cxx index 74e2a5d8..77fa0a8f 100644 --- a/Public/Kits/ZipKit/Zip.cxx +++ b/Public/Kits/ZipKit/Zip.cxx @@ -29,7 +29,7 @@ HFilePtr ZipStream::FlushToFile(const char *name) { this->fSharedSz = HHeap::Shared()->Size(this->fSharedData); - fp->SetMIME("application/x-bzip"); + fp->MIME("application/x-bzip"); fp->Write(this->fSharedData, this->fSharedSz); return fp; |
