|
|
4210fa |
From 8e9d3af7b108cc76ce18017b3f58ad4b2e60989f Mon Sep 17 00:00:00 2001
|
|
|
4210fa |
From: Mohanraj S <mohanrajx.sakkaraiappan@intel.com>
|
|
|
4210fa |
Date: Tue, 27 Aug 2013 09:27:00 -0700
|
|
|
4210fa |
Subject: [PATCH 02/74] shim.c: Add support for hashing/relocation of 32-bit
|
|
|
4210fa |
binaries
|
|
|
4210fa |
|
|
|
4210fa |
Change-Id: Ib93305f7f1691d1b142567507df1058de62dde06
|
|
|
4210fa |
Signed-off-by: Andrew Boie <andrew.p.boie@intel.com>
|
|
|
4210fa |
---
|
|
|
4210fa |
shim.c | 72 +++++++++++++++++++++++++++++++++++++++++++++---------------------
|
|
|
4210fa |
1 file changed, 49 insertions(+), 23 deletions(-)
|
|
|
4210fa |
|
|
|
4210fa |
diff --git a/shim.c b/shim.c
|
|
|
4210fa |
index dcb36d0..a043779 100644
|
|
|
4210fa |
--- a/shim.c
|
|
|
4210fa |
+++ b/shim.c
|
|
|
4210fa |
@@ -126,7 +126,11 @@ static EFI_STATUS relocate_coff (PE_COFF_LOADER_IMAGE_CONTEXT *context,
|
|
|
4210fa |
int size = context->ImageSize;
|
|
|
4210fa |
void *ImageEnd = (char *)data + size;
|
|
|
4210fa |
|
|
|
4210fa |
+#if __LP64__
|
|
|
4210fa |
context->PEHdr->Pe32Plus.OptionalHeader.ImageBase = (UINT64)data;
|
|
|
4210fa |
+#else
|
|
|
4210fa |
+ context->PEHdr->Pe32.OptionalHeader.ImageBase = (UINT32)data;
|
|
|
4210fa |
+#endif
|
|
|
4210fa |
|
|
|
4210fa |
if (context->NumberOfRvaAndSizes <= EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
|
|
|
4210fa |
Print(L"Image has no relocation entry\n");
|
|
|
4210fa |
@@ -141,7 +145,7 @@ static EFI_STATUS relocate_coff (PE_COFF_LOADER_IMAGE_CONTEXT *context,
|
|
|
4210fa |
return EFI_UNSUPPORTED;
|
|
|
4210fa |
}
|
|
|
4210fa |
|
|
|
4210fa |
- Adjust = (UINT64)data - context->ImageAddress;
|
|
|
4210fa |
+ Adjust = (UINTN)data - context->ImageAddress;
|
|
|
4210fa |
|
|
|
4210fa |
if (Adjust == 0)
|
|
|
4210fa |
return EFI_SUCCESS;
|
|
|
4210fa |
@@ -549,9 +553,15 @@ static EFI_STATUS generate_hash (char *data, int datasize,
|
|
|
4210fa |
}
|
|
|
4210fa |
|
|
|
4210fa |
/* Hash end of certificate table to end of image header */
|
|
|
4210fa |
+#if __LP64__
|
|
|
4210fa |
hashbase = (char *) &context->PEHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
|
|
|
4210fa |
hashsize = context->PEHdr->Pe32Plus.OptionalHeader.SizeOfHeaders -
|
|
|
4210fa |
(int) ((char *) (&context->PEHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1]) - data);
|
|
|
4210fa |
+#else
|
|
|
4210fa |
+ hashbase = (char *) &context->PEHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1];
|
|
|
4210fa |
+ hashsize = context->PEHdr->Pe32.OptionalHeader.SizeOfHeaders -
|
|
|
4210fa |
+ (int) ((char *) (&context->PEHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY + 1]) - data);
|
|
|
4210fa |
+#endif
|
|
|
4210fa |
|
|
|
4210fa |
if (!(Sha256Update(sha256ctx, hashbase, hashsize)) ||
|
|
|
4210fa |
!(Sha1Update(sha1ctx, hashbase, hashsize))) {
|
|
|
4210fa |
@@ -561,7 +571,11 @@ static EFI_STATUS generate_hash (char *data, int datasize,
|
|
|
4210fa |
}
|
|
|
4210fa |
|
|
|
4210fa |
/* Sort sections */
|
|
|
4210fa |
+#if __LP64__
|
|
|
4210fa |
SumOfBytesHashed = context->PEHdr->Pe32Plus.OptionalHeader.SizeOfHeaders;
|
|
|
4210fa |
+#else
|
|
|
4210fa |
+ SumOfBytesHashed = context->PEHdr->Pe32.OptionalHeader.SizeOfHeaders;
|
|
|
4210fa |
+#endif
|
|
|
4210fa |
|
|
|
4210fa |
Section = (EFI_IMAGE_SECTION_HEADER *) (
|
|
|
4210fa |
(char *)context->PEHdr + sizeof (UINT32) +
|
|
|
4210fa |
@@ -628,7 +642,11 @@ static EFI_STATUS generate_hash (char *data, int datasize,
|
|
|
4210fa |
hashbase = data + SumOfBytesHashed;
|
|
|
4210fa |
hashsize = (unsigned int)(
|
|
|
4210fa |
size -
|
|
|
4210fa |
+#if __LP64__
|
|
|
4210fa |
context->PEHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size -
|
|
|
4210fa |
+#else
|
|
|
4210fa |
+ context->PEHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size -
|
|
|
4210fa |
+#endif
|
|
|
4210fa |
SumOfBytesHashed);
|
|
|
4210fa |
|
|
|
4210fa |
if (!(Sha256Update(sha256ctx, hashbase, hashsize)) ||
|
|
|
4210fa |
@@ -781,7 +799,7 @@ static EFI_STATUS read_header(void *data, unsigned int datasize,
|
|
|
4210fa |
{
|
|
|
4210fa |
EFI_IMAGE_DOS_HEADER *DosHdr = data;
|
|
|
4210fa |
EFI_IMAGE_OPTIONAL_HEADER_UNION *PEHdr = data;
|
|
|
4210fa |
- unsigned long HeaderWithoutDataDir, SectionHeaderOffset;
|
|
|
4210fa |
+ unsigned long HeaderWithoutDataDir, SectionHeaderOffset, OptHeaderSize;
|
|
|
4210fa |
|
|
|
4210fa |
if (datasize < sizeof(EFI_IMAGE_DOS_HEADER)) {
|
|
|
4210fa |
Print(L"Invalid image\n");
|
|
|
4210fa |
@@ -790,18 +808,28 @@ static EFI_STATUS read_header(void *data, unsigned int datasize,
|
|
|
4210fa |
|
|
|
4210fa |
if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE)
|
|
|
4210fa |
PEHdr = (EFI_IMAGE_OPTIONAL_HEADER_UNION *)((char *)data + DosHdr->e_lfanew);
|
|
|
4210fa |
+#if __LP64__
|
|
|
4210fa |
+ context->NumberOfRvaAndSizes = PEHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes;
|
|
|
4210fa |
+ context->SizeOfHeaders = PEHdr->Pe32Plus.OptionalHeader.SizeOfHeaders;
|
|
|
4210fa |
+ context->ImageSize = PEHdr->Pe32Plus.OptionalHeader.SizeOfImage;
|
|
|
4210fa |
+ OptHeaderSize = sizeof(EFI_IMAGE_OPTIONAL_HEADER64);
|
|
|
4210fa |
+#else
|
|
|
4210fa |
+ context->NumberOfRvaAndSizes = PEHdr->Pe32.OptionalHeader.NumberOfRvaAndSizes;
|
|
|
4210fa |
+ context->SizeOfHeaders = PEHdr->Pe32.OptionalHeader.SizeOfHeaders;
|
|
|
4210fa |
+ context->ImageSize = (UINT64)PEHdr->Pe32.OptionalHeader.SizeOfImage;
|
|
|
4210fa |
+ OptHeaderSize = sizeof(EFI_IMAGE_OPTIONAL_HEADER32);
|
|
|
4210fa |
+#endif
|
|
|
4210fa |
+ context->NumberOfSections = PEHdr->Pe32.FileHeader.NumberOfSections;
|
|
|
4210fa |
|
|
|
4210fa |
- if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES
|
|
|
4210fa |
- < PEHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes) {
|
|
|
4210fa |
+ if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES < context->NumberOfRvaAndSizes) {
|
|
|
4210fa |
Print(L"Image header too small\n");
|
|
|
4210fa |
return EFI_UNSUPPORTED;
|
|
|
4210fa |
}
|
|
|
4210fa |
|
|
|
4210fa |
- HeaderWithoutDataDir = sizeof (EFI_IMAGE_OPTIONAL_HEADER64)
|
|
|
4210fa |
+ HeaderWithoutDataDir = OptHeaderSize
|
|
|
4210fa |
- sizeof (EFI_IMAGE_DATA_DIRECTORY) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;
|
|
|
4210fa |
- if (((UINT32)PEHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader - HeaderWithoutDataDir) !=
|
|
|
4210fa |
- PEHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes
|
|
|
4210fa |
- * sizeof (EFI_IMAGE_DATA_DIRECTORY)) {
|
|
|
4210fa |
+ if (((UINT32)PEHdr->Pe32.FileHeader.SizeOfOptionalHeader - HeaderWithoutDataDir) !=
|
|
|
4210fa |
+ context->NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY)) {
|
|
|
4210fa |
Print(L"Image header overflows data directory\n");
|
|
|
4210fa |
return EFI_UNSUPPORTED;
|
|
|
4210fa |
}
|
|
|
4210fa |
@@ -809,15 +837,15 @@ static EFI_STATUS read_header(void *data, unsigned int datasize,
|
|
|
4210fa |
SectionHeaderOffset = DosHdr->e_lfanew
|
|
|
4210fa |
+ sizeof (UINT32)
|
|
|
4210fa |
+ sizeof (EFI_IMAGE_FILE_HEADER)
|
|
|
4210fa |
- + PEHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader;
|
|
|
4210fa |
- if ((PEHdr->Pe32Plus.OptionalHeader.SizeOfImage - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER
|
|
|
4210fa |
- <= PEHdr->Pe32Plus.FileHeader.NumberOfSections) {
|
|
|
4210fa |
+ + PEHdr->Pe32.FileHeader.SizeOfOptionalHeader;
|
|
|
4210fa |
+ if (((UINT32)context->ImageSize - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER
|
|
|
4210fa |
+ <= context->NumberOfSections) {
|
|
|
4210fa |
Print(L"Image sections overflow image size\n");
|
|
|
4210fa |
return EFI_UNSUPPORTED;
|
|
|
4210fa |
}
|
|
|
4210fa |
|
|
|
4210fa |
- if ((PEHdr->Pe32Plus.OptionalHeader.SizeOfHeaders - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER
|
|
|
4210fa |
- < (UINT32)PEHdr->Pe32Plus.FileHeader.NumberOfSections) {
|
|
|
4210fa |
+ if ((context->SizeOfHeaders - SectionHeaderOffset) / EFI_IMAGE_SIZEOF_SECTION_HEADER
|
|
|
4210fa |
+ < (UINT32)context->NumberOfSections) {
|
|
|
4210fa |
Print(L"Image sections overflow section headers\n");
|
|
|
4210fa |
return EFI_UNSUPPORTED;
|
|
|
4210fa |
}
|
|
|
4210fa |
@@ -837,21 +865,19 @@ static EFI_STATUS read_header(void *data, unsigned int datasize,
|
|
|
4210fa |
return EFI_UNSUPPORTED;
|
|
|
4210fa |
}
|
|
|
4210fa |
|
|
|
4210fa |
- if (PEHdr->Pe32.OptionalHeader.Magic != EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
|
|
|
4210fa |
- Print(L"Only 64-bit images supported\n");
|
|
|
4210fa |
- return EFI_UNSUPPORTED;
|
|
|
4210fa |
- }
|
|
|
4210fa |
-
|
|
|
4210fa |
context->PEHdr = PEHdr;
|
|
|
4210fa |
+#if __LP64__
|
|
|
4210fa |
context->ImageAddress = PEHdr->Pe32Plus.OptionalHeader.ImageBase;
|
|
|
4210fa |
- context->ImageSize = (UINT64)PEHdr->Pe32Plus.OptionalHeader.SizeOfImage;
|
|
|
4210fa |
- context->SizeOfHeaders = PEHdr->Pe32Plus.OptionalHeader.SizeOfHeaders;
|
|
|
4210fa |
context->EntryPoint = PEHdr->Pe32Plus.OptionalHeader.AddressOfEntryPoint;
|
|
|
4210fa |
context->RelocDir = &PEHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
|
|
|
4210fa |
- context->NumberOfRvaAndSizes = PEHdr->Pe32Plus.OptionalHeader.NumberOfRvaAndSizes;
|
|
|
4210fa |
- context->NumberOfSections = PEHdr->Pe32.FileHeader.NumberOfSections;
|
|
|
4210fa |
- context->FirstSection = (EFI_IMAGE_SECTION_HEADER *)((char *)PEHdr + PEHdr->Pe32.FileHeader.SizeOfOptionalHeader + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER));
|
|
|
4210fa |
context->SecDir = (EFI_IMAGE_DATA_DIRECTORY *) &PEHdr->Pe32Plus.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
|
|
|
4210fa |
+#else
|
|
|
4210fa |
+ context->ImageAddress = PEHdr->Pe32.OptionalHeader.ImageBase;
|
|
|
4210fa |
+ context->EntryPoint = PEHdr->Pe32.OptionalHeader.AddressOfEntryPoint;
|
|
|
4210fa |
+ context->RelocDir = &PEHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
|
|
|
4210fa |
+ context->SecDir = (EFI_IMAGE_DATA_DIRECTORY *) &PEHdr->Pe32.OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY];
|
|
|
4210fa |
+#endif
|
|
|
4210fa |
+ context->FirstSection = (EFI_IMAGE_SECTION_HEADER *)((char *)PEHdr + PEHdr->Pe32.FileHeader.SizeOfOptionalHeader + sizeof(UINT32) + sizeof(EFI_IMAGE_FILE_HEADER));
|
|
|
4210fa |
|
|
|
4210fa |
if (context->ImageSize < context->SizeOfHeaders) {
|
|
|
4210fa |
Print(L"Invalid image\n");
|
|
|
4210fa |
--
|
|
|
4210fa |
1.9.3
|
|
|
4210fa |
|