15a207
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
15a207
From: Hans de Goede <hdegoede@redhat.com>
15a207
Date: Tue, 6 Mar 2018 17:11:15 +0100
15a207
Subject: [PATCH] EFI: console: Do not set text-mode until we actually need it
15a207
15a207
If we're running with a hidden menu we may never need text mode, so do not
15a207
change the video-mode to text until we actually need it.
15a207
15a207
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
15a207
---
15a207
 grub-core/term/efi/console.c | 65 ++++++++++++++++++++++++++++++--------------
15a207
 1 file changed, 44 insertions(+), 21 deletions(-)
15a207
15a207
diff --git a/grub-core/term/efi/console.c b/grub-core/term/efi/console.c
15a207
index 4840cc59d3f..051633d71e9 100644
15a207
--- a/grub-core/term/efi/console.c
15a207
+++ b/grub-core/term/efi/console.c
15a207
@@ -24,6 +24,11 @@
15a207
 #include <grub/efi/api.h>
15a207
 #include <grub/efi/console.h>
15a207
 
15a207
+static grub_err_t grub_prepare_for_text_output(struct grub_term_output *term);
15a207
+
15a207
+static int text_mode_available = -1;
15a207
+static int text_colorstate = -1;
15a207
+
15a207
 static grub_uint32_t
15a207
 map_char (grub_uint32_t c)
15a207
 {
15a207
@@ -66,14 +71,14 @@ map_char (grub_uint32_t c)
15a207
 }
15a207
 
15a207
 static void
15a207
-grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
15a207
+grub_console_putchar (struct grub_term_output *term,
15a207
 		      const struct grub_unicode_glyph *c)
15a207
 {
15a207
   grub_efi_char16_t str[2 + 30];
15a207
   grub_efi_simple_text_output_interface_t *o;
15a207
   unsigned i, j;
15a207
 
15a207
-  if (grub_efi_is_finished)
15a207
+  if (grub_prepare_for_text_output (term))
15a207
     return;
15a207
 
15a207
   o = grub_efi_system_table->con_out;
15a207
@@ -223,14 +228,15 @@ grub_console_getkey (struct grub_term_input *term)
15a207
 }
15a207
 
15a207
 static struct grub_term_coordinate
15a207
-grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
15a207
+grub_console_getwh (struct grub_term_output *term)
15a207
 {
15a207
   grub_efi_simple_text_output_interface_t *o;
15a207
   grub_efi_uintn_t columns, rows;
15a207
 
15a207
   o = grub_efi_system_table->con_out;
15a207
-  if (grub_efi_is_finished || efi_call_4 (o->query_mode, o, o->mode->mode,
15a207
-					  &columns, &rows) != GRUB_EFI_SUCCESS)
15a207
+  if (grub_prepare_for_text_output (term) != GRUB_ERR_NONE ||
15a207
+      efi_call_4 (o->query_mode, o, o->mode->mode,
15a207
+		  &columns, &rows) != GRUB_EFI_SUCCESS)
15a207
     {
15a207
       /* Why does this fail?  */
15a207
       columns = 80;
15a207
@@ -245,7 +251,7 @@ grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
15a207
 {
15a207
   grub_efi_simple_text_output_interface_t *o;
15a207
 
15a207
-  if (grub_efi_is_finished)
15a207
+  if (grub_efi_is_finished || text_mode_available != 1)
15a207
     return (struct grub_term_coordinate) { 0, 0 };
15a207
 
15a207
   o = grub_efi_system_table->con_out;
15a207
@@ -253,12 +259,12 @@ grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
15a207
 }
15a207
 
15a207
 static void
15a207
-grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
15a207
+grub_console_gotoxy (struct grub_term_output *term,
15a207
 		     struct grub_term_coordinate pos)
15a207
 {
15a207
   grub_efi_simple_text_output_interface_t *o;
15a207
 
15a207
-  if (grub_efi_is_finished)
15a207
+  if (grub_prepare_for_text_output (term))
15a207
     return;
15a207
 
15a207
   o = grub_efi_system_table->con_out;
15a207
@@ -271,7 +277,7 @@ grub_console_cls (struct grub_term_output *term __attribute__ ((unused)))
15a207
   grub_efi_simple_text_output_interface_t *o;
15a207
   grub_efi_int32_t orig_attr;
15a207
 
15a207
-  if (grub_efi_is_finished)
15a207
+  if (grub_efi_is_finished || text_mode_available != 1)
15a207
     return;
15a207
 
15a207
   o = grub_efi_system_table->con_out;
15a207
@@ -291,6 +297,12 @@ grub_console_setcolorstate (struct grub_term_output *term
15a207
   if (grub_efi_is_finished)
15a207
     return;
15a207
 
15a207
+  if (text_mode_available != 1) {
15a207
+    /* Avoid "color_normal" environment writes causing a switch to textmode */
15a207
+    text_colorstate = state;
15a207
+    return;
15a207
+  }
15a207
+
15a207
   o = grub_efi_system_table->con_out;
15a207
 
15a207
   switch (state) {
15a207
@@ -315,7 +327,7 @@ grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
15a207
 {
15a207
   grub_efi_simple_text_output_interface_t *o;
15a207
 
15a207
-  if (grub_efi_is_finished)
15a207
+  if (grub_efi_is_finished || text_mode_available != 1)
15a207
     return;
15a207
 
15a207
   o = grub_efi_system_table->con_out;
15a207
@@ -323,18 +335,38 @@ grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
15a207
 }
15a207
 
15a207
 static grub_err_t
15a207
-grub_efi_console_output_init (struct grub_term_output *term)
15a207
+grub_prepare_for_text_output(struct grub_term_output *term)
15a207
 {
15a207
-  grub_efi_set_text_mode (1);
15a207
+  if (grub_efi_is_finished)
15a207
+    return GRUB_ERR_BAD_DEVICE;
15a207
+
15a207
+  if (text_mode_available != -1)
15a207
+    return text_mode_available ? 0 : GRUB_ERR_BAD_DEVICE;
15a207
+
15a207
+  if (! grub_efi_set_text_mode (1))
15a207
+    {
15a207
+      /* This really should never happen */
15a207
+      grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
15a207
+      text_mode_available = 0;
15a207
+      return GRUB_ERR_BAD_DEVICE;
15a207
+    }
15a207
+
15a207
   grub_console_setcursor (term, 1);
15a207
+  if (text_colorstate != -1)
15a207
+    grub_console_setcolorstate (term, text_colorstate);
15a207
+  text_mode_available = 1;
15a207
   return 0;
15a207
 }
15a207
 
15a207
 static grub_err_t
15a207
 grub_efi_console_output_fini (struct grub_term_output *term)
15a207
 {
15a207
+  if (text_mode_available != 1)
15a207
+    return 0;
15a207
+
15a207
   grub_console_setcursor (term, 0);
15a207
   grub_efi_set_text_mode (0);
15a207
+  text_mode_available = -1;
15a207
   return 0;
15a207
 }
15a207
 
15a207
@@ -348,7 +380,6 @@ static struct grub_term_input grub_console_term_input =
15a207
 static struct grub_term_output grub_console_term_output =
15a207
   {
15a207
     .name = "console",
15a207
-    .init = grub_efi_console_output_init,
15a207
     .fini = grub_efi_console_output_fini,
15a207
     .putchar = grub_console_putchar,
15a207
     .getwh = grub_console_getwh,
15a207
@@ -364,14 +395,6 @@ static struct grub_term_output grub_console_term_output =
15a207
 void
15a207
 grub_console_init (void)
15a207
 {
15a207
-  /* FIXME: it is necessary to consider the case where no console control
15a207
-     is present but the default is already in text mode.  */
15a207
-  if (! grub_efi_set_text_mode (1))
15a207
-    {
15a207
-      grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
15a207
-      return;
15a207
-    }
15a207
-
15a207
   grub_term_register_output ("console", &grub_console_term_output);
15a207
   grub_term_register_input ("console", &grub_console_term_input);
15a207
 }