efi_console.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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, cols;
  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. return EFI_EXIT(EFI_SUCCESS);
  288. }
  289. static efi_status_t EFIAPI efi_cout_set_cursor_position(
  290. struct efi_simple_text_output_protocol *this,
  291. unsigned long column, unsigned long row)
  292. {
  293. EFI_ENTRY("%p, %ld, %ld", this, column, row);
  294. printf(ESC"[%d;%df", (int)row, (int)column);
  295. efi_con_mode.cursor_column = column;
  296. efi_con_mode.cursor_row = row;
  297. return EFI_EXIT(EFI_SUCCESS);
  298. }
  299. static efi_status_t EFIAPI efi_cout_enable_cursor(
  300. struct efi_simple_text_output_protocol *this,
  301. bool enable)
  302. {
  303. EFI_ENTRY("%p, %d", this, enable);
  304. printf(ESC"[?25%c", enable ? 'h' : 'l');
  305. return EFI_EXIT(EFI_SUCCESS);
  306. }
  307. struct efi_simple_text_output_protocol efi_con_out = {
  308. .reset = efi_cout_reset,
  309. .output_string = efi_cout_output_string,
  310. .test_string = efi_cout_test_string,
  311. .query_mode = efi_cout_query_mode,
  312. .set_mode = efi_cout_set_mode,
  313. .set_attribute = efi_cout_set_attribute,
  314. .clear_screen = efi_cout_clear_screen,
  315. .set_cursor_position = efi_cout_set_cursor_position,
  316. .enable_cursor = efi_cout_enable_cursor,
  317. .mode = (void*)&efi_con_mode,
  318. };
  319. static efi_status_t EFIAPI efi_cin_reset(
  320. struct efi_simple_input_interface *this,
  321. bool extended_verification)
  322. {
  323. EFI_ENTRY("%p, %d", this, extended_verification);
  324. return EFI_EXIT(EFI_UNSUPPORTED);
  325. }
  326. /*
  327. * Analyze modifiers (shift, alt, ctrl) for function keys.
  328. * This gets called when we have already parsed CSI.
  329. *
  330. * @modifiers: bitmask (shift, alt, ctrl)
  331. * @return: the unmodified code
  332. */
  333. static char skip_modifiers(int *modifiers)
  334. {
  335. char c, mod = 0, ret = 0;
  336. c = getc();
  337. if (c != ';') {
  338. ret = c;
  339. if (c == '~')
  340. goto out;
  341. c = getc();
  342. }
  343. for (;;) {
  344. switch (c) {
  345. case '0'...'9':
  346. mod *= 10;
  347. mod += c - '0';
  348. /* fall through */
  349. case ';':
  350. c = getc();
  351. break;
  352. default:
  353. goto out;
  354. }
  355. }
  356. out:
  357. if (mod)
  358. --mod;
  359. if (modifiers)
  360. *modifiers = mod;
  361. if (!ret)
  362. ret = c;
  363. return ret;
  364. }
  365. static efi_status_t EFIAPI efi_cin_read_key_stroke(
  366. struct efi_simple_input_interface *this,
  367. struct efi_input_key *key)
  368. {
  369. struct efi_input_key pressed_key = {
  370. .scan_code = 0,
  371. .unicode_char = 0,
  372. };
  373. char ch;
  374. EFI_ENTRY("%p, %p", this, key);
  375. /* We don't do interrupts, so check for timers cooperatively */
  376. efi_timer_check();
  377. if (!tstc()) {
  378. /* No key pressed */
  379. return EFI_EXIT(EFI_NOT_READY);
  380. }
  381. ch = getc();
  382. if (ch == cESC) {
  383. /*
  384. * Xterm Control Sequences
  385. * https://www.xfree86.org/4.8.0/ctlseqs.html
  386. */
  387. ch = getc();
  388. switch (ch) {
  389. case cESC: /* ESC */
  390. pressed_key.scan_code = 23;
  391. break;
  392. case 'O': /* F1 - F4 */
  393. ch = getc();
  394. /* skip modifiers */
  395. if (ch <= '9')
  396. ch = getc();
  397. pressed_key.scan_code = ch - 'P' + 11;
  398. break;
  399. case 'a'...'z':
  400. ch = ch - 'a';
  401. break;
  402. case '[':
  403. ch = getc();
  404. switch (ch) {
  405. case 'A'...'D': /* up, down right, left */
  406. pressed_key.scan_code = ch - 'A' + 1;
  407. break;
  408. case 'F': /* End */
  409. pressed_key.scan_code = 6;
  410. break;
  411. case 'H': /* Home */
  412. pressed_key.scan_code = 5;
  413. break;
  414. case '1':
  415. ch = skip_modifiers(NULL);
  416. switch (ch) {
  417. case '1'...'5': /* F1 - F5 */
  418. pressed_key.scan_code = ch - '1' + 11;
  419. break;
  420. case '7'...'9': /* F6 - F8 */
  421. pressed_key.scan_code = ch - '7' + 16;
  422. break;
  423. case 'A'...'D': /* up, down right, left */
  424. pressed_key.scan_code = ch - 'A' + 1;
  425. break;
  426. case 'F':
  427. pressed_key.scan_code = 6; /* End */
  428. break;
  429. case 'H':
  430. pressed_key.scan_code = 5; /* Home */
  431. break;
  432. }
  433. break;
  434. case '2':
  435. ch = skip_modifiers(NULL);
  436. switch (ch) {
  437. case '0'...'1': /* F9 - F10 */
  438. pressed_key.scan_code = ch - '0' + 19;
  439. break;
  440. case '3'...'4': /* F11 - F12 */
  441. pressed_key.scan_code = ch - '3' + 21;
  442. break;
  443. case '~': /* INS */
  444. pressed_key.scan_code = 7;
  445. break;
  446. }
  447. break;
  448. case '3': /* DEL */
  449. pressed_key.scan_code = 8;
  450. skip_modifiers(NULL);
  451. break;
  452. case '5': /* PG UP */
  453. pressed_key.scan_code = 9;
  454. skip_modifiers(NULL);
  455. break;
  456. case '6': /* PG DOWN */
  457. pressed_key.scan_code = 10;
  458. skip_modifiers(NULL);
  459. break;
  460. }
  461. break;
  462. }
  463. } else if (ch == 0x7f) {
  464. /* Backspace */
  465. ch = 0x08;
  466. }
  467. if (!pressed_key.scan_code)
  468. pressed_key.unicode_char = ch;
  469. *key = pressed_key;
  470. return EFI_EXIT(EFI_SUCCESS);
  471. }
  472. struct efi_simple_input_interface efi_con_in = {
  473. .reset = efi_cin_reset,
  474. .read_key_stroke = efi_cin_read_key_stroke,
  475. .wait_for_key = NULL,
  476. };
  477. static struct efi_event *console_timer_event;
  478. static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
  479. {
  480. }
  481. /*
  482. * Notification function of the console timer event.
  483. *
  484. * event: console timer event
  485. * context: not used
  486. */
  487. static void EFIAPI efi_console_timer_notify(struct efi_event *event,
  488. void *context)
  489. {
  490. EFI_ENTRY("%p, %p", event, context);
  491. /* Check if input is available */
  492. if (tstc()) {
  493. /* Queue the wait for key event */
  494. efi_con_in.wait_for_key->is_signaled = true;
  495. efi_signal_event(efi_con_in.wait_for_key, true);
  496. }
  497. EFI_EXIT(EFI_SUCCESS);
  498. }
  499. /* This gets called from do_bootefi_exec(). */
  500. int efi_console_register(void)
  501. {
  502. efi_status_t r;
  503. struct efi_object *efi_console_output_obj;
  504. struct efi_object *efi_console_input_obj;
  505. /* Set up mode information */
  506. query_console_size();
  507. /* Create handles */
  508. r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
  509. if (r != EFI_SUCCESS)
  510. goto out_of_memory;
  511. r = efi_add_protocol(efi_console_output_obj->handle,
  512. &efi_guid_text_output_protocol, &efi_con_out);
  513. if (r != EFI_SUCCESS)
  514. goto out_of_memory;
  515. r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
  516. if (r != EFI_SUCCESS)
  517. goto out_of_memory;
  518. r = efi_add_protocol(efi_console_input_obj->handle,
  519. &efi_guid_text_input_protocol, &efi_con_in);
  520. if (r != EFI_SUCCESS)
  521. goto out_of_memory;
  522. /* Create console events */
  523. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
  524. NULL, NULL, &efi_con_in.wait_for_key);
  525. if (r != EFI_SUCCESS) {
  526. printf("ERROR: Failed to register WaitForKey event\n");
  527. return r;
  528. }
  529. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  530. efi_console_timer_notify, NULL, NULL,
  531. &console_timer_event);
  532. if (r != EFI_SUCCESS) {
  533. printf("ERROR: Failed to register console event\n");
  534. return r;
  535. }
  536. /* 5000 ns cycle is sufficient for 2 MBaud */
  537. r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
  538. if (r != EFI_SUCCESS)
  539. printf("ERROR: Failed to set console timer\n");
  540. return r;
  541. out_of_memory:
  542. printf("ERROR: Out of meemory\n");
  543. return r;
  544. }