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 "bootstrap.h"
35 static SIMPLE_TEXT_OUTPUT_INTERFACE *conout;
36 static SIMPLE_INPUT_INTERFACE *conin;
39 #define DEFAULT_FGCOLOR EFI_LIGHTGRAY
40 #define DEFAULT_BGCOLOR EFI_BLACK
43 static int args[MAXARGS], argc;
44 static int fg_c, bg_c, curx, cury;
47 void get_pos(int *x, int *y);
48 void curs_move(int *_x, int *_y, int x, int y);
55 static unsigned keybuf[KEYBUFSZ]; /* keybuf for extended codes */
56 static int key_pending;
58 static void efi_cons_probe(struct console *);
59 static int efi_cons_init(int);
60 void efi_cons_putchar(int);
61 int efi_cons_getchar(void);
62 void efi_cons_efiputchar(int);
63 int efi_cons_poll(void);
65 struct console efi_console = {
78 /* Get cursor position. */
80 get_pos(int *x, int *y)
82 *x = conout->Mode->CursorColumn;
83 *y = conout->Mode->CursorRow;
86 /* Move cursor to x rows and y cols (0-based). */
88 curs_move(int *_x, int *_y, int x, int y)
90 conout->SetCursorPosition(conout, x, y);
92 *_x = conout->Mode->CursorColumn;
94 *_y = conout->Mode->CursorRow;
97 /* Clear internal state of the terminal emulation code. */
108 efi_cons_probe(struct console *cp)
112 cp->c_flags |= C_PRESENTIN | C_PRESENTOUT;
116 efi_cons_init(int arg)
119 conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR,
122 get_pos(&curx, &cury);
123 curs_move(&curx, &cury, curx, cury);
124 fg_c = DEFAULT_FGCOLOR;
125 bg_c = DEFAULT_BGCOLOR;
127 conout->EnableCursor(conout, TRUE);
132 efi_cons_rawputchar(int c)
136 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
141 n = 8 - ((conout->Mode->CursorColumn + 8) % 8);
142 for (i = 0; i < n; i++)
143 efi_cons_rawputchar(' ');
147 efi_cons_efiputchar('\r');
148 efi_cons_efiputchar(c);
153 efi_cons_efiputchar('\r');
156 efi_cons_efiputchar('\n');
157 efi_cons_efiputchar('\r');
165 efi_cons_efiputchar('\b');
170 efi_cons_efiputchar(c);
186 /* Gracefully exit ESC-sequence processing in case of misunderstanding. */
194 efi_cons_rawputchar('\033');
196 efi_cons_rawputchar(esc);
197 for (i = 0; i <= argc; ++i) {
198 sprintf(buf, "%d", args[i]);
201 efi_cons_rawputchar(*ch++);
204 efi_cons_rawputchar(c);
208 /* Clear display from current position to end of screen. */
214 get_pos(&curx, &cury);
215 if (curx == 0 && cury == 0) {
216 conout->ClearScreen(conout);
221 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
222 CL(0); /* clear current line from cursor to end */
223 for (i = cury + 1; i < y-1; i++) {
224 curs_move(NULL, NULL, 0, i);
227 curs_move(NULL, NULL, curx, cury);
232 * Absolute cursor move to args[0] rows and args[1] columns
233 * (the coordinates are 1-based).
242 curs_move(&curx, &cury, args[1], args[0]);
246 /* Home cursor (left top corner), also called from mode command. */
251 args[0] = args[1] = 1;
255 /* Clear line from current position to end of line */
263 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
265 case 0: /* from cursor to end */
268 case 1: /* from beginning to cursor */
271 case 2: /* entire line */
274 default: /* NOTREACHED */
281 line = malloc(len * sizeof (CHAR16));
283 printf("out of memory\n");
286 for (i = 0; i < len; i++)
291 curs_move(NULL, NULL, 0, cury);
293 conout->OutputString(conout, line);
294 /* restore cursor position */
295 curs_move(NULL, NULL, curx, cury);
306 args[argc] += c - '0';
309 /* Emulate basic capabilities of cons25 terminal */
313 static int ansi_col[] = {
314 0, 4, 2, 6, 1, 5, 3, 7
325 efi_cons_rawputchar(c);
346 else if (argc + 1 >= MAXARGS)
351 case 'H': /* ho = \E[H */
359 case 'J': /* cd = \E[J */
367 fg_c = DEFAULT_FGCOLOR;
368 bg_c = DEFAULT_BGCOLOR;
370 for (i = 0; i <= argc; ++i) {
372 case 0: /* back to normal */
373 fg_c = DEFAULT_FGCOLOR;
374 bg_c = DEFAULT_BGCOLOR;
379 case 4: /* underline */
383 case 7: /* reverse */
388 case 22: /* normal intensity */
391 case 24: /* not underline */
392 case 25: /* not blinking */
395 case 30: case 31: case 32: case 33:
396 case 34: case 35: case 36: case 37:
397 fg_c = ansi_col[args[i] - 30];
399 case 39: /* normal */
400 fg_c = DEFAULT_FGCOLOR;
402 case 40: case 41: case 42: case 43:
403 case 44: case 45: case 46: case 47:
404 bg_c = ansi_col[args[i] - 40];
406 case 49: /* normal */
407 bg_c = DEFAULT_BGCOLOR;
411 conout->SetAttribute(conout, EFI_TEXT_ATTR(fg_c, bg_c));
435 efi_cons_putchar(int c)
440 efi_cons_rawputchar(c);
449 for (i = 0; i < KEYBUFSZ; i++) {
450 if (keybuf[i] != 0) {
465 for (i = 0; i < KEYBUFSZ; i++) {
473 * We are not reading input before keybuf is empty, so we are safe
474 * just to fill keybuf from the beginning.
477 keybuf_inschar(EFI_INPUT_KEY *key)
480 switch (key->ScanCode) {
482 keybuf[0] = 0x1b; /* esc */
487 keybuf[0] = 0x1b; /* esc */
491 case 0x3: /* RIGHT */
492 keybuf[0] = 0x1b; /* esc */
497 keybuf[0] = 0x1b; /* esc */
502 keybuf[0] = 0x1b; /* esc */
505 keybuf[0] = key->UnicodeChar;
516 status = conin->ReadKeyStroke(conin, &key);
517 if (status == EFI_SUCCESS) {
518 keybuf_inschar(&key);
525 efi_cons_getchar(void)
529 if ((c = keybuf_getchar()) != 0)
535 return (keybuf_getchar());
544 if (keybuf_ischar() || key_pending)
548 * Some EFI implementation (u-boot for example) do not support
550 * CheckEvent() can clear the signaled state.
552 if (conin->WaitForKey == NULL)
553 key_pending = efi_readkey();
555 key_pending = BS->CheckEvent(conin->WaitForKey) == EFI_SUCCESS;
557 return (key_pending);
560 /* Plain direct access to EFI OutputString(). */
562 efi_cons_efiputchar(int c)
567 * translate box chars to unicode
571 case 0xb3: buf[0] = BOXDRAW_VERTICAL; break;
572 case 0xbf: buf[0] = BOXDRAW_DOWN_LEFT; break;
573 case 0xc0: buf[0] = BOXDRAW_UP_RIGHT; break;
574 case 0xc4: buf[0] = BOXDRAW_HORIZONTAL; break;
575 case 0xda: buf[0] = BOXDRAW_DOWN_RIGHT; break;
576 case 0xd9: buf[0] = BOXDRAW_UP_LEFT; break;
579 case 0xba: buf[0] = BOXDRAW_DOUBLE_VERTICAL; break;
580 case 0xbb: buf[0] = BOXDRAW_DOUBLE_DOWN_LEFT; break;
581 case 0xbc: buf[0] = BOXDRAW_DOUBLE_UP_LEFT; break;
582 case 0xc8: buf[0] = BOXDRAW_DOUBLE_UP_RIGHT; break;
583 case 0xc9: buf[0] = BOXDRAW_DOUBLE_DOWN_RIGHT; break;
584 case 0xcd: buf[0] = BOXDRAW_DOUBLE_HORIZONTAL; break;
589 buf[1] = 0; /* terminate string */
591 conout->OutputString(conout, buf);