[BOOTLIB]: Cleanup, less magic.

svn path=/trunk/; revision=70633
This commit is contained in:
Alex Ionescu 2016-01-21 15:47:14 +00:00
parent 953f5a4f99
commit 55b6c66d7a
3 changed files with 81 additions and 51 deletions

View file

@ -155,6 +155,12 @@ DEFINE_GUID(BadMemoryGuid, 0x54B8275B, 0xD431, 0x473F, 0xAC, 0xFB, 0xE5, 0x36, 0
#define BL_LOAD_PE_IMG_CHECK_SUBSYSTEM 0x80
#define BL_LOAD_PE_IMG_CHECK_FORCED_INTEGRITY 0x200
#define BL_UTL_CHECKSUM_COMPLEMENT 0x10000
#define BL_UTL_CHECKSUM_ROTATE 0x20000
#define BL_UTL_CHECKSUM_NEGATE 0x40000
#define BL_UTL_CHECKSUM_UCHAR_BUFFER 0x01
#define BL_UTL_CHECKSUM_USHORT_BUFFER 0x02
/* ENUMERATIONS **************************************************************/
typedef enum _BL_COLOR
@ -1620,6 +1626,14 @@ BlUtlRegisterProgressRoutine (
VOID
);
ULONG
BlUtlCheckSum (
_In_ ULONG PartialSum,
_In_ PUCHAR Buffer,
_In_ ULONG Length,
_In_ ULONG Flags
);
NTSTATUS
BlGetApplicationBaseAndSize (
_Out_ PVOID* ImageBase,

View file

@ -651,54 +651,6 @@ BlImgUnLoadImage (
return BlImgUnallocateImageBuffer(ImageBase, ImageSize, ImageFlags);
}
unsigned int BlUtlCheckSum(unsigned int PartialSum, PUCHAR Source, unsigned int Length, unsigned int Flags)
{
unsigned int Type; // eax@1
int Type1; // eax@1
unsigned int AlignedLength; // ebx@3
unsigned int i; // ebx@21 MAPDST
Type = Flags & 3;
Type1 = Type - 1;
if (Type1)
{
if (Type1 == 1)
{
PartialSum = (unsigned __int16)PartialSum;
AlignedLength = Length & ~1;
if (Length & ~1)
{
i = 0;
do
{
PartialSum += *(unsigned __int16 *)&Source[i];
if (Flags & 0x10000)
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
i += 2;
} while (i < AlignedLength);
}
if (Length != AlignedLength)
{
PartialSum += (unsigned __int8)Source[AlignedLength];
if (Flags & 0x10000)
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
}
if (Flags & 0x40000)
return ~PartialSum;
PartialSum = (unsigned __int16)PartialSum;
}
}
else
{
EfiPrintf(L"checksum type not supported\r\n");
}
if (Flags & 0x40000)
return ~PartialSum;
return PartialSum;
}
NTSTATUS
ImgpLoadPEImage (
_In_ PBL_IMG_FILE ImageFile,
@ -921,7 +873,11 @@ ImgpLoadPEImage (
NtHeaders->OptionalHeader.CheckSum = 0;
/* Calculate the checksum of the header, and restore the original one */
PartialSum = BlUtlCheckSum(0, VirtualAddress, HeaderSize, 0x10002);
PartialSum = BlUtlCheckSum(0,
VirtualAddress,
HeaderSize,
BL_UTL_CHECKSUM_COMPLEMENT |
BL_UTL_CHECKSUM_USHORT_BUFFER);
NtHeaders->OptionalHeader.CheckSum = CheckSum;
/* Record our current position (right after the headers) */
@ -1037,7 +993,8 @@ ImgpLoadPEImage (
PartialSum = BlUtlCheckSum(PartialSum,
(PUCHAR)SectionStart,
AlignSize,
0x10002);
BL_UTL_CHECKSUM_COMPLEMENT |
BL_UTL_CHECKSUM_USHORT_BUFFER);
}
}
@ -1114,7 +1071,8 @@ ImgpLoadPEImage (
PartialSum = BlUtlCheckSum(PartialSum,
LocalBuffer,
BytesRead,
0x10002);
BL_UTL_CHECKSUM_COMPLEMENT |
BL_UTL_CHECKSUM_USHORT_BUFFER);
}
/* Finally, calculate the final checksum and compare it */

View file

@ -720,3 +720,61 @@ Quickie:
return Status;
}
ULONG
BlUtlCheckSum (
_In_ ULONG PartialSum,
_In_ PUCHAR Buffer,
_In_ ULONG Length,
_In_ ULONG Flags
)
{
ULONG i;
if (Flags & BL_UTL_CHECKSUM_UCHAR_BUFFER)
{
EfiPrintf(L"Not supported\r\n");
return 0;
}
else if (Flags & BL_UTL_CHECKSUM_USHORT_BUFFER)
{
PartialSum = (unsigned __int16)PartialSum;
Length &= ~1;
for (i = 0; i < Length; i += 2)
{
PartialSum += *(unsigned __int16 *)&Buffer[i];
if (Flags & BL_UTL_CHECKSUM_COMPLEMENT)
{
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
}
}
if (Length != Length)
{
PartialSum += (unsigned __int8)Buffer[Length];
if (Flags & BL_UTL_CHECKSUM_COMPLEMENT)
{
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
}
}
if (Flags & BL_UTL_CHECKSUM_NEGATE)
{
return ~PartialSum;
}
PartialSum = (unsigned __int16)PartialSum;
}
else
{
/* Invalid mode */
return 0;
}
if (Flags & BL_UTL_CHECKSUM_NEGATE)
{
return ~PartialSum;
}
return PartialSum;
}