efi_console.c 26 KB

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