Skip to content

Commit 55b6c66

Browse files
committed
[BOOTLIB]: Cleanup, less magic.
svn path=/trunk/; revision=70633
1 parent 953f5a4 commit 55b6c66

File tree

3 files changed

+81
-51
lines changed

3 files changed

+81
-51
lines changed

reactos/boot/environ/include/bl.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,12 @@ DEFINE_GUID(BadMemoryGuid, 0x54B8275B, 0xD431, 0x473F, 0xAC, 0xFB, 0xE5, 0x36, 0
155155
#define BL_LOAD_PE_IMG_CHECK_SUBSYSTEM 0x80
156156
#define BL_LOAD_PE_IMG_CHECK_FORCED_INTEGRITY 0x200
157157

158+
#define BL_UTL_CHECKSUM_COMPLEMENT 0x10000
159+
#define BL_UTL_CHECKSUM_ROTATE 0x20000
160+
#define BL_UTL_CHECKSUM_NEGATE 0x40000
161+
#define BL_UTL_CHECKSUM_UCHAR_BUFFER 0x01
162+
#define BL_UTL_CHECKSUM_USHORT_BUFFER 0x02
163+
158164
/* ENUMERATIONS **************************************************************/
159165

160166
typedef enum _BL_COLOR
@@ -1620,6 +1626,14 @@ BlUtlRegisterProgressRoutine (
16201626
VOID
16211627
);
16221628

1629+
ULONG
1630+
BlUtlCheckSum (
1631+
_In_ ULONG PartialSum,
1632+
_In_ PUCHAR Buffer,
1633+
_In_ ULONG Length,
1634+
_In_ ULONG Flags
1635+
);
1636+
16231637
NTSTATUS
16241638
BlGetApplicationBaseAndSize (
16251639
_Out_ PVOID* ImageBase,

reactos/boot/environ/lib/misc/image.c

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -651,54 +651,6 @@ BlImgUnLoadImage (
651651
return BlImgUnallocateImageBuffer(ImageBase, ImageSize, ImageFlags);
652652
}
653653

654-
unsigned int BlUtlCheckSum(unsigned int PartialSum, PUCHAR Source, unsigned int Length, unsigned int Flags)
655-
{
656-
unsigned int Type; // eax@1
657-
int Type1; // eax@1
658-
unsigned int AlignedLength; // ebx@3
659-
unsigned int i; // ebx@21 MAPDST
660-
661-
Type = Flags & 3;
662-
Type1 = Type - 1;
663-
if (Type1)
664-
{
665-
if (Type1 == 1)
666-
{
667-
PartialSum = (unsigned __int16)PartialSum;
668-
AlignedLength = Length & ~1;
669-
if (Length & ~1)
670-
{
671-
i = 0;
672-
do
673-
{
674-
PartialSum += *(unsigned __int16 *)&Source[i];
675-
if (Flags & 0x10000)
676-
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
677-
i += 2;
678-
} while (i < AlignedLength);
679-
}
680-
681-
if (Length != AlignedLength)
682-
{
683-
PartialSum += (unsigned __int8)Source[AlignedLength];
684-
if (Flags & 0x10000)
685-
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
686-
}
687-
if (Flags & 0x40000)
688-
return ~PartialSum;
689-
PartialSum = (unsigned __int16)PartialSum;
690-
}
691-
}
692-
else
693-
{
694-
EfiPrintf(L"checksum type not supported\r\n");
695-
}
696-
697-
if (Flags & 0x40000)
698-
return ~PartialSum;
699-
return PartialSum;
700-
}
701-
702654
NTSTATUS
703655
ImgpLoadPEImage (
704656
_In_ PBL_IMG_FILE ImageFile,
@@ -921,7 +873,11 @@ ImgpLoadPEImage (
921873
NtHeaders->OptionalHeader.CheckSum = 0;
922874

923875
/* Calculate the checksum of the header, and restore the original one */
924-
PartialSum = BlUtlCheckSum(0, VirtualAddress, HeaderSize, 0x10002);
876+
PartialSum = BlUtlCheckSum(0,
877+
VirtualAddress,
878+
HeaderSize,
879+
BL_UTL_CHECKSUM_COMPLEMENT |
880+
BL_UTL_CHECKSUM_USHORT_BUFFER);
925881
NtHeaders->OptionalHeader.CheckSum = CheckSum;
926882

927883
/* Record our current position (right after the headers) */
@@ -1037,7 +993,8 @@ ImgpLoadPEImage (
1037993
PartialSum = BlUtlCheckSum(PartialSum,
1038994
(PUCHAR)SectionStart,
1039995
AlignSize,
1040-
0x10002);
996+
BL_UTL_CHECKSUM_COMPLEMENT |
997+
BL_UTL_CHECKSUM_USHORT_BUFFER);
1041998
}
1042999
}
10431000

@@ -1114,7 +1071,8 @@ ImgpLoadPEImage (
11141071
PartialSum = BlUtlCheckSum(PartialSum,
11151072
LocalBuffer,
11161073
BytesRead,
1117-
0x10002);
1074+
BL_UTL_CHECKSUM_COMPLEMENT |
1075+
BL_UTL_CHECKSUM_USHORT_BUFFER);
11181076
}
11191077

11201078
/* Finally, calculate the final checksum and compare it */

reactos/boot/environ/lib/misc/util.c

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,3 +720,61 @@ BlHtStore (
720720
return Status;
721721
}
722722

723+
ULONG
724+
BlUtlCheckSum (
725+
_In_ ULONG PartialSum,
726+
_In_ PUCHAR Buffer,
727+
_In_ ULONG Length,
728+
_In_ ULONG Flags
729+
)
730+
{
731+
ULONG i;
732+
733+
if (Flags & BL_UTL_CHECKSUM_UCHAR_BUFFER)
734+
{
735+
EfiPrintf(L"Not supported\r\n");
736+
return 0;
737+
}
738+
else if (Flags & BL_UTL_CHECKSUM_USHORT_BUFFER)
739+
{
740+
PartialSum = (unsigned __int16)PartialSum;
741+
Length &= ~1;
742+
743+
for (i = 0; i < Length; i += 2)
744+
{
745+
PartialSum += *(unsigned __int16 *)&Buffer[i];
746+
if (Flags & BL_UTL_CHECKSUM_COMPLEMENT)
747+
{
748+
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
749+
}
750+
}
751+
752+
if (Length != Length)
753+
{
754+
PartialSum += (unsigned __int8)Buffer[Length];
755+
if (Flags & BL_UTL_CHECKSUM_COMPLEMENT)
756+
{
757+
PartialSum = (unsigned __int16)((PartialSum >> 16) + PartialSum);
758+
}
759+
}
760+
761+
if (Flags & BL_UTL_CHECKSUM_NEGATE)
762+
{
763+
return ~PartialSum;
764+
}
765+
766+
PartialSum = (unsigned __int16)PartialSum;
767+
}
768+
else
769+
{
770+
/* Invalid mode */
771+
return 0;
772+
}
773+
774+
if (Flags & BL_UTL_CHECKSUM_NEGATE)
775+
{
776+
return ~PartialSum;
777+
}
778+
779+
return PartialSum;
780+
}

0 commit comments

Comments
 (0)