Add coreboot framebuffer support to libpayload

Signed-off-by: Stefan Reinauer <stepan@coresystems.de>


--- include/sysinfo.h	(revision 5280)
+++ include/sysinfo.h	(working copy)
@@ -50,6 +50,8 @@
 	u32 cmos_range_end;
 	u32 cmos_checksum_location;
 
+	struct cb_framebuffer *framebuffer;
+
 	unsigned long *mbtable; /** Pointer to the multiboot table */
 };
 
--- include/i386/arch/endian.h	(revision 5280)
+++ include/i386/arch/endian.h	(working copy)
@@ -38,4 +38,9 @@
 		(( (in) & 0xFF0000) >> 8) | (( (in) & 0xFF000000) >> 24))
 
 #define ntohll(in) (((u64) ntohl( (in) & 0xFFFFFFFF) << 32) | ((u64) ntohl( (in) >> 32))) 
+
+#define htonw(in) ntohw(in)
+#define htonl(in) ntohl(in)
+#define htonll(in) ntohll(in)
+
 #endif
--- include/coreboot_tables.h	(revision 5280)
+++ include/coreboot_tables.h	(working copy)
@@ -136,6 +136,26 @@
 	u64 forward;
 };
 
+#define CB_TAG_FRAMEBUFFER      0x0012
+struct cb_framebuffer {
+	u32 tag;
+	u32 size;
+
+	u64 physical_address;
+	u32 x_resolution;
+	u32 y_resolution;
+	u32 bytes_per_line;
+	u8 bits_per_pixel;
+        u8 red_mask_pos;
+	u8 red_mask_size;
+	u8 green_mask_pos;
+	u8 green_mask_size;
+	u8 blue_mask_pos;
+	u8 blue_mask_size;
+	u8 reserved_mask_pos;
+	u8 reserved_mask_size;
+};
+
 #define CB_TAG_CMOS_OPTION_TABLE 0x00c8
 struct cb_cmos_option_table {
 	u32 tag;
--- include/video_console.h	(revision 5280)
+++ include/video_console.h	(working copy)
@@ -30,9 +30,6 @@
 #ifndef _VIDEO_CONSOLE_H
 #define _VIDEO_CONSOLE_H
 
-#define VIDEO_ROWS 25
-#define VIDEO_COLS 80
-
 struct video_console {
 	int (*init)(void);
 	void (*putc)(u8, u8, unsigned int);
@@ -42,6 +39,9 @@
 	void (*get_cursor)(unsigned int *, unsigned int *, unsigned int *);
 	void (*set_cursor)(unsigned int, unsigned int);
 	void (*enable_cursor)(int);
+
+	unsigned int rows;
+	unsigned int columns;
 };
 
 #endif
--- Config.in	(revision 5280)
+++ Config.in	(working copy)
@@ -116,6 +153,14 @@
 	depends on VIDEO_CONSOLE
 	default n
 
+config COREBOOT_VIDEO_CONSOLE
+	bool "coreboot video console driver"
+	depends on VIDEO_CONSOLE && !GEODELX_VIDEO_CONSOLE
+	default n
+	help
+	  Say Y here if coreboot switched to a graphics mode and
+	  your payload wants to use it.
+
 config PC_KEYBOARD
 	bool "Allow input from a PC keyboard"
 	default y
--- arch/i386/coreboot.c	(revision 5280)
+++ arch/i386/coreboot.c	(working copy)
@@ -95,6 +95,13 @@
 }
 #endif
 
+#ifdef CONFIG_COREBOOT_VIDEO_CONSOLE
+static void cb_parse_framebuffer(unsigned char *ptr, struct sysinfo_t *info)
+{
+	info->framebuffer = (struct cb_framebuffer *)ptr;
+}
+#endif
+
 static int cb_parse_header(void *addr, int len, struct sysinfo_t *info)
 {
 	struct cb_header *header;
@@ -147,6 +154,13 @@
 			cb_parse_checksum(ptr, info);
 			break;
 #endif
+#ifdef CONFIG_COREBOOT_VIDEO_CONSOLE
+		// FIXME we should warn on serial if coreboot set up a
+		// framebuffer buf the payload does not know about it.
+		case CB_TAG_FRAMEBUFFER:
+			cb_parse_framebuffer(ptr, info);
+			break;
+#endif
 		}
 
 		ptr += rec->size;
--- drivers/Makefile.inc	(revision 5280)
+++ drivers/Makefile.inc	(working copy)
@@ -48,6 +48,10 @@
 TARGETS-$(CONFIG_GEODELX_VIDEO_CONSOLE) += drivers/video/geodelx.o
 TARGETS-$(CONFIG_GEODELX_VIDEO_CONSOLE) += drivers/video/font8x16.o
 
+# coreboot generic framebuffer driver
+TARGETS-$(CONFIG_COREBOOT_VIDEO_CONSOLE) += drivers/video/corebootfb.o
+TARGETS-$(CONFIG_COREBOOT_VIDEO_CONSOLE) += drivers/video/font8x16.o
+
 # USB stack
 TARGETS-$(CONFIG_USB) += drivers/usb/usbinit.o
 TARGETS-$(CONFIG_USB) += drivers/usb/usb.o
--- drivers/video/corebootfb.c	(revision 0)
+++ drivers/video/corebootfb.c	(revision 0)
@@ -0,0 +1,268 @@
+/*
+ * This file is part of the libpayload project.
+ *
+ * Copyright (C) 2008 Advanced Micro Devices, Inc.
+ * Copyright (C) 2010 coresystems GmbH
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <libpayload.h>
+#include <coreboot_tables.h>
+#include <pci.h>
+#include <video_console.h>
+#include <arch/msr.h>
+#include "font8x16.h"
+
+struct video_console coreboot_video_console;
+
+static unsigned int cursor_x = 0, cursor_y = 0, cursor_en = 0;
+
+/* color definitions for the 16 standard colors */
+
+#define VGA_COLOR_DEFAULT 7
+static const u32 vga_colors[] = {
+	// BLUE      | GREEN       | RED
+	(0x00 << 16) | (0x00 << 8) | 0x00,
+	(0xAA << 16) | (0x00 << 8) | 0x00,
+	(0x00 << 16) | (0xAA << 8) | 0x00,
+	(0xAA << 16) | (0x55 << 8) | 0x00,
+	(0x00 << 16) | (0x00 << 8) | 0xAA,
+	(0xAA << 16) | (0x00 << 8) | 0xAA,
+	(0x00 << 16) | (0xAA << 8) | 0xAA,
+	(0xAA << 16) | (0xAA << 8) | 0xAA,
+	(0x55 << 16) | (0x55 << 8) | 0x55,
+	(0xFF << 16) | (0x55 << 8) | 0x55,
+	(0x55 << 16) | (0xFF << 8) | 0x55,
+	(0xFF << 16) | (0xFF << 8) | 0x55,
+	(0x55 << 16) | (0x55 << 8) | 0xFF,
+	(0xFF << 16) | (0x55 << 8) | 0xFF,
+	(0x55 << 16) | (0xFF << 8) | 0xFF,
+	(0xFF << 16) | (0xFF << 8) | 0xFF,
+};
+
+/* Addresses for the various components */
+static unsigned long fbinfo;
+static unsigned long fbaddr;
+static unsigned long chars;
+
+#define FI ((struct cb_framebuffer *) phys_to_virt(fbinfo))
+#define FB ((unsigned char *) phys_to_virt(fbaddr))
+#define CHARS ((unsigned short *) phys_to_virt(chars))
+
+static void corebootfb_scroll_up(void)
+{
+	unsigned char *dst = FB;
+	unsigned char *src = FB + (FI->bytes_per_line * FONT_HEIGHT);
+	int y;
+
+	/* Scroll all lines up */
+	for(y = 0; y < FI->y_resolution - FONT_HEIGHT; y++) {
+		memcpy(dst, src, FI->x_resolution * (FI->bits_per_pixel >> 3));
+
+		dst += FI->bytes_per_line;
+		src += FI->bytes_per_line;
+	}
+
+	/* Erase last line */
+	dst = FB + (FI->y_resolution - FONT_HEIGHT) * FI->bytes_per_line;
+
+	for(; y < FI->y_resolution; y++) {
+		memset(dst, 0, FI->x_resolution * (FI->bits_per_pixel >> 3));
+		dst += FI->bytes_per_line;
+	}
+
+	/* And update the char buffer */
+	dst = (unsigned char *) CHARS;
+	src = (unsigned char *) (CHARS + coreboot_video_console.columns);
+	memcpy(dst, src, coreboot_video_console.columns *
+			(coreboot_video_console.rows - 1) * 2);
+	int column;
+	for (column = 0; column < coreboot_video_console.columns; column++)
+		CHARS[(coreboot_video_console.rows - 1) * coreboot_video_console.columns + column] = (VGA_COLOR_DEFAULT << 8);
+
+	cursor_y--;
+}
+
+static void corebootfb_clear(void)
+{
+	int row, column;
+	unsigned char *ptr = FB;
+
+	/* Clear the screen */
+	for(row = 0; row < FI->y_resolution; row++) {
+		memset(ptr, 0, FI->x_resolution * (FI->bits_per_pixel >> 3));
+		ptr += FI->bytes_per_line;
+	}
+
+	/* And update the char buffer */
+	for(row = 0; row < coreboot_video_console.rows; row++)
+		for (column = 0; column < coreboot_video_console.columns; column++)
+			CHARS[row * coreboot_video_console.columns + column] = (VGA_COLOR_DEFAULT << 8);
+}
+
+static void corebootfb_putchar(u8 row, u8 col, unsigned int ch)
+{
+	unsigned char *dst;
+	unsigned char *glyph = font8x16 + ((ch & 0xFF) * FONT_HEIGHT);
+
+	unsigned char bg = (ch >> 12) & 0xF;
+	unsigned char fg = (ch >> 8) & 0xF;
+	u32 fgval = 0, bgval = 0;
+	u16 *dst16;
+	u32 *dst32;
+
+	int x, y;
+
+	if (FI->bits_per_pixel > 8) {
+		bgval = ((((vga_colors[bg] >> 0) & 0xff) >> (8 - FI->blue_mask_size)) << FI->blue_mask_pos) |
+			((((vga_colors[bg] >> 8) & 0xff) >> (8 - FI->green_mask_size)) << FI->green_mask_pos) |
+			((((vga_colors[bg] >> 16) & 0xff) >> (8 - FI->red_mask_size)) << FI->red_mask_pos);
+		fgval = ((((vga_colors[fg] >> 0) & 0xff) >> (8 - FI->blue_mask_size)) << FI->blue_mask_pos) |
+			((((vga_colors[fg] >> 8) & 0xff) >> (8 - FI->green_mask_size)) << FI->green_mask_pos) |
+			((((vga_colors[fg] >> 16) & 0xff) >> (8 - FI->red_mask_size)) << FI->red_mask_pos);
+	}
+
+
+	dst = FB + ((row * FONT_HEIGHT) * FI->bytes_per_line);
+	dst += (col * FONT_WIDTH * (FI->bits_per_pixel >> 3));
+
+	for(y = 0; y < FONT_HEIGHT; y++) {
+		for(x = FONT_WIDTH - 1; x >= 0; x--) {
+	
+			switch (FI->bits_per_pixel) {
+			case 8: /* Indexed */
+				dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3)] = (*glyph & (1 << x)) ?  fg : bg;
+				break;
+			case 16: /* 16 bpp */
+				dst16 = (u16 *)(dst + (FONT_WIDTH - x) * (FI->bits_per_pixel >> 3));
+				*dst16 = (*glyph & (1 << x)) ? fgval : bgval;
+				break;
+			case 24: /* 24 bpp */
+				if (*glyph & (1 << x)) {
+					dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 0] = fgval & 0xff;
+					dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 1] = (fgval >> 8) & 0xff;
+					dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 2] = (fgval >> 16) & 0xff;
+				} else {
+					dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 0] = bgval & 0xff;
+					dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 1] = (bgval >> 8) & 0xff;
+					dst[(FONT_WIDTH - x) * (FI->bits_per_pixel >> 3) + 2] = (bgval >> 16) & 0xff;
+				}
+				break;
+			case 32: /* 32 bpp */
+				dst32 = (u32 *)(dst + (FONT_WIDTH - x) * (FI->bits_per_pixel >> 3));
+				*dst32 = (*glyph & (1 << x)) ? fgval : bgval;
+				break;
+			}
+		}
+
+		dst += FI->bytes_per_line;
+		glyph++;
+	}
+}
+
+static void corebootfb_putc(u8 row, u8 col, unsigned int ch)
+{
+	CHARS[row * coreboot_video_console.columns + col] = ch;
+	corebootfb_putchar(row, col, ch);
+}
+
+static void corebootfb_update_cursor(void)
+{
+	int ch, paint;
+	if(cursor_en) {
+		ch = CHARS[cursor_y * coreboot_video_console.columns + cursor_x];
+		paint = (ch & 0xff) | ((ch<<4) & 0xf000) | ((ch >> 4) & 0x0f00);
+	} else {
+		paint = CHARS[cursor_y * coreboot_video_console.columns + cursor_x];
+	}
+
+	if (cursor_y >= 0 && cursor_y < coreboot_video_console.rows)
+		corebootfb_putchar(cursor_y, cursor_x, paint);
+}
+
+static void corebootfb_enable_cursor(int state)
+{
+	cursor_en = state;
+	corebootfb_update_cursor();
+}
+
+static void corebootfb_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en)
+{
+	*x = cursor_x;
+	*y = cursor_y;
+	*en = cursor_en;
+}
+
+static void corebootfb_set_cursor(unsigned int x, unsigned int y)
+{
+	int cursor_remember = cursor_en;
+	if (cursor_remember)
+		corebootfb_enable_cursor(0);
+
+	cursor_x = x;
+	cursor_y = y;
+
+	if (cursor_remember)
+		corebootfb_enable_cursor(1);
+}
+
+static int corebootfb_init(void)
+{
+	if (lib_sysinfo.framebuffer == NULL)
+		return -1;
+
+	fbinfo = (unsigned long)lib_sysinfo.framebuffer;
+
+	fbaddr = FI->physical_address;
+
+	coreboot_video_console.columns = FI->x_resolution / FONT_WIDTH;
+	coreboot_video_console.rows = FI->y_resolution / FONT_HEIGHT;
+
+	chars = (unsigned long) malloc(coreboot_video_console.rows *
+			coreboot_video_console.columns * 2);
+
+	// clear boot splash screen if there is one.
+	corebootfb_clear();
+
+	corebootfb_set_cursor(0, 0);
+	corebootfb_enable_cursor(1);
+
+	return 0;
+}
+
+struct video_console coreboot_video_console = {
+	.init = corebootfb_init,
+	.putc = corebootfb_putc,
+	.clear = corebootfb_clear,
+	.scroll_up = corebootfb_scroll_up,
+
+	.get_cursor = corebootfb_get_cursor,
+	.set_cursor = corebootfb_set_cursor,
+	.enable_cursor = corebootfb_enable_cursor,
+
+	.columns = 80,
+	.rows    = 25
+};
+
--- drivers/video/video.c	(revision 5280)
+++ drivers/video/video.c	(working copy)
@@ -35,6 +35,10 @@
 extern struct video_console geodelx_video_console;
 #endif
 
+#ifdef CONFIG_COREBOOT_VIDEO_CONSOLE
+extern struct video_console coreboot_video_console;
+#endif
+
 #ifdef CONFIG_VGA_VIDEO_CONSOLE
 extern struct video_console vga_video_console;
 #endif
@@ -44,6 +48,9 @@
 #ifdef CONFIG_GEODELX_VIDEO_CONSOLE
 	&geodelx_video_console,
 #endif
+#ifdef CONFIG_COREBOOT_VIDEO_CONSOLE
+	&coreboot_video_console,
+#endif
 #ifdef CONFIG_VGA_VIDEO_CONSOLE
 	&vga_video_console,
 #endif
@@ -66,12 +73,12 @@
 	if (cursory < 0)
 		cursory = 0;
 
-	if (cursorx >= VIDEO_COLS) {
+	if (cursorx >= console->columns) {
 		cursorx = 0;
 		cursory++;
 	}
 
-	while(cursory >= VIDEO_ROWS) {
+	while(cursory >= console->rows) {
 		console->scroll_up();
 		cursory--;
 	}
@@ -112,10 +119,12 @@
 void video_console_putchar(unsigned int ch)
 {
 	/* replace black-on-black with light-gray-on-black.
-	   do it here, instead of in libc/console.c */
+	 * do it here, instead of in libc/console.c
+	 */
 	if ((ch & 0xFF00) == 0) {
 		ch |= 0x0700;
 	}
+
 	switch(ch & 0xFF) {
 	case '\r':
 		cursorx = 0;
@@ -129,12 +138,12 @@
 		cursorx--;
 		if (cursorx < 0) {
 			cursory--;
-			cursorx = VIDEO_COLS;
+			cursorx = console->columns;
 		}
 		break;
 
 	case '\t':
-		while(cursorx % 8 && cursorx < VIDEO_COLS) {
+		while(cursorx % 8 && cursorx < console->columns) {
 			if (console)
 				console->putc(cursory, cursorx, (ch & 0xFF00) | ' ');
 
--- drivers/video/geodelx.c	(revision 5280)
+++ drivers/video/geodelx.c	(working copy)
@@ -287,5 +287,12 @@
 	.init = geodelx_init,
 	.putc = geodelx_putc,
 	.clear = geodelx_clear,
-	.scroll_up = geodelx_scroll_up
+	.scroll_up = geodelx_scroll_up,
+
+	/* TODO .get_cursor */
+	/* TODO .set_cursor */
+	/* TODO .enable_cursor */
+
+	.columns = 80,
+	.rows    = 25
 };
--- drivers/video/vga.c	(revision 5280)
+++ drivers/video/vga.c	(working copy)
@@ -30,13 +30,15 @@
 #include <libpayload.h>
 #include <video_console.h>
 
+struct video_console vga_video_console;
+
 #define VGA_COLOR_WHITE 7
 
 #define CRTC_INDEX      0x3d4
 #define CRTC_DATA       0x3d5
 
 #define VIDEO(_r, _c)\
-  ((u16 *) (phys_to_virt(0xB8000) + ((_r) * (VIDEO_COLS * 2)) + ((_c) * 2)))
+  ((u16 *) (phys_to_virt(0xB8000) + ((_r) * (vga_video_console.columns * 2)) + ((_c) * 2)))
 
 static u8 crtc_read(u8 index)
 {
@@ -56,8 +58,8 @@
 	addr = ((unsigned int) crtc_read(0x0E)) << 8;
 	addr += crtc_read(0x0F);
 
-	*x = addr % VIDEO_COLS;
-	*y = addr / VIDEO_COLS;
+	*x = addr % vga_video_console.columns;
+	*y = addr / vga_video_console.columns;
 
 	*en = !(crtc_read(0x0A) & (1 << 5));
 }
@@ -66,7 +68,7 @@
 {
 	unsigned int addr;
 
-	addr = x + (VIDEO_COLS * y);
+	addr = x + (vga_video_console.columns * y);
 	crtc_write(addr >> 8, 0x0E);
 	crtc_write(addr, 0x0F);
 }
@@ -89,7 +91,7 @@
 	int col;
 	u16 *ptr = VIDEO(row, 0);
 
-	for(col = 0; col < VIDEO_COLS; col++)
+	for(col = 0; col < vga_video_console.columns; col++)
 		ptr[col] = ((attr & 0xFF) << 8) | (ch & 0xFF);
 }
 
@@ -99,16 +101,16 @@
 	u16 *dst = VIDEO(0,0);
 	int i;
 
-	for(i = 0; i < (VIDEO_ROWS - 1) * VIDEO_COLS; i++)
+	for(i = 0; i < (vga_video_console.rows - 1) * vga_video_console.columns; i++)
 		*dst++ = *src++;
 
-	vga_clear_line(VIDEO_ROWS - 1, ' ', VGA_COLOR_WHITE);
+	vga_clear_line(vga_video_console.rows - 1, ' ', VGA_COLOR_WHITE);
 }
 
 static void vga_fill(u8 ch, u8 attr)
 {
 	u8 row;
-	for(row = 0; row < VIDEO_ROWS; row++)
+	for(row = 0; row < vga_video_console.rows; row++)
 		vga_clear_line(row, ch, attr);
 }
 
@@ -151,4 +153,7 @@
 	.get_cursor = vga_get_cursor,
 	.set_cursor = vga_set_cursor,
 	.enable_cursor = vga_enable_cursor,
+
+	.columns = 80,
+	.rows    = 25
 };
