From c7b637d7b68d9b7e271e70a5a5cc0c4fcf9172de Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 12 Oct 2024 19:20:29 +0200 Subject: IMP: Porting stack to AARCH64. - Bootloader and kernel were made to be ported to mulitple platforms. - Some part of the stack have to be written specifically for the platform though. Signed-off-by: Amlal El Mahrouss --- dev/zba/BootKit/BootKit.hxx | 1 - dev/zba/BootKit/HW/ATA.hxx | 2 +- dev/zba/BootKit/Support.hxx | 18 +++ dev/zba/amd64-efi.make | 8 +- dev/zba/arm64-efi.make | 119 ++++++++++++++++++ dev/zba/src/BootFileReader.cxx | 203 ++++++++++++++++++++++++++++++ dev/zba/src/BootString.cxx | 92 ++++++++++++++ dev/zba/src/BootSupport.cxx | 80 ++++++++++++ dev/zba/src/BootTextWriter.cxx | 169 +++++++++++++++++++++++++ dev/zba/src/HEL/AMD64/BootFileReader.cxx | 203 ------------------------------ dev/zba/src/HEL/AMD64/BootMain.cxx | 50 ++++---- dev/zba/src/HEL/AMD64/BootPlatform.cxx | 3 +- dev/zba/src/HEL/AMD64/BootString.cxx | 92 -------------- dev/zba/src/HEL/AMD64/BootTextWriter.cxx | 169 ------------------------- dev/zba/src/HEL/AMD64/New+Delete.cxx | 62 --------- dev/zba/src/HEL/AMD64/Support.cxx | 82 ------------ dev/zba/src/HEL/ARM64/BootMain.cxx | 20 +++ dev/zba/src/HEL/ARM64/BootPlatform.cxx | 40 ++++++ dev/zba/src/New+Delete.cxx | 60 +++++++++ dev/zba/src/Thread.cxx | 10 -- dev/zka/ArchKit/ArchKit.hxx | 24 ---- dev/zka/HALKit/AMD64/HalBMPMgr.cxx | 202 ----------------------------- dev/zka/HALKit/ARM64/HalSchedulerCore.cxx | 17 ++- dev/zka/HALKit/ARM64/HalTimer.cxx | 2 - dev/zka/amd64-efi.make | 6 +- dev/zka/arm64-efi.make | 2 +- dev/zka/src/BitMapMgr.cxx | 202 +++++++++++++++++++++++++++++ 27 files changed, 1047 insertions(+), 891 deletions(-) create mode 100644 dev/zba/arm64-efi.make create mode 100644 dev/zba/src/BootFileReader.cxx create mode 100644 dev/zba/src/BootString.cxx create mode 100644 dev/zba/src/BootSupport.cxx create mode 100644 dev/zba/src/BootTextWriter.cxx delete mode 100644 dev/zba/src/HEL/AMD64/BootFileReader.cxx delete mode 100644 dev/zba/src/HEL/AMD64/BootString.cxx delete mode 100644 dev/zba/src/HEL/AMD64/BootTextWriter.cxx delete mode 100644 dev/zba/src/HEL/AMD64/New+Delete.cxx delete mode 100644 dev/zba/src/HEL/AMD64/Support.cxx create mode 100644 dev/zba/src/HEL/ARM64/BootMain.cxx create mode 100644 dev/zba/src/HEL/ARM64/BootPlatform.cxx create mode 100644 dev/zba/src/New+Delete.cxx delete mode 100644 dev/zka/HALKit/AMD64/HalBMPMgr.cxx create mode 100644 dev/zka/src/BitMapMgr.cxx (limited to 'dev') diff --git a/dev/zba/BootKit/BootKit.hxx b/dev/zba/BootKit/BootKit.hxx index 12e595e2..4dde8f1e 100644 --- a/dev/zba/BootKit/BootKit.hxx +++ b/dev/zba/BootKit/BootKit.hxx @@ -17,7 +17,6 @@ /// include NeFS header and Support header as well. -#include #include #include diff --git a/dev/zba/BootKit/HW/ATA.hxx b/dev/zba/BootKit/HW/ATA.hxx index 67a8aa97..2d4e6e8b 100644 --- a/dev/zba/BootKit/HW/ATA.hxx +++ b/dev/zba/BootKit/HW/ATA.hxx @@ -23,7 +23,7 @@ public: explicit BootDeviceATA() noexcept; ~BootDeviceATA() = default; - ZKA_COPY_DEFAULT(BootDeviceATA); + ZKA_COPY_DELETE(BootDeviceATA); enum { diff --git a/dev/zba/BootKit/Support.hxx b/dev/zba/BootKit/Support.hxx index 8b9ab105..47270c39 100644 --- a/dev/zba/BootKit/Support.hxx +++ b/dev/zba/BootKit/Support.hxx @@ -14,6 +14,24 @@ #ifdef __NEWOSLDR__ +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C void* memset(void* dst, int byte, long long unsigned int len); + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C void* memcpy(void* dst, const void* src, long long unsigned int len); + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char* whatToCheck); + +/// @brief strcmp definition in C++. +EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight); + #define SetMem(dst, c, sz) memset(dst, c, sz) #define MoveMem(dst, src, sz) memcpy(dst, src, sz) #define CopyMem(dst, src, sz) memcpy(dst, src, sz) diff --git a/dev/zba/amd64-efi.make b/dev/zba/amd64-efi.make index c00a0699..00427303 100644 --- a/dev/zba/amd64-efi.make +++ b/dev/zba/amd64-efi.make @@ -3,10 +3,10 @@ # This is the bootloader makefile. ################################################## -CC_GNU=x86_64-w64-mingw32-g++.exe -LD_GNU=x86_64-w64-mingw32-ld.exe +CC_GNU=x86_64-w64-mingw32-g++ +LD_GNU=x86_64-w64-mingw32-ld -WINDRES=x86_64-w64-mingw32-windres.exe +WINDRES=x86_64-w64-mingw32-windres ADD_FILE=touch COPY=cp @@ -48,7 +48,7 @@ REM_FLAG=-f FLAG_ASM=-f win64 FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mno-red-zone -D__NEWOSKRNL__ -D__NEWOSLDR__ \ - -DEFI_FUNCTION_WRAPPER -I./ -I../Vendor -I../zka -I../ -c -nostdlib -fno-rtti -fno-exceptions \ + -DEFI_FUNCTION_WRAPPER -I./ -I../zka -I../ -c -nostdlib -fno-rtti -fno-exceptions \ -std=c++20 -D__HAVE_ZKA_APIS__ -D__ZKA_AMD64__ -D__ZKA__ BOOT_LOADER=newosldr.exe diff --git a/dev/zba/arm64-efi.make b/dev/zba/arm64-efi.make new file mode 100644 index 00000000..e546989d --- /dev/null +++ b/dev/zba/arm64-efi.make @@ -0,0 +1,119 @@ +################################################## +# (C) ZKA Technologies, all rights reserved. +# This is the bootloader makefile. +################################################## + +CC_GNU = clang++ +LD_GNU = lld-link + +ADD_FILE=touch +COPY=cp +HTTP_GET=wget + +# Select this for Windows. +ifneq ($(findstring CYGWIN_NT-10.0,$(shell uname)), ) +EMU=qemu-system-aarch64w.exe +else +# this for NT distributions +EMU=qemu-system-aarch64 +endif + +ifeq ($(NEWS_MODEL), ) +ZKA_MODEL=-DkMachineModel="\"ZKA SSD\"" +endif + +BIOS=OVMF.fd +IMG=epm-master-1.img +IMG_2=epm-slave.img +IMG_3=epm-master-2.img + +EMU_FLAGS=-net none -m 8G -cpu max -M virt-9.1 -vga std \ + -bios $(BIOS) \ + -drive id=disk,file=$(IMG),format=raw,if=none \ + -drive \ + file=fat:rw:src/Root/,index=1,format=raw \ + -d int -no-shutdown -no-reboot + +LD_FLAGS=-subsystem:efi_application -entry:Main /nodefaultlib + +STANDALONE_MACRO=-D__STANDALONE__ +OBJ=*.o + +REM=rm +REM_FLAG=-f + +FLAG_ASM=-f win64 +FLAG_GNU=-fshort-wchar -c -ffreestanding -MMD -mno-red-zone -D__ZKA_ARM64__ -fno-rtti -fno-exceptions -I./ \ + -target aarch64-unknown-windows \ + -std=c++20 -D__FSKIT_USE_NEFS__ -D__NEWOSKRNL__ -D__NEWOSLDR__ -D__HAVE_ZKA_APIS__ -D__ZKA__ -I../ -I../zka + +BOOT_LOADER=newosldr.exe +KERNEL=minoskrnl.exe +DDK=ddk.dll +SCI=zka-sci-cxx.dll +CRT=zka-crt-cxx.dll +SYS_CHK=syschk.sys +STARTUP=startup.sys + +.PHONY: invalid-recipe +invalid-recipe: + @echo "invalid-recipe: Use make compile- instead." + +.PHONY: all +all: compile-amd64 + mkdir -p src/Root/EFI/BOOT + $(LD_GNU) $(OBJ) $(LD_FLAGS) /out:src/$(BOOT_LOADER) + $(COPY) src/$(BOOT_LOADER) src/Root/EFI/BOOT/BOOTAA64.EFI + $(COPY) src/$(BOOT_LOADER) src/Root/EFI/BOOT/NEWOSLDR.EFI + $(COPY) ../zka/$(KERNEL) src/Root/$(KERNEL) + $(COPY) ../sci/$(SCI) src/Root/$(SCI) + $(COPY) ../ddk/$(DDK) src/Root/$(DDK) + $(COPY) ./modules/SysChk/$(SYS_CHK) src/Root/$(SYS_CHK) + $(COPY) ./modules/SysChk/$(SYS_CHK) src/Root/zka/$(STARTUP) + $(COPY) ../crt/$(CRT) src/Root/$(CRT) + $(COPY) src/$(BOOT_LOADER) src/Root/$(BOOT_LOADER) + +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + +.PHONY: compile-amd64 +compile-amd64: + $(RESCMD) + $(CC_GNU) $(ZKA_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ + $(wildcard src/HEL/ARM64/*.cxx) \ + $(wildcard src/HEL/ARM64/*.S) \ + $(wildcard src/*.cxx) + +.PHONY: run-efi-amd64 +run-efi-amd64: + $(EMU) $(EMU_FLAGS) + +# img_2 is the rescue disk. img is the bootable disk, as provided by the Zeta. +.PHONY: epm-img +epm-img: + qemu-img create -f raw $(IMG) 10G + qemu-img create -f raw $(IMG_2) 4G + qemu-img create -f raw $(IMG_3) 4G + +.PHONY: download-edk +download-edk: + $(HTTP_GET) https://retrage.github.io/edk2-nightly/bin/DEBUGAARCH64_QEMU_EFI.fd -O OVMF.fd + +BINS=*.bin +EXECUTABLES=newosldr.exe minoskrnl.exe OVMF.fd + +TARGETS=$(REM_FLAG) $(OBJ) $(BIN) $(IMG) $(IMG_2) $(EXECUTABLES) + +.PHONY: clean +clean: + $(REM) $(TARGETS) + +.PHONY: help +help: + @echo "=== HELP ===" + @echo "epm-img: Format a disk using the Explicit Partition Map." + @echo "gpt-img: Format a disk using the Explicit Partition Map." + @echo "clean: clean bootloader." + @echo "bootloader-amd64: Build bootloader. (PC AMD64)" + @echo "run-efi-amd64: Run bootloader. (PC AMD64)" diff --git a/dev/zba/src/BootFileReader.cxx b/dev/zba/src/BootFileReader.cxx new file mode 100644 index 00000000..e81ba3fb --- /dev/null +++ b/dev/zba/src/BootFileReader.cxx @@ -0,0 +1,203 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + + File: FileReader.cxx + Purpose: New Boot FileReader, + Read complete file and store it in a buffer. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include + +/// @file BootFileReader +/// @brief Bootloader File reader. +/// BUGS: 0 + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// +/// +/// @name BFileReader class +/// @brief Reads the file as a blob. +/// +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/*** + @brief File Reader constructor. +*/ +Boot::BFileReader::BFileReader(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"NEWOSLDR: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + } + + if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) + { + mWriter.Write(L"NEWOSLDR: 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"NEWOSLDR: Fetch-Protocol: No-Such-Volume").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + EfiFileProtocol* KernelFile = nullptr; + + if (mRootFs->Open(mRootFs, &KernelFile, mPath, kEFIFileRead, kEFIReadOnly) != + kEfiOk) + { + mWriter.Write(L"NEWOSLDR: Fetch-Protocol: No-Such-Path: ") + .Write(mPath) + .Write(L"\r"); + this->mErrorCode = kNotSupported; + + CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR MINKRNL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + + mRootFs->Close(mRootFs); + + return; + } + + mSizeFile = 0; + mFile = KernelFile; + mErrorCode = kOperationOkay; +} + +Boot::BFileReader::~BFileReader() +{ + 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::BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr outAddress) +{ + if (mBlob == nullptr) + { + EfiFileInfo newPtrInfo; + UInt32 szInfo = 0; + + EfiGUID cFileInfoGUID = EFI_FILE_INFO_GUID; + + if (mFile->GetInfo(mFile, &cFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) + { + if (newPtrInfo.FileSize < readUntil) + readUntil = newPtrInfo.FileSize; + else if (readUntil < 1) + readUntil = newPtrInfo.FileSize; + + mWriter.Write(L"NEWOSLDR: Physical size: ").Write(readUntil).Write("\r"); + } + + if (!outAddress) + { + if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*)&mBlob) != + kEfiOk) + { + mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); + EFI::ThrowError(L"OutOfMemory", L"Out of memory."); + } + } + else + { + mBlob = (VoidPtr)outAddress; + } + } + + mErrorCode = kNotSupported; + + 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::BFileReader::Error() +{ + return mErrorCode; +} + +/// @brief blob getter. +/// @return the blob. +VoidPtr Boot::BFileReader::Blob() +{ + return mBlob; +} + +/// @breif Size getter. +/// @return the size of the file. +UInt64& Boot::BFileReader::Size() +{ + return mSizeFile; +} diff --git a/dev/zba/src/BootString.cxx b/dev/zba/src/BootString.cxx new file mode 100644 index 00000000..1c109b80 --- /dev/null +++ b/dev/zba/src/BootString.cxx @@ -0,0 +1,92 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include +#include +#include + +/// 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(CharacterTypeUTF8* src, const CharacterTypeUTF8 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/zba/src/BootSupport.cxx b/dev/zba/src/BootSupport.cxx new file mode 100644 index 00000000..bab53c2c --- /dev/null +++ b/dev/zba/src/BootSupport.cxx @@ -0,0 +1,80 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __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 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) +{ + if (!whatToCheck || *whatToCheck == 0) + return 0; + + SizeT len = 0; + + while (whatToCheck[len] == whatToCheckRight[len]) + { + if (whatToCheck[len] == 0) + return 0; + + ++len; + } + + return len; +} + +/// @brief something specific to the Microsoft's ABI, When the stack grows too big. +EXTERN_C void ___chkstk_ms(void) {} + +#endif diff --git a/dev/zba/src/BootTextWriter.cxx b/dev/zba/src/BootTextWriter.cxx new file mode 100644 index 00000000..b0679715 --- /dev/null +++ b/dev/zba/src/BootTextWriter.cxx @@ -0,0 +1,169 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + + File: String.cxx + Purpose: NewBoot string library + + Revision History: + + + +------------------------------------------- */ + +#include +#include +#include +#include + +/// BUGS: 0 + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +/** +@brief puts wrapper over EFI ConOut. +*/ +Boot::BTextWriter& Boot::BTextWriter::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; +} + +/// @brief UTF-8 equivalent of Write (UTF-16). +/// @param str the input string. +Boot::BTextWriter& Boot::BTextWriter::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; +} + +Boot::BTextWriter& Boot::BTextWriter::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; +} + +/** +@brief putc wrapper over EFI ConOut. +*/ +Boot::BTextWriter& Boot::BTextWriter::WriteCharacter(CharacterTypeUTF16 c) +{ +#ifdef __DEBUG__ + EfiCharType str[2]; + + str[0] = c; + str[1] = 0; + ST->ConOut->OutputString(ST->ConOut, str); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BTextWriter& Boot::BTextWriter::Write(const Long& x) +{ +#ifdef __DEBUG__ + this->_Write(x); + this->Write("h"); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BTextWriter& Boot::BTextWriter::_Write(const Long& 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 < 0) + y = -y; + + const char cNumbers[] = "0123456789ABCDEF"; + + this->WriteCharacter(cNumbers[h]); +#endif // ifdef __DEBUG__ + + return *this; +} diff --git a/dev/zba/src/HEL/AMD64/BootFileReader.cxx b/dev/zba/src/HEL/AMD64/BootFileReader.cxx deleted file mode 100644 index e81ba3fb..00000000 --- a/dev/zba/src/HEL/AMD64/BootFileReader.cxx +++ /dev/null @@ -1,203 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - - File: FileReader.cxx - Purpose: New Boot FileReader, - Read complete file and store it in a buffer. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include - -/// @file BootFileReader -/// @brief Bootloader File reader. -/// BUGS: 0 - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// -/// -/// @name BFileReader class -/// @brief Reads the file as a blob. -/// -/// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -/*** - @brief File Reader constructor. -*/ -Boot::BFileReader::BFileReader(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"NEWOSLDR: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - } - - if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) - { - mWriter.Write(L"NEWOSLDR: 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"NEWOSLDR: Fetch-Protocol: No-Such-Volume").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - EfiFileProtocol* KernelFile = nullptr; - - if (mRootFs->Open(mRootFs, &KernelFile, mPath, kEFIFileRead, kEFIReadOnly) != - kEfiOk) - { - mWriter.Write(L"NEWOSLDR: Fetch-Protocol: No-Such-Path: ") - .Write(mPath) - .Write(L"\r"); - this->mErrorCode = kNotSupported; - - CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR MINKRNL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - - mRootFs->Close(mRootFs); - - return; - } - - mSizeFile = 0; - mFile = KernelFile; - mErrorCode = kOperationOkay; -} - -Boot::BFileReader::~BFileReader() -{ - 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::BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr outAddress) -{ - if (mBlob == nullptr) - { - EfiFileInfo newPtrInfo; - UInt32 szInfo = 0; - - EfiGUID cFileInfoGUID = EFI_FILE_INFO_GUID; - - if (mFile->GetInfo(mFile, &cFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) - { - if (newPtrInfo.FileSize < readUntil) - readUntil = newPtrInfo.FileSize; - else if (readUntil < 1) - readUntil = newPtrInfo.FileSize; - - mWriter.Write(L"NEWOSLDR: Physical size: ").Write(readUntil).Write("\r"); - } - - if (!outAddress) - { - if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*)&mBlob) != - kEfiOk) - { - mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); - EFI::ThrowError(L"OutOfMemory", L"Out of memory."); - } - } - else - { - mBlob = (VoidPtr)outAddress; - } - } - - mErrorCode = kNotSupported; - - 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::BFileReader::Error() -{ - return mErrorCode; -} - -/// @brief blob getter. -/// @return the blob. -VoidPtr Boot::BFileReader::Blob() -{ - return mBlob; -} - -/// @breif Size getter. -/// @return the size of the file. -UInt64& Boot::BFileReader::Size() -{ - return mSizeFile; -} diff --git a/dev/zba/src/HEL/AMD64/BootMain.cxx b/dev/zba/src/HEL/AMD64/BootMain.cxx index 8f8c45d1..3c5e80a3 100644 --- a/dev/zba/src/HEL/AMD64/BootMain.cxx +++ b/dev/zba/src/HEL/AMD64/BootMain.cxx @@ -5,11 +5,9 @@ ------------------------------------------- */ #include -#include #include #include #include -#include #include #include #include @@ -17,7 +15,6 @@ #include #include #include -#include // make the compiler shut up. #ifndef kMachineModel @@ -71,14 +68,6 @@ STATIC Void InitVideoFB() noexcept } } -/// @brief check the BootDevice if suitable. -STATIC Bool CheckBootDevice(BootDeviceATA& ataDev) -{ - if (ataDev.Leak().mErr) - return false; - return true; -} - EXTERN_C VoidPtr boot_read_cr3(); EXTERN_C Void boot_write_cr3(VoidPtr new_cr3); @@ -88,21 +77,23 @@ EXTERN EfiBootServices* BS; /// @param ImageHandle Handle of this image. /// @param SystemTable The system table of it. /// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, +EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, EfiSystemTable* SystemTable) { InitEFI(SystemTable); ///! Init the EFI library. - InitVideoFB(); ///! Init the GOP. - - UInt32 MapKey = 0; - UInt32 SizePtr = sizeof(EfiMemoryDescriptor); - EfiMemoryDescriptor* Descriptor = nullptr; - UInt32 SzDesc = sizeof(EfiMemoryDescriptor); - UInt32 RevDesc = 0; HEL::HANDOVER_INFO_HEADER* handover_hdr = new HEL::HANDOVER_INFO_HEADER(); + UInt32 map_key = 0; + UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + EfiMemoryDescriptor* struct_ptr = nullptr; + UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 rev_desc = 0; + +#ifdef __ZKA_USE_FB__ + InitVideoFB(); ///! Init the GOP. + for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; ++indexVT) { @@ -130,6 +121,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, 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 // __ZKA_USE_FB__ // ------------------------------------------- // // Grab MP services, extended to runtime. // @@ -144,17 +136,21 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, kHandoverHeader = handover_hdr; +#ifdef __ZKA_USE_FB__ CGInit(); CGDrawInRegion(CGColor(0xFF, 0x3A, 0x3A), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0); CGFini(); +#endif // __ZKA_USE_FB__ UInt32 cnt_enabled = 0; UInt32 cnt_disabled = 0; mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); +#ifdef __ZKA_USE_FB__ CGDrawString("NEWOSLDR (C) ZKA TECHNOLOGIES.", 10, 10, RGB(0xFF, 0xFF, 0xFF)); CGDrawString((cnt_enabled > 1) ? "MULTIPLE PROCESSORS DETECTED." : "SINGLE PROCESSOR DETECTED.", 20, 10, RGB(0xFF, 0xFF, 0xFF)); +#endif // __ZKA_USE_FB__ handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; // Fill handover header now. @@ -180,10 +176,10 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, rt_reset_hardware(); } - BS->GetMemoryMap(&SizePtr, Descriptor, &MapKey, &SzDesc, &RevDesc); + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - Descriptor = new EfiMemoryDescriptor[SzDesc]; - BS->GetMemoryMap(&SizePtr, Descriptor, &MapKey, &SzDesc, &RevDesc); + struct_ptr = new EfiMemoryDescriptor[sz_desc]; + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); auto cDefaultMemoryMap = 0; // Grab any usable entries. @@ -193,7 +189,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, SizeT lookIndex = 0UL; - for (; Descriptor[lookIndex].Kind != EfiMemoryType::EfiConventionalMemory; ++lookIndex) + for (; struct_ptr[lookIndex].Kind != EfiMemoryType::EfiConventionalMemory; ++lookIndex) { ZKA_UNUSED(0); } @@ -274,7 +270,9 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, } else { +#ifdef __ZKA_USE_FB__ CGDrawString("NEWOSLDR: PLEASE RECOVER YOUR MINKRNL IMAGE.", 30, 10, RGB(0xFF, 0xFF, 0xFF)); +#endif // __ZKA_USE_FB__ } Boot::BFileReader chimeWav(L"zka\\startup.wav", ImageHandle); @@ -300,13 +298,17 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, } else { +#ifdef __ZKA_USE_FB__ CGDrawString("NEWOSLDR: ONE OR MORE SYSTEM COMPONENTS ARE MISSING, PLEASE REFORMAT THE OS.", 30, 10, RGB(0xFF, 0xFF, 0xFF)); +#endif // __ZKA_USE_FB__ } - EFI::ExitBootServices(MapKey, ImageHandle); + EFI::ExitBootServices(map_key, ImageHandle); +#ifdef __ZKA_USE_FB__ CGDrawInRegion(CGColor(0xFF, 0x3A, 0x3A), handover_hdr->f_GOP.f_Height, handover_hdr->f_GOP.f_Width, 0, 0); CGFini(); +#endif // __ZKA_USE_FB__ // ---------------------------------------------------- // // Finally load Kernel, and the cr3 to it. diff --git a/dev/zba/src/HEL/AMD64/BootPlatform.cxx b/dev/zba/src/HEL/AMD64/BootPlatform.cxx index 31566a95..ff863a57 100644 --- a/dev/zba/src/HEL/AMD64/BootPlatform.cxx +++ b/dev/zba/src/HEL/AMD64/BootPlatform.cxx @@ -1,3 +1,4 @@ + /* ------------------------------------------- Copyright ZKA Technologies. @@ -102,4 +103,4 @@ void rt_hlt() Kernel::HAL::rt_halt(); } -#endif // 0 +#endif // __STANDALONE__ diff --git a/dev/zba/src/HEL/AMD64/BootString.cxx b/dev/zba/src/HEL/AMD64/BootString.cxx deleted file mode 100644 index 1c109b80..00000000 --- a/dev/zba/src/HEL/AMD64/BootString.cxx +++ /dev/null @@ -1,92 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - - File: String.cxx - Purpose: NewBoot string library - - Revision History: - - - -------------------------------------------- */ - -#include -#include -#include - -/// 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(CharacterTypeUTF8* src, const CharacterTypeUTF8 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/zba/src/HEL/AMD64/BootTextWriter.cxx b/dev/zba/src/HEL/AMD64/BootTextWriter.cxx deleted file mode 100644 index b0679715..00000000 --- a/dev/zba/src/HEL/AMD64/BootTextWriter.cxx +++ /dev/null @@ -1,169 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - - File: String.cxx - Purpose: NewBoot string library - - Revision History: - - - -------------------------------------------- */ - -#include -#include -#include -#include - -/// BUGS: 0 - -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -/** -@brief puts wrapper over EFI ConOut. -*/ -Boot::BTextWriter& Boot::BTextWriter::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; -} - -/// @brief UTF-8 equivalent of Write (UTF-16). -/// @param str the input string. -Boot::BTextWriter& Boot::BTextWriter::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; -} - -Boot::BTextWriter& Boot::BTextWriter::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; -} - -/** -@brief putc wrapper over EFI ConOut. -*/ -Boot::BTextWriter& Boot::BTextWriter::WriteCharacter(CharacterTypeUTF16 c) -{ -#ifdef __DEBUG__ - EfiCharType str[2]; - - str[0] = c; - str[1] = 0; - ST->ConOut->OutputString(ST->ConOut, str); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BTextWriter& Boot::BTextWriter::Write(const Long& x) -{ -#ifdef __DEBUG__ - this->_Write(x); - this->Write("h"); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BTextWriter& Boot::BTextWriter::_Write(const Long& 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 < 0) - y = -y; - - const char cNumbers[] = "0123456789ABCDEF"; - - this->WriteCharacter(cNumbers[h]); -#endif // ifdef __DEBUG__ - - return *this; -} diff --git a/dev/zba/src/HEL/AMD64/New+Delete.cxx b/dev/zba/src/HEL/AMD64/New+Delete.cxx deleted file mode 100644 index 4a308f94..00000000 --- a/dev/zba/src/HEL/AMD64/New+Delete.cxx +++ /dev/null @@ -1,62 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include -#include - -#ifdef __STANDALONE__ -#include /* Since we're using GCC for this EFI program. */ - -EXTERN EfiBootServices* BS; - -/// @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) == kBufferTooSmall) - BS->FreePool(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); - - return buf; -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete(void* buf) -{ - BS->FreePool(buf); -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete[](void* 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) -{ - BS->FreePool(buf); -} - -#endif // __STANDALONE__ diff --git a/dev/zba/src/HEL/AMD64/Support.cxx b/dev/zba/src/HEL/AMD64/Support.cxx deleted file mode 100644 index aa93318b..00000000 --- a/dev/zba/src/HEL/AMD64/Support.cxx +++ /dev/null @@ -1,82 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include -#include -#include -#include -#include -#include -#include - -#ifdef __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 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) -{ - if (!whatToCheck || *whatToCheck == 0) - return 0; - - SizeT len = 0; - - while (whatToCheck[len] == whatToCheckRight[len]) - { - if (whatToCheck[len] == 0) - return 0; - - ++len; - } - - return len; -} - -/// @brief somthing specific to the Microsoft's ABI, When the stack grows too big. -EXTERN_C void ___chkstk_ms(void) -{ -} - -#endif diff --git a/dev/zba/src/HEL/ARM64/BootMain.cxx b/dev/zba/src/HEL/ARM64/BootMain.cxx new file mode 100644 index 00000000..ff26b14c --- /dev/null +++ b/dev/zba/src/HEL/ARM64/BootMain.cxx @@ -0,0 +1,20 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include + +EXTERN EfiBootServices* BS; + +/// @brief Main EFI entrypoint. +/// @param ImageHandle Handle of this image. +/// @param SystemTable The system table of it. +/// @return nothing, never returns. +EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr ImageHandle, + EfiSystemTable* SystemTable) +{ + + return 0; +} diff --git a/dev/zba/src/HEL/ARM64/BootPlatform.cxx b/dev/zba/src/HEL/ARM64/BootPlatform.cxx new file mode 100644 index 00000000..fba4b61c --- /dev/null +++ b/dev/zba/src/HEL/ARM64/BootPlatform.cxx @@ -0,0 +1,40 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include + +#ifdef __STANDALONE__ + +using namespace Boot; + +EXTERN_C void rt_hlt() +{ + while (Yes); +} + +EXTERN_C void rt_cli() +{ + +} + +EXTERN_C void rt_sti() +{ + +} + +EXTERN_C void rt_cld() +{ + +} + +EXTERN_C void rt_std() +{ + +} + +#endif // __STANDALONE__ diff --git a/dev/zba/src/New+Delete.cxx b/dev/zba/src/New+Delete.cxx new file mode 100644 index 00000000..7240875a --- /dev/null +++ b/dev/zba/src/New+Delete.cxx @@ -0,0 +1,60 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include +#include +#include + +#ifdef __STANDALONE__ +EXTERN EfiBootServices* BS; + +/// @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) == kBufferTooSmall) + BS->FreePool(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); + + return buf; +} + +/// @brief Deletes the object. +/// @param buf the object. +void operator delete(void* buf) +{ + BS->FreePool(buf); +} + +/// @brief Deletes the object. +/// @param buf the object. +void operator delete[](void* 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) +{ + BS->FreePool(buf); +} + +#endif // __STANDALONE__ diff --git a/dev/zba/src/Thread.cxx b/dev/zba/src/Thread.cxx index b19894a9..2383d675 100644 --- a/dev/zba/src/Thread.cxx +++ b/dev/zba/src/Thread.cxx @@ -15,14 +15,6 @@ #include #include -EXTERN_C -{ -#include - - Void - rt_jump_to_address(VoidPtr start, VoidPtr handover); -} - // External boot services symbol. EXTERN EfiBootServices* BS; @@ -161,8 +153,6 @@ namespace Boot err_fn(handover); } - rt_jump_to_address(fStartAddress, handover); - reinterpret_cast(fStartAddress)(handover); } diff --git a/dev/zka/ArchKit/ArchKit.hxx b/dev/zka/ArchKit/ArchKit.hxx index 3c617705..e52eeabf 100644 --- a/dev/zka/ArchKit/ArchKit.hxx +++ b/dev/zka/ArchKit/ArchKit.hxx @@ -56,30 +56,6 @@ namespace Kernel { return *(volatile UInt32*)((UInt64)base + reg); } - - /// @brief Print a region of memory. - /// @param start - /// @param length - inline Void ke_print_raw_memory(const void* start, Size length) - { - const UInt8* ptr = (const UInt8*)start; - - for (Size i = 0; i < length; i++) - { - if (i % 16 == 0) - { - kcout << hex_number((UIntPtr)ptr + i); - } - else - { - kcout << hex_number(ptr[i]); - } - - kcout << " "; - } - - kcout << "\r"; - } } // namespace Kernel #define kKernelMaxSystemCalls (256) diff --git a/dev/zka/HALKit/AMD64/HalBMPMgr.cxx b/dev/zka/HALKit/AMD64/HalBMPMgr.cxx deleted file mode 100644 index e577bd7e..00000000 --- a/dev/zka/HALKit/AMD64/HalBMPMgr.cxx +++ /dev/null @@ -1,202 +0,0 @@ -/* ------------------------------------------- - - Copyright ZKA Technologies. - -------------------------------------------- */ - -#include - -#define cBitMpMagic ((Kernel::UIntPtr)0x10210) - -#ifdef __ZKA_AMD64__ -#include -#elif defined(__ZKA_ARM64__) -#include -#endif - -#include -#include - -#define cBitMapMagIdx (0) -#define cBitMapSizeIdx (1) -#define cBitMapUsedIdx (2) - -namespace Kernel -{ - namespace HAL - { - namespace Detail - { - /// \brief Proxy Interface to allocate a bitmap. - class IBitMapAllocator final - { - public: - explicit IBitMapAllocator() = default; - ~IBitMapAllocator() = default; - - ZKA_COPY_DELETE(IBitMapAllocator); - - auto IsBitMap(VoidPtr page_ptr) -> Bool - { - if (!page_ptr) - return No; - - UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr); - - if (!ptr_bit_set[cBitMapMagIdx] || - ptr_bit_set[cBitMapMagIdx] != cBitMpMagic) - return No; - - return Yes; - } - - auto FreeBitMap(VoidPtr page_ptr) -> Bool - { - if (this->IsBitMap(page_ptr) == No) - return No; - - UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr); - - ptr_bit_set[cBitMapMagIdx] = cBitMpMagic; - ptr_bit_set[cBitMapUsedIdx] = No; - - this->GetBitMapStatus(ptr_bit_set); - - mm_map_page(ptr_bit_set, ~eFlagsPresent); - mm_map_page(ptr_bit_set, ~eFlagsWr); - mm_map_page(ptr_bit_set, ~eFlagsUser); - - return Yes; - } - - UInt32 MakeFlags(Bool wr, Bool user) - { - - UInt32 flags = eFlagsPresent; - - if (wr) - flags |= eFlagsWr; - - if (user) - flags |= eFlagsUser; - - return flags; - } - - /// @brief Iterate over availables pages for a free one. - /// @return The new address which was found. - auto FindBitMap(VoidPtr base_ptr, SizeT size, Bool wr, Bool user) -> VoidPtr - { - VoidPtr base = reinterpret_cast(((UIntPtr)base_ptr) + kPageSize); - - while (base && size) - { - UIntPtr* ptr_bit_set = reinterpret_cast(base); - - if (ptr_bit_set[cBitMapMagIdx] == cBitMpMagic && - ptr_bit_set[cBitMapSizeIdx] <= size) - { - if (ptr_bit_set[cBitMapUsedIdx] == No) - { - ptr_bit_set[cBitMapSizeIdx] = size; - ptr_bit_set[cBitMapUsedIdx] = Yes; - - this->GetBitMapStatus(ptr_bit_set); - - UInt32 flags = this->MakeFlags(wr, user); - mm_map_page(ptr_bit_set, flags); - - return (VoidPtr)ptr_bit_set; - } - } - else if (ptr_bit_set[cBitMapMagIdx] != cBitMpMagic) - { - UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); - - ptr_bit_set[cBitMapMagIdx] = cBitMpMagic; - ptr_bit_set[cBitMapSizeIdx] = size; - ptr_bit_set[cBitMapUsedIdx] = Yes; - - this->GetBitMapStatus(ptr_bit_set); - - UInt32 flags = this->MakeFlags(wr, user); - mm_map_page(ptr_bit_set, flags); - - return (VoidPtr)ptr_bit_set; - } - - base = reinterpret_cast(reinterpret_cast(base_ptr) + (ptr_bit_set[0] != cBitMpMagic ? size : ptr_bit_set[1])); - - if ((UIntPtr)base_ptr < (reinterpret_cast(base) + kHandoverHeader->f_BitMapSize)) - return nullptr; - } - - return nullptr; - } - - /// @brief Print Bitmap status - auto GetBitMapStatus(UIntPtr* ptr_bit_set) -> Void - { - if (!this->IsBitMap(ptr_bit_set)) - { - kcout << "Not a BitMap: " << hex_number((UIntPtr)ptr_bit_set) << endl; - return; - } - - kcout << "Magic BitMap Number: " << hex_number(ptr_bit_set[cBitMapMagIdx]) << endl; - kcout << "Allocated: " << (ptr_bit_set[cBitMapUsedIdx] ? "Yes" : "No") << endl; - kcout << "Size of BitMap (B): " << number(ptr_bit_set[cBitMapSizeIdx]) << endl; - kcout << "Size of BitMap (KIB): " << number(KIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Size of BitMap (MIB): " << number(MIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Size of BitMap (GIB): " << number(GIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Size of BitMap (TIB): " << number(TIB(ptr_bit_set[cBitMapSizeIdx])) << endl; - kcout << "Address Of BitMap: " << hex_number((UIntPtr)ptr_bit_set) << endl; - } - }; - } // namespace Detail - - /// @brief Allocate a new page to be used by the OS. - /// @param wr read/write bit. - /// @param user user bit. - /// @return - auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size) -> VoidPtr - { - VoidPtr ptr_new = nullptr; - Detail::IBitMapAllocator traits; - - ptr_new = traits.FindBitMap(kKernelBitMpStart, size, wr, user); - - if (!ptr_new) - { - ke_stop(RUNTIME_CHECK_PAGE); - } - - if (wr) - mm_map_page(ptr_new, eFlagsWr | eFlagsPresent); - else if (user && wr) - mm_map_page(ptr_new, eFlagsUser | eFlagsWr | eFlagsPresent); - else if (user) - mm_map_page(ptr_new, eFlagsUser | eFlagsPresent); - else - mm_map_page(ptr_new, eFlagsPresent); - - return (UIntPtr*)ptr_new; - } - - auto mm_free_bitmap(VoidPtr page_ptr) -> Bool - { - if (!page_ptr) - return No; - - Detail::IBitMapAllocator traits; - Bool ret = traits.FreeBitMap(page_ptr); - - if (ret) - { - mm_map_page(page_ptr, ~eFlagsPresent); - } - - return ret; - } - } // namespace HAL -} // namespace Kernel diff --git a/dev/zka/HALKit/ARM64/HalSchedulerCore.cxx b/dev/zka/HALKit/ARM64/HalSchedulerCore.cxx index 7545b350..630df4fd 100644 --- a/dev/zka/HALKit/ARM64/HalSchedulerCore.cxx +++ b/dev/zka/HALKit/ARM64/HalSchedulerCore.cxx @@ -6,19 +6,16 @@ #include -using namespace Kernel; - -Void UserProcess::SetImageStart(UIntPtr& imageStart) noexcept +namespace Kernel { - if (imageStart == 0) - this->Crash(); + Void UserProcess::SetImageStart(VoidPtr image_start) noexcept + { + if (image_start == 0) + this->Crash(); - this->StackFrame->BP = imageStart; - this->StackFrame->SP = this->StackFrame->BP; -} + this->Image = image_start; + } -namespace Kernel -{ bool hal_check_stack(HAL::StackFramePtr stackPtr) { if (!stackPtr) diff --git a/dev/zka/HALKit/ARM64/HalTimer.cxx b/dev/zka/HALKit/ARM64/HalTimer.cxx index 4ef87227..fd3bf87b 100644 --- a/dev/zka/HALKit/ARM64/HalTimer.cxx +++ b/dev/zka/HALKit/ARM64/HalTimer.cxx @@ -12,5 +12,3 @@ ------------------------------------------- */ #include - -struct TIMER_INFO; diff --git a/dev/zka/amd64-efi.make b/dev/zka/amd64-efi.make index 6c69a423..f1a8742b 100644 --- a/dev/zka/amd64-efi.make +++ b/dev/zka/amd64-efi.make @@ -3,13 +3,13 @@ # This is the minoskrnl's makefile. ################################################## -CC = x86_64-w64-mingw32-g++.exe -LD = x86_64-w64-mingw32-ld.exe +CC = x86_64-w64-mingw32-g++ +LD = x86_64-w64-mingw32-ld CCFLAGS = -fshort-wchar -c -D__ZKA_AMD64__ -mno-red-zone -fno-rtti -fno-exceptions \ -std=c++20 -D__ZKA_SUPPORT_NX__ -I../Vendor -D__FSKIT_USE_NEFS__ \ -D__NEWOSKRNL__ -D__HAVE_ZKA_APIS__ -D__FREESTANDING__ -D__ZKA__ -I./ -I../ -I../zba -ASM = nasm.exe +ASM = nasm DISK_DRV = diff --git a/dev/zka/arm64-efi.make b/dev/zka/arm64-efi.make index 51753026..51ea8638 100644 --- a/dev/zka/arm64-efi.make +++ b/dev/zka/arm64-efi.make @@ -7,7 +7,7 @@ CC = clang++ LD = lld-link CCFLAGS = -fshort-wchar -c -ffreestanding -MMD -mno-red-zone -D__ZKA_ARM64__ -fno-rtti -fno-exceptions -I./ \ -target aarch64-unknown-windows \ - -std=c++20 -D__FSKIT_USE_NEFS__ -D__ZETA_MACHINE__ -D__NEWOSKRNL__ -D__HAVE_ZKA_APIS__ -D__ZKA__ -I../ + -std=c++20 -D__FSKIT_USE_NEFS__ -D__NEWOSKRNL__ -D__HAVE_ZKA_APIS__ -D__ZKA__ -I../ ASM = clang++ diff --git a/dev/zka/src/BitMapMgr.cxx b/dev/zka/src/BitMapMgr.cxx new file mode 100644 index 00000000..e577bd7e --- /dev/null +++ b/dev/zka/src/BitMapMgr.cxx @@ -0,0 +1,202 @@ +/* ------------------------------------------- + + Copyright ZKA Technologies. + +------------------------------------------- */ + +#include + +#define cBitMpMagic ((Kernel::UIntPtr)0x10210) + +#ifdef __ZKA_AMD64__ +#include +#elif defined(__ZKA_ARM64__) +#include +#endif + +#include +#include + +#define cBitMapMagIdx (0) +#define cBitMapSizeIdx (1) +#define cBitMapUsedIdx (2) + +namespace Kernel +{ + namespace HAL + { + namespace Detail + { + /// \brief Proxy Interface to allocate a bitmap. + class IBitMapAllocator final + { + public: + explicit IBitMapAllocator() = default; + ~IBitMapAllocator() = default; + + ZKA_COPY_DELETE(IBitMapAllocator); + + auto IsBitMap(VoidPtr page_ptr) -> Bool + { + if (!page_ptr) + return No; + + UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr); + + if (!ptr_bit_set[cBitMapMagIdx] || + ptr_bit_set[cBitMapMagIdx] != cBitMpMagic) + return No; + + return Yes; + } + + auto FreeBitMap(VoidPtr page_ptr) -> Bool + { + if (this->IsBitMap(page_ptr) == No) + return No; + + UIntPtr* ptr_bit_set = reinterpret_cast(page_ptr); + + ptr_bit_set[cBitMapMagIdx] = cBitMpMagic; + ptr_bit_set[cBitMapUsedIdx] = No; + + this->GetBitMapStatus(ptr_bit_set); + + mm_map_page(ptr_bit_set, ~eFlagsPresent); + mm_map_page(ptr_bit_set, ~eFlagsWr); + mm_map_page(ptr_bit_set, ~eFlagsUser); + + return Yes; + } + + UInt32 MakeFlags(Bool wr, Bool user) + { + + UInt32 flags = eFlagsPresent; + + if (wr) + flags |= eFlagsWr; + + if (user) + flags |= eFlagsUser; + + return flags; + } + + /// @brief Iterate over availables pages for a free one. + /// @return The new address which was found. + auto FindBitMap(VoidPtr base_ptr, SizeT size, Bool wr, Bool user) -> VoidPtr + { + VoidPtr base = reinterpret_cast(((UIntPtr)base_ptr) + kPageSize); + + while (base && size) + { + UIntPtr* ptr_bit_set = reinterpret_cast(base); + + if (ptr_bit_set[cBitMapMagIdx] == cBitMpMagic && + ptr_bit_set[cBitMapSizeIdx] <= size) + { + if (ptr_bit_set[cBitMapUsedIdx] == No) + { + ptr_bit_set[cBitMapSizeIdx] = size; + ptr_bit_set[cBitMapUsedIdx] = Yes; + + this->GetBitMapStatus(ptr_bit_set); + + UInt32 flags = this->MakeFlags(wr, user); + mm_map_page(ptr_bit_set, flags); + + return (VoidPtr)ptr_bit_set; + } + } + else if (ptr_bit_set[cBitMapMagIdx] != cBitMpMagic) + { + UIntPtr* ptr_bit_set = reinterpret_cast(base_ptr); + + ptr_bit_set[cBitMapMagIdx] = cBitMpMagic; + ptr_bit_set[cBitMapSizeIdx] = size; + ptr_bit_set[cBitMapUsedIdx] = Yes; + + this->GetBitMapStatus(ptr_bit_set); + + UInt32 flags = this->MakeFlags(wr, user); + mm_map_page(ptr_bit_set, flags); + + return (VoidPtr)ptr_bit_set; + } + + base = reinterpret_cast(reinterpret_cast(base_ptr) + (ptr_bit_set[0] != cBitMpMagic ? size : ptr_bit_set[1])); + + if ((UIntPtr)base_ptr < (reinterpret_cast(base) + kHandoverHeader->f_BitMapSize)) + return nullptr; + } + + return nullptr; + } + + /// @brief Print Bitmap status + auto GetBitMapStatus(UIntPtr* ptr_bit_set) -> Void + { + if (!this->IsBitMap(ptr_bit_set)) + { + kcout << "Not a BitMap: " << hex_number((UIntPtr)ptr_bit_set) << endl; + return; + } + + kcout << "Magic BitMap Number: " << hex_number(ptr_bit_set[cBitMapMagIdx]) << endl; + kcout << "Allocated: " << (ptr_bit_set[cBitMapUsedIdx] ? "Yes" : "No") << endl; + kcout << "Size of BitMap (B): " << number(ptr_bit_set[cBitMapSizeIdx]) << endl; + kcout << "Size of BitMap (KIB): " << number(KIB(ptr_bit_set[cBitMapSizeIdx])) << endl; + kcout << "Size of BitMap (MIB): " << number(MIB(ptr_bit_set[cBitMapSizeIdx])) << endl; + kcout << "Size of BitMap (GIB): " << number(GIB(ptr_bit_set[cBitMapSizeIdx])) << endl; + kcout << "Size of BitMap (TIB): " << number(TIB(ptr_bit_set[cBitMapSizeIdx])) << endl; + kcout << "Address Of BitMap: " << hex_number((UIntPtr)ptr_bit_set) << endl; + } + }; + } // namespace Detail + + /// @brief Allocate a new page to be used by the OS. + /// @param wr read/write bit. + /// @param user user bit. + /// @return + auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size) -> VoidPtr + { + VoidPtr ptr_new = nullptr; + Detail::IBitMapAllocator traits; + + ptr_new = traits.FindBitMap(kKernelBitMpStart, size, wr, user); + + if (!ptr_new) + { + ke_stop(RUNTIME_CHECK_PAGE); + } + + if (wr) + mm_map_page(ptr_new, eFlagsWr | eFlagsPresent); + else if (user && wr) + mm_map_page(ptr_new, eFlagsUser | eFlagsWr | eFlagsPresent); + else if (user) + mm_map_page(ptr_new, eFlagsUser | eFlagsPresent); + else + mm_map_page(ptr_new, eFlagsPresent); + + return (UIntPtr*)ptr_new; + } + + auto mm_free_bitmap(VoidPtr page_ptr) -> Bool + { + if (!page_ptr) + return No; + + Detail::IBitMapAllocator traits; + Bool ret = traits.FreeBitMap(page_ptr); + + if (ret) + { + mm_map_page(page_ptr, ~eFlagsPresent); + } + + return ret; + } + } // namespace HAL +} // namespace Kernel -- cgit v1.2.3