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