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 * Frame buffer console
32 #include <ipxe/ansiesc.h>
33 #include <ipxe/image.h>
34 #include <ipxe/pixbuf.h>
35 #include <ipxe/umalloc.h>
36 #include <ipxe/console.h>
37 #include <ipxe/fbcon.h>
40 * Calculate raw colour value
42 * @v fbcon Frame buffer console
43 * @v rgb 24-bit RGB value
46 static uint32_t fbcon_colour ( struct fbcon *fbcon, uint32_t rgb ) {
47 struct fbcon_colour_map *map = fbcon->map;
48 uint8_t red = ( rgb >> 16 );
49 uint8_t green = ( rgb >> 8 );
50 uint8_t blue = ( rgb >> 0 );
53 mapped = ( ( ( red >> map->red_scale ) << map->red_lsb ) |
54 ( ( green >> map->green_scale ) << map->green_lsb ) |
55 ( ( blue >> map->blue_scale ) << map->blue_lsb ) );
56 return cpu_to_le32 ( mapped );
60 * Calculate ANSI font colour
62 * @v fbcon Frame buffer console
63 * @v ansicol ANSI colour value (0-based)
64 * @ret colour Raw colour
66 static uint32_t fbcon_ansi_colour ( struct fbcon *fbcon,
67 unsigned int ansicol ) {
70 /* Treat ansicol as 3-bit BGR with intensity 0xaa */
71 rgb = ( ( ( ansicol & ( 1 << 0 ) ) ? 0xaa0000 : 0 ) |
72 ( ( ansicol & ( 1 << 1 ) ) ? 0x00aa00 : 0 ) |
73 ( ( ansicol & ( 1 << 2 ) ) ? 0x0000aa : 0 ) );
75 return fbcon_colour ( fbcon, rgb );
79 * Set default foreground colour
81 * @v fbcon Frame buffer console
83 static void fbcon_set_default_foreground ( struct fbcon *fbcon ) {
85 /* Default to non-bold white foreground */
86 fbcon->foreground = fbcon_ansi_colour ( fbcon, 0x7 );
91 * Set default background colour
93 * @v fbcon Frame buffer console
95 static void fbcon_set_default_background ( struct fbcon *fbcon ) {
97 /* Default to transparent background */
98 fbcon->background = FBCON_TRANSPARENT;
102 * Clear rows of characters
104 * @v fbcon Frame buffer console
105 * @v ypos Starting Y position
107 static void fbcon_clear ( struct fbcon *fbcon, unsigned int ypos ) {
108 struct fbcon_text_cell cell = {
109 .foreground = fbcon->foreground,
110 .background = fbcon->background,
116 /* Clear stored character array */
117 for ( ; ypos < fbcon->character.height ; ypos++ ) {
118 offset = ( ypos * fbcon->character.width * sizeof ( cell ) );
119 for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
120 copy_to_user ( fbcon->text.start, offset, &cell,
122 offset += sizeof ( cell );
128 * Store character at specified position
130 * @v fbcon Frame buffer console
135 static void fbcon_store ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
136 unsigned int xpos, unsigned int ypos ) {
140 offset = ( ( ( ypos * fbcon->character.width ) + xpos ) *
142 copy_to_user ( fbcon->text.start, offset, cell, sizeof ( *cell ) );
146 * Draw character at specified position
148 * @v fbcon Frame buffer console
153 static void fbcon_draw ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
154 unsigned int xpos, unsigned int ypos ) {
155 struct fbcon_font_glyph glyph;
165 /* Get font character */
166 copy_from_user ( &glyph, fbcon->font->start,
167 ( cell->character * sizeof ( glyph ) ),
170 /* Calculate pixel geometry */
171 offset = ( fbcon->indent +
172 ( ypos * fbcon->character.stride ) +
173 ( xpos * fbcon->character.len ) );
174 pixel_len = fbcon->pixel->len;
175 skip_len = ( fbcon->pixel->stride - fbcon->character.len );
177 /* Check for transparent background colour */
178 transparent = ( cell->background == FBCON_TRANSPARENT );
180 /* Draw character rows */
181 for ( row = 0 ; row < FBCON_CHAR_HEIGHT ; row++ ) {
183 /* Draw background picture, if applicable */
185 if ( fbcon->picture.start ) {
186 memcpy_user ( fbcon->start, offset,
187 fbcon->picture.start, offset,
188 fbcon->character.len );
190 memset_user ( fbcon->start, offset, 0,
191 fbcon->character.len );
195 /* Draw character row */
196 for ( column = FBCON_CHAR_WIDTH, bitmask = glyph.bitmask[row] ;
197 column ; column--, bitmask <<= 1, offset += pixel_len ) {
198 if ( bitmask & 0x80 ) {
199 src = &cell->foreground;
200 } else if ( ! transparent ) {
201 src = &cell->background;
205 copy_to_user ( fbcon->start, offset, src, pixel_len );
208 /* Move to next row */
214 * Redraw all characters
216 * @v fbcon Frame buffer console
218 static void fbcon_redraw ( struct fbcon *fbcon ) {
219 struct fbcon_text_cell cell;
224 /* Redraw characters */
225 for ( ypos = 0 ; ypos < fbcon->character.height ; ypos++ ) {
226 for ( xpos = 0 ; xpos < fbcon->character.width ; xpos++ ) {
227 copy_from_user ( &cell, fbcon->text.start, offset,
229 fbcon_draw ( fbcon, &cell, xpos, ypos );
230 offset += sizeof ( cell );
238 * @v fbcon Frame buffer console
240 static void fbcon_scroll ( struct fbcon *fbcon ) {
244 assert ( fbcon->ypos == fbcon->character.height );
246 /* Scroll up character array */
247 row_len = ( fbcon->character.width * sizeof ( struct fbcon_text_cell ));
248 memmove_user ( fbcon->text.start, 0, fbcon->text.start, row_len,
249 ( row_len * ( fbcon->character.height - 1 ) ) );
250 fbcon_clear ( fbcon, ( fbcon->character.height - 1 ) );
252 /* Update cursor position */
255 /* Redraw all characters */
256 fbcon_redraw ( fbcon );
260 * Draw character at cursor position
262 * @v fbcon Frame buffer console
263 * @v show_cursor Show cursor
265 static void fbcon_draw_cursor ( struct fbcon *fbcon, int show_cursor ) {
266 struct fbcon_text_cell cell;
269 offset = ( ( ( fbcon->ypos * fbcon->character.width ) + fbcon->xpos ) *
271 copy_from_user ( &cell, fbcon->text.start, offset, sizeof ( cell ) );
273 cell.background = fbcon->foreground;
274 cell.foreground = ( ( fbcon->background == FBCON_TRANSPARENT ) ?
275 0 : fbcon->background );
277 fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
281 * Handle ANSI CUP (cursor position)
283 * @v ctx ANSI escape sequence context
284 * @v count Parameter count
285 * @v params[0] Row (1 is top)
286 * @v params[1] Column (1 is left)
288 static void fbcon_handle_cup ( struct ansiesc_context *ctx,
289 unsigned int count __unused, int params[] ) {
290 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
291 int cx = ( params[1] - 1 );
292 int cy = ( params[0] - 1 );
294 fbcon_draw_cursor ( fbcon, 0 );
296 if ( fbcon->xpos >= fbcon->character.width )
299 if ( fbcon->ypos >= fbcon->character.height )
301 fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
305 * Handle ANSI ED (erase in page)
307 * @v ctx ANSI escape sequence context
308 * @v count Parameter count
309 * @v params[0] Region to erase
311 static void fbcon_handle_ed ( struct ansiesc_context *ctx,
312 unsigned int count __unused,
313 int params[] __unused ) {
314 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
316 /* We assume that we always clear the whole screen */
317 assert ( params[0] == ANSIESC_ED_ALL );
319 /* Clear character array */
320 fbcon_clear ( fbcon, 0 );
322 /* Redraw all characters */
323 fbcon_redraw ( fbcon );
325 /* Reset cursor position */
328 fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
332 * Handle ANSI SGR (set graphics rendition)
334 * @v ctx ANSI escape sequence context
335 * @v count Parameter count
336 * @v params List of graphic rendition aspects
338 static void fbcon_handle_sgr ( struct ansiesc_context *ctx, unsigned int count,
340 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
341 uint32_t *custom = NULL;
347 for ( i = 0 ; i < count ; i++ ) {
352 fbcon_set_default_foreground ( fbcon );
353 fbcon_set_default_background ( fbcon );
354 } else if ( aspect == 1 ) {
355 fbcon->bold = fbcon_colour ( fbcon, FBCON_BOLD );
356 } else if ( aspect == 22 ) {
358 } else if ( ( aspect >= 30 ) && ( aspect <= 37 ) ) {
360 fbcon_ansi_colour ( fbcon, aspect - 30 );
361 } else if ( aspect == 38 ) {
362 custom = &fbcon->foreground;
363 } else if ( aspect == 39 ) {
364 fbcon_set_default_foreground ( fbcon );
365 } else if ( ( aspect >= 40 ) && ( aspect <= 47 ) ) {
367 fbcon_ansi_colour ( fbcon, aspect - 40 );
368 } else if ( aspect == 48 ) {
369 custom = &fbcon->background;
370 } else if ( aspect == 49 ) {
371 fbcon_set_default_background ( fbcon );
374 /* Process custom RGB colour, if applicable
376 * We support the xterm-compatible
377 * "<ESC>[38;2;<red>;<green>;<blue>m" and
378 * "<ESC>[48;2;<red>;<green>;<blue>m" sequences.
383 for ( ; ( i < count ) && ( i < end ) ; i++ )
384 rgb = ( ( rgb << 8 ) | params[i] );
385 *custom = fbcon_colour ( fbcon, rgb );
392 * Handle ANSI DECTCEM set (show cursor)
394 * @v ctx ANSI escape sequence context
395 * @v count Parameter count
396 * @v params List of graphic rendition aspects
398 static void fbcon_handle_dectcem_set ( struct ansiesc_context *ctx,
399 unsigned int count __unused,
400 int params[] __unused ) {
401 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
403 fbcon->show_cursor = 1;
404 fbcon_draw_cursor ( fbcon, 1 );
408 * Handle ANSI DECTCEM reset (hide cursor)
410 * @v ctx ANSI escape sequence context
411 * @v count Parameter count
412 * @v params List of graphic rendition aspects
414 static void fbcon_handle_dectcem_reset ( struct ansiesc_context *ctx,
415 unsigned int count __unused,
416 int params[] __unused ) {
417 struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
419 fbcon->show_cursor = 0;
420 fbcon_draw_cursor ( fbcon, 0 );
423 /** ANSI escape sequence handlers */
424 static struct ansiesc_handler fbcon_ansiesc_handlers[] = {
425 { ANSIESC_CUP, fbcon_handle_cup },
426 { ANSIESC_ED, fbcon_handle_ed },
427 { ANSIESC_SGR, fbcon_handle_sgr },
428 { ANSIESC_DECTCEM_SET, fbcon_handle_dectcem_set },
429 { ANSIESC_DECTCEM_RESET, fbcon_handle_dectcem_reset },
434 * Print a character to current cursor position
436 * @v fbcon Frame buffer console
437 * @v character Character
439 void fbcon_putchar ( struct fbcon *fbcon, int character ) {
440 struct fbcon_text_cell cell;
442 /* Intercept ANSI escape sequences */
443 character = ansiesc_process ( &fbcon->ctx, character );
447 /* Handle control characters */
448 switch ( character ) {
450 fbcon_draw_cursor ( fbcon, 0 );
454 fbcon_draw_cursor ( fbcon, 0 );
459 fbcon_draw_cursor ( fbcon, 0 );
462 } else if ( fbcon->ypos ) {
463 fbcon->xpos = ( fbcon->character.width - 1 );
468 /* Print character at current cursor position */
469 cell.foreground = ( fbcon->foreground | fbcon->bold );
470 cell.background = fbcon->background;
471 cell.character = character;
472 fbcon_store ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
473 fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
477 if ( fbcon->xpos >= fbcon->character.width ) {
484 /* Scroll screen if necessary */
485 if ( fbcon->ypos >= fbcon->character.height )
486 fbcon_scroll ( fbcon );
489 fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
493 * Initialise background picture
495 * @v fbcon Frame buffer console
496 * @v pixbuf Background picture
497 * @ret rc Return status code
499 static int fbcon_picture_init ( struct fbcon *fbcon,
500 struct pixel_buffer *pixbuf ) {
501 struct fbcon_geometry *pixel = fbcon->pixel;
502 struct fbcon_picture *picture = &fbcon->picture;
504 size_t pixbuf_stride;
506 size_t pixbuf_indent;
508 size_t pixbuf_offset;
519 /* Allocate buffer */
520 len = ( pixel->height * pixel->stride );
521 picture->start = umalloc ( len );
522 if ( ! picture->start ) {
523 DBGC ( fbcon, "FBCON %p could not allocate %zd bytes for "
524 "picture\n", fbcon, len );
529 /* Centre picture on console */
530 pixbuf_stride = ( pixbuf->width * sizeof ( rgb ) );
531 xgap = ( ( ( int ) ( pixel->width - pixbuf->width ) ) / 2 );
532 ygap = ( ( ( int ) ( pixel->height - pixbuf->height ) ) / 2 );
533 indent = ( ( ( ( ygap >= 0 ) ? ygap : 0 ) * pixel->stride ) +
534 ( ( ( xgap >= 0 ) ? xgap : 0 ) * pixel->len ) );
535 pixbuf_indent = ( ( ( ( ygap < 0 ) ? -ygap : 0 ) * pixbuf_stride ) +
536 ( ( ( xgap < 0 ) ? -xgap : 0 ) * sizeof ( rgb ) ) );
537 width = pixbuf->width;
538 if ( width > pixel->width )
539 width = pixel->width;
540 height = pixbuf->height;
541 if ( height > pixel->height )
542 height = pixel->height;
543 DBGC ( fbcon, "FBCON %p picture is pixel %dx%d at [%d,%d),[%d,%d)\n",
544 fbcon, width, height, xgap, ( xgap + pixbuf->width ), ygap,
545 ( ygap + pixbuf->height ) );
547 /* Convert to frame buffer raw format */
548 memset_user ( picture->start, 0, 0, len );
549 for ( y = 0 ; y < height ; y++ ) {
550 offset = ( indent + ( y * pixel->stride ) );
551 pixbuf_offset = ( pixbuf_indent + ( y * pixbuf_stride ) );
552 for ( x = 0 ; x < width ; x++ ) {
553 copy_from_user ( &rgb, pixbuf->data, pixbuf_offset,
555 raw = fbcon_colour ( fbcon, rgb );
556 copy_to_user ( picture->start, offset, &raw,
558 offset += pixel->len;
559 pixbuf_offset += sizeof ( rgb );
565 ufree ( picture->start );
571 * Initialise frame buffer console
573 * @v fbcon Frame buffer console
574 * @v start Start address
575 * @v pixel Pixel geometry
576 * @v margin Minimum margin
577 * @v map Colour mapping
578 * @v font Font definition
579 * @v pixbuf Background picture (if any)
580 * @ret rc Return status code
582 int fbcon_init ( struct fbcon *fbcon, userptr_t start,
583 struct fbcon_geometry *pixel,
584 struct fbcon_margin *margin,
585 struct fbcon_colour_map *map,
586 struct fbcon_font *font,
587 struct pixel_buffer *pixbuf ) {
594 /* Initialise data structure */
595 memset ( fbcon, 0, sizeof ( *fbcon ) );
596 fbcon->start = start;
597 fbcon->pixel = pixel;
598 assert ( pixel->len <= sizeof ( uint32_t ) );
601 fbcon->ctx.handlers = fbcon_ansiesc_handlers;
602 fbcon->show_cursor = 1;
604 /* Derive overall length */
605 fbcon->len = ( pixel->height * pixel->stride );
606 DBGC ( fbcon, "FBCON %p at [%08lx,%08lx)\n", fbcon,
607 user_to_phys ( fbcon->start, 0 ),
608 user_to_phys ( fbcon->start, fbcon->len ) );
610 /* Expand margin to accommodate whole characters */
611 width = ( pixel->width - margin->left - margin->right );
612 height = ( pixel->height - margin->top - margin->bottom );
613 if ( ( width < FBCON_CHAR_WIDTH ) || ( height < FBCON_CHAR_HEIGHT ) ) {
614 DBGC ( fbcon, "FBCON %p has unusable character area "
615 "[%d-%d),[%d-%d)\n", fbcon,
616 margin->left, ( pixel->width - margin->right ),
617 margin->top, ( pixel->height - margin->bottom ) );
621 xgap = ( width % FBCON_CHAR_WIDTH );
622 ygap = ( height % FBCON_CHAR_HEIGHT );
623 fbcon->margin.left = ( margin->left + ( xgap / 2 ) );
624 fbcon->margin.top = ( margin->top + ( ygap / 2 ) );
625 fbcon->margin.right = ( margin->right + ( xgap - ( xgap / 2 ) ) );
626 fbcon->margin.bottom = ( margin->bottom + ( ygap - ( ygap / 2 ) ) );
627 fbcon->indent = ( ( fbcon->margin.top * pixel->stride ) +
628 ( fbcon->margin.left * pixel->len ) );
630 /* Derive character geometry from pixel geometry */
631 fbcon->character.width = ( width / FBCON_CHAR_WIDTH );
632 fbcon->character.height = ( height / FBCON_CHAR_HEIGHT );
633 fbcon->character.len = ( pixel->len * FBCON_CHAR_WIDTH );
634 fbcon->character.stride = ( pixel->stride * FBCON_CHAR_HEIGHT );
635 DBGC ( fbcon, "FBCON %p is pixel %dx%d, char %dx%d at "
636 "[%d-%d),[%d-%d)\n", fbcon, fbcon->pixel->width,
637 fbcon->pixel->height, fbcon->character.width,
638 fbcon->character.height, fbcon->margin.left,
639 ( fbcon->pixel->width - fbcon->margin.right ),
641 ( fbcon->pixel->height - fbcon->margin.bottom ) );
643 /* Set default colours */
644 fbcon_set_default_foreground ( fbcon );
645 fbcon_set_default_background ( fbcon );
647 /* Allocate and initialise stored character array */
648 fbcon->text.start = umalloc ( fbcon->character.width *
649 fbcon->character.height *
650 sizeof ( struct fbcon_text_cell ) );
651 if ( ! fbcon->text.start ) {
655 fbcon_clear ( fbcon, 0 );
657 /* Set framebuffer to all black (including margins) */
658 memset_user ( fbcon->start, 0, 0, fbcon->len );
660 /* Generate pixel buffer from background image, if applicable */
661 if ( pixbuf && ( ( rc = fbcon_picture_init ( fbcon, pixbuf ) ) != 0 ) )
664 /* Draw background picture (including margins), if applicable */
665 if ( fbcon->picture.start ) {
666 memcpy_user ( fbcon->start, 0, fbcon->picture.start, 0,
670 /* Update console width and height */
671 console_set_size ( fbcon->character.width, fbcon->character.height );
675 ufree ( fbcon->picture.start );
677 ufree ( fbcon->text.start );
684 * Finalise frame buffer console
686 * @v fbcon Frame buffer console
688 void fbcon_fini ( struct fbcon *fbcon ) {
690 ufree ( fbcon->text.start );
691 ufree ( fbcon->picture.start );