summaryrefslogtreecommitdiff
path: root/payloads/libpayload/drivers/video/corebootfb.c
blob: b5ad1a511d1298986798e73f8576c32a8189026a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * 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 "font.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 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)] = font_glyph_filled(ch, x, y) ?  fg : bg;
				break;
			case 16: /* 16 bpp */
				dst16 = (u16 *)(dst + (font_width - x) * (FI->bits_per_pixel >> 3));
				*dst16 = font_glyph_filled(ch, x, y) ? fgval : bgval;
				break;
			case 24: /* 24 bpp */
				if (font_glyph_filled(ch, x, y)) {
					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 = font_glyph_filled(ch, x, y) ? fgval : bgval;
				break;
			}
		}

		dst += FI->bytes_per_line;
	}
}

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 < 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;

	/* We might have been called before relocation (like FILO does). So
	   just keep the physical address which won't break on relocation. */
	fbinfo = virt_to_phys(lib_sysinfo.framebuffer);

	fbaddr = FI->physical_address;
	if (fbaddr == 0)
		return -1;

	font_init(FI->x_resolution);

	coreboot_video_console.columns = FI->x_resolution / font_width;
	coreboot_video_console.rows = FI->y_resolution / font_height;

	/* See setting of fbinfo above. */
	chars = virt_to_phys(malloc(coreboot_video_console.rows *
				    coreboot_video_console.columns * 2));

	// clear boot splash screen if there is one.
	corebootfb_clear();
	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
};