efi_console.c 27 KB

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