]> git.ipfire.org Git - thirdparty/u-boot.git/blobdiff - lib/efi_loader/efi_console.c
efi_loader: fix SetAttribute()
[thirdparty/u-boot.git] / lib / efi_loader / efi_console.c
index 7ecdbb166695c376d028d0651bb4d420b30b0831..706e6ad31eaee39210cd04b636d53eb9446ace56 100644 (file)
@@ -62,6 +62,21 @@ static struct simple_text_output_mode efi_con_mode = {
        .cursor_visible = 1,
 };
 
+static int term_get_char(s32 *c)
+{
+       u64 timeout;
+
+       /* Wait up to 100 ms for a character */
+       timeout = timer_get_us() + 100000;
+
+       while (!tstc())
+               if (timer_get_us() > timeout)
+                       return 1;
+
+       *c = getc();
+       return 0;
+}
+
 /*
  * Receive and parse a reply from the terminal.
  *
@@ -72,34 +87,36 @@ static struct simple_text_output_mode efi_con_mode = {
  */
 static int term_read_reply(int *n, int num, char end_char)
 {
-       char c;
+       s32 c;
        int i = 0;
 
-       c = getc();
-       if (c != cESC)
+       if (term_get_char(&c) || c != cESC)
                return -1;
-       c = getc();
-       if (c != '[')
+
+       if (term_get_char(&c) || c != '[')
                return -1;
 
        n[0] = 0;
        while (1) {
-               c = getc();
-               if (c == ';') {
-                       i++;
-                       if (i >= num)
+               if (!term_get_char(&c)) {
+                       if (c == ';') {
+                               i++;
+                               if (i >= num)
+                                       return -1;
+                               n[i] = 0;
+                               continue;
+                       } else if (c == end_char) {
+                               break;
+                       } else if (c > '9' || c < '0') {
                                return -1;
-                       n[i] = 0;
-                       continue;
-               } else if (c == end_char) {
-                       break;
-               } else if (c > '9' || c < '0') {
+                       }
+
+                       /* Read one more decimal position */
+                       n[i] *= 10;
+                       n[i] += c - '0';
+               } else {
                        return -1;
                }
-
-               /* Read one more decimal position */
-               n[i] *= 10;
-               n[i] += c - '0';
        }
        if (i != num - 1)
                return -1;
@@ -119,6 +136,11 @@ static efi_status_t EFIAPI efi_cout_output_string(
 
        EFI_ENTRY("%p, %p", this, string);
 
+       if (!this || !string) {
+               ret = EFI_INVALID_PARAMETER;
+               goto out;
+       }
+
        buf = malloc(utf16_utf8_strlen(string) + 1);
        if (!buf) {
                ret = EFI_OUT_OF_RESOURCES;
@@ -196,7 +218,6 @@ static int query_console_serial(int *rows, int *cols)
 {
        int ret = 0;
        int n[2];
-       u64 timeout;
 
        /* Empty input buffer */
        while (tstc())
@@ -205,7 +226,7 @@ static int query_console_serial(int *rows, int *cols)
        /*
         * Not all terminals understand CSI [18t for querying the console size.
         * We should adhere to escape sequences documented in the console_codes
-        * manpage and the ECMA-48 standard.
+        * man page and the ECMA-48 standard.
         *
         * So here we follow a different approach. We position the cursor to the
         * bottom right and query its position. Before leaving the function we
@@ -216,14 +237,6 @@ static int query_console_serial(int *rows, int *cols)
               ESC "[999;999H"  /* Move to bottom right corner */
               ESC "[6n");      /* Query cursor position */
 
-       /* Allow up to one second for a response */
-       timeout = timer_get_us() + 1000000;
-       while (!tstc())
-               if (timer_get_us() > timeout) {
-                       ret = -1;
-                       goto out;
-               }
-
        /* Read {rows,cols} */
        if (term_read_reply(n, 2, 'R')) {
                ret = 1;
@@ -303,23 +316,6 @@ static efi_status_t EFIAPI efi_cout_query_mode(
        return EFI_EXIT(EFI_SUCCESS);
 }
 
-static efi_status_t EFIAPI efi_cout_set_mode(
-                       struct efi_simple_text_output_protocol *this,
-                       unsigned long mode_number)
-{
-       EFI_ENTRY("%p, %ld", this, mode_number);
-
-
-       if (mode_number > efi_con_mode.max_mode)
-               return EFI_EXIT(EFI_UNSUPPORTED);
-
-       efi_con_mode.mode = mode_number;
-       efi_con_mode.cursor_column = 0;
-       efi_con_mode.cursor_row = 0;
-
-       return EFI_EXIT(EFI_SUCCESS);
-}
-
 static const struct {
        unsigned int fg;
        unsigned int bg;
@@ -345,6 +341,7 @@ static efi_status_t EFIAPI efi_cout_set_attribute(
 
        EFI_ENTRY("%p, %lx", this, attribute);
 
+       efi_con_mode.attribute = attribute;
        if (attribute)
                printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
        else
@@ -365,6 +362,20 @@ static efi_status_t EFIAPI efi_cout_clear_screen(
        return EFI_EXIT(EFI_SUCCESS);
 }
 
+static efi_status_t EFIAPI efi_cout_set_mode(
+                       struct efi_simple_text_output_protocol *this,
+                       unsigned long mode_number)
+{
+       EFI_ENTRY("%p, %ld", this, mode_number);
+
+       if (mode_number >= efi_con_mode.max_mode)
+               return EFI_EXIT(EFI_UNSUPPORTED);
+       efi_con_mode.mode = mode_number;
+       EFI_CALL(efi_cout_clear_screen(this));
+
+       return EFI_EXIT(EFI_SUCCESS);
+}
+
 static efi_status_t EFIAPI efi_cout_reset(
                        struct efi_simple_text_output_protocol *this,
                        char extended_verification)
@@ -374,6 +385,7 @@ static efi_status_t EFIAPI efi_cout_reset(
        /* Clear screen */
        EFI_CALL(efi_cout_clear_screen(this));
        /* Set default colors */
+       efi_con_mode.attribute = 0x07;
        printf(ESC "[0;37;40m");
 
        return EFI_EXIT(EFI_SUCCESS);
@@ -417,6 +429,7 @@ static efi_status_t EFIAPI efi_cout_enable_cursor(
        EFI_ENTRY("%p, %d", this, enable);
 
        printf(ESC"[?25%c", enable ? 'h' : 'l');
+       efi_con_mode.cursor_visible = !!enable;
 
        return EFI_EXIT(EFI_SUCCESS);
 }
@@ -480,7 +493,7 @@ void set_shift_mask(int mod, struct efi_key_state *key_state)
  *
  * This gets called when we have already parsed CSI.
  *
- * @modifiers:  bitmask (shift, alt, ctrl)
+ * @modifiers:  bit mask (shift, alt, ctrl)
  * @return:    the unmodified code
  */
 static int analyze_modifiers(struct efi_key_state *key_state)
@@ -690,7 +703,7 @@ static void efi_cin_check(void)
        efi_status_t ret;
 
        if (key_available) {
-               efi_signal_event(efi_con_in.wait_for_key, true);
+               efi_signal_event(efi_con_in.wait_for_key);
                return;
        }
 
@@ -704,7 +717,7 @@ static void efi_cin_check(void)
 
                        /* Queue the wait for key event */
                        if (key_available)
-                               efi_signal_event(efi_con_in.wait_for_key, true);
+                               efi_signal_event(efi_con_in.wait_for_key);
                }
        }
 }
@@ -789,9 +802,26 @@ static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
                ret = EFI_NOT_READY;
                goto out;
        }
+       /*
+        * CTRL+A - CTRL+Z have to be signaled as a - z.
+        * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
+        */
+       switch (next_key.key.unicode_char) {
+       case 0x01 ... 0x07:
+       case 0x0b ... 0x0c:
+       case 0x0e ... 0x1a:
+               if (!(next_key.key_state.key_toggle_state &
+                     EFI_CAPS_LOCK_ACTIVE) ^
+                   !(next_key.key_state.key_shift_state &
+                     (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
+                       next_key.key.unicode_char += 0x40;
+               else
+                       next_key.key.unicode_char += 0x60;
+       }
        *key_data = next_key;
        key_available = false;
        efi_con_in.wait_for_key->is_signaled = false;
+
 out:
        return EFI_EXIT(ret);
 }
@@ -800,7 +830,7 @@ out:
  * efi_cin_set_state() - set toggle key state
  *
  * @this:              instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @key_toggle_state:  key toggle state
+ * @key_toggle_state:  pointer to key toggle state
  * Return:             status code
  *
  * This function implements the SetState service of the
@@ -811,9 +841,9 @@ out:
  */
 static efi_status_t EFIAPI efi_cin_set_state(
                struct efi_simple_text_input_ex_protocol *this,
-               u8 key_toggle_state)
+               u8 *key_toggle_state)
 {
-       EFI_ENTRY("%p, %u", this, key_toggle_state);
+       EFI_ENTRY("%p, %p", this, key_toggle_state);
        /*
         * U-Boot supports multiple console input sources like serial and
         * net console for which a key toggle state cannot be set at all.
@@ -1045,38 +1075,40 @@ static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
  * efi_console_register() - install the console protocols
  *
  * This function is called from do_bootefi_exec().
+ *
+ * Return:     status code
  */
-int efi_console_register(void)
+efi_status_t efi_console_register(void)
 {
        efi_status_t r;
-       struct efi_object *efi_console_output_obj;
-       struct efi_object *efi_console_input_obj;
+       efi_handle_t console_output_handle;
+       efi_handle_t console_input_handle;
 
        /* Set up mode information */
        query_console_size();
 
        /* Create handles */
-       r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
+       r = efi_create_handle(&console_output_handle);
        if (r != EFI_SUCCESS)
                goto out_of_memory;
 
-       r = efi_add_protocol(efi_console_output_obj->handle,
+       r = efi_add_protocol(console_output_handle,
                             &efi_guid_text_output_protocol, &efi_con_out);
        if (r != EFI_SUCCESS)
                goto out_of_memory;
-       systab.con_out_handle = efi_console_output_obj->handle;
-       systab.stderr_handle = efi_console_output_obj->handle;
+       systab.con_out_handle = console_output_handle;
+       systab.stderr_handle = console_output_handle;
 
-       r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
+       r = efi_create_handle(&console_input_handle);
        if (r != EFI_SUCCESS)
                goto out_of_memory;
 
-       r = efi_add_protocol(efi_console_input_obj->handle,
+       r = efi_add_protocol(console_input_handle,
                             &efi_guid_text_input_protocol, &efi_con_in);
        if (r != EFI_SUCCESS)
                goto out_of_memory;
-       systab.con_in_handle = efi_console_input_obj->handle;
-       r = efi_add_protocol(efi_console_input_obj->handle,
+       systab.con_in_handle = console_input_handle;
+       r = efi_add_protocol(console_input_handle,
                             &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
        if (r != EFI_SUCCESS)
                goto out_of_memory;