From a0a90c80e5dd17df8f609aebc253b4bf9147f90f Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Wed, 4 Sep 2024 12:56:23 +0200 Subject: Created a new branch for the overhaul project. Signed-off-by: Amlal El Mahrouss --- dev/ZBA/Sources/HEL/AMD64/BootAPI.S | 47 +++++++++++++++++ dev/ZBA/Sources/HEL/AMD64/BootJump.S | 41 --------------- dev/ZBA/Sources/HEL/AMD64/BootMain.cxx | 77 +++++++++++++++------------- dev/ZBA/Sources/HEL/AMD64/BootTextWriter.cxx | 8 +-- dev/ZBA/Sources/Thread.cxx | 2 +- 5 files changed, 93 insertions(+), 82 deletions(-) create mode 100644 dev/ZBA/Sources/HEL/AMD64/BootAPI.S delete mode 100644 dev/ZBA/Sources/HEL/AMD64/BootJump.S (limited to 'dev/ZBA/Sources') diff --git a/dev/ZBA/Sources/HEL/AMD64/BootAPI.S b/dev/ZBA/Sources/HEL/AMD64/BootAPI.S new file mode 100644 index 00000000..e391351d --- /dev/null +++ b/dev/ZBA/Sources/HEL/AMD64/BootAPI.S @@ -0,0 +1,47 @@ +.global rt_jump_to_address +.global rt_reset_hardware + +.text + +.intel_syntax noprefix + +/** + @brief this function setups a stack and then jumps to + a function */ +rt_jump_to_address: + mov rbx, rcx + mov rcx, rdx + jmp rbx + + ret + +rt_reset_hardware: + /* dont raise any interrupts. (except ofc NMIs.) */ + cli +wait_gate1: + /* wait for gate 1 to be ready? */ + in al,0x64 + and al,2 + jnz wait_gate1 + mov al,0x0D1 + out 0x64,al +wait_gate2: + /* wait for gate 2 to be ready? */ + in al,0x64 + and al,2 + jnz wait_gate2 + mov al,0x0FE + out 0x60,al + + /* trigger triple fault, by writing to cr4 */ + + xor rax,rax + mov cr4,rax +reset_wait: + jmp reset_wait + +.global write_cr3 + +write_cr3: + mov cr3, rcx + ret \ No newline at end of file diff --git a/dev/ZBA/Sources/HEL/AMD64/BootJump.S b/dev/ZBA/Sources/HEL/AMD64/BootJump.S deleted file mode 100644 index d8e09d4e..00000000 --- a/dev/ZBA/Sources/HEL/AMD64/BootJump.S +++ /dev/null @@ -1,41 +0,0 @@ -.global rt_jump_to_address -.global rt_reset_hardware - -.text - -.intel_syntax noprefix - -/** - @brief this function setups a stack and then jumps to - a function */ -rt_jump_to_address: - mov rbx, rcx - mov rcx, rdx - jmp rbx - - ret - -rt_reset_hardware: - /* dont raise any interrupts. (except ofc NMIs.) */ - cli -wait_gate1: - /* wait for gate 1 to be ready? */ - in al,0x64 - and al,2 - jnz wait_gate1 - mov al,0x0D1 - out 0x64,al -wait_gate2: - /* wait for gate 2 to be ready? */ - in al,0x64 - and al,2 - jnz wait_gate2 - mov al,0x0FE - out 0x60,al - - /* trigger triple fault, by writing to cr4 */ - - xor rax,rax - mov cr4,rax -reset_wait: - jmp reset_wait diff --git a/dev/ZBA/Sources/HEL/AMD64/BootMain.cxx b/dev/ZBA/Sources/HEL/AMD64/BootMain.cxx index 576ada08..55f10ea2 100644 --- a/dev/ZBA/Sources/HEL/AMD64/BootMain.cxx +++ b/dev/ZBA/Sources/HEL/AMD64/BootMain.cxx @@ -79,6 +79,8 @@ STATIC Bool CheckBootDevice(BootDeviceATA& ataDev) return true; } +EXTERN_C Void write_cr3(VoidPtr new_cr3); + /// @brief Main EFI entrypoint. /// @param ImageHandle Handle of this image. /// @param SystemTable The system table of it. @@ -186,17 +188,45 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, handoverHdrPtr->f_FirmwareSpecific[HEL::kHandoverSpecificMemoryEfi] = (UIntPtr)Descriptor; - handoverHdrPtr->f_VirtualStart = - (VoidPtr)Descriptor[cDefaultMemoryMap].VirtualStart; + handoverHdrPtr->f_BitMapStart = 0; - handoverHdrPtr->f_HeapStart = 0; + while (BS->AllocatePool(EfiRuntimeServicesData, kHandoverHeapSz, &handoverHdrPtr->f_BitMapStart) != kEfiOk) + ; - while (BS->AllocatePool(EfiLoaderData, kHandoverHeapSz, &handoverHdrPtr->f_HeapStart) != kEfiOk) + auto extended_heap = (VoidPtr)((UIntPtr)handoverHdrPtr->f_BitMapStart + kHandoverHeapSz); + + while (BS->AllocatePool(EfiRuntimeServicesData, kHandoverHeapSz, &extended_heap) != kEfiOk) ; handoverHdrPtr->f_VirtualSize = Descriptor[cDefaultMemoryMap].NumberOfPages; /* # of pages */ + + handoverHdrPtr->f_FirmwareCustomTables[0] = (VoidPtr)BS; + handoverHdrPtr->f_FirmwareCustomTables[1] = (VoidPtr)ST; + + BFileReader readerSysChk(L"syschk.sys", ImageHandle); + readerSysChk.ReadAll(0); + + Boot::BThread* loaderSysChk = nullptr; + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (readerSysChk.Blob()) + { + loaderSysChk = new Boot::BThread(readerSysChk.Blob()); + loaderSysChk->SetName("System Check SYS."); + } + + loaderSysChk->Start(handoverHdrPtr); + + // nullify these fields, to avoid being reused later. + + handoverHdrPtr->f_FirmwareCustomTables[0] = nullptr; + handoverHdrPtr->f_FirmwareCustomTables[1] = nullptr; + handoverHdrPtr->f_FirmwareVendorLen = BStrLen(SystemTable->FirmwareVendor); handoverHdrPtr->f_Magic = kHandoverMagic; @@ -213,6 +243,12 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, BDiskFormatFactory checkPart; + // ---------------------------------------------------- // + // The following checks for an exisiting partition + // inside the disk, if it doesn't have one, + // format the disk. + // ---------------------------------------------------- // + if (!checkPart.IsPartitionValid()) { BDiskFormatFactory::BFileDescriptor root; @@ -226,37 +262,6 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, rt_reset_hardware(); } - // ---------------------------------------------------- // - // The following checks for an exisiting partition - // inside the disk, if it doesn't have one, - // format the disk. - // ---------------------------------------------------- // - - handoverHdrPtr->f_FirmwareCustomTables[0] = (VoidPtr)BS; - handoverHdrPtr->f_FirmwareCustomTables[1] = (VoidPtr)ST; - - BFileReader readerSysChk(L"syschk.sys", ImageHandle); - readerSysChk.ReadAll(0); - - Boot::BThread* loaderSysChk = nullptr; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (readerSysChk.Blob()) - { - loaderSysChk = new Boot::BThread(readerSysChk.Blob()); - loaderSysChk->SetName("64-bit System Check DLL."); - } - - loaderSysChk->Start(handoverHdrPtr); - - // nullify these fields, to avoid being reused later. - - handoverHdrPtr->f_FirmwareCustomTables[0] = nullptr; - handoverHdrPtr->f_FirmwareCustomTables[1] = nullptr; - BFileReader readerKernel(L"newoskrnl.exe", ImageHandle); readerKernel.ReadAll(0); @@ -270,7 +275,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, if (readerKernel.Blob()) { loader = new Boot::BThread(readerKernel.Blob()); - loader->SetName("64-bit Kernel DLL."); + loader->SetName("64-Bit Kernel EXE."); handoverHdrPtr->f_KernelImage = readerKernel.Blob(); } diff --git a/dev/ZBA/Sources/HEL/AMD64/BootTextWriter.cxx b/dev/ZBA/Sources/HEL/AMD64/BootTextWriter.cxx index 467f4303..65ea3d89 100644 --- a/dev/ZBA/Sources/HEL/AMD64/BootTextWriter.cxx +++ b/dev/ZBA/Sources/HEL/AMD64/BootTextWriter.cxx @@ -25,7 +25,7 @@ */ BTextWriter& BTextWriter::Write(const CharacterTypeUTF16* str) { -#ifdef __DEBUG__ +#ifdef __DEBUG_OUTPUT__ if (!str || *str == 0) return *this; @@ -57,7 +57,7 @@ BTextWriter& BTextWriter::Write(const CharacterTypeUTF16* str) /// @param str the input string. BTextWriter& BTextWriter::Write(const Char* str) { -#ifdef __DEBUG__ +#ifdef __DEBUG_OUTPUT__ if (!str || *str == 0) return *this; @@ -87,7 +87,7 @@ BTextWriter& BTextWriter::Write(const Char* str) BTextWriter& BTextWriter::Write(const UChar* str) { -#ifdef __DEBUG__ +#ifdef __DEBUG_OUTPUT__ if (!str || *str == 0) return *this; @@ -120,7 +120,7 @@ BTextWriter& BTextWriter::Write(const UChar* str) */ BTextWriter& BTextWriter::WriteCharacter(CharacterTypeUTF16 c) { -#ifdef __DEBUG__ +#ifdef __DEBUG_OUTPUT__ EfiCharType str[2]; str[0] = c; diff --git a/dev/ZBA/Sources/Thread.cxx b/dev/ZBA/Sources/Thread.cxx index 1ea83618..13b02b92 100644 --- a/dev/ZBA/Sources/Thread.cxx +++ b/dev/ZBA/Sources/Thread.cxx @@ -165,7 +165,7 @@ namespace Boot err_fn(handover); } - rt_jump_to_address(fStartAddress, handover); + reinterpret_cast(fStartAddress)(handover); } const Char* BThread::GetName() -- cgit v1.2.3