Browse Source

uefi: genfv: Avoid unaligned accesses during checksum

We were passing unaligned pointers to USHORTs into
GenfvCalculateChecksum16(). Add the includes necessary to use
READ_UNALIGNED16(), and change the types around to be a UINT8 pointer
instead, so the compiler has nothing to complain about.
Evan Green 3 years ago
parent
commit
8438101bee
1 changed files with 17 additions and 6 deletions
  1. 17 6
      uefi/tools/genfv/genfv.c

+ 17 - 6
uefi/tools/genfv/genfv.c

@@ -30,6 +30,13 @@ Environment:
 // ------------------------------------------------------------------- Includes
 //
 
+//
+// Define away API decorators, as everything is linked statically.
+//
+
+#define RTL_API
+#define KERNEL_API
+
 #include <assert.h>
 #include <errno.h>
 #include <getopt.h>
@@ -39,6 +46,11 @@ Environment:
 #include <sys/stat.h>
 #include <unistd.h>
 
+#include <minoca/lib/types.h>
+#include <minoca/lib/status.h>
+#include <minoca/lib/rtl.h>
+#include <minoca/kernel/hmod.h>
+
 #include "uefifw.h"
 #include "efiffs.h"
 #include "fwvol.h"
@@ -170,7 +182,7 @@ GenfvCalculateChecksum8 (
 
 UINT16
 GenfvCalculateChecksum16 (
-    UINT16 *Buffer,
+    UINT8 *Buffer,
     UINTN Size
     );
 
@@ -510,7 +522,7 @@ Return Value:
     Header->BlockMap[0].BlockLength = Context->BlockSize;
     Header->BlockMap[1].BlockCount = 0;
     Header->BlockMap[1].BlockLength = 0;
-    Header->Checksum = GenfvCalculateChecksum16((UINT16 *)Header,
+    Header->Checksum = GenfvCalculateChecksum16((UINT8 *)Header,
                                                 Header->HeaderLength);
 
     //
@@ -1021,7 +1033,7 @@ Return Value:
 
 UINT16
 GenfvCalculateChecksum16 (
-    UINT16 *Buffer,
+    UINT8 *Buffer,
     UINTN Size
     )
 
@@ -1049,10 +1061,9 @@ Return Value:
     UINTN Index;
     UINT16 Sum;
 
-    Size = Size / sizeof(UINT16);
     Sum = 0;
-    for (Index = 0; Index < Size; Index += 1) {
-        Sum = (UINT16)(Sum + Buffer[Index]);
+    for (Index = 0; Index < Size; Index += 2) {
+        Sum = (UINT16)(Sum + READ_UNALIGNED16(Buffer + Index));
     }
 
     return 0x10000 - Sum;