1 /*
2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
3 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA.
18 *
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
22 */
23
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25
26 /** @file
27 *
28 * VESA frame buffer console
29 *
30 */
31
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <realmode.h>
36 #include <ipxe/console.h>
37 #include <ipxe/io.h>
38 #include <ipxe/ansicol.h>
39 #include <ipxe/fbcon.h>
40 #include <ipxe/vesafb.h>
41 #include <config/console.h>
42
43 /* Avoid dragging in BIOS console if not otherwise used */
44 extern struct console_driver bios_console;
45 struct console_driver bios_console __attribute__ (( weak ));
46
47 /* Disambiguate the various error causes */
48 #define EIO_FAILED __einfo_error ( EINFO_EIO_FAILED )
49 #define EINFO_EIO_FAILED \
50 __einfo_uniqify ( EINFO_EIO, 0x01, \
51 "Function call failed" )
52 #define EIO_HARDWARE __einfo_error ( EINFO_EIO_HARDWARE )
53 #define EINFO_EIO_HARDWARE \
54 __einfo_uniqify ( EINFO_EIO, 0x02, \
55 "Not supported in current configuration" )
56 #define EIO_MODE __einfo_error ( EINFO_EIO_MODE )
57 #define EINFO_EIO_MODE \
58 __einfo_uniqify ( EINFO_EIO, 0x03, \
59 "Invalid in current video mode" )
60 #define EIO_VBE( code ) \
61 EUNIQ ( EINFO_EIO, (code), EIO_FAILED, EIO_HARDWARE, EIO_MODE )
62
63 /* Set default console usage if applicable
64 *
65 * We accept either CONSOLE_FRAMEBUFFER or CONSOLE_VESAFB.
66 */
67 #if ( defined ( CONSOLE_FRAMEBUFFER ) && ! defined ( CONSOLE_VESAFB ) )
68 #define CONSOLE_VESAFB CONSOLE_FRAMEBUFFER
69 #endif
70 #if ! ( defined ( CONSOLE_VESAFB ) && CONSOLE_EXPLICIT ( CONSOLE_VESAFB ) )
71 #undef CONSOLE_VESAFB
72 #define CONSOLE_VESAFB ( CONSOLE_USAGE_ALL & ~CONSOLE_USAGE_LOG )
73 #endif
74
75 /** Character height */
76 #define VESAFB_CHAR_HEIGHT 16
77
78 /** Font corresponding to selected character width and height */
79 #define VESAFB_FONT VBE_FONT_8x16
80
81 /* Forward declaration */
82 struct console_driver vesafb_console __console_driver;
83
84 /** A VESA frame buffer */
85 struct vesafb {
86 /** Frame buffer console */
87 struct fbcon fbcon;
88 /** Physical start address */
89 physaddr_t start;
90 /** Pixel geometry */
91 struct fbcon_geometry pixel;
92 /** Colour mapping */
93 struct fbcon_colour_map map;
94 /** Font definition */
95 struct fbcon_font font;
96 /** Character glyphs */
97 struct segoff glyphs;
98 /** Saved VGA mode */
99 uint8_t saved_mode;
100 };
101
102 /** The VESA frame buffer */
103 static struct vesafb vesafb;
104
105 /** Base memory buffer used for VBE calls */
106 union vbe_buffer {
107 /** VBE controller information block */
108 struct vbe_controller_info controller;
109 /** VBE mode information block */
110 struct vbe_mode_info mode;
111 };
112 static union vbe_buffer __bss16 ( vbe_buf );
113 #define vbe_buf __use_data16 ( vbe_buf )
114
115 /**
116 * Convert VBE status code to iPXE status code
117 *
118 * @v status VBE status code
119 * @ret rc Return status code
120 */
vesafb_rc(unsigned int status)121 static int vesafb_rc ( unsigned int status ) {
122 unsigned int code;
123
124 if ( ( status & 0xff ) != 0x4f )
125 return -ENOTSUP;
126 code = ( ( status >> 8 ) & 0xff );
127 return ( code ? -EIO_VBE ( code ) : 0 );
128 }
129
130 /**
131 * Get character glyph
132 *
133 * @v character Character
134 * @v glyph Character glyph to fill in
135 */
vesafb_glyph(unsigned int character,uint8_t * glyph)136 static void vesafb_glyph ( unsigned int character, uint8_t *glyph ) {
137 size_t offset = ( character * VESAFB_CHAR_HEIGHT );
138
139 copy_from_real ( glyph, vesafb.glyphs.segment,
140 ( vesafb.glyphs.offset + offset ), VESAFB_CHAR_HEIGHT);
141 }
142
143 /**
144 * Get font definition
145 *
146 */
vesafb_font(void)147 static void vesafb_font ( void ) {
148
149 /* Get font information
150 *
151 * Working around gcc bugs is icky here. The value we want is
152 * returned in %ebp, but there's no way to specify %ebp in an
153 * output constraint. We can't put %ebp in the clobber list,
154 * because this tends to cause random build failures on some
155 * gcc versions. We can't manually push/pop %ebp and return
156 * the value via a generic register output constraint, because
157 * gcc might choose to use %ebp to satisfy that constraint
158 * (and we have no way to prevent it from so doing).
159 *
160 * Work around this hideous mess by using %ecx and %edx as the
161 * output registers, since they get clobbered anyway.
162 */
163 __asm__ __volatile__ ( REAL_CODE ( "pushw %%bp\n\t" /* gcc bug */
164 "int $0x10\n\t"
165 "movw %%es, %%cx\n\t"
166 "movw %%bp, %%dx\n\t"
167 "popw %%bp\n\t" /* gcc bug */ )
168 : "=c" ( vesafb.glyphs.segment ),
169 "=d" ( vesafb.glyphs.offset )
170 : "a" ( VBE_GET_FONT ),
171 "b" ( VESAFB_FONT ) );
172 DBGC ( &vbe_buf, "VESAFB has font %04x at %04x:%04x\n",
173 VESAFB_FONT, vesafb.glyphs.segment, vesafb.glyphs.offset );
174 vesafb.font.height = VESAFB_CHAR_HEIGHT;
175 vesafb.font.glyph = vesafb_glyph;
176 }
177
178 /**
179 * Get VBE mode list
180 *
181 * @ret mode_numbers Mode number list (terminated with VBE_MODE_END)
182 * @ret rc Return status code
183 *
184 * The caller is responsible for eventually freeing the mode list.
185 */
vesafb_mode_list(uint16_t ** mode_numbers)186 static int vesafb_mode_list ( uint16_t **mode_numbers ) {
187 struct vbe_controller_info *controller = &vbe_buf.controller;
188 userptr_t video_mode_ptr;
189 uint16_t mode_number;
190 uint16_t status;
191 size_t len;
192 int rc;
193
194 /* Avoid returning uninitialised data on error */
195 *mode_numbers = NULL;
196
197 /* Get controller information block */
198 controller->vbe_signature = 0;
199 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
200 : "=a" ( status )
201 : "a" ( VBE_CONTROLLER_INFO ),
202 "D" ( __from_data16 ( controller ) )
203 : "memory", "ebx", "edx" );
204 if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
205 DBGC ( &vbe_buf, "VESAFB could not get controller information: "
206 "[%04x] %s\n", status, strerror ( rc ) );
207 return rc;
208 }
209 if ( controller->vbe_signature != VBE_CONTROLLER_SIGNATURE ) {
210 DBGC ( &vbe_buf, "VESAFB invalid controller signature "
211 "\"%c%c%c%c\"\n", ( controller->vbe_signature >> 0 ),
212 ( controller->vbe_signature >> 8 ),
213 ( controller->vbe_signature >> 16 ),
214 ( controller->vbe_signature >> 24 ) );
215 DBGC_HDA ( &vbe_buf, 0, controller, sizeof ( *controller ) );
216 return -EINVAL;
217 }
218 DBGC ( &vbe_buf, "VESAFB found VBE version %d.%d with mode list at "
219 "%04x:%04x\n", controller->vbe_major_version,
220 controller->vbe_minor_version,
221 controller->video_mode_ptr.segment,
222 controller->video_mode_ptr.offset );
223
224 /* Calculate length of mode list */
225 video_mode_ptr = real_to_user ( controller->video_mode_ptr.segment,
226 controller->video_mode_ptr.offset );
227 len = 0;
228 do {
229 copy_from_user ( &mode_number, video_mode_ptr, len,
230 sizeof ( mode_number ) );
231 len += sizeof ( mode_number );
232 } while ( mode_number != VBE_MODE_END );
233
234 /* Allocate and fill mode list */
235 *mode_numbers = malloc ( len );
236 if ( ! *mode_numbers )
237 return -ENOMEM;
238 copy_from_user ( *mode_numbers, video_mode_ptr, 0, len );
239
240 return 0;
241 }
242
243 /**
244 * Get video mode information
245 *
246 * @v mode_number Mode number
247 * @ret rc Return status code
248 */
vesafb_mode_info(unsigned int mode_number)249 static int vesafb_mode_info ( unsigned int mode_number ) {
250 struct vbe_mode_info *mode = &vbe_buf.mode;
251 uint16_t status;
252 int rc;
253
254 /* Get mode information */
255 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
256 : "=a" ( status )
257 : "a" ( VBE_MODE_INFO ),
258 "c" ( mode_number ),
259 "D" ( __from_data16 ( mode ) )
260 : "memory" );
261 if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
262 DBGC ( &vbe_buf, "VESAFB could not get mode %04x information: "
263 "[%04x] %s\n", mode_number, status, strerror ( rc ) );
264 return rc;
265 }
266 DBGC ( &vbe_buf, "VESAFB mode %04x %dx%d %dbpp(%d:%d:%d:%d) model "
267 "%02x [x%d]%s%s%s%s%s\n", mode_number, mode->x_resolution,
268 mode->y_resolution, mode->bits_per_pixel, mode->rsvd_mask_size,
269 mode->red_mask_size, mode->green_mask_size, mode->blue_mask_size,
270 mode->memory_model, ( mode->number_of_image_pages + 1 ),
271 ( ( mode->mode_attributes & VBE_MODE_ATTR_SUPPORTED ) ?
272 "" : " [unsupported]" ),
273 ( ( mode->mode_attributes & VBE_MODE_ATTR_TTY ) ?
274 " [tty]" : "" ),
275 ( ( mode->mode_attributes & VBE_MODE_ATTR_GRAPHICS ) ?
276 "" : " [text]" ),
277 ( ( mode->mode_attributes & VBE_MODE_ATTR_LINEAR ) ?
278 "" : " [nonlinear]" ),
279 ( ( mode->mode_attributes & VBE_MODE_ATTR_TRIPLE_BUF ) ?
280 " [buf]" : "" ) );
281
282 return 0;
283 }
284
285 /**
286 * Set video mode
287 *
288 * @v mode_number Mode number
289 * @ret rc Return status code
290 */
vesafb_set_mode(unsigned int mode_number)291 static int vesafb_set_mode ( unsigned int mode_number ) {
292 struct vbe_mode_info *mode = &vbe_buf.mode;
293 uint16_t status;
294 int rc;
295
296 /* Get mode information */
297 if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
298 return rc;
299
300 /* Record mode parameters */
301 vesafb.start = mode->phys_base_ptr;
302 vesafb.pixel.width = mode->x_resolution;
303 vesafb.pixel.height = mode->y_resolution;
304 vesafb.pixel.len = ( ( mode->bits_per_pixel + 7 ) / 8 );
305 vesafb.pixel.stride = mode->bytes_per_scan_line;
306 DBGC ( &vbe_buf, "VESAFB mode %04x has frame buffer at %08x\n",
307 mode_number, mode->phys_base_ptr );
308
309 /* Initialise font colours */
310 vesafb.map.red_scale = ( 8 - mode->red_mask_size );
311 vesafb.map.green_scale = ( 8 - mode->green_mask_size );
312 vesafb.map.blue_scale = ( 8 - mode->blue_mask_size );
313 vesafb.map.red_lsb = mode->red_field_position;
314 vesafb.map.green_lsb = mode->green_field_position;
315 vesafb.map.blue_lsb = mode->blue_field_position;
316
317 /* Select this mode */
318 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
319 : "=a" ( status )
320 : "a" ( VBE_SET_MODE ),
321 "b" ( mode_number ) );
322 if ( ( rc = vesafb_rc ( status ) ) != 0 ) {
323 DBGC ( &vbe_buf, "VESAFB could not set mode %04x: [%04x] %s\n",
324 mode_number, status, strerror ( rc ) );
325 return rc;
326 }
327
328 return 0;
329 }
330
331 /**
332 * Select video mode
333 *
334 * @v mode_numbers Mode number list (terminated with VBE_MODE_END)
335 * @v min_width Minimum required width (in pixels)
336 * @v min_height Minimum required height (in pixels)
337 * @v min_bpp Minimum required colour depth (in bits per pixel)
338 * @ret mode_number Mode number, or negative error
339 */
vesafb_select_mode(const uint16_t * mode_numbers,unsigned int min_width,unsigned int min_height,unsigned int min_bpp)340 static int vesafb_select_mode ( const uint16_t *mode_numbers,
341 unsigned int min_width, unsigned int min_height,
342 unsigned int min_bpp ) {
343 struct vbe_mode_info *mode = &vbe_buf.mode;
344 int best_mode_number = -ENOENT;
345 unsigned int best_score = INT_MAX;
346 unsigned int score;
347 uint16_t mode_number;
348 int rc;
349
350 /* Find the first suitable mode */
351 while ( ( mode_number = *(mode_numbers++) ) != VBE_MODE_END ) {
352
353 /* Force linear mode variant */
354 mode_number |= VBE_MODE_LINEAR;
355
356 /* Get mode information */
357 if ( ( rc = vesafb_mode_info ( mode_number ) ) != 0 )
358 continue;
359
360 /* Skip unusable modes */
361 if ( ( mode->mode_attributes & ( VBE_MODE_ATTR_SUPPORTED |
362 VBE_MODE_ATTR_GRAPHICS |
363 VBE_MODE_ATTR_LINEAR ) ) !=
364 ( VBE_MODE_ATTR_SUPPORTED | VBE_MODE_ATTR_GRAPHICS |
365 VBE_MODE_ATTR_LINEAR ) ) {
366 continue;
367 }
368 if ( mode->memory_model != VBE_MODE_MODEL_DIRECT_COLOUR )
369 continue;
370
371 /* Skip modes not meeting the requirements */
372 if ( ( mode->x_resolution < min_width ) ||
373 ( mode->y_resolution < min_height ) ||
374 ( mode->bits_per_pixel < min_bpp ) ) {
375 continue;
376 }
377
378 /* Select this mode if it has the best (i.e. lowest)
379 * score. We choose the scoring system to favour
380 * modes close to the specified width and height;
381 * within modes of the same width and height we prefer
382 * a higher colour depth.
383 */
384 score = ( ( mode->x_resolution * mode->y_resolution ) -
385 mode->bits_per_pixel );
386 if ( score < best_score ) {
387 best_mode_number = mode_number;
388 best_score = score;
389 }
390 }
391
392 if ( best_mode_number >= 0 ) {
393 DBGC ( &vbe_buf, "VESAFB selected mode %04x\n",
394 best_mode_number );
395 } else {
396 DBGC ( &vbe_buf, "VESAFB found no suitable mode\n" );
397 }
398
399 return best_mode_number;
400 }
401
402 /**
403 * Restore video mode
404 *
405 */
vesafb_restore(void)406 static void vesafb_restore ( void ) {
407 uint32_t discard_a;
408
409 /* Restore saved VGA mode */
410 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
411 : "=a" ( discard_a )
412 : "a" ( VBE_SET_VGA_MODE | vesafb.saved_mode ) );
413 DBGC ( &vbe_buf, "VESAFB restored VGA mode %#02x\n",
414 vesafb.saved_mode );
415 }
416
417 /**
418 * Initialise VESA frame buffer
419 *
420 * @v config Console configuration, or NULL to reset
421 * @ret rc Return status code
422 */
vesafb_init(struct console_configuration * config)423 static int vesafb_init ( struct console_configuration *config ) {
424 uint32_t discard_b;
425 uint16_t *mode_numbers;
426 int mode_number;
427 int rc;
428
429 /* Record current VGA mode */
430 __asm__ __volatile__ ( REAL_CODE ( "int $0x10" )
431 : "=a" ( vesafb.saved_mode ), "=b" ( discard_b )
432 : "a" ( VBE_GET_VGA_MODE ) );
433 DBGC ( &vbe_buf, "VESAFB saved VGA mode %#02x\n", vesafb.saved_mode );
434
435 /* Get VESA mode list */
436 if ( ( rc = vesafb_mode_list ( &mode_numbers ) ) != 0 )
437 goto err_mode_list;
438
439 /* Select mode */
440 if ( ( mode_number = vesafb_select_mode ( mode_numbers, config->width,
441 config->height,
442 config->depth ) ) < 0 ) {
443 rc = mode_number;
444 goto err_select_mode;
445 }
446
447 /* Set mode */
448 if ( ( rc = vesafb_set_mode ( mode_number ) ) != 0 )
449 goto err_set_mode;
450
451 /* Get font data */
452 vesafb_font();
453
454 /* Initialise frame buffer console */
455 if ( ( rc = fbcon_init ( &vesafb.fbcon, phys_to_user ( vesafb.start ),
456 &vesafb.pixel, &vesafb.map, &vesafb.font,
457 config ) ) != 0 )
458 goto err_fbcon_init;
459
460 free ( mode_numbers );
461 return 0;
462
463 fbcon_fini ( &vesafb.fbcon );
464 err_fbcon_init:
465 err_set_mode:
466 vesafb_restore();
467 err_select_mode:
468 free ( mode_numbers );
469 err_mode_list:
470 return rc;
471 }
472
473 /**
474 * Finalise VESA frame buffer
475 *
476 */
vesafb_fini(void)477 static void vesafb_fini ( void ) {
478
479 /* Finalise frame buffer console */
480 fbcon_fini ( &vesafb.fbcon );
481
482 /* Restore saved VGA mode */
483 vesafb_restore();
484 }
485
486 /**
487 * Print a character to current cursor position
488 *
489 * @v character Character
490 */
vesafb_putchar(int character)491 static void vesafb_putchar ( int character ) {
492
493 fbcon_putchar ( &vesafb.fbcon, character );
494 }
495
496 /**
497 * Configure console
498 *
499 * @v config Console configuration, or NULL to reset
500 * @ret rc Return status code
501 */
vesafb_configure(struct console_configuration * config)502 static int vesafb_configure ( struct console_configuration *config ) {
503 int rc;
504
505 /* Reset console, if applicable */
506 if ( ! vesafb_console.disabled ) {
507 vesafb_fini();
508 bios_console.disabled &= ~CONSOLE_DISABLED_OUTPUT;
509 ansicol_reset_magic();
510 }
511 vesafb_console.disabled = CONSOLE_DISABLED;
512
513 /* Do nothing more unless we have a usable configuration */
514 if ( ( config == NULL ) ||
515 ( config->width == 0 ) || ( config->height == 0 ) ) {
516 return 0;
517 }
518
519 /* Initialise VESA frame buffer */
520 if ( ( rc = vesafb_init ( config ) ) != 0 )
521 return rc;
522
523 /* Mark console as enabled */
524 vesafb_console.disabled = 0;
525 bios_console.disabled |= CONSOLE_DISABLED_OUTPUT;
526
527 /* Set magic colour to transparent if we have a background picture */
528 if ( config->pixbuf )
529 ansicol_set_magic_transparent();
530
531 return 0;
532 }
533
534 /** VESA frame buffer console driver */
535 struct console_driver vesafb_console __console_driver = {
536 .usage = CONSOLE_VESAFB,
537 .putchar = vesafb_putchar,
538 .configure = vesafb_configure,
539 .disabled = CONSOLE_DISABLED,
540 };
541