Skip to content

Commit

Permalink
Enable uppercase hex for totalbytes and print computed value on mismatch
Browse files Browse the repository at this point in the history
* This requires a small alteration to UpdateProgress() to make sure that
  Progress->Current is not modified there.
  • Loading branch information
pbatard committed Mar 19, 2024
1 parent 5d5e216 commit 9a83938
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 4 additions & 1 deletion src/boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
9 changes: 3 additions & 6 deletions src/console.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,26 +290,23 @@ 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);
Print(L"%c", BLOCKELEMENT_FULL_BLOCK);
}
}

if (Progress->Current == Progress->Maximum)
if (Progress->Current >= Progress->Maximum)
Progress->Active = FALSE;
}

Expand Down
10 changes: 8 additions & 2 deletions src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/test_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9a83938

Please sign in to comment.