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