2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2006,2007,2008 Free Software Foundation, Inc.
2N/A * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/term.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/types.h>
2N/A#include <grub/err.h>
2N/A#include <grub/efi/efi.h>
2N/A#include <grub/efi/api.h>
2N/A#include <grub/efi/console.h>
2N/A
2N/Astatic grub_efi_guid_t stiex_guid = GRUB_EFI_SIMPLE_TEXT_INPUT_EX_GUID;
2N/Astatic grub_efi_simple_input_ex_interface_t *stiex;
2N/A
2N/A#define GRUB_EFI_SHIFT_TO_INTERNAL(s) \
2N/A ((((s) & GRUB_EFI_INPUTEX_RT_SHIFT) ? \
2N/A GRUB_TERM_SHIFT : 0) | \
2N/A (((s) & GRUB_EFI_INPUTEX_LT_SHIFT) ? \
2N/A GRUB_TERM_SHIFT : 0) | \
2N/A (((s) & GRUB_EFI_INPUTEX_RT_CTRL) ? \
2N/A GRUB_TERM_CTRL : 0) | \
2N/A (((s) & GRUB_EFI_INPUTEX_LT_CTRL) ? \
2N/A GRUB_TERM_CTRL : 0) | \
2N/A (((s) & GRUB_EFI_INPUTEX_RT_ALT) ? \
2N/A GRUB_TERM_ALT : 0) | \
2N/A (((s) & GRUB_EFI_INPUTEX_LT_ALT) ? \
2N/A GRUB_TERM_ALT : 0))
2N/A
2N/Astatic grub_uint32_t
2N/Amap_char (grub_uint32_t c)
2N/A{
2N/A /* Map some unicode characters to the EFI character. */
2N/A switch (c)
2N/A {
2N/A case GRUB_UNICODE_LEFTARROW:
2N/A c = GRUB_UNICODE_BLACK_LEFT_TRIANGLE;
2N/A break;
2N/A case GRUB_UNICODE_UPARROW:
2N/A c = GRUB_UNICODE_BLACK_UP_TRIANGLE;
2N/A break;
2N/A case GRUB_UNICODE_RIGHTARROW:
2N/A c = GRUB_UNICODE_BLACK_RIGHT_TRIANGLE;
2N/A break;
2N/A case GRUB_UNICODE_DOWNARROW:
2N/A c = GRUB_UNICODE_BLACK_DOWN_TRIANGLE;
2N/A break;
2N/A case GRUB_UNICODE_HLINE:
2N/A c = GRUB_UNICODE_LIGHT_HLINE;
2N/A break;
2N/A case GRUB_UNICODE_VLINE:
2N/A c = GRUB_UNICODE_LIGHT_VLINE;
2N/A break;
2N/A case GRUB_UNICODE_CORNER_UL:
2N/A c = GRUB_UNICODE_LIGHT_CORNER_UL;
2N/A break;
2N/A case GRUB_UNICODE_CORNER_UR:
2N/A c = GRUB_UNICODE_LIGHT_CORNER_UR;
2N/A break;
2N/A case GRUB_UNICODE_CORNER_LL:
2N/A c = GRUB_UNICODE_LIGHT_CORNER_LL;
2N/A break;
2N/A case GRUB_UNICODE_CORNER_LR:
2N/A c = GRUB_UNICODE_LIGHT_CORNER_LR;
2N/A break;
2N/A }
2N/A
2N/A return c;
2N/A}
2N/A
2N/Astatic void
2N/Agrub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
2N/A const struct grub_unicode_glyph *c)
2N/A{
2N/A grub_efi_char16_t str[2 + c->ncomb];
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A unsigned i, j;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A
2N/A /* For now, do not try to use a surrogate pair. */
2N/A if (c->base > 0xffff)
2N/A str[0] = '?';
2N/A else
2N/A str[0] = (grub_efi_char16_t) map_char (c->base & 0xffff);
2N/A j = 1;
2N/A for (i = 0; i < c->ncomb; i++)
2N/A if (c->base < 0xffff)
2N/A str[j++] = c->combining[i].code;
2N/A str[j] = 0;
2N/A
2N/A /* Should this test be cached? */
2N/A if ((c->base > 0x7f || c->ncomb)
2N/A && efi_call_2 (o->test_string, o, str) != GRUB_EFI_SUCCESS)
2N/A return;
2N/A
2N/A efi_call_2 (o->output_string, o, str);
2N/A}
2N/A
2N/Aconst unsigned efi_codes[] =
2N/A {
2N/A 0, GRUB_TERM_KEY_UP, GRUB_TERM_KEY_DOWN, GRUB_TERM_KEY_RIGHT,
2N/A GRUB_TERM_KEY_LEFT, GRUB_TERM_KEY_HOME, GRUB_TERM_KEY_END, GRUB_TERM_KEY_INSERT,
2N/A GRUB_TERM_KEY_DC, GRUB_TERM_KEY_PPAGE, GRUB_TERM_KEY_NPAGE, GRUB_TERM_KEY_F1,
2N/A GRUB_TERM_KEY_F2, GRUB_TERM_KEY_F3, GRUB_TERM_KEY_F4, GRUB_TERM_KEY_F5,
2N/A GRUB_TERM_KEY_F6, GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8, GRUB_TERM_KEY_F9,
2N/A GRUB_TERM_KEY_F10, 0, 0, '\e'
2N/A };
2N/A
2N/Astatic int
2N/Aneed_unshift(grub_efi_char16_t ch)
2N/A{
2N/A if (ch >= 32 && ch < 128)
2N/A return 1;
2N/A return 0;
2N/A}
2N/A
2N/Astatic int
2N/Agrub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
2N/A{
2N/A grub_efi_simple_input_interface_t *i;
2N/A grub_efi_key_data_t keyex;
2N/A grub_efi_status_t status;
2N/A int mods = 0;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return 0;
2N/A
2N/A if (stiex)
2N/A status = efi_call_2 (stiex->read_key_stroke_ex, stiex, &keyex);
2N/A else
2N/A {
2N/A grub_memset(&keyex.key_state, 0, sizeof (keyex.key_state));
2N/A i = grub_efi_system_table->con_in;
2N/A status = efi_call_2 (i->read_key_stroke, i, &keyex.key);
2N/A }
2N/A
2N/A if (status != GRUB_EFI_SUCCESS)
2N/A return GRUB_TERM_NO_KEY;
2N/A
2N/A if (keyex.key_state.key_shift_state & GRUB_EFI_INPUTEX_SHIFT_VALID)
2N/A mods = GRUB_EFI_SHIFT_TO_INTERNAL(keyex.key_state.key_shift_state);
2N/A
2N/A if (keyex.key.scan_code == 0)
2N/A {
2N/A grub_efi_char16_t c = keyex.key.unicode_char;
2N/A
2N/A if (need_unshift(c))
2N/A mods &= ~GRUB_TERM_SHIFT;
2N/A
2N/A if (c == 0x7f)
2N/A c = '\b';
2N/A if (c == '\t' || c == '\b' || c == '\n' || c == '\r')
2N/A mods &= ~GRUB_TERM_CTRL;
2N/A else if (c < 0x20)
2N/A mods |= GRUB_TERM_CTRL;
2N/A
2N/A if ((mods & GRUB_TERM_CTRL) && keyex.key.unicode_char < 0x20)
2N/A return ('a' + (c - 1)) | mods;
2N/A
2N/A return c | mods;
2N/A }
2N/A else if (keyex.key.scan_code < ARRAY_SIZE (efi_codes))
2N/A return efi_codes[keyex.key.scan_code] | mods;
2N/A return GRUB_TERM_NO_KEY;
2N/A}
2N/A
2N/Astatic grub_uint16_t
2N/Agrub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A grub_efi_uintn_t columns, rows;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A if (grub_efi_is_finished || efi_call_4 (o->query_mode, o, o->mode->mode,
2N/A &columns, &rows) != GRUB_EFI_SUCCESS)
2N/A {
2N/A /* Why does this fail? */
2N/A columns = 80;
2N/A rows = 25;
2N/A }
2N/A
2N/A return ((columns << 8) | rows);
2N/A}
2N/A
2N/Astatic grub_uint16_t
2N/Agrub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return 0;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A return ((o->mode->cursor_column << 8) | o->mode->cursor_row);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
2N/A grub_uint8_t x, grub_uint8_t y)
2N/A{
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A efi_call_3 (o->set_cursor_position, o, x, y);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_console_cls (struct grub_term_output *term __attribute__ ((unused)))
2N/A{
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A grub_efi_int32_t orig_attr;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A orig_attr = o->mode->attribute;
2N/A efi_call_2 (o->set_attributes, o, GRUB_EFI_BACKGROUND_BLACK);
2N/A efi_call_1 (o->clear_screen, o);
2N/A efi_call_2 (o->set_attributes, o, orig_attr);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_console_setcolorstate (struct grub_term_output *term,
2N/A grub_term_color_state state)
2N/A{
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A
2N/A switch (state) {
2N/A case GRUB_TERM_COLOR_STANDARD:
2N/A efi_call_2 (o->set_attributes, o, GRUB_TERM_DEFAULT_STANDARD_COLOR
2N/A & 0x7f);
2N/A break;
2N/A case GRUB_TERM_COLOR_NORMAL:
2N/A efi_call_2 (o->set_attributes, o, term->normal_color & 0x7f);
2N/A break;
2N/A case GRUB_TERM_COLOR_HIGHLIGHT:
2N/A efi_call_2 (o->set_attributes, o, term->highlight_color & 0x7f);
2N/A break;
2N/A default:
2N/A break;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Agrub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
2N/A int on)
2N/A{
2N/A grub_efi_simple_text_output_interface_t *o;
2N/A
2N/A if (grub_efi_is_finished)
2N/A return;
2N/A
2N/A o = grub_efi_system_table->con_out;
2N/A efi_call_2 (o->enable_cursor, o, on);
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_efi_console_init (struct grub_term_output *term)
2N/A{
2N/A grub_console_setcursor (term, 1);
2N/A return 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_efi_console_fini (struct grub_term_output *term)
2N/A{
2N/A grub_console_setcursor (term, 0);
2N/A return 0;
2N/A}
2N/A
2N/Astatic struct grub_term_input grub_console_term_input =
2N/A {
2N/A .name = "console",
2N/A .getkey = grub_console_getkey,
2N/A };
2N/A
2N/Astatic struct grub_term_output grub_console_term_output =
2N/A {
2N/A .name = "console",
2N/A .init = grub_efi_console_init,
2N/A .fini = grub_efi_console_fini,
2N/A .putchar = grub_console_putchar,
2N/A .getwh = grub_console_getwh,
2N/A .getxy = grub_console_getxy,
2N/A .gotoxy = grub_console_gotoxy,
2N/A .cls = grub_console_cls,
2N/A .setcolorstate = grub_console_setcolorstate,
2N/A .setcursor = grub_console_setcursor,
2N/A .normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
2N/A .highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
2N/A .flags = GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS
2N/A };
2N/A
2N/Avoid
2N/Agrub_console_init (void)
2N/A{
2N/A /* FIXME: it is necessary to consider the case where no console control
2N/A is present but the default is already in text mode. */
2N/A if (! grub_efi_set_text_mode (1))
2N/A {
2N/A grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
2N/A return;
2N/A }
2N/A
2N/A stiex = grub_efi_open_protocol (grub_efi_system_table->console_in_handle,
2N/A &stiex_guid, GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
2N/A
2N/A grub_term_register_input ("console", &grub_console_term_input);
2N/A grub_term_register_output ("console", &grub_console_term_output);
2N/A}
2N/A
2N/Avoid
2N/Agrub_console_fini (void)
2N/A{
2N/A grub_term_unregister_input (&grub_console_term_input);
2N/A grub_term_unregister_output (&grub_console_term_output);
2N/A}