diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-24 03:02:43 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-11-24 03:02:43 +0100 |
| commit | 83d870e58457a1d335a1d9b9966a6a1887cc297b (patch) | |
| tree | 72888f88c7728c82f3f6df1f4f70591de15eab36 /dev/boot/src | |
| parent | ab37adbacf0f33845804c788b39680cd754752a8 (diff) | |
feat! breaking changes on kernel sources.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev/boot/src')
25 files changed, 0 insertions, 2097 deletions
diff --git a/dev/boot/src/.gitkeep b/dev/boot/src/.gitkeep deleted file mode 100644 index e69de29b..00000000 --- a/dev/boot/src/.gitkeep +++ /dev/null diff --git a/dev/boot/src/BootFileReader.cc b/dev/boot/src/BootFileReader.cc deleted file mode 100644 index a929bb93..00000000 --- a/dev/boot/src/BootFileReader.cc +++ /dev/null @@ -1,177 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - - File: FileReader.cc - Purpose: New Boot FileReader, - Read complete file and store it in a buffer. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> -#include <FirmwareKit/EFI/API.h> -#include <FirmwareKit/Handover.h> -#include <modules/CoreGfx/TextGfx.h> - -/// @file BootFileReader -/// @brief Bootloader File reader. -/// BUGS: 0 - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// -/// -/// @name BootFileReader class -/// @brief Reads the file as a blob. -/// -/// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -/*** - @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. - - EFI_GUID guidEfp = EFI_GUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); - - EfiSimpleFilesystemProtocol* efp = nullptr; - - EfiLoadImageProtocol* img = nullptr; - EFI_GUID guidImg = EFI_GUID(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; - - cg_render_string("BootZ: PLEASE RECOVER YOUR NEKERNEL 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); -} - -/** - @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{}; - - EFI_GUID 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; -} - -/// @brief blob getter. -/// @return the blob. -VoidPtr Boot::BootFileReader::Blob() { - return mBlob; -} - -/// @breif Size getter. -/// @return the size of the file. -UInt64& Boot::BootFileReader::Size() { - return mSizeFile; -} diff --git a/dev/boot/src/BootString.cc b/dev/boot/src/BootString.cc deleted file mode 100644 index 6dadda3f..00000000 --- a/dev/boot/src/BootString.cc +++ /dev/null @@ -1,81 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - - File: BootString.cc - Purpose: BootZ string library - - Revision History: - - - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> - -/// BUGS: 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]; - } - - return index; -} - -Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) { - if (!ptr) return 0; - - Kernel::SizeT cnt = 0; - - while (*ptr != (CharacterTypeUTF16) 0) { - ++ptr; - ++cnt; - } - - return cnt; -} - -Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, - const Kernel::SizeT len) { - if (!src) return 0; - - Kernel::SizeT cnt = 0UL; - - while (*src != 0) { - if (cnt > len) break; - - *src = byte; - ++src; - - ++cnt; - } - - return cnt; -} - -Kernel::SizeT Boot::BSetMem(CharacterTypeASCII* src, const CharacterTypeASCII byte, - const Kernel::SizeT len) { - if (!src) return 0; - - Kernel::SizeT cnt = 0UL; - - while (*src != 0) { - if (cnt > len) break; - - *src = byte; - ++src; - - ++cnt; - } - - return cnt; -} diff --git a/dev/boot/src/BootSupport.cc b/dev/boot/src/BootSupport.cc deleted file mode 100644 index 24e09094..00000000 --- a/dev/boot/src/BootSupport.cc +++ /dev/null @@ -1,128 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Support.h> -#include <FirmwareKit/EFI/API.h> -#include <FirmwareKit/EFI/EFI.h> -#include <FirmwareKit/Handover.h> -#include <KernelKit/MSDOS.h> -#include <KernelKit/PE.h> - -#ifdef __BOOTZ_STANDALONE__ - -/// @brief memset definition in C++. -/// @param dst destination pointer. -/// @param byte value to fill in. -/// @param len length of of src. -EXTERN_C VoidPtr memnset(void* dst, int byte, long long unsigned int len, - long long unsigned int dst_size) { - if (!dst || len > dst_size) { - // For now, we return nullptr or an error status. - return nullptr; - } - unsigned char* p = (unsigned char*) dst; - unsigned char val = (unsigned char) byte; - for (size_t i = 0UL; i < len; ++i) { - p[i] = val; - } - return dst; -} - -/// @brief memcpy definition in C++. -/// @param dst destination pointer. -/// @param src source pointer. -/// @param len length of of src. -EXTERN_C VoidPtr memncpy(void* dst, const void* src, long long unsigned int len, - long long unsigned int dst_size) { - if (!dst || !src || len > dst_size) { - // Similar to memset, this is a critical failure. - return nullptr; - } - unsigned char* d = (unsigned char*) dst; - const unsigned char* s = (const unsigned char*) src; - for (size_t i = 0UL; i < len; ++i) { - d[i] = s[i]; - } - return dst; -} - -/// @brief strlen definition in C++. -EXTERN_C size_t strnlen(const char* whatToCheck, size_t max_len) { - size_t len = 0; - while (len < max_len && whatToCheck[len] != '\0') { - ++len; - } - return len; -} - -/// @brief strcmp definition in C++. -EXTERN_C int strncmp(const char* whatToCheck, const char* whatToCheckRight, size_t max_len) { - size_t i = 0; - while (i < max_len && whatToCheck[i] == whatToCheckRight[i]) { - if (whatToCheck[i] == '\0') return 0; - ++i; - } - if (i == max_len) { - return 0; - } - return (unsigned char) whatToCheck[i] - (unsigned char) whatToCheckRight[i]; -} - -/// @brief something specific to the Microsoft's ABI, When the stack grows too big. -EXTERN_C void ___chkstk_ms(void) {} - -/// @note GCC expects them to be here. - -/// @brief memset definition in C++. -/// @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; -} - -/// @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; -} - -/// @brief strlen definition in C++. -EXTERN_C size_t strlen(const char* whatToCheck) { - SizeT len = 0; - - while (whatToCheck[len] != 0) { - ++len; - } - - return len; -} - -/// @brief strcmp definition in C++. -EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) { - SizeT len = 0; - - while (whatToCheck[len] == whatToCheckRight[len]) { - if (whatToCheck[len] == 0) return 0; - - ++len; - } - - return len; -} - -#endif diff --git a/dev/boot/src/BootTextWriter.cc b/dev/boot/src/BootTextWriter.cc deleted file mode 100644 index 0b53e845..00000000 --- a/dev/boot/src/BootTextWriter.cc +++ /dev/null @@ -1,157 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - - File: BootTextWriter.cc - Purpose: BootZ string library - - Revision History: - - - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> -#include <FirmwareKit/EFI/API.h> - -///////////////////////////////////////////////////////////////////////////////////////////////////////// -/// BUGS: 0 /// -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -/** -@brief puts wrapper over EFI ConOut. -*/ -Boot::BootTextWriter& Boot::BootTextWriter::Write(const CharacterTypeUTF16* str) { - NE_UNUSED(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; -} - -/// @brief UTF-8 equivalent of Write (UTF-16). -/// @param str the input string. -Boot::BootTextWriter& Boot::BootTextWriter::Write(const Char* str) { - NE_UNUSED(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; -} - -Boot::BootTextWriter& Boot::BootTextWriter::Write(const UChar* str) { - NE_UNUSED(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; -} - -/** -@brief putc wrapper over EFI ConOut. -*/ -Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) { - NE_UNUSED(c); - -#ifdef __DEBUG__ - EfiCharType str[2]; - - str[0] = c; - str[1] = 0; - ST->ConOut->OutputString(ST->ConOut, str); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BootTextWriter& Boot::BootTextWriter::Write(const UInt64& x) { - NE_UNUSED(x); - -#ifdef __DEBUG__ - this->_Write(x); - this->Write("h"); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BootTextWriter& Boot::BootTextWriter::_Write(const UInt64& x) { - NE_UNUSED(x); - -#ifdef __DEBUG__ - UInt64 y = (x > 0 ? x : -x) / 16; - UInt64 h = (x > 0 ? x : -x) % 16; - - if (y) this->_Write(y); - - /* fail if the hex number is not base-16 */ - if (h > 16) { - this->WriteCharacter('?'); - return *this; - } - - if (y == ~0UL) y = -y; - - const char kNumberList[] = "0123456789ABCDEF"; - - this->WriteCharacter(kNumberList[h]); -#endif // ifdef __DEBUG__ - - return *this; -} diff --git a/dev/boot/src/BootThread.cc b/dev/boot/src/BootThread.cc deleted file mode 100644 index e3ca9221..00000000 --- a/dev/boot/src/BootThread.cc +++ /dev/null @@ -1,216 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/BootThread.h> -#include <BootKit/Support.h> -#include <FirmwareKit/EFI/API.h> - -#include <CFKit/Utils.h> -#include <KernelKit/MSDOS.h> -#include <KernelKit/PE.h> -#include <KernelKit/PEF.h> -#include <modules/CoreGfx/TextGfx.h> - -#define kBootThreadSz mib_cast(16) - -/// @brief External boot services symbol. -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); - -BootThread::BootThread(VoidPtr blob) : fStartAddress(nullptr), fBlob(blob) { - // detect the format. - const Char* blob_bytes = reinterpret_cast<char*>(fBlob); - - BootTextWriter writer; - - 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 (!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; - } -#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; - - writer.Write("BootZ: Image-Base: ").Write(loadStartAddress).Write("\r"); - - fStack = new UInt8[kBootThreadSz]; - - 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"); - - /// @note .text region shall be marked as executable on ARM. - -#ifdef __NE_ARM64__ - -#endif - } 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"); - } -#elif defined(__NE_ARM64__) - 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) { - if (!fStartAddress) { - return kEfiFail; - } - - if (!handover) { - return kEfiFail; - } - - fHandover = handover; - - 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[kBootThreadSz - 1]).Write("\r"); - writer.Write("BootZ: Stack size: ").Write(kBootThreadSz).Write("\r"); - - fHandover->f_StackTop = &fStack[kBootThreadSz - 1]; - fHandover->f_StackSz = kBootThreadSz; - - auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[kBootThreadSz - 1]); - - // we don't need the stack anymore. - - delete[] fStack; - fStack = nullptr; - - return ret; - } else { - writer.Write("BootZ: Using the bootloader's stack.\r"); - - return reinterpret_cast<HEL::HandoverProc>(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/64X000/.gitkeep b/dev/boot/src/HEL/64X000/.gitkeep deleted file mode 100644 index e69de29b..00000000 --- a/dev/boot/src/HEL/64X000/.gitkeep +++ /dev/null diff --git a/dev/boot/src/HEL/64X000/BootCB.S b/dev/boot/src/HEL/64X000/BootCB.S deleted file mode 100644 index 223a697e..00000000 --- a/dev/boot/src/HEL/64X000/BootCB.S +++ /dev/null @@ -1,35 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -.section .boot_hdr -.align 4 - -/* BootZ boot header begin for a 64x000 Kernel. */ - -boot_hdr_mag: - .ascii "CB" -boot_hdr_name: - // it has to match ten bytes. - .asciz "bootz\0\0\0" -boot_hdr_ver: - .word 0x104 -boot_hdr_proc: - .long bootloader_start - -/* BootZ boot header end */ - -.extern bootloader_main -.extern bootloader_stack - -.globl bootloader_start -bootloader_start: - psh 4 /* real address of .Laddr */ - ldi 0,(bootloader_stack-bootloader_start)(4) /* stack address location */ - mv 19,0 /* use user defined stack */ - jrl - - bl bootloader_main - blr diff --git a/dev/boot/src/HEL/AMD64/BootAPI.S b/dev/boot/src/HEL/AMD64/BootAPI.S deleted file mode 100644 index 33c1f5d3..00000000 --- a/dev/boot/src/HEL/AMD64/BootAPI.S +++ /dev/null @@ -1,122 +0,0 @@ -.global rt_jump_to_address -.global rt_reset_hardware - -.data - -.global kApicBaseAddress - -kApicBaseAddress: - .long 0 - -.text - -.intel_syntax noprefix - -.global hal_load_idt - -hal_load_idt: - ret - -.global sched_jump_to_task - -sched_jump_to_task: - ret - -/** - @brief this function setups a stack and then jumps to - a function */ -rt_jump_to_address: - mov rbx, rcx - mov rcx, rdx - push rbx - push rdx - mov rsp, r8 - push rax - jmp rbx - - pop rdx - pop rbx - pop rax - - ret - -rt_reset_hardware: - /* dont raise any interrupts. (except ofc NMIs.) */ - cli - /* remap PIC */ -wait_gate1: - in al,0x64 - and al,2 - jnz wait_gate1 - mov al,0x0D1 - out 0x64,al -wait_gate2: - in al,0x64 - and al,2 - jnz wait_gate2 - mov al,0x0FE - out 0x60,al - - /* trigger triple fault, by writing to cr4 */ - - mov rax, 0 - lidt [rax] - -reset_wait: - jmp reset_wait - -.global boot_write_cr3 -.global boot_read_cr3 - -boot_read_cr3: - mov rax, cr3 - ret - -boot_write_cr3: - mov cr3, rcx - ret - -.section .text - -.extern rt_wait_400ns - -.global rt_out8 -.global rt_out16 -.global rt_out32 - -.global rt_in8 -.global rt_in16 -.global rt_in32 - -rt_out8: - mov al, dl - mov dx, cx - out dx, al - ret - -rt_out16: - mov ax, dx - mov dx, cx - out dx, ax - ret - -rt_out32: - mov eax, edx - mov edx, ecx - out dx, eax - ret - -rt_in8: - mov dx, cx - in al, dx - ret - -rt_in16: - mov edx, ecx - in ax, dx - ret - -rt_in32: - mov rdx, rcx - in eax, dx - ret
\ No newline at end of file diff --git a/dev/boot/src/HEL/AMD64/BootATA.cc b/dev/boot/src/HEL/AMD64/BootATA.cc deleted file mode 100644 index 02051471..00000000 --- a/dev/boot/src/HEL/AMD64/BootATA.cc +++ /dev/null @@ -1,269 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss/Symphony Corp, licensed under the Apache 2.0 license. - -======================================== */ - -/** - * @file BootATA.cc - * @author Amlal El Mahrouss (amlal@nekernel.org) - * @brief ATA driver. - * @version 0.1 - * @date 2024-02-02 - * - * @copyright Copyright (c) Amlal El Mahrouss - * - */ - -#include <BootKit/BootKit.h> -#include <BootKit/HW/ATA.h> -#include <FirmwareKit/EFI.h> - -#define kATADataLen (256) - -/// bugs: 0 - -using namespace Boot; - -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); - -ATAWaitForIO_Retry: - auto status_rdy = rt_in8(IO + ATA_REG_STATUS); - - if ((status_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry; - -ATAWaitForIO_Retry2: - status_rdy = rt_in8(IO + ATA_REG_STATUS); - - if (status_rdy & ATA_SR_ERR) return false; - - if (!(status_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2; - - 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); -} - -Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { - NE_UNUSED(Drive); - - if (boot_ata_detected()) return true; - - BootTextWriter writer; - - UInt16 IO = Bus; - - boot_ata_select(IO); - - // Bus init, NEIN bit. - rt_out8(IO + ATA_REG_NEIN, 1); - - // identify until it's good. -ATAInit_Retry: - auto status_rdy = rt_in8(IO + ATA_REG_STATUS); - - if (status_rdy & ATA_SR_ERR) { - writer.Write(L"VMBoot: ATA: Not an IDE based drive.\r"); - - return false; - } - - if ((status_rdy & ATA_SR_BSY)) goto ATAInit_Retry; - - boot_ata_select(IO); - - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - - /// fetch serial info - /// model, speed, number of sectors... - - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)) - ; - - for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { - kATAData[indexData] = rt_in16(IO + ATA_REG_DATA); - } - - OutBus = (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; - - OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; - - return true; -} - -Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz, - SizeT Size) { - Lba /= SectorSz; - - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - - boot_ata_wait_io(IO); - boot_ata_select(IO); - - 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_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); - - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)) - ; - - for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { - boot_ata_wait_io(IO); - - auto in = rt_in16(IO + ATA_REG_DATA); - - Buf[IndexOff] = in & 0xFF; - Buf[IndexOff + 1] = (in >> 8) & 0xFF; - boot_ata_wait_io(IO); - } -} - -Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz, - SizeT Size) { - Lba /= SectorSz; - - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - - boot_ata_wait_io(IO); - boot_ata_select(IO); - - 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_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); - - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)) - ; - - for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { - boot_ata_wait_io(IO); - - UInt8 low = (UInt8) Buf[IndexOff]; - UInt8 high = (IndexOff + 1 < Size) ? (UInt8) Buf[IndexOff + 1] : 0; - UInt16 packed = (high << 8) | low; - - rt_out16(IO + ATA_REG_DATA, packed); - - boot_ata_wait_io(IO); - } - - boot_ata_wait_io(IO); -} - -/// @check is ATA detected? -Boolean boot_ata_detected(Void) { - return kATADetected; -} - -/*** - * - * - * @brief ATA Device class. - * - * - */ - -/** - * @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; - } -} -/** - * @brief Is ATA detected? - */ -BootDeviceATA::operator bool() { - return boot_ata_detected(); -} - -/** - @brief Read Buf from disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceATA& BootDeviceATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) { - if (!boot_ata_detected()) { - Leak().mErr = true; - return *this; - } - - this->Leak().mErr = false; - - if (!Buf || SectorSz < 1) return *this; - - boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, - this->Leak().mSize); - - return *this; -} - -/** - @brief Write Buf into disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceATA& BootDeviceATA::Write(CharacterTypeASCII* 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; -} - -/*** - @brief Getter, gets the number of sectors inside the drive. -*/ -SizeT BootDeviceATA::GetSectorsCount() noexcept { - return (kATAData[61] << 16) | kATAData[60]; -} - -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 deleted file mode 100644 index 331ded5f..00000000 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ /dev/null @@ -1,257 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/BootThread.h> -#include <FirmwareKit/EFI.h> -#include <FirmwareKit/EFI/API.h> -#include <FirmwareKit/Handover.h> -#include <KernelKit/MSDOS.h> -#include <KernelKit/PE.h> -#include <KernelKit/PEF.h> -#include <NeKit/Macros.h> -#include <NeKit/Ref.h> -#include <modules/CoreGfx/CoreGfx.h> -#include <modules/CoreGfx/TextGfx.h> - -/** Graphics related. */ - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EFI_GUID kGopGuid; - -/** Related to jumping to the reset vector. */ - -EXTERN_C Void rt_reset_hardware(); - -EXTERN_C Kernel::VoidPtr boot_read_cr3(); // @brief Page directory inside cr3 register. - -/** - @brief Finds and stores the GOP object. -*/ -STATIC Bool boot_init_fb() noexcept { - kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; - - kGopStride = 4; - - return Yes; -} - -EFI_GUID kEfiGlobalNamespaceVarGUID = { - 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. - - 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); - - HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); - - 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(); - } - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { - Char* vendor_table = - reinterpret_cast<Char*>(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. - // ------------------------------------------ // - - 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. // - // ------------------------------------------- // - - EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; - - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast<VoidPtr*>(&mp)); - - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast<VoidPtr>(mp); - - kHandoverHeader = handover_hdr; - - FB::cg_clear_video(); - - 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; - } - - // Fill handover header now. - - handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ - - kHandoverHeader->f_BitMapStart = nullptr; /* Start of bitmap. */ - kHandoverHeader->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ - - UInt16 trials = 5; - - while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize, - &kHandoverHeader->f_BitMapStart) != kEfiOk) { - --trials; - - if (!trials) { - writer.Write("BootZ: Unable to allocate sufficient memory, trying again with 2GB...\r"); - - trials = 3; - - kHandoverHeader->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ - - while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize, - &kHandoverHeader->f_BitMapStart) != kEfiOk) { - --trials; - - if (!trials) { - writer.Write("BootZ: Unable to allocate sufficient memory, aborting...\r"); - Boot::Stop(); - } - } - } - } - - handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableBS] = (VoidPtr) BS; - handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST] = (VoidPtr) ST; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); - reader_syschk.ReadAll(0); - - Boot::BootThread* syschk_thread = nullptr; - - if (reader_syschk.Blob()) { - syschk_thread = new Boot::BootThread(reader_syschk.Blob()); - syschk_thread->SetName("SysChk"); - - syschk_thread->Start(handover_hdr, NO); - } - - 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_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; - - handover_hdr->f_HardwareTables.f_ImageKey = map_key; - handover_hdr->f_HardwareTables.f_ImageHandle = image_handle; - - // Provide fimware vendor name. - - 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'. - - WideChar kernel_path[256U] = L"ne_kernel"; - UInt32 kernel_path_sz = StrLen("ne_kernel"); - - UInt32 sz_ver = sizeof(UInt64); - UInt64 ver = KERNEL_VERSION_BCD; - - ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, - &ver); - - 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"); - - if (ST->RuntimeServices->GetVariable(L"/props/kernel_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/kernel_path", kEfiGlobalNamespaceVarGUID, &attr, - &kernel_path_sz, kernel_path); - } - } else { - writer.Write("BootZ: Version: ").Write(ver).Write("\r"); - } - - // boot to kernel, if not bootnet this. - - Boot::BootFileReader reader_kernel(kernel_path, image_handle); - reader_kernel.ReadAll(0); - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_kernel.Blob()) { - handover_hdr->f_PageStart = boot_read_cr3(); - - auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - - kernel_thread.SetName("NeKernel"); - - handover_hdr->f_KernelImage = reader_kernel.Blob(); - handover_hdr->f_KernelSz = reader_kernel.Size(); - - kernel_thread.Start(handover_hdr, YES); - } - - Boot::BootFileReader reader_netboot(L"net.efi", image_handle); - reader_netboot.ReadAll(0); - - if (!reader_netboot.Blob()) return kEfiFail; - - auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); - netboot_thread.SetName("BootNet"); - - 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 deleted file mode 100644 index 7d88b883..00000000 --- a/dev/boot/src/HEL/AMD64/BootPlatform.cc +++ /dev/null @@ -1,36 +0,0 @@ - -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> - -#ifdef __BOOTZ_STANDALONE__ - -using namespace Boot; - -EXTERN_C void rt_halt() { - asm volatile("hlt"); -} - -EXTERN_C void rt_cli() { - asm volatile("cli"); -} - -EXTERN_C void rt_sti() { - asm volatile("sti"); -} - -EXTERN_C void rt_cld() { - asm volatile("cld"); -} - -EXTERN_C void rt_std() { - asm volatile("std"); -} - -#endif // __BOOTZ_STANDALONE__ diff --git a/dev/boot/src/HEL/AMD64/BootSATA.cc b/dev/boot/src/HEL/AMD64/BootSATA.cc deleted file mode 100644 index 1364c58b..00000000 --- a/dev/boot/src/HEL/AMD64/BootSATA.cc +++ /dev/null @@ -1,76 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -/** - * @file BootAHCI.cc - * @author Amlal El Mahrouss (amlal@nekernel.org) - * @brief SATA support for BootZ. - * @version 0.1 - * @date 2024-02-02 - * - * @copyright Copyright (c) Amlal El Mahrouss - * - */ - -#include <BootKit/HW/SATA.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> - -#include <BootKit/BootKit.h> -#include <FirmwareKit/EFI.h> - -#if defined(__AHCI__) && defined(__SYSCHK__) - -using namespace Boot; - -/*** - * - * - * @brief SATA Device class. - * - * - */ - -/** - * @brief ATA Device constructor. - * @param void none. - */ -BootDeviceSATA::BootDeviceSATA() noexcept { - UInt16 pi = 0u; - drv_std_init(pi); -} - -/** - @brief Read Buf from disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceSATA& BootDeviceSATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) { - drv_std_read(mTrait.mBase / SectorSz, Buf, SectorSz, mTrait.mSize); - - return *this; -} - -/** - @brief Write Buf into disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceSATA& BootDeviceSATA::Write(CharacterTypeASCII* Buf, SizeT SectorSz) { - drv_std_write(mTrait.mBase / SectorSz, Buf, SectorSz, mTrait.mSize); - - return *this; -} - -/** - * @brief ATA trait getter. - * @return BootDeviceSATA::ATATrait& the drive config. - */ -BootDeviceSATA::SATATrait& BootDeviceSATA::Leak() { - return mTrait; -} - -#endif
\ No newline at end of file diff --git a/dev/boot/src/HEL/ARM64/.gitkeep b/dev/boot/src/HEL/ARM64/.gitkeep deleted file mode 100644 index e69de29b..00000000 --- a/dev/boot/src/HEL/ARM64/.gitkeep +++ /dev/null diff --git a/dev/boot/src/HEL/ARM64/BootAPI.S b/dev/boot/src/HEL/ARM64/BootAPI.S deleted file mode 100644 index 55183abf..00000000 --- a/dev/boot/src/HEL/ARM64/BootAPI.S +++ /dev/null @@ -1,12 +0,0 @@ -.global rt_jump_to_address - -.text - -/** - @brief this function setups a stack and then jumps to - a function */ -rt_jump_to_address: - mov sp, x1 - br x0 - ret - diff --git a/dev/boot/src/HEL/ARM64/BootEFI.cc b/dev/boot/src/HEL/ARM64/BootEFI.cc deleted file mode 100644 index ac5eb030..00000000 --- a/dev/boot/src/HEL/ARM64/BootEFI.cc +++ /dev/null @@ -1,196 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/BootThread.h> -#include <FirmwareKit/EFI.h> -#include <FirmwareKit/EFI/API.h> -#include <FirmwareKit/Handover.h> -#include <KernelKit/MSDOS.h> -#include <KernelKit/PE.h> -#include <KernelKit/PEF.h> -#include <NeKit/Macros.h> -#include <NeKit/Ref.h> -#include <modules/CoreGfx/CoreGfx.h> -#include <modules/CoreGfx/TextGfx.h> - -#ifndef kExpectedWidth -#define kExpectedWidth (800) -#endif - -#ifndef kExpectedHeight -#define kExpectedHeight (600) -#endif - -/** Graphics related. */ - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EFI_GUID kGopGuid; - -EXTERN_C Void rt_reset_hardware(); - -EXTERN EfiBootServices* BS; - -/** - @brief Finds and stores the GOP object. -*/ -STATIC Bool boot_init_fb() noexcept { - kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; - - kGopStride = 4; - - for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) { - EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; - UInt32 sz = 0U; - - kGop->QueryMode(kGop, i, &sz, &infoPtr); - - if (infoPtr->HorizontalResolution == kExpectedWidth && - infoPtr->VerticalResolution == kExpectedHeight) { - kGop->SetMode(kGop, i); - return Yes; - } - } - - return No; -} - -EXTERN EfiBootServices* BS; - -/// @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. - - kHandoverHeader = new HEL::BootInfoHeader(); - -#ifdef ZBA_USE_FB - if (!boot_init_fb()) return kEfiFail; ///! Init the GOP. - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { - Char* vendor_table = - reinterpret_cast<Char*>(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] == ' ') { - kHandoverHeader->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; - break; - } - } - - // ------------------------------------------ // - // draw background color. - // ------------------------------------------ // - - kHandoverHeader->f_GOP.f_The = kGop->Mode->FrameBufferBase; - kHandoverHeader->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - kHandoverHeader->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - kHandoverHeader->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - kHandoverHeader->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - kHandoverHeader->f_GOP.f_Size = kGop->Mode->FrameBufferSize; -#endif // ZBA_USE_FB - - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // - - EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; - - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast<VoidPtr*>(&mp)); - kHandoverHeader->f_HardwareTables.f_MpPtr = reinterpret_cast<VoidPtr>(mp); - - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; - - if (mp) { - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - } else { - kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = NO; - } - - //-------------------------------------------------------------// - // Allocate heap. - //-------------------------------------------------------------// - - Boot::BootTextWriter writer; - - kHandoverHeader->f_BitMapStart = nullptr; /* Start of bitmap. */ - kHandoverHeader->f_BitMapSize = kHandoverBitMapSz; /* Size of bitmap in bytes. */ - - UInt16 trials = 5; - - while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize, - &kHandoverHeader->f_BitMapStart) != kEfiOk) { - --trials; - - if (!trials) { - writer.Write("BootZ: Unable to allocate sufficient memory, trying again with 2GB...\r"); - - trials = 3; - - kHandoverHeader->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ - - while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize, - &kHandoverHeader->f_BitMapStart) != kEfiOk) { - --trials; - - if (!trials) { - writer.Write("BootZ: Unable to allocate sufficient memory, aborting...\r"); - Boot::Stop(); - } - } - } - } - - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // - - kHandoverHeader->f_FirmwareCustomTables[0] = nullptr; - kHandoverHeader->f_FirmwareCustomTables[1] = nullptr; - - kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - kHandoverHeader->f_Magic = kHandoverMagic; - kHandoverHeader->f_Version = kHandoverVersion; - - // Provide fimware vendor name. - - Boot::BCopyMem(kHandoverHeader->f_FirmwareVendorName, sys_table->FirmwareVendor, - kHandoverHeader->f_FirmwareVendorLen); - - kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - Boot::BootFileReader reader_kernel(L"ne_kernel", image_handle); - - reader_kernel.ReadAll(0); - - // ------------------------------------------ // - // 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("NeKernel"); - - kHandoverHeader->f_KernelImage = reader_kernel.Blob(); - kHandoverHeader->f_KernelSz = reader_kernel.Size(); - - kernel_thread.Start(kHandoverHeader, YES); - } - - CANT_REACH(); -} diff --git a/dev/boot/src/HEL/ARM64/BootNB.S b/dev/boot/src/HEL/ARM64/BootNB.S deleted file mode 100644 index f781ad37..00000000 --- a/dev/boot/src/HEL/ARM64/BootNB.S +++ /dev/null @@ -1,40 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#ifdef __NE_NEBOOT__ - -.section .boot_hdr -.align 4 - -/* BootZ boot header begin */ - -boot_hdr_mag: - .ascii "CB" -boot_hdr_name: - // it has to match ten bytes. - .asciz "bootz\0\0\0" -boot_hdr_ver: - .word 0x101 -boot_hdr_proc: - .long bootloader_start - -/* BootZ boot header end */ - -.extern bootloader_main -.extern bootloader_stack - -.globl bootloader_start -bootloader_start: - adr x0, bootloader_stack - ldr x1, =bootloader_start - sub x0, x0, x1 - ldr x0, [x0] - mov sp, x0 - - bl bootloader_main - ret - -#endif // __NE_NEBOOT__
\ No newline at end of file diff --git a/dev/boot/src/HEL/ARM64/BootPlatform.cc b/dev/boot/src/HEL/ARM64/BootPlatform.cc deleted file mode 100644 index 2ad9b776..00000000 --- a/dev/boot/src/HEL/ARM64/BootPlatform.cc +++ /dev/null @@ -1,28 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> - -#ifdef __BOOTZ_STANDALONE__ - -using namespace Boot; - -EXTERN_C void rt_halt() { - while (Yes) - ; -} - -EXTERN_C void rt_cli() {} - -EXTERN_C void rt_sti() {} - -EXTERN_C void rt_cld() {} - -EXTERN_C void rt_std() {} - -#endif // __BOOTZ_STANDALONE__ diff --git a/dev/boot/src/HEL/POWER/.gitkeep b/dev/boot/src/HEL/POWER/.gitkeep deleted file mode 100644 index e69de29b..00000000 --- a/dev/boot/src/HEL/POWER/.gitkeep +++ /dev/null diff --git a/dev/boot/src/HEL/POWER/BootNB.S b/dev/boot/src/HEL/POWER/BootNB.S deleted file mode 100644 index 45b9c9e1..00000000 --- a/dev/boot/src/HEL/POWER/BootNB.S +++ /dev/null @@ -1,34 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -.section .boot_hdr -.align 4 - -/* BootZ boot header begin */ - -boot_hdr_mag: - .ascii "CB" -boot_hdr_name: - // it has to match ten bytes. - .asciz "bootz\0\0\0" -boot_hdr_ver: - .word 0x101 -boot_hdr_proc: - .long bootloader_start - -/* BootZ boot header end */ - -.extern bootloader_main -.extern bootloader_stack - -.globl bootloader_start -bootloader_start: - mflr 4 /* real address of .Laddr */ - lwz 0,(bootloader_stack-bootloader_start)(4) /* stack address location */ - mr 1,0 /* use user defined stack */ - - bl bootloader_main - blr diff --git a/dev/boot/src/New+Delete.cc b/dev/boot/src/New+Delete.cc deleted file mode 100644 index a66d8464..00000000 --- a/dev/boot/src/New+Delete.cc +++ /dev/null @@ -1,74 +0,0 @@ -/* ======================================== - - Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license. - -======================================== */ - -#include <BootKit/BootKit.h> -#include <BootKit/Platform.h> -#include <BootKit/Protocol.h> -#include <FirmwareKit/EFI/API.h> - -#ifdef __BOOTZ_STANDALONE__ - -/// @brief Allocates a new object. -/// @param sz the size. -/// @return -void* operator new(size_t sz) { - void* buf = nullptr; - - while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) != kEfiOk) - ; - - 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); - - return buf; -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete(void* buf) noexcept { - if (!buf) return; - - BS->FreePool(buf); -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete[](void* buf) noexcept { - if (!buf) return; - - 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; - - NE_UNUSED(size); - - 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; - - NE_UNUSED(size); - - BS->FreePool(buf); -} - -#endif // __BOOTZ_STANDALONE__ diff --git a/dev/boot/src/boot_rsrc.rsrc b/dev/boot/src/boot_rsrc.rsrc deleted file mode 100644 index e875fa24..00000000 --- a/dev/boot/src/boot_rsrc.rsrc +++ /dev/null @@ -1,25 +0,0 @@ -#include "../../kernel/CompilerKit/Version.h" - -1 VERSIONINFO -FILEVERSION 1,0,0,0 -PRODUCTVERSION 1,0,0,0 -BEGIN - BLOCK "StringFileInfo" - BEGIN - BLOCK "080904E4" - BEGIN - VALUE "CompanyName", "Amlal El Mahrouss" - VALUE "FileDescription", "NeKernel OS Loader." - VALUE "FileVersion", BOOTLOADER_VERSION - VALUE "InternalName", "bootz" - VALUE "LegalCopyright", "Copyright (C) 2024-2025, Amlal El Mahrouss licensed under the Apache 2.0 license." - VALUE "OriginalFilename", "ne_bootz" - VALUE "ProductName", "bootz" - VALUE "ProductVersion", BOOTLOADER_VERSION - END - END - BLOCK "VarFileInfo" - BEGIN - VALUE "Translation", 0x809, 1252 - END -END diff --git a/dev/boot/src/docs/KERN_VER.md b/dev/boot/src/docs/KERN_VER.md deleted file mode 100644 index c47c3d5b..00000000 --- a/dev/boot/src/docs/KERN_VER.md +++ /dev/null @@ -1,18 +0,0 @@ -# `/props/kern_ver` โ NVRAM EFI Variable - -The `/props/kern_ver` variable is used to track NeKernel's current version in a BCD format. - -## ๐ Reason - -- It is also used for: - - Bug tracking and system patching. - - Version and compatibility checking. - -## ๐งช Usage - -N/A - -## ยฉ License - - Copyright (C) 2025, - Amlal El Mahrouss โ Licensed under the Apache 2.0 license.
\ No newline at end of file diff --git a/dev/boot/src/docs/MKFS_HEFS.md b/dev/boot/src/docs/MKFS_HEFS.md deleted file mode 100644 index fd2a099e..00000000 --- a/dev/boot/src/docs/MKFS_HEFS.md +++ /dev/null @@ -1,106 +0,0 @@ -# `mkfs.hefs` โ OpenHeFS Filesystem Formatter - -`mkfs.hefs` is a command-line utility used to format a block device or disk image with the **High-throughput Extended File System (OpenHeFS)** used by NeKernel. This tool initializes a OpenHeFS volume by writing a boot node and configuring directory and inode index regions, block ranges, and volume metadata. - ---- - -## ๐ Features - -- Writes a valid `BootNode` to the specified output device or file. -- Sets disk size, sector size, and volume label. -- Supports user-defined ranges for: - - Index Node Directory (IND) - - Inodes (IN) - - Data blocks -- UTF-8 encoded volume label support. -- Fully compatible with NeKernel's VFS subsystem. - ---- - -## ๐งช Usage - - mkfs.hefs -L <label> -s <sector_size> \ - -b <ind_start> -e <ind_end> \ - -bs <block_start> -be <block_end> \ - -is <in_start> -ie <in_end> \ - -S <disk_size> -o <output_device> - ---- - -## ๐งพ Arguments - -| Option | Description | -|---------------|-------------------------------------------------------------------------| -| `-L` | Volume label (UTF-8, internally stored as UTF-16) | -| `-s` | Sector size (e.g., 512) | -| `-b` `-e` | Start and end addresses for the **Index Node Directory (IND)** region | -| `-bs` `-be` | Start and end addresses for the **Block** data region | -| `-is` `-ie` | Start and end addresses for the **Inode** region | -| `-S` | Disk size in **gigabytes** | -| `-o` | Path to the output device or image file | - -> All address-based inputs (`-b`, `-e`, etc.) must be specified in **hexadecimal** format. - ---- - -## ๐งท Notes - -- Default sector size is `512` bytes. -- Default volume name is `"HeFS_VOLUME"`, defined as `kHeFSDefaultVolumeName`. -- The tool writes a `BootNode` at the beginning of the index node range. -- A CRC-safe magic signature is embedded for boot and integrity validation. -- After writing the metadata, the tool flushes and closes the file stream. - ---- - -## ๐ป Example - - mkfs.hefs -L "MyHeFS" -s 512 \ - -b 0x1000 -e 0x8000 \ - -bs 0x8000 -be 0x800000 \ - -is 0x800000 -ie 0xA00000 \ - -S 128 -o hefs.img - -This will create a 128 GiB formatted OpenHeFS image named `hefs.img` with specified region boundaries. - ---- - -## ๐ BootNode Structure - -The `BootNode` stores key filesystem metadata: - - struct BootNode { - char magic[8]; - char16_t volumeName[64]; - uint16_t version; - uint16_t diskKind; - uint16_t encoding; - uint64_t diskSize; - uint32_t sectorSize; - uint64_t startIND, endIND; - uint64_t startIN, endIN; - uint64_t startBlock, endBlock; - uint64_t indCount; - uint16_t diskStatus; - }; - ---- - -## โ ๏ธ Error Handling - -- Prints usage and exits on invalid/missing arguments. -- Exits with error if the output device cannot be opened or written to. -- Checks for zero sector size or disk size to prevent invalid formatting. - ---- - -## ๐ Source Location - -Part of the [OpenHeFS Tooling module](https://github.com/nekernel-org/nekernel) and used during system setup or disk preparation for NeKernel. - ---- - -## ยฉ License - - Copyright (C) 2025, - Amlal El Mahrouss โ Licensed under the Apache 2.0 license.
\ No newline at end of file diff --git a/dev/boot/src/root/ifs.json b/dev/boot/src/root/ifs.json deleted file mode 100644 index 354ab503..00000000 --- a/dev/boot/src/root/ifs.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "type": "IFS", - "sys": [ - "/~drivers/fat32.sys", - "/~drivers/ntfs.sys", - "/~drivers/ext4.sys", - "/~drivers/iso9660.sys", - "/~drivers/nfs4.sys" - ] -}
\ No newline at end of file |
