xref: /openbmc/u-boot/lib/efi_loader/efi_console.c (revision 4e97e257)
1 /*
2  *  EFI application console interface
3  *
4  *  Copyright (c) 2016 Alexander Graf
5  *
6  *  SPDX-License-Identifier:     GPL-2.0+
7  */
8 
9 #include <common.h>
10 #include <charset.h>
11 #include <dm/device.h>
12 #include <efi_loader.h>
13 #include <stdio_dev.h>
14 #include <video_console.h>
15 
16 static bool console_size_queried;
17 
18 #define EFI_COUT_MODE_2 2
19 #define EFI_MAX_COUT_MODE 3
20 
21 struct cout_mode {
22 	unsigned long columns;
23 	unsigned long rows;
24 	int present;
25 };
26 
27 static struct cout_mode efi_cout_modes[] = {
28 	/* EFI Mode 0 is 80x25 and always present */
29 	{
30 		.columns = 80,
31 		.rows = 25,
32 		.present = 1,
33 	},
34 	/* EFI Mode 1 is always 80x50 */
35 	{
36 		.columns = 80,
37 		.rows = 50,
38 		.present = 0,
39 	},
40 	/* Value are unknown until we query the console */
41 	{
42 		.columns = 0,
43 		.rows = 0,
44 		.present = 0,
45 	},
46 };
47 
48 const efi_guid_t efi_guid_console_control = CONSOLE_CONTROL_GUID;
49 
50 #define cESC '\x1b'
51 #define ESC "\x1b"
52 
53 static efi_status_t EFIAPI efi_cin_get_mode(
54 			struct efi_console_control_protocol *this,
55 			int *mode, char *uga_exists, char *std_in_locked)
56 {
57 	EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
58 
59 	if (mode)
60 		*mode = EFI_CONSOLE_MODE_TEXT;
61 	if (uga_exists)
62 		*uga_exists = 0;
63 	if (std_in_locked)
64 		*std_in_locked = 0;
65 
66 	return EFI_EXIT(EFI_SUCCESS);
67 }
68 
69 static efi_status_t EFIAPI efi_cin_set_mode(
70 			struct efi_console_control_protocol *this, int mode)
71 {
72 	EFI_ENTRY("%p, %d", this, mode);
73 	return EFI_EXIT(EFI_UNSUPPORTED);
74 }
75 
76 static efi_status_t EFIAPI efi_cin_lock_std_in(
77 			struct efi_console_control_protocol *this,
78 			uint16_t *password)
79 {
80 	EFI_ENTRY("%p, %p", this, password);
81 	return EFI_EXIT(EFI_UNSUPPORTED);
82 }
83 
84 const struct efi_console_control_protocol efi_console_control = {
85 	.get_mode = efi_cin_get_mode,
86 	.set_mode = efi_cin_set_mode,
87 	.lock_std_in = efi_cin_lock_std_in,
88 };
89 
90 /* Default to mode 0 */
91 static struct simple_text_output_mode efi_con_mode = {
92 	.max_mode = 1,
93 	.mode = 0,
94 	.attribute = 0,
95 	.cursor_column = 0,
96 	.cursor_row = 0,
97 	.cursor_visible = 1,
98 };
99 
100 static int term_read_reply(int *n, int maxnum, char end_char)
101 {
102 	char c;
103 	int i = 0;
104 
105 	c = getc();
106 	if (c != cESC)
107 		return -1;
108 	c = getc();
109 	if (c != '[')
110 		return -1;
111 
112 	n[0] = 0;
113 	while (1) {
114 		c = getc();
115 		if (c == ';') {
116 			i++;
117 			if (i >= maxnum)
118 				return -1;
119 			n[i] = 0;
120 			continue;
121 		} else if (c == end_char) {
122 			break;
123 		} else if (c > '9' || c < '0') {
124 			return -1;
125 		}
126 
127 		/* Read one more decimal position */
128 		n[i] *= 10;
129 		n[i] += c - '0';
130 	}
131 
132 	return 0;
133 }
134 
135 static efi_status_t EFIAPI efi_cout_reset(
136 			struct efi_simple_text_output_protocol *this,
137 			char extended_verification)
138 {
139 	EFI_ENTRY("%p, %d", this, extended_verification);
140 	return EFI_EXIT(EFI_UNSUPPORTED);
141 }
142 
143 static efi_status_t EFIAPI efi_cout_output_string(
144 			struct efi_simple_text_output_protocol *this,
145 			const efi_string_t string)
146 {
147 	struct simple_text_output_mode *con = &efi_con_mode;
148 	struct cout_mode *mode = &efi_cout_modes[con->mode];
149 
150 	EFI_ENTRY("%p, %p", this, string);
151 
152 	unsigned int n16 = utf16_strlen(string);
153 	char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
154 	char *p;
155 
156 	*utf16_to_utf8((u8 *)buf, string, n16) = '\0';
157 
158 	fputs(stdout, buf);
159 
160 	for (p = buf; *p; p++) {
161 		switch (*p) {
162 		case '\r':   /* carriage-return */
163 			con->cursor_column = 0;
164 			break;
165 		case '\n':   /* newline */
166 			con->cursor_column = 0;
167 			con->cursor_row++;
168 			break;
169 		case '\t':   /* tab, assume 8 char align */
170 			break;
171 		case '\b':   /* backspace */
172 			con->cursor_column = max(0, con->cursor_column - 1);
173 			break;
174 		default:
175 			con->cursor_column++;
176 			break;
177 		}
178 		if (con->cursor_column >= mode->columns) {
179 			con->cursor_column = 0;
180 			con->cursor_row++;
181 		}
182 		con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
183 	}
184 
185 	return EFI_EXIT(EFI_SUCCESS);
186 }
187 
188 static efi_status_t EFIAPI efi_cout_test_string(
189 			struct efi_simple_text_output_protocol *this,
190 			const efi_string_t string)
191 {
192 	EFI_ENTRY("%p, %p", this, string);
193 	return EFI_EXIT(EFI_SUCCESS);
194 }
195 
196 static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
197 {
198 	if (!mode->present)
199 		return false;
200 
201 	return (mode->rows == rows) && (mode->columns == cols);
202 }
203 
204 static int query_console_serial(int *rows, int *cols)
205 {
206 	/* Ask the terminal about its size */
207 	int n[3];
208 	u64 timeout;
209 
210 	/* Empty input buffer */
211 	while (tstc())
212 		getc();
213 
214 	printf(ESC"[18t");
215 
216 	/* Check if we have a terminal that understands */
217 	timeout = timer_get_us() + 1000000;
218 	while (!tstc())
219 		if (timer_get_us() > timeout)
220 			return -1;
221 
222 	/* Read {depth,rows,cols} */
223 	if (term_read_reply(n, 3, 't'))
224 		return -1;
225 
226 	*cols = n[2];
227 	*rows = n[1];
228 
229 	return 0;
230 }
231 
232 static efi_status_t EFIAPI efi_cout_query_mode(
233 			struct efi_simple_text_output_protocol *this,
234 			unsigned long mode_number, unsigned long *columns,
235 			unsigned long *rows)
236 {
237 	EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
238 
239 	if (!console_size_queried) {
240 		const char *stdout_name = env_get("stdout");
241 		int rows, cols;
242 
243 		console_size_queried = true;
244 
245 		if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
246 		    IS_ENABLED(CONFIG_DM_VIDEO)) {
247 			struct stdio_dev *stdout_dev =
248 				stdio_get_by_name("vidconsole");
249 			struct udevice *dev = stdout_dev->priv;
250 			struct vidconsole_priv *priv =
251 				dev_get_uclass_priv(dev);
252 			rows = priv->rows;
253 			cols = priv->cols;
254 		} else if (query_console_serial(&rows, &cols)) {
255 			goto out;
256 		}
257 
258 		/* Test if we can have Mode 1 */
259 		if (cols >= 80 && rows >= 50) {
260 			efi_cout_modes[1].present = 1;
261 			efi_con_mode.max_mode = 2;
262 		}
263 
264 		/*
265 		 * Install our mode as mode 2 if it is different
266 		 * than mode 0 or 1 and set it  as the currently selected mode
267 		 */
268 		if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
269 		    !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
270 			efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
271 			efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
272 			efi_cout_modes[EFI_COUT_MODE_2].present = 1;
273 			efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
274 			efi_con_mode.mode = EFI_COUT_MODE_2;
275 		}
276 	}
277 
278 	if (mode_number >= efi_con_mode.max_mode)
279 		return EFI_EXIT(EFI_UNSUPPORTED);
280 
281 	if (efi_cout_modes[mode_number].present != 1)
282 		return EFI_EXIT(EFI_UNSUPPORTED);
283 
284 out:
285 	if (columns)
286 		*columns = efi_cout_modes[mode_number].columns;
287 	if (rows)
288 		*rows = efi_cout_modes[mode_number].rows;
289 
290 	return EFI_EXIT(EFI_SUCCESS);
291 }
292 
293 static efi_status_t EFIAPI efi_cout_set_mode(
294 			struct efi_simple_text_output_protocol *this,
295 			unsigned long mode_number)
296 {
297 	EFI_ENTRY("%p, %ld", this, mode_number);
298 
299 
300 	if (mode_number > efi_con_mode.max_mode)
301 		return EFI_EXIT(EFI_UNSUPPORTED);
302 
303 	efi_con_mode.mode = mode_number;
304 	efi_con_mode.cursor_column = 0;
305 	efi_con_mode.cursor_row = 0;
306 
307 	return EFI_EXIT(EFI_SUCCESS);
308 }
309 
310 static const struct {
311 	unsigned int fg;
312 	unsigned int bg;
313 } color[] = {
314 	{ 30, 40 },     /* 0: black */
315 	{ 34, 44 },     /* 1: blue */
316 	{ 32, 42 },     /* 2: green */
317 	{ 36, 46 },     /* 3: cyan */
318 	{ 31, 41 },     /* 4: red */
319 	{ 35, 45 },     /* 5: magenta */
320 	{ 33, 43 },     /* 6: brown, map to yellow as edk2 does*/
321 	{ 37, 47 },     /* 7: light grey, map to white */
322 };
323 
324 /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
325 static efi_status_t EFIAPI efi_cout_set_attribute(
326 			struct efi_simple_text_output_protocol *this,
327 			unsigned long attribute)
328 {
329 	unsigned int bold = EFI_ATTR_BOLD(attribute);
330 	unsigned int fg = EFI_ATTR_FG(attribute);
331 	unsigned int bg = EFI_ATTR_BG(attribute);
332 
333 	EFI_ENTRY("%p, %lx", this, attribute);
334 
335 	if (attribute)
336 		printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
337 	else
338 		printf(ESC"[0;37;40m");
339 
340 	return EFI_EXIT(EFI_SUCCESS);
341 }
342 
343 static efi_status_t EFIAPI efi_cout_clear_screen(
344 			struct efi_simple_text_output_protocol *this)
345 {
346 	EFI_ENTRY("%p", this);
347 
348 	printf(ESC"[2J");
349 
350 	return EFI_EXIT(EFI_SUCCESS);
351 }
352 
353 static efi_status_t EFIAPI efi_cout_set_cursor_position(
354 			struct efi_simple_text_output_protocol *this,
355 			unsigned long column, unsigned long row)
356 {
357 	EFI_ENTRY("%p, %ld, %ld", this, column, row);
358 
359 	printf(ESC"[%d;%df", (int)row, (int)column);
360 	efi_con_mode.cursor_column = column;
361 	efi_con_mode.cursor_row = row;
362 
363 	return EFI_EXIT(EFI_SUCCESS);
364 }
365 
366 static efi_status_t EFIAPI efi_cout_enable_cursor(
367 			struct efi_simple_text_output_protocol *this,
368 			bool enable)
369 {
370 	EFI_ENTRY("%p, %d", this, enable);
371 
372 	printf(ESC"[?25%c", enable ? 'h' : 'l');
373 
374 	return EFI_EXIT(EFI_SUCCESS);
375 }
376 
377 const struct efi_simple_text_output_protocol efi_con_out = {
378 	.reset = efi_cout_reset,
379 	.output_string = efi_cout_output_string,
380 	.test_string = efi_cout_test_string,
381 	.query_mode = efi_cout_query_mode,
382 	.set_mode = efi_cout_set_mode,
383 	.set_attribute = efi_cout_set_attribute,
384 	.clear_screen = efi_cout_clear_screen,
385 	.set_cursor_position = efi_cout_set_cursor_position,
386 	.enable_cursor = efi_cout_enable_cursor,
387 	.mode = (void*)&efi_con_mode,
388 };
389 
390 static efi_status_t EFIAPI efi_cin_reset(
391 			struct efi_simple_input_interface *this,
392 			bool extended_verification)
393 {
394 	EFI_ENTRY("%p, %d", this, extended_verification);
395 	return EFI_EXIT(EFI_UNSUPPORTED);
396 }
397 
398 static efi_status_t EFIAPI efi_cin_read_key_stroke(
399 			struct efi_simple_input_interface *this,
400 			struct efi_input_key *key)
401 {
402 	struct efi_input_key pressed_key = {
403 		.scan_code = 0,
404 		.unicode_char = 0,
405 	};
406 	char ch;
407 
408 	EFI_ENTRY("%p, %p", this, key);
409 
410 	/* We don't do interrupts, so check for timers cooperatively */
411 	efi_timer_check();
412 
413 	if (!tstc()) {
414 		/* No key pressed */
415 		return EFI_EXIT(EFI_NOT_READY);
416 	}
417 
418 	ch = getc();
419 	if (ch == cESC) {
420 		/* Escape Sequence */
421 		ch = getc();
422 		switch (ch) {
423 		case cESC: /* ESC */
424 			pressed_key.scan_code = 23;
425 			break;
426 		case 'O': /* F1 - F4 */
427 			pressed_key.scan_code = getc() - 'P' + 11;
428 			break;
429 		case 'a'...'z':
430 			ch = ch - 'a';
431 			break;
432 		case '[':
433 			ch = getc();
434 			switch (ch) {
435 			case 'A'...'D': /* up, down right, left */
436 				pressed_key.scan_code = ch - 'A' + 1;
437 				break;
438 			case 'F': /* End */
439 				pressed_key.scan_code = 6;
440 				break;
441 			case 'H': /* Home */
442 				pressed_key.scan_code = 5;
443 				break;
444 			case '1': /* F5 - F8 */
445 				pressed_key.scan_code = getc() - '0' + 11;
446 				getc();
447 				break;
448 			case '2': /* F9 - F12 */
449 				pressed_key.scan_code = getc() - '0' + 19;
450 				getc();
451 				break;
452 			case '3': /* DEL */
453 				pressed_key.scan_code = 8;
454 				getc();
455 				break;
456 			}
457 			break;
458 		}
459 	} else if (ch == 0x7f) {
460 		/* Backspace */
461 		ch = 0x08;
462 	}
463 	pressed_key.unicode_char = ch;
464 	*key = pressed_key;
465 
466 	return EFI_EXIT(EFI_SUCCESS);
467 }
468 
469 struct efi_simple_input_interface efi_con_in = {
470 	.reset = efi_cin_reset,
471 	.read_key_stroke = efi_cin_read_key_stroke,
472 	.wait_for_key = NULL,
473 };
474 
475 static struct efi_event *console_timer_event;
476 
477 static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
478 {
479 }
480 
481 static void EFIAPI efi_console_timer_notify(struct efi_event *event,
482 					    void *context)
483 {
484 	EFI_ENTRY("%p, %p", event, context);
485 	if (tstc()) {
486 		efi_con_in.wait_for_key->is_signaled = true;
487 		efi_signal_event(efi_con_in.wait_for_key);
488 		}
489 	EFI_EXIT(EFI_SUCCESS);
490 }
491 
492 
493 static struct efi_object efi_console_control_obj =
494 	EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
495 static struct efi_object efi_console_output_obj =
496 	EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
497 static struct efi_object efi_console_input_obj =
498 	EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
499 
500 /* This gets called from do_bootefi_exec(). */
501 int efi_console_register(void)
502 {
503 	efi_status_t r;
504 
505 	/* Hook up to the device list */
506 	list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
507 	list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
508 	list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
509 
510 	r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
511 			     efi_key_notify, NULL, &efi_con_in.wait_for_key);
512 	if (r != EFI_SUCCESS) {
513 		printf("ERROR: Failed to register WaitForKey event\n");
514 		return r;
515 	}
516 	r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
517 			     efi_console_timer_notify, NULL,
518 			     &console_timer_event);
519 	if (r != EFI_SUCCESS) {
520 		printf("ERROR: Failed to register console event\n");
521 		return r;
522 	}
523 	/* 5000 ns cycle is sufficient for 2 MBaud */
524 	r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
525 	if (r != EFI_SUCCESS)
526 		printf("ERROR: Failed to set console timer\n");
527 	return r;
528 }
529