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>
32 #include "bootstrap.h"
34 static SIMPLE_TEXT_OUTPUT_INTERFACE
*conout
;
35 static SIMPLE_INPUT_INTERFACE
*conin
;
38 #define DEFAULT_FGCOLOR EFI_LIGHTGRAY
39 #define DEFAULT_BGCOLOR EFI_BLACK
43 static unsigned keybuf
[KEYBUFSZ
]; /* keybuf for extended codes */
47 static int args
[MAXARGS
], argc
;
48 static int fg_c
, bg_c
;
49 static UINTN curx
, cury
;
52 void get_pos(UINTN
*x
, UINTN
*y
);
53 void curs_move(UINTN
*_x
, UINTN
*_y
, UINTN x
, UINTN y
);
59 static void efi_cons_probe(struct console
*);
60 static int efi_cons_init(struct console
*, int);
61 void efi_cons_putchar(struct console
*, int);
62 int efi_cons_getchar(struct console
*);
63 void efi_cons_efiputchar(int);
64 int efi_cons_poll(struct console
*);
66 struct console efi_console
= {
80 /* Get cursor position. */
82 get_pos(UINTN
*x
, UINTN
*y
)
84 *x
= conout
->Mode
->CursorColumn
;
85 *y
= conout
->Mode
->CursorRow
;
88 /* Move cursor to x rows and y cols (0-based). */
90 curs_move(UINTN
*_x
, UINTN
*_y
, UINTN x
, UINTN y
)
92 conout
->SetCursorPosition(conout
, x
, y
);
94 *_x
= conout
->Mode
->CursorColumn
;
96 *_y
= conout
->Mode
->CursorRow
;
99 /* Clear internal state of the terminal emulation code. */
110 efi_cons_probe(struct console
*cp
)
114 cp
->c_flags
|= C_PRESENTIN
| C_PRESENTOUT
;
118 efi_cons_init(struct console
*cp
__attribute((unused
)),
119 int arg
__attribute((unused
)))
121 conout
->SetAttribute(conout
, EFI_TEXT_ATTR(DEFAULT_FGCOLOR
,
125 get_pos(&curx
, &cury
);
126 curs_move(&curx
, &cury
, curx
, cury
);
127 fg_c
= DEFAULT_FGCOLOR
;
128 bg_c
= DEFAULT_BGCOLOR
;
129 memset(keybuf
, 0, KEYBUFSZ
);
131 conout
->EnableCursor(conout
, TRUE
);
136 efi_cons_rawputchar(int c
)
140 conout
->QueryMode(conout
, conout
->Mode
->Mode
, &x
, &y
);
141 static int ignorenl
= 0;
144 /* XXX lame tab expansion */
145 for (i
= 0; i
< 8; i
++)
146 efi_cons_rawputchar(' ');
150 efi_cons_efiputchar('\r');
152 efi_cons_efiputchar(c
);
163 if ((efi_console
.c_flags
& C_MODERAW
) == 0)
166 efi_cons_efiputchar('\n');
178 curs_move(&curx
, &cury
, curx
, cury
);
180 if ((efi_console
.c_flags
& C_MODERAW
) == 0) {
183 efi_cons_efiputchar('\n');
185 curs_move(&curx
, &cury
, curx
, cury
);
188 efi_cons_efiputchar(c
);
190 if ((efi_console
.c_flags
& C_MODERAW
) == 0) {
195 } else if (curx
== x
) {
198 efi_cons_efiputchar('\n');
203 curs_move(&curx
, &cury
, curx
, cury
);
208 /* Gracefully exit ESC-sequence processing in case of misunderstanding. */
216 efi_cons_rawputchar('\033');
218 efi_cons_rawputchar(esc
);
219 for (i
= 0; i
<= argc
; ++i
) {
220 sprintf(buf
, "%d", args
[i
]);
223 efi_cons_rawputchar(*ch
++);
226 efi_cons_rawputchar(c
);
230 /* Clear display from current position to end of screen. */
235 get_pos(&curx
, &cury
);
236 if (curx
== 0 && cury
== 0) {
237 conout
->ClearScreen(conout
);
242 conout
->QueryMode(conout
, conout
->Mode
->Mode
, &x
, &y
);
243 CL(0); /* clear current line from cursor to end */
244 for (i
= cury
+ 1; i
< y
-1; i
++) {
245 curs_move(NULL
, NULL
, 0, i
);
248 curs_move(NULL
, NULL
, curx
, cury
);
253 * Absolute cursor move to args[0] rows and args[1] columns
254 * (the coordinates are 1-based).
263 curs_move(&curx
, &cury
, args
[1], args
[0]);
267 /* Home cursor (left top corner), also called from mode command. */
272 args
[0] = args
[1] = 1;
276 /* Clear line from current position to end of line */
284 conout
->QueryMode(conout
, conout
->Mode
->Mode
, &x
, &y
);
286 case 0: /* from cursor to end */
289 case 1: /* from beginning to cursor */
292 case 2: /* entire line */
301 line
= malloc(len
* sizeof (CHAR16
));
303 printf("out of memory\n");
306 for (i
= 0; i
< len
; i
++)
311 curs_move(NULL
, NULL
, 0, cury
);
313 conout
->OutputString(conout
, line
);
314 /* restore cursor position */
315 curs_move(NULL
, NULL
, curx
, cury
);
326 args
[argc
] += c
- '0';
329 /* Emulate basic capabilities of sun-color terminal */
333 static int ansi_col
[] = {
334 0, 4, 2, 6, 1, 5, 3, 7
345 efi_cons_rawputchar(c
);
366 else if (argc
+ 1 >= MAXARGS
)
371 case 'A': /* UP = \E[%dA */
376 args
[0] = y
- args
[0] + 1;
381 case 'B': /* DO = \E[%dB */
386 args
[0] = y
+ args
[0] + 1;
391 case 'C': /* RI = \E[%dC */
395 args
[1] = args
[0] + 1;
401 case 'H': /* ho = \E[H */
409 case 'J': /* cd = \E[J */
433 fg_c
= DEFAULT_FGCOLOR
;
434 bg_c
= DEFAULT_BGCOLOR
;
436 for (i
= 0; i
<= argc
; ++i
) {
438 case 0: /* back to normal */
439 fg_c
= DEFAULT_FGCOLOR
;
440 bg_c
= DEFAULT_BGCOLOR
;
445 case 4: /* underline */
449 case 7: /* reverse */
454 case 30: case 31: case 32: case 33:
455 case 34: case 35: case 36: case 37:
456 fg_c
= ansi_col
[args
[i
] - 30];
458 case 39: /* normal */
459 fg_c
= DEFAULT_FGCOLOR
;
461 case 40: case 41: case 42: case 43:
462 case 44: case 45: case 46: case 47:
463 bg_c
= ansi_col
[args
[i
] - 40];
465 case 49: /* normal */
466 bg_c
= DEFAULT_BGCOLOR
;
470 conout
->SetAttribute(conout
, EFI_TEXT_ATTR(fg_c
, bg_c
));
488 efi_cons_putchar(struct console
*cp
__attribute((unused
)), int c
)
493 efi_cons_rawputchar(c
);
498 efi_cons_getchar(struct console
*cp
__attribute((unused
)))
504 for (i
= 0; i
< KEYBUFSZ
; i
++) {
505 if (keybuf
[i
] != 0) {
514 status
= conin
->ReadKeyStroke(conin
, &key
);
515 if (status
== EFI_NOT_READY
)
518 switch (key
.ScanCode
) {
522 return (0x1b); /* esc */
526 return (0x1b); /* esc */
527 case 0x3: /* RIGHT */
530 return (0x1b); /* esc */
534 return (0x1b); /* esc */
536 return (0x1b); /* esc */
539 /* this can return */
540 return (key
.UnicodeChar
);
544 efi_cons_poll(struct console
*cp
__attribute((unused
)))
548 for (i
= 0; i
< KEYBUFSZ
; i
++) {
556 /* This can clear the signaled state. */
557 pending
= BS
->CheckEvent(conin
->WaitForKey
) == EFI_SUCCESS
;
562 /* Plain direct access to EFI OutputString(). */
564 efi_cons_efiputchar(int c
)
569 * translate box chars to unicode
573 case 0xb3: buf
[0] = BOXDRAW_VERTICAL
; break;
574 case 0xbf: buf
[0] = BOXDRAW_DOWN_LEFT
; break;
575 case 0xc0: buf
[0] = BOXDRAW_UP_RIGHT
; break;
576 case 0xc4: buf
[0] = BOXDRAW_HORIZONTAL
; break;
577 case 0xda: buf
[0] = BOXDRAW_DOWN_RIGHT
; break;
578 case 0xd9: buf
[0] = BOXDRAW_UP_LEFT
; break;
581 case 0xba: buf
[0] = BOXDRAW_DOUBLE_VERTICAL
; break;
582 case 0xbb: buf
[0] = BOXDRAW_DOUBLE_DOWN_LEFT
; break;
583 case 0xbc: buf
[0] = BOXDRAW_DOUBLE_UP_LEFT
; break;
584 case 0xc8: buf
[0] = BOXDRAW_DOUBLE_UP_RIGHT
; break;
585 case 0xc9: buf
[0] = BOXDRAW_DOUBLE_DOWN_RIGHT
; break;
586 case 0xcd: buf
[0] = BOXDRAW_DOUBLE_HORIZONTAL
; break;
591 buf
[1] = 0; /* terminate string */
593 conout
->OutputString(conout
, buf
);