From 55b6c66d7aa6daa4030ff3c33d1a186c6b348e2e Mon Sep 17 00:00:00 2001 From: Alex Ionescu Date: Thu, 21 Jan 2016 15:47:14 +0000 Subject: [PATCH] [BOOTLIB]: Cleanup, less magic. svn path=/trunk/; revision=70633 --- reactos/boot/environ/include/bl.h | 14 +++++++ reactos/boot/environ/lib/misc/image.c | 60 ++++----------------------- reactos/boot/environ/lib/misc/util.c | 58 ++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 51 deletions(-) diff --git a/reactos/boot/environ/include/bl.h b/reactos/boot/environ/include/bl.h index 6488e308ce1..923a28338b7 100644 --- a/reactos/boot/environ/include/bl.h +++ b/reactos/boot/environ/include/bl.h @@ -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, diff --git a/reactos/boot/environ/lib/misc/image.c b/reactos/boot/environ/lib/misc/image.c index c155930d41c..a734bcc4a70 100644 --- a/reactos/boot/environ/lib/misc/image.c +++ b/reactos/boot/environ/lib/misc/image.c @@ -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 */ diff --git a/reactos/boot/environ/lib/misc/util.c b/reactos/boot/environ/lib/misc/util.c index bd37bebe8bb..3e152d3dc07 100644 --- a/reactos/boot/environ/lib/misc/util.c +++ b/reactos/boot/environ/lib/misc/util.c @@ -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; +}