Update boot sequence and check PE/COFF image machine type compatibility

This commit is contained in:
Rafal Kupiec 2022-12-20 19:11:15 +01:00
parent edee9a8ec6
commit 7c38efc802
Signed by: belliash
GPG Key ID: 4E829243E0CFE6B4
3 changed files with 24 additions and 14 deletions

View File

@ -10,11 +10,11 @@
#define __XTDK_AMD64_KETYPES_H
/* Static Kernel-Mode Address start */
/* Static Kernel-Mode address start */
#define KSEG0_BASE 0xFFFFF80000000000
/* XTOS Kernel address base */
#define KERNEL_ADDRESS_BASE 0x0000000800000000
#define KSEG0_KERNEL_BASE 0x0000000800000000
/* XTOS Kernel stack size */
#define KERNEL_STACK_SIZE 8

View File

@ -14,7 +14,7 @@
#define KSEG0_BASE 0x80000000
/* XTOS Kernel address base */
#define KERNEL_ADDRESS_BASE 0x01800000
#define KSEG0_KERNEL_BASE 0x01800000
/* XTOS Kernel stack size */
#define KERNEL_STACK_SIZE 8

View File

@ -152,7 +152,7 @@ XtBootSystem(IN PXT_BOOT_PROTOCOL_PARAMETERS Parameters)
}
EFI_STATUS
XtpInitializeLoaderBlock(IN PKERNEL_INITIALIZATION_BLOCK *InitializationBlock)
XtpInitializeLoaderBlock(IN PLIST_ENTRY MemoryMappings, IN PVOID *VirtualAddress)
{
PKERNEL_INITIALIZATION_BLOCK LoaderBlock;
EFI_PHYSICAL_ADDRESS Address;
@ -173,7 +173,9 @@ XtpInitializeLoaderBlock(IN PKERNEL_INITIALIZATION_BLOCK *InitializationBlock)
LoaderBlock->LoaderInformation.DbgPrint = XtLdrProtocol->DbgPrint;
LoaderBlock->Version = INITIALIZATION_BLOCK_VERSION;
*InitializationBlock = LoaderBlock;
XtLdrProtocol->AddVirtualMemoryMapping(MemoryMappings, *VirtualAddress, (PVOID)LoaderBlock, BlockPages, LoaderSystemBlock);
*VirtualAddress += (UINT_PTR)(BlockPages * EFI_PAGE_SIZE);
return STATUS_EFI_SUCCESS;
}
@ -195,7 +197,7 @@ EFI_STATUS
XtpBootSequence(IN PEFI_FILE_HANDLE BootDir,
IN PXT_BOOT_PROTOCOL_PARAMETERS Parameters)
{
PKERNEL_INITIALIZATION_BLOCK KernelParameters, LoaderBlock;
PKERNEL_INITIALIZATION_BLOCK KernelParameters;
EFI_GUID LoadedImageGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID;
PPECOFF_IMAGE_CONTEXT ImageContext = NULL;
PEFI_LOADED_IMAGE_PROTOCOL ImageProtocol;
@ -207,9 +209,9 @@ XtpBootSequence(IN PEFI_FILE_HANDLE BootDir,
/* Initialize XTOS startup sequence */
XtLdrProtocol->DbgPrint(L"Initializing XTOS startup sequence\n");
/* Set virtual memory area for the kernel */
/* Set base virtual memory area for the kernel mappings */
VirtualMemoryArea = (PVOID)KSEG0_BASE;
VirtualAddress = (PVOID)(KSEG0_BASE + KERNEL_ADDRESS_BASE);
VirtualAddress = (PVOID)(KSEG0_BASE + KSEG0_KERNEL_BASE);
/* Initialize memory mapping linked list */
RtlInitializeListHead(&MemoryMappings);
@ -230,7 +232,7 @@ XtpBootSequence(IN PEFI_FILE_HANDLE BootDir,
return Status;
}
/* Add memory mapping for the kernel */
/* Add kernel image memory mapping */
Status = XtLdrProtocol->AddVirtualMemoryMapping(&MemoryMappings, ImageContext->VirtualAddress,
ImageContext->PhysicalAddress, ImageContext->ImagePages, 0);
if(Status != STATUS_EFI_SUCCESS)
@ -241,13 +243,12 @@ XtpBootSequence(IN PEFI_FILE_HANDLE BootDir,
/* Set next valid virtual address right after the kernel */
VirtualAddress += ImageContext->ImagePages * EFI_PAGE_SIZE;
/* Setup and map kernel initialization block */
Status = XtpInitializeLoaderBlock(&LoaderBlock);
XtLdrProtocol->AddVirtualMemoryMapping(&MemoryMappings, VirtualAddress, (PVOID)LoaderBlock, 1, LoaderSystemBlock);
/* Store virtual address of kernel initialization block for future kernel call */
KernelParameters = (PKERNEL_INITIALIZATION_BLOCK)VirtualAddress;
/* Setup and map kernel initialization block */
Status = XtpInitializeLoaderBlock(&MemoryMappings, &VirtualAddress);
/* Get kernel entry point */
XtPeCoffProtocol->GetEntryPoint(ImageContext, (PVOID)&KernelEntryPoint);
@ -295,7 +296,7 @@ XtpLoadModule(IN PEFI_FILE_HANDLE SystemDir,
OUT PPECOFF_IMAGE_CONTEXT *ImageContext)
{
PEFI_FILE_HANDLE ModuleHandle;
USHORT SubSystem;
USHORT MachineType, SubSystem;
EFI_STATUS Status;
/* Print debug message */
@ -322,6 +323,15 @@ XtpLoadModule(IN PEFI_FILE_HANDLE SystemDir,
/* Close image file */
ModuleHandle->Close(ModuleHandle);
/* Check PE/COFF image machine type compatibility */
XtPeCoffProtocol->GetMachineType(*ImageContext, &MachineType);
if(MachineType != _ARCH_IMAGE_MACHINE_TYPE)
{
/* Machine type mismatch */
XtLdrProtocol->DbgPrint(L"ERROR: Loaded incompatible PE/COFF image (machine type mismatch)\n");
return STATUS_EFI_INCOMPATIBLE_VERSION;
}
/* Check PE/COFF image subsystem */
XtPeCoffProtocol->GetSubSystem(*ImageContext, &SubSystem);
if(SubSystem != PECOFF_IMAGE_SUBSYSTEM_XT_NATIVE_APPLICATION &&