blob: 6502081706e5ef5aab1a76a49b1148830cd587dd [file] [log] [blame]
Alexander Graf4aacacc2016-03-04 01:10:00 +01001/*
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>
Rob Clark0d138cf2017-09-09 06:47:40 -040010#include <charset.h>
Alexander Graf4aacacc2016-03-04 01:10:00 +010011#include <efi_loader.h>
12
Alexander Graf4aacacc2016-03-04 01:10:00 +010013static bool console_size_queried;
14
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010015#define EFI_COUT_MODE_2 2
16#define EFI_MAX_COUT_MODE 3
17
18struct cout_mode {
19 unsigned long columns;
20 unsigned long rows;
21 int present;
22};
23
24static 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
Alexander Graf4aacacc2016-03-04 01:10:00 +010045const efi_guid_t efi_guid_console_control = CONSOLE_CONTROL_GUID;
46
47#define cESC '\x1b'
48#define ESC "\x1b"
49
50static 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
66static 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
73static 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
81const 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
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010087/* Default to mode 0 */
Alexander Graf4aacacc2016-03-04 01:10:00 +010088static struct simple_text_output_mode efi_con_mode = {
Emmanuel Vadota074d5d2016-11-08 06:03:29 +010089 .max_mode = 1,
Alexander Graf4aacacc2016-03-04 01:10:00 +010090 .mode = 0,
91 .attribute = 0,
92 .cursor_column = 0,
93 .cursor_row = 0,
94 .cursor_visible = 1,
95};
96
97static 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
132static 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
140static void print_unicode_in_utf8(u16 c)
141{
Rob Clark0d138cf2017-09-09 06:47:40 -0400142 char utf8[MAX_UTF8_PER_UTF16] = { 0 };
143 utf16_to_utf8((u8 *)utf8, &c, 1);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100144 puts(utf8);
145}
146
147static efi_status_t EFIAPI efi_cout_output_string(
148 struct efi_simple_text_output_protocol *this,
149 const unsigned short *string)
150{
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100151 struct cout_mode *mode;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100152 u16 ch;
153
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100154 mode = &efi_cout_modes[efi_con_mode.mode];
Alexander Graf4aacacc2016-03-04 01:10:00 +0100155 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++;
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100162 } else if (efi_con_mode.cursor_column > mode->columns) {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100163 efi_con_mode.cursor_column = 1;
164 efi_con_mode.cursor_row++;
165 }
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100166 if (efi_con_mode.cursor_row > mode->rows)
167 efi_con_mode.cursor_row = mode->rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100168 }
169
170 return EFI_EXIT(EFI_SUCCESS);
171}
172
173static 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
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100181static 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
Rob Clark1e948922017-09-13 18:05:42 -0400189static int query_console_serial(int *rows, int *cols)
190{
191 /* Ask the terminal about its size */
192 int n[3];
193 u64 timeout;
194
195 /* Empty input buffer */
196 while (tstc())
197 getc();
198
199 printf(ESC"[18t");
200
201 /* Check if we have a terminal that understands */
202 timeout = timer_get_us() + 1000000;
203 while (!tstc())
204 if (timer_get_us() > timeout)
205 return -1;
206
207 /* Read {depth,rows,cols} */
208 if (term_read_reply(n, 3, 't'))
209 return -1;
210
211 *cols = n[2];
212 *rows = n[1];
213
214 return 0;
215}
216
Alexander Graf4aacacc2016-03-04 01:10:00 +0100217static efi_status_t EFIAPI efi_cout_query_mode(
218 struct efi_simple_text_output_protocol *this,
219 unsigned long mode_number, unsigned long *columns,
220 unsigned long *rows)
221{
222 EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
223
224 if (!console_size_queried) {
Rob Clark1e948922017-09-13 18:05:42 -0400225 int rows, cols;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100226
227 console_size_queried = true;
228
Rob Clark1e948922017-09-13 18:05:42 -0400229 if (query_console_serial(&rows, &cols))
Alexander Graf4aacacc2016-03-04 01:10:00 +0100230 goto out;
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100231
232 /* Test if we can have Mode 1 */
233 if (cols >= 80 && rows >= 50) {
234 efi_cout_modes[1].present = 1;
235 efi_con_mode.max_mode = 2;
236 }
237
238 /*
239 * Install our mode as mode 2 if it is different
240 * than mode 0 or 1 and set it as the currently selected mode
241 */
242 if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
243 !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
244 efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
245 efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
246 efi_cout_modes[EFI_COUT_MODE_2].present = 1;
247 efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
248 efi_con_mode.mode = EFI_COUT_MODE_2;
249 }
Alexander Graf4aacacc2016-03-04 01:10:00 +0100250 }
251
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100252 if (mode_number >= efi_con_mode.max_mode)
253 return EFI_EXIT(EFI_UNSUPPORTED);
254
255 if (efi_cout_modes[mode_number].present != 1)
256 return EFI_EXIT(EFI_UNSUPPORTED);
257
Alexander Graf4aacacc2016-03-04 01:10:00 +0100258out:
259 if (columns)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100260 *columns = efi_cout_modes[mode_number].columns;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100261 if (rows)
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100262 *rows = efi_cout_modes[mode_number].rows;
Alexander Graf4aacacc2016-03-04 01:10:00 +0100263
264 return EFI_EXIT(EFI_SUCCESS);
265}
266
267static efi_status_t EFIAPI efi_cout_set_mode(
268 struct efi_simple_text_output_protocol *this,
269 unsigned long mode_number)
270{
271 EFI_ENTRY("%p, %ld", this, mode_number);
272
Alexander Graf4aacacc2016-03-04 01:10:00 +0100273
Emmanuel Vadota074d5d2016-11-08 06:03:29 +0100274 if (mode_number > efi_con_mode.max_mode)
275 return EFI_EXIT(EFI_UNSUPPORTED);
276
277 efi_con_mode.mode = mode_number;
278 efi_con_mode.cursor_column = 0;
279 efi_con_mode.cursor_row = 0;
280
281 return EFI_EXIT(EFI_SUCCESS);
Alexander Graf4aacacc2016-03-04 01:10:00 +0100282}
283
284static efi_status_t EFIAPI efi_cout_set_attribute(
285 struct efi_simple_text_output_protocol *this,
286 unsigned long attribute)
287{
288 EFI_ENTRY("%p, %lx", this, attribute);
289
290 /* Just ignore attributes (colors) for now */
291 return EFI_EXIT(EFI_UNSUPPORTED);
292}
293
294static efi_status_t EFIAPI efi_cout_clear_screen(
295 struct efi_simple_text_output_protocol *this)
296{
297 EFI_ENTRY("%p", this);
298
299 printf(ESC"[2J");
300
301 return EFI_EXIT(EFI_SUCCESS);
302}
303
304static efi_status_t EFIAPI efi_cout_set_cursor_position(
305 struct efi_simple_text_output_protocol *this,
306 unsigned long column, unsigned long row)
307{
308 EFI_ENTRY("%p, %ld, %ld", this, column, row);
309
310 printf(ESC"[%d;%df", (int)row, (int)column);
311 efi_con_mode.cursor_column = column;
312 efi_con_mode.cursor_row = row;
313
314 return EFI_EXIT(EFI_SUCCESS);
315}
316
317static efi_status_t EFIAPI efi_cout_enable_cursor(
318 struct efi_simple_text_output_protocol *this,
319 bool enable)
320{
321 EFI_ENTRY("%p, %d", this, enable);
322
323 printf(ESC"[?25%c", enable ? 'h' : 'l');
324
325 return EFI_EXIT(EFI_SUCCESS);
326}
327
328const struct efi_simple_text_output_protocol efi_con_out = {
329 .reset = efi_cout_reset,
330 .output_string = efi_cout_output_string,
331 .test_string = efi_cout_test_string,
332 .query_mode = efi_cout_query_mode,
333 .set_mode = efi_cout_set_mode,
334 .set_attribute = efi_cout_set_attribute,
335 .clear_screen = efi_cout_clear_screen,
336 .set_cursor_position = efi_cout_set_cursor_position,
337 .enable_cursor = efi_cout_enable_cursor,
338 .mode = (void*)&efi_con_mode,
339};
340
341static efi_status_t EFIAPI efi_cin_reset(
342 struct efi_simple_input_interface *this,
343 bool extended_verification)
344{
345 EFI_ENTRY("%p, %d", this, extended_verification);
346 return EFI_EXIT(EFI_UNSUPPORTED);
347}
348
349static efi_status_t EFIAPI efi_cin_read_key_stroke(
350 struct efi_simple_input_interface *this,
351 struct efi_input_key *key)
352{
353 struct efi_input_key pressed_key = {
354 .scan_code = 0,
355 .unicode_char = 0,
356 };
357 char ch;
358
359 EFI_ENTRY("%p, %p", this, key);
360
361 /* We don't do interrupts, so check for timers cooperatively */
362 efi_timer_check();
363
364 if (!tstc()) {
365 /* No key pressed */
366 return EFI_EXIT(EFI_NOT_READY);
367 }
368
369 ch = getc();
370 if (ch == cESC) {
371 /* Escape Sequence */
372 ch = getc();
373 switch (ch) {
374 case cESC: /* ESC */
375 pressed_key.scan_code = 23;
376 break;
377 case 'O': /* F1 - F4 */
378 pressed_key.scan_code = getc() - 'P' + 11;
379 break;
380 case 'a'...'z':
381 ch = ch - 'a';
382 break;
383 case '[':
384 ch = getc();
385 switch (ch) {
386 case 'A'...'D': /* up, down right, left */
387 pressed_key.scan_code = ch - 'A' + 1;
388 break;
389 case 'F': /* End */
390 pressed_key.scan_code = 6;
391 break;
392 case 'H': /* Home */
393 pressed_key.scan_code = 5;
394 break;
395 case '1': /* F5 - F8 */
396 pressed_key.scan_code = getc() - '0' + 11;
397 getc();
398 break;
399 case '2': /* F9 - F12 */
400 pressed_key.scan_code = getc() - '0' + 19;
401 getc();
402 break;
403 case '3': /* DEL */
404 pressed_key.scan_code = 8;
405 getc();
406 break;
407 }
408 break;
409 }
410 } else if (ch == 0x7f) {
411 /* Backspace */
412 ch = 0x08;
413 }
414 pressed_key.unicode_char = ch;
415 *key = pressed_key;
416
417 return EFI_EXIT(EFI_SUCCESS);
418}
419
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200420struct efi_simple_input_interface efi_con_in = {
Alexander Graf4aacacc2016-03-04 01:10:00 +0100421 .reset = efi_cin_reset,
422 .read_key_stroke = efi_cin_read_key_stroke,
423 .wait_for_key = NULL,
424};
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200425
426static struct efi_event *console_timer_event;
427
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +0200428static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200429{
430}
431
xypron.glpk@gmx.de0ce42dc2017-07-20 05:26:07 +0200432static void EFIAPI efi_console_timer_notify(struct efi_event *event,
433 void *context)
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200434{
435 EFI_ENTRY("%p, %p", event, context);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200436 if (tstc()) {
437 efi_con_in.wait_for_key->signaled = 1;
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200438 efi_signal_event(efi_con_in.wait_for_key);
Heinrich Schuchardt8b11a8a2017-09-15 10:06:13 +0200439 }
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200440 EFI_EXIT(EFI_SUCCESS);
441}
442
Rob Clark49f7b4b2017-07-24 10:39:01 -0400443
444static struct efi_object efi_console_control_obj =
445 EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
446static struct efi_object efi_console_output_obj =
447 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
448static struct efi_object efi_console_input_obj =
449 EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
450
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200451/* This gets called from do_bootefi_exec(). */
452int efi_console_register(void)
453{
454 efi_status_t r;
Rob Clark49f7b4b2017-07-24 10:39:01 -0400455
456 /* Hook up to the device list */
457 list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
458 list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
459 list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
460
xypron.glpk@gmx.de54c7a8e2017-07-18 20:17:22 +0200461 r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
462 efi_key_notify, NULL, &efi_con_in.wait_for_key);
463 if (r != EFI_SUCCESS) {
464 printf("ERROR: Failed to register WaitForKey event\n");
465 return r;
466 }
467 r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
468 efi_console_timer_notify, NULL,
469 &console_timer_event);
470 if (r != EFI_SUCCESS) {
471 printf("ERROR: Failed to register console event\n");
472 return r;
473 }
474 /* 5000 ns cycle is sufficient for 2 MBaud */
475 r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
476 if (r != EFI_SUCCESS)
477 printf("ERROR: Failed to set console timer\n");
478 return r;
479}