2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
33 #include <sys/reboot.h>
35 #include "bootstrap.h"
37 static EFI_GUID simple_input_ex_guid = EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
38 static SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
39 static SIMPLE_INPUT_INTERFACE *conin;
40 static EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL *coninex;
42 static int mode; /* Does ConOut have serial console? */
44 static uint32_t utf8_left;
45 static uint32_t utf8_partial;
47 #define DEFAULT_FGCOLOR EFI_LIGHTGRAY
48 #define DEFAULT_BGCOLOR EFI_BLACK
51 static int args[MAXARGS], argc;
52 static int fg_c, bg_c, curx, cury;
55 void get_pos(int *x, int *y);
56 void curs_move(int *_x, int *_y, int x, int y);
62 static tf_bell_t efi_cons_bell;
63 static tf_cursor_t efi_text_cursor;
64 static tf_putchar_t efi_text_putchar;
65 static tf_fill_t efi_text_fill;
66 static tf_copy_t efi_text_copy;
67 static tf_param_t efi_text_param;
68 static tf_respond_t efi_cons_respond;
70 static teken_funcs_t tf = {
71 .tf_bell = efi_cons_bell,
72 .tf_cursor = efi_text_cursor,
73 .tf_putchar = efi_text_putchar,
74 .tf_fill = efi_text_fill,
75 .tf_copy = efi_text_copy,
76 .tf_param = efi_text_param,
77 .tf_respond = efi_cons_respond,
88 static struct text_pixel *buffer;
91 static unsigned keybuf[KEYBUFSZ]; /* keybuf for extended codes */
92 static int key_pending;
94 static const unsigned char teken_color_to_efi_color[16] = {
113 static void efi_cons_probe(struct console *);
114 static int efi_cons_init(int);
115 void efi_cons_putchar(int);
116 int efi_cons_getchar(void);
117 void efi_cons_efiputchar(int);
118 int efi_cons_poll(void);
120 struct console efi_console = {
135 efi_cons_bell(void *s __unused)
140 efi_text_cursor(void *s __unused, const teken_pos_t *p)
144 (void) conout->QueryMode(conout, conout->Mode->Mode, &col, &row);
146 if (p->tp_col == col)
151 if (p->tp_row == row)
156 conout->SetCursorPosition(conout, col, row);
160 efi_text_printchar(const teken_pos_t *p, bool autoscroll)
163 struct text_pixel *px;
164 teken_color_t fg, bg, tmp;
166 px = buffer + p->tp_col + p->tp_row * tp.tp_col;
167 a = conout->Mode->Attribute;
169 fg = teken_256to16(px->a.ta_fgcolor);
170 bg = teken_256to16(px->a.ta_bgcolor);
171 if (px->a.ta_format & TF_BOLD)
173 if (px->a.ta_format & TF_BLINK)
176 if (px->a.ta_format & TF_REVERSE) {
182 attr = EFI_TEXT_ATTR(teken_color_to_efi_color[fg],
183 teken_color_to_efi_color[bg] & 0x7);
185 conout->SetCursorPosition(conout, p->tp_col, p->tp_row);
187 /* to prvent autoscroll, skip print of lower right char */
189 p->tp_row == tp.tp_row - 1 &&
190 p->tp_col == tp.tp_col - 1)
193 (void) conout->SetAttribute(conout, attr);
194 efi_cons_efiputchar(px->c);
195 (void) conout->SetAttribute(conout, a);
199 efi_text_putchar(void *s __unused, const teken_pos_t *p, teken_char_t c,
200 const teken_attr_t *a)
205 idx = p->tp_col + p->tp_row * tp.tp_col;
208 efi_text_printchar(p, false);
212 efi_text_fill(void *s, const teken_rect_t *r, teken_char_t c,
213 const teken_attr_t *a)
218 (void) conout->QueryMode(conout, conout->Mode->Mode, &col, &row);
220 conout->EnableCursor(conout, FALSE);
221 for (p.tp_row = r->tr_begin.tp_row; p.tp_row < r->tr_end.tp_row;
223 for (p.tp_col = r->tr_begin.tp_col;
224 p.tp_col < r->tr_end.tp_col; p.tp_col++)
225 efi_text_putchar(s, &p, c, a);
226 conout->EnableCursor(conout, TRUE);
230 efi_same_pixel(struct text_pixel *px1, struct text_pixel *px2)
232 if (px1->c != px2->c)
235 if (px1->a.ta_format != px2->a.ta_format)
237 if (px1->a.ta_fgcolor != px2->a.ta_fgcolor)
239 if (px1->a.ta_bgcolor != px2->a.ta_bgcolor)
246 efi_text_copy(void *ptr __unused, const teken_rect_t *r, const teken_pos_t *p)
249 int nrow, ncol, x, y; /* Has to be signed - >= 0 comparison */
254 * Copying is a little tricky. We must make sure we do it in
255 * correct order, to make sure we don't overwrite our own data.
258 nrow = r->tr_end.tp_row - r->tr_begin.tp_row;
259 ncol = r->tr_end.tp_col - r->tr_begin.tp_col;
262 * Check if we do copy whole screen.
264 if (p->tp_row == 0 && p->tp_col == 0 &&
265 nrow == tp.tp_row - 2 && ncol == tp.tp_col - 2)
268 conout->EnableCursor(conout, FALSE);
269 if (p->tp_row < r->tr_begin.tp_row) {
270 /* Copy from bottom to top. */
271 for (y = 0; y < nrow; y++) {
272 d.tp_row = p->tp_row + y;
273 s.tp_row = r->tr_begin.tp_row + y;
274 drow = d.tp_row * tp.tp_col;
275 srow = s.tp_row * tp.tp_col;
276 for (x = 0; x < ncol; x++) {
277 d.tp_col = p->tp_col + x;
278 s.tp_col = r->tr_begin.tp_col + x;
281 &buffer[d.tp_col + drow],
282 &buffer[s.tp_col + srow])) {
283 buffer[d.tp_col + drow] =
284 buffer[s.tp_col + srow];
286 efi_text_printchar(&d, false);
289 * Draw last char and trigger
294 efi_text_printchar(&d, true);
300 /* Copy from top to bottom. */
301 if (p->tp_col < r->tr_begin.tp_col) {
302 /* Copy from right to left. */
303 for (y = nrow - 1; y >= 0; y--) {
304 d.tp_row = p->tp_row + y;
305 s.tp_row = r->tr_begin.tp_row + y;
306 drow = d.tp_row * tp.tp_col;
307 srow = s.tp_row * tp.tp_col;
308 for (x = 0; x < ncol; x++) {
309 d.tp_col = p->tp_col + x;
310 s.tp_col = r->tr_begin.tp_col + x;
313 &buffer[d.tp_col + drow],
314 &buffer[s.tp_col + srow])) {
315 buffer[d.tp_col + drow] =
316 buffer[s.tp_col + srow];
317 efi_text_printchar(&d, false);
322 /* Copy from left to right. */
323 for (y = nrow - 1; y >= 0; y--) {
324 d.tp_row = p->tp_row + y;
325 s.tp_row = r->tr_begin.tp_row + y;
326 drow = d.tp_row * tp.tp_col;
327 srow = s.tp_row * tp.tp_col;
328 for (x = ncol - 1; x >= 0; x--) {
329 d.tp_col = p->tp_col + x;
330 s.tp_col = r->tr_begin.tp_col + x;
333 &buffer[d.tp_col + drow],
334 &buffer[s.tp_col + srow])) {
335 buffer[d.tp_col + drow] =
336 buffer[s.tp_col + srow];
337 efi_text_printchar(&d, false);
343 conout->EnableCursor(conout, TRUE);
347 efi_text_param(void *s __unused, int cmd, unsigned int value)
350 case TP_SETLOCALCURSOR:
352 * 0 means normal (usually block), 1 means hidden, and
353 * 2 means blinking (always block) for compatibility with
354 * syscons. We don't support any changes except hiding,
355 * so must map 2 to 0.
357 value = (value == 1) ? 0 : 1;
361 conout->EnableCursor(conout, TRUE);
363 conout->EnableCursor(conout, FALSE);
366 /* Not yet implemented */
375 efi_cons_respond(void *s __unused, const void *buf __unused,
381 * Set up conin/conout/coninex to make sure we have input ready.
384 efi_cons_probe(struct console *cp)
391 status = BS->OpenProtocol(ST->ConsoleInHandle, &simple_input_ex_guid,
392 (void **)&coninex, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
393 if (status != EFI_SUCCESS)
396 cp->c_flags |= C_PRESENTIN | C_PRESENTOUT;
400 color_name_to_teken(const char *name, int *val)
402 if (strcasecmp(name, "black") == 0) {
406 if (strcasecmp(name, "red") == 0) {
410 if (strcasecmp(name, "green") == 0) {
414 if (strcasecmp(name, "brown") == 0) {
418 if (strcasecmp(name, "blue") == 0) {
422 if (strcasecmp(name, "magenta") == 0) {
426 if (strcasecmp(name, "cyan") == 0) {
430 if (strcasecmp(name, "white") == 0) {
438 efi_set_colors(struct env_var *ev, int flags, const void *value)
443 const teken_attr_t *ap;
449 if (color_name_to_teken(value, &val)) {
450 snprintf(buf, sizeof (buf), "%d", val);
456 val = (int)strtol(value, &end, 0);
457 if (errno != 0 || *end != '\0') {
458 printf("Allowed values are either ansi color name or "
459 "number from range [0-7].\n");
465 ap = teken_get_defattr(&teken);
467 if (strcmp(ev->ev_name, "teken.fg_color") == 0) {
468 /* is it already set? */
469 if (ap->ta_fgcolor == val)
473 if (strcmp(ev->ev_name, "teken.bg_color") == 0) {
474 /* is it already set? */
475 if (ap->ta_bgcolor == val)
479 env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL);
480 teken_set_defattr(&teken, &a);
485 /* Get cursor position. */
487 get_pos(int *x, int *y)
489 *x = conout->Mode->CursorColumn;
490 *y = conout->Mode->CursorRow;
493 /* Move cursor to x rows and y cols (0-based). */
495 curs_move(int *_x, int *_y, int x, int y)
497 conout->SetCursorPosition(conout, x, y);
499 *_x = conout->Mode->CursorColumn;
501 *_y = conout->Mode->CursorRow;
504 /* Clear internal state of the terminal emulation code. */
514 efi_cons_rawputchar(int c)
518 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
523 n = 8 - ((conout->Mode->CursorColumn + 8) % 8);
524 for (i = 0; i < n; i++)
525 efi_cons_rawputchar(' ');
529 efi_cons_efiputchar('\r');
530 efi_cons_efiputchar(c);
535 efi_cons_efiputchar('\r');
538 efi_cons_efiputchar('\n');
539 efi_cons_efiputchar('\r');
547 efi_cons_efiputchar('\b');
552 efi_cons_efiputchar(c);
565 conout->EnableCursor(conout, TRUE);
569 /* Gracefully exit ESC-sequence processing in case of misunderstanding. */
577 efi_cons_rawputchar('\033');
579 efi_cons_rawputchar(esc);
580 for (i = 0; i <= argc; ++i) {
581 sprintf(buf, "%d", args[i]);
584 efi_cons_rawputchar(*ch++);
587 efi_cons_rawputchar(c);
591 /* Clear display from current position to end of screen. */
598 get_pos(&curx, &cury);
599 if (curx == 0 && cury == 0) {
600 conout->ClearScreen(conout);
605 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
606 CL(0); /* clear current line from cursor to end */
607 for (i = cury + 1; i < y-1; i++) {
608 curs_move(NULL, NULL, 0, i);
611 curs_move(NULL, NULL, curx, cury);
616 * Absolute cursor move to args[0] rows and args[1] columns
617 * (the coordinates are 1-based).
626 curs_move(&curx, &cury, args[1], args[0]);
630 /* Home cursor (left top corner), also called from mode command. */
635 args[0] = args[1] = 1;
639 /* Clear line from current position to end of line */
647 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
649 case 0: /* from cursor to end */
652 case 1: /* from beginning to cursor */
655 case 2: /* entire line */
658 default: /* NOTREACHED */
665 line = malloc(len * sizeof (CHAR16));
667 printf("out of memory\n");
670 for (i = 0; i < len; i++)
675 curs_move(NULL, NULL, 0, cury);
677 conout->OutputString(conout, line);
678 /* restore cursor position */
679 curs_move(NULL, NULL, curx, cury);
690 args[argc] += c - '0';
694 /* Emulate basic capabilities of cons25 terminal */
699 static int ansi_col[] = {
700 0, 4, 2, 6, 1, 5, 3, 7
712 efi_cons_rawputchar(c);
733 else if (argc + 1 >= MAXARGS)
738 case 'H': /* ho = \E[H */
746 case 'J': /* cd = \E[J */
754 fg_c = DEFAULT_FGCOLOR;
755 bg_c = DEFAULT_BGCOLOR;
757 for (i = 0; i <= argc; ++i) {
759 case 0: /* back to normal */
760 fg_c = DEFAULT_FGCOLOR;
761 bg_c = DEFAULT_BGCOLOR;
766 case 4: /* underline */
770 case 7: /* reverse */
775 case 22: /* normal intensity */
778 case 24: /* not underline */
779 case 25: /* not blinking */
782 case 30: case 31: case 32: case 33:
783 case 34: case 35: case 36: case 37:
784 fg_c = ansi_col[args[i] - 30];
786 case 39: /* normal */
787 fg_c = DEFAULT_FGCOLOR;
789 case 40: case 41: case 42: case 43:
790 case 44: case 45: case 46: case 47:
791 bg_c = ansi_col[args[i] - 40];
793 case 49: /* normal */
794 bg_c = DEFAULT_BGCOLOR;
798 conout->SetAttribute(conout, EFI_TEXT_ATTR(fg_c, bg_c));
814 efi_cons_rawputchar(c);
819 efi_cons_update_mode(void)
822 const teken_attr_t *a;
826 status = conout->QueryMode(conout, conout->Mode->Mode, &cols, &rows);
827 if (EFI_ERROR(status)) {
833 * When we have serial port listed in ConOut, use pre-teken emulator,
835 * The problem is, we can not output text on efi and comconsole when
836 * efi also has comconsole bound. But then again, we need to have
837 * terminal emulator for efi text mode to support the menu.
838 * While teken is too expensive to be used on serial console, the
839 * pre-teken emulator is light enough to be used on serial console.
841 * When doing multiple consoles (both serial and video),
842 * also just use the old emulator. RB_MULTIPLE also implies
843 * we're using a serial console.
845 mode = parse_uefi_con_out();
846 if ((mode & (RB_SERIAL | RB_MULTIPLE)) == 0) {
847 if (buffer != NULL) {
848 if (tp.tp_row == rows && tp.tp_col == cols)
852 teken_init(&teken, &tf, NULL);
857 buffer = malloc(rows * cols * sizeof(*buffer));
861 teken_set_winsize(&teken, &tp);
862 a = teken_get_defattr(&teken);
864 snprintf(env, sizeof(env), "%d", a->ta_fgcolor);
865 env_setenv("teken.fg_color", EV_VOLATILE, env, efi_set_colors,
867 snprintf(env, sizeof(env), "%d", a->ta_bgcolor);
868 env_setenv("teken.bg_color", EV_VOLATILE, env, efi_set_colors,
871 for (int row = 0; row < rows; row++) {
872 for (int col = 0; col < cols; col++) {
873 buffer[col + row * tp.tp_col].c = ' ';
874 buffer[col + row * tp.tp_col].a = *a;
879 conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR,
882 get_pos(&curx, &cury);
883 curs_move(&curx, &cury, curx, cury);
884 fg_c = DEFAULT_FGCOLOR;
885 bg_c = DEFAULT_BGCOLOR;
889 snprintf(env, sizeof (env), "%u", (unsigned)rows);
890 setenv("LINES", env, 1);
891 snprintf(env, sizeof (env), "%u", (unsigned)cols);
892 setenv("COLUMNS", env, 1);
898 efi_cons_init(int arg)
905 conout->EnableCursor(conout, TRUE);
906 if (efi_cons_update_mode())
921 for (i = 0; i < sizeof(utf8_partial); i++) {
922 c = (utf8_partial >> (24 - (i << 3))) & 0xff;
931 input_byte(uint8_t c)
933 if ((c & 0x80) == 0x00) {
934 /* One-byte sequence. */
939 if ((c & 0xe0) == 0xc0) {
940 /* Two-byte sequence. */
946 if ((c & 0xf0) == 0xe0) {
947 /* Three-byte sequence. */
953 if ((c & 0xf8) == 0xf0) {
954 /* Four-byte sequence. */
960 if ((c & 0xc0) == 0x80) {
962 if (utf8_left == 0) {
967 utf8_partial = (utf8_partial << 8) | c;
968 if (utf8_left == 0) {
974 b = (u >> 24) & 0xff;
975 if (b != 0) { /* Four-byte sequence */
977 b = (u >> 16) & 0xff;
978 v = (v << 6) | (b & 0x3f);
980 v = (v << 6) | (b & 0x3f);
982 v = (v << 6) | (b & 0x3f);
983 } else if ((b = (u >> 16) & 0xff) != 0) {
984 v = b & 0x0f; /* Three-byte sequence */
986 v = (v << 6) | (b & 0x3f);
988 v = (v << 6) | (b & 0x3f);
989 } else if ((b = (u >> 8) & 0xff) != 0) {
990 v = b & 0x1f; /* Two-byte sequence */
992 v = (v << 6) | (b & 0x3f);
994 /* Send unicode char directly to console. */
995 efi_cons_efiputchar(v);
1000 /* Anything left is illegal in UTF-8 sequence. */
1006 efi_cons_putchar(int c)
1008 unsigned char ch = c;
1011 * Don't use Teken when we're doing pure serial, or a multiple console
1012 * with video "primary" because that's also serial.
1014 if ((mode & (RB_SERIAL | RB_MULTIPLE)) != 0) {
1020 teken_input(&teken, &ch, sizeof (ch));
1022 efi_cons_efiputchar(c);
1026 keybuf_getchar(void)
1030 for (i = 0; i < KEYBUFSZ; i++) {
1031 if (keybuf[i] != 0) {
1046 for (i = 0; i < KEYBUFSZ; i++) {
1054 * We are not reading input before keybuf is empty, so we are safe
1055 * just to fill keybuf from the beginning.
1058 keybuf_inschar(EFI_INPUT_KEY *key)
1061 switch (key->ScanCode) {
1062 case SCAN_UP: /* UP */
1063 keybuf[0] = 0x1b; /* esc */
1067 case SCAN_DOWN: /* DOWN */
1068 keybuf[0] = 0x1b; /* esc */
1072 case SCAN_RIGHT: /* RIGHT */
1073 keybuf[0] = 0x1b; /* esc */
1077 case SCAN_LEFT: /* LEFT */
1078 keybuf[0] = 0x1b; /* esc */
1083 keybuf[0] = CHAR_BACKSPACE;
1086 keybuf[0] = 0x1b; /* esc */
1089 keybuf[0] = key->UnicodeChar;
1100 status = conin->ReadKeyStroke(conin, &key);
1101 if (status == EFI_SUCCESS) {
1102 keybuf_inschar(&key);
1109 efi_readkey_ex(void)
1113 EFI_KEY_DATA key_data;
1116 status = coninex->ReadKeyStrokeEx(coninex, &key_data);
1117 if (status == EFI_SUCCESS) {
1118 kss = key_data.KeyState.KeyShiftState;
1120 if (kss & EFI_SHIFT_STATE_VALID) {
1123 * quick mapping to control chars, replace with
1126 if (kss & EFI_RIGHT_CONTROL_PRESSED ||
1127 kss & EFI_LEFT_CONTROL_PRESSED) {
1128 if (kp->UnicodeChar >= 'a' &&
1129 kp->UnicodeChar <= 'z') {
1130 kp->UnicodeChar -= 'a';
1136 * The shift state and/or toggle state may not be valid,
1137 * but we still can have ScanCode or UnicodeChar.
1139 if (kp->ScanCode == 0 && kp->UnicodeChar == 0)
1148 efi_cons_getchar(void)
1152 if ((c = keybuf_getchar()) != 0)
1157 if (coninex == NULL) {
1159 return (keybuf_getchar());
1161 if (efi_readkey_ex())
1162 return (keybuf_getchar());
1173 if (keybuf_ischar() || key_pending)
1177 * Some EFI implementation (u-boot for example) do not support
1179 * CheckEvent() can clear the signaled state.
1181 if (coninex != NULL) {
1182 if (coninex->WaitForKeyEx == NULL) {
1183 key_pending = efi_readkey_ex();
1185 status = BS->CheckEvent(coninex->WaitForKeyEx);
1186 key_pending = status == EFI_SUCCESS;
1189 if (conin->WaitForKey == NULL) {
1190 key_pending = efi_readkey();
1192 status = BS->CheckEvent(conin->WaitForKey);
1193 key_pending = status == EFI_SUCCESS;
1197 return (key_pending);
1200 /* Plain direct access to EFI OutputString(). */
1202 efi_cons_efiputchar(int c)
1208 buf[1] = 0; /* terminate string */
1210 status = conout->TestString(conout, buf);
1211 if (EFI_ERROR(status))
1213 conout->OutputString(conout, buf);