efi_console.c 13 KB

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