123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970 |
- // SPDX-License-Identifier: GPL-2.0+
- /*
- * EFI application console interface
- *
- * Copyright (c) 2016 Alexander Graf
- */
- #include <common.h>
- #include <charset.h>
- #include <dm/device.h>
- #include <efi_loader.h>
- #include <stdio_dev.h>
- #include <video_console.h>
- #define EFI_COUT_MODE_2 2
- #define EFI_MAX_COUT_MODE 3
- struct cout_mode {
- unsigned long columns;
- unsigned long rows;
- int present;
- };
- static struct cout_mode efi_cout_modes[] = {
- /* EFI Mode 0 is 80x25 and always present */
- {
- .columns = 80,
- .rows = 25,
- .present = 1,
- },
- /* EFI Mode 1 is always 80x50 */
- {
- .columns = 80,
- .rows = 50,
- .present = 0,
- },
- /* Value are unknown until we query the console */
- {
- .columns = 0,
- .rows = 0,
- .present = 0,
- },
- };
- const efi_guid_t efi_guid_text_input_ex_protocol =
- EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
- const efi_guid_t efi_guid_text_input_protocol =
- EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
- const efi_guid_t efi_guid_text_output_protocol =
- EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
- #define cESC '\x1b'
- #define ESC "\x1b"
- /* Default to mode 0 */
- static struct simple_text_output_mode efi_con_mode = {
- .max_mode = 1,
- .mode = 0,
- .attribute = 0,
- .cursor_column = 0,
- .cursor_row = 0,
- .cursor_visible = 1,
- };
- /*
- * Receive and parse a reply from the terminal.
- *
- * @n: array of return values
- * @num: number of return values expected
- * @end_char: character indicating end of terminal message
- * @return: non-zero indicates error
- */
- static int term_read_reply(int *n, int num, char end_char)
- {
- char c;
- int i = 0;
- c = getc();
- if (c != cESC)
- return -1;
- c = getc();
- if (c != '[')
- return -1;
- n[0] = 0;
- while (1) {
- c = getc();
- 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;
- }
- /* Read one more decimal position */
- n[i] *= 10;
- n[i] += c - '0';
- }
- if (i != num - 1)
- return -1;
- return 0;
- }
- static efi_status_t EFIAPI efi_cout_output_string(
- struct efi_simple_text_output_protocol *this,
- const efi_string_t string)
- {
- struct simple_text_output_mode *con = &efi_con_mode;
- struct cout_mode *mode = &efi_cout_modes[con->mode];
- char *buf, *pos;
- u16 *p;
- efi_status_t ret = EFI_SUCCESS;
- EFI_ENTRY("%p, %p", this, string);
- buf = malloc(utf16_utf8_strlen(string) + 1);
- if (!buf) {
- ret = EFI_OUT_OF_RESOURCES;
- goto out;
- }
- pos = buf;
- utf16_utf8_strcpy(&pos, string);
- fputs(stdout, buf);
- free(buf);
- /*
- * 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.
- */
- for (p = string; *p; ++p) {
- switch (*p) {
- case '\b': /* U+0008, backspace */
- con->cursor_column = max(0, con->cursor_column - 1);
- break;
- case '\n': /* U+000A, newline */
- con->cursor_column = 0;
- con->cursor_row++;
- break;
- case '\r': /* U+000D, carriage-return */
- con->cursor_column = 0;
- break;
- case 0xd800 ... 0xdbff:
- /*
- * Ignore high surrogates, we do not want to count a
- * Unicode character twice.
- */
- break;
- default:
- 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);
- }
- out:
- return EFI_EXIT(ret);
- }
- static efi_status_t EFIAPI efi_cout_test_string(
- struct efi_simple_text_output_protocol *this,
- const efi_string_t string)
- {
- EFI_ENTRY("%p, %p", this, string);
- return EFI_EXIT(EFI_SUCCESS);
- }
- static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
- {
- if (!mode->present)
- return false;
- return (mode->rows == rows) && (mode->columns == cols);
- }
- static int query_console_serial(int *rows, int *cols)
- {
- /* Ask the terminal about its size */
- int n[3];
- u64 timeout;
- /* 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];
- return 0;
- }
- /*
- * Update the mode table.
- *
- * By default the only mode available is 80x25. If the console has at least 50
- * lines, enable mode 80x50. If we can query the console size and it is neither
- * 80x25 nor 80x50, set it as an additional mode.
- */
- static void query_console_size(void)
- {
- const char *stdout_name = env_get("stdout");
- int rows = 25, cols = 80;
- if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
- IS_ENABLED(CONFIG_DM_VIDEO)) {
- struct stdio_dev *stdout_dev =
- stdio_get_by_name("vidconsole");
- struct udevice *dev = stdout_dev->priv;
- struct vidconsole_priv *priv =
- dev_get_uclass_priv(dev);
- rows = priv->rows;
- cols = priv->cols;
- } else if (query_console_serial(&rows, &cols)) {
- return;
- }
- /* Test if we can have Mode 1 */
- if (cols >= 80 && rows >= 50) {
- efi_cout_modes[1].present = 1;
- efi_con_mode.max_mode = 2;
- }
- /*
- * Install our mode as mode 2 if it is different
- * than mode 0 or 1 and set it as the currently selected mode
- */
- if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
- !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
- efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
- efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
- efi_cout_modes[EFI_COUT_MODE_2].present = 1;
- efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
- efi_con_mode.mode = EFI_COUT_MODE_2;
- }
- }
- static efi_status_t EFIAPI efi_cout_query_mode(
- struct efi_simple_text_output_protocol *this,
- unsigned long mode_number, unsigned long *columns,
- unsigned long *rows)
- {
- EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
- if (mode_number >= efi_con_mode.max_mode)
- return EFI_EXIT(EFI_UNSUPPORTED);
- if (efi_cout_modes[mode_number].present != 1)
- return EFI_EXIT(EFI_UNSUPPORTED);
- if (columns)
- *columns = efi_cout_modes[mode_number].columns;
- if (rows)
- *rows = efi_cout_modes[mode_number].rows;
- 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;
- } color[] = {
- { 30, 40 }, /* 0: black */
- { 34, 44 }, /* 1: blue */
- { 32, 42 }, /* 2: green */
- { 36, 46 }, /* 3: cyan */
- { 31, 41 }, /* 4: red */
- { 35, 45 }, /* 5: magenta */
- { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
- { 37, 47 }, /* 7: light gray, map to white */
- };
- /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
- static efi_status_t EFIAPI efi_cout_set_attribute(
- struct efi_simple_text_output_protocol *this,
- unsigned long attribute)
- {
- unsigned int bold = EFI_ATTR_BOLD(attribute);
- unsigned int fg = EFI_ATTR_FG(attribute);
- unsigned int bg = EFI_ATTR_BG(attribute);
- EFI_ENTRY("%p, %lx", this, attribute);
- if (attribute)
- printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
- else
- printf(ESC"[0;37;40m");
- return EFI_EXIT(EFI_SUCCESS);
- }
- static efi_status_t EFIAPI efi_cout_clear_screen(
- struct efi_simple_text_output_protocol *this)
- {
- EFI_ENTRY("%p", this);
- printf(ESC"[2J");
- efi_con_mode.cursor_column = 0;
- efi_con_mode.cursor_row = 0;
- return EFI_EXIT(EFI_SUCCESS);
- }
- static efi_status_t EFIAPI efi_cout_reset(
- struct efi_simple_text_output_protocol *this,
- char extended_verification)
- {
- EFI_ENTRY("%p, %d", this, extended_verification);
- /* Clear screen */
- EFI_CALL(efi_cout_clear_screen(this));
- /* Set default colors */
- printf(ESC "[0;37;40m");
- return EFI_EXIT(EFI_SUCCESS);
- }
- static efi_status_t EFIAPI efi_cout_set_cursor_position(
- struct efi_simple_text_output_protocol *this,
- unsigned long column, unsigned long row)
- {
- EFI_ENTRY("%p, %ld, %ld", this, column, row);
- printf(ESC"[%d;%df", (int)row, (int)column);
- efi_con_mode.cursor_column = column;
- efi_con_mode.cursor_row = row;
- return EFI_EXIT(EFI_SUCCESS);
- }
- static efi_status_t EFIAPI efi_cout_enable_cursor(
- struct efi_simple_text_output_protocol *this,
- bool enable)
- {
- EFI_ENTRY("%p, %d", this, enable);
- printf(ESC"[?25%c", enable ? 'h' : 'l');
- return EFI_EXIT(EFI_SUCCESS);
- }
- struct efi_simple_text_output_protocol efi_con_out = {
- .reset = efi_cout_reset,
- .output_string = efi_cout_output_string,
- .test_string = efi_cout_test_string,
- .query_mode = efi_cout_query_mode,
- .set_mode = efi_cout_set_mode,
- .set_attribute = efi_cout_set_attribute,
- .clear_screen = efi_cout_clear_screen,
- .set_cursor_position = efi_cout_set_cursor_position,
- .enable_cursor = efi_cout_enable_cursor,
- .mode = (void*)&efi_con_mode,
- };
- static bool key_available;
- static struct efi_key_data next_key;
- /**
- * set_shift_mask() - set shift mask
- *
- * @mod: Xterm shift mask
- */
- void set_shift_mask(int mod, struct efi_key_state *key_state)
- {
- key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
- if (mod) {
- --mod;
- if (mod & 1)
- key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
- if (mod & 2)
- key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
- if (mod & 4)
- key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
- if (mod & 8)
- key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
- } else {
- key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
- }
- }
- /**
- * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
- *
- * This gets called when we have already parsed CSI.
- *
- * @modifiers: bitmask (shift, alt, ctrl)
- * @return: the unmodified code
- */
- static int analyze_modifiers(struct efi_key_state *key_state)
- {
- int c, mod = 0, ret = 0;
- c = getc();
- if (c != ';') {
- ret = c;
- if (c == '~')
- goto out;
- c = getc();
- }
- for (;;) {
- switch (c) {
- case '0'...'9':
- mod *= 10;
- mod += c - '0';
- /* fall through */
- case ';':
- c = getc();
- break;
- default:
- goto out;
- }
- }
- out:
- set_shift_mask(mod, key_state);
- if (!ret)
- ret = c;
- return ret;
- }
- /**
- * efi_cin_read_key() - read a key from the console input
- *
- * @key: - key received
- * Return: - status code
- */
- static efi_status_t efi_cin_read_key(struct efi_key_data *key)
- {
- struct efi_input_key pressed_key = {
- .scan_code = 0,
- .unicode_char = 0,
- };
- s32 ch;
- if (console_read_unicode(&ch))
- return EFI_NOT_READY;
- key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
- key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
- /* We do not support multi-word codes */
- if (ch >= 0x10000)
- ch = '?';
- switch (ch) {
- case 0x1b:
- /*
- * Xterm Control Sequences
- * https://www.xfree86.org/4.8.0/ctlseqs.html
- */
- ch = getc();
- switch (ch) {
- case cESC: /* ESC */
- pressed_key.scan_code = 23;
- break;
- case 'O': /* F1 - F4 */
- ch = getc();
- /* consider modifiers */
- if (ch < 'P') {
- set_shift_mask(ch - '0', &key->key_state);
- ch = getc();
- }
- pressed_key.scan_code = ch - 'P' + 11;
- break;
- case '[':
- ch = getc();
- switch (ch) {
- case 'A'...'D': /* up, down right, left */
- pressed_key.scan_code = ch - 'A' + 1;
- break;
- case 'F': /* End */
- pressed_key.scan_code = 6;
- break;
- case 'H': /* Home */
- pressed_key.scan_code = 5;
- break;
- case '1':
- ch = analyze_modifiers(&key->key_state);
- switch (ch) {
- 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;
- break;
- case 'A'...'D': /* up, down right, left */
- pressed_key.scan_code = ch - 'A' + 1;
- break;
- case 'F':
- pressed_key.scan_code = 6; /* End */
- break;
- case 'H':
- pressed_key.scan_code = 5; /* Home */
- break;
- }
- break;
- case '2':
- ch = analyze_modifiers(&key->key_state);
- switch (ch) {
- case '0'...'1': /* F9 - F10 */
- pressed_key.scan_code = ch - '0' + 19;
- break;
- case '3'...'4': /* F11 - F12 */
- pressed_key.scan_code = ch - '3' + 21;
- break;
- case '~': /* INS */
- pressed_key.scan_code = 7;
- break;
- }
- break;
- case '3': /* DEL */
- pressed_key.scan_code = 8;
- analyze_modifiers(&key->key_state);
- break;
- case '5': /* PG UP */
- pressed_key.scan_code = 9;
- analyze_modifiers(&key->key_state);
- break;
- case '6': /* PG DOWN */
- pressed_key.scan_code = 10;
- analyze_modifiers(&key->key_state);
- break;
- } /* [ */
- break;
- default:
- /* ALT key */
- set_shift_mask(3, &key->key_state);
- }
- break;
- case 0x7f:
- /* Backspace */
- ch = 0x08;
- }
- if (pressed_key.scan_code) {
- key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
- } else {
- pressed_key.unicode_char = ch;
- /*
- * Assume left control key for control characters typically
- * entered using the control key.
- */
- if (ch >= 0x01 && ch <= 0x1f) {
- key->key_state.key_shift_state |=
- EFI_SHIFT_STATE_VALID;
- switch (ch) {
- case 0x01 ... 0x07:
- case 0x0b ... 0x0c:
- case 0x0e ... 0x1f:
- key->key_state.key_shift_state |=
- EFI_LEFT_CONTROL_PRESSED;
- }
- }
- }
- key->key = pressed_key;
- return EFI_SUCCESS;
- }
- /**
- * efi_cin_check() - check if keyboard input is available
- */
- static void efi_cin_check(void)
- {
- efi_status_t ret;
- if (key_available) {
- efi_signal_event(efi_con_in.wait_for_key, true);
- return;
- }
- if (tstc()) {
- ret = efi_cin_read_key(&next_key);
- if (ret == EFI_SUCCESS) {
- key_available = true;
- /* Queue the wait for key event */
- efi_signal_event(efi_con_in.wait_for_key, true);
- }
- }
- }
- /**
- * efi_cin_empty_buffer() - empty input buffer
- */
- static void efi_cin_empty_buffer(void)
- {
- while (tstc())
- getc();
- key_available = false;
- }
- /**
- * efi_cin_reset_ex() - reset console input
- *
- * @this: - the extended simple text input protocol
- * @extended_verification: - extended verification
- *
- * This function implements the reset service of the
- * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- *
- * Return: old value of the task priority level
- */
- static efi_status_t EFIAPI efi_cin_reset_ex(
- struct efi_simple_text_input_ex_protocol *this,
- bool extended_verification)
- {
- efi_status_t ret = EFI_SUCCESS;
- EFI_ENTRY("%p, %d", this, extended_verification);
- /* Check parameters */
- if (!this) {
- ret = EFI_INVALID_PARAMETER;
- goto out;
- }
- efi_cin_empty_buffer();
- out:
- return EFI_EXIT(ret);
- }
- /**
- * efi_cin_read_key_stroke_ex() - read key stroke
- *
- * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @key_data: key read from console
- * Return: status code
- *
- * This function implements the ReadKeyStrokeEx service of the
- * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- */
- static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
- struct efi_simple_text_input_ex_protocol *this,
- struct efi_key_data *key_data)
- {
- efi_status_t ret = EFI_SUCCESS;
- EFI_ENTRY("%p, %p", this, key_data);
- /* Check parameters */
- if (!this || !key_data) {
- ret = EFI_INVALID_PARAMETER;
- goto out;
- }
- /* We don't do interrupts, so check for timers cooperatively */
- efi_timer_check();
- /* Enable console input after ExitBootServices */
- efi_cin_check();
- if (!key_available) {
- ret = EFI_NOT_READY;
- goto out;
- }
- *key_data = next_key;
- key_available = false;
- efi_con_in.wait_for_key->is_signaled = false;
- out:
- return EFI_EXIT(ret);
- }
- /**
- * efi_cin_set_state() - set toggle key state
- *
- * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @key_toggle_state: key toggle state
- * Return: status code
- *
- * This function implements the SetState service of the
- * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- */
- static efi_status_t EFIAPI efi_cin_set_state(
- struct efi_simple_text_input_ex_protocol *this,
- u8 key_toggle_state)
- {
- EFI_ENTRY("%p, %u", 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.
- *
- * According to the UEFI specification it is allowable to not implement
- * this service.
- */
- return EFI_EXIT(EFI_UNSUPPORTED);
- }
- /**
- * efi_cin_register_key_notify() - register key notification function
- *
- * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @key_data: key to be notified
- * @key_notify_function: function to be called if the key is pressed
- * @notify_handle: handle for unregistering the notification
- * Return: status code
- *
- * This function implements the SetState service of the
- * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- */
- static efi_status_t EFIAPI efi_cin_register_key_notify(
- struct efi_simple_text_input_ex_protocol *this,
- struct efi_key_data *key_data,
- efi_status_t (EFIAPI *key_notify_function)(
- struct efi_key_data *key_data),
- void **notify_handle)
- {
- EFI_ENTRY("%p, %p, %p, %p",
- this, key_data, key_notify_function, notify_handle);
- return EFI_EXIT(EFI_OUT_OF_RESOURCES);
- }
- /**
- * efi_cin_unregister_key_notify() - unregister key notification function
- *
- * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @notification_handle: handle received when registering
- * Return: status code
- *
- * This function implements the SetState service of the
- * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- */
- static efi_status_t EFIAPI efi_cin_unregister_key_notify(
- struct efi_simple_text_input_ex_protocol *this,
- void *notification_handle)
- {
- EFI_ENTRY("%p, %p", this, notification_handle);
- return EFI_EXIT(EFI_INVALID_PARAMETER);
- }
- /**
- * efi_cin_reset() - drain the input buffer
- *
- * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @extended_verification: allow for exhaustive verification
- * Return: status code
- *
- * This function implements the Reset service of the
- * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- */
- static efi_status_t EFIAPI efi_cin_reset
- (struct efi_simple_text_input_protocol *this,
- bool extended_verification)
- {
- efi_status_t ret = EFI_SUCCESS;
- EFI_ENTRY("%p, %d", this, extended_verification);
- /* Check parameters */
- if (!this) {
- ret = EFI_INVALID_PARAMETER;
- goto out;
- }
- efi_cin_empty_buffer();
- out:
- return EFI_EXIT(ret);
- }
- /**
- * efi_cin_read_key_stroke() - read key stroke
- *
- * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
- * @key: key read from console
- * Return: status code
- *
- * This function implements the ReadKeyStroke service of the
- * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
- *
- * See the Unified Extensible Firmware Interface (UEFI) specification for
- * details.
- */
- static efi_status_t EFIAPI efi_cin_read_key_stroke
- (struct efi_simple_text_input_protocol *this,
- struct efi_input_key *key)
- {
- efi_status_t ret = EFI_SUCCESS;
- EFI_ENTRY("%p, %p", this, key);
- /* Check parameters */
- if (!this || !key) {
- ret = EFI_INVALID_PARAMETER;
- goto out;
- }
- /* We don't do interrupts, so check for timers cooperatively */
- efi_timer_check();
- /* Enable console input after ExitBootServices */
- efi_cin_check();
- if (!key_available) {
- ret = EFI_NOT_READY;
- goto out;
- }
- *key = next_key.key;
- key_available = false;
- efi_con_in.wait_for_key->is_signaled = false;
- out:
- return EFI_EXIT(ret);
- }
- static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
- .reset = efi_cin_reset_ex,
- .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
- .wait_for_key_ex = NULL,
- .set_state = efi_cin_set_state,
- .register_key_notify = efi_cin_register_key_notify,
- .unregister_key_notify = efi_cin_unregister_key_notify,
- };
- struct efi_simple_text_input_protocol efi_con_in = {
- .reset = efi_cin_reset,
- .read_key_stroke = efi_cin_read_key_stroke,
- .wait_for_key = NULL,
- };
- static struct efi_event *console_timer_event;
- /*
- * efi_console_timer_notify() - notify the console timer event
- *
- * @event: console timer event
- * @context: not used
- */
- static void EFIAPI efi_console_timer_notify(struct efi_event *event,
- void *context)
- {
- EFI_ENTRY("%p, %p", event, context);
- efi_cin_check();
- EFI_EXIT(EFI_SUCCESS);
- }
- /**
- * efi_key_notify() - notify the wait for key event
- *
- * @event: wait for key event
- * @context: not used
- */
- static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
- {
- EFI_ENTRY("%p, %p", event, context);
- efi_cin_check();
- EFI_EXIT(EFI_SUCCESS);
- }
- /**
- * efi_console_register() - install the console protocols
- *
- * This function is called from do_bootefi_exec().
- */
- int efi_console_register(void)
- {
- efi_status_t r;
- struct efi_object *efi_console_output_obj;
- struct efi_object *efi_console_input_obj;
- /* Set up mode information */
- query_console_size();
- /* Create handles */
- r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
- if (r != EFI_SUCCESS)
- goto out_of_memory;
- r = efi_add_protocol(efi_console_output_obj->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;
- r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
- if (r != EFI_SUCCESS)
- goto out_of_memory;
- r = efi_add_protocol(efi_console_input_obj->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,
- &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
- if (r != EFI_SUCCESS)
- goto out_of_memory;
- /* Create console events */
- r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
- NULL, NULL, &efi_con_in.wait_for_key);
- if (r != EFI_SUCCESS) {
- printf("ERROR: Failed to register WaitForKey event\n");
- return r;
- }
- efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
- r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
- efi_console_timer_notify, NULL, NULL,
- &console_timer_event);
- if (r != EFI_SUCCESS) {
- printf("ERROR: Failed to register console event\n");
- return r;
- }
- /* 5000 ns cycle is sufficient for 2 MBaud */
- r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
- if (r != EFI_SUCCESS)
- printf("ERROR: Failed to set console timer\n");
- return r;
- out_of_memory:
- printf("ERROR: Out of memory\n");
- return r;
- }
|