efi_loader: variable: support APPEND_WRITE
[oweals/u-boot.git] / lib / efi_loader / efi_console.c
index 2fc25e118f2ddb07141b6895326defba89888882..a55e4b33eced265b12a699ea5a078f1aba578414 100644 (file)
@@ -9,6 +9,7 @@
 #include <charset.h>
 #include <dm/device.h>
 #include <efi_loader.h>
+#include <env.h>
 #include <stdio_dev.h>
 #include <video_console.h>
 
@@ -155,13 +156,14 @@ static efi_status_t EFIAPI efi_cout_output_string(
         * Update the cursor position.
         *
         * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
-        * and U000D. All other characters, including control characters
-        * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
+        * and U000D. All other control characters are ignored. Any non-control
+        * character increase the column by one.
         */
        for (p = string; *p; ++p) {
                switch (*p) {
                case '\b':      /* U+0008, backspace */
-                       con->cursor_column = max(0, con->cursor_column - 1);
+                       if (con->cursor_column)
+                               con->cursor_column--;
                        break;
                case '\n':      /* U+000A, newline */
                        con->cursor_column = 0;
@@ -177,14 +179,21 @@ static efi_status_t EFIAPI efi_cout_output_string(
                         */
                        break;
                default:
-                       con->cursor_column++;
+                       /* Exclude control codes */
+                       if (*p > 0x1f)
+                               con->cursor_column++;
                        break;
                }
                if (con->cursor_column >= mode->columns) {
                        con->cursor_column = 0;
                        con->cursor_row++;
                }
-               con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
+               /*
+                * When we exceed the row count the terminal will scroll up one
+                * line. We have to adjust the cursor position.
+                */
+               if (con->cursor_row >= mode->rows && con->cursor_row)
+                       con->cursor_row--;
        }
 
 out:
@@ -210,9 +219,9 @@ static bool cout_mode_matches(struct cout_mode *mode, int rows, int 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
+ * @rows:      pointer to return number of rows
+ * @cols:      pointer to return number of columns
+ * Returns:    0 on success
  */
 static int query_console_serial(int *rows, int *cols)
 {
@@ -316,23 +325,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;
@@ -358,6 +350,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
@@ -378,6 +371,24 @@ 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);
+
+       if (!efi_cout_modes[mode_number].present)
+               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)
@@ -387,6 +398,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);
@@ -452,7 +464,7 @@ struct efi_simple_text_output_protocol efi_con_out = {
  * struct efi_cin_notify_function - registered console input notify function
  *
  * @link:      link to list
- * @data:      key to notify
+ * @key:       key to notify
  * @function:  function to call
  */
 struct efi_cin_notify_function {
@@ -470,6 +482,7 @@ static LIST_HEAD(cin_notify_functions);
  * set_shift_mask() - set shift mask
  *
  * @mod:       Xterm shift mask
+ * @key_state:  receives the state of the shift, alt, control, and logo keys
  */
 void set_shift_mask(int mod, struct efi_key_state *key_state)
 {
@@ -482,10 +495,8 @@ void set_shift_mask(int mod, struct efi_key_state *key_state)
                        key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
                if (mod & 4)
                        key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
-               if (mod & 8)
+               if (!mod || (mod & 8))
                        key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
-       } else {
-               key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
        }
 }
 
@@ -494,7 +505,7 @@ void set_shift_mask(int mod, struct efi_key_state *key_state)
  *
  * This gets called when we have already parsed CSI.
  *
- * @modifiers:  bit mask (shift, alt, ctrl)
+ * @key_state:  receives the state of the shift, alt, control, and logo keys
  * @return:    the unmodified code
  */
 static int analyze_modifiers(struct efi_key_state *key_state)
@@ -564,10 +575,13 @@ static efi_status_t efi_cin_read_key(struct efi_key_data *key)
                case cESC: /* ESC */
                        pressed_key.scan_code = 23;
                        break;
-               case 'O': /* F1 - F4 */
+               case 'O': /* F1 - F4, End */
                        ch = getc();
                        /* consider modifiers */
-                       if (ch < 'P') {
+                       if (ch == 'F') { /* End */
+                               pressed_key.scan_code = 6;
+                               break;
+                       } else if (ch < 'P') {
                                set_shift_mask(ch - '0', &key->key_state);
                                ch = getc();
                        }
@@ -591,17 +605,20 @@ static efi_status_t efi_cin_read_key(struct efi_key_data *key)
                                case '1'...'5': /* F1 - F5 */
                                        pressed_key.scan_code = ch - '1' + 11;
                                        break;
-                               case '7'...'9': /* F6 - F8 */
-                                       pressed_key.scan_code = ch - '7' + 16;
+                               case '6'...'9': /* F5 - F8 */
+                                       pressed_key.scan_code = ch - '6' + 15;
                                        break;
                                case 'A'...'D': /* up, down right, left */
                                        pressed_key.scan_code = ch - 'A' + 1;
                                        break;
-                               case 'F':
-                                       pressed_key.scan_code = 6; /* End */
+                               case 'F': /* End */
+                                       pressed_key.scan_code = 6;
+                                       break;
+                               case 'H': /* Home */
+                                       pressed_key.scan_code = 5;
                                        break;
-                               case 'H':
-                                       pressed_key.scan_code = 5; /* Home */
+                               case '~': /* Home */
+                                       pressed_key.scan_code = 5;
                                        break;
                                }
                                break;