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

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