Skip to content

Commit

Permalink
add application banner and coloured text output
Browse files Browse the repository at this point in the history
  • Loading branch information
pbatard committed Sep 25, 2021
1 parent f2363f4 commit f8fcde2
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
67 changes: 60 additions & 7 deletions boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,52 @@ static VOID DisconnectBlockingDrivers(VOID) {
FreePool(Handles);
}

/*
* Display a centered application banner
*/
static VOID DisplayBanner(VOID)
{
UINTN i, Len;
CHAR16 String[BANNER_LINE_SIZE + 1];

// The platform logo may still be displayed → remove it
gST->ConOut->ClearScreen(gST->ConOut);

SetText(TEXT_REVERSED);
Print(L"\n%c", BOXDRAW_DOWN_RIGHT);
for (i = 0; i < BANNER_LINE_SIZE - 2; i++)
Print(L"%c", BOXDRAW_HORIZONTAL);
Print(L"%c\n", BOXDRAW_DOWN_LEFT);

UnicodeSPrint(String, ARRAY_SIZE(String), L"UEFI:NTFS %s (%s)", VERSION_STRING, Arch);
Len = SafeStrLen(String);
V_ASSERT(Len < BANNER_LINE_SIZE);
Print(L"%c", BOXDRAW_VERTICAL);
for (i = 1; i < (BANNER_LINE_SIZE - Len) / 2; i++)
Print(L" ");
Print(String);
for (i += Len; i < BANNER_LINE_SIZE - 1; i++)
Print(L" ");
Print(L"%c\n", BOXDRAW_VERTICAL);

UnicodeSPrint(String, ARRAY_SIZE(String), L"<https://un.akeo.ie>");
Len = SafeStrLen(String);
V_ASSERT(Len < BANNER_LINE_SIZE);
Print(L"%c", BOXDRAW_VERTICAL);
for (i = 1; i < (BANNER_LINE_SIZE - Len) / 2; i++)
Print(L" ");
Print(String);
for (i += Len; i < BANNER_LINE_SIZE - 1; i++)
Print(L" ");
Print(L"%c\n", BOXDRAW_VERTICAL);

Print(L"%c", BOXDRAW_UP_RIGHT);
for (i = 0; i < 77; i++)
Print(L"%c", BOXDRAW_HORIZONTAL);
Print(L"%c\n\n", BOXDRAW_UP_LEFT);
DefText();
}

/*
* Application entry-point
* NB: This must be set to 'efi_main' for gnu-efi crt0 compatibility
Expand Down Expand Up @@ -173,15 +219,20 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable
#endif
MainImageHandle = ImageHandle;

// The platform logo may still be displayed → remove it
gST->ConOut->ClearScreen(gST->ConOut);

Print(L"\n*** UEFI:NTFS %s (%s) ***\n\n", VERSION_STRING, Arch);
DisplayBanner();
PrintSystemInfo();
SecureBootStatus = GetSecureBootStatus();
PrintInfo(L"Secure Boot status: %s",
(SecureBootStatus > 0) ? L"Enabled" :
((SecureBootStatus < 0) ? L"Setup" : L"Disabled"));
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();
}

Status = gBS->OpenProtocol(MainImageHandle, &gEfiLoadedImageProtocolGuid,
(VOID**)&LoadedImage, MainImageHandle, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
Expand Down Expand Up @@ -410,7 +461,9 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable

// Wait for a keystroke on error
if (EFI_ERROR(Status)) {
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);
}
Expand Down
29 changes: 26 additions & 3 deletions boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,38 @@
/* FreePool() replacement, that NULLs the freed pointer. */
#define SafeFree(p) do { FreePool(p); p = NULL;} while(0)

/* Maximum line size for our banner */
#define BANNER_LINE_SIZE 79

/*
* Console colours we will be using
*/
#define TEXT_DEFAULT EFI_TEXT_ATTR(EFI_LIGHTGRAY, EFI_BLACK)
#define TEXT_REVERSED EFI_TEXT_ATTR(EFI_BLACK, EFI_LIGHTGRAY)
#define TEXT_YELLOW EFI_TEXT_ATTR(EFI_YELLOW, EFI_BLACK)
#define TEXT_RED EFI_TEXT_ATTR(EFI_LIGHTRED, EFI_BLACK)
#define TEXT_GREEN EFI_TEXT_ATTR(EFI_LIGHTGREEN, EFI_BLACK)
#define TEXT_WHITE EFI_TEXT_ATTR(EFI_WHITE, EFI_BLACK)

/*
* Set and restore the console text colour
*/
#define SetText(attr) gST->ConOut->SetAttribute(gST->ConOut, (attr))
#define DefText() gST->ConOut->SetAttribute(gST->ConOut, TEXT_DEFAULT)

/*
* Convenience macros to print informational, warning or error messages.
*/
#define PrintInfo(fmt, ...) Print(L"[INFO] " fmt L"\n", ##__VA_ARGS__)
#define PrintWarning(fmt, ...) Print(L"[WARN] " fmt L"\n", ##__VA_ARGS__)
#define PrintError(fmt, ...) Print(L"[FAIL] " fmt L": [%d] %r\n", ##__VA_ARGS__, (Status&0x7FFFFFFF), Status)
#define PrintInfo(fmt, ...) do { SetText(TEXT_WHITE); Print(L"[INFO]"); DefText(); \
Print(L" " fmt L"\n", ##__VA_ARGS__); } while(0)
#define PrintWarning(fmt, ...) do { SetText(TEXT_YELLOW); Print(L"[WARN]"); DefText(); \
Print(L" " fmt L"\n", ##__VA_ARGS__); } while(0)
#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 assertion macro */
#define P_ASSERT(f, l, a) if(!(a)) do { Print(L"*** ASSERT FAILED: %a(%d): %a ***\n", f, l, #a); while(1); } while(0)
#define V_ASSERT(a) P_ASSERT(__FILE__, __LINE__, a)

/*
* EDK2 and gnu-efi's CompareGuid() return opposite values for a match!
Expand Down
2 changes: 1 addition & 1 deletion path.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ EFI_STATUS SetPathCase(CONST EFI_FILE_HANDLE Root, CHAR16* Path)

Len = SafeStrLen(Path);
/* The checks above ensure that Len is always >= 1, but just in case... */
P_ASSERT(__FILE__, __LINE__, Len >= 1);
V_ASSERT(Len >= 1);

// Find the last backslash in the path
for (i = Len - 1; (i != 0) && (Path[i] != L'\\'); i--);
Expand Down
2 changes: 1 addition & 1 deletion system.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
static EFI_STATUS GetSystemConfigurationTable(EFI_GUID* TableGuid, VOID** Table)
{
UINTN Index;
P_ASSERT(__FILE__, __LINE__, Table != NULL);
V_ASSERT(Table != NULL);

for (Index = 0; Index < gST->NumberOfTableEntries; Index++) {
if (COMPARE_GUID(TableGuid, &(gST->ConfigurationTable[Index].VendorGuid))) {
Expand Down

0 comments on commit f8fcde2

Please sign in to comment.