iPXE
Defines | Functions | Variables
efi_console.c File Reference
#include <stddef.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/efi/efi.h>
#include <ipxe/efi/Protocol/ConsoleControl/ConsoleControl.h>
#include <ipxe/ansiesc.h>
#include <ipxe/console.h>
#include <ipxe/init.h>
#include <config/console.h>

Go to the source code of this file.

Defines

#define ATTR_BOLD   0x08
#define ATTR_FCOL_MASK   0x07
#define ATTR_FCOL_BLACK   0x00
#define ATTR_FCOL_BLUE   0x01
#define ATTR_FCOL_GREEN   0x02
#define ATTR_FCOL_CYAN   0x03
#define ATTR_FCOL_RED   0x04
#define ATTR_FCOL_MAGENTA   0x05
#define ATTR_FCOL_YELLOW   0x06
#define ATTR_FCOL_WHITE   0x07
#define ATTR_BCOL_MASK   0x70
#define ATTR_BCOL_BLACK   0x00
#define ATTR_BCOL_BLUE   0x10
#define ATTR_BCOL_GREEN   0x20
#define ATTR_BCOL_CYAN   0x30
#define ATTR_BCOL_RED   0x40
#define ATTR_BCOL_MAGENTA   0x50
#define ATTR_BCOL_YELLOW   0x60
#define ATTR_BCOL_WHITE   0x70
#define ATTR_DEFAULT   ATTR_FCOL_WHITE
#define CONSOLE_EFI   ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )

Functions

 FILE_LICENCE (GPL2_OR_LATER)
 EFI_REQUEST_PROTOCOL (EFI_CONSOLE_CONTROL_PROTOCOL,&conctrl)
static void efi_handle_cup (struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[])
 Handle ANSI CUP (cursor position)
static void efi_handle_ed (struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
 Handle ANSI ED (erase in page)
static void efi_handle_sgr (struct ansiesc_context *ctx __unused, unsigned int count, int params[])
 Handle ANSI SGR (set graphics rendition)
static void efi_handle_dectcem_set (struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
 Handle ANSI DECTCEM set (show cursor)
static void efi_handle_dectcem_reset (struct ansiesc_context *ctx __unused, unsigned int count __unused, int params[] __unused)
 Handle ANSI DECTCEM reset (hide cursor)
static void efi_putchar (int character)
 Print a character to EFI console.
static const char * scancode_to_ansi_seq (unsigned int scancode)
 Get ANSI escape sequence corresponding to EFI scancode.
static int efi_getchar (void)
 Get character from EFI console.
static int efi_iskey (void)
 Check for character ready to read from EFI console.
static void efi_console_init (void)
 Initialise EFI console.
struct init_fn efi_console_init_fn __init_fn (INIT_EARLY)
 EFI console initialisation function.

Variables

static unsigned int efi_attr = ATTR_DEFAULT
 Current character attribute.
static
EFI_CONSOLE_CONTROL_PROTOCOL
conctrl
 Console control protocol.
static struct ansiesc_handler efi_ansiesc_handlers []
 EFI console ANSI escape sequence handlers.
static struct ansiesc_context efi_ansiesc_ctx
 EFI console ANSI escape sequence context.
static const char * ansi_input = ""
 Pointer to current ANSI output sequence.
static const char * ansi_sequences []
 Mapping from EFI scan codes to ANSI escape sequences.
struct console_driver efi_console __console_driver
 EFI console driver.

Define Documentation

#define ATTR_BOLD   0x08

Definition at line 33 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_MASK   0x07

Definition at line 35 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_BLACK   0x00

Definition at line 36 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_BLUE   0x01

Definition at line 37 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_GREEN   0x02

Definition at line 38 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_CYAN   0x03

Definition at line 39 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_RED   0x04

Definition at line 40 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_MAGENTA   0x05

Definition at line 41 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_YELLOW   0x06

Definition at line 42 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_FCOL_WHITE   0x07

Definition at line 43 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_MASK   0x70

Definition at line 45 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_BLACK   0x00

Definition at line 46 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_BLUE   0x10

Definition at line 47 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_GREEN   0x20

Definition at line 48 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_CYAN   0x30

Definition at line 49 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_RED   0x40

Definition at line 50 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_MAGENTA   0x50

Definition at line 51 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_YELLOW   0x60

Definition at line 52 of file efi_console.c.

Referenced by efi_handle_sgr().

#define ATTR_BCOL_WHITE   0x70

Definition at line 53 of file efi_console.c.

Referenced by efi_handle_sgr().

Definition at line 55 of file efi_console.c.

Referenced by efi_handle_sgr().

Definition at line 60 of file efi_console.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER  )
static void efi_handle_cup ( struct ansiesc_context *ctx  __unused,
unsigned int count  __unused,
int  params[] 
) [static]

Handle ANSI CUP (cursor position)

Parameters:
ctxANSI escape sequence context
countParameter count
params[0]Row (1 is top)
params[1]Column (1 is left)

Definition at line 78 of file efi_console.c.

References EFI_SYSTEM_TABLE::ConOut, cx, efi_systab, and _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL::SetCursorPosition.

                                                                         {
        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
        int cx = ( params[1] - 1 );
        int cy = ( params[0] - 1 );

        if ( cx < 0 )
                cx = 0;
        if ( cy < 0 )
                cy = 0;

        conout->SetCursorPosition ( conout, cx, cy );
}
static void efi_handle_ed ( struct ansiesc_context *ctx  __unused,
unsigned int count  __unused,
int params[]  __unused 
) [static]

Handle ANSI ED (erase in page)

Parameters:
ctxANSI escape sequence context
countParameter count
params[0]Region to erase

Definition at line 99 of file efi_console.c.

References ANSIESC_ED_ALL, assert, _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL::ClearScreen, EFI_SYSTEM_TABLE::ConOut, efi_systab, and params.

                                                    {
        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;

        /* We assume that we always clear the whole screen */
        assert ( params[0] == ANSIESC_ED_ALL );

        conout->ClearScreen ( conout );
}
static void efi_handle_sgr ( struct ansiesc_context *ctx  __unused,
unsigned int  count,
int  params[] 
) [static]

Handle ANSI SGR (set graphics rendition)

Parameters:
ctxANSI escape sequence context
countParameter count
paramsList of graphic rendition aspects

Definition at line 117 of file efi_console.c.

References ATTR_BCOL_BLACK, ATTR_BCOL_BLUE, ATTR_BCOL_CYAN, ATTR_BCOL_GREEN, ATTR_BCOL_MAGENTA, ATTR_BCOL_MASK, ATTR_BCOL_RED, ATTR_BCOL_WHITE, ATTR_BCOL_YELLOW, ATTR_BOLD, ATTR_DEFAULT, ATTR_FCOL_BLACK, ATTR_FCOL_BLUE, ATTR_FCOL_CYAN, ATTR_FCOL_GREEN, ATTR_FCOL_MAGENTA, ATTR_FCOL_MASK, ATTR_FCOL_RED, ATTR_FCOL_WHITE, ATTR_FCOL_YELLOW, EFI_SYSTEM_TABLE::ConOut, count, efi_attr, efi_systab, and _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL::SetAttribute.

                                                                {
        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
        static const uint8_t efi_attr_fcols[10] = {
                ATTR_FCOL_BLACK, ATTR_FCOL_RED, ATTR_FCOL_GREEN,
                ATTR_FCOL_YELLOW, ATTR_FCOL_BLUE, ATTR_FCOL_MAGENTA,
                ATTR_FCOL_CYAN, ATTR_FCOL_WHITE,
                ATTR_FCOL_WHITE, ATTR_FCOL_WHITE /* defaults */
        };
        static const uint8_t efi_attr_bcols[10] = {
                ATTR_BCOL_BLACK, ATTR_BCOL_RED, ATTR_BCOL_GREEN,
                ATTR_BCOL_YELLOW, ATTR_BCOL_BLUE, ATTR_BCOL_MAGENTA,
                ATTR_BCOL_CYAN, ATTR_BCOL_WHITE,
                ATTR_BCOL_BLACK, ATTR_BCOL_BLACK /* defaults */
        };
        unsigned int i;
        int aspect;

        for ( i = 0 ; i < count ; i++ ) {
                aspect = params[i];
                if ( aspect == 0 ) {
                        efi_attr = ATTR_DEFAULT;
                } else if ( aspect == 1 ) {
                        efi_attr |= ATTR_BOLD;
                } else if ( aspect == 22 ) {
                        efi_attr &= ~ATTR_BOLD;
                } else if ( ( aspect >= 30 ) && ( aspect <= 39 ) ) {
                        efi_attr &= ~ATTR_FCOL_MASK;
                        efi_attr |= efi_attr_fcols[ aspect - 30 ];
                } else if ( ( aspect >= 40 ) && ( aspect <= 49 ) ) {
                        efi_attr &= ~ATTR_BCOL_MASK;
                        efi_attr |= efi_attr_bcols[ aspect - 40 ];
                }
        }

        conout->SetAttribute ( conout, efi_attr );
}
static void efi_handle_dectcem_set ( struct ansiesc_context *ctx  __unused,
unsigned int count  __unused,
int params[]  __unused 
) [static]

Handle ANSI DECTCEM set (show cursor)

Parameters:
ctxANSI escape sequence context
countParameter count
paramsList of graphic rendition aspects

Definition at line 162 of file efi_console.c.

References EFI_SYSTEM_TABLE::ConOut, efi_systab, _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL::EnableCursor, and TRUE.

                                                             {
        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;

        conout->EnableCursor ( conout, TRUE );
}
static void efi_handle_dectcem_reset ( struct ansiesc_context *ctx  __unused,
unsigned int count  __unused,
int params[]  __unused 
) [static]

Handle ANSI DECTCEM reset (hide cursor)

Parameters:
ctxANSI escape sequence context
countParameter count
paramsList of graphic rendition aspects

Definition at line 177 of file efi_console.c.

References EFI_SYSTEM_TABLE::ConOut, efi_systab, _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL::EnableCursor, and FALSE.

                                                               {
        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;

        conout->EnableCursor ( conout, FALSE );
}
static void efi_putchar ( int  character) [static]

Print a character to EFI console.

Parameters:
characterCharacter to be printed

Definition at line 205 of file efi_console.c.

References ansiesc_process(), EFI_SYSTEM_TABLE::ConOut, efi_systab, and _EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL::OutputString.

                                          {
        EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *conout = efi_systab->ConOut;
        wchar_t wstr[] = { character, 0 };

        /* Intercept ANSI escape sequences */
        character = ansiesc_process ( &efi_ansiesc_ctx, character );
        if ( character < 0 )
                return;

        conout->OutputString ( conout, wstr );
}
static const char* scancode_to_ansi_seq ( unsigned int  scancode) [static]

Get ANSI escape sequence corresponding to EFI scancode.

Parameters:
scancodeEFI scancode
Return values:
ansi_seqANSI escape sequence, if any, otherwise NULL

Definition at line 266 of file efi_console.c.

References ansi_sequences, and NULL.

Referenced by efi_getchar().

                                                                   {
        if ( scancode < ( sizeof ( ansi_sequences ) /
                          sizeof ( ansi_sequences[0] ) ) ) {
                return ansi_sequences[scancode];
        }
        return NULL;
}
static int efi_getchar ( void  ) [static]

Get character from EFI console.

Return values:
characterCharacter read from console

Definition at line 279 of file efi_console.c.

References ansi_input, EFI_SYSTEM_TABLE::ConIn, DBG, DBG2, EEFI, efi_systab, key, rc, _EFI_SIMPLE_TEXT_INPUT_PROTOCOL::ReadKeyStroke, EFI_INPUT_KEY::ScanCode, scancode_to_ansi_seq(), strerror(), and EFI_INPUT_KEY::UnicodeChar.

                                {
        EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
        const char *ansi_seq;
        EFI_INPUT_KEY key;
        EFI_STATUS efirc;
        int rc;

        /* If we are mid-sequence, pass out the next byte */
        if ( *ansi_input )
                return *(ansi_input++);

        /* Read key from real EFI console */
        if ( ( efirc = conin->ReadKeyStroke ( conin, &key ) ) != 0 ) {
                rc = -EEFI ( efirc );
                DBG ( "EFI could not read keystroke: %s\n", strerror ( rc ) );
                return 0;
        }
        DBG2 ( "EFI read key stroke with unicode %04x scancode %04x\n",
               key.UnicodeChar, key.ScanCode );

        /* If key has a Unicode representation, return it */
        if ( key.UnicodeChar )
                return key.UnicodeChar;

        /* Otherwise, check for a special key that we know about */
        if ( ( ansi_seq = scancode_to_ansi_seq ( key.ScanCode ) ) ) {
                /* Start of escape sequence: return ESC (0x1b) */
                ansi_input = ansi_seq;
                return 0x1b;
        }

        return 0;
}
static int efi_iskey ( void  ) [static]

Check for character ready to read from EFI console.

Return values:
TrueCharacter available to read
FalseNo character available to read

Definition at line 319 of file efi_console.c.

References ansi_input, EFI_SYSTEM_TABLE::BootServices, EFI_BOOT_SERVICES::CheckEvent, EFI_SYSTEM_TABLE::ConIn, efi_systab, and _EFI_SIMPLE_TEXT_INPUT_PROTOCOL::WaitForKey.

                              {
        EFI_BOOT_SERVICES *bs = efi_systab->BootServices;
        EFI_SIMPLE_TEXT_INPUT_PROTOCOL *conin = efi_systab->ConIn;
        EFI_STATUS efirc;

        /* If we are mid-sequence, we are always ready */
        if ( *ansi_input )
                return 1;

        /* Check to see if the WaitForKey event has fired */
        if ( ( efirc = bs->CheckEvent ( conin->WaitForKey ) ) == 0 )
                return 1;

        return 0;
}
static void efi_console_init ( void  ) [static]

Initialise EFI console.

Definition at line 347 of file efi_console.c.

References EfiConsoleControlScreenText, _EFI_CONSOLE_CONTROL_PROTOCOL::GetMode, NULL, and _EFI_CONSOLE_CONTROL_PROTOCOL::SetMode.

                                      {
        EFI_CONSOLE_CONTROL_SCREEN_MODE mode;

        /* On some older EFI 1.10 implementations, we must use the
         * (now obsolete) EFI_CONSOLE_CONTROL_PROTOCOL to switch the
         * console into text mode.
         */
        if ( conctrl ) {
                conctrl->GetMode ( conctrl, &mode, NULL, NULL );
                if ( mode != EfiConsoleControlScreenText ) {
                        conctrl->SetMode ( conctrl,
                                           EfiConsoleControlScreenText );
                }
        }
}
struct init_fn efi_console_init_fn __init_fn ( INIT_EARLY  ) [read]

EFI console initialisation function.


Variable Documentation

unsigned int efi_attr = ATTR_DEFAULT [static]

Current character attribute.

Definition at line 64 of file efi_console.c.

Referenced by efi_handle_sgr().

Console control protocol.

Definition at line 67 of file efi_console.c.

Initial value:

EFI console ANSI escape sequence handlers.

Definition at line 186 of file efi_console.c.

Initial value:
 {
        .handlers = efi_ansiesc_handlers,
}

EFI console ANSI escape sequence context.

Definition at line 196 of file efi_console.c.

const char* ansi_input = "" [static]

Pointer to current ANSI output sequence.

While we are in the middle of returning an ANSI sequence for a special key, this will point to the next character to return. When not in the middle of such a sequence, this will point to a NUL (note: not "will be NULL").

Definition at line 225 of file efi_console.c.

Referenced by efi_getchar(), and efi_iskey().

const char* ansi_sequences[] [static]
Initial value:
 {
        [SCAN_UP] = "[A",
        [SCAN_DOWN] = "[B",
        [SCAN_RIGHT] = "[C",
        [SCAN_LEFT] = "[D",
        [SCAN_HOME] = "[H",
        [SCAN_END] = "[F",
        [SCAN_INSERT] = "[2~",
        
        [SCAN_DELETE] = "[3~",
        [SCAN_PAGE_UP] = "[5~",
        [SCAN_PAGE_DOWN] = "[6~",
        [SCAN_F5] = "[15~",
        [SCAN_F6] = "[17~",
        [SCAN_F7] = "[18~",
        [SCAN_F8] = "[19~",
        [SCAN_F9] = "[20~",
        [SCAN_F10] = "[21~",
        [SCAN_F11] = "[23~",
        [SCAN_F12] = "[24~",
        
        [SCAN_ESC] = "",
}

Mapping from EFI scan codes to ANSI escape sequences.

Definition at line 228 of file efi_console.c.

Referenced by scancode_to_ansi_seq().

Initial value:
 {
        .putchar = efi_putchar,
        .getchar = efi_getchar,
        .iskey = efi_iskey,
        .usage = CONSOLE_EFI,
}

EFI console driver.

Definition at line 336 of file efi_console.c.