efi_console.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  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_input_ex_protocol =
  41. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
  42. const efi_guid_t efi_guid_text_input_protocol =
  43. EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
  44. const efi_guid_t efi_guid_text_output_protocol =
  45. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
  46. #define cESC '\x1b'
  47. #define ESC "\x1b"
  48. /* Default to mode 0 */
  49. static struct simple_text_output_mode efi_con_mode = {
  50. .max_mode = 1,
  51. .mode = 0,
  52. .attribute = 0,
  53. .cursor_column = 0,
  54. .cursor_row = 0,
  55. .cursor_visible = 1,
  56. };
  57. /*
  58. * Receive and parse a reply from the terminal.
  59. *
  60. * @n: array of return values
  61. * @num: number of return values expected
  62. * @end_char: character indicating end of terminal message
  63. * @return: non-zero indicates error
  64. */
  65. static int term_read_reply(int *n, int num, char end_char)
  66. {
  67. char c;
  68. int i = 0;
  69. c = getc();
  70. if (c != cESC)
  71. return -1;
  72. c = getc();
  73. if (c != '[')
  74. return -1;
  75. n[0] = 0;
  76. while (1) {
  77. c = getc();
  78. if (c == ';') {
  79. i++;
  80. if (i >= num)
  81. return -1;
  82. n[i] = 0;
  83. continue;
  84. } else if (c == end_char) {
  85. break;
  86. } else if (c > '9' || c < '0') {
  87. return -1;
  88. }
  89. /* Read one more decimal position */
  90. n[i] *= 10;
  91. n[i] += c - '0';
  92. }
  93. if (i != num - 1)
  94. return -1;
  95. return 0;
  96. }
  97. static efi_status_t EFIAPI efi_cout_output_string(
  98. struct efi_simple_text_output_protocol *this,
  99. const efi_string_t string)
  100. {
  101. struct simple_text_output_mode *con = &efi_con_mode;
  102. struct cout_mode *mode = &efi_cout_modes[con->mode];
  103. char *buf, *pos;
  104. u16 *p;
  105. efi_status_t ret = EFI_SUCCESS;
  106. EFI_ENTRY("%p, %p", this, string);
  107. buf = malloc(utf16_utf8_strlen(string) + 1);
  108. if (!buf) {
  109. ret = EFI_OUT_OF_RESOURCES;
  110. goto out;
  111. }
  112. pos = buf;
  113. utf16_utf8_strcpy(&pos, string);
  114. fputs(stdout, buf);
  115. free(buf);
  116. /*
  117. * Update the cursor position.
  118. *
  119. * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
  120. * and U000D. All other characters, including control characters
  121. * U+0007 (BEL) and U+0009 (TAB), have to increase the column by one.
  122. */
  123. for (p = string; *p; ++p) {
  124. switch (*p) {
  125. case '\b': /* U+0008, backspace */
  126. con->cursor_column = max(0, con->cursor_column - 1);
  127. break;
  128. case '\n': /* U+000A, newline */
  129. con->cursor_column = 0;
  130. con->cursor_row++;
  131. break;
  132. case '\r': /* U+000D, carriage-return */
  133. con->cursor_column = 0;
  134. break;
  135. case 0xd800 ... 0xdbff:
  136. /*
  137. * Ignore high surrogates, we do not want to count a
  138. * Unicode character twice.
  139. */
  140. break;
  141. default:
  142. con->cursor_column++;
  143. break;
  144. }
  145. if (con->cursor_column >= mode->columns) {
  146. con->cursor_column = 0;
  147. con->cursor_row++;
  148. }
  149. con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
  150. }
  151. out:
  152. return EFI_EXIT(ret);
  153. }
  154. static efi_status_t EFIAPI efi_cout_test_string(
  155. struct efi_simple_text_output_protocol *this,
  156. const efi_string_t string)
  157. {
  158. EFI_ENTRY("%p, %p", this, string);
  159. return EFI_EXIT(EFI_SUCCESS);
  160. }
  161. static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
  162. {
  163. if (!mode->present)
  164. return false;
  165. return (mode->rows == rows) && (mode->columns == cols);
  166. }
  167. static int query_console_serial(int *rows, int *cols)
  168. {
  169. /* Ask the terminal about its size */
  170. int n[3];
  171. u64 timeout;
  172. /* Empty input buffer */
  173. while (tstc())
  174. getc();
  175. printf(ESC"[18t");
  176. /* Check if we have a terminal that understands */
  177. timeout = timer_get_us() + 1000000;
  178. while (!tstc())
  179. if (timer_get_us() > timeout)
  180. return -1;
  181. /* Read {depth,rows,cols} */
  182. if (term_read_reply(n, 3, 't'))
  183. return -1;
  184. *cols = n[2];
  185. *rows = n[1];
  186. return 0;
  187. }
  188. /*
  189. * Update the mode table.
  190. *
  191. * By default the only mode available is 80x25. If the console has at least 50
  192. * lines, enable mode 80x50. If we can query the console size and it is neither
  193. * 80x25 nor 80x50, set it as an additional mode.
  194. */
  195. static void query_console_size(void)
  196. {
  197. const char *stdout_name = env_get("stdout");
  198. int rows = 25, cols = 80;
  199. if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
  200. IS_ENABLED(CONFIG_DM_VIDEO)) {
  201. struct stdio_dev *stdout_dev =
  202. stdio_get_by_name("vidconsole");
  203. struct udevice *dev = stdout_dev->priv;
  204. struct vidconsole_priv *priv =
  205. dev_get_uclass_priv(dev);
  206. rows = priv->rows;
  207. cols = priv->cols;
  208. } else if (query_console_serial(&rows, &cols)) {
  209. return;
  210. }
  211. /* Test if we can have Mode 1 */
  212. if (cols >= 80 && rows >= 50) {
  213. efi_cout_modes[1].present = 1;
  214. efi_con_mode.max_mode = 2;
  215. }
  216. /*
  217. * Install our mode as mode 2 if it is different
  218. * than mode 0 or 1 and set it as the currently selected mode
  219. */
  220. if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
  221. !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
  222. efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
  223. efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
  224. efi_cout_modes[EFI_COUT_MODE_2].present = 1;
  225. efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
  226. efi_con_mode.mode = EFI_COUT_MODE_2;
  227. }
  228. }
  229. static efi_status_t EFIAPI efi_cout_query_mode(
  230. struct efi_simple_text_output_protocol *this,
  231. unsigned long mode_number, unsigned long *columns,
  232. unsigned long *rows)
  233. {
  234. EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
  235. if (mode_number >= efi_con_mode.max_mode)
  236. return EFI_EXIT(EFI_UNSUPPORTED);
  237. if (efi_cout_modes[mode_number].present != 1)
  238. return EFI_EXIT(EFI_UNSUPPORTED);
  239. if (columns)
  240. *columns = efi_cout_modes[mode_number].columns;
  241. if (rows)
  242. *rows = efi_cout_modes[mode_number].rows;
  243. return EFI_EXIT(EFI_SUCCESS);
  244. }
  245. static efi_status_t EFIAPI efi_cout_set_mode(
  246. struct efi_simple_text_output_protocol *this,
  247. unsigned long mode_number)
  248. {
  249. EFI_ENTRY("%p, %ld", this, mode_number);
  250. if (mode_number > efi_con_mode.max_mode)
  251. return EFI_EXIT(EFI_UNSUPPORTED);
  252. efi_con_mode.mode = mode_number;
  253. efi_con_mode.cursor_column = 0;
  254. efi_con_mode.cursor_row = 0;
  255. return EFI_EXIT(EFI_SUCCESS);
  256. }
  257. static const struct {
  258. unsigned int fg;
  259. unsigned int bg;
  260. } color[] = {
  261. { 30, 40 }, /* 0: black */
  262. { 34, 44 }, /* 1: blue */
  263. { 32, 42 }, /* 2: green */
  264. { 36, 46 }, /* 3: cyan */
  265. { 31, 41 }, /* 4: red */
  266. { 35, 45 }, /* 5: magenta */
  267. { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
  268. { 37, 47 }, /* 7: light gray, map to white */
  269. };
  270. /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
  271. static efi_status_t EFIAPI efi_cout_set_attribute(
  272. struct efi_simple_text_output_protocol *this,
  273. unsigned long attribute)
  274. {
  275. unsigned int bold = EFI_ATTR_BOLD(attribute);
  276. unsigned int fg = EFI_ATTR_FG(attribute);
  277. unsigned int bg = EFI_ATTR_BG(attribute);
  278. EFI_ENTRY("%p, %lx", this, attribute);
  279. if (attribute)
  280. printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
  281. else
  282. printf(ESC"[0;37;40m");
  283. return EFI_EXIT(EFI_SUCCESS);
  284. }
  285. static efi_status_t EFIAPI efi_cout_clear_screen(
  286. struct efi_simple_text_output_protocol *this)
  287. {
  288. EFI_ENTRY("%p", this);
  289. printf(ESC"[2J");
  290. efi_con_mode.cursor_column = 0;
  291. efi_con_mode.cursor_row = 0;
  292. return EFI_EXIT(EFI_SUCCESS);
  293. }
  294. static efi_status_t EFIAPI efi_cout_reset(
  295. struct efi_simple_text_output_protocol *this,
  296. char extended_verification)
  297. {
  298. EFI_ENTRY("%p, %d", this, extended_verification);
  299. /* Clear screen */
  300. EFI_CALL(efi_cout_clear_screen(this));
  301. /* Set default colors */
  302. printf(ESC "[0;37;40m");
  303. return EFI_EXIT(EFI_SUCCESS);
  304. }
  305. static efi_status_t EFIAPI efi_cout_set_cursor_position(
  306. struct efi_simple_text_output_protocol *this,
  307. unsigned long column, unsigned long row)
  308. {
  309. EFI_ENTRY("%p, %ld, %ld", this, column, row);
  310. printf(ESC"[%d;%df", (int)row, (int)column);
  311. efi_con_mode.cursor_column = column;
  312. efi_con_mode.cursor_row = row;
  313. return EFI_EXIT(EFI_SUCCESS);
  314. }
  315. static efi_status_t EFIAPI efi_cout_enable_cursor(
  316. struct efi_simple_text_output_protocol *this,
  317. bool enable)
  318. {
  319. EFI_ENTRY("%p, %d", this, enable);
  320. printf(ESC"[?25%c", enable ? 'h' : 'l');
  321. return EFI_EXIT(EFI_SUCCESS);
  322. }
  323. struct efi_simple_text_output_protocol efi_con_out = {
  324. .reset = efi_cout_reset,
  325. .output_string = efi_cout_output_string,
  326. .test_string = efi_cout_test_string,
  327. .query_mode = efi_cout_query_mode,
  328. .set_mode = efi_cout_set_mode,
  329. .set_attribute = efi_cout_set_attribute,
  330. .clear_screen = efi_cout_clear_screen,
  331. .set_cursor_position = efi_cout_set_cursor_position,
  332. .enable_cursor = efi_cout_enable_cursor,
  333. .mode = (void*)&efi_con_mode,
  334. };
  335. static bool key_available;
  336. static struct efi_key_data next_key;
  337. /**
  338. * set_shift_mask() - set shift mask
  339. *
  340. * @mod: Xterm shift mask
  341. */
  342. void set_shift_mask(int mod, struct efi_key_state *key_state)
  343. {
  344. key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
  345. if (mod) {
  346. --mod;
  347. if (mod & 1)
  348. key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
  349. if (mod & 2)
  350. key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
  351. if (mod & 4)
  352. key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
  353. if (mod & 8)
  354. key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
  355. } else {
  356. key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
  357. }
  358. }
  359. /**
  360. * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
  361. *
  362. * This gets called when we have already parsed CSI.
  363. *
  364. * @modifiers: bitmask (shift, alt, ctrl)
  365. * @return: the unmodified code
  366. */
  367. static int analyze_modifiers(struct efi_key_state *key_state)
  368. {
  369. int c, mod = 0, ret = 0;
  370. c = getc();
  371. if (c != ';') {
  372. ret = c;
  373. if (c == '~')
  374. goto out;
  375. c = getc();
  376. }
  377. for (;;) {
  378. switch (c) {
  379. case '0'...'9':
  380. mod *= 10;
  381. mod += c - '0';
  382. /* fall through */
  383. case ';':
  384. c = getc();
  385. break;
  386. default:
  387. goto out;
  388. }
  389. }
  390. out:
  391. set_shift_mask(mod, key_state);
  392. if (!ret)
  393. ret = c;
  394. return ret;
  395. }
  396. /**
  397. * efi_cin_read_key() - read a key from the console input
  398. *
  399. * @key: - key received
  400. * Return: - status code
  401. */
  402. static efi_status_t efi_cin_read_key(struct efi_key_data *key)
  403. {
  404. struct efi_input_key pressed_key = {
  405. .scan_code = 0,
  406. .unicode_char = 0,
  407. };
  408. s32 ch;
  409. if (console_read_unicode(&ch))
  410. return EFI_NOT_READY;
  411. key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
  412. key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
  413. /* We do not support multi-word codes */
  414. if (ch >= 0x10000)
  415. ch = '?';
  416. switch (ch) {
  417. case 0x1b:
  418. /*
  419. * Xterm Control Sequences
  420. * https://www.xfree86.org/4.8.0/ctlseqs.html
  421. */
  422. ch = getc();
  423. switch (ch) {
  424. case cESC: /* ESC */
  425. pressed_key.scan_code = 23;
  426. break;
  427. case 'O': /* F1 - F4 */
  428. ch = getc();
  429. /* consider modifiers */
  430. if (ch < 'P') {
  431. set_shift_mask(ch - '0', &key->key_state);
  432. ch = getc();
  433. }
  434. pressed_key.scan_code = ch - 'P' + 11;
  435. break;
  436. case '[':
  437. ch = getc();
  438. switch (ch) {
  439. case 'A'...'D': /* up, down right, left */
  440. pressed_key.scan_code = ch - 'A' + 1;
  441. break;
  442. case 'F': /* End */
  443. pressed_key.scan_code = 6;
  444. break;
  445. case 'H': /* Home */
  446. pressed_key.scan_code = 5;
  447. break;
  448. case '1':
  449. ch = analyze_modifiers(&key->key_state);
  450. switch (ch) {
  451. case '1'...'5': /* F1 - F5 */
  452. pressed_key.scan_code = ch - '1' + 11;
  453. break;
  454. case '7'...'9': /* F6 - F8 */
  455. pressed_key.scan_code = ch - '7' + 16;
  456. break;
  457. case 'A'...'D': /* up, down right, left */
  458. pressed_key.scan_code = ch - 'A' + 1;
  459. break;
  460. case 'F':
  461. pressed_key.scan_code = 6; /* End */
  462. break;
  463. case 'H':
  464. pressed_key.scan_code = 5; /* Home */
  465. break;
  466. }
  467. break;
  468. case '2':
  469. ch = analyze_modifiers(&key->key_state);
  470. switch (ch) {
  471. case '0'...'1': /* F9 - F10 */
  472. pressed_key.scan_code = ch - '0' + 19;
  473. break;
  474. case '3'...'4': /* F11 - F12 */
  475. pressed_key.scan_code = ch - '3' + 21;
  476. break;
  477. case '~': /* INS */
  478. pressed_key.scan_code = 7;
  479. break;
  480. }
  481. break;
  482. case '3': /* DEL */
  483. pressed_key.scan_code = 8;
  484. analyze_modifiers(&key->key_state);
  485. break;
  486. case '5': /* PG UP */
  487. pressed_key.scan_code = 9;
  488. analyze_modifiers(&key->key_state);
  489. break;
  490. case '6': /* PG DOWN */
  491. pressed_key.scan_code = 10;
  492. analyze_modifiers(&key->key_state);
  493. break;
  494. } /* [ */
  495. break;
  496. default:
  497. /* ALT key */
  498. set_shift_mask(3, &key->key_state);
  499. }
  500. break;
  501. case 0x7f:
  502. /* Backspace */
  503. ch = 0x08;
  504. }
  505. if (pressed_key.scan_code) {
  506. key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
  507. } else {
  508. pressed_key.unicode_char = ch;
  509. /*
  510. * Assume left control key for control characters typically
  511. * entered using the control key.
  512. */
  513. if (ch >= 0x01 && ch <= 0x1f) {
  514. key->key_state.key_shift_state |=
  515. EFI_SHIFT_STATE_VALID;
  516. switch (ch) {
  517. case 0x01 ... 0x07:
  518. case 0x0b ... 0x0c:
  519. case 0x0e ... 0x1f:
  520. key->key_state.key_shift_state |=
  521. EFI_LEFT_CONTROL_PRESSED;
  522. }
  523. }
  524. }
  525. key->key = pressed_key;
  526. return EFI_SUCCESS;
  527. }
  528. /**
  529. * efi_cin_check() - check if keyboard input is available
  530. */
  531. static void efi_cin_check(void)
  532. {
  533. efi_status_t ret;
  534. if (key_available) {
  535. efi_signal_event(efi_con_in.wait_for_key, true);
  536. return;
  537. }
  538. if (tstc()) {
  539. ret = efi_cin_read_key(&next_key);
  540. if (ret == EFI_SUCCESS) {
  541. key_available = true;
  542. /* Queue the wait for key event */
  543. efi_signal_event(efi_con_in.wait_for_key, true);
  544. }
  545. }
  546. }
  547. /**
  548. * efi_cin_empty_buffer() - empty input buffer
  549. */
  550. static void efi_cin_empty_buffer(void)
  551. {
  552. while (tstc())
  553. getc();
  554. key_available = false;
  555. }
  556. /**
  557. * efi_cin_reset_ex() - reset console input
  558. *
  559. * @this: - the extended simple text input protocol
  560. * @extended_verification: - extended verification
  561. *
  562. * This function implements the reset service of the
  563. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  564. *
  565. * See the Unified Extensible Firmware Interface (UEFI) specification for
  566. * details.
  567. *
  568. * Return: old value of the task priority level
  569. */
  570. static efi_status_t EFIAPI efi_cin_reset_ex(
  571. struct efi_simple_text_input_ex_protocol *this,
  572. bool extended_verification)
  573. {
  574. efi_status_t ret = EFI_SUCCESS;
  575. EFI_ENTRY("%p, %d", this, extended_verification);
  576. /* Check parameters */
  577. if (!this) {
  578. ret = EFI_INVALID_PARAMETER;
  579. goto out;
  580. }
  581. efi_cin_empty_buffer();
  582. out:
  583. return EFI_EXIT(ret);
  584. }
  585. /**
  586. * efi_cin_read_key_stroke_ex() - read key stroke
  587. *
  588. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  589. * @key_data: key read from console
  590. * Return: status code
  591. *
  592. * This function implements the ReadKeyStrokeEx service of the
  593. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  594. *
  595. * See the Unified Extensible Firmware Interface (UEFI) specification for
  596. * details.
  597. */
  598. static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
  599. struct efi_simple_text_input_ex_protocol *this,
  600. struct efi_key_data *key_data)
  601. {
  602. efi_status_t ret = EFI_SUCCESS;
  603. EFI_ENTRY("%p, %p", this, key_data);
  604. /* Check parameters */
  605. if (!this || !key_data) {
  606. ret = EFI_INVALID_PARAMETER;
  607. goto out;
  608. }
  609. /* We don't do interrupts, so check for timers cooperatively */
  610. efi_timer_check();
  611. /* Enable console input after ExitBootServices */
  612. efi_cin_check();
  613. if (!key_available) {
  614. ret = EFI_NOT_READY;
  615. goto out;
  616. }
  617. *key_data = next_key;
  618. key_available = false;
  619. efi_con_in.wait_for_key->is_signaled = false;
  620. out:
  621. return EFI_EXIT(ret);
  622. }
  623. /**
  624. * efi_cin_set_state() - set toggle key state
  625. *
  626. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  627. * @key_toggle_state: key toggle state
  628. * Return: status code
  629. *
  630. * This function implements the SetState service of the
  631. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  632. *
  633. * See the Unified Extensible Firmware Interface (UEFI) specification for
  634. * details.
  635. */
  636. static efi_status_t EFIAPI efi_cin_set_state(
  637. struct efi_simple_text_input_ex_protocol *this,
  638. u8 key_toggle_state)
  639. {
  640. EFI_ENTRY("%p, %u", this, key_toggle_state);
  641. /*
  642. * U-Boot supports multiple console input sources like serial and
  643. * net console for which a key toggle state cannot be set at all.
  644. *
  645. * According to the UEFI specification it is allowable to not implement
  646. * this service.
  647. */
  648. return EFI_EXIT(EFI_UNSUPPORTED);
  649. }
  650. /**
  651. * efi_cin_register_key_notify() - register key notification function
  652. *
  653. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  654. * @key_data: key to be notified
  655. * @key_notify_function: function to be called if the key is pressed
  656. * @notify_handle: handle for unregistering the notification
  657. * Return: status code
  658. *
  659. * This function implements the SetState service of the
  660. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  661. *
  662. * See the Unified Extensible Firmware Interface (UEFI) specification for
  663. * details.
  664. */
  665. static efi_status_t EFIAPI efi_cin_register_key_notify(
  666. struct efi_simple_text_input_ex_protocol *this,
  667. struct efi_key_data *key_data,
  668. efi_status_t (EFIAPI *key_notify_function)(
  669. struct efi_key_data *key_data),
  670. void **notify_handle)
  671. {
  672. EFI_ENTRY("%p, %p, %p, %p",
  673. this, key_data, key_notify_function, notify_handle);
  674. return EFI_EXIT(EFI_OUT_OF_RESOURCES);
  675. }
  676. /**
  677. * efi_cin_unregister_key_notify() - unregister key notification function
  678. *
  679. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  680. * @notification_handle: handle received when registering
  681. * Return: status code
  682. *
  683. * This function implements the SetState service of the
  684. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  685. *
  686. * See the Unified Extensible Firmware Interface (UEFI) specification for
  687. * details.
  688. */
  689. static efi_status_t EFIAPI efi_cin_unregister_key_notify(
  690. struct efi_simple_text_input_ex_protocol *this,
  691. void *notification_handle)
  692. {
  693. EFI_ENTRY("%p, %p", this, notification_handle);
  694. return EFI_EXIT(EFI_INVALID_PARAMETER);
  695. }
  696. /**
  697. * efi_cin_reset() - drain the input buffer
  698. *
  699. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  700. * @extended_verification: allow for exhaustive verification
  701. * Return: status code
  702. *
  703. * This function implements the Reset service of the
  704. * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
  705. *
  706. * See the Unified Extensible Firmware Interface (UEFI) specification for
  707. * details.
  708. */
  709. static efi_status_t EFIAPI efi_cin_reset
  710. (struct efi_simple_text_input_protocol *this,
  711. bool extended_verification)
  712. {
  713. efi_status_t ret = EFI_SUCCESS;
  714. EFI_ENTRY("%p, %d", this, extended_verification);
  715. /* Check parameters */
  716. if (!this) {
  717. ret = EFI_INVALID_PARAMETER;
  718. goto out;
  719. }
  720. efi_cin_empty_buffer();
  721. out:
  722. return EFI_EXIT(ret);
  723. }
  724. /**
  725. * efi_cin_read_key_stroke() - read key stroke
  726. *
  727. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  728. * @key: key read from console
  729. * Return: status code
  730. *
  731. * This function implements the ReadKeyStroke service of the
  732. * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
  733. *
  734. * See the Unified Extensible Firmware Interface (UEFI) specification for
  735. * details.
  736. */
  737. static efi_status_t EFIAPI efi_cin_read_key_stroke
  738. (struct efi_simple_text_input_protocol *this,
  739. struct efi_input_key *key)
  740. {
  741. efi_status_t ret = EFI_SUCCESS;
  742. EFI_ENTRY("%p, %p", this, key);
  743. /* Check parameters */
  744. if (!this || !key) {
  745. ret = EFI_INVALID_PARAMETER;
  746. goto out;
  747. }
  748. /* We don't do interrupts, so check for timers cooperatively */
  749. efi_timer_check();
  750. /* Enable console input after ExitBootServices */
  751. efi_cin_check();
  752. if (!key_available) {
  753. ret = EFI_NOT_READY;
  754. goto out;
  755. }
  756. *key = next_key.key;
  757. key_available = false;
  758. efi_con_in.wait_for_key->is_signaled = false;
  759. out:
  760. return EFI_EXIT(ret);
  761. }
  762. static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
  763. .reset = efi_cin_reset_ex,
  764. .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
  765. .wait_for_key_ex = NULL,
  766. .set_state = efi_cin_set_state,
  767. .register_key_notify = efi_cin_register_key_notify,
  768. .unregister_key_notify = efi_cin_unregister_key_notify,
  769. };
  770. struct efi_simple_text_input_protocol efi_con_in = {
  771. .reset = efi_cin_reset,
  772. .read_key_stroke = efi_cin_read_key_stroke,
  773. .wait_for_key = NULL,
  774. };
  775. static struct efi_event *console_timer_event;
  776. /*
  777. * efi_console_timer_notify() - notify the console timer event
  778. *
  779. * @event: console timer event
  780. * @context: not used
  781. */
  782. static void EFIAPI efi_console_timer_notify(struct efi_event *event,
  783. void *context)
  784. {
  785. EFI_ENTRY("%p, %p", event, context);
  786. efi_cin_check();
  787. EFI_EXIT(EFI_SUCCESS);
  788. }
  789. /**
  790. * efi_key_notify() - notify the wait for key event
  791. *
  792. * @event: wait for key event
  793. * @context: not used
  794. */
  795. static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
  796. {
  797. EFI_ENTRY("%p, %p", event, context);
  798. efi_cin_check();
  799. EFI_EXIT(EFI_SUCCESS);
  800. }
  801. /**
  802. * efi_console_register() - install the console protocols
  803. *
  804. * This function is called from do_bootefi_exec().
  805. */
  806. int efi_console_register(void)
  807. {
  808. efi_status_t r;
  809. struct efi_object *efi_console_output_obj;
  810. struct efi_object *efi_console_input_obj;
  811. /* Set up mode information */
  812. query_console_size();
  813. /* Create handles */
  814. r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
  815. if (r != EFI_SUCCESS)
  816. goto out_of_memory;
  817. r = efi_add_protocol(efi_console_output_obj->handle,
  818. &efi_guid_text_output_protocol, &efi_con_out);
  819. if (r != EFI_SUCCESS)
  820. goto out_of_memory;
  821. systab.con_out_handle = efi_console_output_obj->handle;
  822. systab.stderr_handle = efi_console_output_obj->handle;
  823. r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
  824. if (r != EFI_SUCCESS)
  825. goto out_of_memory;
  826. r = efi_add_protocol(efi_console_input_obj->handle,
  827. &efi_guid_text_input_protocol, &efi_con_in);
  828. if (r != EFI_SUCCESS)
  829. goto out_of_memory;
  830. systab.con_in_handle = efi_console_input_obj->handle;
  831. r = efi_add_protocol(efi_console_input_obj->handle,
  832. &efi_guid_text_input_ex_protocol, &efi_con_in_ex);
  833. if (r != EFI_SUCCESS)
  834. goto out_of_memory;
  835. /* Create console events */
  836. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
  837. NULL, NULL, &efi_con_in.wait_for_key);
  838. if (r != EFI_SUCCESS) {
  839. printf("ERROR: Failed to register WaitForKey event\n");
  840. return r;
  841. }
  842. efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
  843. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  844. efi_console_timer_notify, NULL, NULL,
  845. &console_timer_event);
  846. if (r != EFI_SUCCESS) {
  847. printf("ERROR: Failed to register console event\n");
  848. return r;
  849. }
  850. /* 5000 ns cycle is sufficient for 2 MBaud */
  851. r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
  852. if (r != EFI_SUCCESS)
  853. printf("ERROR: Failed to set console timer\n");
  854. return r;
  855. out_of_memory:
  856. printf("ERROR: Out of memory\n");
  857. return r;
  858. }