Blame SOURCES/0129-Make-CTRL-and-ALT-keys-work-as-expected-on-EFI-syste.patch

f731ee
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f731ee
From: Peter Jones <pjones@redhat.com>
f731ee
Date: Mon, 3 Feb 2014 15:21:46 -0500
f731ee
Subject: [PATCH] Make CTRL and ALT keys work as expected on EFI systems
f731ee
 (version 5).
f731ee
f731ee
This is version 4.
f731ee
f731ee
Changes from version 1:
f731ee
- handles SHIFT as a modifier
f731ee
- handles F11 and F12 keys
f731ee
- uses the handle provided by the system table to find our _EX protocol.
f731ee
f731ee
Changes from version 2:
f731ee
- eliminate duplicate keycode translation.
f731ee
f731ee
Changes from version 3:
f731ee
- Do not add the shift modifier for any ascii character between space
f731ee
  (0x20) and DEL (0x7f); the combination of the modifier and many of the
f731ee
  keys causes it not to be recognized at all.  Specifically, if we
f731ee
  include the modifier on any querty punctuation character, i.e.
f731ee
  anything the string "~!@#$%^&*()_+{}|:\"<>?" represents in C, it stops
f731ee
  being recognized whatsoever.
f731ee
f731ee
Changes from version 4:
f731ee
- Always initialize term->data from locate protocol (i.e. make it
f731ee
  unconditional.)
f731ee
f731ee
Signed-off-by: Peter Jones <pjones@redhat.com>
f731ee
---
f731ee
 grub-core/term/efi/console.c | 118 +++++++++++++++++++++++++++++++++++--------
f731ee
 include/grub/efi/api.h       |  65 +++++++++++++++++++++++-
f731ee
 2 files changed, 161 insertions(+), 22 deletions(-)
f731ee
f731ee
diff --git a/grub-core/term/efi/console.c b/grub-core/term/efi/console.c
f731ee
index a37eb841c72..677eab5820f 100644
f731ee
--- a/grub-core/term/efi/console.c
f731ee
+++ b/grub-core/term/efi/console.c
f731ee
@@ -104,26 +104,12 @@ const unsigned efi_codes[] =
f731ee
     GRUB_TERM_KEY_DC, GRUB_TERM_KEY_PPAGE, GRUB_TERM_KEY_NPAGE, GRUB_TERM_KEY_F1,
f731ee
     GRUB_TERM_KEY_F2, GRUB_TERM_KEY_F3, GRUB_TERM_KEY_F4, GRUB_TERM_KEY_F5,
f731ee
     GRUB_TERM_KEY_F6, GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8, GRUB_TERM_KEY_F9,
f731ee
-    GRUB_TERM_KEY_F10, 0, 0, '\e'
f731ee
+    GRUB_TERM_KEY_F10, GRUB_TERM_KEY_F10, GRUB_TERM_KEY_F11, '\e'
f731ee
   };
f731ee
 
f731ee
-
f731ee
 static int
f731ee
-grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
f731ee
+grub_efi_translate_key (grub_efi_input_key_t key)
f731ee
 {
f731ee
-  grub_efi_simple_input_interface_t *i;
f731ee
-  grub_efi_input_key_t key;
f731ee
-  grub_efi_status_t status;
f731ee
-
f731ee
-  if (grub_efi_is_finished)
f731ee
-    return 0;
f731ee
-
f731ee
-  i = grub_efi_system_table->con_in;
f731ee
-  status = efi_call_2 (i->read_key_stroke, i, &key);
f731ee
-
f731ee
-  if (status != GRUB_EFI_SUCCESS)
f731ee
-    return GRUB_TERM_NO_KEY;
f731ee
-
f731ee
   if (key.scan_code == 0)
f731ee
     {
f731ee
       /* Some firmware implementations use VT100-style codes against the spec.
f731ee
@@ -139,9 +125,98 @@ grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
f731ee
   else if (key.scan_code < ARRAY_SIZE (efi_codes))
f731ee
     return efi_codes[key.scan_code];
f731ee
 
f731ee
+  if (key.unicode_char >= 0x20 && key.unicode_char <= 0x7f)
f731ee
+    return key.unicode_char;
f731ee
+
f731ee
   return GRUB_TERM_NO_KEY;
f731ee
 }
f731ee
 
f731ee
+static int
f731ee
+grub_console_getkey_con (struct grub_term_input *term __attribute__ ((unused)))
f731ee
+{
f731ee
+  grub_efi_simple_input_interface_t *i;
f731ee
+  grub_efi_input_key_t key;
f731ee
+  grub_efi_status_t status;
f731ee
+
f731ee
+  i = grub_efi_system_table->con_in;
f731ee
+  status = efi_call_2 (i->read_key_stroke, i, &key);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return GRUB_TERM_NO_KEY;
f731ee
+
f731ee
+  return grub_efi_translate_key(key);
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_console_getkey_ex(struct grub_term_input *term)
f731ee
+{
f731ee
+  grub_efi_key_data_t key_data;
f731ee
+  grub_efi_status_t status;
f731ee
+  grub_efi_uint32_t kss;
f731ee
+  int key = -1;
f731ee
+
f731ee
+  grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
f731ee
+
f731ee
+  status = efi_call_2 (text_input->read_key_stroke, text_input, &key_data);
f731ee
+
f731ee
+  if (status != GRUB_EFI_SUCCESS)
f731ee
+    return GRUB_TERM_NO_KEY;
f731ee
+
f731ee
+  kss = key_data.key_state.key_shift_state;
f731ee
+  key = grub_efi_translate_key(key_data.key);
f731ee
+
f731ee
+  if (key == GRUB_TERM_NO_KEY)
f731ee
+    return GRUB_TERM_NO_KEY;
f731ee
+
f731ee
+  if (kss & GRUB_EFI_SHIFT_STATE_VALID)
f731ee
+    {
f731ee
+      if ((kss & GRUB_EFI_LEFT_SHIFT_PRESSED
f731ee
+	   || kss & GRUB_EFI_RIGHT_SHIFT_PRESSED)
f731ee
+	  && !(key >= 0x20 && key <= 0x7f))
f731ee
+	key |= GRUB_TERM_SHIFT;
f731ee
+      if (kss & GRUB_EFI_LEFT_ALT_PRESSED || kss & GRUB_EFI_RIGHT_ALT_PRESSED)
f731ee
+	key |= GRUB_TERM_ALT;
f731ee
+      if (kss & GRUB_EFI_LEFT_CONTROL_PRESSED
f731ee
+	  || kss & GRUB_EFI_RIGHT_CONTROL_PRESSED)
f731ee
+	key |= GRUB_TERM_CTRL;
f731ee
+    }
f731ee
+
f731ee
+  return key;
f731ee
+}
f731ee
+
f731ee
+static grub_err_t
f731ee
+grub_efi_console_input_init (struct grub_term_input *term)
f731ee
+{
f731ee
+  grub_efi_guid_t text_input_ex_guid =
f731ee
+    GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
f731ee
+
f731ee
+  if (grub_efi_is_finished)
f731ee
+    return 0;
f731ee
+
f731ee
+  grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
f731ee
+  if (text_input)
f731ee
+    return 0;
f731ee
+
f731ee
+  text_input = grub_efi_open_protocol(grub_efi_system_table->console_in_handler,
f731ee
+				      &text_input_ex_guid,
f731ee
+				      GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
f731ee
+  term->data = (void *)text_input;
f731ee
+
f731ee
+  return 0;
f731ee
+}
f731ee
+
f731ee
+static int
f731ee
+grub_console_getkey (struct grub_term_input *term)
f731ee
+{
f731ee
+  if (grub_efi_is_finished)
f731ee
+    return 0;
f731ee
+
f731ee
+  if (term->data)
f731ee
+    return grub_console_getkey_ex(term);
f731ee
+  else
f731ee
+    return grub_console_getkey_con(term);
f731ee
+}
f731ee
+
f731ee
 static struct grub_term_coordinate
f731ee
 grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
f731ee
 {
f731ee
@@ -243,7 +318,7 @@ grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
f731ee
 }
f731ee
 
f731ee
 static grub_err_t
f731ee
-grub_efi_console_init (struct grub_term_output *term)
f731ee
+grub_efi_console_output_init (struct grub_term_output *term)
f731ee
 {
f731ee
   grub_efi_set_text_mode (1);
f731ee
   grub_console_setcursor (term, 1);
f731ee
@@ -251,7 +326,7 @@ grub_efi_console_init (struct grub_term_output *term)
f731ee
 }
f731ee
 
f731ee
 static grub_err_t
f731ee
-grub_efi_console_fini (struct grub_term_output *term)
f731ee
+grub_efi_console_output_fini (struct grub_term_output *term)
f731ee
 {
f731ee
   grub_console_setcursor (term, 0);
f731ee
   grub_efi_set_text_mode (0);
f731ee
@@ -262,13 +337,14 @@ static struct grub_term_input grub_console_term_input =
f731ee
   {
f731ee
     .name = "console",
f731ee
     .getkey = grub_console_getkey,
f731ee
+    .init = grub_efi_console_input_init,
f731ee
   };
f731ee
 
f731ee
 static struct grub_term_output grub_console_term_output =
f731ee
   {
f731ee
     .name = "console",
f731ee
-    .init = grub_efi_console_init,
f731ee
-    .fini = grub_efi_console_fini,
f731ee
+    .init = grub_efi_console_output_init,
f731ee
+    .fini = grub_efi_console_output_fini,
f731ee
     .putchar = grub_console_putchar,
f731ee
     .getwh = grub_console_getwh,
f731ee
     .getxy = grub_console_getxy,
f731ee
@@ -291,8 +367,8 @@ grub_console_init (void)
f731ee
       return;
f731ee
     }
f731ee
 
f731ee
-  grub_term_register_input ("console", &grub_console_term_input);
f731ee
   grub_term_register_output ("console", &grub_console_term_output);
f731ee
+  grub_term_register_input ("console", &grub_console_term_input);
f731ee
 }
f731ee
 
f731ee
 void
f731ee
diff --git a/include/grub/efi/api.h b/include/grub/efi/api.h
f731ee
index e5dd543a801..142340372e1 100644
f731ee
--- a/include/grub/efi/api.h
f731ee
+++ b/include/grub/efi/api.h
f731ee
@@ -111,7 +111,7 @@
f731ee
     { 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b } \
f731ee
   }
f731ee
 
f731ee
-#define EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \
f731ee
+#define GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID \
f731ee
   { 0xdd9e7534, 0x7762, 0x4698, \
f731ee
     { 0x8c, 0x14, 0xf5, 0x85, 0x17, 0xa6, 0x25, 0xaa } \
f731ee
   }
f731ee
@@ -952,6 +952,32 @@ struct grub_efi_input_key
f731ee
 };
f731ee
 typedef struct grub_efi_input_key grub_efi_input_key_t;
f731ee
 
f731ee
+typedef grub_efi_uint8_t grub_efi_key_toggle_state_t;
f731ee
+struct grub_efi_key_state
f731ee
+{
f731ee
+	grub_efi_uint32_t key_shift_state;
f731ee
+	grub_efi_key_toggle_state_t key_toggle_state;
f731ee
+};
f731ee
+typedef struct grub_efi_key_state grub_efi_key_state_t;
f731ee
+
f731ee
+#define GRUB_EFI_SHIFT_STATE_VALID     0x80000000
f731ee
+#define GRUB_EFI_RIGHT_SHIFT_PRESSED   0x00000001
f731ee
+#define GRUB_EFI_LEFT_SHIFT_PRESSED    0x00000002
f731ee
+#define GRUB_EFI_RIGHT_CONTROL_PRESSED 0x00000004
f731ee
+#define GRUB_EFI_LEFT_CONTROL_PRESSED  0x00000008
f731ee
+#define GRUB_EFI_RIGHT_ALT_PRESSED     0x00000010
f731ee
+#define GRUB_EFI_LEFT_ALT_PRESSED      0x00000020
f731ee
+#define GRUB_EFI_RIGHT_LOGO_PRESSED    0x00000040
f731ee
+#define GRUB_EFI_LEFT_LOGO_PRESSED     0x00000080
f731ee
+#define GRUB_EFI_MENU_KEY_PRESSED      0x00000100
f731ee
+#define GRUB_EFI_SYS_REQ_PRESSED       0x00000200
f731ee
+
f731ee
+#define GRUB_EFI_TOGGLE_STATE_VALID 0x80
f731ee
+#define GRUB_EFI_KEY_STATE_EXPOSED  0x40
f731ee
+#define GRUB_EFI_SCROLL_LOCK_ACTIVE 0x01
f731ee
+#define GRUB_EFI_NUM_LOCK_ACTIVE    0x02
f731ee
+#define GRUB_EFI_CAPS_LOCK_ACTIVE   0x04
f731ee
+
f731ee
 struct grub_efi_simple_text_output_mode
f731ee
 {
f731ee
   grub_efi_int32_t max_mode;
f731ee
@@ -1294,6 +1320,43 @@ struct grub_efi_simple_input_interface
f731ee
 };
f731ee
 typedef struct grub_efi_simple_input_interface grub_efi_simple_input_interface_t;
f731ee
 
f731ee
+struct grub_efi_key_data {
f731ee
+	grub_efi_input_key_t key;
f731ee
+	grub_efi_key_state_t key_state;
f731ee
+};
f731ee
+typedef struct grub_efi_key_data grub_efi_key_data_t;
f731ee
+
f731ee
+typedef grub_efi_status_t (*grub_efi_key_notify_function_t) (
f731ee
+	grub_efi_key_data_t *key_data
f731ee
+	);
f731ee
+
f731ee
+struct grub_efi_simple_text_input_ex_interface
f731ee
+{
f731ee
+	grub_efi_status_t
f731ee
+	(*reset) (struct grub_efi_simple_text_input_ex_interface *this,
f731ee
+		  grub_efi_boolean_t extended_verification);
f731ee
+
f731ee
+	grub_efi_status_t
f731ee
+	(*read_key_stroke) (struct grub_efi_simple_text_input_ex_interface *this,
f731ee
+			    grub_efi_key_data_t *key_data);
f731ee
+
f731ee
+	grub_efi_event_t wait_for_key;
f731ee
+
f731ee
+	grub_efi_status_t
f731ee
+	(*set_state) (struct grub_efi_simple_text_input_ex_interface *this,
f731ee
+		      grub_efi_key_toggle_state_t *key_toggle_state);
f731ee
+
f731ee
+	grub_efi_status_t
f731ee
+	(*register_key_notify) (struct grub_efi_simple_text_input_ex_interface *this,
f731ee
+				grub_efi_key_data_t *key_data,
f731ee
+				grub_efi_key_notify_function_t key_notification_function);
f731ee
+
f731ee
+	grub_efi_status_t
f731ee
+	(*unregister_key_notify) (struct grub_efi_simple_text_input_ex_interface *this,
f731ee
+				  void *notification_handle);
f731ee
+};
f731ee
+typedef struct grub_efi_simple_text_input_ex_interface grub_efi_simple_text_input_ex_interface_t;
f731ee
+
f731ee
 struct grub_efi_simple_text_output_interface
f731ee
 {
f731ee
   grub_efi_status_t