Blame SOURCES/0039-MokManager-console-mode-modification-for-hi-dpi-scre.patch

00e791
From 55163bc82c5179adb109c3d8b982c2689d68b4c9 Mon Sep 17 00:00:00 2001
00e791
From: Ivan Hu <ivan.hu@canonical.com>
00e791
Date: Fri, 10 May 2019 17:50:12 +0800
00e791
Subject: [PATCH 39/62] MokManager: console mode modification for hi-dpi screen
00e791
 devices
00e791
00e791
There are lots of hi-dpi laptops nowadays, as doing mok enrollment, the font
00e791
is too small to see.
00e791
https://bugs.launchpad.net/ubuntu/+source/shim/+bug/1822043
00e791
00e791
This patch checks if the resolution is larger than Full HD (1920x1080) and
00e791
current console output columns and rows is in a good mode. Then swith the
00e791
console output to a better mode.
00e791
00e791
Signed-off-by: Ivan Hu <ivan.hu@canonical.com>
00e791
Upstream-commit-id: cf05af6d899
00e791
---
00e791
 MokManager.c      |   2 +
00e791
 lib/console.c     | 161 +++++++++++++++++++++++++++++++++++++++++++++-
00e791
 include/console.h |   2 +
00e791
 3 files changed, 164 insertions(+), 1 deletion(-)
00e791
00e791
diff --git a/MokManager.c b/MokManager.c
00e791
index 30192c16789..78da9fd95ee 100644
00e791
--- a/MokManager.c
00e791
+++ b/MokManager.c
00e791
@@ -2560,6 +2560,8 @@ EFI_STATUS efi_main(EFI_HANDLE image_handle, EFI_SYSTEM_TABLE * systab)
00e791
 
00e791
 	setup_rand();
00e791
 
00e791
+	console_mode_handle();
00e791
+
00e791
 	efi_status = check_mok_request(image_handle);
00e791
 
00e791
 	console_fini();
00e791
diff --git a/lib/console.c b/lib/console.c
00e791
index 3aee41cd276..c92d27f3c86 100644
00e791
--- a/lib/console.c
00e791
+++ b/lib/console.c
00e791
@@ -409,7 +409,166 @@ console_notify(CHAR16 *string)
00e791
 	console_alertbox(str_arr);
00e791
 }
00e791
 
00e791
-#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
00e791
+void
00e791
+console_save_and_set_mode(SIMPLE_TEXT_OUTPUT_MODE * SavedMode)
00e791
+{
00e791
+	SIMPLE_TEXT_OUTPUT_INTERFACE *co = ST->ConOut;
00e791
+
00e791
+	if (!SavedMode) {
00e791
+		console_print(L"Invalid parameter: SavedMode\n");
00e791
+		return;
00e791
+	}
00e791
+
00e791
+	CopyMem(SavedMode, co->Mode, sizeof(SIMPLE_TEXT_OUTPUT_MODE));
00e791
+	co->EnableCursor(co, FALSE);
00e791
+	co->SetAttribute(co, EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE);
00e791
+}
00e791
+
00e791
+void
00e791
+console_restore_mode(SIMPLE_TEXT_OUTPUT_MODE * SavedMode)
00e791
+{
00e791
+	SIMPLE_TEXT_OUTPUT_INTERFACE *co = ST->ConOut;
00e791
+
00e791
+	co->EnableCursor(co, SavedMode->CursorVisible);
00e791
+	co->SetCursorPosition(co, SavedMode->CursorColumn,
00e791
+				SavedMode->CursorRow);
00e791
+	co->SetAttribute(co, SavedMode->Attribute);
00e791
+}
00e791
+
00e791
+int
00e791
+console_countdown(CHAR16* title, const CHAR16* message, int timeout)
00e791
+{
00e791
+	SIMPLE_TEXT_OUTPUT_INTERFACE *co = ST->ConOut;
00e791
+	SIMPLE_INPUT_INTERFACE *ci = ST->ConIn;
00e791
+	SIMPLE_TEXT_OUTPUT_MODE SavedMode;
00e791
+	EFI_INPUT_KEY key;
00e791
+	EFI_STATUS efi_status;
00e791
+	UINTN cols, rows;
00e791
+	CHAR16 *titles[2];
00e791
+	int wait = 10000000;
00e791
+
00e791
+	console_save_and_set_mode(&SavedMode);
00e791
+
00e791
+	titles[0] = title;
00e791
+	titles[1] = NULL;
00e791
+
00e791
+	console_print_box_at(titles, -1, 0, 0, -1, -1, 1, 1);
00e791
+
00e791
+	co->QueryMode(co, co->Mode->Mode, &cols, &rows);
00e791
+
00e791
+	console_print_at((cols - StrLen(message)) / 2, rows / 2, message);
00e791
+	while (1) {
00e791
+		if (timeout > 1)
00e791
+			console_print_at(2, rows - 3,
00e791
+					 L"Booting in %d seconds  ",
00e791
+					 timeout);
00e791
+		else if (timeout)
00e791
+			console_print_at(2, rows - 3,
00e791
+					 L"Booting in %d second   ",
00e791
+					 timeout);
00e791
+
00e791
+		efi_status = WaitForSingleEvent(ci->WaitForKey, wait);
00e791
+		if (efi_status != EFI_TIMEOUT) {
00e791
+			/* Clear the key in the queue */
00e791
+			ci->ReadKeyStroke(ci, &key);
00e791
+			break;
00e791
+		}
00e791
+
00e791
+		timeout--;
00e791
+		if (!timeout)
00e791
+			break;
00e791
+	}
00e791
+
00e791
+	console_restore_mode(&SavedMode);
00e791
+
00e791
+	return timeout;
00e791
+}
00e791
+
00e791
+#define HORIZONTAL_MAX_OK 1920
00e791
+#define VERTICAL_MAX_OK 1080
00e791
+#define COLUMNS_MAX_OK 200
00e791
+#define ROWS_MAX_OK 100
00e791
+
00e791
+void
00e791
+console_mode_handle(VOID)
00e791
+{
00e791
+	SIMPLE_TEXT_OUTPUT_INTERFACE *co = ST->ConOut;
00e791
+	EFI_GRAPHICS_OUTPUT_PROTOCOL *gop;
00e791
+	EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
00e791
+	EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *Info;
00e791
+
00e791
+	UINTN mode_set;
00e791
+	UINTN rows = 0, columns = 0;
00e791
+	EFI_STATUS efi_status = EFI_SUCCESS;
00e791
+
00e791
+	efi_status = gBS->LocateProtocol(&gop_guid, NULL, (void **)&gop);
00e791
+	if (EFI_ERROR(efi_status)) {
00e791
+		console_error(L"Locate graphic output protocol fail", efi_status);
00e791
+		return;
00e791
+	}
00e791
+
00e791
+	Info = gop->Mode->Info;
00e791
+
00e791
+	/*
00e791
+	 * Start verifying if we are in a resolution larger than Full HD
00e791
+	 * (1920x1080). If we're not, assume we're in a good mode and do not
00e791
+	 * try to change it.
00e791
+	 */
00e791
+	if (Info->HorizontalResolution <= HORIZONTAL_MAX_OK &&
00e791
+	    Info->VerticalResolution <= VERTICAL_MAX_OK) {
00e791
+		/* keep original mode and return */
00e791
+		return;
00e791
+	}
00e791
+
00e791
+        efi_status = co->QueryMode(co, co->Mode->Mode, &columns, &rows);
00e791
+	if (EFI_ERROR(efi_status)) {
00e791
+		console_error(L"Console query mode fail", efi_status);
00e791
+		return;
00e791
+	}
00e791
+
00e791
+	/*
00e791
+	 * Verify current console output to check if the character columns and
00e791
+	 * rows in a good mode.
00e791
+	 */
00e791
+	if (columns <= COLUMNS_MAX_OK && rows <= ROWS_MAX_OK) {
00e791
+		/* keep original mode and return */
00e791
+		return;
00e791
+	}
00e791
+
00e791
+	if (!console_text_mode)
00e791
+		setup_console(1);
00e791
+
00e791
+	co->Reset(co, TRUE);
00e791
+
00e791
+	/*
00e791
+	 * If we reached here, then we have a high resolution screen and the
00e791
+	 * text too small. Try to switch to a better mode. Mode number 2 is
00e791
+	 * first non standard mode, which is provided by the device
00e791
+	 * manufacturer, so it should be a good mode.
00e791
+	 */
00e791
+	if (co->Mode->MaxMode > 2)
00e791
+		mode_set = 2;
00e791
+	else
00e791
+		mode_set = 0;
00e791
+
00e791
+	efi_status = co->SetMode(co, mode_set);
00e791
+	if (EFI_ERROR(efi_status) && mode_set != 0) {
00e791
+		/*
00e791
+		 * Set to 0 mode which is required that all output devices
00e791
+		 * support at least 80x25 text mode.
00e791
+		 */
00e791
+		mode_set = 0;
00e791
+		efi_status = co->SetMode(co, mode_set);
00e791
+	}
00e791
+
00e791
+	co->ClearScreen(co);
00e791
+
00e791
+	if (EFI_ERROR(efi_status)) {
00e791
+		console_error(L"Console set mode fail", efi_status);
00e791
+	}
00e791
+
00e791
+	return;
00e791
+}
00e791
 
00e791
 /* Copy of gnu-efi-3.0 with the added secure boot strings */
00e791
 static struct {
00e791
diff --git a/include/console.h b/include/console.h
00e791
index deb4fa3db23..9f259c71b72 100644
00e791
--- a/include/console.h
00e791
+++ b/include/console.h
00e791
@@ -34,6 +34,8 @@ void
00e791
 console_notify(CHAR16 *string);
00e791
 void
00e791
 console_reset(void);
00e791
+void
00e791
+console_mode_handle(void);
00e791
 #define NOSEL 0x7fffffff
00e791
 
00e791
 typedef struct _EFI_CONSOLE_CONTROL_PROTOCOL   EFI_CONSOLE_CONTROL_PROTOCOL;
00e791
-- 
00e791
2.26.2
00e791