Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / interface / efi / efi_console.c
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stddef.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <assert.h>
26 #include <ipxe/efi/efi.h>
27 #include <ipxe/efi/Protocol/ConsoleControl/ConsoleControl.h>
28 #include <ipxe/ansiesc.h>
29 #include <ipxe/console.h>
30 #include <ipxe/init.h>
31 #include <config/console.h>
32
33 #define ATTR_BOLD               0x08
34
35 #define ATTR_FCOL_MASK          0x07
36 #define ATTR_FCOL_BLACK         0x00
37 #define ATTR_FCOL_BLUE          0x01
38 #define ATTR_FCOL_GREEN         0x02
39 #define ATTR_FCOL_CYAN          0x03
40 #define ATTR_FCOL_RED           0x04
41 #define ATTR_FCOL_MAGENTA       0x05
42 #define ATTR_FCOL_YELLOW        0x06
43 #define ATTR_FCOL_WHITE         0x07
44
45 #define ATTR_BCOL_MASK          0x70
46 #define ATTR_BCOL_BLACK         0x00
47 #define ATTR_BCOL_BLUE          0x10
48 #define ATTR_BCOL_GREEN         0x20
49 #define ATTR_BCOL_CYAN          0x30
50 #define ATTR_BCOL_RED           0x40
51 #define ATTR_BCOL_MAGENTA       0x50
52 #define ATTR_BCOL_YELLOW        0x60
53 #define ATTR_BCOL_WHITE         0x70
54
55 #define ATTR_DEFAULT            ATTR_FCOL_WHITE
56
57 /* Set default console usage if applicable */
58 #if ! ( defined ( CONSOLE_EFI ) && CONSOLE_EXPLICIT ( CONSOLE_EFI ) )
59 #undef CONSOLE_EFI
60 #define CONSOLE_EFI ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
61 #endif
62
63 /** Current character attribute */
64 static unsigned int efi_attr = ATTR_DEFAULT;
65
66 /** Console control protocol */
67 static EFI_CONSOLE_CONTROL_PROTOCOL *conctrl;
68 EFI_REQUEST_PROTOCOL ( EFI_CONSOLE_CONTROL_PROTOCOL, &conctrl );
69
70 /**
71  * Handle ANSI CUP (cursor position)
72  *
73  * @v ctx               ANSI escape sequence context
74  * @v count             Parameter count
75  * @v params[0]         Row (1 is top)
76  * @v params[1]         Column (1 is left)
77  */
78 static void efi_handle_cup ( struct ansiesc_context *ctx __unused,
79                              unsigned int count __unused, int params[] ) {
80         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
81         int cx = ( params[1] - 1 );
82         int cy = ( params[0] - 1 );
83
84         if ( cx < 0 )
85                 cx = 0;
86         if ( cy < 0 )
87                 cy = 0;
88
89         conout->SetCursorPosition ( conout, cx, cy );
90 }
91
92 /**
93  * Handle ANSI ED (erase in page)
94  *
95  * @v ctx               ANSI escape sequence context
96  * @v count             Parameter count
97  * @v params[0]         Region to erase
98  */
99 static void efi_handle_ed ( struct ansiesc_context *ctx __unused,
100                             unsigned int count __unused,
101                             int params[] __unused ) {
102         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
103
104         /* We assume that we always clear the whole screen */
105         assert ( params[0] == ANSIESC_ED_ALL );
106
107         conout->ClearScreen ( conout );
108 }
109
110 /**
111  * Handle ANSI SGR (set graphics rendition)
112  *
113  * @v ctx               ANSI escape sequence context
114  * @v count             Parameter count
115  * @v params            List of graphic rendition aspects
116  */
117 static void efi_handle_sgr ( struct ansiesc_context *ctx __unused,
118                              unsigned int count, int params[] ) {
119         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
120         static const uint8_t efi_attr_fcols[10] = {
121                 ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
122                 ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
123                 ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
124                 ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
125         };
126         static const uint8_t efi_attr_bcols[10] = {
127                 ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
128                 ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
129                 ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
130                 ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
131         };
132         unsigned int i;
133         int aspect;
134
135         for ( i = 0 ; i < count ; i++ ) {
136                 aspect = params[i];
137                 if ( aspect == 0 ) {
138                         efi_attr = ATTR_DEFAULT;
139                 } else if ( aspect == 1 ) {
140                         efi_attr |= ATTR_BOLD;
141                 } else if ( aspect == 22 ) {
142                         efi_attr &= ~ATTR_BOLD;
143                 } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
144                         efi_attr &= ~ATTR_FCOL_MASK;
145                         efi_attr |= efi_attr_fcols[ aspect - 30 ];
146                 } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
147                         efi_attr &= ~ATTR_BCOL_MASK;
148                         efi_attr |= efi_attr_bcols[ aspect - 40 ];
149                 }
150         }
151
152         conout->SetAttribute ( conout, efi_attr );
153 }
154
155 /**
156  * Handle ANSI DECTCEM set (show cursor)
157  *
158  * @v ctx               ANSI escape sequence context
159  * @v count             Parameter count
160  * @v params            List of graphic rendition aspects
161  */
162 static void efi_handle_dectcem_set ( struct ansiesc_context *ctx __unused,
163                                      unsigned int count __unused,
164                                      int params[] __unused ) {
165         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
166
167         conout->EnableCursor ( conout, TRUE );
168 }
169
170 /**
171  * Handle ANSI DECTCEM reset (hide cursor)
172  *
173  * @v ctx               ANSI escape sequence context
174  * @v count             Parameter count
175  * @v params            List of graphic rendition aspects
176  */
177 static void efi_handle_dectcem_reset ( struct ansiesc_context *ctx __unused,
178                                        unsigned int count __unused,
179                                        int params[] __unused ) {
180         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
181
182         conout->EnableCursor ( conout, FALSE );
183 }
184
185 /** EFI console ANSI escape sequence handlers */
186 static struct ansiesc_handler efi_ansiesc_handlers[] = {
187         { ANSIESC_CUP, efi_handle_cup },
188         { ANSIESC_ED, efi_handle_ed },
189         { ANSIESC_SGR, efi_handle_sgr },
190         { ANSIESC_DECTCEM_SET, efi_handle_dectcem_set },
191         { ANSIESC_DECTCEM_RESET, efi_handle_dectcem_reset },
192         { 0, NULL }
193 };
194
195 /** EFI console ANSI escape sequence context */
196 static struct ansiesc_context efi_ansiesc_ctx = {
197         .handlers = efi_ansiesc_handlers,
198 };
199
200 /**
201  * Print a character to EFI console
202  *
203  * @v character         Character to be printed
204  */
205 static void efi_putchar ( int character ) {
206         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
207         wchar_t wstr[] = { character, 0 };
208
209         /* Intercept ANSI escape sequences */
210         character = ansiesc_process ( &efi_ansiesc_ctx, character );
211         if ( character < 0 )
212                 return;
213
214         conout->OutputString ( conout, wstr );
215 }
216
217 /**
218  * Pointer to current ANSI output sequence
219  *
220  * While we are in the middle of returning an ANSI sequence for a
221  * special key, this will point to the next character to return.  When
222  * not in the middle of such a sequence, this will point to a NUL
223  * (note: not "will be NULL").
224  */
225 static const char *ansi_input = "";
226
227 /** Mapping from EFI scan codes to ANSI escape sequences */
228 static const char *ansi_sequences[] = {
229         [SCAN_UP] = "[A",
230         [SCAN_DOWN] = "[B",
231         [SCAN_RIGHT] = "[C",
232         [SCAN_LEFT] = "[D",
233         [SCAN_HOME] = "[H",
234         [SCAN_END] = "[F",
235         [SCAN_INSERT] = "[2~",
236         /* EFI translates an incoming backspace via the serial console
237          * into a SCAN_DELETE.  There's not much we can do about this.
238          */
239         [SCAN_DELETE] = "[3~",
240         [SCAN_PAGE_UP] = "[5~",
241         [SCAN_PAGE_DOWN] = "[6~",
242         /* EFI translates some (but not all) incoming escape sequences
243          * via the serial console into equivalent scancodes.  When it
244          * doesn't recognise a sequence, it helpfully(!) translates
245          * the initial ESC and passes the remainder through verbatim.
246          * Treating SCAN_ESC as equivalent to an empty escape sequence
247          * works around this bug.
248          */
249         [SCAN_ESC] = "",
250 };
251
252 /**
253  * Get ANSI escape sequence corresponding to EFI scancode
254  *
255  * @v scancode          EFI scancode
256  * @ret ansi_seq        ANSI escape sequence, if any, otherwise NULL
257  */
258 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
259         if ( scancode < ( sizeof ( ansi_sequences ) /
260                           sizeof ( ansi_sequences[0] ) ) ) {
261                 return ansi_sequences[scancode];
262         }
263         return NULL;
264 }
265
266 /**
267  * Get character from EFI console
268  *
269  * @ret character       Character read from console
270  */
271 static int efi_getchar ( void ) {
272         EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
273         const char *ansi_seq;
274         EFI_INPUT_KEY key;
275         EFI_STATUS efirc;
276         int rc;
277
278         /* If we are mid-sequence, pass out the next byte */
279         if ( *ansi_input )
280                 return *(ansi_input++);
281
282         /* Read key from real EFI console */
283         if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
284                 rc = -EEFI ( efirc );
285                 DBG ( "EFI could not read keystroke: %s\n", strerror ( rc ) );
286                 return 0;
287         }
288         DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
289                key.UnicodeChar, key.ScanCode );
290
291         /* If key has a Unicode representation, return it */
292         if ( key.UnicodeChar )
293                 return key.UnicodeChar;
294
295         /* Otherwise, check for a special key that we know about */
296         if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
297                 /* Start of escape sequence: return ESC (0x1b) */
298                 ansi_input = ansi_seq;
299                 return 0x1b;
300         }
301
302         return 0;
303 }
304
305 /**
306  * Check for character ready to read from EFI console
307  *
308  * @ret True            Character available to read
309  * @ret False           No character available to read
310  */
311 static int efi_iskey ( void ) {
312         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
313         EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
314         EFI_STATUS efirc;
315
316         /* If we are mid-sequence, we are always ready */
317         if ( *ansi_input )
318                 return 1;
319
320         /* Check to see if the WaitForKey event has fired */
321         if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
322                 return 1;
323
324         return 0;
325 }
326
327 /** EFI console driver */
328 struct console_driver efi_console __console_driver = {
329         .putchar = efi_putchar,
330         .getchar = efi_getchar,
331         .iskey = efi_iskey,
332         .usage = CONSOLE_EFI,
333 };
334
335 /**
336  * Initialise EFI console
337  *
338  */
339 static void efi_console_init ( void ) {
340         EFI_CONSOLE_CONTROL_SCREEN_MODE mode;
341
342         /* On some older EFI 1.10 implementations, we must use the
343          * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
344          * console into text mode.
345          */
346         if ( conctrl ) {
347                 conctrl->GetMode ( conctrl, &mode, NULL, NULL );
348                 if ( mode != EfiConsoleControlScreenText ) {
349                         conctrl->SetMode ( conctrl,
350                                            EfiConsoleControlScreenText );
351                 }
352         }
353 }
354
355 /**
356  * EFI console initialisation function
357  */
358 struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
359         .initialise = efi_console_init,
360 };