iPXE
efi_console.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  */
00019 
00020 FILE_LICENCE ( GPL2_OR_LATER );
00021 
00022 #include <stddef.h>
00023 #include <string.h>
00024 #include <errno.h>
00025 #include <assert.h>
00026 #include <ipxe/efi/efi.h>
00027 #include <ipxe/efi/Protocol/ConsoleControl/ConsoleControl.h>
00028 #include <ipxe/ansiesc.h>
00029 #include <ipxe/console.h>
00030 #include <ipxe/init.h>
00031 #include <config/console.h>
00032 
00033 #define ATTR_BOLD               0x08
00034 
00035 #define ATTR_FCOL_MASK          0x07
00036 #define ATTR_FCOL_BLACK         0x00
00037 #define ATTR_FCOL_BLUE          0x01
00038 #define ATTR_FCOL_GREEN         0x02
00039 #define ATTR_FCOL_CYAN          0x03
00040 #define ATTR_FCOL_RED           0x04
00041 #define ATTR_FCOL_MAGENTA       0x05
00042 #define ATTR_FCOL_YELLOW        0x06
00043 #define ATTR_FCOL_WHITE         0x07
00044 
00045 #define ATTR_BCOL_MASK          0x70
00046 #define ATTR_BCOL_BLACK         0x00
00047 #define ATTR_BCOL_BLUE          0x10
00048 #define ATTR_BCOL_GREEN         0x20
00049 #define ATTR_BCOL_CYAN          0x30
00050 #define ATTR_BCOL_RED           0x40
00051 #define ATTR_BCOL_MAGENTA       0x50
00052 #define ATTR_BCOL_YELLOW        0x60
00053 #define ATTR_BCOL_WHITE         0x70
00054 
00055 #define ATTR_DEFAULT            ATTR_FCOL_WHITE
00056 
00057 /* Set default console usage if applicable */
00058 #if ! ( defined ( CONSOLE_EFI ) && CONSOLE_EXPLICIT ( CONSOLE_EFI ) )
00059 #undef CONSOLE_EFI
00060 #define CONSOLE_EFI ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
00061 #endif
00062 
00063 /** Current character attribute */
00064 static unsigned int efi_attr = ATTR_DEFAULT;
00065 
00066 /** Console control protocol */
00067 static EFI_CONSOLE_CONTROL_PROTOCOL *conctrl;
00068 EFI_REQUEST_PROTOCOL ( EFI_CONSOLE_CONTROL_PROTOCOL, &conctrl );
00069 
00070 /**
00071  * Handle ANSI CUP (cursor position)
00072  *
00073  * @v ctx               ANSI escape sequence context
00074  * @v count             Parameter count
00075  * @v params[0]         Row (1 is top)
00076  * @v params[1]         Column (1 is left)
00077  */
00078 static void efi_handle_cup ( struct ansiesc_context *ctx __unused,
00079                              unsigned int count __unused, int params[] ) {
00080         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00081         int cx = ( params[1] - 1 );
00082         int cy = ( params[0] - 1 );
00083 
00084         if ( cx < 0 )
00085                 cx = 0;
00086         if ( cy < 0 )
00087                 cy = 0;
00088 
00089         conout->SetCursorPosition ( conout, cx, cy );
00090 }
00091 
00092 /**
00093  * Handle ANSI ED (erase in page)
00094  *
00095  * @v ctx               ANSI escape sequence context
00096  * @v count             Parameter count
00097  * @v params[0]         Region to erase
00098  */
00099 static void efi_handle_ed ( struct ansiesc_context *ctx __unused,
00100                             unsigned int count __unused,
00101                             int params[] __unused ) {
00102         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00103 
00104         /* We assume that we always clear the whole screen */
00105         assert ( params[0] == ANSIESC_ED_ALL );
00106 
00107         conout->ClearScreen ( conout );
00108 }
00109 
00110 /**
00111  * Handle ANSI SGR (set graphics rendition)
00112  *
00113  * @v ctx               ANSI escape sequence context
00114  * @v count             Parameter count
00115  * @v params            List of graphic rendition aspects
00116  */
00117 static void efi_handle_sgr ( struct ansiesc_context *ctx __unused,
00118                              unsigned int count, int params[] ) {
00119         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00120         static const uint8_t efi_attr_fcols[10] = {
00121                 ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
00122                 ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
00123                 ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
00124                 ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
00125         };
00126         static const uint8_t efi_attr_bcols[10] = {
00127                 ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
00128                 ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
00129                 ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
00130                 ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
00131         };
00132         unsigned int i;
00133         int aspect;
00134 
00135         for ( i = 0 ; i < count ; i++ ) {
00136                 aspect = params[i];
00137                 if ( aspect == 0 ) {
00138                         efi_attr = ATTR_DEFAULT;
00139                 } else if ( aspect == 1 ) {
00140                         efi_attr |= ATTR_BOLD;
00141                 } else if ( aspect == 22 ) {
00142                         efi_attr &= ~ATTR_BOLD;
00143                 } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
00144                         efi_attr &= ~ATTR_FCOL_MASK;
00145                         efi_attr |= efi_attr_fcols[ aspect - 30 ];
00146                 } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
00147                         efi_attr &= ~ATTR_BCOL_MASK;
00148                         efi_attr |= efi_attr_bcols[ aspect - 40 ];
00149                 }
00150         }
00151 
00152         conout->SetAttribute ( conout, efi_attr );
00153 }
00154 
00155 /**
00156  * Handle ANSI DECTCEM set (show cursor)
00157  *
00158  * @v ctx               ANSI escape sequence context
00159  * @v count             Parameter count
00160  * @v params            List of graphic rendition aspects
00161  */
00162 static void efi_handle_dectcem_set ( struct ansiesc_context *ctx __unused,
00163                                      unsigned int count __unused,
00164                                      int params[] __unused ) {
00165         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00166 
00167         conout->EnableCursor ( conout, TRUE );
00168 }
00169 
00170 /**
00171  * Handle ANSI DECTCEM reset (hide cursor)
00172  *
00173  * @v ctx               ANSI escape sequence context
00174  * @v count             Parameter count
00175  * @v params            List of graphic rendition aspects
00176  */
00177 static void efi_handle_dectcem_reset ( struct ansiesc_context *ctx __unused,
00178                                        unsigned int count __unused,
00179                                        int params[] __unused ) {
00180         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00181 
00182         conout->EnableCursor ( conout, FALSE );
00183 }
00184 
00185 /** EFI console ANSI escape sequence handlers */
00186 static struct ansiesc_handler efi_ansiesc_handlers[] = {
00187         { ANSIESC_CUP, efi_handle_cup },
00188         { ANSIESC_ED, efi_handle_ed },
00189         { ANSIESC_SGR, efi_handle_sgr },
00190         { ANSIESC_DECTCEM_SET, efi_handle_dectcem_set },
00191         { ANSIESC_DECTCEM_RESET, efi_handle_dectcem_reset },
00192         { 0, NULL }
00193 };
00194 
00195 /** EFI console ANSI escape sequence context */
00196 static struct ansiesc_context efi_ansiesc_ctx = {
00197         .handlers = efi_ansiesc_handlers,
00198 };
00199 
00200 /**
00201  * Print a character to EFI console
00202  *
00203  * @v character         Character to be printed
00204  */
00205 static void efi_putchar ( int character ) {
00206         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00207         wchar_t wstr[] = { character, 0 };
00208 
00209         /* Intercept ANSI escape sequences */
00210         character = ansiesc_process ( &efi_ansiesc_ctx, character );
00211         if ( character < 0 )
00212                 return;
00213 
00214         conout->OutputString ( conout, wstr );
00215 }
00216 
00217 /**
00218  * Pointer to current ANSI output sequence
00219  *
00220  * While we are in the middle of returning an ANSI sequence for a
00221  * special key, this will point to the next character to return.  When
00222  * not in the middle of such a sequence, this will point to a NUL
00223  * (note: not "will be NULL").
00224  */
00225 static const char *ansi_input = "";
00226 
00227 /** Mapping from EFI scan codes to ANSI escape sequences */
00228 static const char *ansi_sequences[] = {
00229         [SCAN_UP] = "[A",
00230         [SCAN_DOWN] = "[B",
00231         [SCAN_RIGHT] = "[C",
00232         [SCAN_LEFT] = "[D",
00233         [SCAN_HOME] = "[H",
00234         [SCAN_END] = "[F",
00235         [SCAN_INSERT] = "[2~",
00236         /* EFI translates an incoming backspace via the serial console
00237          * into a SCAN_DELETE.  There's not much we can do about this.
00238          */
00239         [SCAN_DELETE] = "[3~",
00240         [SCAN_PAGE_UP] = "[5~",
00241         [SCAN_PAGE_DOWN] = "[6~",
00242         [SCAN_F5] = "[15~",
00243         [SCAN_F6] = "[17~",
00244         [SCAN_F7] = "[18~",
00245         [SCAN_F8] = "[19~",
00246         [SCAN_F9] = "[20~",
00247         [SCAN_F10] = "[21~",
00248         [SCAN_F11] = "[23~",
00249         [SCAN_F12] = "[24~",
00250         /* EFI translates some (but not all) incoming escape sequences
00251          * via the serial console into equivalent scancodes.  When it
00252          * doesn't recognise a sequence, it helpfully(!) translates
00253          * the initial ESC and passes the remainder through verbatim.
00254          * Treating SCAN_ESC as equivalent to an empty escape sequence
00255          * works around this bug.
00256          */
00257         [SCAN_ESC] = "",
00258 };
00259 
00260 /**
00261  * Get ANSI escape sequence corresponding to EFI scancode
00262  *
00263  * @v scancode          EFI scancode
00264  * @ret ansi_seq        ANSI escape sequence, if any, otherwise NULL
00265  */
00266 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
00267         if ( scancode < ( sizeof ( ansi_sequences ) /
00268                           sizeof ( ansi_sequences[0] ) ) ) {
00269                 return ansi_sequences[scancode];
00270         }
00271         return NULL;
00272 }
00273 
00274 /**
00275  * Get character from EFI console
00276  *
00277  * @ret character       Character read from console
00278  */
00279 static int efi_getchar ( void ) {
00280         EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
00281         const char *ansi_seq;
00282         EFI_INPUT_KEY key;
00283         EFI_STATUS efirc;
00284         int rc;
00285 
00286         /* If we are mid-sequence, pass out the next byte */
00287         if ( *ansi_input )
00288                 return *(ansi_input++);
00289 
00290         /* Read key from real EFI console */
00291         if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
00292                 rc = -EEFI ( efirc );
00293                 DBG ( "EFI could not read keystroke: %s\n", strerror ( rc ) );
00294                 return 0;
00295         }
00296         DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
00297                key.UnicodeChar, key.ScanCode );
00298 
00299         /* If key has a Unicode representation, return it */
00300         if ( key.UnicodeChar )
00301                 return key.UnicodeChar;
00302 
00303         /* Otherwise, check for a special key that we know about */
00304         if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
00305                 /* Start of escape sequence: return ESC (0x1b) */
00306                 ansi_input = ansi_seq;
00307                 return 0x1b;
00308         }
00309 
00310         return 0;
00311 }
00312 
00313 /**
00314  * Check for character ready to read from EFI console
00315  *
00316  * @ret True            Character available to read
00317  * @ret False           No character available to read
00318  */
00319 static int efi_iskey ( void ) {
00320         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00321         EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
00322         EFI_STATUS efirc;
00323 
00324         /* If we are mid-sequence, we are always ready */
00325         if ( *ansi_input )
00326                 return 1;
00327 
00328         /* Check to see if the WaitForKey event has fired */
00329         if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
00330                 return 1;
00331 
00332         return 0;
00333 }
00334 
00335 /** EFI console driver */
00336 struct console_driver efi_console __console_driver = {
00337         .putchar = efi_putchar,
00338         .getchar = efi_getchar,
00339         .iskey = efi_iskey,
00340         .usage = CONSOLE_EFI,
00341 };
00342 
00343 /**
00344  * Initialise EFI console
00345  *
00346  */
00347 static void efi_console_init ( void ) {
00348         EFI_CONSOLE_CONTROL_SCREEN_MODE mode;
00349 
00350         /* On some older EFI 1.10 implementations, we must use the
00351          * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
00352          * console into text mode.
00353          */
00354         if ( conctrl ) {
00355                 conctrl->GetMode ( conctrl, &mode, NULL, NULL );
00356                 if ( mode != EfiConsoleControlScreenText ) {
00357                         conctrl->SetMode ( conctrl,
00358                                            EfiConsoleControlScreenText );
00359                 }
00360         }
00361 }
00362 
00363 /**
00364  * EFI console initialisation function
00365  */
00366 struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY ) = {
00367         .initialise = efi_console_init,
00368 };