From 03a0fee13445aeb95f01d64409e5304b8e97b31c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 26 Apr 2024 15:48:09 +0200 Subject: MHR-16: Fix NewBoot bug due to terrible code. Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 73 ++++++++++++++------------- Private/NewBoot/Source/makefile | 10 +++- 2 files changed, 47 insertions(+), 36 deletions(-) (limited to 'Private/NewBoot/Source') diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 68762089..558bf001 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -70,31 +70,15 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, .Write(SystemTable->FirmwareVendor) .Write(L"\r\n"); - UInt32 MapKey = 0; - UInt32* SizePtr = nullptr; - EfiMemoryDescriptor* Descriptor = nullptr; - UInt32 SzDesc = 0; - UInt32 RevDesc = 0; - - if (BS->AllocatePool(EfiLoaderData, sizeof(UInt32), (VoidPtr*)&SizePtr) != - kEfiOk) { - EFI::ThrowError(L"Bad-Alloc", L"New Boot ran out of memory!"); - } - - /**** - * - * Load kernel into memory. - * - */ + UInt32* MapKey = new UInt32(); + UInt32* SizePtr = new UInt32(); + EfiMemoryDescriptor* Descriptor = new EfiMemoryDescriptor(); + UInt32* SzDesc = new UInt32(); + UInt32* RevDesc = new UInt32(); *SizePtr = sizeof(EfiMemoryDescriptor); - if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), - (VoidPtr*)&Descriptor) != kEfiOk) { - EFI::ThrowError(L"Bad-Alloc", L"New Boot ran out of memory!"); - } - - HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; + HEL::HandoverInformationHeader* handoverHdrPtr = new HEL::HandoverInformationHeader(); for (SizeT indexVT = 0; indexVT < SystemTable->NumberOfTableEntries; ++indexVT) { @@ -141,20 +125,21 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, ToolboxClearRsrc(); - BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader), - (VoidPtr*)&handoverHdrPtr); + EfiPhysicalAddress* whereAddress = + reinterpret_cast(kBootVirtualAddress); - handoverHdrPtr->f_PhysicalStart = 0; - handoverHdrPtr->f_PhysicalSize = 0; + BS->GetMemoryMap(SizePtr, Descriptor, MapKey, SzDesc, RevDesc); + + handoverHdrPtr->f_PhysicalStart = (VoidPtr)Descriptor->PhysicalStart; + + handoverHdrPtr->f_FirmwareSpecific[0] = Descriptor->Attribute; + handoverHdrPtr->f_FirmwareSpecific[1] = Descriptor->Kind; - EfiPhysicalAddress* whereAddress = - reinterpret_cast(kBootVirtualAddress); BS->AllocatePages(EfiAllocateType::AllocateAnyPages, EfiMemoryType::EfiConventionalMemory, 1, whereAddress); - handoverHdrPtr->f_VirtualStart = reinterpret_cast(whereAddress); - + handoverHdrPtr->f_VirtualStart = (VoidPtr)Descriptor->VirtualStart; handoverHdrPtr->f_VirtualSize = Descriptor->NumberOfPages; /* # of pages */ handoverHdrPtr->f_FirmwareVendorLen = BStrLen(SystemTable->FirmwareVendor); @@ -209,7 +194,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, memcpy(bootDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); bootDesc.fBlobSz = BootDeviceATA::kSectorSize; - bootDesc.fBlob = new Char[rootDesc.fBlobSz]; + bootDesc.fBlob = new Char[bootDesc.fBlobSz]; memset(bootDesc.fBlob, 0, bootDesc.fBlobSz); @@ -217,12 +202,32 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, strlen(kMachineModel " startup folder.")); rootDesc.fNext = &bootDesc; - rootDesc.fNext->fPrev = &rootDesc; + rootDesc.fNext->fPrev = nullptr; + + BDiskFormatFactory::BFileDescriptor appDesc{0}; + + appDesc.fKind = kNewFSCatalogKindDir; + + memcpy(appDesc.fFileName, "/Applications", strlen("/Applications")); + memcpy(appDesc.fForkName, kNewFSResourceFork, strlen(kNewFSResourceFork)); + + appDesc.fBlobSz = BootDeviceATA::kSectorSize; + appDesc.fBlob = new Char[appDesc.fBlobSz]; + + memset(appDesc.fBlob, 0, appDesc.fBlobSz); + + memcpy(appDesc.fBlob, kMachineModel " applications folder.", + strlen(kMachineModel " applications folder.")); + + appDesc.fNext = nullptr; + appDesc.fNext->fPrev = &bootDesc; + + bootDesc.fNext = &appDesc; - diskFormatter.Format(kMachineModel, &rootDesc, 2); + diskFormatter.Format(kMachineModel, &rootDesc, 3); } - EFI::ExitBootServices(MapKey, ImageHandle); + EFI::ExitBootServices(*MapKey, ImageHandle); hal_init_platform(kHandoverHeader); diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 8306f844..f3a292c5 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -28,7 +28,13 @@ EMU_FLAGS=-net none -smp 4 -m 8G -M q35 -bios OVMF.fd -device piix3-ide,id=ide - LD_FLAGS=-e Main --subsystem=10 +ifeq ($(NEWS_STANDLONE), ) OBJ=*.o ../../Objects/*.obj +else +RESCMD=$(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o +STANDALONE_MACRO=-D__STANDALONE__ +OBJ=*.o +endif REM=rm REM_FLAG=-f @@ -60,8 +66,8 @@ endif .PHONY: compile-amd64 compile-amd64: - # $(WINDRES) BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o - $(CC_GNU) $(NEWOS_MODEL) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard HEL/AMD64/*.S) $(wildcard *.cxx) + $(RESCMD) + $(CC_GNU) $(NEWOS_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) $(wildcard HEL/AMD64/*.cxx) $(wildcard HEL/AMD64/*.S) $(wildcard *.cxx) .PHONY: run-efi-amd64 run-efi-amd64: -- cgit v1.2.3