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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * Frame buffer console
36 #include <ipxe/ansiesc.h>
37 #include <ipxe/image.h>
38 #include <ipxe/pixbuf.h>
39 #include <ipxe/umalloc.h>
40 #include <ipxe/console.h>
41 #include <ipxe/fbcon.h>
44 * Calculate raw colour value
46 * @v fbcon Frame buffer console
47 * @v rgb 24-bit RGB value
50 static uint32_t fbcon_colour ( struct fbcon *fbcon, uint32_t rgb ) {
51 struct fbcon_colour_map *map = fbcon->map;
52 uint8_t red = ( rgb >> 16 );
53 uint8_t green = ( rgb >> 8 );
54 uint8_t blue = ( rgb >> 0 );
57 mapped = ( ( ( red >> map->red_scale ) << map->red_lsb ) |
58 ( ( green >> map->green_scale ) << map->green_lsb ) |
59 ( ( blue >> map->blue_scale ) << map->blue_lsb ) );
60 return cpu_to_le32 ( mapped );
64 * Calculate ANSI font colour
66 * @v fbcon Frame buffer console
67 * @v ansicol ANSI colour value (0-based)
68 * @ret colour Raw colour
70 static uint32_t fbcon_ansi_colour ( struct fbcon *fbcon,
71 unsigned int ansicol ) {
74 /* Treat ansicol as 3-bit BGR with intensity 0xaa */
75 rgb = ( ( ( ansicol & ( 1 << 0 ) ) ? 0xaa0000 : 0 ) |
76 ( ( ansicol & ( 1 << 1 ) ) ? 0x00aa00 : 0 ) |
77 ( ( ansicol & ( 1 << 2 ) ) ? 0x0000aa : 0 ) );
79 return fbcon_colour ( fbcon, rgb );
83 * Set default foreground colour
85 * @v fbcon Frame buffer console
87 static void fbcon_set_default_foreground ( struct fbcon *fbcon ) {
89 /* Default to non-bold white foreground */
90 fbcon->foreground = fbcon_ansi_colour ( fbcon, 0x7 );
95 * Set default background colour
97 * @v fbcon Frame buffer console
99 static void fbcon_set_default_background ( struct fbcon *fbcon ) {
101 /* Default to transparent background */
102 fbcon->background = FBCON_TRANSPARENT;
106 * Clear rows of characters
108 * @v fbcon Frame buffer console
109 * @v ypos Starting Y position
111 static void fbcon_clear ( struct fbcon *fbcon, unsigned int ypos ) {
112 struct fbcon_text_cell cell = {
113 .foreground = fbcon->foreground,
114 .background = fbcon->background,
120 /* Clear stored character array */
121 for ( ; ypos < fbcon->character.height ; ypos++ ) {
122 offset = ( ypos * fbcon->character.width * sizeof ( cell ) );
123 for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
124 copy_to_user ( fbcon->text.start, offset, &cell,
126 offset += sizeof ( cell );
132 * Store character at specified position
134 * @v fbcon Frame buffer console
139 static void fbcon_store ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
140 unsigned int xpos, unsigned int ypos ) {
144 offset = ( ( ( ypos * fbcon->character.width ) + xpos ) *
146 copy_to_user ( fbcon->text.start, offset, cell, sizeof ( *cell ) );
150 * Draw character at specified position
152 * @v fbcon Frame buffer console
157 static void fbcon_draw ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
158 unsigned int xpos, unsigned int ypos ) {
159 struct fbcon_font_glyph glyph;
169 /* Get font character */
170 copy_from_user ( &glyph, fbcon->font->start,
171 ( cell->character * sizeof ( glyph ) ),
174 /* Calculate pixel geometry */
175 offset = ( fbcon->indent +
176 ( ypos * fbcon->character.stride ) +
177 ( xpos * fbcon->character.len ) );
178 pixel_len = fbcon->pixel->len;
179 skip_len = ( fbcon->pixel->stride - fbcon->character.len );
181 /* Check for transparent background colour */
182 transparent = ( cell->background == FBCON_TRANSPARENT );
184 /* Draw character rows */
185 for ( row = 0 ; row < FBCON_CHAR_HEIGHT ; row++ ) {
187 /* Draw background picture, if applicable */
189 if ( fbcon->picture.start ) {
190 memcpy_user ( fbcon->start, offset,
191 fbcon->picture.start, offset,
192 fbcon->character.len );
194 memset_user ( fbcon->start, offset, 0,
195 fbcon->character.len );
199 /* Draw character row */
200 for ( column = FBCON_CHAR_WIDTH, bitmask = glyph.bitmask[row] ;
201 column ; column--, bitmask <<= 1, offset += pixel_len ) {
202 if ( bitmask & 0x80 ) {
203 src = &cell->foreground;
204 } else if ( ! transparent ) {
205 src = &cell->background;
209 copy_to_user ( fbcon->start, offset, src, pixel_len );
212 /* Move to next row */
218 * Redraw all characters
220 * @v fbcon Frame buffer console
222 static void fbcon_redraw ( struct fbcon *fbcon ) {
223 struct fbcon_text_cell cell;
228 /* Redraw characters */
229 for ( ypos = 0 ; ypos < fbcon->character.height ; ypos++ ) {
230 for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
231 copy_from_user ( &cell, fbcon->text.start, offset,
233 fbcon_draw ( fbcon, &cell, xpos, ypos );
234 offset += sizeof ( cell );
242 * @v fbcon Frame buffer console
244 static void fbcon_scroll ( struct fbcon *fbcon ) {
248 assert ( fbcon->ypos == fbcon->character.height );
250 /* Scroll up character array */
251 row_len = ( fbcon->character.width * sizeof ( struct fbcon_text_cell ));
252 memmove_user ( fbcon->text.start, 0, fbcon->text.start, row_len,
253 ( row_len * ( fbcon->character.height - 1 ) ) );
254 fbcon_clear ( fbcon, ( fbcon->character.height - 1 ) );
256 /* Update cursor position */
259 /* Redraw all characters */
260 fbcon_redraw ( fbcon );
264 * Draw character at cursor position
266 * @v fbcon Frame buffer console
267 * @v show_cursor Show cursor
269 static void fbcon_draw_cursor ( struct fbcon *fbcon, int show_cursor ) {
270 struct fbcon_text_cell cell;
273 offset = ( ( ( fbcon->ypos * fbcon->character.width ) + fbcon->xpos ) *
275 copy_from_user ( &cell, fbcon->text.start, offset, sizeof ( cell ) );
277 cell.background = fbcon->foreground;
278 cell.foreground = ( ( fbcon->background == FBCON_TRANSPARENT ) ?
279 0 : fbcon->background );
281 fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
285 * Handle ANSI CUP (cursor position)
287 * @v ctx ANSI escape sequence context
288 * @v count Parameter count
289 * @v params[0] Row (1 is top)
290 * @v params[1] Column (1 is left)
292 static void fbcon_handle_cup ( struct ansiesc_context *ctx,
293 unsigned int count __unused, int params[] ) {
294 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
295 int cx = ( params[1] - 1 );
296 int cy = ( params[0] - 1 );
298 fbcon_draw_cursor ( fbcon, 0 );
300 if ( fbcon->xpos >= fbcon->character.width )
303 if ( fbcon->ypos >= fbcon->character.height )
305 fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
309 * Handle ANSI ED (erase in page)
311 * @v ctx ANSI escape sequence context
312 * @v count Parameter count
313 * @v params[0] Region to erase
315 static void fbcon_handle_ed ( struct ansiesc_context *ctx,
316 unsigned int count __unused,
317 int params[] __unused ) {
318 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
320 /* We assume that we always clear the whole screen */
321 assert ( params[0] == ANSIESC_ED_ALL );
323 /* Clear character array */
324 fbcon_clear ( fbcon, 0 );
326 /* Redraw all characters */
327 fbcon_redraw ( fbcon );
329 /* Reset cursor position */
332 fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
336 * Handle ANSI SGR (set graphics rendition)
338 * @v ctx ANSI escape sequence context
339 * @v count Parameter count
340 * @v params List of graphic rendition aspects
342 static void fbcon_handle_sgr ( struct ansiesc_context *ctx, unsigned int count,
344 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
345 uint32_t *custom = NULL;
351 for ( i = 0 ; i < count ; i++ ) {
356 fbcon_set_default_foreground ( fbcon );
357 fbcon_set_default_background ( fbcon );
358 } else if ( aspect == 1 ) {
359 fbcon->bold = fbcon_colour ( fbcon, FBCON_BOLD );
360 } else if ( aspect == 22 ) {
362 } else if ( ( aspect >= 30 ) && ( aspect <= 37 ) ) {
364 fbcon_ansi_colour ( fbcon, aspect - 30 );
365 } else if ( aspect == 38 ) {
366 custom = &fbcon->foreground;
367 } else if ( aspect == 39 ) {
368 fbcon_set_default_foreground ( fbcon );
369 } else if ( ( aspect >= 40 ) && ( aspect <= 47 ) ) {
371 fbcon_ansi_colour ( fbcon, aspect - 40 );
372 } else if ( aspect == 48 ) {
373 custom = &fbcon->background;
374 } else if ( aspect == 49 ) {
375 fbcon_set_default_background ( fbcon );
378 /* Process custom RGB colour, if applicable
380 * We support the xterm-compatible
381 * "<ESC>[38;2;<red>;<green>;<blue>m" and
382 * "<ESC>[48;2;<red>;<green>;<blue>m" sequences.
387 for ( ; ( i < count ) && ( i < end ) ; i++ )
388 rgb = ( ( rgb << 8 ) | params[i] );
389 *custom = fbcon_colour ( fbcon, rgb );
396 * Handle ANSI DECTCEM set (show cursor)
398 * @v ctx ANSI escape sequence context
399 * @v count Parameter count
400 * @v params List of graphic rendition aspects
402 static void fbcon_handle_dectcem_set ( struct ansiesc_context *ctx,
403 unsigned int count __unused,
404 int params[] __unused ) {
405 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
407 fbcon->show_cursor = 1;
408 fbcon_draw_cursor ( fbcon, 1 );
412 * Handle ANSI DECTCEM reset (hide cursor)
414 * @v ctx ANSI escape sequence context
415 * @v count Parameter count
416 * @v params List of graphic rendition aspects
418 static void fbcon_handle_dectcem_reset ( struct ansiesc_context *ctx,
419 unsigned int count __unused,
420 int params[] __unused ) {
421 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
423 fbcon->show_cursor = 0;
424 fbcon_draw_cursor ( fbcon, 0 );
427 /** ANSI escape sequence handlers */
428 static struct ansiesc_handler fbcon_ansiesc_handlers[] = {
429 { ANSIESC_CUP, fbcon_handle_cup },
430 { ANSIESC_ED, fbcon_handle_ed },
431 { ANSIESC_SGR, fbcon_handle_sgr },
432 { ANSIESC_DECTCEM_SET, fbcon_handle_dectcem_set },
433 { ANSIESC_DECTCEM_RESET, fbcon_handle_dectcem_reset },
438 * Print a character to current cursor position
440 * @v fbcon Frame buffer console
441 * @v character Character
443 void fbcon_putchar ( struct fbcon *fbcon, int character ) {
444 struct fbcon_text_cell cell;
446 /* Intercept ANSI escape sequences */
447 character = ansiesc_process ( &fbcon->ctx, character );
451 /* Handle control characters */
452 switch ( character ) {
454 fbcon_draw_cursor ( fbcon, 0 );
458 fbcon_draw_cursor ( fbcon, 0 );
463 fbcon_draw_cursor ( fbcon, 0 );
466 } else if ( fbcon->ypos ) {
467 fbcon->xpos = ( fbcon->character.width - 1 );
472 /* Print character at current cursor position */
473 cell.foreground = ( fbcon->foreground | fbcon->bold );
474 cell.background = fbcon->background;
475 cell.character = character;
476 fbcon_store ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
477 fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
481 if ( fbcon->xpos >= fbcon->character.width ) {
488 /* Scroll screen if necessary */
489 if ( fbcon->ypos >= fbcon->character.height )
490 fbcon_scroll ( fbcon );
493 fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
497 * Initialise background picture
499 * @v fbcon Frame buffer console
500 * @v pixbuf Background picture
501 * @ret rc Return status code
503 static int fbcon_picture_init ( struct fbcon *fbcon,
504 struct pixel_buffer *pixbuf ) {
505 struct fbcon_geometry *pixel = fbcon->pixel;
506 struct fbcon_picture *picture = &fbcon->picture;
508 size_t pixbuf_stride;
510 size_t pixbuf_indent;
512 size_t pixbuf_offset;
523 /* Allocate buffer */
524 len = ( pixel->height * pixel->stride );
525 picture->start = umalloc ( len );
526 if ( ! picture->start ) {
527 DBGC ( fbcon, "FBCON %p could not allocate %zd bytes for "
528 "picture\n", fbcon, len );
533 /* Centre picture on console */
534 pixbuf_stride = ( pixbuf->width * sizeof ( rgb ) );
535 xgap = ( ( ( int ) ( pixel->width - pixbuf->width ) ) / 2 );
536 ygap = ( ( ( int ) ( pixel->height - pixbuf->height ) ) / 2 );
537 indent = ( ( ( ( ygap >= 0 ) ? ygap : 0 ) * pixel->stride ) +
538 ( ( ( xgap >= 0 ) ? xgap : 0 ) * pixel->len ) );
539 pixbuf_indent = ( ( ( ( ygap < 0 ) ? -ygap : 0 ) * pixbuf_stride ) +
540 ( ( ( xgap < 0 ) ? -xgap : 0 ) * sizeof ( rgb ) ) );
541 width = pixbuf->width;
542 if ( width > pixel->width )
543 width = pixel->width;
544 height = pixbuf->height;
545 if ( height > pixel->height )
546 height = pixel->height;
547 DBGC ( fbcon, "FBCON %p picture is pixel %dx%d at [%d,%d),[%d,%d)\n",
548 fbcon, width, height, xgap, ( xgap + pixbuf->width ), ygap,
549 ( ygap + pixbuf->height ) );
551 /* Convert to frame buffer raw format */
552 memset_user ( picture->start, 0, 0, len );
553 for ( y = 0 ; y < height ; y++ ) {
554 offset = ( indent + ( y * pixel->stride ) );
555 pixbuf_offset = ( pixbuf_indent + ( y * pixbuf_stride ) );
556 for ( x = 0 ; x < width ; x++ ) {
557 copy_from_user ( &rgb, pixbuf->data, pixbuf_offset,
559 raw = fbcon_colour ( fbcon, rgb );
560 copy_to_user ( picture->start, offset, &raw,
562 offset += pixel->len;
563 pixbuf_offset += sizeof ( rgb );
569 ufree ( picture->start );
575 * Initialise frame buffer console
577 * @v fbcon Frame buffer console
578 * @v start Start address
579 * @v pixel Pixel geometry
580 * @v margin Minimum margin
581 * @v map Colour mapping
582 * @v font Font definition
583 * @v pixbuf Background picture (if any)
584 * @ret rc Return status code
586 int fbcon_init ( struct fbcon *fbcon, userptr_t start,
587 struct fbcon_geometry *pixel,
588 struct fbcon_margin *margin,
589 struct fbcon_colour_map *map,
590 struct fbcon_font *font,
591 struct pixel_buffer *pixbuf ) {
598 /* Initialise data structure */
599 memset ( fbcon, 0, sizeof ( *fbcon ) );
600 fbcon->start = start;
601 fbcon->pixel = pixel;
602 assert ( pixel->len <= sizeof ( uint32_t ) );
605 fbcon->ctx.handlers = fbcon_ansiesc_handlers;
606 fbcon->show_cursor = 1;
608 /* Derive overall length */
609 fbcon->len = ( pixel->height * pixel->stride );
610 DBGC ( fbcon, "FBCON %p at [%08lx,%08lx)\n", fbcon,
611 user_to_phys ( fbcon->start, 0 ),
612 user_to_phys ( fbcon->start, fbcon->len ) );
614 /* Expand margin to accommodate whole characters */
615 width = ( pixel->width - margin->left - margin->right );
616 height = ( pixel->height - margin->top - margin->bottom );
617 if ( ( width < FBCON_CHAR_WIDTH ) || ( height < FBCON_CHAR_HEIGHT ) ) {
618 DBGC ( fbcon, "FBCON %p has unusable character area "
619 "[%d-%d),[%d-%d)\n", fbcon,
620 margin->left, ( pixel->width - margin->right ),
621 margin->top, ( pixel->height - margin->bottom ) );
625 xgap = ( width % FBCON_CHAR_WIDTH );
626 ygap = ( height % FBCON_CHAR_HEIGHT );
627 fbcon->margin.left = ( margin->left + ( xgap / 2 ) );
628 fbcon->margin.top = ( margin->top + ( ygap / 2 ) );
629 fbcon->margin.right = ( margin->right + ( xgap - ( xgap / 2 ) ) );
630 fbcon->margin.bottom = ( margin->bottom + ( ygap - ( ygap / 2 ) ) );
631 fbcon->indent = ( ( fbcon->margin.top * pixel->stride ) +
632 ( fbcon->margin.left * pixel->len ) );
634 /* Derive character geometry from pixel geometry */
635 fbcon->character.width = ( width / FBCON_CHAR_WIDTH );
636 fbcon->character.height = ( height / FBCON_CHAR_HEIGHT );
637 fbcon->character.len = ( pixel->len * FBCON_CHAR_WIDTH );
638 fbcon->character.stride = ( pixel->stride * FBCON_CHAR_HEIGHT );
639 DBGC ( fbcon, "FBCON %p is pixel %dx%d, char %dx%d at "
640 "[%d-%d),[%d-%d)\n", fbcon, fbcon->pixel->width,
641 fbcon->pixel->height, fbcon->character.width,
642 fbcon->character.height, fbcon->margin.left,
643 ( fbcon->pixel->width - fbcon->margin.right ),
645 ( fbcon->pixel->height - fbcon->margin.bottom ) );
647 /* Set default colours */
648 fbcon_set_default_foreground ( fbcon );
649 fbcon_set_default_background ( fbcon );
651 /* Allocate and initialise stored character array */
652 fbcon->text.start = umalloc ( fbcon->character.width *
653 fbcon->character.height *
654 sizeof ( struct fbcon_text_cell ) );
655 if ( ! fbcon->text.start ) {
659 fbcon_clear ( fbcon, 0 );
661 /* Set framebuffer to all black (including margins) */
662 memset_user ( fbcon->start, 0, 0, fbcon->len );
664 /* Generate pixel buffer from background image, if applicable */
665 if ( pixbuf && ( ( rc = fbcon_picture_init ( fbcon, pixbuf ) ) != 0 ) )
668 /* Draw background picture (including margins), if applicable */
669 if ( fbcon->picture.start ) {
670 memcpy_user ( fbcon->start, 0, fbcon->picture.start, 0,
674 /* Update console width and height */
675 console_set_size ( fbcon->character.width, fbcon->character.height );
679 ufree ( fbcon->picture.start );
681 ufree ( fbcon->text.start );
688 * Finalise frame buffer console
690 * @v fbcon Frame buffer console
692 void fbcon_fini ( struct fbcon *fbcon ) {
694 ufree ( fbcon->text.start );
695 ufree ( fbcon->picture.start );