From 53cd4e00526f2086bf70d4a1e9359d8e2cd61cda Mon Sep 17 00:00:00 2001 From: Rafal Kupiec Date: Wed, 10 Jan 2024 16:05:36 +0100 Subject: [PATCH] Cleanup PE/COFF related structures --- sdk/xtdk/xtimage.h | 201 ++++++++++++++++---------------- xtldr/modules/pecoff_o/pecoff.c | 4 +- xtldr/protocol.c | 2 +- 3 files changed, 104 insertions(+), 103 deletions(-) diff --git a/sdk/xtdk/xtimage.h b/sdk/xtdk/xtimage.h index 2f3ad8f4..047c3526 100644 --- a/sdk/xtdk/xtimage.h +++ b/sdk/xtdk/xtimage.h @@ -238,116 +238,117 @@ typedef struct _PECOFF_IMAGE_FILE_HEADER /* DOS PE image header */ typedef struct _PECOFF_IMAGE_DOS_HEADER { - USHORT e_magic; - USHORT e_cblp; - USHORT e_cp; - USHORT e_crlc; - USHORT e_cparhdr; - USHORT e_minalloc; - USHORT e_maxalloc; - USHORT e_ss; - USHORT e_sp; - USHORT e_csum; - USHORT e_ip; - USHORT e_cs; - USHORT e_lfarlc; - USHORT e_ovno; - USHORT e_res[4]; - USHORT e_oemid; - USHORT e_oeminfo; - USHORT e_res2[10]; - LONG e_lfanew; + USHORT Magic; + USHORT LastPageBytes; + USHORT ImagePages; + USHORT Relocations; + USHORT HeaderParagraphs; + USHORT MinExtraParagraphs; + USHORT MaxExtraParagraphs; + USHORT InitialSS; + USHORT InitialSP; + USHORT Checksum; + USHORT InitialIP; + USHORT InitialCS; + USHORT RelocationTableOffset; + USHORT OverlayNumber; + USHORT Reserved1[4]; + USHORT OemIdentification; + USHORT OemInformation; + USHORT Reserved2[10]; + LONG PeHeaderOffset; } PECOFF_IMAGE_DOS_HEADER, *PPECOFF_IMAGE_DOS_HEADER; /* OS/2 PE image header */ typedef struct _PECOFF_IMAGE_OS2_HEADER { - USHORT ne_magic; - CHAR ne_ver; - CHAR ne_rev; - USHORT ne_enttab; - USHORT ne_cbenttab; - LONG ne_crc; - USHORT ne_flags; - USHORT ne_autodata; - USHORT ne_heap; - USHORT ne_stack; - LONG ne_csip; - LONG ne_sssp; - USHORT ne_cseg; - USHORT ne_cmod; - USHORT ne_cbnrestab; - USHORT ne_segtab; - USHORT ne_rsrctab; - USHORT ne_restab; - USHORT ne_modtab; - USHORT ne_imptab; - LONG ne_nrestab; - USHORT ne_cmovent; - USHORT ne_align; - USHORT ne_cres; - UCHAR ne_exetyp; - UCHAR ne_flagsothers; - USHORT ne_pretthunks; - USHORT ne_psegrefbytes; - USHORT ne_swaparea; - USHORT ne_expver; + USHORT Magic; + CHAR MajorVersion; + CHAR MinorVersion; + USHORT EntryTableOffset; + USHORT EntryTableLength; + LONG FileLoadCRC; + UCHAR ProgFlags; + UCHAR ApplFlags; + USHORT AutoDataSegIndex; + USHORT InitHeapSize; + USHORT InitStackSize; + LONG EntryPoint; + LONG InitStack; + USHORT SegCount; + USHORT ModRefs; + USHORT NoResNamesTabSiz; + USHORT SegTableOffset; + USHORT ResTableOffset; + USHORT ResidNamTable; + USHORT ModRefTable; + USHORT ImportNameTable; + LONG NonResTabableOffset; + USHORT MovEntryCount; + USHORT ImageAlignment; + USHORT ResTableEntries; + UCHAR ImageType; + UCHAR ImageFlags; + USHORT ReturnThunkOffset; + USHORT ReferenceThunksOffset; + USHORT SwapArea; + USHORT WindowsVersion; } PECOFF_IMAGE_OS2_HEADER, *PPECOFF_IMAGE_OS2_HEADER; /* Windows VXD PE image header */ typedef struct _PECOFF_IMAGE_VXD_HEADER { - USHORT e32_magic; - UCHAR e32_border; - UCHAR e32_worder; - ULONG e32_level; - USHORT e32_cpu; - USHORT e32_os; - ULONG e32_ver; - ULONG e32_mflags; - ULONG e32_mpages; - ULONG e32_startobj; - ULONG e32_eip; - ULONG e32_stackobj; - ULONG e32_esp; - ULONG e32_pagesize; - ULONG e32_lastpagesize; - ULONG e32_fixupsize; - ULONG e32_fixupsum; - ULONG e32_ldrsize; - ULONG e32_ldrsum; - ULONG e32_objtab; - ULONG e32_objcnt; - ULONG e32_objmap; - ULONG e32_itermap; - ULONG e32_rsrctab; - ULONG e32_rsrccnt; - ULONG e32_restab; - ULONG e32_enttab; - ULONG e32_dirtab; - ULONG e32_dircnt; - ULONG e32_fpagetab; - ULONG e32_frectab; - ULONG e32_impmod; - ULONG e32_impmodcnt; - ULONG e32_impproc; - ULONG e32_pagesum; - ULONG e32_datapage; - ULONG e32_preload; - ULONG e32_nrestab; - ULONG e32_cbnrestab; - ULONG e32_nressum; - ULONG e32_autodata; - ULONG e32_debuginfo; - ULONG e32_debuglen; - ULONG e32_instpreload; - ULONG e32_instdemand; - ULONG e32_heapsize; - UCHAR e32_res3[12]; - ULONG e32_winresoff; - ULONG e32_winreslen; - USHORT e32_devid; - USHORT e32_ddkver; + USHORT Magic; + UCHAR BytesOrder; + UCHAR WordsOrder; + ULONG FormatLevel; + USHORT CpuType; + USHORT OSType; + ULONG ModuleVersion; + ULONG ModuleFlags; + ULONG ModulePages; + ULONG EntryPoint; + ULONG Eip; + ULONG Stack; + ULONG Esp; + ULONG PageSize; + ULONG LastPageSize; + ULONG FixupSectionSize; + ULONG FixupSectionChecksum; + ULONG LoaderSectionSize; + ULONG LoaderSectionChecksum; + ULONG ObjectTableOffset; + ULONG NumberOfObjects; + ULONG ObjectPageMapOffset; + ULONG ObjectIterMapOffset; + ULONG ResTableOffset; + ULONG NumberOfResources; + ULONG ResidentTableOffset; + ULONG EntryTableOffset; + ULONG DirectiveTableOffset; + ULONG NumberOfDirectives; + ULONG FixupPageTableOffset; + ULONG FixupRecordTableOffset; + ULONG ImportTableOffset; + ULONG NumberOfImports; + ULONG ImportProcTableOffset; + ULONG PageChecksumTableOffset; + ULONG DataPagesOffset; + ULONG NumberOfPreloadPages; + ULONG NonResidentTableOffset; + ULONG NonResidentTableSize; + ULONG NonResidentTableChecksum; + ULONG AutoDataObject; + ULONG DebugInfoOffset; + ULONG DebugInfoLength; + ULONG PreLoadSectionPages; + ULONG DemandLoadSectionPages; + ULONG HeapSize; + UCHAR Reserved[12]; + ULONG WinResOffset; + ULONG WinResLength; + USHORT DeviceId; + USHORT DDKVersion; } PECOFF_IMAGE_VXD_HEADER, *PPECOFF_IMAGE_VXD_HEADER; /* PE/COFF section header */ diff --git a/xtldr/modules/pecoff_o/pecoff.c b/xtldr/modules/pecoff_o/pecoff.c index 36ff2f45..2a4baed4 100644 --- a/xtldr/modules/pecoff_o/pecoff.c +++ b/xtldr/modules/pecoff_o/pecoff.c @@ -231,7 +231,7 @@ PeLoadImage(IN PEFI_FILE_HANDLE FileHandle, /* Extract DOS and PE headers */ ImageData->DosHeader = (PPECOFF_IMAGE_DOS_HEADER)Data; - ImageData->PeHeader = (PPECOFF_IMAGE_PE_HEADER)((PUINT8)Data + ImageData->DosHeader->e_lfanew); + ImageData->PeHeader = (PPECOFF_IMAGE_PE_HEADER)((PUINT8)Data + ImageData->DosHeader->PeHeaderOffset); /* Validate headers */ Status = PepValidateImageHeaders(ImageData->DosHeader, ImageData->PeHeader, ImageData->FileSize); @@ -541,7 +541,7 @@ PepValidateImageHeaders(IN PPECOFF_IMAGE_DOS_HEADER DosHeader, } /* Validate DOS header */ - if(DosHeader->e_magic != PECOFF_IMAGE_DOS_SIGNATURE) + if(DosHeader->Magic != PECOFF_IMAGE_DOS_SIGNATURE) { XtLdrProtocol->Debug.Print(L"WARNING: Invalid DOS signature found\n"); return STATUS_EFI_INCOMPATIBLE_VERSION; diff --git a/xtldr/protocol.c b/xtldr/protocol.c index 5cefb67d..10622d9a 100644 --- a/xtldr/protocol.c +++ b/xtldr/protocol.c @@ -223,7 +223,7 @@ BlLoadModule(IN PWCHAR ModuleName) /* Setup PE/COFF EFI image headers */ DosHeader = (PPECOFF_IMAGE_DOS_HEADER)ModuleData; - PeHeader = (PPECOFF_IMAGE_PE_HEADER)(ModuleData + DosHeader->e_lfanew); + PeHeader = (PPECOFF_IMAGE_PE_HEADER)(ModuleData + DosHeader->PeHeaderOffset); SectionHeader = (PPECOFF_IMAGE_SECTION_HEADER)((PUCHAR)&PeHeader->OptionalHeader + PeHeader->FileHeader.SizeOfOptionalHeader);