From 4b9b2b4251d0cec7d9b6c0da24c063a19bd2acfb Mon Sep 17 00:00:00 2001
From: Sergey Isakov <isakov-sl@bk.ru>
Date: Thu, 19 Sep 2019 15:56:27 +0300
Subject: [PATCH] Sum will never overflow

---
 .../ApfsDriverLoader/ApfsDriverLoader.c       | 31 +++++--------------
 .../ApfsDriverLoader/ApfsDriverLoader.h       | 22 +++++++++++++
 2 files changed, 29 insertions(+), 24 deletions(-)

diff --git a/FileSystems/ApfsDriverLoader/ApfsDriverLoader.c b/FileSystems/ApfsDriverLoader/ApfsDriverLoader.c
index 9b82abf50..2b17757f0 100644
--- a/FileSystems/ApfsDriverLoader/ApfsDriverLoader.c
+++ b/FileSystems/ApfsDriverLoader/ApfsDriverLoader.c
@@ -17,26 +17,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 
 **/
 //#include <AppleSupportPkgVersion.h>
-#include <Uefi/UefiGpt.h>
-#include <Library/DebugLib.h>
-#include <Library/BaseMemoryLib.h>
-#include <Library/DevicePathLib.h>
-#include <Library/MemoryAllocationLib.h>
-#include <Library/UefiDriverEntryPoint.h>
-#include <Library/UefiBootServicesTableLib.h>
-#include <Library/UefiRuntimeServicesTableLib.h>
-//#include <Library/OcAppleImageVerificationLib.h>
-#include <Protocol/BlockIo.h>
-#include <Protocol/DiskIo.h>
-#include <Protocol/BlockIo2.h>
-#include <Protocol/DiskIo2.h>
-#include <Protocol/LoadedImage.h>
-#include <Protocol/ComponentName.h>
-#include <Protocol/DriverBinding.h>
-#include <Protocol/PartitionInfo.h>
-#include <Protocol/ApplePartitionInfo.h>
-#include <Protocol/ApfsEfiBootRecordInfo.h>
-#include <Protocol/NullTextOutput.h>
 
 #define APPLE_SUPPORT_VERSION  L"2.0.9"
 #include "ApfsDriverLoader.h"
@@ -56,11 +36,13 @@ ApfsBlockChecksumCalculate (
   UINT64        Check1 = 0;
   UINT64        Sum2 = 0;
   UINT64        Check2 = 0;
-  CONST UINT64  ModValue = 0xFFFFFFFF;
+  CONST UINT64  ModValue = 0xFFFFFFFFull;
 
   for (Index = 0; Index < DataSize / sizeof (UINT32); Index++) {
-    Sum1 = ((Sum1 + (UINT64)Data[Index]) % ModValue);
-    Sum2 = (Sum2 + Sum1) % ModValue;
+//    Sum1 = ((Sum1 + (UINT64)Data[Index]) % ModValue);
+//    Sum2 = (Sum2 + Sum1) % ModValue;
+    Sum1 += (UINT64)*Data++;
+    Sum2 += Sum1;
   }
 
   Check1 = ModValue - ((Sum1 + Sum2) % ModValue);
@@ -811,7 +793,7 @@ ApfsDriverLoaderStart (
 
   //
   // Free ApfsBlock and allocate one of a correct size.
-  // ContainerSuperBlock & EfiBootRecordBlockPtr will not valid now
+  // ContainerSuperBlock (& EfiBootRecordBlockPtr ?) will not valid now
   //
   FreePool (ApfsBlock);
   ApfsBlock = AllocateZeroPool (ApfsBlockSize);
@@ -849,6 +831,7 @@ ApfsDriverLoaderStart (
   //
   ContainerSuperBlock = (APFS_CSB *)ApfsBlock;
   CopyMem (&ContainerUuid, &ContainerSuperBlock->Uuid, sizeof (EFI_GUID));
+  EfiBootRecordBlockPtr = ContainerSuperBlock->EfiBootRecordBlock;
 
   //
   // Calculate Offset of EfiBootRecordBlock
diff --git a/FileSystems/ApfsDriverLoader/ApfsDriverLoader.h b/FileSystems/ApfsDriverLoader/ApfsDriverLoader.h
index ff7d55851..743e623d5 100644
--- a/FileSystems/ApfsDriverLoader/ApfsDriverLoader.h
+++ b/FileSystems/ApfsDriverLoader/ApfsDriverLoader.h
@@ -19,6 +19,28 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
 #ifndef APFS_DRIVER_LOADER_H_
 #define APFS_DRIVER_LOADER_H_
 
+#include <Uefi/UefiGpt.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DevicePathLib.h>
+#include <Library/MemoryAllocationLib.h>
+#include <Library/UefiDriverEntryPoint.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/UefiRuntimeServicesTableLib.h>
+//#include <Library/OcAppleImageVerificationLib.h>
+#include <Protocol/BlockIo.h>
+#include <Protocol/DiskIo.h>
+#include <Protocol/BlockIo2.h>
+#include <Protocol/DiskIo2.h>
+#include <Protocol/LoadedImage.h>
+#include <Protocol/ComponentName.h>
+#include <Protocol/DriverBinding.h>
+#include <Protocol/PartitionInfo.h>
+#include <Protocol/ApplePartitionInfo.h>
+#include <Protocol/ApfsEfiBootRecordInfo.h>
+#include <Protocol/NullTextOutput.h>
+
+
 #define APFS_DRIVER_INFO_PRIVATE_DATA_SIGNATURE  SIGNATURE_32 ('A', 'F', 'J', 'S')
 
 //