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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <stddef.h>
00022 #include <assert.h>
00023 #include <gpxe/efi/efi.h>
00024 #include <gpxe/ansiesc.h>
00025 #include <console.h>
00026 
00027 #define ATTR_BOLD               0x08
00028 
00029 #define ATTR_FCOL_MASK          0x07
00030 #define ATTR_FCOL_BLACK         0x00
00031 #define ATTR_FCOL_BLUE          0x01
00032 #define ATTR_FCOL_GREEN         0x02
00033 #define ATTR_FCOL_CYAN          0x03
00034 #define ATTR_FCOL_RED           0x04
00035 #define ATTR_FCOL_MAGENTA       0x05
00036 #define ATTR_FCOL_YELLOW        0x06
00037 #define ATTR_FCOL_WHITE         0x07
00038 
00039 #define ATTR_BCOL_MASK          0x70
00040 #define ATTR_BCOL_BLACK         0x00
00041 #define ATTR_BCOL_BLUE          0x10
00042 #define ATTR_BCOL_GREEN         0x20
00043 #define ATTR_BCOL_CYAN          0x30
00044 #define ATTR_BCOL_RED           0x40
00045 #define ATTR_BCOL_MAGENTA       0x50
00046 #define ATTR_BCOL_YELLOW        0x60
00047 #define ATTR_BCOL_WHITE         0x70
00048 
00049 #define ATTR_DEFAULT            ATTR_FCOL_WHITE
00050 
00051 /** Current character attribute */
00052 static unsigned int efi_attr = ATTR_DEFAULT;
00053 
00054 /**
00055  * Handle ANSI CUP (cursor position)
00056  *
00057  * @v count             Parameter count
00058  * @v params[0]         Row (1 is top)
00059  * @v params[1]         Column (1 is left)
00060  */
00061 static void efi_handle_cup ( unsigned int count __unused, int params[] ) {
00062         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00063         int cx = ( params[1] - 1 );
00064         int cy = ( params[0] - 1 );
00065 
00066         if ( cx < 0 )
00067                 cx = 0;
00068         if ( cy < 0 )
00069                 cy = 0;
00070 
00071         conout->SetCursorPosition ( conout, cx, cy );
00072 }
00073 
00074 /**
00075  * Handle ANSI ED (erase in page)
00076  *
00077  * @v count             Parameter count
00078  * @v params[0]         Region to erase
00079  */
00080 static void efi_handle_ed ( unsigned int count __unused,
00081                              int params[] __unused ) {
00082         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00083 
00084         /* We assume that we always clear the whole screen */
00085         assert ( params[0] == ANSIESC_ED_ALL );
00086 
00087         conout->ClearScreen ( conout );
00088 }
00089 
00090 /**
00091  * Handle ANSI SGR (set graphics rendition)
00092  *
00093  * @v count             Parameter count
00094  * @v params            List of graphic rendition aspects
00095  */
00096 static void efi_handle_sgr ( unsigned int count, int params[] ) {
00097         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00098         static const uint8_t efi_attr_fcols[10] = {
00099                 ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
00100                 ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
00101                 ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
00102                 ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
00103         };
00104         static const uint8_t efi_attr_bcols[10] = {
00105                 ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
00106                 ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
00107                 ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
00108                 ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
00109         };
00110         unsigned int i;
00111         int aspect;
00112 
00113         for ( i = 0 ; i < count ; i++ ) {
00114                 aspect = params[i];
00115                 if ( aspect == 0 ) {
00116                         efi_attr = ATTR_DEFAULT;
00117                 } else if ( aspect == 1 ) {
00118                         efi_attr |= ATTR_BOLD;
00119                 } else if ( aspect == 22 ) {
00120                         efi_attr &= ~ATTR_BOLD;
00121                 } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
00122                         efi_attr &= ~ATTR_FCOL_MASK;
00123                         efi_attr |= efi_attr_fcols[ aspect - 30 ];
00124                 } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
00125                         efi_attr &= ~ATTR_BCOL_MASK;
00126                         efi_attr |= efi_attr_bcols[ aspect - 40 ];
00127                 }
00128         }
00129 
00130         conout->SetAttribute ( conout, efi_attr );
00131 }
00132 
00133 /** EFI console ANSI escape sequence handlers */
00134 static struct ansiesc_handler efi_ansiesc_handlers[] = {
00135         { ANSIESC_CUP, efi_handle_cup },
00136         { ANSIESC_ED, efi_handle_ed },
00137         { ANSIESC_SGR, efi_handle_sgr },
00138         { 0, NULL }
00139 };
00140 
00141 /** EFI console ANSI escape sequence context */
00142 static struct ansiesc_context efi_ansiesc_ctx = {
00143         .handlers = efi_ansiesc_handlers,
00144 };
00145 
00146 /**
00147  * Print a character to EFI console
00148  *
00149  * @v character         Character to be printed
00150  */
00151 static void efi_putchar ( int character ) {
00152         EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
00153         wchar_t wstr[] = { character, 0 };
00154 
00155         /* Intercept ANSI escape sequences */
00156         character = ansiesc_process ( &efi_ansiesc_ctx, character );
00157         if ( character < 0 )
00158                 return;
00159 
00160         conout->OutputString ( conout, wstr );
00161 }
00162 
00163 /**
00164  * Pointer to current ANSI output sequence
00165  *
00166  * While we are in the middle of returning an ANSI sequence for a
00167  * special key, this will point to the next character to return.  When
00168  * not in the middle of such a sequence, this will point to a NUL
00169  * (note: not "will be NULL").
00170  */
00171 static const char *ansi_input = "";
00172 
00173 /** Mapping from EFI scan codes to ANSI escape sequences */
00174 static const char *ansi_sequences[] = {
00175         [SCAN_UP] = "[A",
00176         [SCAN_DOWN] = "[B",
00177         [SCAN_RIGHT] = "[C",
00178         [SCAN_LEFT] = "[D",
00179         [SCAN_HOME] = "[H",
00180         [SCAN_END] = "[F",
00181         [SCAN_INSERT] = "[2~",
00182         /* EFI translates an incoming backspace via the serial console
00183          * into a SCAN_DELETE.  There's not much we can do about this.
00184          */
00185         [SCAN_DELETE] = "[3~",
00186         [SCAN_PAGE_UP] = "[5~",
00187         [SCAN_PAGE_DOWN] = "[6~",
00188         /* EFI translates some (but not all) incoming escape sequences
00189          * via the serial console into equivalent scancodes.  When it
00190          * doesn't recognise a sequence, it helpfully(!) translates
00191          * the initial ESC and passes the remainder through verbatim.
00192          * Treating SCAN_ESC as equivalent to an empty escape sequence
00193          * works around this bug.
00194          */
00195         [SCAN_ESC] = "",
00196 };
00197 
00198 /**
00199  * Get ANSI escape sequence corresponding to EFI scancode
00200  *
00201  * @v scancode          EFI scancode
00202  * @ret ansi_seq        ANSI escape sequence, if any, otherwise NULL
00203  */
00204 static const char * scancode_to_ansi_seq ( unsigned int scancode ) {
00205         if ( scancode < ( sizeof ( ansi_sequences ) /
00206                           sizeof ( ansi_sequences[0] ) ) ) {
00207                 return ansi_sequences[scancode];
00208         }
00209         return NULL;
00210 }
00211 
00212 /**
00213  * Get character from EFI console
00214  *
00215  * @ret character       Character read from console
00216  */
00217 static int efi_getchar ( void ) {
00218         EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
00219         const char *ansi_seq;
00220         EFI_INPUT_KEY key;
00221         EFI_STATUS efirc;
00222 
00223         /* If we are mid-sequence, pass out the next byte */
00224         if ( *ansi_input )
00225                 return *(ansi_input++);
00226 
00227         /* Read key from real EFI console */
00228         if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
00229                 DBG ( "EFI could not read keystroke: %s\n",
00230                       efi_strerror ( efirc ) );
00231                 return 0;
00232         }
00233         DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
00234                key.UnicodeChar, key.ScanCode );
00235 
00236         /* If key has a Unicode representation, return it */
00237         if ( key.UnicodeChar )
00238                 return key.UnicodeChar;
00239 
00240         /* Otherwise, check for a special key that we know about */
00241         if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
00242                 /* Start of escape sequence: return ESC (0x1b) */
00243                 ansi_input = ansi_seq;
00244                 return 0x1b;
00245         }
00246 
00247         return 0;
00248 }
00249 
00250 /**
00251  * Check for character ready to read from EFI console
00252  *
00253  * @ret True            Character available to read
00254  * @ret False           No character available to read
00255  */
00256 static int efi_iskey ( void ) {
00257         EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
00258         EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
00259         EFI_STATUS efirc;
00260 
00261         /* If we are mid-sequence, we are always ready */
00262         if ( *ansi_input )
00263                 return 1;
00264 
00265         /* Check to see if the WaitForKey event has fired */
00266         if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
00267                 return 1;
00268 
00269         return 0;
00270 }
00271 
00272 struct console_driver efi_console __console_driver = {
00273         .putchar = efi_putchar,
00274         .getchar = efi_getchar,
00275         .iskey = efi_iskey,
00276 };

Generated on Tue Apr 6 20:01:09 2010 for gPXE by  doxygen 1.5.7.1