diff --git a/README.md b/README.md index 8e23a6d..cc8a020 100644 --- a/README.md +++ b/README.md @@ -56,10 +56,10 @@ as well the avoidance of apparent progress "freezeouts" when very large files are being hashed (such as large squashfs or install.wim images). It should be noted however that, currently, uefi-md5sum supports only the -provision of an `md5sum_totalbytes` value in **lowercase** hexadecimal (no -uppercase hex, no decimal). On the other hand, there is no restriction to where, -in `md5sum.txt`, `md5sum_totalbytes` needs to be specified (i.e. it does not -necessarily mean to appear at the beginning of the file). +provision of an `md5sum_totalbytes` value in hexadecimal (no decimal values). +On the other hand, there is no restriction to where, in `md5sum.txt`, +`md5sum_totalbytes` needs to be specified (i.e. it does not necessarily need to +appear at the beginning of the file). ## Prerequisites diff --git a/src/boot.c b/src/boot.c index 6b686db..d5f974f 100644 --- a/src/boot.c +++ b/src/boot.c @@ -353,7 +353,7 @@ EFI_STATUS EFIAPI efi_main( V_ASSERT(HashList.Entry != NULL); // Print any extra data we want to validate - PrintTest(L"TotalBytes = 0x%lX", HashList.TotalBytes); + PrintTest(L"TotalBytes = 0x%lx", HashList.TotalBytes); // Set up the progress bar data Progress.Type = (HashList.TotalBytes == 0) ? PROGRESS_TYPE_FILE : PROGRESS_TYPE_BYTE; @@ -426,6 +426,9 @@ EFI_STATUS EFIAPI efi_main( UnicodeSPrint(Message, ARRAY_SIZE(Message), L"%d/%d file%s processed [%d failed]", Index, HashList.NumEntries, (HashList.NumEntries == 1) ? L"" : L"s", NumFailed); PrintCentered(Message, Progress.YPos + 2); + if (Status == EFI_SUCCESS && HashList.TotalBytes != 0 && + Progress.Current != HashList.TotalBytes) + PrintWarning(L"Actual 'md5sum_totalbytes' was 0x%lx", Progress.Current); out: SafeFree(HashList.Buffer); diff --git a/src/boot.h b/src/boot.h index 63a0dfe..daf5b96 100644 --- a/src/boot.h +++ b/src/boot.h @@ -246,7 +246,7 @@ typedef struct { /* Check for a valid lowercase hex ASCII value */ STATIC __inline BOOLEAN IsValidHexAscii(CHAR8 c) { - return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')); + return ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')); } /* Check for a valid whitespace character */ diff --git a/src/console.c b/src/console.c index 3733f97..0ca32be 100644 --- a/src/console.c +++ b/src/console.c @@ -290,18 +290,15 @@ VOID UpdateProgress( gConsole.Cols < COLS_MIN || gConsole.Cols >= STRING_MAX) return; - if (Progress->Current > Progress->Maximum) - Progress->Current = Progress->Maximum; - // Update the percentage figure - PerMille = (UINTN)((Progress->Current * 1000) / Progress->Maximum); + PerMille = (UINTN)((MIN(Progress->Current, Progress->Maximum) * 1000) / Progress->Maximum); if (!gIsTestMode) { SetTextPosition(Progress->PPos, Progress->YPos); Print(L"%d.%d%%", PerMille / 10, PerMille % 10); } // Update the progress bar - CurCol = (UINTN)((Progress->Current * gConsole.Cols) / Progress->Maximum); + CurCol = (UINTN)((MIN(Progress->Current, Progress->Maximum) * gConsole.Cols) / Progress->Maximum); if (!gIsTestMode) { for (; CurCol > Progress->LastCol && Progress->LastCol < gConsole.Cols; Progress->LastCol++) { SetTextPosition(Progress->LastCol, Progress->YPos + 1); @@ -309,7 +306,7 @@ VOID UpdateProgress( } } - if (Progress->Current == Progress->Maximum) + if (Progress->Current >= Progress->Maximum) Progress->Active = FALSE; } diff --git a/src/parse.c b/src/parse.c index 6771cbe..bc0658d 100644 --- a/src/parse.c +++ b/src/parse.c @@ -188,8 +188,14 @@ EFI_STATUS Parse( } NumDigits++; TotalBytes <<= 4; - TotalBytes |= (UINT64)(((HashFile[c] - '0') < 0xa) ? - (HashFile[c] - '0') : (HashFile[c] - 'a' + 0xa)); + // IsValidHexAscii() above made sure that our character + // is in the [0-9] or [A-F] or [a-f] ranges. + if (HashFile[c] - '0' < 0xa) + TotalBytes |= HashFile[c] - '0'; + else if (HashFile[c] - 'A' < 6) + TotalBytes |= HashFile[c] - 'A' + 0xa; + else + TotalBytes |= HashFile[c] - 'a' + 0xa; } } } diff --git a/tests/test_list.txt b/tests/test_list.txt index 7e4fd77..701161d 100644 --- a/tests/test_list.txt +++ b/tests/test_list.txt @@ -177,6 +177,13 @@ file: [14] Not Found file: [14] Not Found 1/1 file processed [1 failed] +# TotalBytes mixed case +> echo "# md5sum_totalbytes = 0xaAbBcCdDeEfF" > image/md5sum.txt +> echo "00112233445566778899aabbccddeeff file" >> image/md5sum.txt +[TEST] TotalBytes = 0xAABBCCDDEEFF +file: [14] Not Found +1/1 file processed [1 failed] + # MD5 basic validation for up to 2-blocks > # Tests all sizes up to 2-blocks (2 x 64 bytes) > for size in {000..128}; do @@ -306,6 +313,7 @@ file832 (832 KB) file896 (896 KB) file960 (960 KB) 16/16 files processed [0 failed] +[WARN] Actual 'md5sum_totalbytes' was 0x880000 < rm image/file* # Progress with TotalBytes exact @@ -354,6 +362,7 @@ file832 (832 KB) file896 (896 KB) file960 (960 KB) 16/16 files processed [0 failed] +[WARN] Actual 'md5sum_totalbytes' was 0x880000 < rm image/file* # Fuzzing test: 100 Random bytes in hash list