summaryrefslogtreecommitdiffhomepage
path: root/Boot
diff options
context:
space:
mode:
authorAmlal <amlal@zka.com>2024-07-18 09:43:27 +0200
committerAmlal <amlal@zka.com>2024-07-18 09:43:27 +0200
commit384300904e6cf9187e5e4c4d9a8fad740592cacb (patch)
treef0a0f196f32f5a224ca529ad7d4e99dd6a95586e /Boot
parente9b8d8f68bdd79907feeed9e87572ba562c213e9 (diff)
[IMP] BootJump has been fixed, LoaderUtils API for CFKit. (Kernel's CoreFoundation like API)
[IMP] Add Write for UChar* types. (BTextWriter) Signed-off-by: Amlal <amlal@zka.com>
Diffstat (limited to 'Boot')
-rw-r--r--Boot/BootKit/BootKit.hxx1
-rw-r--r--Boot/BootKit/ProgramLoader.hxx3
-rw-r--r--Boot/Sources/HEL/AMD64/BootJump.S11
-rw-r--r--Boot/Sources/HEL/AMD64/BootMain.cxx14
-rw-r--r--Boot/Sources/HEL/AMD64/BootTextWriter.cxx30
-rw-r--r--Boot/Sources/ProgramLoader.cxx49
6 files changed, 87 insertions, 21 deletions
diff --git a/Boot/BootKit/BootKit.hxx b/Boot/BootKit/BootKit.hxx
index 16f81696..1b93bd30 100644
--- a/Boot/BootKit/BootKit.hxx
+++ b/Boot/BootKit/BootKit.hxx
@@ -66,6 +66,7 @@ public:
BTextWriter& Write(const Char* str);
BTextWriter& Write(const CharacterTypeUTF16* str);
BTextWriter& WriteCharacter(CharacterTypeUTF16 c);
+ BTextWriter& Write(const UChar* str);
public:
explicit BTextWriter() = default;
diff --git a/Boot/BootKit/ProgramLoader.hxx b/Boot/BootKit/ProgramLoader.hxx
index 6f789d37..90f8996f 100644
--- a/Boot/BootKit/ProgramLoader.hxx
+++ b/Boot/BootKit/ProgramLoader.hxx
@@ -35,7 +35,8 @@ namespace Boot
private:
Char fBlobName[255];
+ Char* fStackPtr{nullptr};
VoidPtr fStartAddress{nullptr};
VoidPtr fBlob{nullptr};
};
-} // namespace Boot \ No newline at end of file
+} // namespace Boot
diff --git a/Boot/Sources/HEL/AMD64/BootJump.S b/Boot/Sources/HEL/AMD64/BootJump.S
index e807ab52..fc7b3c68 100644
--- a/Boot/Sources/HEL/AMD64/BootJump.S
+++ b/Boot/Sources/HEL/AMD64/BootJump.S
@@ -8,11 +8,12 @@
@brief this function setups a stack and then jumps to
a function */
rt_jump_to_address:
- mov rdx, rsp
- mov rdi, rcx
- mov rdx, rbp
+ mov r8, rsp
+
push rax
- mov r8, rcx
- call rdi
+ push rdx
+ jmp rcx
+ pop rdx
pop rax
+
ret
diff --git a/Boot/Sources/HEL/AMD64/BootMain.cxx b/Boot/Sources/HEL/AMD64/BootMain.cxx
index 51f8e8cf..8946aadc 100644
--- a/Boot/Sources/HEL/AMD64/BootMain.cxx
+++ b/Boot/Sources/HEL/AMD64/BootMain.cxx
@@ -13,7 +13,7 @@
#include <FirmwareKit/Handover.hxx>
#include <KernelKit/MSDOS.hxx>
#include <KernelKit/PE.hxx>
-#include <KernelKit/PEF.hpp>
+#include <KernelKit/PEF.hxx>
#include <NewKit/Macros.hpp>
#include <NewKit/Ref.hpp>
#include <BootKit/ProgramLoader.hxx>
@@ -121,8 +121,8 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle,
vendorTable[4] == 'P' && vendorTable[5] == 'T' &&
vendorTable[6] == 'R' && vendorTable[7] == ' ')
{
- writer.Write(L"newosldr: filling rsdptr...\r");
- handoverHdrPtr->f_HardwareTables.f_RsdPtr = (VoidPtr)vendorTable;
+ writer.Write(L"newosldr: Filling rsdptr...\r");
+ handoverHdrPtr->f_HardwareTables.f_VendorPtr = (VoidPtr)vendorTable;
break;
}
@@ -149,8 +149,8 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle,
kHandoverHeader = handoverHdrPtr;
- // check if we are in AMD64
-#if defined(__NEWOS_AMD64__)
+ // 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
@@ -244,10 +244,10 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle,
#ifdef __NEWOS_OTA__
if (loader)
loader->Start(handoverHdrPtr);
+#else
+ hal_init_platform(handoverHdrPtr);
#endif // ifdef __NEWOS_OTA__
- hal_init_platform(handoverHdrPtr);
-
EFI::Stop();
CANT_REACH();
diff --git a/Boot/Sources/HEL/AMD64/BootTextWriter.cxx b/Boot/Sources/HEL/AMD64/BootTextWriter.cxx
index 18f32bd8..7b0ab50c 100644
--- a/Boot/Sources/HEL/AMD64/BootTextWriter.cxx
+++ b/Boot/Sources/HEL/AMD64/BootTextWriter.cxx
@@ -85,6 +85,36 @@ BTextWriter& BTextWriter::Write(const Char* str)
return *this;
}
+BTextWriter& 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.
*/
diff --git a/Boot/Sources/ProgramLoader.cxx b/Boot/Sources/ProgramLoader.cxx
index 88e2f14a..5938c2a0 100644
--- a/Boot/Sources/ProgramLoader.cxx
+++ b/Boot/Sources/ProgramLoader.cxx
@@ -8,7 +8,10 @@
#include <BootKit/Support.hxx>
#include <BootKit/BootKit.hxx>
-#include <KernelKit/PEF.hpp>
+#include <KernelKit/PEF.hxx>
+#include <KernelKit/PE.hxx>
+#include <KernelKit/MSDOS.hxx>
+#include <CFKit/LoaderUtils.hxx>
EXTERN_C
{
@@ -17,6 +20,8 @@ EXTERN_C
namespace Boot
{
+ EXTERN_C Int32 rt_jump_to_address(HEL::HandoverProc baseCode, HEL::HandoverInformationHeader* handover, Char* stackPointer);
+
ProgramLoader::ProgramLoader(VoidPtr blob)
: fBlob(blob), fStartAddress(nullptr)
{
@@ -34,9 +39,21 @@ namespace Boot
if (firstBytes[0] == kMagMz0 &&
firstBytes[1] == kMagMz1)
{
+ writer.Write("newosldr: MZ executable detected.\r");
+
+ ExecHeaderPtr hdrPtr = (ldr_find_exec_header(firstBytes));
+ ExecOptionalHeaderPtr optHdr = (ldr_find_opt_exec_header(firstBytes));
+
// Parse PE32+
- fStartAddress = nullptr;
- writer.Write("newosldr: MZ executable detected.\r");
+ fStartAddress = (VoidPtr)((UIntPtr)optHdr->mImageBase + optHdr->mBaseOfCode + optHdr->mAddressOfEntryPoint);
+ fStackPtr = new Char[optHdr->mSizeOfStackReserve];
+
+ writer.Write("newosldr: Major Linker: ").Write(optHdr->mMajorLinkerVersion).Write("\r");
+ writer.Write("newosldr: Minor Linker: ").Write(optHdr->mMinorLinkerVersion).Write("\r");
+ writer.Write("newosldr: Major Subsystem: ").Write(optHdr->mMajorSubsystemVersion).Write("\r");
+ writer.Write("newosldr: Minor Subsystem: ").Write(optHdr->mMinorSubsystemVersion).Write("\r");
+ writer.Write("newosldr: Magic: ").Write(optHdr->mMagic).Write("\r");
+ writer.Write("newosldr: StartAddress: ").Write((UIntPtr)optHdr->mImageBase + optHdr->mBaseOfCode + optHdr->mAddressOfEntryPoint).Write("\r");
}
else if (firstBytes[0] == kPefMagic[0] &&
firstBytes[1] == kPefMagic[1] &&
@@ -56,18 +73,34 @@ namespace Boot
/// @note handover header has to be valid!
Void ProgramLoader::Start(HEL::HandoverInformationHeader* handover)
{
- MUST_PASS(handover);
+ BTextWriter writer;
+
+ if (!handover)
+ {
+ writer.Write("newosldr: Exec format error.\r");
+ return;
+ }
- BTextWriter writer;
writer.Write("newosldr: Trying to run: ").Write(fBlobName).Write("\r");
- if (!fStartAddress)
+ if (!fStartAddress ||
+ ((Char*)fStartAddress)[0] == 0x0)
{
- writer.Write("newosldr: Exec format error.\r");
+ HEL::HandoverProc fn = [](HEL::HandoverInformationHeader* rcx) -> void {
+ 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.)");
+ };
+
+ rt_jump_to_address(fn, handover, fStackPtr);
+
return;
}
- ((HEL::HandoverProc)fStartAddress)(handover);
+ HEL::HandoverProc start = reinterpret_cast<HEL::HandoverProc>((UIntPtr)fStartAddress);
+
+ rt_jump_to_address(start, handover, fStackPtr);
}
const Char* ProgramLoader::GetName()