mb/google/brya/var/omnigul: Modify NVMe and UFS Storage support
[coreboot.git] / payloads / libpayload / drivers / video / video.c
blob4cd630751a4c5ef6d9c0be9ff3549805431ef08b
1 /*
3 * Copyright (C) 2008 Advanced Micro Devices, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
29 #include <libpayload-config.h>
30 #include <libpayload.h>
31 #include <video_console.h>
33 #if CONFIG(LP_GEODELX_VIDEO_CONSOLE)
34 extern struct video_console geodelx_video_console;
35 #endif
37 #if CONFIG(LP_COREBOOT_VIDEO_CONSOLE)
38 extern struct video_console coreboot_video_console;
39 #endif
41 #if CONFIG(LP_VGA_VIDEO_CONSOLE)
42 extern struct video_console vga_video_console;
43 #endif
45 static struct video_console *console_list[] =
47 #if CONFIG(LP_GEODELX_VIDEO_CONSOLE)
48 &geodelx_video_console,
49 #endif
50 #if CONFIG(LP_COREBOOT_VIDEO_CONSOLE)
51 &coreboot_video_console,
52 #endif
53 #if CONFIG(LP_VGA_VIDEO_CONSOLE)
54 &vga_video_console,
55 #endif
58 static struct video_console *console;
60 static int cursorx;
61 static int cursory;
63 void video_get_rows_cols(unsigned int *rows, unsigned int *cols)
65 if (console) {
66 *cols = console->columns;
67 *rows = console->rows;
68 } else {
69 *cols = *rows = 0;
73 static void video_console_fixup_cursor(void)
75 if (!console)
76 return;
78 if (cursorx < 0)
79 cursorx = 0;
81 if (cursory < 0)
82 cursory = 0;
84 if (cursorx >= console->columns) {
85 cursorx = 0;
86 cursory++;
89 if (console->rows <= 0)
90 return;
92 while(cursory >= console->rows) {
93 console->scroll_up();
94 cursory--;
97 if (console->set_cursor)
98 console->set_cursor(cursorx, cursory);
101 void video_console_cursor_enable(int state)
103 if (console && console->enable_cursor)
104 console->enable_cursor(state);
107 void video_console_clear(void)
109 if (console)
110 console->clear();
112 cursorx = 0;
113 cursory = 0;
115 if (console && console->set_cursor)
116 console->set_cursor(cursorx, cursory);
119 void video_console_putc(u8 row, u8 col, unsigned int ch)
121 if (console)
122 console->putc(row, col, ch);
125 void video_console_putchar(unsigned int ch)
127 if (!console)
128 return;
130 /* replace black-on-black with light-gray-on-black.
131 * do it here, instead of in libc/console.c
133 if ((ch & 0xFF00) == 0) {
134 ch |= 0x0700;
137 switch(ch & 0xFF) {
138 case '\r':
139 cursorx = 0;
140 break;
142 case '\n':
143 cursorx = 0;
144 cursory++;
145 break;
147 case '\b':
148 cursorx--;
149 if (cursorx < 0) {
150 cursory--;
151 cursorx = console->columns;
153 break;
155 case '\t':
156 while(cursorx % 8 && cursorx < console->columns)
157 console->putc(cursory, cursorx++, (ch & 0xFF00) | ' ');
158 break;
159 default:
160 console->putc(cursory, cursorx++, ch);
161 break;
164 video_console_fixup_cursor();
167 void video_printf(int foreground, int background, enum video_printf_align align,
168 const char *fmt, ...)
170 int i = 0, len;
171 char str[200];
173 va_list ap;
174 va_start(ap, fmt);
175 len = vsnprintf(str, ARRAY_SIZE(str), fmt, ap);
176 va_end(ap);
177 if (len <= 0)
178 return;
180 /* vsnprintf can return len larger than size. when it happens,
181 * only size-1 characters have been actually written. */
182 if (len >= ARRAY_SIZE(str))
183 len = ARRAY_SIZE(str) - 1;
185 if (len > console->columns) {
186 cursorx = 0;
187 } else {
188 switch (align) {
189 case VIDEO_PRINTF_ALIGN_LEFT:
190 cursorx = 0;
191 break;
192 case VIDEO_PRINTF_ALIGN_CENTER:
193 cursorx = (console->columns - len) / 2;
194 break;
195 case VIDEO_PRINTF_ALIGN_RIGHT:
196 cursorx = console->columns - len;
197 break;
198 default:
199 break;
203 foreground &= 0xf;
204 foreground <<= 8;
205 background &= 0xf;
206 background <<= 12;
208 while (str[i])
209 video_console_putchar(str[i++] | foreground | background);
212 void video_console_get_cursor(unsigned int *x, unsigned int *y, unsigned int *en)
214 *x=0;
215 *y=0;
216 *en=0;
218 if (console && console->get_cursor)
219 console->get_cursor(x, y, en);
221 *x = cursorx;
222 *y = cursory;
225 void video_console_set_cursor(unsigned int x, unsigned int y)
227 cursorx = x;
228 cursory = y;
229 video_console_fixup_cursor();
232 static struct console_output_driver cons = {
233 .putchar = video_console_putchar
236 int video_init(void)
238 int i;
239 unsigned int dummy_cursor_enabled;
241 for (i = 0; i < ARRAY_SIZE(console_list); i++) {
242 if (console_list[i]->init())
243 continue;
245 console = console_list[i];
247 if (console->get_cursor)
248 console->get_cursor((unsigned int*)&cursorx,
249 (unsigned int*)&cursory,
250 &dummy_cursor_enabled);
252 if (cursorx) {
253 cursorx = 0;
254 cursory++;
257 video_console_fixup_cursor();
258 return 0;
260 return 1;
263 int video_console_init(void)
265 int ret = video_init();
266 if (ret)
267 return ret;
268 console_add_output_driver(&cons);
269 return 0;