From 8a45a4dc3f27e5088ea6e10322b52d503da90270 Mon Sep 17 00:00:00 2001 From: Marshall Dawson Date: Sun, 24 Feb 2019 07:18:44 -0700 Subject: util/amdfwtool: Clarify call to fletcher32 The fletcher32 algorithm generates a sum over a range of 16-bit WORDs. Change the function's interface to be more generic, accepting a more intuitive size in BYTEs. Don't require the caller to understand the nature of the algorithm and convert to WORDs prior to calling. TEST=Verify no difference in amdfw.rom for google/grunt before and after the patch is applied Change-Id: Iad70558347cbdb3c51bd598479ee4484219c0869 Signed-off-by: Marshall Dawson Reviewed-on: https://review.coreboot.org/c/coreboot/+/31728 Reviewed-by: Martin Roth Reviewed-by: Paul Menzel Tested-by: build bot (Jenkins) --- util/amdfwtool/amdfwtool.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'util/amdfwtool') diff --git a/util/amdfwtool/amdfwtool.c b/util/amdfwtool/amdfwtool.c index 3ed6885624..e1c59aec6a 100644 --- a/util/amdfwtool/amdfwtool.c +++ b/util/amdfwtool/amdfwtool.c @@ -121,12 +121,15 @@ typedef unsigned short uint16_t; * inserted 8 bytes after the beginning of the file. * stderr: Used to print out error messages. */ -static uint32_t fletcher32(const uint16_t *pptr, int length) +static uint32_t fletcher32(const void *data, int length) { uint32_t c0; uint32_t c1; uint32_t checksum; int index; + const uint16_t *pptr = data; + + length /= 2; c0 = 0xFFFF; c1 = 0xFFFF; @@ -138,13 +141,15 @@ static uint32_t fletcher32(const uint16_t *pptr, int length) c0 += *(pptr++); c1 += c0; if ((index % 360) == 0) { - c0 = (c0 & 0xFFFF) + (c0 >> 16); // Sum0 modulo 65535 + the overflow - c1 = (c1 & 0xFFFF) + (c1 >> 16); // Sum1 modulo 65535 + the overflow + /* Sums[0,1] mod 64K + overflow */ + c0 = (c0 & 0xFFFF) + (c0 >> 16); + c1 = (c1 & 0xFFFF) + (c1 >> 16); } } - c0 = (c0 & 0xFFFF) + (c0 >> 16); // Sum0 modulo 65535 + the overflow - c1 = (c1 & 0xFFFF) + (c1 >> 16); // Sum1 modulo 65535 + the overflow + /* Sums[0,1] mod 64K + overflow */ + c0 = (c0 & 0xFFFF) + (c0 >> 16); + c1 = (c1 & 0xFFFF) + (c1 >> 16); checksum = (c1 << 16) | c0; return checksum; @@ -331,9 +336,9 @@ static void fill_psp_head(psp_directory_table *pspdir, uint32_t count) /* checksum everything that comes after the Checksum field */ pspdir->header.checksum = fletcher32( (void *)&pspdir->header.num_entries, - (count * sizeof(psp_directory_entry) + count * sizeof(psp_directory_entry) + sizeof(pspdir->header.num_entries) - + sizeof(pspdir->header.reserved)) / 2); + + sizeof(pspdir->header.reserved)); } static uint32_t integrate_firmwares(char *base, uint32_t pos, @@ -848,11 +853,11 @@ int main(int argc, char **argv) combo_dir->header.reserved[1] = 0; combo_dir->header.checksum = fletcher32( (void *)&combo_dir->header.num_entries, - (1 * sizeof(psp_directory_entry) + 1 * sizeof(psp_directory_entry) + sizeof(combo_dir->header.num_entries) + sizeof(combo_dir->header.lookup) + sizeof(combo_dir->header.reserved[0]) - + sizeof(combo_dir->header.reserved[1])) / 2); + + sizeof(combo_dir->header.reserved[1])); #else current = integrate_psp_firmwares(rom, current, psp2dir, amd_psp2_fw_table, rom_size); -- cgit v1.2.3