Enhance the check when ImageRead function return.

Signed-off-by: Eric Dong <eric.dong@intel.com>
Reviewed-by: Ye Ting  <ting.ye@intel.com>

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@13657 6f19259b-4bc3-4df7-8a09-765794883524
This commit is contained in:
ydong10 2012-08-21 08:38:58 +00:00
parent 05bf16e04d
commit 3acb198567
2 changed files with 32 additions and 18 deletions

View File

@ -5,7 +5,7 @@
IA-32, x86, IPF, and EBC processor types. The library functions are memory-based IA-32, x86, IPF, and EBC processor types. The library functions are memory-based
and can be ported easily to any environment. and can be ported easily to any environment.
Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR> Copyright (c) 2006 - 2012, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials are licensed and made available under This program and the accompanying materials are licensed and made available under
the terms and conditions of the BSD License that accompanies this distribution. the terms and conditions of the BSD License that accompanies this distribution.
The full text of the license may be found at The full text of the license may be found at
@ -60,7 +60,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
@param Buffer Output buffer that contains the data read from the PE/COFF image. @param Buffer Output buffer that contains the data read from the PE/COFF image.
@retval RETURN_SUCCESS The specified portion of the PE/COFF image was @retval RETURN_SUCCESS The specified portion of the PE/COFF image was
read and the size read and the size return in ReadSize.
@retval RETURN_DEVICE_ERROR The specified portion of the PE/COFF image @retval RETURN_DEVICE_ERROR The specified portion of the PE/COFF image
could not be read due to a device error. could not be read due to a device error.

View File

@ -83,6 +83,7 @@ PeCoffLoaderGetPeHeader (
RETURN_STATUS Status; RETURN_STATUS Status;
EFI_IMAGE_DOS_HEADER DosHdr; EFI_IMAGE_DOS_HEADER DosHdr;
UINTN Size; UINTN Size;
UINTN ReadSize;
UINT16 Magic; UINT16 Magic;
UINT32 SectionHeaderOffset; UINT32 SectionHeaderOffset;
UINT32 Index; UINT32 Index;
@ -94,13 +95,14 @@ PeCoffLoaderGetPeHeader (
// Read the DOS image header to check for its existence // Read the DOS image header to check for its existence
// //
Size = sizeof (EFI_IMAGE_DOS_HEADER); Size = sizeof (EFI_IMAGE_DOS_HEADER);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
0, 0,
&Size, &Size,
&DosHdr &DosHdr
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status; return Status;
} }
@ -121,13 +123,14 @@ PeCoffLoaderGetPeHeader (
// location in both images. // location in both images.
// //
Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION); Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
ImageContext->PeCoffHeaderOffset, ImageContext->PeCoffHeaderOffset,
&Size, &Size,
Hdr.Pe32 Hdr.Pe32
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status; return Status;
} }
@ -173,16 +176,17 @@ PeCoffLoaderGetPeHeader (
} }
// //
// Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file // 2.2 Read last byte of Hdr.Pe32.OptionalHeader.SizeOfHeaders from the file.
// //
Size = 1; Size = 1;
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1, Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1,
&Size, &Size,
&BufferData &BufferData
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status; return Status;
} }
@ -202,9 +206,10 @@ PeCoffLoaderGetPeHeader (
} }
// //
// Read section header from file // Read last byte of section header from file
// //
Size = 1; Size = 1;
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress + Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +
@ -212,7 +217,7 @@ PeCoffLoaderGetPeHeader (
&Size, &Size,
&BufferData &BufferData
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status; return Status;
} }
} }
@ -246,16 +251,17 @@ PeCoffLoaderGetPeHeader (
} }
// //
// Read Hdr.Pe32.OptionalHeader.SizeOfHeaders data from file // 2.2 Read last byte of Hdr.Pe32Plus.OptionalHeader.SizeOfHeaders from the file.
// //
Size = 1; Size = 1;
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1, Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,
&Size, &Size,
&BufferData &BufferData
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status; return Status;
} }
@ -275,9 +281,10 @@ PeCoffLoaderGetPeHeader (
} }
// //
// Read section header from file // Read last byte of section header from file
// //
Size = 1; Size = 1;
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress + Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +
@ -285,7 +292,7 @@ PeCoffLoaderGetPeHeader (
&Size, &Size,
&BufferData &BufferData
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status; return Status;
} }
} }
@ -333,13 +340,14 @@ PeCoffLoaderGetPeHeader (
// Read section header from file // Read section header from file
// //
Size = sizeof (EFI_IMAGE_SECTION_HEADER); Size = sizeof (EFI_IMAGE_SECTION_HEADER);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
SectionHeaderOffset, SectionHeaderOffset,
&Size, &Size,
&SectionHeader &SectionHeader
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status; return Status;
} }
@ -356,13 +364,14 @@ PeCoffLoaderGetPeHeader (
// Read the last byte to make sure the data is in the image region. // Read the last byte to make sure the data is in the image region.
// //
Size = 1; Size = 1;
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1, SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1,
&Size, &Size,
&BufferData &BufferData
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
return Status; return Status;
} }
} }
@ -416,6 +425,7 @@ PeCoffLoaderGetImageInfo (
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry; EFI_IMAGE_DATA_DIRECTORY *DebugDirectoryEntry;
UINTN Size; UINTN Size;
UINTN ReadSize;
UINTN Index; UINTN Index;
UINTN DebugDirectoryEntryRva; UINTN DebugDirectoryEntryRva;
UINTN DebugDirectoryEntryFileOffset; UINTN DebugDirectoryEntryFileOffset;
@ -538,13 +548,14 @@ PeCoffLoaderGetImageInfo (
// Read section header from file // Read section header from file
// //
Size = sizeof (EFI_IMAGE_SECTION_HEADER); Size = sizeof (EFI_IMAGE_SECTION_HEADER);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
SectionHeaderOffset, SectionHeaderOffset,
&Size, &Size,
&SectionHeader &SectionHeader
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status; return Status;
} }
@ -565,13 +576,14 @@ PeCoffLoaderGetImageInfo (
// Read next debug directory entry // Read next debug directory entry
// //
Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
DebugDirectoryEntryFileOffset + Index, DebugDirectoryEntryFileOffset + Index,
&Size, &Size,
&DebugEntry &DebugEntry
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status; return Status;
} }
@ -599,13 +611,14 @@ PeCoffLoaderGetImageInfo (
// Read section header from file // Read section header from file
// //
Size = sizeof (EFI_IMAGE_SECTION_HEADER); Size = sizeof (EFI_IMAGE_SECTION_HEADER);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
SectionHeaderOffset, SectionHeaderOffset,
&Size, &Size,
&SectionHeader &SectionHeader
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status; return Status;
} }
@ -652,13 +665,14 @@ PeCoffLoaderGetImageInfo (
// Read next debug directory entry // Read next debug directory entry
// //
Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY); Size = sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY);
ReadSize = Size;
Status = ImageContext->ImageRead ( Status = ImageContext->ImageRead (
ImageContext->Handle, ImageContext->Handle,
DebugDirectoryEntryFileOffset + Index, DebugDirectoryEntryFileOffset + Index,
&Size, &Size,
&DebugEntry &DebugEntry
); );
if (RETURN_ERROR (Status)) { if (RETURN_ERROR (Status) || (Size != ReadSize)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status; return Status;
} }