efi_console.c 32 KB

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