File shim-mokmanager-handle-keystroke-error.patch of Package shim

From 7edfcaa6e03c8aa1ce39e8eb193d2064f4119342 Mon Sep 17 00:00:00 2001
From: Gary Ching-Pang Lin <glin@suse.com>
Date: Tue, 3 Dec 2013 15:52:02 +0800
Subject: [PATCH] MokManager: handle the error status from ReadKeyStroke

On some machines, even though the key event was signaled, ReadKeyStroke
still got EFI_NOT_READY. This commit handles the error status to avoid
console_get_keystroke from returning unexpected keys.

Signed-off-by: Gary Ching-Pang Lin <glin@suse.com>
---
 MokManager.c      | 17 +++++++++++++----
 include/console.h |  4 ++--
 lib/console.c     | 26 ++++++++++++++++++--------
 3 files changed, 33 insertions(+), 14 deletions(-)

Index: shim-0.7/MokManager.c
===================================================================
--- shim-0.7.orig/MokManager.c
+++ shim-0.7/MokManager.c
@@ -548,13 +548,19 @@ static EFI_STATUS list_keys (void *KeyLi
 	return EFI_SUCCESS;
 }
 
-static UINT8 get_line (UINT32 *length, CHAR16 *line, UINT32 line_max, UINT8 show)
+static EFI_STATUS get_line (UINT32 *length, CHAR16 *line, UINT32 line_max, UINT8 show)
 {
 	EFI_INPUT_KEY key;
+	EFI_STATUS status;
 	int count = 0;
 
 	do {
-		key = console_get_keystroke();
+		status = console_get_keystroke(&key);
+		if (EFI_ERROR (status)) {
+			console_error(L"Failed to read the keystroke", status);
+			*length = 0;
+			return status;
+		}
 
 		if ((count >= line_max &&
 		     key.UnicodeChar != CHAR_BACKSPACE) ||
@@ -585,7 +591,7 @@ static UINT8 get_line (UINT32 *length, C
 
 	*length = count;
 
-	return 1;
+	return EFI_SUCCESS;
 }
 
 static EFI_STATUS compute_pw_hash (void *Data, UINTN DataSize, UINT8 *password,
@@ -1238,6 +1244,7 @@ static INTN mok_deletion_prompt (void *M
 static CHAR16 get_password_charater (CHAR16 *prompt)
 {
 	SIMPLE_TEXT_OUTPUT_MODE SavedMode;
+	EFI_STATUS status;
 	CHAR16 *message[2];
 	CHAR16 character;
 	UINTN length;
@@ -1252,7 +1259,9 @@ static CHAR16 get_password_charater (CHA
 	message[1] = NULL;
 	length = StrLen(message[0]);
 	console_print_box_at(message, -1, -length-4, -5, length+4, 3, 0, 1);
-	get_line(&pw_length, &character, 1, 0);
+	status = get_line(&pw_length, &character, 1, 0);
+	if (EFI_ERROR(status))
+		character = 0;
 
 	console_restore_mode(&SavedMode);
 
Index: shim-0.7/include/console.h
===================================================================
--- shim-0.7.orig/include/console.h
+++ shim-0.7/include/console.h
@@ -1,8 +1,8 @@
 #ifndef _SHIM_LIB_CONSOLE_H
 #define _SHIM_LIB_CONSOLE_H 1
 
-EFI_INPUT_KEY
-console_get_keystroke(void);
+EFI_STATUS
+console_get_keystroke(EFI_INPUT_KEY *key);
 void
 console_print_box_at(CHAR16 *str_arr[], int highlight, int start_col, int start_row, int size_cols, int size_rows, int offset, int lines);
 void
Index: shim-0.7/lib/console.c
===================================================================
--- shim-0.7.orig/lib/console.c
+++ shim-0.7/lib/console.c
@@ -40,16 +40,18 @@ SetMem16(CHAR16 *dst, UINT32 n, CHAR16 c
 	}
 }
 
-EFI_INPUT_KEY
-console_get_keystroke(void)
+EFI_STATUS
+console_get_keystroke(EFI_INPUT_KEY *key)
 {
-	EFI_INPUT_KEY key;
 	UINTN EventIndex;
+	EFI_STATUS status;
 
-	uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &EventIndex);
-	uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
+	do {
+		uefi_call_wrapper(BS->WaitForEvent, 3, 1, &ST->ConIn->WaitForKey, &EventIndex);
+		status = uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, key);
+	} while (status == EFI_NOT_READY);
 
-	return key;
+	return status;
 }
 
 void
@@ -162,6 +164,8 @@ console_print_box(CHAR16 *str_arr[], int
 {
 	SIMPLE_TEXT_OUTPUT_MODE SavedConsoleMode;
 	SIMPLE_TEXT_OUTPUT_INTERFACE *co = ST->ConOut;
+	EFI_INPUT_KEY key;
+
 	CopyMem(&SavedConsoleMode, co->Mode, sizeof(SavedConsoleMode));
 	uefi_call_wrapper(co->EnableCursor, 2, co, FALSE);
 	uefi_call_wrapper(co->SetAttribute, 2, co, EFI_LIGHTGRAY | EFI_BACKGROUND_BLUE);
@@ -169,7 +173,7 @@ console_print_box(CHAR16 *str_arr[], int
 	console_print_box_at(str_arr, highlight, 0, 0, -1, -1, 0,
 			     count_lines(str_arr));
 
-	console_get_keystroke();
+	console_get_keystroke(&key);
 
 	uefi_call_wrapper(co->EnableCursor, 2, co, SavedConsoleMode.CursorVisible);
 
@@ -184,6 +188,7 @@ console_select(CHAR16 *title[], CHAR16*
 	SIMPLE_TEXT_OUTPUT_MODE SavedConsoleMode;
 	SIMPLE_TEXT_OUTPUT_INTERFACE *co = ST->ConOut;
 	EFI_INPUT_KEY k;
+	EFI_STATUS status;
 	int selector;
 	int selector_lines = count_lines(selectors);
 	int selector_max_cols = 0;
@@ -237,7 +242,12 @@ console_select(CHAR16 *title[], CHAR16*
 			     size_cols, size_rows, 0, lines);
 
 	do {
-		k = console_get_keystroke();
+		status = console_get_keystroke(&k);
+		if (EFI_ERROR (status)) {
+			Print(L"Failed to read the keystroke: %r", status);
+			selector = -1;
+			break;
+		}
 
 		if (k.ScanCode == SCAN_ESC) {
 			selector = -1;
openSUSE Build Service is sponsored by