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

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