efi_console.c 14 KB

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