Add qemu 2.4.0
[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
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 /** @file
23  *
24  * Frame buffer console
25  *
26  */
27
28 #include <string.h>
29 #include <errno.h>
30 #include <assert.h>
31 #include <byteswap.h>
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>
38
39 /**
40  * Calculate raw colour value
41  *
42  * @v fbcon             Frame buffer console
43  * @v rgb               24-bit RGB value
44  * @ret raw             Raw colour
45  */
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 );
51         uint32_t mapped;
52
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 );
57 }
58
59 /**
60  * Calculate ANSI font colour
61  *
62  * @v fbcon             Frame buffer console
63  * @v ansicol           ANSI colour value (0-based)
64  * @ret colour          Raw colour
65  */
66 static uint32_t fbcon_ansi_colour ( struct fbcon *fbcon,
67                                     unsigned int ansicol ) {
68         uint32_t rgb;
69
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 ) );
74
75         return fbcon_colour ( fbcon, rgb );
76 }
77
78 /**
79  * Set default foreground colour
80  *
81  * @v fbcon             Frame buffer console
82  */
83 static void fbcon_set_default_foreground ( struct fbcon *fbcon ) {
84
85         /* Default to non-bold white foreground */
86         fbcon->foreground = fbcon_ansi_colour ( fbcon, 0x7 );
87         fbcon->bold = 0;
88 }
89
90 /**
91  * Set default background colour
92  *
93  * @v fbcon             Frame buffer console
94  */
95 static void fbcon_set_default_background ( struct fbcon *fbcon ) {
96
97         /* Default to transparent background */
98         fbcon->background = FBCON_TRANSPARENT;
99 }
100
101 /**
102  * Clear rows of characters
103  *
104  * @v fbcon             Frame buffer console
105  * @v ypos              Starting Y position
106  */
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,
111                 .character = ' ',
112         };
113         size_t offset;
114         unsigned int xpos;
115
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,
121                                        sizeof ( cell ) );
122                         offset += sizeof ( cell );
123                 }
124         }
125 }
126
127 /**
128  * Store character at specified position
129  *
130  * @v fbcon             Frame buffer console
131  * @v cell              Text cell
132  * @v xpos              X position
133  * @v ypos              Y position
134  */
135 static void fbcon_store ( struct fbcon *fbcon, struct fbcon_text_cell *cell,
136                           unsigned int xpos, unsigned int ypos ) {
137         size_t offset;
138
139         /* Store cell */
140         offset = ( ( ( ypos * fbcon->character.width ) + xpos ) *
141                    sizeof ( *cell ) );
142         copy_to_user ( fbcon->text.start, offset, cell, sizeof ( *cell ) );
143 }
144
145 /**
146  * Draw character at specified position
147  *
148  * @v fbcon             Frame buffer console
149  * @v cell              Text cell
150  * @v xpos              X position
151  * @v ypos              Y position
152  */
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;
156         size_t offset;
157         size_t pixel_len;
158         size_t skip_len;
159         unsigned int row;
160         unsigned int column;
161         uint8_t bitmask;
162         int transparent;
163         void *src;
164
165         /* Get font character */
166         copy_from_user ( &glyph, fbcon->font->start,
167                          ( cell->character * sizeof ( glyph ) ),
168                          sizeof ( glyph ) );
169
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 );
176
177         /* Check for transparent background colour */
178         transparent = ( cell->background == FBCON_TRANSPARENT );
179
180         /* Draw character rows */
181         for ( row = 0 ; row < FBCON_CHAR_HEIGHT ; row++ ) {
182
183                 /* Draw background picture, if applicable */
184                 if ( transparent ) {
185                         if ( fbcon->picture.start ) {
186                                 memcpy_user ( fbcon->start, offset,
187                                               fbcon->picture.start, offset,
188                                               fbcon->character.len );
189                         } else {
190                                 memset_user ( fbcon->start, offset, 0,
191                                               fbcon->character.len );
192                         }
193                 }
194
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;
202                         } else {
203                                 continue;
204                         }
205                         copy_to_user ( fbcon->start, offset, src, pixel_len );
206                 }
207
208                 /* Move to next row */
209                 offset += skip_len;
210         }
211 }
212
213 /**
214  * Redraw all characters
215  *
216  * @v fbcon             Frame buffer console
217  */
218 static void fbcon_redraw ( struct fbcon *fbcon ) {
219         struct fbcon_text_cell cell;
220         size_t offset = 0;
221         unsigned int xpos;
222         unsigned int ypos;
223
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,
228                                          sizeof ( cell ) );
229                         fbcon_draw ( fbcon, &cell, xpos, ypos );
230                         offset += sizeof ( cell );
231                 }
232         }
233 }
234
235 /**
236  * Scroll screen
237  *
238  * @v fbcon             Frame buffer console
239  */
240 static void fbcon_scroll ( struct fbcon *fbcon ) {
241         size_t row_len;
242
243         /* Sanity check */
244         assert ( fbcon->ypos == fbcon->character.height );
245
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 ) );
251
252         /* Update cursor position */
253         fbcon->ypos--;
254
255         /* Redraw all characters */
256         fbcon_redraw ( fbcon );
257 }
258
259 /**
260  * Draw character at cursor position
261  *
262  * @v fbcon             Frame buffer console
263  * @v show_cursor       Show cursor
264  */
265 static void fbcon_draw_cursor ( struct fbcon *fbcon, int show_cursor ) {
266         struct fbcon_text_cell cell;
267         size_t offset;
268
269         offset = ( ( ( fbcon->ypos * fbcon->character.width ) + fbcon->xpos ) *
270                    sizeof ( cell ) );
271         copy_from_user ( &cell, fbcon->text.start, offset, sizeof ( cell ) );
272         if ( show_cursor ) {
273                 cell.background = fbcon->foreground;
274                 cell.foreground = ( ( fbcon->background == FBCON_TRANSPARENT ) ?
275                                     0 : fbcon->background );
276         }
277         fbcon_draw ( fbcon, &cell, fbcon->xpos, fbcon->ypos );
278 }
279
280 /**
281  * Handle ANSI CUP (cursor position)
282  *
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)
287  */
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 );
293
294         fbcon_draw_cursor ( fbcon, 0 );
295         fbcon->xpos = cx;
296         if ( fbcon->xpos >= fbcon->character.width )
297                 fbcon->xpos = 0;
298         fbcon->ypos = cy;
299         if ( fbcon->ypos >= fbcon->character.height )
300                 fbcon->ypos = 0;
301         fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
302 }
303
304 /**
305  * Handle ANSI ED (erase in page)
306  *
307  * @v ctx               ANSI escape sequence context
308  * @v count             Parameter count
309  * @v params[0]         Region to erase
310  */
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 );
315
316         /* We assume that we always clear the whole screen */
317         assert ( params[0] == ANSIESC_ED_ALL );
318
319         /* Clear character array */
320         fbcon_clear ( fbcon, 0 );
321
322         /* Redraw all characters */
323         fbcon_redraw ( fbcon );
324
325         /* Reset cursor position */
326         fbcon->xpos = 0;
327         fbcon->ypos = 0;
328         fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
329 }
330
331 /**
332  * Handle ANSI SGR (set graphics rendition)
333  *
334  * @v ctx               ANSI escape sequence context
335  * @v count             Parameter count
336  * @v params            List of graphic rendition aspects
337  */
338 static void fbcon_handle_sgr ( struct ansiesc_context *ctx, unsigned int count,
339                                int params[] ) {
340         struct fbcon *fbcon = container_of ( ctx, struct fbcon, ctx );
341         uint32_t *custom = NULL;
342         uint32_t rgb;
343         unsigned int end;
344         unsigned int i;
345         int aspect;
346
347         for ( i = 0 ; i < count ; i++ ) {
348
349                 /* Process aspect */
350                 aspect = params[i];
351                 if ( aspect == 0 ) {
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 ) {
357                         fbcon->bold = 0;
358                 } else if ( ( aspect >= 30 ) && ( aspect <= 37 ) ) {
359                         fbcon->foreground =
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 ) ) {
366                         fbcon->background =
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 );
372                 }
373
374                 /* Process custom RGB colour, if applicable
375                  *
376                  * We support the xterm-compatible
377                  * "<ESC>[38;2;<red>;<green>;<blue>m" and
378                  * "<ESC>[48;2;<red>;<green>;<blue>m" sequences.
379                  */
380                 if ( custom ) {
381                         rgb = 0;
382                         end = ( i + 5 );
383                         for ( ; ( i < count ) && ( i < end ) ; i++ )
384                                 rgb = ( ( rgb << 8 ) | params[i] );
385                         *custom = fbcon_colour ( fbcon, rgb );
386                         custom = NULL;
387                 }
388         }
389 }
390
391 /**
392  * Handle ANSI DECTCEM set (show cursor)
393  *
394  * @v ctx               ANSI escape sequence context
395  * @v count             Parameter count
396  * @v params            List of graphic rendition aspects
397  */
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 );
402
403         fbcon->show_cursor = 1;
404         fbcon_draw_cursor ( fbcon, 1 );
405 }
406
407 /**
408  * Handle ANSI DECTCEM reset (hide cursor)
409  *
410  * @v ctx               ANSI escape sequence context
411  * @v count             Parameter count
412  * @v params            List of graphic rendition aspects
413  */
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 );
418
419         fbcon->show_cursor = 0;
420         fbcon_draw_cursor ( fbcon, 0 );
421 }
422
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 },
430         { 0, NULL }
431 };
432
433 /**
434  * Print a character to current cursor position
435  *
436  * @v fbcon             Frame buffer console
437  * @v character         Character
438  */
439 void fbcon_putchar ( struct fbcon *fbcon, int character ) {
440         struct fbcon_text_cell cell;
441
442         /* Intercept ANSI escape sequences */
443         character = ansiesc_process ( &fbcon->ctx, character );
444         if ( character < 0 )
445                 return;
446
447         /* Handle control characters */
448         switch ( character ) {
449         case '\r':
450                 fbcon_draw_cursor ( fbcon, 0 );
451                 fbcon->xpos = 0;
452                 break;
453         case '\n':
454                 fbcon_draw_cursor ( fbcon, 0 );
455                 fbcon->xpos = 0;
456                 fbcon->ypos++;
457                 break;
458         case '\b':
459                 fbcon_draw_cursor ( fbcon, 0 );
460                 if ( fbcon->xpos ) {
461                         fbcon->xpos--;
462                 } else if ( fbcon->ypos ) {
463                         fbcon->xpos = ( fbcon->character.width - 1 );
464                         fbcon->ypos--;
465                 }
466                 break;
467         default:
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 );
474
475                 /* Advance cursor */
476                 fbcon->xpos++;
477                 if ( fbcon->xpos >= fbcon->character.width ) {
478                         fbcon->xpos = 0;
479                         fbcon->ypos++;
480                 }
481                 break;
482         }
483
484         /* Scroll screen if necessary */
485         if ( fbcon->ypos >= fbcon->character.height )
486                 fbcon_scroll ( fbcon );
487
488         /* Show cursor */
489         fbcon_draw_cursor ( fbcon, fbcon->show_cursor );
490 }
491
492 /**
493  * Initialise background picture
494  *
495  * @v fbcon             Frame buffer console
496  * @v pixbuf            Background picture
497  * @ret rc              Return status code
498  */
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;
503         size_t len;
504         size_t pixbuf_stride;
505         size_t indent;
506         size_t pixbuf_indent;
507         size_t offset;
508         size_t pixbuf_offset;
509         uint32_t rgb;
510         uint32_t raw;
511         unsigned int x;
512         unsigned int y;
513         unsigned int width;
514         unsigned int height;
515         int xgap;
516         int ygap;
517         int rc;
518
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 );
525                 rc = -ENOMEM;
526                 goto err_umalloc;
527         }
528
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 ) );
546
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,
554                                          sizeof ( rgb ) );
555                         raw = fbcon_colour ( fbcon, rgb );
556                         copy_to_user ( picture->start, offset, &raw,
557                                        pixel->len );
558                         offset += pixel->len;
559                         pixbuf_offset += sizeof ( rgb );
560                 }
561         }
562
563         return 0;
564
565         ufree ( picture->start );
566  err_umalloc:
567         return rc;
568 }
569
570 /**
571  * Initialise frame buffer console
572  *
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
581  */
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 ) {
588         int width;
589         int height;
590         unsigned int xgap;
591         unsigned int ygap;
592         int rc;
593
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 ) );
599         fbcon->map = map;
600         fbcon->font = font;
601         fbcon->ctx.handlers = fbcon_ansiesc_handlers;
602         fbcon->show_cursor = 1;
603
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 ) );
609
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 ) );
618                 rc = -EINVAL;
619                 goto err_margin;
620         }
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 ) );
629
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 ),
640                fbcon->margin.top,
641                ( fbcon->pixel->height - fbcon->margin.bottom ) );
642
643         /* Set default colours */
644         fbcon_set_default_foreground ( fbcon );
645         fbcon_set_default_background ( fbcon );
646
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 ) {
652                 rc = -ENOMEM;
653                 goto err_text;
654         }
655         fbcon_clear ( fbcon, 0 );
656
657         /* Set framebuffer to all black (including margins) */
658         memset_user ( fbcon->start, 0, 0, fbcon->len );
659
660         /* Generate pixel buffer from background image, if applicable */
661         if ( pixbuf && ( ( rc = fbcon_picture_init ( fbcon, pixbuf ) ) != 0 ) )
662                 goto err_picture;
663
664         /* Draw background picture (including margins), if applicable */
665         if ( fbcon->picture.start ) {
666                 memcpy_user ( fbcon->start, 0, fbcon->picture.start, 0,
667                               fbcon->len );
668         }
669
670         /* Update console width and height */
671         console_set_size ( fbcon->character.width, fbcon->character.height );
672
673         return 0;
674
675         ufree ( fbcon->picture.start );
676  err_picture:
677         ufree ( fbcon->text.start );
678  err_text:
679  err_margin:
680         return rc;
681 }
682
683 /**
684  * Finalise frame buffer console
685  *
686  * @v fbcon             Frame buffer console
687  */
688 void fbcon_fini ( struct fbcon *fbcon ) {
689
690         ufree ( fbcon->text.start );
691         ufree ( fbcon->picture.start );
692 }