123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492 |
-
-
- FILE_LICENCE ( GPL2_OR_LATER );
-
-
-
- #include <stdlib.h>
- #include <errno.h>
- #include <limits.h>
- #include <realmode.h>
- #include <ipxe/console.h>
- #include <ipxe/io.h>
- #include <ipxe/fbcon.h>
- #include <ipxe/vesafb.h>
- #include <config/console.h>
-
-
- extern struct console_driver bios_console;
- struct console_driver bios_console __attribute__ (( weak ));
-
-
- #define EIO_FAILED __einfo_error ( EINFO_EIO_FAILED )
- #define EINFO_EIO_FAILED \
- __einfo_uniqify ( EINFO_EIO, 0x01, \
- "Function call failed" )
- #define EIO_HARDWARE __einfo_error ( EINFO_EIO_HARDWARE )
- #define EINFO_EIO_HARDWARE \
- __einfo_uniqify ( EINFO_EIO, 0x02, \
- "Not supported in current configuration" )
- #define EIO_MODE __einfo_error ( EINFO_EIO_MODE )
- #define EINFO_EIO_MODE \
- __einfo_uniqify ( EINFO_EIO, 0x03, \
- "Invalid in current video mode" )
- #define EIO_VBE( code ) \
- EUNIQ ( EINFO_EIO, (code), EIO_FAILED, EIO_HARDWARE, EIO_MODE )
-
-
- #if ! ( defined ( CONSOLE_VESAFB ) && CONSOLE_EXPLICIT ( CONSOLE_VESAFB ) )
- #undef CONSOLE_VESAFB
- #define CONSOLE_VESAFB ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
- #endif
-
-
- #define VESAFB_FONT VBE_FONT_8x16
-
-
- struct console_driver vesafb_console __console_driver;
-
-
- struct vesafb {
-
- struct fbcon fbcon;
-
- physaddr_t start;
-
- struct fbcon_geometry pixel;
-
- struct fbcon_colour_map map;
-
- struct fbcon_font font;
-
- uint8_t saved_mode;
- };
-
-
- static struct vesafb vesafb;
-
-
- union vbe_buffer {
-
- struct vbe_controller_info controller;
-
- struct vbe_mode_info mode;
- };
- static union vbe_buffer __bss16 ( vbe_buf );
- #define vbe_buf __use_data16 ( vbe_buf )
-
-
- static int vesafb_rc ( unsigned int status ) {
- unsigned int code;
-
- if ( ( status & 0xff ) != 0x4f )
- return -ENOTSUP;
- code = ( ( status >> 8 ) & 0xff );
- return ( code ? -EIO_VBE ( code ) : 0 );
- }
-
-
- static void vesafb_font ( void ) {
- struct segoff font;
-
-
-
- __asm__ __volatile__ ( REAL_CODE ( "pushw %%bp\n\t"
- "int $0x10\n\t"
- "movw %%es, %%cx\n\t"
- "movw %%bp, %%dx\n\t"
- "popw %%bp\n\t" )
- : "=c" ( font.segment ),
- "=d" ( font.offset )
- : "a" ( VBE_GET_FONT ),
- "b" ( VESAFB_FONT ) );
- DBGC ( &vbe_buf, "VESAFB has font %04x at %04x:%04x\n",
- VESAFB_FONT, font.segment, font.offset );
- vesafb.font.start = real_to_user ( font.segment, font.offset );
- }
-
-
- static int vesafb_mode_list ( uint16_t **mode_numbers ) {
- struct vbe_controller_info *controller = &vbe_buf.controller;
- userptr_t video_mode_ptr;
- uint16_t mode_number;
- uint16_t status;
- size_t len;
- int rc;
-
-
- *mode_numbers = NULL;
-
-
- controller->vbe_signature = 0;
- __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
- : "=a" ( status )
- : "a" ( VBE_CONTROLLER_INFO ),
- "D" ( __from_data16 ( controller ) )
- : "memory" );
- if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
- DBGC ( &vbe_buf, "VESAFB could not get controller information: "
- "[%04x] %s\n", status, strerror ( rc ) );
- return rc;
- }
- if ( controller->vbe_signature != VBE_CONTROLLER_SIGNATURE ) {
- DBGC ( &vbe_buf, "VESAFB invalid controller signature "
- "\"%c%c%c%c\"\n", ( controller->vbe_signature >> 0 ),
- ( controller->vbe_signature >> 8 ),
- ( controller->vbe_signature >> 16 ),
- ( controller->vbe_signature >> 24 ) );
- DBGC_HDA ( &vbe_buf, 0, controller, sizeof ( *controller ) );
- return -EINVAL;
- }
- DBGC ( &vbe_buf, "VESAFB found VBE version %d.%d with mode list at "
- "%04x:%04x\n", controller->vbe_major_version,
- controller->vbe_minor_version,
- controller->video_mode_ptr.segment,
- controller->video_mode_ptr.offset );
-
-
- video_mode_ptr = real_to_user ( controller->video_mode_ptr.segment,
- controller->video_mode_ptr.offset );
- len = 0;
- do {
- copy_from_user ( &mode_number, video_mode_ptr, len,
- sizeof ( mode_number ) );
- len += sizeof ( mode_number );
- } while ( mode_number != VBE_MODE_END );
-
-
- *mode_numbers = malloc ( len );
- if ( ! *mode_numbers )
- return -ENOMEM;
- copy_from_user ( *mode_numbers, video_mode_ptr, 0, len );
-
- return 0;
- }
-
-
- static int vesafb_mode_info ( unsigned int mode_number ) {
- struct vbe_mode_info *mode = &vbe_buf.mode;
- uint16_t status;
- int rc;
-
-
- __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
- : "=a" ( status )
- : "a" ( VBE_MODE_INFO ),
- "c" ( mode_number ),
- "D" ( __from_data16 ( mode ) )
- : "memory" );
- if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
- DBGC ( &vbe_buf, "VESAFB could not get mode %04x information: "
- "[%04x] %s\n", mode_number, status, strerror ( rc ) );
- return rc;
- }
- DBGC ( &vbe_buf, "VESAFB mode %04x %dx%d %dbpp(%d:%d:%d:%d) model "
- "%02x [x%d]%s%s%s%s%s\n", mode_number, mode->x_resolution,
- mode->y_resolution, mode->bits_per_pixel, mode->rsvd_mask_size,
- mode->red_mask_size, mode->green_mask_size, mode->blue_mask_size,
- mode->memory_model, ( mode->number_of_image_pages + 1 ),
- ( ( mode->mode_attributes & VBE_MODE_ATTR_SUPPORTED ) ?
- "" : " [unsupported]" ),
- ( ( mode->mode_attributes & VBE_MODE_ATTR_TTY ) ?
- " [tty]" : "" ),
- ( ( mode->mode_attributes & VBE_MODE_ATTR_GRAPHICS ) ?
- "" : " [text]" ),
- ( ( mode->mode_attributes & VBE_MODE_ATTR_LINEAR ) ?
- "" : " [nonlinear]" ),
- ( ( mode->mode_attributes & VBE_MODE_ATTR_TRIPLE_BUF ) ?
- " [buf]" : "" ) );
-
- return 0;
- }
-
-
- static int vesafb_set_mode ( unsigned int mode_number ) {
- struct vbe_mode_info *mode = &vbe_buf.mode;
- uint16_t status;
- int rc;
-
-
- if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
- return rc;
-
-
- vesafb.start = mode->phys_base_ptr;
- vesafb.pixel.width = mode->x_resolution;
- vesafb.pixel.height = mode->y_resolution;
- vesafb.pixel.len = ( ( mode->bits_per_pixel + 7 ) / 8 );
- vesafb.pixel.stride = mode->bytes_per_scan_line;
- DBGC ( &vbe_buf, "VESAFB mode %04x has frame buffer at %08x\n",
- mode_number, mode->phys_base_ptr );
-
-
- vesafb.map.red_scale = ( 8 - mode->red_mask_size );
- vesafb.map.green_scale = ( 8 - mode->green_mask_size );
- vesafb.map.blue_scale = ( 8 - mode->blue_mask_size );
- vesafb.map.red_lsb = mode->red_field_position;
- vesafb.map.green_lsb = mode->green_field_position;
- vesafb.map.blue_lsb = mode->blue_field_position;
-
-
- __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
- : "=a" ( status )
- : "a" ( VBE_SET_MODE ),
- "b" ( mode_number ) );
- if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
- DBGC ( &vbe_buf, "VESAFB could not set mode %04x: [%04x] %s\n",
- mode_number, status, strerror ( rc ) );
- return rc;
- }
-
- return 0;
- }
-
-
- static int vesafb_select_mode ( const uint16_t *mode_numbers,
- unsigned int min_width, unsigned int min_height,
- unsigned int min_bpp ) {
- struct vbe_mode_info *mode = &vbe_buf.mode;
- int best_mode_number = -ENOENT;
- unsigned int best_score = INT_MAX;
- unsigned int score;
- uint16_t mode_number;
- int rc;
-
-
- while ( ( mode_number = *(mode_numbers++) ) != VBE_MODE_END ) {
-
-
- mode_number |= VBE_MODE_LINEAR;
-
-
- if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
- continue;
-
-
- if ( ( mode->mode_attributes & ( VBE_MODE_ATTR_SUPPORTED |
- VBE_MODE_ATTR_GRAPHICS |
- VBE_MODE_ATTR_LINEAR ) ) !=
- ( VBE_MODE_ATTR_SUPPORTED | VBE_MODE_ATTR_GRAPHICS |
- VBE_MODE_ATTR_LINEAR ) ) {
- continue;
- }
- if ( mode->memory_model != VBE_MODE_MODEL_DIRECT_COLOUR )
- continue;
-
-
- if ( ( mode->x_resolution < min_width ) ||
- ( mode->y_resolution < min_height ) ||
- ( mode->bits_per_pixel < min_bpp ) ) {
- continue;
- }
-
-
-
- score = ( ( mode->x_resolution * mode->y_resolution ) -
- mode->bits_per_pixel );
- if ( score < best_score ) {
- best_mode_number = mode_number;
- best_score = score;
- }
- }
-
- if ( best_mode_number >= 0 ) {
- DBGC ( &vbe_buf, "VESAFB selected mode %04x\n",
- best_mode_number );
- } else {
- DBGC ( &vbe_buf, "VESAFB found no suitable mode\n" );
- }
-
- return best_mode_number;
- }
-
-
- static int vesafb_init ( unsigned int min_width, unsigned int min_height,
- unsigned int min_bpp, struct pixel_buffer *pixbuf ) {
- uint32_t discard_b;
- uint16_t *mode_numbers;
- int mode_number;
- int rc;
-
-
- __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
- : "=a" ( vesafb.saved_mode ), "=b" ( discard_b )
- : "a" ( VBE_GET_VGA_MODE ) );
- DBGC ( &vbe_buf, "VESAFB saved VGA mode %#02x\n", vesafb.saved_mode );
-
-
- if ( ( rc = vesafb_mode_list ( &mode_numbers ) ) != 0 )
- goto err_mode_list;
-
-
- if ( ( mode_number = vesafb_select_mode ( mode_numbers, min_width,
- min_height, min_bpp ) ) < 0 ){
- rc = mode_number;
- goto err_select_mode;
- }
-
-
- if ( ( rc = vesafb_set_mode ( mode_number ) ) != 0 )
- goto err_set_mode;
-
-
- vesafb_font();
-
-
- fbcon_init ( &vesafb.fbcon, phys_to_user ( vesafb.start ),
- &vesafb.pixel, &vesafb.map, &vesafb.font, pixbuf );
-
- err_set_mode:
- err_select_mode:
- free ( mode_numbers );
- err_mode_list:
- return rc;
- }
-
-
- static void vesafb_fini ( void ) {
- uint32_t discard_a;
-
-
- fbcon_fini ( &vesafb.fbcon );
-
-
- __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
- : "=a" ( discard_a )
- : "a" ( VBE_SET_VGA_MODE | vesafb.saved_mode ) );
- DBGC ( &vbe_buf, "VESAFB restored VGA mode %#02x\n",
- vesafb.saved_mode );
- }
-
-
- static void vesafb_putchar ( int character ) {
-
- fbcon_putchar ( &vesafb.fbcon, character );
- }
-
-
- static int vesafb_configure ( struct console_configuration *config ) {
- int rc;
-
-
- if ( ! vesafb_console.disabled ) {
- vesafb_fini();
- bios_console.disabled &= ~CONSOLE_DISABLED_OUTPUT;
- }
- vesafb_console.disabled = CONSOLE_DISABLED;
-
-
- if ( ( config == NULL ) ||
- ( config->width == 0 ) || ( config->height == 0 ) ) {
- return 0;
- }
-
-
- if ( ( rc = vesafb_init ( config->width, config->height, config->bpp,
- config->pixbuf ) ) != 0 )
- return rc;
-
-
- vesafb_console.disabled = 0;
- bios_console.disabled |= CONSOLE_DISABLED_OUTPUT;
-
- return 0;
- }
-
-
- struct console_driver vesafb_console __console_driver = {
- .usage = CONSOLE_VESAFB,
- .putchar = vesafb_putchar,
- .configure = vesafb_configure,
- .disabled = CONSOLE_DISABLED,
- };
|