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 efi_cons_probe(struct console *cp)
383 cp->c_flags |= C_PRESENTIN | C_PRESENTOUT;
387 color_name_to_teken(const char *name, int *val)
389 if (strcasecmp(name, "black") == 0) {
393 if (strcasecmp(name, "red") == 0) {
397 if (strcasecmp(name, "green") == 0) {
401 if (strcasecmp(name, "brown") == 0) {
405 if (strcasecmp(name, "blue") == 0) {
409 if (strcasecmp(name, "magenta") == 0) {
413 if (strcasecmp(name, "cyan") == 0) {
417 if (strcasecmp(name, "white") == 0) {
425 efi_set_colors(struct env_var *ev, int flags, const void *value)
430 const teken_attr_t *ap;
436 if (color_name_to_teken(value, &val)) {
437 snprintf(buf, sizeof (buf), "%d", val);
443 val = (int)strtol(value, &end, 0);
444 if (errno != 0 || *end != '\0') {
445 printf("Allowed values are either ansi color name or "
446 "number from range [0-7].\n");
452 ap = teken_get_defattr(&teken);
454 if (strcmp(ev->ev_name, "teken.fg_color") == 0) {
455 /* is it already set? */
456 if (ap->ta_fgcolor == val)
460 if (strcmp(ev->ev_name, "teken.bg_color") == 0) {
461 /* is it already set? */
462 if (ap->ta_bgcolor == val)
466 env_setenv(ev->ev_name, flags | EV_NOHOOK, evalue, NULL, NULL);
467 teken_set_defattr(&teken, &a);
472 /* Get cursor position. */
474 get_pos(int *x, int *y)
476 *x = conout->Mode->CursorColumn;
477 *y = conout->Mode->CursorRow;
480 /* Move cursor to x rows and y cols (0-based). */
482 curs_move(int *_x, int *_y, int x, int y)
484 conout->SetCursorPosition(conout, x, y);
486 *_x = conout->Mode->CursorColumn;
488 *_y = conout->Mode->CursorRow;
491 /* Clear internal state of the terminal emulation code. */
501 efi_cons_rawputchar(int c)
505 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
510 n = 8 - ((conout->Mode->CursorColumn + 8) % 8);
511 for (i = 0; i < n; i++)
512 efi_cons_rawputchar(' ');
516 efi_cons_efiputchar('\r');
517 efi_cons_efiputchar(c);
522 efi_cons_efiputchar('\r');
525 efi_cons_efiputchar('\n');
526 efi_cons_efiputchar('\r');
534 efi_cons_efiputchar('\b');
539 efi_cons_efiputchar(c);
552 conout->EnableCursor(conout, TRUE);
556 /* Gracefully exit ESC-sequence processing in case of misunderstanding. */
564 efi_cons_rawputchar('\033');
566 efi_cons_rawputchar(esc);
567 for (i = 0; i <= argc; ++i) {
568 sprintf(buf, "%d", args[i]);
571 efi_cons_rawputchar(*ch++);
574 efi_cons_rawputchar(c);
578 /* Clear display from current position to end of screen. */
585 get_pos(&curx, &cury);
586 if (curx == 0 && cury == 0) {
587 conout->ClearScreen(conout);
592 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
593 CL(0); /* clear current line from cursor to end */
594 for (i = cury + 1; i < y-1; i++) {
595 curs_move(NULL, NULL, 0, i);
598 curs_move(NULL, NULL, curx, cury);
603 * Absolute cursor move to args[0] rows and args[1] columns
604 * (the coordinates are 1-based).
613 curs_move(&curx, &cury, args[1], args[0]);
617 /* Home cursor (left top corner), also called from mode command. */
622 args[0] = args[1] = 1;
626 /* Clear line from current position to end of line */
634 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
636 case 0: /* from cursor to end */
639 case 1: /* from beginning to cursor */
642 case 2: /* entire line */
645 default: /* NOTREACHED */
652 line = malloc(len * sizeof (CHAR16));
654 printf("out of memory\n");
657 for (i = 0; i < len; i++)
662 curs_move(NULL, NULL, 0, cury);
664 conout->OutputString(conout, line);
665 /* restore cursor position */
666 curs_move(NULL, NULL, curx, cury);
677 args[argc] += c - '0';
681 /* Emulate basic capabilities of cons25 terminal */
686 static int ansi_col[] = {
687 0, 4, 2, 6, 1, 5, 3, 7
699 efi_cons_rawputchar(c);
720 else if (argc + 1 >= MAXARGS)
725 case 'H': /* ho = \E[H */
733 case 'J': /* cd = \E[J */
741 fg_c = DEFAULT_FGCOLOR;
742 bg_c = DEFAULT_BGCOLOR;
744 for (i = 0; i <= argc; ++i) {
746 case 0: /* back to normal */
747 fg_c = DEFAULT_FGCOLOR;
748 bg_c = DEFAULT_BGCOLOR;
753 case 4: /* underline */
757 case 7: /* reverse */
762 case 22: /* normal intensity */
765 case 24: /* not underline */
766 case 25: /* not blinking */
769 case 30: case 31: case 32: case 33:
770 case 34: case 35: case 36: case 37:
771 fg_c = ansi_col[args[i] - 30];
773 case 39: /* normal */
774 fg_c = DEFAULT_FGCOLOR;
776 case 40: case 41: case 42: case 43:
777 case 44: case 45: case 46: case 47:
778 bg_c = ansi_col[args[i] - 40];
780 case 49: /* normal */
781 bg_c = DEFAULT_BGCOLOR;
785 conout->SetAttribute(conout, EFI_TEXT_ATTR(fg_c, bg_c));
801 efi_cons_rawputchar(c);
806 efi_cons_update_mode(void)
809 const teken_attr_t *a;
813 status = conout->QueryMode(conout, conout->Mode->Mode, &cols, &rows);
814 if (EFI_ERROR(status)) {
820 * When we have serial port listed in ConOut, use pre-teken emulator,
822 * The problem is, we can not output text on efi and comconsole when
823 * efi also has comconsole bound. But then again, we need to have
824 * terminal emulator for efi text mode to support the menu.
825 * While teken is too expensive to be used on serial console, the
826 * pre-teken emulator is light enough to be used on serial console.
828 * When doing multiple consoles (both serial and video),
829 * also just use the old emulator. RB_MULTIPLE also implies
830 * we're using a serial console.
832 mode = parse_uefi_con_out();
833 if ((mode & (RB_SERIAL | RB_MULTIPLE)) == 0) {
834 if (buffer != NULL) {
835 if (tp.tp_row == rows && tp.tp_col == cols)
839 teken_init(&teken, &tf, NULL);
844 buffer = malloc(rows * cols * sizeof(*buffer));
848 teken_set_winsize(&teken, &tp);
849 a = teken_get_defattr(&teken);
851 snprintf(env, sizeof(env), "%d", a->ta_fgcolor);
852 env_setenv("teken.fg_color", EV_VOLATILE, env, efi_set_colors,
854 snprintf(env, sizeof(env), "%d", a->ta_bgcolor);
855 env_setenv("teken.bg_color", EV_VOLATILE, env, efi_set_colors,
858 for (int row = 0; row < rows; row++) {
859 for (int col = 0; col < cols; col++) {
860 buffer[col + row * tp.tp_col].c = ' ';
861 buffer[col + row * tp.tp_col].a = *a;
866 conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR,
869 get_pos(&curx, &cury);
870 curs_move(&curx, &cury, curx, cury);
871 fg_c = DEFAULT_FGCOLOR;
872 bg_c = DEFAULT_BGCOLOR;
876 snprintf(env, sizeof (env), "%u", (unsigned)rows);
877 setenv("LINES", env, 1);
878 snprintf(env, sizeof (env), "%u", (unsigned)cols);
879 setenv("COLUMNS", env, 1);
885 efi_cons_init(int arg)
895 conout->EnableCursor(conout, TRUE);
896 status = BS->OpenProtocol(ST->ConsoleInHandle, &simple_input_ex_guid,
897 (void **)&coninex, IH, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);
898 if (status != EFI_SUCCESS)
901 if (efi_cons_update_mode())
916 for (i = 0; i < sizeof(utf8_partial); i++) {
917 c = (utf8_partial >> (24 - (i << 3))) & 0xff;
926 input_byte(uint8_t c)
928 if ((c & 0x80) == 0x00) {
929 /* One-byte sequence. */
934 if ((c & 0xe0) == 0xc0) {
935 /* Two-byte sequence. */
941 if ((c & 0xf0) == 0xe0) {
942 /* Three-byte sequence. */
948 if ((c & 0xf8) == 0xf0) {
949 /* Four-byte sequence. */
955 if ((c & 0xc0) == 0x80) {
957 if (utf8_left == 0) {
962 utf8_partial = (utf8_partial << 8) | c;
963 if (utf8_left == 0) {
969 b = (u >> 24) & 0xff;
970 if (b != 0) { /* Four-byte sequence */
972 b = (u >> 16) & 0xff;
973 v = (v << 6) | (b & 0x3f);
975 v = (v << 6) | (b & 0x3f);
977 v = (v << 6) | (b & 0x3f);
978 } else if ((b = (u >> 16) & 0xff) != 0) {
979 v = b & 0x0f; /* Three-byte sequence */
981 v = (v << 6) | (b & 0x3f);
983 v = (v << 6) | (b & 0x3f);
984 } else if ((b = (u >> 8) & 0xff) != 0) {
985 v = b & 0x1f; /* Two-byte sequence */
987 v = (v << 6) | (b & 0x3f);
989 /* Send unicode char directly to console. */
990 efi_cons_efiputchar(v);
995 /* Anything left is illegal in UTF-8 sequence. */
1001 efi_cons_putchar(int c)
1003 unsigned char ch = c;
1006 * Don't use Teken when we're doing pure serial, or a multiple console
1007 * with video "primary" because that's also serial.
1009 if ((mode & (RB_SERIAL | RB_MULTIPLE)) != 0) {
1015 teken_input(&teken, &ch, sizeof (ch));
1017 efi_cons_efiputchar(c);
1021 keybuf_getchar(void)
1025 for (i = 0; i < KEYBUFSZ; i++) {
1026 if (keybuf[i] != 0) {
1041 for (i = 0; i < KEYBUFSZ; i++) {
1049 * We are not reading input before keybuf is empty, so we are safe
1050 * just to fill keybuf from the beginning.
1053 keybuf_inschar(EFI_INPUT_KEY *key)
1056 switch (key->ScanCode) {
1057 case SCAN_UP: /* UP */
1058 keybuf[0] = 0x1b; /* esc */
1062 case SCAN_DOWN: /* DOWN */
1063 keybuf[0] = 0x1b; /* esc */
1067 case SCAN_RIGHT: /* RIGHT */
1068 keybuf[0] = 0x1b; /* esc */
1072 case SCAN_LEFT: /* LEFT */
1073 keybuf[0] = 0x1b; /* esc */
1078 keybuf[0] = CHAR_BACKSPACE;
1081 keybuf[0] = 0x1b; /* esc */
1084 keybuf[0] = key->UnicodeChar;
1095 status = conin->ReadKeyStroke(conin, &key);
1096 if (status == EFI_SUCCESS) {
1097 keybuf_inschar(&key);
1104 efi_readkey_ex(void)
1108 EFI_KEY_DATA key_data;
1111 status = coninex->ReadKeyStrokeEx(coninex, &key_data);
1112 if (status == EFI_SUCCESS) {
1113 kss = key_data.KeyState.KeyShiftState;
1115 if (kss & EFI_SHIFT_STATE_VALID) {
1118 * quick mapping to control chars, replace with
1121 if (kss & EFI_RIGHT_CONTROL_PRESSED ||
1122 kss & EFI_LEFT_CONTROL_PRESSED) {
1123 if (kp->UnicodeChar >= 'a' &&
1124 kp->UnicodeChar <= 'z') {
1125 kp->UnicodeChar -= 'a';
1131 * The shift state and/or toggle state may not be valid,
1132 * but we still can have ScanCode or UnicodeChar.
1134 if (kp->ScanCode == 0 && kp->UnicodeChar == 0)
1143 efi_cons_getchar(void)
1147 if ((c = keybuf_getchar()) != 0)
1152 if (coninex == NULL) {
1154 return (keybuf_getchar());
1156 if (efi_readkey_ex())
1157 return (keybuf_getchar());
1168 if (keybuf_ischar() || key_pending)
1172 * Some EFI implementation (u-boot for example) do not support
1174 * CheckEvent() can clear the signaled state.
1176 if (coninex != NULL) {
1177 if (coninex->WaitForKeyEx == NULL) {
1178 key_pending = efi_readkey_ex();
1180 status = BS->CheckEvent(coninex->WaitForKeyEx);
1181 key_pending = status == EFI_SUCCESS;
1184 if (conin->WaitForKey == NULL) {
1185 key_pending = efi_readkey();
1187 status = BS->CheckEvent(conin->WaitForKey);
1188 key_pending = status == EFI_SUCCESS;
1192 return (key_pending);
1195 /* Plain direct access to EFI OutputString(). */
1197 efi_cons_efiputchar(int c)
1203 buf[1] = 0; /* terminate string */
1205 status = conout->TestString(conout, buf);
1206 if (EFI_ERROR(status))
1208 conout->OutputString(conout, buf);