From 17bd847ebc85f4f9e62f510563482939a98f0c8e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 6 Jun 2025 05:44:34 +0200 Subject: feat: kernel: Start enforcing `User`s validation when issuing OS calls. refactor: PE: remove BaseOfData, as we are only relying on PE32+ now. Signed-off-by: Amlal El Mahrouss --- dev/boot/src/BootThread.cc | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'dev/boot/src/BootThread.cc') diff --git a/dev/boot/src/BootThread.cc b/dev/boot/src/BootThread.cc index b502b52e..5566e3e0 100644 --- a/dev/boot/src/BootThread.cc +++ b/dev/boot/src/BootThread.cc @@ -15,6 +15,8 @@ #include #include +#define kBootThreadSz mib_cast(16) + /// @brief External boot services symbol. EXTERN EfiBootServices* BS; @@ -67,11 +69,10 @@ BootThread::BootThread(VoidPtr blob) : fStartAddress(nullptr), fBlob(blob) { writer.Write("BootZ: Magic: ").Write(header_ptr->Signature).Write("\r"); EfiPhysicalAddress loadStartAddress = opt_header_ptr->ImageBase; - loadStartAddress += opt_header_ptr->BaseOfData; - writer.Write("BootZ: Image base: ").Write(loadStartAddress).Write("\r"); + writer.Write("BootZ: Image-Base: ").Write(loadStartAddress).Write("\r"); - fStack = new UInt8[mib_cast(16)]; + fStack = new UInt8[kBootThreadSz]; if (!fStack) { writer.Write("BootZ: Unable to allocate stack.\r"); @@ -172,13 +173,13 @@ Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { if (own_stack) { writer.Write("BootZ: Using it's own stack.\r"); - writer.Write("BootZ: Stack address: ").Write((UIntPtr) &fStack[mib_cast(16) - 1]).Write("\r"); - writer.Write("BootZ: Stack size: ").Write(mib_cast(16)).Write("\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[mib_cast(16) - 1]; - fHandover->f_StackSz = mib_cast(16); + fHandover->f_StackTop = &fStack[kBootThreadSz - 1]; + fHandover->f_StackSz = kBootThreadSz; - auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[mib_cast(16) - 1]); + auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[kBootThreadSz - 1]); // we don't need the stack anymore. @@ -187,7 +188,7 @@ Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { return ret; } else { - writer.Write("BootZ: Using Bootloader's stack.\r"); + writer.Write("BootZ: Using the bootloader's stack.\r"); return reinterpret_cast(fStartAddress)(fHandover); } -- cgit v1.2.3 From 98b47e78991d4cc7712e98e6dc1496aa0f0fcf38 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 10 Jun 2025 09:08:35 +0200 Subject: feat: boot: Better checks in BootThread, and cleanup BootEFI's init on AArch64. Signed-off-by: Amlal El Mahrouss --- dev/boot/src/BootThread.cc | 6 ++-- dev/boot/src/HEL/ARM64/BootEFI.cc | 65 ++++++++++++++++++--------------------- 2 files changed, 33 insertions(+), 38 deletions(-) (limited to 'dev/boot/src/BootThread.cc') diff --git a/dev/boot/src/BootThread.cc b/dev/boot/src/BootThread.cc index 5566e3e0..c477d659 100644 --- a/dev/boot/src/BootThread.cc +++ b/dev/boot/src/BootThread.cc @@ -156,16 +156,16 @@ BootThread::BootThread(VoidPtr blob) : fStartAddress(nullptr), fBlob(blob) { /// @note handover header has to be valid! Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { - fHandover = handover; - if (!fStartAddress) { return kEfiFail; } - if (!fHandover) { + if (!handover) { return kEfiFail; } + fHandover = handover; + BootTextWriter writer; writer.Write("BootZ: Starting: ").Write(fBlobName).Write("\r"); diff --git a/dev/boot/src/HEL/ARM64/BootEFI.cc b/dev/boot/src/HEL/ARM64/BootEFI.cc index 0421d1cf..44c6a778 100644 --- a/dev/boot/src/HEL/ARM64/BootEFI.cc +++ b/dev/boot/src/HEL/ARM64/BootEFI.cc @@ -71,12 +71,10 @@ EXTERN EfiBootServices* BS; EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { fw_init_efi(sys_table); ///! Init the EFI library. - HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); - - UInt32 map_key = 0; + kHandoverHeader = new HEL::BootInfoHeader(); #ifdef ZBA_USE_FB - if (!boot_init_fb()) return 1; ///! Init the GOP. + if (!boot_init_fb()) return kEfiFail; ///! Init the GOP. for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { Char* vendor_table = @@ -86,7 +84,7 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa 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; + kHandoverHeader->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; break; } } @@ -95,12 +93,12 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa // 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; + 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 // ------------------------------------------- // @@ -111,19 +109,16 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa EfiMpServicesProtocol* mp = nullptr; BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); - - // Assign to global 'kHandoverHeader'. - kHandoverHeader = handover_hdr; + kHandoverHeader->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); 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; + kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; } else { - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; + kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = NO; } //-------------------------------------------------------------// @@ -132,12 +127,12 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa Boot::BootTextWriter writer; - 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. */ Int32 trials = 5 * 10000000; - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, - &handover_hdr->f_BitMapStart) != kEfiOk) { + while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize, + &kHandoverHeader->f_BitMapStart) != kEfiOk) { --trials; if (!trials) { @@ -145,10 +140,10 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa trials = 3 * 10000000; - handover_hdr->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ + kHandoverHeader->f_BitMapSize = kHandoverBitMapSz / 2; /* Size of bitmap in bytes. */ - while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, - &handover_hdr->f_BitMapStart) != kEfiOk) { + while (BS->AllocatePool(EfiLoaderData, kHandoverHeader->f_BitMapSize, + &kHandoverHeader->f_BitMapStart) != kEfiOk) { --trials; if (!trials) { @@ -163,20 +158,20 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa // null these fields, to avoid being reused later. // ------------------------------------------ // - handover_hdr->f_FirmwareCustomTables[0] = nullptr; - handover_hdr->f_FirmwareCustomTables[1] = nullptr; + kHandoverHeader->f_FirmwareCustomTables[0] = nullptr; + kHandoverHeader->f_FirmwareCustomTables[1] = nullptr; - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - handover_hdr->f_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; + kHandoverHeader->f_Magic = kHandoverMagic; + kHandoverHeader->f_Version = kHandoverVersion; // Provide fimware vendor name. - Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, - handover_hdr->f_FirmwareVendorLen); + Boot::BCopyMem(kHandoverHeader->f_FirmwareVendorName, sys_table->FirmwareVendor, + kHandoverHeader->f_FirmwareVendorLen); - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); Boot::BootFileReader reader_kernel(L"ne_kernel", image_handle); @@ -190,10 +185,10 @@ EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTa 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(); + kHandoverHeader->f_KernelImage = reader_kernel.Blob(); + kHandoverHeader->f_KernelSz = reader_kernel.Size(); - kernel_thread.Start(handover_hdr, YES); + kernel_thread.Start(kHandoverHeader, YES); } CANT_REACH(); -- cgit v1.2.3