Skip to content

Commit

Permalink
Misc. code cleanup
Browse files Browse the repository at this point in the history
* Remove unneeded code elements and drop comprehensive system info report.
* Add console output positioning.
* Update gnu-efi to latest.
  • Loading branch information
pbatard committed Nov 29, 2023
1 parent 0ce5851 commit 259be7e
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 160 deletions.
12 changes: 0 additions & 12 deletions Md5SumPkg-extra.uni

This file was deleted.

65 changes: 32 additions & 33 deletions boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@
*/
BOOLEAN IsTestMode = FALSE;

/* Copies of the global image handle and system table for the current executable */
EFI_SYSTEM_TABLE* MainSystemTable = NULL;
EFI_HANDLE MainImageHandle = NULL;

/* Strings used to identify the plaform */
#if defined(_M_X64) || defined(__x86_64__)
STATIC CHAR16* Arch = L"x64";
Expand Down Expand Up @@ -99,51 +95,42 @@ STATIC VOID DisplayBanner(VOID)
*/
EFI_STATUS EFIAPI efi_main(EFI_HANDLE BaseImageHandle, EFI_SYSTEM_TABLE *SystemTable)
{
EFI_STATUS Status = EFI_SUCCESS;
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL* LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL* Volume;
EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *SimpleTextOut;
EFI_FILE_HANDLE Root;
HASH_LIST HashList = { 0 };
INTN SecureBootStatus;
CHAR16 *PluralFiles;
#if defined(EFI_DEBUG)
UINTN Event;
UINTN Index;
#endif

MainSystemTable = SystemTable;
MainImageHandle = BaseImageHandle;

#if defined(_GNU_EFI)
InitializeLib(BaseImageHandle, SystemTable);
#endif

IsTestMode = IsTestSystem();

// Display non-critical system information
DisplayBanner();
PrintSystemInfo();
SecureBootStatus = GetSecureBootStatus();
SetText(TEXT_WHITE);
Print(L"[INFO]");
DefText();
Print(L" Secure Boot status: ");
if (SecureBootStatus == 0) {
Print(L"Disabled\n");
} else {
SetText((SecureBootStatus > 0) ? TEXT_WHITE : TEXT_YELLOW);
Print(L"%s\n", (SecureBootStatus > 0) ? L"Enabled" : L"Setup");
DefText();
if (!IsTestMode)
DisplayBanner();

Status = gBS->LocateProtocol(&gEfiSimpleTextOutProtocolGuid, NULL, (VOID**)&SimpleTextOut);
if (EFI_ERROR(Status)) {
PrintError(L"Unable to initialize text output");
goto out;
}

Status = gBS->OpenProtocol(MainImageHandle, &gEfiLoadedImageProtocolGuid,
(VOID**)&LoadedImage, MainImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
Status = gBS->OpenProtocol(BaseImageHandle, &gEfiLoadedImageProtocolGuid,
(VOID**)&LoadedImage, BaseImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
if (EFI_ERROR(Status)) {
PrintError(L"Unable to access boot image interface");
goto out;
}

// Open the the root directory on the boot volume
Status = gBS->OpenProtocol(LoadedImage->DeviceHandle, &gEfiSimpleFileSystemProtocolGuid,
(VOID**)&Volume, MainImageHandle, NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
(VOID**)&Volume, BaseImageHandle, NULL, EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL);
if (EFI_ERROR(Status)) {
PrintError(L"Unable to open boot volume");
goto out;
Expand All @@ -155,25 +142,37 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE BaseImageHandle, EFI_SYSTEM_TABLE *SystemT
goto out;
}

// Parse the md5sum.txt to construct a hash list
Status = Parse(Root, HASH_FILE, &HashList);
if (EFI_ERROR(Status))
goto out;

Print(L"Found %d entries (Total Bytes = 0x%lX)\n", HashList.Size, HashList.TotalBytes);
if (IsTestMode) {
// Print any extra data we want to validate in test mode
Print(L"[TEST] TotalBytes = 0x%lX\n", HashList.TotalBytes);
} else {
// Position our output near the center of the screen
SetTextPosition(TEXT_POSITION_X, TEXT_POSITION_Y);
Print(L"Media verification - Press any key to cancel\n");
}

PluralFiles = (HashList.Size == 1) ? L"" : L"s";
SetTextPosition(TEXT_POSITION_X, TEXT_POSITION_Y + 1);
Print(L"0/%d file%s processed\n", HashList.Size, PluralFiles);

out:
// If running in test mode, close QEMU by invoking shutdown
// If running in test mode, close QEMU by invoking ShutDown()
if (IsTestMode)
SHUTDOWN;
ShutDown();

#if defined(EFI_DEBUG)
// If running debug, wait for a user keystroke and shutdown
// If running debug, wait for a user keystroke and shut down
SetText(TEXT_YELLOW);
Print(L"\nPress any key to exit.\n");
DefText();
gST->ConIn->Reset(gST->ConIn, FALSE);
gST->BootServices->WaitForEvent(1, &gST->ConIn->WaitForKey, &Event);
SHUTDOWN;
gST->BootServices->WaitForEvent(1, &gST->ConIn->WaitForKey, &Index);
ShutDown();
#endif

return Status;
Expand Down
38 changes: 30 additions & 8 deletions boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@
/* Set to true when we are running the GitHub Actions tests */
extern BOOLEAN IsTestMode;

/* Copies of the global image handle and system table for the current executable */
extern EFI_HANDLE MainImageHandle;
extern EFI_SYSTEM_TABLE* MainSystemTable;

/* SMBIOS vendor name used by GitHub Actions' qemu when running the tests */
#define TESTING_SMBIOS_NAME "GitHub Actions Test"

/* Name of the file containing the list of hashes */
#define HASH_FILE L"md5sum.txt"

/* Used to center our output on screen */
#define TEXT_POSITION_X 0
#define TEXT_POSITION_Y 6

/* Size of an MD5 hash */
#define MD5_HASHSIZE 16

Expand Down Expand Up @@ -132,13 +132,16 @@ extern EFI_SYSTEM_TABLE* MainSystemTable;
#define PrintError(fmt, ...) do { SetText(TEXT_RED); Print(L"[FAIL]"); DefText(); \
Print(L" " fmt L": [%d] %r\n", ##__VA_ARGS__, (Status&0x7FFFFFFF), Status); } while (0)

/* Convenience macro to position text on screen (when not running in test mode). */
#define SetTextPosition(x, y) do { if (!IsTestMode) SimpleTextOut->SetCursorPosition(SimpleTextOut, x, y);} while (0)

/* Halt/Shutdown macros */
#define SHUTDOWN MainSystemTable->RuntimeServices->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL)
#define SHUTDOWN gRT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL)
#define HALT while(1)

/* Convenience assertion macro */
#define P_ASSERT(f, l, a) do { if(!(a)) { Print(L"*** ASSERT FAILED: %a(%d): %a ***\n", f, l, #a); \
if (IsTestMode) SHUTDOWN; else HALT; } } while(0)
if (IsTestMode) ShutDown(); else Halt(); } } while(0)
#define V_ASSERT(a) P_ASSERT(__FILE__, __LINE__, a)

/*
Expand Down Expand Up @@ -167,17 +170,36 @@ typedef struct {
} HASH_LIST;

/* Check for a valid lowercase hex ASCII value */
static __inline BOOLEAN IsValidHexAscii(CHAR8 c)
STATIC __inline BOOLEAN IsValidHexAscii(CHAR8 c)
{
return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
}

/* Check for a valid whitespace character */
static __inline BOOLEAN IsWhiteSpace(CHAR8 c)
STATIC __inline BOOLEAN IsWhiteSpace(CHAR8 c)
{
return (c == ' ' || c == '\t');
}

/* Pause the system for a specific duration (in ms) */
STATIC __inline EFI_STATUS Sleep(UINTN MicroSeconds)
{
return gBS->Stall(MicroSeconds);
}

/* Shut down the system immediately */
STATIC __inline VOID ShutDown()
{
gRT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
}

/* Freeze the system with current screen output, then shut it down after one hour */
STATIC __inline VOID Halt()
{
Sleep(3600 * 1000);
gRT->ResetSystem(EfiResetShutdown, EFI_SUCCESS, 0, NULL);
}

/*
* Secure string length, that asserts if the string is NULL or if
* the length is larger than a predetermined value (STRING_MAX)
Expand Down
35 changes: 21 additions & 14 deletions gen_tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# Hash list min size
> echo -n "00112233445566778899aabbccddeeff ." > image/md5sum.txt
Found 1 entries (Total Bytes = 0x0)
0/1 file processed

# Hash list too large
> dd if=/dev/zero of=image/md5sum.txt bs=1M count=65
Expand Down Expand Up @@ -49,7 +49,7 @@ Found 1 entries (Total Bytes = 0x0)

# Hash containing uppercase
> echo "00112233445566778899AaBbCcDdEeFf Mixed case hash" > image/md5sum.txt
Found 1 entries (Total Bytes = 0x0)
0/1 file processed

# Path containing NUL
> echo "00112233445566778899aabbccddeeff This path contains a NUL" | tr '\11' '\0' > image/md5sum.txt
Expand All @@ -67,17 +67,17 @@ Found 1 entries (Total Bytes = 0x0)
# Max Path size
> echo -n "00112233445566778899aabbccddeeff " > image/md5sum.txt
> cat /dev/zero | tr '\0' 'a' | head -c 512 - >> image/md5sum.txt
Found 1 entries (Total Bytes = 0x0)
0/1 file processed

# Comment preceded by whitespaces
> echo " # Comment preceded by whitespaces" > image/md5sum.txt
> echo "0123456789abcdef0123456789abcdef file" >> image/md5sum.txt
Found 1 entries (Total Bytes = 0x0)
0/1 file processed

# Comment followed by whitespaces
> echo "# Comment followed by whitespaces " > image/md5sum.txt
> echo "0123456789abcdef0123456789abcdef file1" >> image/md5sum.txt
Found 1 entries (Total Bytes = 0x0)
0/1 file processed

# Comment containing a NUL
> echo "00112233445566778899aabbccddeeff file1" > image/md5sum.txt
Expand All @@ -89,12 +89,12 @@ Found 1 entries (Total Bytes = 0x0)
> echo "00112233445566778899aabbccddeeff file1" > image/md5sum.txt
> echo "# Comment in the middle" >> image/md5sum.txt
> echo "0123456789abcdef0123456789abcdef file2" >> image/md5sum.txt
Found 2 entries (Total Bytes = 0x0)
0/2 files processed

# Unterminated hash list
> echo "00112233445566778899aabbccddeeff file1" > image/md5sum.txt
> echo -n "0123456789abcdef0123456789abcdef file2" >> image/md5sum.txt
Found 2 entries (Total Bytes = 0x0)
0/2 files processed

# Empty path
> echo "00112233445566778899aabbccddeeff " > image/md5sum.txt
Expand All @@ -105,38 +105,45 @@ Found 2 entries (Total Bytes = 0x0)
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes:" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
Found 1 entries (Total Bytes = 0x0)
[TEST] TotalBytes = 0x0
0/1 file processed

# Invalid TotalBytes
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: invalid" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
Found 1 entries (Total Bytes = 0x0)
[TEST] TotalBytes = 0x0
0/1 file processed

# TotalBytes invalid 0x prefix
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: 0 x1234" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
Found 1 entries (Total Bytes = 0x0)
[TEST] TotalBytes = 0x0
0/1 file processed

# TotalBytes missing 0x prefix
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: 1234" >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
Found 1 entries (Total Bytes = 0x0)
[TEST] TotalBytes = 0x0
0/1 file processed

# TotalBytes 1 character value
> echo "# TotalBytes: 0x8 " > image/md5sum.txt
> echo "00112233445566778899aabbccddeeff file" >> image/md5sum.txt
Found 1 entries (Total Bytes = 0x8)
[TEST] TotalBytes = 0x8
0/1 file processed

# TotalBytes 16 characters value
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "#TotalBytes: 0x12345678abcdef0" >> image/md5sum.txt
Found 1 entries (Total Bytes = 0x12345678ABCDEF0)
[TEST] TotalBytes = 0x12345678ABCDEF0
0/1 file processed

# TotalBytes 17 characters value
> echo "00112233445566778899aabbccddeeff file" > image/md5sum.txt
> echo "# TotalBytes: 0xabcdabcdabcdabcd0 " >> image/md5sum.txt
[WARN] Ignoring invalid TotalBytes value
Found 1 entries (Total Bytes = 0x0)
[TEST] TotalBytes = 0x0
0/1 file processed
2 changes: 1 addition & 1 deletion gnu-efi
2 changes: 1 addition & 1 deletion parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "boot.h"

/* The hash sum list file may provide a comment with the total size of bytes to process */
/* The hash sum list file may provide a comment with the total size of bytes to process */
STATIC CONST CHAR8 TotalBytesString[] = "TotalBytes:";

/**
Expand Down
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ for t in $TEST_DIR/*.dat; do
NUM_FAIL=$((NUM_FAIL + 1))
echo "Output of failed test was:"
echo "-------------------------------------------------------------------------------"
tail -n +4 output.txt
tail -n +3 output.txt
echo "-------------------------------------------------------------------------------"
fi
fi
Expand Down
Loading

0 comments on commit 259be7e

Please sign in to comment.