efi_console.c 31 KB

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