2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
20 FILE_LICENCE ( GPL2_OR_LATER );
24 * VESA frame buffer console
32 #include <ipxe/console.h>
34 #include <ipxe/ansicol.h>
35 #include <ipxe/fbcon.h>
36 #include <ipxe/vesafb.h>
37 #include <config/console.h>
39 /* Avoid dragging in BIOS console if not otherwise used */
40 extern struct console_driver bios_console;
41 struct console_driver bios_console __attribute__ (( weak ));
43 /* Disambiguate the various error causes */
44 #define EIO_FAILED __einfo_error ( EINFO_EIO_FAILED )
45 #define EINFO_EIO_FAILED \
46 __einfo_uniqify ( EINFO_EIO, 0x01, \
47 "Function call failed" )
48 #define EIO_HARDWARE __einfo_error ( EINFO_EIO_HARDWARE )
49 #define EINFO_EIO_HARDWARE \
50 __einfo_uniqify ( EINFO_EIO, 0x02, \
51 "Not supported in current configuration" )
52 #define EIO_MODE __einfo_error ( EINFO_EIO_MODE )
53 #define EINFO_EIO_MODE \
54 __einfo_uniqify ( EINFO_EIO, 0x03, \
55 "Invalid in current video mode" )
56 #define EIO_VBE( code ) \
57 EUNIQ ( EINFO_EIO, (code), EIO_FAILED, EIO_HARDWARE, EIO_MODE )
59 /* Set default console usage if applicable */
60 #if ! ( defined ( CONSOLE_VESAFB ) && CONSOLE_EXPLICIT ( CONSOLE_VESAFB ) )
62 #define CONSOLE_VESAFB ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
65 /** Font corresponding to selected character width and height */
66 #define VESAFB_FONT VBE_FONT_8x16
68 /* Forward declaration */
69 struct console_driver vesafb_console __console_driver;
71 /** A VESA frame buffer */
73 /** Frame buffer console */
75 /** Physical start address */
78 struct fbcon_geometry pixel;
80 struct fbcon_margin margin;
82 struct fbcon_colour_map map;
83 /** Font definition */
84 struct fbcon_font font;
89 /** The VESA frame buffer */
90 static struct vesafb vesafb;
92 /** Base memory buffer used for VBE calls */
94 /** VBE controller information block */
95 struct vbe_controller_info controller;
96 /** VBE mode information block */
97 struct vbe_mode_info mode;
99 static union vbe_buffer __bss16 ( vbe_buf );
100 #define vbe_buf __use_data16 ( vbe_buf )
103 * Convert VBE status code to iPXE status code
105 * @v status VBE status code
106 * @ret rc Return status code
108 static int vesafb_rc ( unsigned int status ) {
111 if ( ( status & 0xff ) != 0x4f )
113 code = ( ( status >> 8 ) & 0xff );
114 return ( code ? -EIO_VBE ( code ) : 0 );
118 * Get font definition
121 static void vesafb_font ( void ) {
124 /* Get font information
126 * Working around gcc bugs is icky here. The value we want is
127 * returned in %ebp, but there's no way to specify %ebp in an
128 * output constraint. We can't put %ebp in the clobber list,
129 * because this tends to cause random build failures on some
130 * gcc versions. We can't manually push/pop %ebp and return
131 * the value via a generic register output constraint, because
132 * gcc might choose to use %ebp to satisfy that constraint
133 * (and we have no way to prevent it from so doing).
135 * Work around this hideous mess by using %ecx and %edx as the
136 * output registers, since they get clobbered anyway.
138 __asm__ __volatile__ ( REAL_CODE ( "pushw %%bp\n\t" /* gcc bug */
140 "movw %%es, %%cx\n\t"
141 "movw %%bp, %%dx\n\t"
142 "popw %%bp\n\t" /* gcc bug */ )
143 : "=c" ( font.segment ),
145 : "a" ( VBE_GET_FONT ),
146 "b" ( VESAFB_FONT ) );
147 DBGC ( &vbe_buf, "VESAFB has font %04x at %04x:%04x\n",
148 VESAFB_FONT, font.segment, font.offset );
149 vesafb.font.start = real_to_user ( font.segment, font.offset );
155 * @ret mode_numbers Mode number list (terminated with VBE_MODE_END)
156 * @ret rc Return status code
158 * The caller is responsible for eventually freeing the mode list.
160 static int vesafb_mode_list ( uint16_t **mode_numbers ) {
161 struct vbe_controller_info *controller = &vbe_buf.controller;
162 userptr_t video_mode_ptr;
163 uint16_t mode_number;
168 /* Avoid returning uninitialised data on error */
169 *mode_numbers = NULL;
171 /* Get controller information block */
172 controller->vbe_signature = 0;
173 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
175 : "a" ( VBE_CONTROLLER_INFO ),
176 "D" ( __from_data16 ( controller ) )
177 : "memory", "ebx", "edx" );
178 if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
179 DBGC ( &vbe_buf, "VESAFB could not get controller information: "
180 "[%04x] %s\n", status, strerror ( rc ) );
183 if ( controller->vbe_signature != VBE_CONTROLLER_SIGNATURE ) {
184 DBGC ( &vbe_buf, "VESAFB invalid controller signature "
185 "\"%c%c%c%c\"\n", ( controller->vbe_signature >> 0 ),
186 ( controller->vbe_signature >> 8 ),
187 ( controller->vbe_signature >> 16 ),
188 ( controller->vbe_signature >> 24 ) );
189 DBGC_HDA ( &vbe_buf, 0, controller, sizeof ( *controller ) );
192 DBGC ( &vbe_buf, "VESAFB found VBE version %d.%d with mode list at "
193 "%04x:%04x\n", controller->vbe_major_version,
194 controller->vbe_minor_version,
195 controller->video_mode_ptr.segment,
196 controller->video_mode_ptr.offset );
198 /* Calculate length of mode list */
199 video_mode_ptr = real_to_user ( controller->video_mode_ptr.segment,
200 controller->video_mode_ptr.offset );
203 copy_from_user ( &mode_number, video_mode_ptr, len,
204 sizeof ( mode_number ) );
205 len += sizeof ( mode_number );
206 } while ( mode_number != VBE_MODE_END );
208 /* Allocate and fill mode list */
209 *mode_numbers = malloc ( len );
210 if ( ! *mode_numbers )
212 copy_from_user ( *mode_numbers, video_mode_ptr, 0, len );
218 * Get video mode information
220 * @v mode_number Mode number
221 * @ret rc Return status code
223 static int vesafb_mode_info ( unsigned int mode_number ) {
224 struct vbe_mode_info *mode = &vbe_buf.mode;
228 /* Get mode information */
229 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
231 : "a" ( VBE_MODE_INFO ),
233 "D" ( __from_data16 ( mode ) )
235 if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
236 DBGC ( &vbe_buf, "VESAFB could not get mode %04x information: "
237 "[%04x] %s\n", mode_number, status, strerror ( rc ) );
240 DBGC ( &vbe_buf, "VESAFB mode %04x %dx%d %dbpp(%d:%d:%d:%d) model "
241 "%02x [x%d]%s%s%s%s%s\n", mode_number, mode->x_resolution,
242 mode->y_resolution, mode->bits_per_pixel, mode->rsvd_mask_size,
243 mode->red_mask_size, mode->green_mask_size, mode->blue_mask_size,
244 mode->memory_model, ( mode->number_of_image_pages + 1 ),
245 ( ( mode->mode_attributes & VBE_MODE_ATTR_SUPPORTED ) ?
246 "" : " [unsupported]" ),
247 ( ( mode->mode_attributes & VBE_MODE_ATTR_TTY ) ?
249 ( ( mode->mode_attributes & VBE_MODE_ATTR_GRAPHICS ) ?
251 ( ( mode->mode_attributes & VBE_MODE_ATTR_LINEAR ) ?
252 "" : " [nonlinear]" ),
253 ( ( mode->mode_attributes & VBE_MODE_ATTR_TRIPLE_BUF ) ?
262 * @v mode_number Mode number
263 * @ret rc Return status code
265 static int vesafb_set_mode ( unsigned int mode_number ) {
266 struct vbe_mode_info *mode = &vbe_buf.mode;
270 /* Get mode information */
271 if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
274 /* Record mode parameters */
275 vesafb.start = mode->phys_base_ptr;
276 vesafb.pixel.width = mode->x_resolution;
277 vesafb.pixel.height = mode->y_resolution;
278 vesafb.pixel.len = ( ( mode->bits_per_pixel + 7 ) / 8 );
279 vesafb.pixel.stride = mode->bytes_per_scan_line;
280 DBGC ( &vbe_buf, "VESAFB mode %04x has frame buffer at %08x\n",
281 mode_number, mode->phys_base_ptr );
283 /* Initialise font colours */
284 vesafb.map.red_scale = ( 8 - mode->red_mask_size );
285 vesafb.map.green_scale = ( 8 - mode->green_mask_size );
286 vesafb.map.blue_scale = ( 8 - mode->blue_mask_size );
287 vesafb.map.red_lsb = mode->red_field_position;
288 vesafb.map.green_lsb = mode->green_field_position;
289 vesafb.map.blue_lsb = mode->blue_field_position;
291 /* Select this mode */
292 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
294 : "a" ( VBE_SET_MODE ),
295 "b" ( mode_number ) );
296 if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
297 DBGC ( &vbe_buf, "VESAFB could not set mode %04x: [%04x] %s\n",
298 mode_number, status, strerror ( rc ) );
308 * @v mode_numbers Mode number list (terminated with VBE_MODE_END)
309 * @v min_width Minimum required width (in pixels)
310 * @v min_height Minimum required height (in pixels)
311 * @v min_bpp Minimum required colour depth (in bits per pixel)
312 * @ret mode_number Mode number, or negative error
314 static int vesafb_select_mode ( const uint16_t *mode_numbers,
315 unsigned int min_width, unsigned int min_height,
316 unsigned int min_bpp ) {
317 struct vbe_mode_info *mode = &vbe_buf.mode;
318 int best_mode_number = -ENOENT;
319 unsigned int best_score = INT_MAX;
321 uint16_t mode_number;
324 /* Find the first suitable mode */
325 while ( ( mode_number = *(mode_numbers++) ) != VBE_MODE_END ) {
327 /* Force linear mode variant */
328 mode_number |= VBE_MODE_LINEAR;
330 /* Get mode information */
331 if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
334 /* Skip unusable modes */
335 if ( ( mode->mode_attributes & ( VBE_MODE_ATTR_SUPPORTED |
336 VBE_MODE_ATTR_GRAPHICS |
337 VBE_MODE_ATTR_LINEAR ) ) !=
338 ( VBE_MODE_ATTR_SUPPORTED | VBE_MODE_ATTR_GRAPHICS |
339 VBE_MODE_ATTR_LINEAR ) ) {
342 if ( mode->memory_model != VBE_MODE_MODEL_DIRECT_COLOUR )
345 /* Skip modes not meeting the requirements */
346 if ( ( mode->x_resolution < min_width ) ||
347 ( mode->y_resolution < min_height ) ||
348 ( mode->bits_per_pixel < min_bpp ) ) {
352 /* Select this mode if it has the best (i.e. lowest)
353 * score. We choose the scoring system to favour
354 * modes close to the specified width and height;
355 * within modes of the same width and height we prefer
356 * a higher colour depth.
358 score = ( ( mode->x_resolution * mode->y_resolution ) -
359 mode->bits_per_pixel );
360 if ( score < best_score ) {
361 best_mode_number = mode_number;
366 if ( best_mode_number >= 0 ) {
367 DBGC ( &vbe_buf, "VESAFB selected mode %04x\n",
370 DBGC ( &vbe_buf, "VESAFB found no suitable mode\n" );
373 return best_mode_number;
380 static void vesafb_restore ( void ) {
383 /* Restore saved VGA mode */
384 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
386 : "a" ( VBE_SET_VGA_MODE | vesafb.saved_mode ) );
387 DBGC ( &vbe_buf, "VESAFB restored VGA mode %#02x\n",
392 * Initialise VESA frame buffer
394 * @v config Console configuration, or NULL to reset
395 * @ret rc Return status code
397 static int vesafb_init ( struct console_configuration *config ) {
399 uint16_t *mode_numbers;
409 /* Record current VGA mode */
410 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
411 : "=a" ( vesafb.saved_mode ), "=b" ( discard_b )
412 : "a" ( VBE_GET_VGA_MODE ) );
413 DBGC ( &vbe_buf, "VESAFB saved VGA mode %#02x\n", vesafb.saved_mode );
415 /* Get VESA mode list */
416 if ( ( rc = vesafb_mode_list ( &mode_numbers ) ) != 0 )
420 if ( ( mode_number = vesafb_select_mode ( mode_numbers, config->width,
422 config->depth ) ) < 0 ) {
424 goto err_select_mode;
428 if ( ( rc = vesafb_set_mode ( mode_number ) ) != 0 )
431 /* Calculate margin. If the actual screen size is larger than
432 * the requested screen size, then update the margins so that
433 * the margin remains relative to the requested screen size.
434 * (As an exception, if a zero margin was specified then treat
435 * this as meaning "expand to edge of actual screen".)
437 xgap = ( vesafb.pixel.width - config->width );
438 ygap = ( vesafb.pixel.height - config->height );
440 right = ( xgap - left );
442 bottom = ( ygap - top );
443 vesafb.margin.left = ( config->left + ( config->left ? left : 0 ) );
444 vesafb.margin.right = ( config->right + ( config->right ? right : 0 ) );
445 vesafb.margin.top = ( config->top + ( config->top ? top : 0 ) );
446 vesafb.margin.bottom =
447 ( config->bottom + ( config->bottom ? bottom : 0 ) );
452 /* Initialise frame buffer console */
453 if ( ( rc = fbcon_init ( &vesafb.fbcon, phys_to_user ( vesafb.start ),
454 &vesafb.pixel, &vesafb.margin, &vesafb.map,
455 &vesafb.font, config->pixbuf ) ) != 0 )
458 free ( mode_numbers );
461 fbcon_fini ( &vesafb.fbcon );
466 free ( mode_numbers );
472 * Finalise VESA frame buffer
475 static void vesafb_fini ( void ) {
477 /* Finalise frame buffer console */
478 fbcon_fini ( &vesafb.fbcon );
480 /* Restore saved VGA mode */
485 * Print a character to current cursor position
487 * @v character Character
489 static void vesafb_putchar ( int character ) {
491 fbcon_putchar ( &vesafb.fbcon, character );
497 * @v config Console configuration, or NULL to reset
498 * @ret rc Return status code
500 static int vesafb_configure ( struct console_configuration *config ) {
503 /* Reset console, if applicable */
504 if ( ! vesafb_console.disabled ) {
506 bios_console.disabled &= ~CONSOLE_DISABLED_OUTPUT;
507 ansicol_reset_magic();
509 vesafb_console.disabled = CONSOLE_DISABLED;
511 /* Do nothing more unless we have a usable configuration */
512 if ( ( config == NULL ) ||
513 ( config->width == 0 ) || ( config->height == 0 ) ) {
517 /* Initialise VESA frame buffer */
518 if ( ( rc = vesafb_init ( config ) ) != 0 )
521 /* Mark console as enabled */
522 vesafb_console.disabled = 0;
523 bios_console.disabled |= CONSOLE_DISABLED_OUTPUT;
525 /* Set magic colour to transparent if we have a background picture */
526 if ( config->pixbuf )
527 ansicol_set_magic_transparent();
532 /** VESA frame buffer console driver */
533 struct console_driver vesafb_console __console_driver = {
534 .usage = CONSOLE_VESAFB,
535 .putchar = vesafb_putchar,
536 .configure = vesafb_configure,
537 .disabled = CONSOLE_DISABLED,