efi_console.c 25 KB

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