efi_console.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * EFI application console interface
  4. *
  5. * Copyright (c) 2016 Alexander Graf
  6. */
  7. #define LOG_CATEGORY LOGC_EFI
  8. #include <ansi.h>
  9. #include <common.h>
  10. #include <charset.h>
  11. #include <malloc.h>
  12. #include <time.h>
  13. #include <dm/device.h>
  14. #include <efi_loader.h>
  15. #include <env.h>
  16. #include <log.h>
  17. #include <stdio_dev.h>
  18. #include <video_console.h>
  19. #include <linux/delay.h>
  20. #define EFI_COUT_MODE_2 2
  21. #define EFI_MAX_COUT_MODE 3
  22. struct cout_mode {
  23. unsigned long columns;
  24. unsigned long rows;
  25. int present;
  26. };
  27. __maybe_unused static struct efi_object uart_obj;
  28. static struct cout_mode efi_cout_modes[] = {
  29. /* EFI Mode 0 is 80x25 and always present */
  30. {
  31. .columns = 80,
  32. .rows = 25,
  33. .present = 1,
  34. },
  35. /* EFI Mode 1 is always 80x50 */
  36. {
  37. .columns = 80,
  38. .rows = 50,
  39. .present = 0,
  40. },
  41. /* Value are unknown until we query the console */
  42. {
  43. .columns = 0,
  44. .rows = 0,
  45. .present = 0,
  46. },
  47. };
  48. const efi_guid_t efi_guid_text_input_ex_protocol =
  49. EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
  50. const efi_guid_t efi_guid_text_input_protocol =
  51. EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
  52. const efi_guid_t efi_guid_text_output_protocol =
  53. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
  54. #define cESC '\x1b'
  55. #define ESC "\x1b"
  56. /*
  57. * efi_con_mode - mode information of the Simple Text Output Protocol
  58. *
  59. * Use safe settings before efi_setup_console_size() is called.
  60. * By default enable only the 80x25 mode which must always exist.
  61. */
  62. static struct simple_text_output_mode efi_con_mode = {
  63. .max_mode = 1,
  64. .mode = 0,
  65. .attribute = 0,
  66. .cursor_column = 0,
  67. .cursor_row = 0,
  68. .cursor_visible = 1,
  69. };
  70. /**
  71. * term_get_char() - read a character from the console
  72. *
  73. * Wait for up to 100 ms to read a character from the console.
  74. *
  75. * @c: pointer to the buffer to receive the character
  76. * Return: 0 on success, 1 otherwise
  77. */
  78. static int term_get_char(s32 *c)
  79. {
  80. u64 timeout;
  81. /* Wait up to 100 ms for a character */
  82. timeout = timer_get_us() + 100000;
  83. while (!tstc())
  84. if (timer_get_us() > timeout)
  85. return 1;
  86. *c = getchar();
  87. return 0;
  88. }
  89. /**
  90. * Receive and parse a reply from the terminal.
  91. *
  92. * @n: array of return values
  93. * @num: number of return values expected
  94. * @end_char: character indicating end of terminal message
  95. * Return: non-zero indicates error
  96. */
  97. static int term_read_reply(int *n, int num, char end_char)
  98. {
  99. s32 c;
  100. int i = 0;
  101. if (term_get_char(&c) || c != cESC)
  102. return -1;
  103. if (term_get_char(&c) || c != '[')
  104. return -1;
  105. n[0] = 0;
  106. while (1) {
  107. if (!term_get_char(&c)) {
  108. if (c == ';') {
  109. i++;
  110. if (i >= num)
  111. return -1;
  112. n[i] = 0;
  113. continue;
  114. } else if (c == end_char) {
  115. break;
  116. } else if (c > '9' || c < '0') {
  117. return -1;
  118. }
  119. /* Read one more decimal position */
  120. n[i] *= 10;
  121. n[i] += c - '0';
  122. } else {
  123. return -1;
  124. }
  125. }
  126. if (i != num - 1)
  127. return -1;
  128. return 0;
  129. }
  130. /**
  131. * efi_cout_output_string() - write Unicode string to console
  132. *
  133. * This function implements the OutputString service of the simple text output
  134. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  135. * for details.
  136. *
  137. * @this: simple text output protocol
  138. * @string: u16 string
  139. * Return: status code
  140. */
  141. static efi_status_t EFIAPI efi_cout_output_string(
  142. struct efi_simple_text_output_protocol *this,
  143. const u16 *string)
  144. {
  145. struct simple_text_output_mode *con = &efi_con_mode;
  146. struct cout_mode *mode = &efi_cout_modes[con->mode];
  147. char *buf, *pos;
  148. const u16 *p;
  149. efi_status_t ret = EFI_SUCCESS;
  150. EFI_ENTRY("%p, %p", this, string);
  151. if (!this || !string) {
  152. ret = EFI_INVALID_PARAMETER;
  153. goto out;
  154. }
  155. buf = malloc(utf16_utf8_strlen(string) + 1);
  156. if (!buf) {
  157. ret = EFI_OUT_OF_RESOURCES;
  158. goto out;
  159. }
  160. pos = buf;
  161. utf16_utf8_strcpy(&pos, string);
  162. fputs(stdout, buf);
  163. free(buf);
  164. /*
  165. * Update the cursor position.
  166. *
  167. * The UEFI spec provides advance rules for U+0000, U+0008, U+000A,
  168. * and U000D. All other control characters are ignored. Any non-control
  169. * character increase the column by one.
  170. */
  171. for (p = string; *p; ++p) {
  172. switch (*p) {
  173. case '\b': /* U+0008, backspace */
  174. if (con->cursor_column)
  175. con->cursor_column--;
  176. break;
  177. case '\n': /* U+000A, newline */
  178. con->cursor_column = 0;
  179. con->cursor_row++;
  180. break;
  181. case '\r': /* U+000D, carriage-return */
  182. con->cursor_column = 0;
  183. break;
  184. case 0xd800 ... 0xdbff:
  185. /*
  186. * Ignore high surrogates, we do not want to count a
  187. * Unicode character twice.
  188. */
  189. break;
  190. default:
  191. /* Exclude control codes */
  192. if (*p > 0x1f)
  193. con->cursor_column++;
  194. break;
  195. }
  196. if (con->cursor_column >= mode->columns) {
  197. con->cursor_column = 0;
  198. con->cursor_row++;
  199. }
  200. /*
  201. * When we exceed the row count the terminal will scroll up one
  202. * line. We have to adjust the cursor position.
  203. */
  204. if (con->cursor_row >= mode->rows && con->cursor_row)
  205. con->cursor_row--;
  206. }
  207. out:
  208. return EFI_EXIT(ret);
  209. }
  210. /**
  211. * efi_cout_test_string() - test writing Unicode string to console
  212. *
  213. * This function implements the TestString service of the simple text output
  214. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  215. * for details.
  216. *
  217. * As in OutputString we simply convert UTF-16 to UTF-8 there are no unsupported
  218. * code points and we can always return EFI_SUCCESS.
  219. *
  220. * @this: simple text output protocol
  221. * @string: u16 string
  222. * Return: status code
  223. */
  224. static efi_status_t EFIAPI efi_cout_test_string(
  225. struct efi_simple_text_output_protocol *this,
  226. const u16 *string)
  227. {
  228. EFI_ENTRY("%p, %p", this, string);
  229. return EFI_EXIT(EFI_SUCCESS);
  230. }
  231. /**
  232. * cout_mode_matches() - check if mode has given terminal size
  233. *
  234. * @mode: text mode
  235. * @rows: number of rows
  236. * @cols: number of columns
  237. * Return: true if number of rows and columns matches the mode and
  238. * the mode is present
  239. */
  240. static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
  241. {
  242. if (!mode->present)
  243. return false;
  244. return (mode->rows == rows) && (mode->columns == cols);
  245. }
  246. /**
  247. * query_console_serial() - query serial console size
  248. *
  249. * When using a serial console or the net console we can only devise the
  250. * terminal size by querying the terminal using ECMA-48 control sequences.
  251. *
  252. * @rows: pointer to return number of rows
  253. * @cols: pointer to return number of columns
  254. * Returns: 0 on success
  255. */
  256. static int query_console_serial(int *rows, int *cols)
  257. {
  258. int ret = 0;
  259. int n[2];
  260. /* Empty input buffer */
  261. while (tstc())
  262. getchar();
  263. /*
  264. * Not all terminals understand CSI [18t for querying the console size.
  265. * We should adhere to escape sequences documented in the console_codes
  266. * man page and the ECMA-48 standard.
  267. *
  268. * So here we follow a different approach. We position the cursor to the
  269. * bottom right and query its position. Before leaving the function we
  270. * restore the original cursor position.
  271. */
  272. printf(ESC "7" /* Save cursor position */
  273. ESC "[r" /* Set scrolling region to full window */
  274. ESC "[999;999H" /* Move to bottom right corner */
  275. ESC "[6n"); /* Query cursor position */
  276. /* Read {rows,cols} */
  277. if (term_read_reply(n, 2, 'R')) {
  278. ret = 1;
  279. goto out;
  280. }
  281. *cols = n[1];
  282. *rows = n[0];
  283. out:
  284. printf(ESC "8"); /* Restore cursor position */
  285. return ret;
  286. }
  287. /**
  288. * query_vidconsole() - query video console size
  289. *
  290. *
  291. * @rows: pointer to return number of rows
  292. * @cols: pointer to return number of columns
  293. * Returns: 0 on success
  294. */
  295. static int __maybe_unused query_vidconsole(int *rows, int *cols)
  296. {
  297. const char *stdout_name = env_get("stdout");
  298. struct stdio_dev *stdout_dev;
  299. struct udevice *dev;
  300. struct vidconsole_priv *priv;
  301. if (!stdout_name || strncmp(stdout_name, "vidconsole", 10))
  302. return -ENODEV;
  303. stdout_dev = stdio_get_by_name("vidconsole");
  304. if (!stdout_dev)
  305. return -ENODEV;
  306. dev = stdout_dev->priv;
  307. if (!dev)
  308. return -ENODEV;
  309. priv = dev_get_uclass_priv(dev);
  310. if (!priv)
  311. return -ENODEV;
  312. *rows = priv->rows;
  313. *cols = priv->cols;
  314. return 0;
  315. }
  316. /**
  317. * efi_setup_console_size() - update the mode table.
  318. *
  319. * By default the only mode available is 80x25. If the console has at least 50
  320. * lines, enable mode 80x50. If we can query the console size and it is neither
  321. * 80x25 nor 80x50, set it as an additional mode.
  322. */
  323. void efi_setup_console_size(void)
  324. {
  325. int rows = 25, cols = 80;
  326. int ret = -ENODEV;
  327. if (IS_ENABLED(CONFIG_VIDEO))
  328. ret = query_vidconsole(&rows, &cols);
  329. if (ret)
  330. ret = query_console_serial(&rows, &cols);
  331. if (ret)
  332. return;
  333. log_debug("Console size %dx%d\n", rows, cols);
  334. /* Test if we can have Mode 1 */
  335. if (cols >= 80 && rows >= 50) {
  336. efi_cout_modes[1].present = 1;
  337. efi_con_mode.max_mode = 2;
  338. }
  339. /*
  340. * Install our mode as mode 2 if it is different
  341. * than mode 0 or 1 and set it as the currently selected mode
  342. */
  343. if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
  344. !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
  345. efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
  346. efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
  347. efi_cout_modes[EFI_COUT_MODE_2].present = 1;
  348. efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
  349. efi_con_mode.mode = EFI_COUT_MODE_2;
  350. }
  351. }
  352. /**
  353. * efi_cout_query_mode() - get terminal size for a text mode
  354. *
  355. * This function implements the QueryMode service of the simple text output
  356. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  357. * for details.
  358. *
  359. * @this: simple text output protocol
  360. * @mode_number: mode number to retrieve information on
  361. * @columns: number of columns
  362. * @rows: number of rows
  363. * Return: status code
  364. */
  365. static efi_status_t EFIAPI efi_cout_query_mode(
  366. struct efi_simple_text_output_protocol *this,
  367. unsigned long mode_number, unsigned long *columns,
  368. unsigned long *rows)
  369. {
  370. EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
  371. if (mode_number >= efi_con_mode.max_mode)
  372. return EFI_EXIT(EFI_UNSUPPORTED);
  373. if (efi_cout_modes[mode_number].present != 1)
  374. return EFI_EXIT(EFI_UNSUPPORTED);
  375. if (columns)
  376. *columns = efi_cout_modes[mode_number].columns;
  377. if (rows)
  378. *rows = efi_cout_modes[mode_number].rows;
  379. return EFI_EXIT(EFI_SUCCESS);
  380. }
  381. static const struct {
  382. unsigned int fg;
  383. unsigned int bg;
  384. } color[] = {
  385. { 30, 40 }, /* 0: black */
  386. { 34, 44 }, /* 1: blue */
  387. { 32, 42 }, /* 2: green */
  388. { 36, 46 }, /* 3: cyan */
  389. { 31, 41 }, /* 4: red */
  390. { 35, 45 }, /* 5: magenta */
  391. { 33, 43 }, /* 6: brown, map to yellow as EDK2 does*/
  392. { 37, 47 }, /* 7: light gray, map to white */
  393. };
  394. /**
  395. * efi_cout_set_attribute() - set fore- and background color
  396. *
  397. * This function implements the SetAttribute service of the simple text output
  398. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  399. * for details.
  400. *
  401. * @this: simple text output protocol
  402. * @attribute: foreground color - bits 0-3, background color - bits 4-6
  403. * Return: status code
  404. */
  405. static efi_status_t EFIAPI efi_cout_set_attribute(
  406. struct efi_simple_text_output_protocol *this,
  407. unsigned long attribute)
  408. {
  409. unsigned int bold = EFI_ATTR_BOLD(attribute);
  410. unsigned int fg = EFI_ATTR_FG(attribute);
  411. unsigned int bg = EFI_ATTR_BG(attribute);
  412. EFI_ENTRY("%p, %lx", this, attribute);
  413. efi_con_mode.attribute = attribute;
  414. if (attribute)
  415. printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
  416. else
  417. printf(ESC"[0;37;40m");
  418. return EFI_EXIT(EFI_SUCCESS);
  419. }
  420. /**
  421. * efi_clear_screen() - clear screen
  422. */
  423. static void efi_clear_screen(void)
  424. {
  425. if (CONFIG_IS_ENABLED(EFI_SCROLL_ON_CLEAR_SCREEN)) {
  426. unsigned int row, screen_rows, screen_columns;
  427. /* Avoid overwriting previous outputs on streaming consoles */
  428. screen_rows = efi_cout_modes[efi_con_mode.mode].rows;
  429. screen_columns = efi_cout_modes[efi_con_mode.mode].columns;
  430. printf(ESC "[%u;%uH", screen_rows, screen_columns);
  431. for (row = 1; row < screen_rows; row++)
  432. printf("\n");
  433. }
  434. /*
  435. * The Linux console wants both a clear and a home command. The video
  436. * uclass does not support <ESC>[H without coordinates, yet.
  437. */
  438. printf(ESC "[2J" ESC "[1;1H");
  439. efi_con_mode.cursor_column = 0;
  440. efi_con_mode.cursor_row = 0;
  441. }
  442. /**
  443. * efi_cout_clear_screen() - clear screen
  444. *
  445. * This function implements the ClearScreen service of the simple text output
  446. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  447. * for details.
  448. *
  449. * @this: pointer to the protocol instance
  450. * Return: status code
  451. */
  452. static efi_status_t EFIAPI efi_cout_clear_screen(
  453. struct efi_simple_text_output_protocol *this)
  454. {
  455. EFI_ENTRY("%p", this);
  456. /* Set default colors if not done yet */
  457. if (efi_con_mode.attribute == 0) {
  458. efi_con_mode.attribute = 0x07;
  459. printf(ESC "[0;37;40m");
  460. }
  461. efi_clear_screen();
  462. return EFI_EXIT(EFI_SUCCESS);
  463. }
  464. /**
  465. * efi_cout_clear_set_mode() - set text model
  466. *
  467. * This function implements the SetMode service of the simple text output
  468. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  469. * for details.
  470. *
  471. * @this: pointer to the protocol instance
  472. * @mode_number: number of the text mode to set
  473. * Return: status code
  474. */
  475. static efi_status_t EFIAPI efi_cout_set_mode(
  476. struct efi_simple_text_output_protocol *this,
  477. unsigned long mode_number)
  478. {
  479. EFI_ENTRY("%p, %ld", this, mode_number);
  480. if (mode_number >= efi_con_mode.max_mode)
  481. return EFI_EXIT(EFI_UNSUPPORTED);
  482. if (!efi_cout_modes[mode_number].present)
  483. return EFI_EXIT(EFI_UNSUPPORTED);
  484. efi_con_mode.mode = mode_number;
  485. efi_clear_screen();
  486. return EFI_EXIT(EFI_SUCCESS);
  487. }
  488. /**
  489. * efi_cout_reset() - reset the terminal
  490. *
  491. * This function implements the Reset service of the simple text output
  492. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  493. * for details.
  494. *
  495. * @this: pointer to the protocol instance
  496. * @extended_verification: if set an extended verification may be executed
  497. * Return: status code
  498. */
  499. static efi_status_t EFIAPI efi_cout_reset(
  500. struct efi_simple_text_output_protocol *this,
  501. char extended_verification)
  502. {
  503. EFI_ENTRY("%p, %d", this, extended_verification);
  504. /* Set default colors */
  505. efi_con_mode.attribute = 0x07;
  506. printf(ESC "[0;37;40m");
  507. /* Clear screen */
  508. efi_clear_screen();
  509. return EFI_EXIT(EFI_SUCCESS);
  510. }
  511. /**
  512. * efi_cout_set_cursor_position() - reset the terminal
  513. *
  514. * This function implements the SetCursorPosition service of the simple text
  515. * output protocol. See the Unified Extensible Firmware Interface (UEFI)
  516. * specification for details.
  517. *
  518. * @this: pointer to the protocol instance
  519. * @column: column to move to
  520. * @row: row to move to
  521. * Return: status code
  522. */
  523. static efi_status_t EFIAPI efi_cout_set_cursor_position(
  524. struct efi_simple_text_output_protocol *this,
  525. unsigned long column, unsigned long row)
  526. {
  527. efi_status_t ret = EFI_SUCCESS;
  528. struct simple_text_output_mode *con = &efi_con_mode;
  529. struct cout_mode *mode = &efi_cout_modes[con->mode];
  530. EFI_ENTRY("%p, %ld, %ld", this, column, row);
  531. /* Check parameters */
  532. if (!this) {
  533. ret = EFI_INVALID_PARAMETER;
  534. goto out;
  535. }
  536. if (row >= mode->rows || column >= mode->columns) {
  537. ret = EFI_UNSUPPORTED;
  538. goto out;
  539. }
  540. /*
  541. * Set cursor position by sending CSI H.
  542. * EFI origin is [0, 0], terminal origin is [1, 1].
  543. */
  544. printf(ESC "[%d;%dH", (int)row + 1, (int)column + 1);
  545. efi_con_mode.cursor_column = column;
  546. efi_con_mode.cursor_row = row;
  547. out:
  548. return EFI_EXIT(ret);
  549. }
  550. /**
  551. * efi_cout_enable_cursor() - enable the cursor
  552. *
  553. * This function implements the EnableCursor service of the simple text output
  554. * protocol. See the Unified Extensible Firmware Interface (UEFI) specification
  555. * for details.
  556. *
  557. * @this: pointer to the protocol instance
  558. * @enable: if true enable, if false disable the cursor
  559. * Return: status code
  560. */
  561. static efi_status_t EFIAPI efi_cout_enable_cursor(
  562. struct efi_simple_text_output_protocol *this,
  563. bool enable)
  564. {
  565. EFI_ENTRY("%p, %d", this, enable);
  566. printf(ESC"[?25%c", enable ? 'h' : 'l');
  567. efi_con_mode.cursor_visible = !!enable;
  568. return EFI_EXIT(EFI_SUCCESS);
  569. }
  570. struct efi_simple_text_output_protocol efi_con_out = {
  571. .reset = efi_cout_reset,
  572. .output_string = efi_cout_output_string,
  573. .test_string = efi_cout_test_string,
  574. .query_mode = efi_cout_query_mode,
  575. .set_mode = efi_cout_set_mode,
  576. .set_attribute = efi_cout_set_attribute,
  577. .clear_screen = efi_cout_clear_screen,
  578. .set_cursor_position = efi_cout_set_cursor_position,
  579. .enable_cursor = efi_cout_enable_cursor,
  580. .mode = (void*)&efi_con_mode,
  581. };
  582. /**
  583. * struct efi_cin_notify_function - registered console input notify function
  584. *
  585. * @link: link to list
  586. * @key: key to notify
  587. * @function: function to call
  588. */
  589. struct efi_cin_notify_function {
  590. struct list_head link;
  591. struct efi_key_data key;
  592. efi_status_t (EFIAPI *function)
  593. (struct efi_key_data *key_data);
  594. };
  595. static bool key_available;
  596. static struct efi_key_data next_key;
  597. static LIST_HEAD(cin_notify_functions);
  598. /**
  599. * set_shift_mask() - set shift mask
  600. *
  601. * @mod: Xterm shift mask
  602. * @key_state: receives the state of the shift, alt, control, and logo keys
  603. */
  604. static void set_shift_mask(int mod, struct efi_key_state *key_state)
  605. {
  606. key_state->key_shift_state = EFI_SHIFT_STATE_VALID;
  607. if (mod) {
  608. --mod;
  609. if (mod & 1)
  610. key_state->key_shift_state |= EFI_LEFT_SHIFT_PRESSED;
  611. if (mod & 2)
  612. key_state->key_shift_state |= EFI_LEFT_ALT_PRESSED;
  613. if (mod & 4)
  614. key_state->key_shift_state |= EFI_LEFT_CONTROL_PRESSED;
  615. if (!mod || (mod & 8))
  616. key_state->key_shift_state |= EFI_LEFT_LOGO_PRESSED;
  617. }
  618. }
  619. /**
  620. * analyze_modifiers() - analyze modifiers (shift, alt, ctrl) for function keys
  621. *
  622. * This gets called when we have already parsed CSI.
  623. *
  624. * @key_state: receives the state of the shift, alt, control, and logo keys
  625. * Return: the unmodified code
  626. */
  627. static int analyze_modifiers(struct efi_key_state *key_state)
  628. {
  629. int c, mod = 0, ret = 0;
  630. c = getchar();
  631. if (c != ';') {
  632. ret = c;
  633. if (c == '~')
  634. goto out;
  635. c = getchar();
  636. }
  637. for (;;) {
  638. switch (c) {
  639. case '0'...'9':
  640. mod *= 10;
  641. mod += c - '0';
  642. /* fall through */
  643. case ';':
  644. c = getchar();
  645. break;
  646. default:
  647. goto out;
  648. }
  649. }
  650. out:
  651. set_shift_mask(mod, key_state);
  652. if (!ret)
  653. ret = c;
  654. return ret;
  655. }
  656. /**
  657. * efi_cin_read_key() - read a key from the console input
  658. *
  659. * @key: - key received
  660. * Return: - status code
  661. */
  662. static efi_status_t efi_cin_read_key(struct efi_key_data *key)
  663. {
  664. struct efi_input_key pressed_key = {
  665. .scan_code = 0,
  666. .unicode_char = 0,
  667. };
  668. s32 ch;
  669. if (console_read_unicode(&ch))
  670. return EFI_NOT_READY;
  671. key->key_state.key_shift_state = EFI_SHIFT_STATE_INVALID;
  672. key->key_state.key_toggle_state = EFI_TOGGLE_STATE_INVALID;
  673. /* We do not support multi-word codes */
  674. if (ch >= 0x10000)
  675. ch = '?';
  676. switch (ch) {
  677. case 0x1b:
  678. /*
  679. * If a second key is received within 10 ms, assume that we are
  680. * dealing with an escape sequence. Otherwise consider this the
  681. * escape key being hit. 10 ms is long enough to work fine at
  682. * 1200 baud and above.
  683. */
  684. udelay(10000);
  685. if (!tstc()) {
  686. pressed_key.scan_code = 23;
  687. break;
  688. }
  689. /*
  690. * Xterm Control Sequences
  691. * https://www.xfree86.org/4.8.0/ctlseqs.html
  692. */
  693. ch = getchar();
  694. switch (ch) {
  695. case cESC: /* ESC */
  696. pressed_key.scan_code = 23;
  697. break;
  698. case 'O': /* F1 - F4, End */
  699. ch = getchar();
  700. /* consider modifiers */
  701. if (ch == 'F') { /* End */
  702. pressed_key.scan_code = 6;
  703. break;
  704. } else if (ch < 'P') {
  705. set_shift_mask(ch - '0', &key->key_state);
  706. ch = getchar();
  707. }
  708. pressed_key.scan_code = ch - 'P' + 11;
  709. break;
  710. case '[':
  711. ch = getchar();
  712. switch (ch) {
  713. case 'A'...'D': /* up, down right, left */
  714. pressed_key.scan_code = ch - 'A' + 1;
  715. break;
  716. case 'F': /* End */
  717. pressed_key.scan_code = 6;
  718. break;
  719. case 'H': /* Home */
  720. pressed_key.scan_code = 5;
  721. break;
  722. case '1':
  723. ch = analyze_modifiers(&key->key_state);
  724. switch (ch) {
  725. case '1'...'5': /* F1 - F5 */
  726. pressed_key.scan_code = ch - '1' + 11;
  727. break;
  728. case '6'...'9': /* F5 - F8 */
  729. pressed_key.scan_code = ch - '6' + 15;
  730. break;
  731. case 'A'...'D': /* up, down right, left */
  732. pressed_key.scan_code = ch - 'A' + 1;
  733. break;
  734. case 'F': /* End */
  735. pressed_key.scan_code = 6;
  736. break;
  737. case 'H': /* Home */
  738. pressed_key.scan_code = 5;
  739. break;
  740. case '~': /* Home */
  741. pressed_key.scan_code = 5;
  742. break;
  743. }
  744. break;
  745. case '2':
  746. ch = analyze_modifiers(&key->key_state);
  747. switch (ch) {
  748. case '0'...'1': /* F9 - F10 */
  749. pressed_key.scan_code = ch - '0' + 19;
  750. break;
  751. case '3'...'4': /* F11 - F12 */
  752. pressed_key.scan_code = ch - '3' + 21;
  753. break;
  754. case '~': /* INS */
  755. pressed_key.scan_code = 7;
  756. break;
  757. }
  758. break;
  759. case '3': /* DEL */
  760. pressed_key.scan_code = 8;
  761. analyze_modifiers(&key->key_state);
  762. break;
  763. case '5': /* PG UP */
  764. pressed_key.scan_code = 9;
  765. analyze_modifiers(&key->key_state);
  766. break;
  767. case '6': /* PG DOWN */
  768. pressed_key.scan_code = 10;
  769. analyze_modifiers(&key->key_state);
  770. break;
  771. } /* [ */
  772. break;
  773. default:
  774. /* ALT key */
  775. set_shift_mask(3, &key->key_state);
  776. }
  777. break;
  778. case 0x7f:
  779. /* Backspace */
  780. ch = 0x08;
  781. }
  782. if (pressed_key.scan_code) {
  783. key->key_state.key_shift_state |= EFI_SHIFT_STATE_VALID;
  784. } else {
  785. pressed_key.unicode_char = ch;
  786. /*
  787. * Assume left control key for control characters typically
  788. * entered using the control key.
  789. */
  790. if (ch >= 0x01 && ch <= 0x1f) {
  791. key->key_state.key_shift_state |=
  792. EFI_SHIFT_STATE_VALID;
  793. switch (ch) {
  794. case 0x01 ... 0x07:
  795. case 0x0b ... 0x0c:
  796. case 0x0e ... 0x1f:
  797. key->key_state.key_shift_state |=
  798. EFI_LEFT_CONTROL_PRESSED;
  799. }
  800. }
  801. }
  802. key->key = pressed_key;
  803. return EFI_SUCCESS;
  804. }
  805. /**
  806. * efi_cin_notify() - notify registered functions
  807. */
  808. static void efi_cin_notify(void)
  809. {
  810. struct efi_cin_notify_function *item;
  811. list_for_each_entry(item, &cin_notify_functions, link) {
  812. bool match = true;
  813. /* We do not support toggle states */
  814. if (item->key.key.unicode_char || item->key.key.scan_code) {
  815. if (item->key.key.unicode_char !=
  816. next_key.key.unicode_char ||
  817. item->key.key.scan_code != next_key.key.scan_code)
  818. match = false;
  819. }
  820. if (item->key.key_state.key_shift_state &&
  821. item->key.key_state.key_shift_state !=
  822. next_key.key_state.key_shift_state)
  823. match = false;
  824. if (match)
  825. /* We don't bother about the return code */
  826. EFI_CALL(item->function(&next_key));
  827. }
  828. }
  829. /**
  830. * efi_cin_check() - check if keyboard input is available
  831. */
  832. static void efi_cin_check(void)
  833. {
  834. efi_status_t ret;
  835. if (key_available) {
  836. efi_signal_event(efi_con_in.wait_for_key);
  837. return;
  838. }
  839. if (tstc()) {
  840. ret = efi_cin_read_key(&next_key);
  841. if (ret == EFI_SUCCESS) {
  842. key_available = true;
  843. /* Notify registered functions */
  844. efi_cin_notify();
  845. /* Queue the wait for key event */
  846. if (key_available)
  847. efi_signal_event(efi_con_in.wait_for_key);
  848. }
  849. }
  850. }
  851. /**
  852. * efi_cin_empty_buffer() - empty input buffer
  853. */
  854. static void efi_cin_empty_buffer(void)
  855. {
  856. while (tstc())
  857. getchar();
  858. key_available = false;
  859. }
  860. /**
  861. * efi_cin_reset_ex() - reset console input
  862. *
  863. * @this: - the extended simple text input protocol
  864. * @extended_verification: - extended verification
  865. *
  866. * This function implements the reset service of the
  867. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  868. *
  869. * See the Unified Extensible Firmware Interface (UEFI) specification for
  870. * details.
  871. *
  872. * Return: old value of the task priority level
  873. */
  874. static efi_status_t EFIAPI efi_cin_reset_ex(
  875. struct efi_simple_text_input_ex_protocol *this,
  876. bool extended_verification)
  877. {
  878. efi_status_t ret = EFI_SUCCESS;
  879. EFI_ENTRY("%p, %d", this, extended_verification);
  880. /* Check parameters */
  881. if (!this) {
  882. ret = EFI_INVALID_PARAMETER;
  883. goto out;
  884. }
  885. efi_cin_empty_buffer();
  886. out:
  887. return EFI_EXIT(ret);
  888. }
  889. /**
  890. * efi_cin_read_key_stroke_ex() - read key stroke
  891. *
  892. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  893. * @key_data: key read from console
  894. * Return: status code
  895. *
  896. * This function implements the ReadKeyStrokeEx service of the
  897. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  898. *
  899. * See the Unified Extensible Firmware Interface (UEFI) specification for
  900. * details.
  901. */
  902. static efi_status_t EFIAPI efi_cin_read_key_stroke_ex(
  903. struct efi_simple_text_input_ex_protocol *this,
  904. struct efi_key_data *key_data)
  905. {
  906. efi_status_t ret = EFI_SUCCESS;
  907. EFI_ENTRY("%p, %p", this, key_data);
  908. /* Check parameters */
  909. if (!this || !key_data) {
  910. ret = EFI_INVALID_PARAMETER;
  911. goto out;
  912. }
  913. /* We don't do interrupts, so check for timers cooperatively */
  914. efi_timer_check();
  915. /* Enable console input after ExitBootServices */
  916. efi_cin_check();
  917. if (!key_available) {
  918. memset(key_data, 0, sizeof(struct efi_key_data));
  919. ret = EFI_NOT_READY;
  920. goto out;
  921. }
  922. /*
  923. * CTRL+A - CTRL+Z have to be signaled as a - z.
  924. * SHIFT+CTRL+A - SHIFT+CTRL+Z have to be signaled as A - Z.
  925. * CTRL+\ - CTRL+_ have to be signaled as \ - _.
  926. */
  927. switch (next_key.key.unicode_char) {
  928. case 0x01 ... 0x07:
  929. case 0x0b ... 0x0c:
  930. case 0x0e ... 0x1a:
  931. if (!(next_key.key_state.key_toggle_state &
  932. EFI_CAPS_LOCK_ACTIVE) ^
  933. !(next_key.key_state.key_shift_state &
  934. (EFI_LEFT_SHIFT_PRESSED | EFI_RIGHT_SHIFT_PRESSED)))
  935. next_key.key.unicode_char += 0x40;
  936. else
  937. next_key.key.unicode_char += 0x60;
  938. break;
  939. case 0x1c ... 0x1f:
  940. next_key.key.unicode_char += 0x40;
  941. }
  942. *key_data = next_key;
  943. key_available = false;
  944. efi_con_in.wait_for_key->is_signaled = false;
  945. out:
  946. return EFI_EXIT(ret);
  947. }
  948. /**
  949. * efi_cin_set_state() - set toggle key state
  950. *
  951. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  952. * @key_toggle_state: pointer to key toggle state
  953. * Return: status code
  954. *
  955. * This function implements the SetState service of the
  956. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  957. *
  958. * See the Unified Extensible Firmware Interface (UEFI) specification for
  959. * details.
  960. */
  961. static efi_status_t EFIAPI efi_cin_set_state(
  962. struct efi_simple_text_input_ex_protocol *this,
  963. u8 *key_toggle_state)
  964. {
  965. EFI_ENTRY("%p, %p", this, key_toggle_state);
  966. /*
  967. * U-Boot supports multiple console input sources like serial and
  968. * net console for which a key toggle state cannot be set at all.
  969. *
  970. * According to the UEFI specification it is allowable to not implement
  971. * this service.
  972. */
  973. return EFI_EXIT(EFI_UNSUPPORTED);
  974. }
  975. /**
  976. * efi_cin_register_key_notify() - register key notification function
  977. *
  978. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  979. * @key_data: key to be notified
  980. * @key_notify_function: function to be called if the key is pressed
  981. * @notify_handle: handle for unregistering the notification
  982. * Return: status code
  983. *
  984. * This function implements the SetState service of the
  985. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  986. *
  987. * See the Unified Extensible Firmware Interface (UEFI) specification for
  988. * details.
  989. */
  990. static efi_status_t EFIAPI efi_cin_register_key_notify(
  991. struct efi_simple_text_input_ex_protocol *this,
  992. struct efi_key_data *key_data,
  993. efi_status_t (EFIAPI *key_notify_function)(
  994. struct efi_key_data *key_data),
  995. void **notify_handle)
  996. {
  997. efi_status_t ret = EFI_SUCCESS;
  998. struct efi_cin_notify_function *notify_function;
  999. EFI_ENTRY("%p, %p, %p, %p",
  1000. this, key_data, key_notify_function, notify_handle);
  1001. /* Check parameters */
  1002. if (!this || !key_data || !key_notify_function || !notify_handle) {
  1003. ret = EFI_INVALID_PARAMETER;
  1004. goto out;
  1005. }
  1006. EFI_PRINT("u+%04x, sc %04x, sh %08x, tg %02x\n",
  1007. key_data->key.unicode_char,
  1008. key_data->key.scan_code,
  1009. key_data->key_state.key_shift_state,
  1010. key_data->key_state.key_toggle_state);
  1011. notify_function = calloc(1, sizeof(struct efi_cin_notify_function));
  1012. if (!notify_function) {
  1013. ret = EFI_OUT_OF_RESOURCES;
  1014. goto out;
  1015. }
  1016. notify_function->key = *key_data;
  1017. notify_function->function = key_notify_function;
  1018. list_add_tail(&notify_function->link, &cin_notify_functions);
  1019. *notify_handle = notify_function;
  1020. out:
  1021. return EFI_EXIT(ret);
  1022. }
  1023. /**
  1024. * efi_cin_unregister_key_notify() - unregister key notification function
  1025. *
  1026. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  1027. * @notification_handle: handle received when registering
  1028. * Return: status code
  1029. *
  1030. * This function implements the SetState service of the
  1031. * EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL.
  1032. *
  1033. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1034. * details.
  1035. */
  1036. static efi_status_t EFIAPI efi_cin_unregister_key_notify(
  1037. struct efi_simple_text_input_ex_protocol *this,
  1038. void *notification_handle)
  1039. {
  1040. efi_status_t ret = EFI_INVALID_PARAMETER;
  1041. struct efi_cin_notify_function *item, *notify_function =
  1042. notification_handle;
  1043. EFI_ENTRY("%p, %p", this, notification_handle);
  1044. /* Check parameters */
  1045. if (!this || !notification_handle)
  1046. goto out;
  1047. list_for_each_entry(item, &cin_notify_functions, link) {
  1048. if (item == notify_function) {
  1049. ret = EFI_SUCCESS;
  1050. break;
  1051. }
  1052. }
  1053. if (ret != EFI_SUCCESS)
  1054. goto out;
  1055. /* Remove the notify function */
  1056. list_del(&notify_function->link);
  1057. free(notify_function);
  1058. out:
  1059. return EFI_EXIT(ret);
  1060. }
  1061. /**
  1062. * efi_cin_reset() - drain the input buffer
  1063. *
  1064. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  1065. * @extended_verification: allow for exhaustive verification
  1066. * Return: status code
  1067. *
  1068. * This function implements the Reset service of the
  1069. * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
  1070. *
  1071. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1072. * details.
  1073. */
  1074. static efi_status_t EFIAPI efi_cin_reset
  1075. (struct efi_simple_text_input_protocol *this,
  1076. bool extended_verification)
  1077. {
  1078. efi_status_t ret = EFI_SUCCESS;
  1079. EFI_ENTRY("%p, %d", this, extended_verification);
  1080. /* Check parameters */
  1081. if (!this) {
  1082. ret = EFI_INVALID_PARAMETER;
  1083. goto out;
  1084. }
  1085. efi_cin_empty_buffer();
  1086. out:
  1087. return EFI_EXIT(ret);
  1088. }
  1089. /**
  1090. * efi_cin_read_key_stroke() - read key stroke
  1091. *
  1092. * @this: instance of the EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  1093. * @key: key read from console
  1094. * Return: status code
  1095. *
  1096. * This function implements the ReadKeyStroke service of the
  1097. * EFI_SIMPLE_TEXT_INPUT_PROTOCOL.
  1098. *
  1099. * See the Unified Extensible Firmware Interface (UEFI) specification for
  1100. * details.
  1101. */
  1102. static efi_status_t EFIAPI efi_cin_read_key_stroke
  1103. (struct efi_simple_text_input_protocol *this,
  1104. struct efi_input_key *key)
  1105. {
  1106. efi_status_t ret = EFI_SUCCESS;
  1107. EFI_ENTRY("%p, %p", this, key);
  1108. /* Check parameters */
  1109. if (!this || !key) {
  1110. ret = EFI_INVALID_PARAMETER;
  1111. goto out;
  1112. }
  1113. /* We don't do interrupts, so check for timers cooperatively */
  1114. efi_timer_check();
  1115. /* Enable console input after ExitBootServices */
  1116. efi_cin_check();
  1117. if (!key_available) {
  1118. ret = EFI_NOT_READY;
  1119. goto out;
  1120. }
  1121. *key = next_key.key;
  1122. key_available = false;
  1123. efi_con_in.wait_for_key->is_signaled = false;
  1124. out:
  1125. return EFI_EXIT(ret);
  1126. }
  1127. static struct efi_simple_text_input_ex_protocol efi_con_in_ex = {
  1128. .reset = efi_cin_reset_ex,
  1129. .read_key_stroke_ex = efi_cin_read_key_stroke_ex,
  1130. .wait_for_key_ex = NULL,
  1131. .set_state = efi_cin_set_state,
  1132. .register_key_notify = efi_cin_register_key_notify,
  1133. .unregister_key_notify = efi_cin_unregister_key_notify,
  1134. };
  1135. struct efi_simple_text_input_protocol efi_con_in = {
  1136. .reset = efi_cin_reset,
  1137. .read_key_stroke = efi_cin_read_key_stroke,
  1138. .wait_for_key = NULL,
  1139. };
  1140. static struct efi_event *console_timer_event;
  1141. /*
  1142. * efi_console_timer_notify() - notify the console timer event
  1143. *
  1144. * @event: console timer event
  1145. * @context: not used
  1146. */
  1147. static void EFIAPI efi_console_timer_notify(struct efi_event *event,
  1148. void *context)
  1149. {
  1150. EFI_ENTRY("%p, %p", event, context);
  1151. efi_cin_check();
  1152. EFI_EXIT(EFI_SUCCESS);
  1153. }
  1154. /**
  1155. * efi_key_notify() - notify the wait for key event
  1156. *
  1157. * @event: wait for key event
  1158. * @context: not used
  1159. */
  1160. static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
  1161. {
  1162. EFI_ENTRY("%p, %p", event, context);
  1163. efi_cin_check();
  1164. EFI_EXIT(EFI_SUCCESS);
  1165. }
  1166. /**
  1167. * efi_console_register() - install the console protocols
  1168. *
  1169. * This function is called from do_bootefi_exec().
  1170. *
  1171. * Return: status code
  1172. */
  1173. efi_status_t efi_console_register(void)
  1174. {
  1175. efi_status_t r;
  1176. struct efi_device_path *dp;
  1177. /* Install protocols on root node */
  1178. r = efi_install_multiple_protocol_interfaces(&efi_root,
  1179. &efi_guid_text_output_protocol,
  1180. &efi_con_out,
  1181. &efi_guid_text_input_protocol,
  1182. &efi_con_in,
  1183. &efi_guid_text_input_ex_protocol,
  1184. &efi_con_in_ex,
  1185. NULL);
  1186. /* Create console node and install device path protocols */
  1187. if (CONFIG_IS_ENABLED(DM_SERIAL)) {
  1188. dp = efi_dp_from_uart();
  1189. if (!dp)
  1190. goto out_of_memory;
  1191. /* Hook UART up to the device list */
  1192. efi_add_handle(&uart_obj);
  1193. /* Install device path */
  1194. r = efi_add_protocol(&uart_obj, &efi_guid_device_path, dp);
  1195. if (r != EFI_SUCCESS)
  1196. goto out_of_memory;
  1197. }
  1198. /* Create console events */
  1199. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK, efi_key_notify,
  1200. NULL, NULL, &efi_con_in.wait_for_key);
  1201. if (r != EFI_SUCCESS) {
  1202. printf("ERROR: Failed to register WaitForKey event\n");
  1203. return r;
  1204. }
  1205. efi_con_in_ex.wait_for_key_ex = efi_con_in.wait_for_key;
  1206. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  1207. efi_console_timer_notify, NULL, NULL,
  1208. &console_timer_event);
  1209. if (r != EFI_SUCCESS) {
  1210. printf("ERROR: Failed to register console event\n");
  1211. return r;
  1212. }
  1213. /* 5000 ns cycle is sufficient for 2 MBaud */
  1214. r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
  1215. if (r != EFI_SUCCESS)
  1216. printf("ERROR: Failed to set console timer\n");
  1217. return r;
  1218. out_of_memory:
  1219. printf("ERROR: Out of memory\n");
  1220. return r;
  1221. }
  1222. /**
  1223. * efi_console_get_u16_string() - get user input string
  1224. *
  1225. * @cin: protocol interface to EFI_SIMPLE_TEXT_INPUT_PROTOCOL
  1226. * @buf: buffer to store user input string in UTF16
  1227. * @count: number of u16 string including NULL terminator that buf has
  1228. * @filter_func: callback to filter user input
  1229. * @row: row number to locate user input form
  1230. * @col: column number to locate user input form
  1231. * Return: status code
  1232. */
  1233. efi_status_t efi_console_get_u16_string(struct efi_simple_text_input_protocol *cin,
  1234. u16 *buf, efi_uintn_t count,
  1235. efi_console_filter_func filter_func,
  1236. int row, int col)
  1237. {
  1238. efi_status_t ret;
  1239. efi_uintn_t len = 0;
  1240. struct efi_input_key key;
  1241. printf(ANSI_CURSOR_POSITION
  1242. ANSI_CLEAR_LINE_TO_END
  1243. ANSI_CURSOR_SHOW, row, col);
  1244. efi_cin_empty_buffer();
  1245. for (;;) {
  1246. do {
  1247. ret = EFI_CALL(cin->read_key_stroke(cin, &key));
  1248. mdelay(10);
  1249. } while (ret == EFI_NOT_READY);
  1250. if (key.unicode_char == u'\b') {
  1251. if (len > 0)
  1252. buf[--len] = u'\0';
  1253. printf(ANSI_CURSOR_POSITION
  1254. "%ls"
  1255. ANSI_CLEAR_LINE_TO_END, row, col, buf);
  1256. continue;
  1257. } else if (key.unicode_char == u'\r') {
  1258. buf[len] = u'\0';
  1259. return EFI_SUCCESS;
  1260. } else if (key.unicode_char == 0x3 || key.scan_code == 23) {
  1261. return EFI_ABORTED;
  1262. } else if (key.unicode_char < 0x20) {
  1263. /* ignore control codes other than Ctrl+C, '\r' and '\b' */
  1264. continue;
  1265. } else if (key.scan_code != 0) {
  1266. /* only accept single ESC press for cancel */
  1267. continue;
  1268. }
  1269. if (filter_func) {
  1270. if (filter_func(&key) != EFI_SUCCESS)
  1271. continue;
  1272. }
  1273. if (len >= (count - 1))
  1274. continue;
  1275. buf[len] = key.unicode_char;
  1276. len++;
  1277. printf(ANSI_CURSOR_POSITION "%ls", row, col, buf);
  1278. }
  1279. }