Skip to content

Commit

Permalink
uefi: genfv: Avoid unaligned accesses during checksum
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
evangreen committed Jun 21, 2020
1 parent 7327104 commit 8438101
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions uefi/tools/genfv/genfv.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ Module Name:
// ------------------------------------------------------------------- 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>
Expand All @@ -39,6 +46,11 @@ Module Name:
#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"
Expand Down Expand Up @@ -170,7 +182,7 @@ GenfvCalculateChecksum8 (

UINT16
GenfvCalculateChecksum16 (
UINT16 *Buffer,
UINT8 *Buffer,
UINTN Size
);

Expand Down Expand Up @@ -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);

//
Expand Down Expand Up @@ -1021,7 +1033,7 @@ Return Value:

UINT16
GenfvCalculateChecksum16 (
UINT16 *Buffer,
UINT8 *Buffer,
UINTN Size
)

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8438101

Please sign in to comment.