From eb5998c58eadbfc5ffd03a7c4491be1510b90ec8 Mon Sep 17 00:00:00 2001 From: belliash Date: Tue, 20 Dec 2022 15:37:13 +0100 Subject: [PATCH] Add a way to check PE/COFF image machine type --- sdk/xtdk/xttarget.h | 2 ++ xtldr/includes/blmod.h | 2 ++ xtldr/modules/pecoff/includes/pecoff.h | 4 ++++ xtldr/modules/pecoff/pecoff.c | 14 ++++++++++++++ 4 files changed, 22 insertions(+) diff --git a/sdk/xtdk/xttarget.h b/sdk/xtdk/xttarget.h index b686c43..e8b7bc7 100644 --- a/sdk/xtdk/xttarget.h +++ b/sdk/xtdk/xttarget.h @@ -19,11 +19,13 @@ #if defined(__i386__) || defined(__i686__) #define _ARCH i686 #define _ARCH_I686 1 + #define _ARCH_IMAGE_MACHINE_TYPE 0x014C #define _XT32 1 #define EFI_ERROR_MASK 0x80000000 #elif defined(__amd64__) || defined(__x86_64__) #define _ARCH amd64 #define _ARCH_AMD64 1 + #define _ARCH_IMAGE_MACHINE_TYPE 0x8664 #define _XT64 1 #define EFI_ERROR_MASK 0x8000000000000000 #else diff --git a/xtldr/includes/blmod.h b/xtldr/includes/blmod.h index cc45a30..fddb05a 100644 --- a/xtldr/includes/blmod.h +++ b/xtldr/includes/blmod.h @@ -22,6 +22,7 @@ typedef struct _XT_PECOFFF_IMAGE_PROTOCOL XT_PECOFF_IMAGE_PROTOCOL, *PXT_PECOFF_ /* Pointers to the routines provided by the modules */ typedef EFI_STATUS (*PXT_BOOTPROTO_BOOT_SYSTEM)(IN PXT_BOOT_PROTOCOL_PARAMETERS Parameters); typedef EFI_STATUS (*PXT_PECOFF_GET_ENTRY_POINT)(IN PPECOFF_IMAGE_CONTEXT Image, OUT PVOID *EntryPoint); +typedef EFI_STATUS (*PXT_PECOFF_GET_MACHINE_TYPE)(IN PPECOFF_IMAGE_CONTEXT Image, OUT PUSHORT MachineType); typedef EFI_STATUS (*PXT_PECOFF_GET_SUBSYSTEM)(IN PPECOFF_IMAGE_CONTEXT Image, OUT PUSHORT SubSystem); typedef EFI_STATUS (*PXT_PECOFF_LOAD_IMAGE)(IN PEFI_FILE_HANDLE FileHandle, IN LOADER_MEMORY_TYPE MemoryType, IN PVOID VirtualAddress, OUT PPECOFF_IMAGE_CONTEXT *Image); @@ -50,6 +51,7 @@ typedef struct _XT_BOOT_PROTOCOL_PARAMETERS typedef struct _XT_PECOFFF_IMAGE_PROTOCOL { PXT_PECOFF_GET_ENTRY_POINT GetEntryPoint; + PXT_PECOFF_GET_MACHINE_TYPE GetMachineType; PXT_PECOFF_GET_SUBSYSTEM GetSubSystem; PXT_PECOFF_LOAD_IMAGE Load; PXT_PECOFF_RELOCATE_IMAGE Relocate; diff --git a/xtldr/modules/pecoff/includes/pecoff.h b/xtldr/modules/pecoff/includes/pecoff.h index 1c61de1..dfb05ab 100644 --- a/xtldr/modules/pecoff/includes/pecoff.h +++ b/xtldr/modules/pecoff/includes/pecoff.h @@ -17,6 +17,10 @@ EFI_STATUS PeGetEntryPoint(IN PPECOFF_IMAGE_CONTEXT Image, OUT PVOID *EntryPoint); +EFI_STATUS +PeGetMachineType(IN PPECOFF_IMAGE_CONTEXT Image, + OUT PUSHORT MachineType); + EFI_STATUS PeGetSubSystem(IN PPECOFF_IMAGE_CONTEXT Image, OUT PUSHORT SubSystem); diff --git a/xtldr/modules/pecoff/pecoff.c b/xtldr/modules/pecoff/pecoff.c index 6e085df..909dd16 100644 --- a/xtldr/modules/pecoff/pecoff.c +++ b/xtldr/modules/pecoff/pecoff.c @@ -50,6 +50,19 @@ PeGetEntryPoint(IN PPECOFF_IMAGE_CONTEXT Image, return STATUS_EFI_SUCCESS; } +EFI_STATUS +PeGetMachineType(IN PPECOFF_IMAGE_CONTEXT Image, + OUT PUSHORT MachineType) +{ + if(!Image || !Image->PeHeader) + { + return STATUS_EFI_INVALID_PARAMETER; + } + + *MachineType = Image->PeHeader->FileHeader.Machine; + return STATUS_EFI_SUCCESS; +} + /** * Returns an information about subsystem that is required to run PE/COFF image. * @@ -548,6 +561,7 @@ BlXtLdrModuleMain(IN EFI_HANDLE ImageHandle, /* Set routines available via PE/COFF image protocol */ XtPeCoffProtocol.GetEntryPoint = PeGetEntryPoint; + XtPeCoffProtocol.GetMachineType = PeGetMachineType; XtPeCoffProtocol.GetSubSystem = PeGetSubSystem; XtPeCoffProtocol.Load = PeLoadImage; XtPeCoffProtocol.Relocate = PeRelocateImage;