Merge tag 'efi-2019-07-rc2' of git://git.denx.de/u-boot-efi
[oweals/u-boot.git] / lib / efi_loader / efi_console.c
index a6fd93d2c5d6a98ad780f2e7e7027ef40968874b..051fc1d339f7b929f3a34111c0a9ddd47c34c08e 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;
@@ -185,32 +202,47 @@ static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
        return (mode->rows == rows) && (mode->columns == cols);
 }
 
+/**
+ * query_console_serial() - query console size
+ *
+ * @rows       pointer to return number of rows
+ * @columns    pointer to return number of columns
+ * Returns     0 on success
+ */
 static int query_console_serial(int *rows, int *cols)
 {
-       /* Ask the terminal about its size */
-       int n[3];
-       u64 timeout;
+       int ret = 0;
+       int n[2];
 
        /* Empty input buffer */
        while (tstc())
                getc();
 
-       printf(ESC"[18t");
-
-       /* Check if we have a terminal that understands */
-       timeout = timer_get_us() + 1000000;
-       while (!tstc())
-               if (timer_get_us() > timeout)
-                       return -1;
-
-       /* Read {depth,rows,cols} */
-       if (term_read_reply(n, 3, 't'))
-               return -1;
-
-       *cols = n[2];
-       *rows = n[1];
+       /*
+        * Not all terminals understand CSI [18t for querying the console size.
+        * We should adhere to escape sequences documented in the console_codes
+        * 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
+        * restore the original cursor position.
+        */
+       printf(ESC "7"          /* Save cursor position */
+              ESC "[r"         /* Set scrolling region to full window */
+              ESC "[999;999H"  /* Move to bottom right corner */
+              ESC "[6n");      /* Query cursor position */
+
+       /* Read {rows,cols} */
+       if (term_read_reply(n, 2, 'R')) {
+               ret = 1;
+               goto out;
+       }
 
-       return 0;
+       *cols = n[1];
+       *rows = n[0];
+out:
+       printf(ESC "8");        /* Restore cursor position */
+       return ret;
 }
 
 /*
@@ -359,13 +391,31 @@ static efi_status_t EFIAPI efi_cout_set_cursor_position(
                        struct efi_simple_text_output_protocol *this,
                        unsigned long column, unsigned long row)
 {
+       efi_status_t ret = EFI_SUCCESS;
+       struct simple_text_output_mode *con = &efi_con_mode;
+       struct cout_mode *mode = &efi_cout_modes[con->mode];
+
        EFI_ENTRY("%p, %ld, %ld", this, column, row);
 
-       printf(ESC"[%d;%df", (int)row, (int)column);
+       /* Check parameters */
+       if (!this) {
+               ret = EFI_INVALID_PARAMETER;
+               goto out;
+       }
+       if (row >= mode->rows || column >= mode->columns) {
+               ret = EFI_UNSUPPORTED;
+               goto out;
+       }
+
+       /*
+        * Set cursor position by sending CSI H.
+        * EFI origin is [0, 0], terminal origin is [1, 1].
+        */
+       printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
        efi_con_mode.cursor_column = column;
        efi_con_mode.cursor_row = row;
-
-       return EFI_EXIT(EFI_SUCCESS);
+out:
+       return EFI_EXIT(ret);
 }
 
 static efi_status_t EFIAPI efi_cout_enable_cursor(
@@ -392,8 +442,23 @@ struct efi_simple_text_output_protocol efi_con_out = {
        .mode = (void*)&efi_con_mode,
 };
 
+/**
+ * struct efi_cin_notify_function - registered console input notify function
+ *
+ * @link:      link to list
+ * @data:      key to notify
+ * @function:  function to call
+ */
+struct efi_cin_notify_function {
+       struct list_head link;
+       struct efi_key_data key;
+       efi_status_t (EFIAPI *function)
+               (struct efi_key_data *key_data);
+};
+
 static bool key_available;
 static struct efi_key_data next_key;
+static LIST_HEAD(cin_notify_functions);
 
 /**
  * set_shift_mask() - set shift mask
@@ -423,7 +488,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)
@@ -597,6 +662,34 @@ static efi_status_t efi_cin_read_key(struct efi_key_data *key)
        return EFI_SUCCESS;
 }
 
+/**
+ * efi_cin_notify() - notify registered functions
+ */
+static void efi_cin_notify(void)
+{
+       struct efi_cin_notify_function *item;
+
+       list_for_each_entry(item, &cin_notify_functions, link) {
+               bool match = true;
+
+               /* We do not support toggle states */
+               if (item->key.key.unicode_char || item->key.key.scan_code) {
+                       if (item->key.key.unicode_char !=
+                           next_key.key.unicode_char ||
+                           item->key.key.scan_code != next_key.key.scan_code)
+                               match = false;
+               }
+               if (item->key.key_state.key_shift_state &&
+                   item->key.key_state.key_shift_state !=
+                   next_key.key_state.key_shift_state)
+                       match = false;
+
+               if (match)
+                       /* We don't bother about the return code */
+                       EFI_CALL(item->function(&next_key));
+       }
+}
+
 /**
  * efi_cin_check() - check if keyboard input is available
  */
@@ -614,8 +707,12 @@ static void efi_cin_check(void)
                if (ret == EFI_SUCCESS) {
                        key_available = true;
 
+                       /* Notify registered functions */
+                       efi_cin_notify();
+
                        /* Queue the wait for key event */
-                       efi_signal_event(efi_con_in.wait_for_key, true);
+                       if (key_available)
+                               efi_signal_event(efi_con_in.wait_for_key, true);
                }
        }
 }
@@ -700,9 +797,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);
 }
@@ -757,9 +871,35 @@ static efi_status_t EFIAPI efi_cin_register_key_notify(
                        struct efi_key_data *key_data),
                void **notify_handle)
 {
+       efi_status_t ret = EFI_SUCCESS;
+       struct efi_cin_notify_function *notify_function;
+
        EFI_ENTRY("%p, %p, %p, %p",
                  this, key_data, key_notify_function, notify_handle);
-       return EFI_EXIT(EFI_OUT_OF_RESOURCES);
+
+       /* Check parameters */
+       if (!this || !key_data || !key_notify_function || !notify_handle) {
+               ret = EFI_INVALID_PARAMETER;
+               goto out;
+       }
+
+       EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
+                 key_data->key.unicode_char,
+              key_data->key.scan_code,
+              key_data->key_state.key_shift_state,
+              key_data->key_state.key_toggle_state);
+
+       notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
+       if (!notify_function) {
+               ret = EFI_OUT_OF_RESOURCES;
+               goto out;
+       }
+       notify_function->key = *key_data;
+       notify_function->function = key_notify_function;
+       list_add_tail(&notify_function->link, &cin_notify_functions);
+       *notify_handle = notify_function;
+out:
+       return EFI_EXIT(ret);
 }
 
 /**
@@ -779,8 +919,30 @@ static efi_status_t EFIAPI efi_cin_unregister_key_notify(
                struct efi_simple_text_input_ex_protocol *this,
                void *notification_handle)
 {
+       efi_status_t ret = EFI_INVALID_PARAMETER;
+       struct efi_cin_notify_function *item, *notify_function =
+                       notification_handle;
+
        EFI_ENTRY("%p, %p", this, notification_handle);
-       return EFI_EXIT(EFI_INVALID_PARAMETER);
+
+       /* Check parameters */
+       if (!this || !notification_handle)
+               goto out;
+
+       list_for_each_entry(item, &cin_notify_functions, link) {
+               if (item == notify_function) {
+                       ret = EFI_SUCCESS;
+                       break;
+               }
+       }
+       if (ret != EFI_SUCCESS)
+               goto out;
+
+       /* Remove the notify function */
+       list_del(&notify_function->link);
+       free(notify_function);
+out:
+       return EFI_EXIT(ret);
 }
 
 
@@ -908,38 +1070,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;