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);
54 static EFI_INPUT_KEY key_cur;
55 static int key_pending;
57 static void efi_cons_probe(struct console *);
58 static int efi_cons_init(int);
59 void efi_cons_putchar(int);
60 int efi_cons_getchar(void);
61 void efi_cons_efiputchar(int);
62 int efi_cons_poll(void);
64 struct console efi_console = {
77 /* Get cursor position. */
79 get_pos(int *x, int *y)
81 *x = conout->Mode->CursorColumn;
82 *y = conout->Mode->CursorRow;
85 /* Move cursor to x rows and y cols (0-based). */
87 curs_move(int *_x, int *_y, int x, int y)
89 conout->SetCursorPosition(conout, x, y);
91 *_x = conout->Mode->CursorColumn;
93 *_y = conout->Mode->CursorRow;
96 /* Clear internal state of the terminal emulation code. */
107 efi_cons_probe(struct console *cp)
111 cp->c_flags |= C_PRESENTIN | C_PRESENTOUT;
115 efi_cons_init(int arg)
118 conout->SetAttribute(conout, EFI_TEXT_ATTR(DEFAULT_FGCOLOR,
121 get_pos(&curx, &cury);
122 curs_move(&curx, &cury, curx, cury);
123 fg_c = DEFAULT_FGCOLOR;
124 bg_c = DEFAULT_BGCOLOR;
126 conout->EnableCursor(conout, TRUE);
131 efi_cons_rawputchar(int c)
135 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
138 /* XXX lame tab expansion */
139 for (i = 0; i < 8; i++)
140 efi_cons_rawputchar(' ');
144 efi_cons_efiputchar('\r');
145 efi_cons_efiputchar(c);
150 efi_cons_efiputchar('\r');
153 efi_cons_efiputchar('\n');
154 efi_cons_efiputchar('\r');
162 efi_cons_efiputchar('\b');
167 efi_cons_efiputchar(c);
183 /* Gracefully exit ESC-sequence processing in case of misunderstanding. */
191 efi_cons_rawputchar('\033');
193 efi_cons_rawputchar(esc);
194 for (i = 0; i <= argc; ++i) {
195 sprintf(buf, "%d", args[i]);
198 efi_cons_rawputchar(*ch++);
201 efi_cons_rawputchar(c);
205 /* Clear display from current position to end of screen. */
211 get_pos(&curx, &cury);
212 if (curx == 0 && cury == 0) {
213 conout->ClearScreen(conout);
218 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
219 CL(0); /* clear current line from cursor to end */
220 for (i = cury + 1; i < y-1; i++) {
221 curs_move(NULL, NULL, 0, i);
224 curs_move(NULL, NULL, curx, cury);
229 * Absolute cursor move to args[0] rows and args[1] columns
230 * (the coordinates are 1-based).
239 curs_move(&curx, &cury, args[1], args[0]);
243 /* Home cursor (left top corner), also called from mode command. */
248 args[0] = args[1] = 1;
252 /* Clear line from current position to end of line */
260 conout->QueryMode(conout, conout->Mode->Mode, &x, &y);
262 case 0: /* from cursor to end */
265 case 1: /* from beginning to cursor */
268 case 2: /* entire line */
271 default: /* NOTREACHED */
278 line = malloc(len * sizeof (CHAR16));
280 printf("out of memory\n");
283 for (i = 0; i < len; i++)
288 curs_move(NULL, NULL, 0, cury);
290 conout->OutputString(conout, line);
291 /* restore cursor position */
292 curs_move(NULL, NULL, curx, cury);
303 args[argc] += c - '0';
306 /* Emulate basic capabilities of cons25 terminal */
310 static int ansi_col[] = {
311 0, 4, 2, 6, 1, 5, 3, 7
322 efi_cons_rawputchar(c);
343 else if (argc + 1 >= MAXARGS)
348 case 'H': /* ho = \E[H */
356 case 'J': /* cd = \E[J */
364 fg_c = DEFAULT_FGCOLOR;
365 bg_c = DEFAULT_BGCOLOR;
367 for (i = 0; i <= argc; ++i) {
369 case 0: /* back to normal */
370 fg_c = DEFAULT_FGCOLOR;
371 bg_c = DEFAULT_BGCOLOR;
376 case 4: /* underline */
380 case 7: /* reverse */
385 case 22: /* normal intensity */
388 case 24: /* not underline */
389 case 25: /* not blinking */
392 case 30: case 31: case 32: case 33:
393 case 34: case 35: case 36: case 37:
394 fg_c = ansi_col[args[i] - 30];
396 case 39: /* normal */
397 fg_c = DEFAULT_FGCOLOR;
399 case 40: case 41: case 42: case 43:
400 case 44: case 45: case 46: case 47:
401 bg_c = ansi_col[args[i] - 40];
403 case 49: /* normal */
404 bg_c = DEFAULT_BGCOLOR;
408 conout->SetAttribute(conout, EFI_TEXT_ATTR(fg_c, bg_c));
432 efi_cons_putchar(int c)
437 efi_cons_rawputchar(c);
452 /* Try to read a key stroke. We wait for one if none is pending. */
453 status = conin->ReadKeyStroke(conin, &key);
454 while (status == EFI_NOT_READY) {
455 /* Some EFI implementation (u-boot for example) do not support WaitForKey */
456 if (conin->WaitForKey != NULL)
457 BS->WaitForEvent(1, &conin->WaitForKey, &junk);
458 status = conin->ReadKeyStroke(conin, &key);
462 switch (key.ScanCode) {
464 return (0x1b); /* esc */
467 /* this can return */
468 return (key.UnicodeChar);
477 if (conin->WaitForKey == NULL) {
480 status = conin->ReadKeyStroke(conin, &key);
481 if (status == EFI_SUCCESS) {
485 return (key_pending);
488 /* This can clear the signaled state. */
489 return (BS->CheckEvent(conin->WaitForKey) == EFI_SUCCESS);
492 /* Plain direct access to EFI OutputString(). */
494 efi_cons_efiputchar(int c)
499 * translate box chars to unicode
503 case 0xb3: buf[0] = BOXDRAW_VERTICAL; break;
504 case 0xbf: buf[0] = BOXDRAW_DOWN_LEFT; break;
505 case 0xc0: buf[0] = BOXDRAW_UP_RIGHT; break;
506 case 0xc4: buf[0] = BOXDRAW_HORIZONTAL; break;
507 case 0xda: buf[0] = BOXDRAW_DOWN_RIGHT; break;
508 case 0xd9: buf[0] = BOXDRAW_UP_LEFT; break;
511 case 0xba: buf[0] = BOXDRAW_DOUBLE_VERTICAL; break;
512 case 0xbb: buf[0] = BOXDRAW_DOUBLE_DOWN_LEFT; break;
513 case 0xbc: buf[0] = BOXDRAW_DOUBLE_UP_LEFT; break;
514 case 0xc8: buf[0] = BOXDRAW_DOUBLE_UP_RIGHT; break;
515 case 0xc9: buf[0] = BOXDRAW_DOUBLE_DOWN_RIGHT; break;
516 case 0xcd: buf[0] = BOXDRAW_DOUBLE_HORIZONTAL; break;
521 buf[1] = 0; /* terminate string */
523 conout->OutputString(conout, buf);