efi_console.c 14 KB

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