These changes are the raw update to qemu-2.6.
[kvmfornfv.git] / qemu / roms / ipxe / src / core / fbcon.c
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  * Frame buffer console
29  *
30  */
31
32 #include <string.h>
33 #include <errno.h>
34 #include <assert.h>
35 #include <byteswap.h>
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>
42
43 /**
44  * Calculate raw colour value
45  *
46  * @v fbcon             Frame buffer console
47  * @v rgb               24-bit RGB value
48  * @ret raw             Raw colour
49  */
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 );
55         uint32_t mapped;
56
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 );
61 }
62
63 /**
64  * Calculate ANSI font colour
65  *
66  * @v fbcon             Frame buffer console
67  * @v ansicol           ANSI colour value (0-based)
68  * @ret colour          Raw colour
69  */
70 static uint32_t fbcon_ansi_colour ( struct fbcon *fbcon,
71                                     unsigned int ansicol ) {
72         uint32_t rgb;
73
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 ) );
78
79         return fbcon_colour ( fbcon, rgb );
80 }
81
82 /**
83  * Set default foreground colour
84  *
85  * @v fbcon             Frame buffer console
86  */
87 static void fbcon_set_default_foreground ( struct fbcon *fbcon ) {
88
89         /* Default to non-bold white foreground */
90         fbcon->foreground = fbcon_ansi_colour ( fbcon, 0x7 );
91         fbcon->bold = 0;
92 }
93
94 /**
95  * Set default background colour
96  *
97  * @v fbcon             Frame buffer console
98  */
99 static void fbcon_set_default_background ( struct fbcon *fbcon ) {
100
101         /* Default to transparent background */
102         fbcon->background = FBCON_TRANSPARENT;
103 }
104
105 /**
106  * Clear rows of characters
107  *
108  * @v fbcon             Frame buffer console
109  * @v ypos              Starting Y position
110  */
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,
115                 .character = ' ',
116         };
117         size_t offset;
118         unsigned int xpos;
119
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,
125                                        sizeof ( cell ) );
126                         offset += sizeof ( cell );
127                 }
128         }
129 }
130
131 /**
132  * Store character at specified position
133  *
134  * @v fbcon             Frame buffer console
135  * @v cell              Text cell
136  * @v xpos              X position
137  * @v ypos              Y position
138  */
139 static void fbcon_store ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
140                           unsigned int xpos, unsigned int ypos ) {
141         size_t offset;
142
143         /* Store cell */
144         offset = ( ( ( ypos * fbcon->character.width ) + xpos ) *
145                    sizeof ( *cell ) );
146         copy_to_user ( fbcon->text.start, offset, cell, sizeof ( *cell ) );
147 }
148
149 /**
150  * Draw character at specified position
151  *
152  * @v fbcon             Frame buffer console
153  * @v cell              Text cell
154  * @v xpos              X position
155  * @v ypos              Y position
156  */
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;
160         size_t offset;
161         size_t pixel_len;
162         size_t skip_len;
163         unsigned int row;
164         unsigned int column;
165         uint8_t bitmask;
166         int transparent;
167         void *src;
168
169         /* Get font character */
170         copy_from_user ( &glyph, fbcon->font->start,
171                          ( cell->character * sizeof ( glyph ) ),
172                          sizeof ( glyph ) );
173
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 );
180
181         /* Check for transparent background colour */
182         transparent = ( cell->background == FBCON_TRANSPARENT );
183
184         /* Draw character rows */
185         for ( row = 0 ; row < FBCON_CHAR_HEIGHT ; row++ ) {
186
187                 /* Draw background picture, if applicable */
188                 if ( transparent ) {
189                         if ( fbcon->picture.start ) {
190                                 memcpy_user ( fbcon->start, offset,
191                                               fbcon->picture.start, offset,
192                                               fbcon->character.len );
193                         } else {
194                                 memset_user ( fbcon->start, offset, 0,
195                                               fbcon->character.len );
196                         }
197                 }
198
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;
206                         } else {
207                                 continue;
208                         }
209                         copy_to_user ( fbcon->start, offset, src, pixel_len );
210                 }
211
212                 /* Move to next row */
213                 offset += skip_len;
214         }
215 }
216
217 /**
218  * Redraw all characters
219  *
220  * @v fbcon             Frame buffer console
221  */
222 static void fbcon_redraw ( struct fbcon *fbcon ) {
223         struct fbcon_text_cell cell;
224         size_t offset = 0;
225         unsigned int xpos;
226         unsigned int ypos;
227
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,
232                                          sizeof ( cell ) );
233                         fbcon_draw ( fbcon, &cell, xpos, ypos );
234                         offset += sizeof ( cell );
235                 }
236         }
237 }
238
239 /**
240  * Scroll screen
241  *
242  * @v fbcon             Frame buffer console
243  */
244 static void fbcon_scroll ( struct fbcon *fbcon ) {
245         size_t row_len;
246
247         /* Sanity check */
248         assert ( fbcon->ypos == fbcon->character.height );
249
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 ) );
255
256         /* Update cursor position */
257         fbcon->ypos--;
258
259         /* Redraw all characters */
260         fbcon_redraw ( fbcon );
261 }
262
263 /**
264  * Draw character at cursor position
265  *
266  * @v fbcon             Frame buffer console
267  * @v show_cursor       Show cursor
268  */
269 static void fbcon_draw_cursor ( struct fbcon *fbcon, int show_cursor ) {
270         struct fbcon_text_cell cell;
271         size_t offset;
272
273         offset = ( ( ( fbcon->ypos * fbcon->character.width ) + fbcon->xpos ) *
274                    sizeof ( cell ) );
275         copy_from_user ( &cell, fbcon->text.start, offset, sizeof ( cell ) );
276         if ( show_cursor ) {
277                 cell.background = fbcon->foreground;
278                 cell.foreground = ( ( fbcon->background == FBCON_TRANSPARENT ) ?
279                                     0 : fbcon->background );
280         }
281         fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
282 }
283
284 /**
285  * Handle ANSI CUP (cursor position)
286  *
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)
291  */
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 );
297
298         fbcon_draw_cursor ( fbcon, 0 );
299         fbcon->xpos = cx;
300         if ( fbcon->xpos >= fbcon->character.width )
301                 fbcon->xpos = 0;
302         fbcon->ypos = cy;
303         if ( fbcon->ypos >= fbcon->character.height )
304                 fbcon->ypos = 0;
305         fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
306 }
307
308 /**
309  * Handle ANSI ED (erase in page)
310  *
311  * @v ctx               ANSI escape sequence context
312  * @v count             Parameter count
313  * @v params[0]         Region to erase
314  */
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 );
319
320         /* We assume that we always clear the whole screen */
321         assert ( params[0] == ANSIESC_ED_ALL );
322
323         /* Clear character array */
324         fbcon_clear ( fbcon, 0 );
325
326         /* Redraw all characters */
327         fbcon_redraw ( fbcon );
328
329         /* Reset cursor position */
330         fbcon->xpos = 0;
331         fbcon->ypos = 0;
332         fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
333 }
334
335 /**
336  * Handle ANSI SGR (set graphics rendition)
337  *
338  * @v ctx               ANSI escape sequence context
339  * @v count             Parameter count
340  * @v params            List of graphic rendition aspects
341  */
342 static void fbcon_handle_sgr ( struct ansiesc_context *ctx, unsigned int count,
343                                int params[] ) {
344         struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
345         uint32_t *custom = NULL;
346         uint32_t rgb;
347         unsigned int end;
348         unsigned int i;
349         int aspect;
350
351         for ( i = 0 ; i < count ; i++ ) {
352
353                 /* Process aspect */
354                 aspect = params[i];
355                 if ( aspect == 0 ) {
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 ) {
361                         fbcon->bold = 0;
362                 } else if ( ( aspect >= 30 ) && ( aspect <= 37 ) ) {
363                         fbcon->foreground =
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 ) ) {
370                         fbcon->background =
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 );
376                 }
377
378                 /* Process custom RGB colour, if applicable
379                  *
380                  * We support the xterm-compatible
381                  * "<ESC>[38;2;<red>;<green>;<blue>m" and
382                  * "<ESC>[48;2;<red>;<green>;<blue>m" sequences.
383                  */
384                 if ( custom ) {
385                         rgb = 0;
386                         end = ( i + 5 );
387                         for ( ; ( i < count ) && ( i < end ) ; i++ )
388                                 rgb = ( ( rgb << 8 ) | params[i] );
389                         *custom = fbcon_colour ( fbcon, rgb );
390                         custom = NULL;
391                 }
392         }
393 }
394
395 /**
396  * Handle ANSI DECTCEM set (show cursor)
397  *
398  * @v ctx               ANSI escape sequence context
399  * @v count             Parameter count
400  * @v params            List of graphic rendition aspects
401  */
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 );
406
407         fbcon->show_cursor = 1;
408         fbcon_draw_cursor ( fbcon, 1 );
409 }
410
411 /**
412  * Handle ANSI DECTCEM reset (hide cursor)
413  *
414  * @v ctx               ANSI escape sequence context
415  * @v count             Parameter count
416  * @v params            List of graphic rendition aspects
417  */
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 );
422
423         fbcon->show_cursor = 0;
424         fbcon_draw_cursor ( fbcon, 0 );
425 }
426
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 },
434         { 0, NULL }
435 };
436
437 /**
438  * Print a character to current cursor position
439  *
440  * @v fbcon             Frame buffer console
441  * @v character         Character
442  */
443 void fbcon_putchar ( struct fbcon *fbcon, int character ) {
444         struct fbcon_text_cell cell;
445
446         /* Intercept ANSI escape sequences */
447         character = ansiesc_process ( &fbcon->ctx, character );
448         if ( character < 0 )
449                 return;
450
451         /* Handle control characters */
452         switch ( character ) {
453         case '\r':
454                 fbcon_draw_cursor ( fbcon, 0 );
455                 fbcon->xpos = 0;
456                 break;
457         case '\n':
458                 fbcon_draw_cursor ( fbcon, 0 );
459                 fbcon->xpos = 0;
460                 fbcon->ypos++;
461                 break;
462         case '\b':
463                 fbcon_draw_cursor ( fbcon, 0 );
464                 if ( fbcon->xpos ) {
465                         fbcon->xpos--;
466                 } else if ( fbcon->ypos ) {
467                         fbcon->xpos = ( fbcon->character.width - 1 );
468                         fbcon->ypos--;
469                 }
470                 break;
471         default:
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 );
478
479                 /* Advance cursor */
480                 fbcon->xpos++;
481                 if ( fbcon->xpos >= fbcon->character.width ) {
482                         fbcon->xpos = 0;
483                         fbcon->ypos++;
484                 }
485                 break;
486         }
487
488         /* Scroll screen if necessary */
489         if ( fbcon->ypos >= fbcon->character.height )
490                 fbcon_scroll ( fbcon );
491
492         /* Show cursor */
493         fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
494 }
495
496 /**
497  * Initialise background picture
498  *
499  * @v fbcon             Frame buffer console
500  * @v pixbuf            Background picture
501  * @ret rc              Return status code
502  */
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;
507         size_t len;
508         size_t pixbuf_stride;
509         size_t indent;
510         size_t pixbuf_indent;
511         size_t offset;
512         size_t pixbuf_offset;
513         uint32_t rgb;
514         uint32_t raw;
515         unsigned int x;
516         unsigned int y;
517         unsigned int width;
518         unsigned int height;
519         int xgap;
520         int ygap;
521         int rc;
522
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 );
529                 rc = -ENOMEM;
530                 goto err_umalloc;
531         }
532
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 ) );
550
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,
558                                          sizeof ( rgb ) );
559                         raw = fbcon_colour ( fbcon, rgb );
560                         copy_to_user ( picture->start, offset, &raw,
561                                        pixel->len );
562                         offset += pixel->len;
563                         pixbuf_offset += sizeof ( rgb );
564                 }
565         }
566
567         return 0;
568
569         ufree ( picture->start );
570  err_umalloc:
571         return rc;
572 }
573
574 /**
575  * Initialise frame buffer console
576  *
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
585  */
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 ) {
592         int width;
593         int height;
594         unsigned int xgap;
595         unsigned int ygap;
596         int rc;
597
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 ) );
603         fbcon->map = map;
604         fbcon->font = font;
605         fbcon->ctx.handlers = fbcon_ansiesc_handlers;
606         fbcon->show_cursor = 1;
607
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 ) );
613
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 ) );
622                 rc = -EINVAL;
623                 goto err_margin;
624         }
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 ) );
633
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 ),
644                fbcon->margin.top,
645                ( fbcon->pixel->height - fbcon->margin.bottom ) );
646
647         /* Set default colours */
648         fbcon_set_default_foreground ( fbcon );
649         fbcon_set_default_background ( fbcon );
650
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 ) {
656                 rc = -ENOMEM;
657                 goto err_text;
658         }
659         fbcon_clear ( fbcon, 0 );
660
661         /* Set framebuffer to all black (including margins) */
662         memset_user ( fbcon->start, 0, 0, fbcon->len );
663
664         /* Generate pixel buffer from background image, if applicable */
665         if ( pixbuf && ( ( rc = fbcon_picture_init ( fbcon, pixbuf ) ) != 0 ) )
666                 goto err_picture;
667
668         /* Draw background picture (including margins), if applicable */
669         if ( fbcon->picture.start ) {
670                 memcpy_user ( fbcon->start, 0, fbcon->picture.start, 0,
671                               fbcon->len );
672         }
673
674         /* Update console width and height */
675         console_set_size ( fbcon->character.width, fbcon->character.height );
676
677         return 0;
678
679         ufree ( fbcon->picture.start );
680  err_picture:
681         ufree ( fbcon->text.start );
682  err_text:
683  err_margin:
684         return rc;
685 }
686
687 /**
688  * Finalise frame buffer console
689  *
690  * @v fbcon             Frame buffer console
691  */
692 void fbcon_fini ( struct fbcon *fbcon ) {
693
694         ufree ( fbcon->text.start );
695         ufree ( fbcon->picture.start );
696 }