diff options
| author | Amlal EL Mahrouss <amlalelmahrouss@icloud.com> | 2024-08-04 16:29:09 +0200 |
|---|---|---|
| committer | Amlal EL Mahrouss <amlalelmahrouss@icloud.com> | 2024-08-04 16:29:09 +0200 |
| commit | 1b92501a27f8781945bc6b19bb43e22588d4c933 (patch) | |
| tree | 8e73c4045188b0620bd8612dacdbab449ae97f94 /Boot/Sources | |
| parent | a38083f7d528111087949a0ba8e3970f091f2fc9 (diff) | |
[IMP] ProgramLoader class for PE32+.
Signed-off-by: Amlal EL Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Boot/Sources')
| -rw-r--r-- | Boot/Sources/HEL/AMD64/BootFileReader.cxx | 30 | ||||
| -rw-r--r-- | Boot/Sources/HEL/AMD64/BootMain.cxx | 10 | ||||
| -rw-r--r-- | Boot/Sources/HEL/AMD64/Support.cxx | 19 | ||||
| -rw-r--r-- | Boot/Sources/ProgramLoader.cxx | 46 |
4 files changed, 60 insertions, 45 deletions
diff --git a/Boot/Sources/HEL/AMD64/BootFileReader.cxx b/Boot/Sources/HEL/AMD64/BootFileReader.cxx index f784bf6f..7b07ee23 100644 --- a/Boot/Sources/HEL/AMD64/BootFileReader.cxx +++ b/Boot/Sources/HEL/AMD64/BootFileReader.cxx @@ -123,12 +123,12 @@ BFileReader::~BFileReader() @param **readUntil** size of file @param **chunkToRead** chunk to read each time. */ -Void BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead) +Void BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr outAddress) { if (mBlob == nullptr) { EfiFileInfo newPtrInfo; - UInt32 szInfo = 0; + UInt32 szInfo = 0; EfiGUID cFileInfoGUID = EFI_FILE_INFO_GUID; @@ -142,11 +142,18 @@ Void BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead) mWriter.Write(L"newosldr: physical size: ").Write(readUntil).Write("\r"); } - if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*)&mBlob) != - kEfiOk) + if (!outAddress) { - mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); - EFI::ThrowError(L"OutOfMemory", L"Out of memory."); + 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; } } @@ -163,16 +170,7 @@ Void BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead) if (res == kBufferTooSmall) { - mErrorCode = kTooSmall; - return; - } - else if (res == kEfiOk) - { - continue; - } - else - { - break; + bufSize = chunkToRead; } } diff --git a/Boot/Sources/HEL/AMD64/BootMain.cxx b/Boot/Sources/HEL/AMD64/BootMain.cxx index 0c0c9953..6aec67f5 100644 --- a/Boot/Sources/HEL/AMD64/BootMain.cxx +++ b/Boot/Sources/HEL/AMD64/BootMain.cxx @@ -157,12 +157,6 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, CGDrawInRegion(CGColor(0xFF, 0xFF, 0xFF), handoverHdrPtr->f_GOP.f_Height, handoverHdrPtr->f_GOP.f_Width, 0, 0); CGFini(); - // check if we are running in the PC platform. If so abort. -#if defined(__NEWOS_AMD64__) && !defined(__DEBUG__) - writer.Write(L"\rnewosldr: AMD64 support is not official.\r"); - EFI::ThrowError(L"Beta-Software", L"Beta Software."); -#endif - // ---------------------------------------------------- // // The following checks for an exisiting partition // inside the disk, if it doesn't have one, @@ -170,7 +164,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, // ---------------------------------------------------- // BFileReader readerKernel(L"newoskrnl.dll", ImageHandle); - + readerKernel.ReadAll(0); Boot::ProgramLoader* loader = nullptr; @@ -198,7 +192,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, // ---------------------------------------------------- // cg_write_text("NEWOSLDR (C) ZKA TECHNOLOGIES.", 10, 10, RGB(0x00, 0x00, 0x00)); - cg_write_text("SMP OS (MAX 8 CORES).", 20, 10, RGB(0x00, 0x00, 0x00)); + cg_write_text("LOADING NEWOSKRNL...", 20, 10, RGB(0x00, 0x00, 0x00)); loader->Start(handoverHdrPtr); diff --git a/Boot/Sources/HEL/AMD64/Support.cxx b/Boot/Sources/HEL/AMD64/Support.cxx index a2ac6394..8c82f78a 100644 --- a/Boot/Sources/HEL/AMD64/Support.cxx +++ b/Boot/Sources/HEL/AMD64/Support.cxx @@ -58,6 +58,25 @@ EXTERN_C size_t strlen(const char* whatToCheck) 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 whatToCheck[len] == whatToCheckRight[len] ? 0 : len; +} + /// @brief somthing specific to the Microsoft's ABI, When the stack grows too big. EXTERN_C void ___chkstk_ms(void) { diff --git a/Boot/Sources/ProgramLoader.cxx b/Boot/Sources/ProgramLoader.cxx index e4f61630..7461b87b 100644 --- a/Boot/Sources/ProgramLoader.cxx +++ b/Boot/Sources/ProgramLoader.cxx @@ -46,43 +46,47 @@ namespace Boot ExecHeaderPtr hdrPtr = ldr_find_exec_header(firstBytes); ExecOptionalHeaderPtr optHdr = ldr_find_opt_exec_header(firstBytes); - // ================================ // - // Allocate stack. - // ================================ // - fStackPtr = new Char[optHdr->mSizeOfStackReserve]; + auto numSecs = hdrPtr->mNumberOfSections; writer.Write("newosldr: Major Linker Ver: ").Write(optHdr->mMajorLinkerVersion).Write("\r"); writer.Write("newosldr: Minor Linker Ver: ").Write(optHdr->mMinorLinkerVersion).Write("\r"); writer.Write("newosldr: Major Subsystem Ver: ").Write(optHdr->mMajorSubsystemVersion).Write("\r"); writer.Write("newosldr: Minor Subsystem Ver: ").Write(optHdr->mMinorSubsystemVersion).Write("\r"); - writer.Write("newosldr: Magic: ").Write(optHdr->mMagic).Write("\r"); + writer.Write("newosldr: Magic: ").Write(hdrPtr->mSignature).Write("\r"); writer.Write("newosldr: ImageBase: ").Write(optHdr->mImageBase).Write("\r"); - EfiPhysicalAddress base_img_addr = optHdr->mImageBase; + constexpr auto cPageSize = 512; - constexpr auto cMaxSectionsOfKernel = 10; + EfiPhysicalAddress loadStartAddress = optHdr->mImageBase; + loadStartAddress += optHdr->mBaseOfData; - BS->AllocatePages(EfiAllocateType::AllocateAnyPages, EfiMemoryType::EfiLoaderCode, cMaxSectionsOfKernel, &base_img_addr); + auto numPages = optHdr->mSizeOfImage / cPageSize; + BS->AllocatePages(AllocateAddress, EfiLoaderData, numPages, &loadStartAddress); - ExecSectionHeaderPtr sectPtr = (ExecSectionHeaderPtr)((UIntPtr)firstBytes + ((DosHeaderPtr)firstBytes)->eLfanew + hdrPtr->mSizeOfOptionalHeader + sizeof(ExecHeader) + sizeof(UInt32)); + ExecSectionHeaderPtr sectPtr = (ExecSectionHeaderPtr)(((Char*)optHdr) + hdrPtr->mSizeOfOptionalHeader); - for (SizeT sectIndex = 0; sectIndex < cMaxSectionsOfKernel; ++sectIndex) + for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) { ExecSectionHeaderPtr sect = §Ptr[sectIndex]; - EfiPhysicalAddress address_to_alloc = sect->mVirtualAddress; - - // if this is a code header, then we can look for the entrypoint. - if (sect->mCharacteristics & eUserSection) + if (strcmp(".text", sect->mName) == 0) { - if (!fStartAddress) - { - fStartAddress = (VoidPtr)((VoidPtr)((UIntPtr)sect->mPointerToRawData + (sect->mVirtualAddress - optHdr->mAddressOfEntryPoint))); - - writer.Write("newosldr: Start Address set: ").Write((UIntPtr)fStartAddress).Write("\r"); - } + fStartAddress = (VoidPtr)((UIntPtr)fBlob + sect->mVirtualAddress + optHdr->mAddressOfEntryPoint); } + + CopyMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->mPointerToRawData), sect->mSizeOfRawData); } + + EfiPhysicalAddress start = (EfiPhysicalAddress)fStartAddress; + + BS->AllocatePages(AllocateAddress, EfiLoaderData, 1, &start); + + writer.Write("newosldr: Start Address: ").Write((UIntPtr)fStartAddress).Write("\r"); + + // ================================ // + // Allocate stack. + // ================================ // + fStackPtr = new Char[optHdr->mSizeOfStackReserve]; } else if (firstBytes[0] == kPefMagic[0] && firstBytes[1] == kPefMagic[1] && @@ -117,7 +121,7 @@ namespace Boot BTextWriter writer; writer.Write("newosldr: Exec format error, Thread has been aborted.\r"); - EFI::ThrowError(L"Exec-Format-Error", L"Format doesn't match (Thread aborted.)"); + EFI::ThrowError(L"Exec-Format-Error", L"Format doesn't match (Thread aborted)."); }; if (!fStartAddress) |
