efi_console.c 13 KB

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