From fb790b07aeba8e22e4190cf3e1834d11ecde6c96 Mon Sep 17 00:00:00 2001 From: Amlal Date: Fri, 25 Apr 2025 13:08:33 +0200 Subject: dev: better .clang-format, ran format command. Signed-off-by: Amlal --- dev/boot/src/BootFileReader.cc | 293 ++++++++++++------------- dev/boot/src/BootString.cc | 97 ++++----- dev/boot/src/BootSupport.cc | 67 +++--- dev/boot/src/BootTextWriter.cc | 214 +++++++++---------- dev/boot/src/BootThread.cc | 378 +++++++++++++++------------------ dev/boot/src/HEL/AMD64/BootATA.cc | 288 +++++++++++-------------- dev/boot/src/HEL/AMD64/BootEFI.cc | 339 ++++++++++++++--------------- dev/boot/src/HEL/AMD64/BootPlatform.cc | 36 ++-- dev/boot/src/HEL/AMD64/BootSATA.cc | 4 +- dev/boot/src/HEL/ARM64/BootEFI.cc | 295 ++++++++++++------------- dev/boot/src/HEL/ARM64/BootPlatform.cc | 28 +-- dev/boot/src/New+Delete.cc | 61 +++--- 12 files changed, 955 insertions(+), 1145 deletions(-) (limited to 'dev/boot/src') diff --git a/dev/boot/src/BootFileReader.cc b/dev/boot/src/BootFileReader.cc index 1236b7f0..32060bd7 100644 --- a/dev/boot/src/BootFileReader.cc +++ b/dev/boot/src/BootFileReader.cc @@ -1,18 +1,18 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - File: FileReader.cc - Purpose: New Boot FileReader, - Read complete file and store it in a buffer. + File: FileReader.cc + Purpose: New Boot FileReader, + Read complete file and store it in a buffer. ------------------------------------------- */ +#include #include #include -#include -#include #include +#include #include /// @file BootFileReader @@ -29,178 +29,149 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// /*** - @brief File Reader constructor. + @brief File Reader constructor. */ -Boot::BootFileReader::BootFileReader(const CharacterTypeUTF16* path, - EfiHandlePtr ImageHandle) -{ - if (path != nullptr) - { - SizeT index = 0UL; - for (; path[index] != L'\0'; ++index) - { - mPath[index] = path[index]; - } - - mPath[index] = 0; - } - - /// Load protocols with their GUIDs. - - EfiGUID guidEfp = EfiGUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); - - EfiSimpleFilesystemProtocol* efp = nullptr; - - EfiLoadImageProtocol* img = nullptr; - EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); - - if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) - { - mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - } - - if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) - { - mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - /// Start doing disk I/O - - if (efp->OpenVolume(efp, &mRootFs) != kEfiOk) - { - mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Volume").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - EfiFileProtocol* fileFs = nullptr; - - if (mRootFs->Open(mRootFs, &fileFs, mPath, kEFIFileRead, kEFIReadOnly) != - kEfiOk) - { - mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Path: ") - .Write(mPath) - .Write(L"\r"); - this->mErrorCode = kNotSupported; - - fb_render_string("BootZ: PLEASE RECOVER YOUR MINKRNL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - - mRootFs->Close(mRootFs); - - return; - } - - mSizeFile = 0; - mFile = fileFs; - mErrorCode = kOperationOkay; +Boot::BootFileReader::BootFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandle) { + if (path != nullptr) { + SizeT index = 0UL; + for (; path[index] != L'\0'; ++index) { + mPath[index] = path[index]; + } + + mPath[index] = 0; + } + + /// Load protocols with their GUIDs. + + EfiGUID guidEfp = EfiGUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); + + EfiSimpleFilesystemProtocol* efp = nullptr; + + EfiLoadImageProtocol* img = nullptr; + EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); + + if (BS->HandleProtocol(ImageHandle, &guidImg, (void**) &img) != kEfiOk) { + mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + } + + if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**) &efp) != kEfiOk) { + mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + /// Start doing disk I/O + + if (efp->OpenVolume(efp, &mRootFs) != kEfiOk) { + mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Volume").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + EfiFileProtocol* fileFs = nullptr; + + if (mRootFs->Open(mRootFs, &fileFs, mPath, kEFIFileRead, kEFIReadOnly) != kEfiOk) { + mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Path: ").Write(mPath).Write(L"\r"); + this->mErrorCode = kNotSupported; + + fb_render_string("BootZ: PLEASE RECOVER YOUR MINKRNL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + + mRootFs->Close(mRootFs); + + return; + } + + mSizeFile = 0; + mFile = fileFs; + mErrorCode = kOperationOkay; } -Boot::BootFileReader::~BootFileReader() -{ - if (this->mFile) - { - this->mFile->Close(this->mFile); - this->mFile = nullptr; - } - - if (this->mRootFs) - { - this->mRootFs->Close(this->mRootFs); - this->mRootFs = nullptr; - } - - if (this->mBlob) - { - BS->FreePool(this->mBlob); - this->mBlob = nullptr; - } - - BSetMem(this->mPath, 0, kPathLen); +Boot::BootFileReader::~BootFileReader() { + if (this->mFile) { + this->mFile->Close(this->mFile); + this->mFile = nullptr; + } + + if (this->mRootFs) { + this->mRootFs->Close(this->mRootFs); + this->mRootFs = nullptr; + } + + if (this->mBlob) { + BS->FreePool(this->mBlob); + this->mBlob = nullptr; + } + + BSetMem(this->mPath, 0, kPathLen); } /** - @brief Reads all of the file into a buffer. - @param **readUntil** size of file - @param **chunkToRead** chunk to read each time. + @brief Reads all of the file into a buffer. + @param **readUntil** size of file + @param **chunkToRead** chunk to read each time. */ -Void Boot::BootFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) -{ - UInt32 szInfo = sizeof(EfiFileInfo); - - EfiFileInfo newPtrInfo{}; - - EfiGUID kFileInfoGUID = EFI_FILE_INFO_GUID; - - if (mFile->GetInfo(mFile, &kFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) - { - readUntil = newPtrInfo.FileSize; - mWriter.Write(L"BootZ: File size: ").Write(readUntil).Write("\r"); - } - - if (readUntil == 0) - { - mErrorCode = kNotSupported; - return; - } - - if (mBlob == nullptr) - { - if (!out_address) - { - if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*)&mBlob) != - kEfiOk) - { - mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); - Boot::ThrowError(L"OutOfMemory", L"Out of memory."); - } - } - else - { - mBlob = (VoidPtr)out_address; - } - } - - mWriter.Write(L"*** Bytes to read: ").Write(readUntil).Write(L" ***\r"); - - UInt64 bufSize = chunkToRead; - UInt64 szCnt = 0UL; - - while (szCnt < readUntil) - { - auto res = mFile->Read(mFile, &bufSize, (VoidPtr)(&((Char*)mBlob)[szCnt])); - - szCnt += bufSize; - - if (res == kBufferTooSmall) - { - bufSize = chunkToRead; - } - } - - mSizeFile = szCnt; - mErrorCode = kOperationOkay; +Void Boot::BootFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) { + UInt32 szInfo = sizeof(EfiFileInfo); + + EfiFileInfo newPtrInfo{}; + + EfiGUID kFileInfoGUID = EFI_FILE_INFO_GUID; + + if (mFile->GetInfo(mFile, &kFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) { + readUntil = newPtrInfo.FileSize; + mWriter.Write(L"BootZ: File size: ").Write(readUntil).Write("\r"); + } + + if (readUntil == 0) { + mErrorCode = kNotSupported; + return; + } + + if (mBlob == nullptr) { + if (!out_address) { + if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*) &mBlob) != kEfiOk) { + mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); + Boot::ThrowError(L"OutOfMemory", L"Out of memory."); + } + } else { + mBlob = (VoidPtr) out_address; + } + } + + mWriter.Write(L"*** Bytes to read: ").Write(readUntil).Write(L" ***\r"); + + UInt64 bufSize = chunkToRead; + UInt64 szCnt = 0UL; + + while (szCnt < readUntil) { + auto res = mFile->Read(mFile, &bufSize, (VoidPtr) (&((Char*) mBlob)[szCnt])); + + szCnt += bufSize; + + if (res == kBufferTooSmall) { + bufSize = chunkToRead; + } + } + + mSizeFile = szCnt; + mErrorCode = kOperationOkay; } /// @brief error code getter. /// @return the error code. -Int32& Boot::BootFileReader::Error() -{ - return mErrorCode; +Int32& Boot::BootFileReader::Error() { + return mErrorCode; } /// @brief blob getter. /// @return the blob. -VoidPtr Boot::BootFileReader::Blob() -{ - return mBlob; +VoidPtr Boot::BootFileReader::Blob() { + return mBlob; } /// @breif Size getter. /// @return the size of the file. -UInt64& Boot::BootFileReader::Size() -{ - return mSizeFile; +UInt64& Boot::BootFileReader::Size() { + return mSizeFile; } diff --git a/dev/boot/src/BootString.cc b/dev/boot/src/BootString.cc index 7efa5bca..8762a151 100644 --- a/dev/boot/src/BootString.cc +++ b/dev/boot/src/BootString.cc @@ -1,92 +1,81 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - File: BootString.cc - Purpose: BootZ string library + File: BootString.cc + Purpose: BootZ string library - Revision History: + Revision History: ------------------------------------------- */ +#include #include #include -#include /// BUGS: 0 ///////////////////////////////////////////////////////////////////////////////////////////////////////// -Kernel::SizeT Boot::BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, const Kernel::SizeT len) -{ - if (!dest || !src) - return 0; +Kernel::SizeT Boot::BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, + const Kernel::SizeT len) { + if (!dest || !src) return 0; - SizeT index = 0UL; - for (; index < len; ++index) - { - dest[index] = src[index]; - } + SizeT index = 0UL; + for (; index < len; ++index) { + dest[index] = src[index]; + } - return index; + return index; } -Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) -{ - if (!ptr) - return 0; +Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) { + if (!ptr) return 0; - Kernel::SizeT cnt = 0; + Kernel::SizeT cnt = 0; - while (*ptr != (CharacterTypeUTF16)0) - { - ++ptr; - ++cnt; - } + while (*ptr != (CharacterTypeUTF16) 0) { + ++ptr; + ++cnt; + } - return cnt; + return cnt; } -Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, const Kernel::SizeT len) -{ - if (!src) - return 0; +Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, + const Kernel::SizeT len) { + if (!src) return 0; - Kernel::SizeT cnt = 0UL; + Kernel::SizeT cnt = 0UL; - while (*src != 0) - { - if (cnt > len) - break; + while (*src != 0) { + if (cnt > len) break; - *src = byte; - ++src; + *src = byte; + ++src; - ++cnt; - } + ++cnt; + } - return cnt; + return cnt; } -Kernel::SizeT Boot::BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, const Kernel::SizeT len) -{ - if (!src) - return 0; +Kernel::SizeT Boot::BSetMem(CharacterTypeUTF8* src, const CharacterTypeUTF8 byte, + const Kernel::SizeT len) { + if (!src) return 0; - Kernel::SizeT cnt = 0UL; + Kernel::SizeT cnt = 0UL; - while (*src != 0) - { - if (cnt > len) - break; + while (*src != 0) { + if (cnt > len) break; - *src = byte; - ++src; + *src = byte; + ++src; - ++cnt; - } + ++cnt; + } - return cnt; + return cnt; } diff --git a/dev/boot/src/BootSupport.cc b/dev/boot/src/BootSupport.cc index ade8ed01..ce824e0f 100644 --- a/dev/boot/src/BootSupport.cc +++ b/dev/boot/src/BootSupport.cc @@ -1,14 +1,14 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ #include +#include #include #include #include -#include #include #include @@ -18,62 +18,51 @@ /// @param dst destination pointer. /// @param byte value to fill in. /// @param len length of of src. -EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) -{ - for (size_t i = 0UL; i < len; ++i) - { - ((int*)dst)[i] = byte; - } - - return dst; +EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) { + for (size_t i = 0UL; i < len; ++i) { + ((int*) dst)[i] = byte; + } + + return dst; } /// @brief memcpy definition in C++. /// @param dst destination pointer. /// @param src source pointer. /// @param len length of of src. -EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) -{ - for (size_t i = 0UL; i < len; ++i) - { - ((int*)dst)[i] = ((int*)src)[i]; - } - - return dst; +EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) { + for (size_t i = 0UL; i < len; ++i) { + ((int*) dst)[i] = ((int*) src)[i]; + } + + return dst; } /// @brief strlen definition in C++. -EXTERN_C size_t strlen(const char* whatToCheck) -{ - SizeT len = 0; +EXTERN_C size_t strlen(const char* whatToCheck) { + SizeT len = 0; - while (whatToCheck[len] != 0) - { - ++len; - } + while (whatToCheck[len] != 0) { + ++len; + } - return len; + return len; } /// @brief strcmp definition in C++. -EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) -{ - SizeT len = 0; +EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) { + SizeT len = 0; - while (whatToCheck[len] == whatToCheckRight[len]) - { - if (whatToCheck[len] == 0) - return 0; + while (whatToCheck[len] == whatToCheckRight[len]) { + if (whatToCheck[len] == 0) return 0; - ++len; - } + ++len; + } - return len; + return len; } /// @brief something specific to the Microsoft's ABI, When the stack grows too big. -EXTERN_C void ___chkstk_ms(void) -{ -} +EXTERN_C void ___chkstk_ms(void) {} #endif diff --git a/dev/boot/src/BootTextWriter.cc b/dev/boot/src/BootTextWriter.cc index 95e248a2..5e826c6d 100644 --- a/dev/boot/src/BootTextWriter.cc +++ b/dev/boot/src/BootTextWriter.cc @@ -1,20 +1,20 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - File: BootTextWriter.cc - Purpose: BootZ string library + File: BootTextWriter.cc + Purpose: BootZ string library - Revision History: + Revision History: ------------------------------------------- */ -#include +#include #include #include -#include +#include ///////////////////////////////////////////////////////////////////////////////////////////////////////// /// BUGS: 0 /// @@ -23,147 +23,123 @@ /** @brief puts wrapper over EFI ConOut. */ -Boot::BootTextWriter& Boot::BootTextWriter::Write(const CharacterTypeUTF16* str) -{ +Boot::BootTextWriter& Boot::BootTextWriter::Write(const CharacterTypeUTF16* str) { #ifdef __DEBUG__ - if (!str || *str == 0) - return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) - { - if (str[i] == '\r') - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - else - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#endif // ifdef __DEBUG__ - - return *this; + if (!str || *str == 0) return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#endif // ifdef __DEBUG__ + + return *this; } /// @brief UTF-8 equivalent of Write (UTF-16). /// @param str the input string. -Boot::BootTextWriter& Boot::BootTextWriter::Write(const Char* str) -{ +Boot::BootTextWriter& Boot::BootTextWriter::Write(const Char* str) { #ifdef __DEBUG__ - if (!str || *str == 0) - return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) - { - if (str[i] == '\r') - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - else - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#endif // ifdef __DEBUG__ - - return *this; + if (!str || *str == 0) return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#endif // ifdef __DEBUG__ + + return *this; } -Boot::BootTextWriter& Boot::BootTextWriter::Write(const UChar* str) -{ +Boot::BootTextWriter& Boot::BootTextWriter::Write(const UChar* str) { #ifdef __DEBUG__ - if (!str || *str == 0) - return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) - { - if (str[i] == '\r') - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - else - { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#endif // ifdef __DEBUG__ - - return *this; + if (!str || *str == 0) return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#endif // ifdef __DEBUG__ + + return *this; } /** @brief putc wrapper over EFI ConOut. */ -Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) -{ +Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) { #ifdef __DEBUG__ - EfiCharType str[2]; + EfiCharType str[2]; - str[0] = c; - str[1] = 0; - ST->ConOut->OutputString(ST->ConOut, str); -#endif // ifdef __DEBUG__ + str[0] = c; + str[1] = 0; + ST->ConOut->OutputString(ST->ConOut, str); +#endif // ifdef __DEBUG__ - return *this; + return *this; } -Boot::BootTextWriter& Boot::BootTextWriter::Write(const UInt64& x) -{ +Boot::BootTextWriter& Boot::BootTextWriter::Write(const UInt64& x) { #ifdef __DEBUG__ - this->_Write(x); - this->Write("h"); -#endif // ifdef __DEBUG__ + this->_Write(x); + this->Write("h"); +#endif // ifdef __DEBUG__ - return *this; + return *this; } -Boot::BootTextWriter& Boot::BootTextWriter::_Write(const UInt64& x) -{ +Boot::BootTextWriter& Boot::BootTextWriter::_Write(const UInt64& x) { #ifdef __DEBUG__ - UInt64 y = (x > 0 ? x : -x) / 16; - UInt64 h = (x > 0 ? x : -x) % 16; + UInt64 y = (x > 0 ? x : -x) / 16; + UInt64 h = (x > 0 ? x : -x) % 16; - if (y) - this->_Write(y); + if (y) this->_Write(y); - /* fail if the hex number is not base-16 */ - if (h > 16) - { - this->WriteCharacter('?'); - return *this; - } + /* fail if the hex number is not base-16 */ + if (h > 16) { + this->WriteCharacter('?'); + return *this; + } - if (y == ~0UL) - y = -y; + if (y == ~0UL) y = -y; - const char cNumbers[] = "0123456789ABCDEF"; + const char cNumbers[] = "0123456789ABCDEF"; - this->WriteCharacter(cNumbers[h]); -#endif // ifdef __DEBUG__ + this->WriteCharacter(cNumbers[h]); +#endif // ifdef __DEBUG__ - return *this; + return *this; } diff --git a/dev/boot/src/BootThread.cc b/dev/boot/src/BootThread.cc index b6236df8..ada864bb 100644 --- a/dev/boot/src/BootThread.cc +++ b/dev/boot/src/BootThread.cc @@ -1,18 +1,18 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ +#include #include #include -#include #include -#include -#include -#include #include +#include +#include +#include #include /// @brief External boot services symbol. @@ -20,211 +20,187 @@ EXTERN EfiBootServices* BS; /// @note BootThread doesn't parse the symbols so doesn't nullify them, .bss is though. -namespace Boot -{ - EXTERN_C Int32 rt_jump_to_address(VoidPtr code, HEL::BootInfoHeader* handover, UInt8* stack); +namespace Boot { +EXTERN_C Int32 rt_jump_to_address(VoidPtr code, HEL::BootInfoHeader* handover, UInt8* stack); - BootThread::BootThread(VoidPtr blob) - : fStartAddress(nullptr), fBlob(blob) - { - // detect the format. - const Char* blob_bytes = reinterpret_cast(fBlob); +BootThread::BootThread(VoidPtr blob) : fStartAddress(nullptr), fBlob(blob) { + // detect the format. + const Char* blob_bytes = reinterpret_cast(fBlob); - BootTextWriter writer; + BootTextWriter writer; - if (!blob_bytes) - { - // failed to provide a valid pointer. - return; - } + if (!blob_bytes) { + // failed to provide a valid pointer. + return; + } - if (blob_bytes[0] == kMagMz0 && - blob_bytes[1] == kMagMz1) - { - LDR_EXEC_HEADER_PTR header_ptr = CF::ldr_find_exec_header(blob_bytes); - LDR_OPTIONAL_HEADER_PTR opt_header_ptr = CF::ldr_find_opt_exec_header(blob_bytes); + if (blob_bytes[0] == kMagMz0 && blob_bytes[1] == kMagMz1) { + LDR_EXEC_HEADER_PTR header_ptr = CF::ldr_find_exec_header(blob_bytes); + LDR_OPTIONAL_HEADER_PTR opt_header_ptr = CF::ldr_find_opt_exec_header(blob_bytes); - if (!header_ptr || !opt_header_ptr) - return; + if (!header_ptr || !opt_header_ptr) return; #ifdef __NE_AMD64__ - if (header_ptr->Machine != kPeMachineAMD64 || - header_ptr->Signature != kPeSignature) - { - writer.Write("BootZ: Not a PE32+ executable.\r"); - return; - } + if (header_ptr->Machine != kPeMachineAMD64 || header_ptr->Signature != kPeSignature) { + writer.Write("BootZ: Not a PE32+ executable.\r"); + return; + } #elif defined(__NE_ARM64__) - if (header_ptr->Machine != kPeMachineARM64 || - header_ptr->Signature != kPeSignature) - { - writer.Write("BootZ: Not a PE32+ executable.\r"); - return; - } -#endif // __NE_AMD64__ || __NE_ARM64__ - - writer.Write("BootZ: PE32+ executable detected (NeKernel Subsystem).\r"); - - auto numSecs = header_ptr->NumberOfSections; - - writer.Write("BootZ: Major Linker Ver: ").Write(opt_header_ptr->MajorLinkerVersion).Write("\r"); - writer.Write("BootZ: Minor Linker Ver: ").Write(opt_header_ptr->MinorLinkerVersion).Write("\r"); - writer.Write("BootZ: Major Subsystem Ver: ").Write(opt_header_ptr->MajorSubsystemVersion).Write("\r"); - writer.Write("BootZ: Minor Subsystem Ver: ").Write(opt_header_ptr->MinorSubsystemVersion).Write("\r"); - writer.Write("BootZ: Magic: ").Write(header_ptr->Signature).Write("\r"); - - EfiPhysicalAddress loadStartAddress = opt_header_ptr->ImageBase; - loadStartAddress += opt_header_ptr->BaseOfData; - - writer.Write("BootZ: Image base: ").Write(loadStartAddress).Write("\r"); - - fStack = new UInt8[mib_cast(16)]; - - if (!fStack) - { - writer.Write("BootZ: Unable to allocate stack.\r"); - return; - } - - LDR_SECTION_HEADER_PTR sectPtr = (LDR_SECTION_HEADER_PTR)(((Char*)opt_header_ptr) + header_ptr->SizeOfOptionalHeader); - - constexpr auto sectionForCode = ".text"; - constexpr auto sectionForBootZ = ".ldr"; - - for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) - { - LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; - - auto sz = sect->VirtualSize; - - if (sect->SizeOfRawData > 0) - sz = sect->SizeOfRawData; - - SetMem((VoidPtr)(loadStartAddress + sect->VirtualAddress), 0, sz); - - if (sect->SizeOfRawData > 0) - { - CopyMem((VoidPtr)(loadStartAddress + sect->VirtualAddress), - (VoidPtr)((UIntPtr)fBlob + sect->PointerToRawData), - sect->SizeOfRawData); - } - - if (StrCmp(sectionForCode, sect->Name) == 0) - { - fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + opt_header_ptr->AddressOfEntryPoint); - writer.Write("BootZ: Executable entry address: ").Write((UIntPtr)fStartAddress).Write("\r"); - } - else if (StrCmp(sectionForBootZ, sect->Name) == 0) - { - struct HANDOVER_INFORMATION_STUB - { - UInt64 HandoverMagic; - UInt32 HandoverType; - UInt32 HandoverPad; - UInt32 HandoverArch; - }* handover_struc = (struct HANDOVER_INFORMATION_STUB*)((UIntPtr)fBlob + sect->PointerToRawData); - - if (handover_struc->HandoverMagic != kHandoverMagic) - { + if (header_ptr->Machine != kPeMachineARM64 || header_ptr->Signature != kPeSignature) { + writer.Write("BootZ: Not a PE32+ executable.\r"); + return; + } +#endif // __NE_AMD64__ || __NE_ARM64__ + + writer.Write("BootZ: PE32+ executable detected (NeKernel Subsystem).\r"); + + auto numSecs = header_ptr->NumberOfSections; + + writer.Write("BootZ: Major Linker Ver: ").Write(opt_header_ptr->MajorLinkerVersion).Write("\r"); + writer.Write("BootZ: Minor Linker Ver: ").Write(opt_header_ptr->MinorLinkerVersion).Write("\r"); + writer.Write("BootZ: Major Subsystem Ver: ") + .Write(opt_header_ptr->MajorSubsystemVersion) + .Write("\r"); + writer.Write("BootZ: Minor Subsystem Ver: ") + .Write(opt_header_ptr->MinorSubsystemVersion) + .Write("\r"); + writer.Write("BootZ: Magic: ").Write(header_ptr->Signature).Write("\r"); + + EfiPhysicalAddress loadStartAddress = opt_header_ptr->ImageBase; + loadStartAddress += opt_header_ptr->BaseOfData; + + writer.Write("BootZ: Image base: ").Write(loadStartAddress).Write("\r"); + + fStack = new UInt8[mib_cast(16)]; + + if (!fStack) { + writer.Write("BootZ: Unable to allocate stack.\r"); + return; + } + + LDR_SECTION_HEADER_PTR sectPtr = + (LDR_SECTION_HEADER_PTR) (((Char*) opt_header_ptr) + header_ptr->SizeOfOptionalHeader); + + constexpr auto sectionForCode = ".text"; + constexpr auto sectionForBootZ = ".ldr"; + + for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) { + LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; + + auto sz = sect->VirtualSize; + + if (sect->SizeOfRawData > 0) sz = sect->SizeOfRawData; + + SetMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), 0, sz); + + if (sect->SizeOfRawData > 0) { + CopyMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), + (VoidPtr) ((UIntPtr) fBlob + sect->PointerToRawData), sect->SizeOfRawData); + } + + if (StrCmp(sectionForCode, sect->Name) == 0) { + fStartAddress = + (VoidPtr) ((UIntPtr) loadStartAddress + opt_header_ptr->AddressOfEntryPoint); + writer.Write("BootZ: Executable entry address: ") + .Write((UIntPtr) fStartAddress) + .Write("\r"); + } else if (StrCmp(sectionForBootZ, sect->Name) == 0) { + struct HANDOVER_INFORMATION_STUB { + UInt64 HandoverMagic; + UInt32 HandoverType; + UInt32 HandoverPad; + UInt32 HandoverArch; + }* handover_struc = + (struct HANDOVER_INFORMATION_STUB*) ((UIntPtr) fBlob + sect->PointerToRawData); + + if (handover_struc->HandoverMagic != kHandoverMagic) { #ifdef __NE_AMD64__ - if (handover_struc->HandoverArch != HEL::kArchAMD64) - { - writer.Write("BootZ: Not an handover header, bad CPU...\r"); - } + if (handover_struc->HandoverArch != HEL::kArchAMD64) { + writer.Write("BootZ: Not an handover header, bad CPU...\r"); + } #elif defined(__NE_ARM64__) - if (handover_struc->HandoverArch != HEL::kArchARM64) - { - writer.Write("BootZ: Not an handover header, bad CPU...\r"); - } + if (handover_struc->HandoverArch != HEL::kArchARM64) { + writer.Write("BootZ: Not an handover header, bad CPU...\r"); + } #endif - writer.Write("BootZ: Not an handover header...\r"); - ::Boot::Stop(); - } - } - - writer.Write("BootZ: Raw offset: ").Write(sect->PointerToRawData).Write(" of ").Write(sect->Name).Write("\r"); - - CopyMem((VoidPtr)(loadStartAddress + sect->VirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->PointerToRawData), sect->SizeOfRawData); - } - } - else if (blob_bytes[0] == kPefMagic[0] && - blob_bytes[1] == kPefMagic[1] && - blob_bytes[2] == kPefMagic[2] && - blob_bytes[3] == kPefMagic[3]) - { - // ========================================= // - // PEF executable has been detected. - // ========================================= // - - fStartAddress = nullptr; - - writer.Write("BootZ: PEF executable detected, won't load it.\r"); - writer.Write("BootZ: note: PEF executables aren't supported for now.\r"); - } - else - { - writer.Write("BootZ: Invalid Executable.\r"); - } - } - - /// @note handover header has to be valid! - Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) - { - fHandover = handover; - - if (!fStartAddress) - { - return kEfiFail; - } - - if (!fHandover) - { - return kEfiFail; - } - - BootTextWriter writer; - - writer.Write("BootZ: Starting: ").Write(fBlobName).Write("\r"); - writer.Write("BootZ: Handover address: ").Write((UIntPtr)fHandover).Write("\r"); - - if (own_stack) - { - writer.Write("BootZ: Using it's own stack.\r"); - writer.Write("BootZ: Stack address: ").Write((UIntPtr)&fStack[mib_cast(16) - 1]).Write("\r"); - writer.Write("BootZ: Stack size: ").Write(mib_cast(16)).Write("\r"); - - auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[mib_cast(16) - 1]); - - // we don't need the stack anymore. - - delete[] fStack; - fStack = nullptr; - - return ret; - } - else - { - writer.Write("BootZ: Using Bootloader's stack.\r"); - - return reinterpret_cast(fStartAddress)(fHandover); - } - - return kEfiFail; - } - - const Char* BootThread::GetName() - { - return fBlobName; - } - - Void BootThread::SetName(const Char* name) - { - CopyMem(fBlobName, name, StrLen(name)); - } - - bool BootThread::IsValid() - { - return fStartAddress != nullptr; - } -} // namespace Boot \ No newline at end of file + writer.Write("BootZ: Not an handover header...\r"); + ::Boot::Stop(); + } + } + + writer.Write("BootZ: Raw offset: ") + .Write(sect->PointerToRawData) + .Write(" of ") + .Write(sect->Name) + .Write("\r"); + + CopyMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), + (VoidPtr) ((UIntPtr) fBlob + sect->PointerToRawData), sect->SizeOfRawData); + } + } else if (blob_bytes[0] == kPefMagic[0] && blob_bytes[1] == kPefMagic[1] && + blob_bytes[2] == kPefMagic[2] && blob_bytes[3] == kPefMagic[3]) { + // ========================================= // + // PEF executable has been detected. + // ========================================= // + + fStartAddress = nullptr; + + writer.Write("BootZ: PEF executable detected, won't load it.\r"); + writer.Write("BootZ: note: PEF executables aren't supported for now.\r"); + } else { + writer.Write("BootZ: Invalid Executable.\r"); + } +} + +/// @note handover header has to be valid! +Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { + fHandover = handover; + + if (!fStartAddress) { + return kEfiFail; + } + + if (!fHandover) { + return kEfiFail; + } + + BootTextWriter writer; + + writer.Write("BootZ: Starting: ").Write(fBlobName).Write("\r"); + writer.Write("BootZ: Handover address: ").Write((UIntPtr) fHandover).Write("\r"); + + if (own_stack) { + writer.Write("BootZ: Using it's own stack.\r"); + writer.Write("BootZ: Stack address: ").Write((UIntPtr) &fStack[mib_cast(16) - 1]).Write("\r"); + writer.Write("BootZ: Stack size: ").Write(mib_cast(16)).Write("\r"); + + auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[mib_cast(16) - 1]); + + // we don't need the stack anymore. + + delete[] fStack; + fStack = nullptr; + + return ret; + } else { + writer.Write("BootZ: Using Bootloader's stack.\r"); + + return reinterpret_cast(fStartAddress)(fHandover); + } + + return kEfiFail; +} + +const Char* BootThread::GetName() { + return fBlobName; +} + +Void BootThread::SetName(const Char* name) { + CopyMem(fBlobName, name, StrLen(name)); +} + +bool BootThread::IsValid() { + return fStartAddress != nullptr; +} +} // namespace Boot \ No newline at end of file diff --git a/dev/boot/src/HEL/AMD64/BootATA.cc b/dev/boot/src/HEL/AMD64/BootATA.cc index 8b1b5a0a..693513e0 100644 --- a/dev/boot/src/HEL/AMD64/BootATA.cc +++ b/dev/boot/src/HEL/AMD64/BootATA.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -15,9 +15,9 @@ * */ -#include #include #include +#include /// bugs: 0 @@ -25,159 +25,143 @@ using namespace Boot; #define kATADataLen (256) -static Boolean kATADetected = false; +static Boolean kATADetected = false; static UInt16 kATAData[kATADataLen] = {0}; Boolean boot_ata_detected(Void); -STATIC Boolean boot_ata_wait_io(UInt16 IO) -{ - for (int i = 0; i < 400; i++) - rt_in8(IO + ATA_REG_STATUS); +STATIC Boolean boot_ata_wait_io(UInt16 IO) { + for (int i = 0; i < 400; i++) rt_in8(IO + ATA_REG_STATUS); ATAWaitForIO_Retry: - auto status_rdy = rt_in8(IO + ATA_REG_STATUS); + auto status_rdy = rt_in8(IO + ATA_REG_STATUS); - if ((status_rdy & ATA_SR_BSY)) - goto ATAWaitForIO_Retry; + if ((status_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry; ATAWaitForIO_Retry2: - status_rdy = rt_in8(IO + ATA_REG_STATUS); + status_rdy = rt_in8(IO + ATA_REG_STATUS); - if (status_rdy & ATA_SR_ERR) - return false; + if (status_rdy & ATA_SR_ERR) return false; - if (!(status_rdy & ATA_SR_DRDY)) - goto ATAWaitForIO_Retry2; + if (!(status_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2; - return true; + return true; } -Void boot_ata_select(UInt16 Bus) -{ - if (Bus == ATA_PRIMARY_IO) - rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); - else - rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); +Void boot_ata_select(UInt16 Bus) { + if (Bus == ATA_PRIMARY_IO) + rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); + else + rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); } -Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) -{ - NE_UNUSED(Drive); +Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { + NE_UNUSED(Drive); - if (boot_ata_detected()) - return true; + if (boot_ata_detected()) return true; - BootTextWriter writer; + BootTextWriter writer; - UInt16 IO = Bus; + UInt16 IO = Bus; - boot_ata_select(IO); + boot_ata_select(IO); - // Bus init, NEIN bit. - rt_out8(IO + ATA_REG_NEIN, 1); + // Bus init, NEIN bit. + rt_out8(IO + ATA_REG_NEIN, 1); - // identify until it's good. + // identify until it's good. ATAInit_Retry: - auto status_rdy = rt_in8(IO + ATA_REG_STATUS); + auto status_rdy = rt_in8(IO + ATA_REG_STATUS); - if (status_rdy & ATA_SR_ERR) - { - writer.Write( - L"BootZ: ATA: Not an IDE based drive.\r"); + if (status_rdy & ATA_SR_ERR) { + writer.Write(L"BootZ: ATA: Not an IDE based drive.\r"); - return false; - } + return false; + } - if ((status_rdy & ATA_SR_BSY)) - goto ATAInit_Retry; + if ((status_rdy & ATA_SR_BSY)) goto ATAInit_Retry; - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - /// fetch serial info - /// model, speed, number of sectors... + /// fetch serial info + /// model, speed, number of sectors... - boot_ata_wait_io(IO); + boot_ata_wait_io(IO); - for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) - { - kATAData[indexData] = Kernel::HAL::rt_in16(IO + ATA_REG_DATA); - } + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { + kATAData[indexData] = Kernel::HAL::rt_in16(IO + ATA_REG_DATA); + } - OutBus = - (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; + OutBus = (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; - OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; + OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; - return true; + return true; } -Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) -{ - Lba /= SectorSz; +Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, + SizeT Size) { + Lba /= SectorSz; - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - boot_ata_wait_io(IO); - boot_ata_select(IO); + boot_ata_wait_io(IO); + boot_ata_select(IO); - rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); + rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba)&0xFF); - rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); - rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); - rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); + rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); + rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); + rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); + rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - boot_ata_wait_io(IO); + boot_ata_wait_io(IO); - for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) - { - boot_ata_wait_io(IO); - Buf[IndexOff] = Kernel::HAL::rt_in16(IO + ATA_REG_DATA); - boot_ata_wait_io(IO); - } + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) { + boot_ata_wait_io(IO); + Buf[IndexOff] = Kernel::HAL::rt_in16(IO + ATA_REG_DATA); + boot_ata_wait_io(IO); + } } -Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, SizeT Size) -{ - Lba /= SectorSz; +Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeUTF8* Buf, SizeT SectorSz, + SizeT Size) { + Lba /= SectorSz; - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - boot_ata_wait_io(IO); - boot_ata_select(IO); + boot_ata_wait_io(IO); + boot_ata_select(IO); - rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); + rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba)&0xFF); - rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); - rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); - rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); + rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); + rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); + rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); + rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); - boot_ata_wait_io(IO); + boot_ata_wait_io(IO); - for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) - { - boot_ata_wait_io(IO); - rt_out16(IO + ATA_REG_DATA, Buf[IndexOff]); - boot_ata_wait_io(IO); - } + for (SizeT IndexOff = 0; IndexOff < Size; ++IndexOff) { + boot_ata_wait_io(IO); + rt_out16(IO + ATA_REG_DATA, Buf[IndexOff]); + boot_ata_wait_io(IO); + } - boot_ata_wait_io(IO); + boot_ata_wait_io(IO); } /// @check is ATA detected? -Boolean boot_ata_detected(Void) -{ - return kATADetected; +Boolean boot_ata_detected(Void) { + return kATADetected; } /*** @@ -192,93 +176,79 @@ Boolean boot_ata_detected(Void) * @brief ATA Device constructor. * @param void none. */ -BootDeviceATA::BootDeviceATA() noexcept -{ - if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, - this->Leak().mMaster) || - boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, - this->Leak().mMaster)) - { - kATADetected = true; - } +BootDeviceATA::BootDeviceATA() noexcept { + if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, this->Leak().mMaster) || + boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, this->Leak().mMaster)) { + kATADetected = true; + } } /** * @brief Is ATA detected? */ -BootDeviceATA::operator bool() -{ - return boot_ata_detected(); +BootDeviceATA::operator bool() { + return boot_ata_detected(); } /** - @brief Read Buf from disk - @param Sz Sector size - @param Buf buffer + @brief Read Buf from disk + @param Sz Sector size + @param Buf buffer */ -BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, SizeT SectorSz) -{ - if (!boot_ata_detected()) - { - Leak().mErr = true; - return *this; - } +BootDeviceATA& BootDeviceATA::Read(CharacterTypeUTF8* Buf, SizeT SectorSz) { + if (!boot_ata_detected()) { + Leak().mErr = true; + return *this; + } - this->Leak().mErr = false; + this->Leak().mErr = false; - if (!Buf || SectorSz < 1) - return *this; + if (!Buf || SectorSz < 1) return *this; - boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, - Buf, SectorSz, this->Leak().mSize); + boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, + this->Leak().mSize); - return *this; + return *this; } /** - @brief Write Buf into disk - @param Sz Sector size - @param Buf buffer + @brief Write Buf into disk + @param Sz Sector size + @param Buf buffer */ -BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, SizeT SectorSz) -{ - if (!boot_ata_detected()) - { - Leak().mErr = true; - return *this; - } - - Leak().mErr = false; - - if (!Buf || SectorSz < 1 || this->Leak().mSize < 1) - { - Leak().mErr = true; - return *this; - } - - boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, - Buf, SectorSz, this->Leak().mSize); - - return *this; +BootDeviceATA& BootDeviceATA::Write(CharacterTypeUTF8* Buf, SizeT SectorSz) { + if (!boot_ata_detected()) { + Leak().mErr = true; + return *this; + } + + Leak().mErr = false; + + if (!Buf || SectorSz < 1 || this->Leak().mSize < 1) { + Leak().mErr = true; + return *this; + } + + boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, + this->Leak().mSize); + + return *this; } /** * @brief ATA trait getter. * @return BootDeviceATA::ATATrait& the drive config. */ -BootDeviceATA::ATATrait& BootDeviceATA::Leak() -{ - return mTrait; +BootDeviceATA::ATATrait& BootDeviceATA::Leak() { + return mTrait; } /*** - @brief Getter, gets the number of sectors inside the drive. + @brief Getter, gets the number of sectors inside the drive. */ -SizeT BootDeviceATA::GetSectorsCount() noexcept -{ - return (kATAData[61] << 16) | kATAData[60]; +SizeT BootDeviceATA::GetSectorsCount() noexcept { + return (kATAData[61] << 16) | kATAData[60]; } -SizeT BootDeviceATA::GetDiskSize() noexcept -{ - return this->GetSectorsCount() * BootDeviceATA::kSectorSize; +SizeT BootDeviceATA::GetDiskSize() noexcept { + return this->GetSectorsCount() * BootDeviceATA::kSectorSize; } diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index 6411383d..739876da 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -1,12 +1,11 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ #include -#include -#include +#include #include #include #include @@ -15,265 +14,249 @@ #include #include #include -#include #include +#include // Makes the compiler shut up. #ifndef kMachineModel #define kMachineModel "OS" -#endif // !kMachineModel +#endif // !kMachineModel /** Graphics related. */ -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EfiGUID kGopGuid; +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EfiGUID kGopGuid; /** Related to jumping to the reset vector. */ EXTERN_C Void rt_reset_hardware(); /** - @brief Finds and stores the GOP object. + @brief Finds and stores the GOP object. */ -STATIC Bool boot_init_fb() noexcept -{ - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; +STATIC Bool boot_init_fb() noexcept { + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop) != kEfiOk) - return No; + if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; - kGopStride = 4; + kGopStride = 4; - return Yes; + return Yes; } EfiGUID kEfiGlobalNamespaceVarGUID = { - 0x8BE4DF61, 0x93CA, 0x11D2, {0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}}; + 0x8BE4DF61, 0x93CA, 0x11D2, {0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}}; /// @brief BootloaderMain EFI entrypoint. /// @param image_handle Handle of this image. /// @param sys_table The system table of it. /// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, - EfiSystemTable* sys_table) -{ - fw_init_efi(sys_table); ///! Init the EFI library. +EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { + fw_init_efi(sys_table); ///! Init the EFI library. + + ST->ConOut->ClearScreen(sys_table->ConOut); + ST->ConOut->SetAttribute(sys_table->ConOut, kEFIYellow); + + ST->BootServices->SetWatchdogTimer(0, 0, 0, nullptr); + ST->ConOut->EnableCursor(ST->ConOut, false); - ST->ConOut->ClearScreen(sys_table->ConOut); - ST->ConOut->SetAttribute(sys_table->ConOut, kEFIYellow); + HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); - ST->BootServices->SetWatchdogTimer(0, 0, 0, nullptr); - ST->ConOut->EnableCursor(ST->ConOut, false); + UInt32 map_key = 0; + UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + EfiMemoryDescriptor* struct_ptr = nullptr; + UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 rev_desc = 0; - HEL::BootInfoHeader* handover_hdr = - new HEL::BootInfoHeader(); + Boot::BootTextWriter writer; - UInt32 map_key = 0; - UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); - EfiMemoryDescriptor* struct_ptr = nullptr; - UInt32 sz_desc = sizeof(EfiMemoryDescriptor); - UInt32 rev_desc = 0; - - Boot::BootTextWriter writer; + if (!boot_init_fb()) { + writer.Write("BootZ: Invalid Framebuffer, can't boot to NeKernel.\r"); + Boot::Stop(); + } - if (!boot_init_fb()) - { - writer.Write("BootZ: Invalid Framebuffer, can't boot to NeKernel.\r"); - Boot::Stop(); - } - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; - ++index_vt) - { - Char* vendor_table = reinterpret_cast( - sys_table->ConfigurationTable[index_vt].VendorTable); + for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { + Char* vendor_table = + reinterpret_cast(sys_table->ConfigurationTable[index_vt].VendorTable); - // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) - if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && - vendor_table[2] == 'D' && vendor_table[3] == ' ' && - vendor_table[4] == 'P' && vendor_table[5] == 'T' && - vendor_table[6] == 'R' && vendor_table[7] == ' ') - { - handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr)vendor_table; - break; - } - } + // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) + if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' && + vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' && + vendor_table[6] == 'R' && vendor_table[7] == ' ') { + handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; + break; + } + } - // ------------------------------------------ // - // draw background color. - // ------------------------------------------ // + // ------------------------------------------ // + // draw background color. + // ------------------------------------------ // - handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; - handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; + handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; + handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; + handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; + handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; + handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; + handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // + // ------------------------------------------- // + // Grab MP services, extended to runtime. // + // ------------------------------------------- // - EfiGUID guid_mp = EfiGUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; + EfiGUID guid_mp = EfiGUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EfiMpServicesProtocol* mp = nullptr; - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); + BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); + handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); - kHandoverHeader = handover_hdr; + kHandoverHeader = handover_hdr; - FB::fb_clear_video(); + FB::fb_clear_video(); - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; + UInt32 cnt_enabled = 0; + UInt32 cnt_disabled = 0; - if (mp) - { - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - } - else - { - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; - } + if (mp) { + mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; + } else { + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; + } - // Fill handover header now. + // Fill handover header now. - handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ + handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ - Int32 trials = 5 * 10000000; + Int32 trials = 5 * 10000000; - writer.Write("BootZ: Welcome to BootZ.\r"); - writer.Write("BootZ: Allocating sufficent memory, trying 4GB...\r"); + writer.Write("BootZ: Welcome to BootZ.\r"); + writer.Write("BootZ: Allocating sufficent memory, trying 4GB...\r"); - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) - { - --trials; + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, + &handover_hdr->f_BitMapStart) != kEfiOk) { + --trials; - if (!trials) - { - writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); + if (!trials) { + writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); - trials = 3 * 10000000; + trials = 3 * 10000000; - handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) - { - --trials; + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, + &handover_hdr->f_BitMapStart) != kEfiOk) { + --trials; - if (!trials) - { - writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); - Boot::Stop(); - } - } - } - } + if (!trials) { + writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); + Boot::Stop(); + } + } + } + } - handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; - handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; + handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr) BS; + handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr) ST; - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // - Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); - reader_syschk.ReadAll(0); + Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); + reader_syschk.ReadAll(0); - Boot::BootThread* syschk_thread = nullptr; + Boot::BootThread* syschk_thread = nullptr; - if (reader_syschk.Blob()) - { - syschk_thread = new Boot::BootThread(reader_syschk.Blob()); - syschk_thread->SetName("BootZ/SysChk"); + if (reader_syschk.Blob()) { + syschk_thread = new Boot::BootThread(reader_syschk.Blob()); + syschk_thread->SetName("BootZ/SysChk"); - syschk_thread->Start(handover_hdr, NO); - } + syschk_thread->Start(handover_hdr, NO); + } - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - handover_hdr->f_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; + handover_hdr->f_Magic = kHandoverMagic; + handover_hdr->f_Version = kHandoverVersion; - handover_hdr->f_HardwareTables.f_ImageKey = map_key; - handover_hdr->f_HardwareTables.f_ImageHandle = image_handle; + handover_hdr->f_HardwareTables.f_ImageKey = map_key; + handover_hdr->f_HardwareTables.f_ImageHandle = image_handle; - // Provide fimware vendor name. + // Provide fimware vendor name. - Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, - handover_hdr->f_FirmwareVendorLen); + Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, + handover_hdr->f_FirmwareVendorLen); - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - // Assign to global 'kHandoverHeader'. + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + // Assign to global 'kHandoverHeader'. - WideChar kernel_path[256U] = L"krnl.efi"; - UInt32 kernel_path_sz = 256U; + WideChar kernel_path[256U] = L"krnl.efi"; + UInt32 kernel_path_sz = 256U; - if (ST->RuntimeServices->GetVariable(L"/props/boot_path", kEfiGlobalNamespaceVarGUID, nullptr, &kernel_path_sz, kernel_path) != kEfiOk) - { - /// access attributes (in order) - /// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS - UInt32 attr = 0x00000001 | 0x00000002 | 0x00000004; + if (ST->RuntimeServices->GetVariable(L"/props/boot_path", kEfiGlobalNamespaceVarGUID, nullptr, + &kernel_path_sz, kernel_path) != kEfiOk) { + /// access attributes (in order) + /// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS + UInt32 attr = 0x00000001 | 0x00000002 | 0x00000004; - ST->RuntimeServices->SetVariable(L"/props/boot_path", kEfiGlobalNamespaceVarGUID, &attr, &kernel_path_sz, kernel_path); - } + ST->RuntimeServices->SetVariable(L"/props/boot_path", kEfiGlobalNamespaceVarGUID, &attr, + &kernel_path_sz, kernel_path); + } - UInt32 sz_ver = sizeof(UInt64); - UInt64 ver = KERNEL_VERSION_BCD; + UInt32 sz_ver = sizeof(UInt64); + UInt64 ver = KERNEL_VERSION_BCD; - ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, &ver); + ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, + &ver); - if (ver != KERNEL_VERSION_BCD) - { - ver = KERNEL_VERSION_BCD; + if (ver != KERNEL_VERSION_BCD) { + ver = KERNEL_VERSION_BCD; - ST->RuntimeServices->SetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, &ver); - writer.Write("BootZ: version has been updated: ").Write(ver).Write("\r"); - } + ST->RuntimeServices->SetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, + &sz_ver, &ver); + writer.Write("BootZ: version has been updated: ").Write(ver).Write("\r"); + } - writer.Write("BootZ: version: ").Write(ver).Write("\r"); + writer.Write("BootZ: version: ").Write(ver).Write("\r"); - // boot to kernel, if not netboot this. + // boot to kernel, if not netboot this. - Boot::BootFileReader reader_kernel(kernel_path, image_handle); + Boot::BootFileReader reader_kernel(kernel_path, image_handle); - reader_kernel.ReadAll(0); + reader_kernel.ReadAll(0); - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // - if (reader_kernel.Blob()) - { - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // + if (reader_kernel.Blob()) { + // ------------------------------------------ // + // null these fields, to avoid being reused later. + // ------------------------------------------ // - auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); + auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - kernel_thread.SetName("BootZ/NeKernel"); + kernel_thread.SetName("BootZ/NeKernel"); - handover_hdr->f_KernelImage = reader_kernel.Blob(); - handover_hdr->f_KernelSz = reader_kernel.Size(); + handover_hdr->f_KernelImage = reader_kernel.Blob(); + handover_hdr->f_KernelSz = reader_kernel.Size(); - kernel_thread.Start(handover_hdr, YES); - } + kernel_thread.Start(handover_hdr, YES); + } - Boot::BootFileReader reader_netboot(L"net.efi", image_handle); - reader_netboot.ReadAll(0); + Boot::BootFileReader reader_netboot(L"net.efi", image_handle); + reader_netboot.ReadAll(0); - if (!reader_netboot.Blob()) - return kEfiFail; + if (!reader_netboot.Blob()) return kEfiFail; - auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); - netboot_thread.SetName("BootZ/BootNet"); + auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); + netboot_thread.SetName("BootZ/BootNet"); - return netboot_thread.Start(handover_hdr, NO); + return netboot_thread.Start(handover_hdr, NO); } diff --git a/dev/boot/src/HEL/AMD64/BootPlatform.cc b/dev/boot/src/HEL/AMD64/BootPlatform.cc index 1a1f9b89..8b4d57c4 100644 --- a/dev/boot/src/HEL/AMD64/BootPlatform.cc +++ b/dev/boot/src/HEL/AMD64/BootPlatform.cc @@ -1,50 +1,44 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ +#include #include #include -#include #ifdef __BOOTZ_STANDALONE__ using namespace Boot; -EXTERN_C void rt_hlt() -{ - asm volatile("hlt"); +EXTERN_C void rt_hlt() { + asm volatile("hlt"); } -EXTERN_C void rt_cli() -{ - asm volatile("cli"); +EXTERN_C void rt_cli() { + asm volatile("cli"); } -EXTERN_C void rt_sti() -{ - asm volatile("sti"); +EXTERN_C void rt_sti() { + asm volatile("sti"); } -EXTERN_C void rt_cld() -{ - asm volatile("cld"); +EXTERN_C void rt_cld() { + asm volatile("cld"); } -EXTERN_C void rt_std() -{ - asm volatile("std"); +EXTERN_C void rt_std() { + asm volatile("std"); } #else #include -void rt_hlt() -{ - Kernel::HAL::rt_halt(); +void rt_hlt() { + Kernel::HAL::rt_halt(); } -#endif // __BOOTZ_STANDALONE__ +#endif // __BOOTZ_STANDALONE__ diff --git a/dev/boot/src/HEL/AMD64/BootSATA.cc b/dev/boot/src/HEL/AMD64/BootSATA.cc index e60aebde..ef5d2096 100644 --- a/dev/boot/src/HEL/AMD64/BootSATA.cc +++ b/dev/boot/src/HEL/AMD64/BootSATA.cc @@ -1,6 +1,6 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -15,6 +15,6 @@ * */ +#include #include #include -#include diff --git a/dev/boot/src/HEL/ARM64/BootEFI.cc b/dev/boot/src/HEL/ARM64/BootEFI.cc index 9132cec1..1e5b62f3 100644 --- a/dev/boot/src/HEL/ARM64/BootEFI.cc +++ b/dev/boot/src/HEL/ARM64/BootEFI.cc @@ -1,12 +1,11 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ #include -#include -#include +#include #include #include #include @@ -15,8 +14,8 @@ #include #include #include -#include #include +#include #ifndef kExpectedWidth #define kExpectedWidth (800) @@ -28,43 +27,39 @@ /** Graphics related. */ -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EfiGUID kGopGuid; +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EfiGUID kGopGuid; EXTERN_C Void rt_reset_hardware(); EXTERN EfiBootServices* BS; /** - @brief Finds and stores the GOP object. + @brief Finds and stores the GOP object. */ -STATIC Bool boot_init_fb() noexcept -{ - kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; +STATIC Bool boot_init_fb() noexcept { + kGopGuid = EfiGUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*)&kGop) != kEfiOk) - return No; + if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; - kGopStride = 4; + kGopStride = 4; - for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) - { - EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; - UInt32 sz = 0U; + for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) { + EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; + UInt32 sz = 0U; - kGop->QueryMode(kGop, i, &sz, &infoPtr); + kGop->QueryMode(kGop, i, &sz, &infoPtr); - if (infoPtr->HorizontalResolution == kExpectedWidth && - infoPtr->VerticalResolution == kExpectedHeight) - { - kGop->SetMode(kGop, i); - return Yes; - } - } + if (infoPtr->HorizontalResolution == kExpectedWidth && + infoPtr->VerticalResolution == kExpectedHeight) { + kGop->SetMode(kGop, i); + return Yes; + } + } - return No; + return No; } EXTERN EfiBootServices* BS; @@ -73,179 +68,167 @@ EXTERN EfiBootServices* BS; /// @param image_handle Handle of this image. /// @param sys_table The system table of it. /// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, - EfiSystemTable* sys_table) -{ - fw_init_efi(sys_table); ///! Init the EFI library. +EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { + fw_init_efi(sys_table); ///! Init the EFI library. - HEL::BootInfoHeader* handover_hdr = - new HEL::BootInfoHeader(); + HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); - UInt32 map_key = 0; + UInt32 map_key = 0; #ifdef ZBA_USE_FB - if (!boot_init_fb()) - return 1; ///! Init the GOP. - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; - ++index_vt) - { - Char* vendor_table = reinterpret_cast( - sys_table->ConfigurationTable[index_vt].VendorTable); - - // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) - if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && - vendor_table[2] == 'D' && vendor_table[3] == ' ' && - vendor_table[4] == 'P' && vendor_table[5] == 'T' && - vendor_table[6] == 'R' && vendor_table[7] == ' ') - { - handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr)vendor_table; - break; - } - } + if (!boot_init_fb()) return 1; ///! Init the GOP. + + for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { + Char* vendor_table = + reinterpret_cast(sys_table->ConfigurationTable[index_vt].VendorTable); + + // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) + if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' && + vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' && + vendor_table[6] == 'R' && vendor_table[7] == ' ') { + handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; + break; + } + } - // ------------------------------------------ // - // draw background color. - // ------------------------------------------ // + // ------------------------------------------ // + // draw background color. + // ------------------------------------------ // - handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; - handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; -#endif // ZBA_USE_FB + handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; + handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; + handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; + handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; + handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; + handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; +#endif // ZBA_USE_FB - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // + // ------------------------------------------- // + // Grab MP services, extended to runtime. // + // ------------------------------------------- // - EfiGUID guid_mp = EfiGUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; + EfiGUID guid_mp = EfiGUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EfiMpServicesProtocol* mp = nullptr; - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); + BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); + handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); - // Assign to global 'kHandoverHeader'. - kHandoverHeader = handover_hdr; + // Assign to global 'kHandoverHeader'. + kHandoverHeader = handover_hdr; - fb_init(); + fb_init(); - FB::fb_clear_video(); + FB::fb_clear_video(); - FBDrawBitMapInRegion(zka_disk, NE_DISK_WIDTH, NE_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - NE_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - NE_DISK_HEIGHT) / 2); + FBDrawBitMapInRegion(zka_disk, NE_DISK_WIDTH, NE_DISK_HEIGHT, + (kHandoverHeader->f_GOP.f_Width - NE_DISK_WIDTH) / 2, + (kHandoverHeader->f_GOP.f_Height - NE_DISK_HEIGHT) / 2); - fb_clear(); + fb_clear(); - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; + UInt32 cnt_enabled = 0; + UInt32 cnt_disabled = 0; - if (mp) - { - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - } - else - { - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; - } + if (mp) { + mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; + } else { + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; + } - //-------------------------------------------------------------// - // Allocate heap. - //-------------------------------------------------------------// + //-------------------------------------------------------------// + // Allocate heap. + //-------------------------------------------------------------// - Boot::BootTextWriter writer; + Boot::BootTextWriter writer; - handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ - Int32 trials = 5 * 10000000; + handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ + Int32 trials = 5 * 10000000; - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) - { - --trials; + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, + &handover_hdr->f_BitMapStart) != kEfiOk) { + --trials; - if (!trials) - { - writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); + if (!trials) { + writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); - trials = 3 * 10000000; + trials = 3 * 10000000; - handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ + handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) - { - --trials; + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, + &handover_hdr->f_BitMapStart) != kEfiOk) { + --trials; - if (!trials) - { - writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); - Boot::Stop(); - } - } - } - } + if (!trials) { + writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); + Boot::Stop(); + } + } + } + } - Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); - reader_syschk.ReadAll(0); + Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); + reader_syschk.ReadAll(0); - Boot::BootThread* syschk_thread = nullptr; + Boot::BootThread* syschk_thread = nullptr; - if (reader_syschk.Blob()) - { - syschk_thread = new Boot::BootThread(reader_syschk.Blob()); - syschk_thread->SetName("BootZ: System Check"); + if (reader_syschk.Blob()) { + syschk_thread = new Boot::BootThread(reader_syschk.Blob()); + syschk_thread->SetName("BootZ: System Check"); - if (syschk_thread->Start(handover_hdr, NO) != kEfiOk) - { - fb_init(); + if (syschk_thread->Start(handover_hdr, NO) != kEfiOk) { + fb_init(); - FB::fb_clear_video(); + FB::fb_clear_video(); - FBDrawBitMapInRegion(zka_no_disk, NE_NO_DISK_WIDTH, NE_NO_DISK_HEIGHT, (kHandoverHeader->f_GOP.f_Width - NE_NO_DISK_WIDTH) / 2, (kHandoverHeader->f_GOP.f_Height - NE_NO_DISK_HEIGHT) / 2); + FBDrawBitMapInRegion(zka_no_disk, NE_NO_DISK_WIDTH, NE_NO_DISK_HEIGHT, + (kHandoverHeader->f_GOP.f_Width - NE_NO_DISK_WIDTH) / 2, + (kHandoverHeader->f_GOP.f_Height - NE_NO_DISK_HEIGHT) / 2); - fb_clear(); - } - } + fb_clear(); + } + } - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // + // ------------------------------------------ // + // null these fields, to avoid being reused later. + // ------------------------------------------ // - handover_hdr->f_FirmwareCustomTables[0] = nullptr; - handover_hdr->f_FirmwareCustomTables[1] = nullptr; + handover_hdr->f_FirmwareCustomTables[0] = nullptr; + handover_hdr->f_FirmwareCustomTables[1] = nullptr; - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - handover_hdr->f_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; + handover_hdr->f_Magic = kHandoverMagic; + handover_hdr->f_Version = kHandoverVersion; - // Provide fimware vendor name. + // Provide fimware vendor name. - Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, - handover_hdr->f_FirmwareVendorLen); + Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, + handover_hdr->f_FirmwareVendorLen); - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - Boot::BootFileReader reader_kernel(L"krnl.efi", image_handle); + Boot::BootFileReader reader_kernel(L"krnl.efi", image_handle); - reader_kernel.ReadAll(0); + reader_kernel.ReadAll(0); - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // - if (reader_kernel.Blob()) - { - auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - kernel_thread.SetName("BootZ: Kernel."); + if (reader_kernel.Blob()) { + auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); + kernel_thread.SetName("BootZ: Kernel."); - handover_hdr->f_KernelImage = reader_kernel.Blob(); - handover_hdr->f_KernelSz = reader_kernel.Size(); + handover_hdr->f_KernelImage = reader_kernel.Blob(); + handover_hdr->f_KernelSz = reader_kernel.Size(); - Boot::ExitBootServices(map_key, image_handle); + Boot::ExitBootServices(map_key, image_handle); - kernel_thread.Start(handover_hdr, YES); - } + kernel_thread.Start(handover_hdr, YES); + } - CANT_REACH(); + CANT_REACH(); } diff --git a/dev/boot/src/HEL/ARM64/BootPlatform.cc b/dev/boot/src/HEL/ARM64/BootPlatform.cc index bf359c6c..0f6a738f 100644 --- a/dev/boot/src/HEL/ARM64/BootPlatform.cc +++ b/dev/boot/src/HEL/ARM64/BootPlatform.cc @@ -1,37 +1,27 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ +#include #include #include -#include #ifdef __BOOTZ_STANDALONE__ using namespace Boot; -EXTERN_C void rt_hlt() -{ - while (Yes) - ; +EXTERN_C void rt_hlt() { + while (Yes); } -EXTERN_C void rt_cli() -{ -} +EXTERN_C void rt_cli() {} -EXTERN_C void rt_sti() -{ -} +EXTERN_C void rt_sti() {} -EXTERN_C void rt_cld() -{ -} +EXTERN_C void rt_cld() {} -EXTERN_C void rt_std() -{ -} +EXTERN_C void rt_std() {} -#endif // __BOOTZ_STANDALONE__ +#endif // __BOOTZ_STANDALONE__ diff --git a/dev/boot/src/New+Delete.cc b/dev/boot/src/New+Delete.cc index 387ceaa0..f7ad2898 100644 --- a/dev/boot/src/New+Delete.cc +++ b/dev/boot/src/New+Delete.cc @@ -1,12 +1,12 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ +#include #include #include -#include #include #ifdef __BOOTZ_STANDALONE__ @@ -14,71 +14,60 @@ /// @brief Allocates a new object. /// @param sz the size. /// @return -void* operator new(size_t sz) -{ - void* buf = nullptr; +void* operator new(size_t sz) { + void* buf = nullptr; - while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) != kEfiOk) - ; + while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) != kEfiOk); - return buf; + return buf; } /// @brief Allocates a new object. /// @param sz the size. /// @return -void* operator new[](size_t sz) -{ - void* buf = nullptr; - BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); +void* operator new[](size_t sz) { + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); - return buf; + return buf; } /// @brief Deletes the object. /// @param buf the object. -void operator delete(void* buf) -{ - if (!buf) - return; +void operator delete(void* buf) { + if (!buf) return; - BS->FreePool(buf); + BS->FreePool(buf); } /// @brief Deletes the object. /// @param buf the object. -void operator delete[](void* buf) -{ - if (!buf) - return; +void operator delete[](void* buf) { + if (!buf) return; - BS->FreePool(buf); + BS->FreePool(buf); } /// @brief Deletes the object (array specific). /// @param buf the object. /// @param size it's size. -void operator delete(void* buf, size_t size) -{ - if (!buf) - return; +void operator delete(void* buf, size_t size) { + if (!buf) return; - NE_UNUSED(size); + NE_UNUSED(size); - BS->FreePool(buf); + BS->FreePool(buf); } /// @brief Deletes the object (array specific). /// @param buf the object. /// @param size it's size. -void operator delete[](void* buf, size_t size) -{ - if (!buf) - return; +void operator delete[](void* buf, size_t size) { + if (!buf) return; - NE_UNUSED(size); + NE_UNUSED(size); - BS->FreePool(buf); + BS->FreePool(buf); } -#endif // __BOOTZ_STANDALONE__ +#endif // __BOOTZ_STANDALONE__ -- cgit v1.2.3