efi_console.c 13 KB

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