efi_console.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * EFI application console interface
  3. *
  4. * Copyright (c) 2016 Alexander Graf
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. #include <common.h>
  9. #include <charset.h>
  10. #include <dm/device.h>
  11. #include <efi_loader.h>
  12. #include <stdio_dev.h>
  13. #include <video_console.h>
  14. static bool console_size_queried;
  15. #define EFI_COUT_MODE_2 2
  16. #define EFI_MAX_COUT_MODE 3
  17. struct cout_mode {
  18. unsigned long columns;
  19. unsigned long rows;
  20. int present;
  21. };
  22. static struct cout_mode efi_cout_modes[] = {
  23. /* EFI Mode 0 is 80x25 and always present */
  24. {
  25. .columns = 80,
  26. .rows = 25,
  27. .present = 1,
  28. },
  29. /* EFI Mode 1 is always 80x50 */
  30. {
  31. .columns = 80,
  32. .rows = 50,
  33. .present = 0,
  34. },
  35. /* Value are unknown until we query the console */
  36. {
  37. .columns = 0,
  38. .rows = 0,
  39. .present = 0,
  40. },
  41. };
  42. const efi_guid_t efi_guid_console_control = CONSOLE_CONTROL_GUID;
  43. const efi_guid_t efi_guid_text_output_protocol =
  44. EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID;
  45. const efi_guid_t efi_guid_text_input_protocol =
  46. EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID;
  47. #define cESC '\x1b'
  48. #define ESC "\x1b"
  49. static efi_status_t EFIAPI efi_cin_get_mode(
  50. struct efi_console_control_protocol *this,
  51. int *mode, char *uga_exists, char *std_in_locked)
  52. {
  53. EFI_ENTRY("%p, %p, %p, %p", this, mode, uga_exists, std_in_locked);
  54. if (mode)
  55. *mode = EFI_CONSOLE_MODE_TEXT;
  56. if (uga_exists)
  57. *uga_exists = 0;
  58. if (std_in_locked)
  59. *std_in_locked = 0;
  60. return EFI_EXIT(EFI_SUCCESS);
  61. }
  62. static efi_status_t EFIAPI efi_cin_set_mode(
  63. struct efi_console_control_protocol *this, int mode)
  64. {
  65. EFI_ENTRY("%p, %d", this, mode);
  66. return EFI_EXIT(EFI_UNSUPPORTED);
  67. }
  68. static efi_status_t EFIAPI efi_cin_lock_std_in(
  69. struct efi_console_control_protocol *this,
  70. uint16_t *password)
  71. {
  72. EFI_ENTRY("%p, %p", this, password);
  73. return EFI_EXIT(EFI_UNSUPPORTED);
  74. }
  75. struct efi_console_control_protocol efi_console_control = {
  76. .get_mode = efi_cin_get_mode,
  77. .set_mode = efi_cin_set_mode,
  78. .lock_std_in = efi_cin_lock_std_in,
  79. };
  80. /* Default to mode 0 */
  81. static struct simple_text_output_mode efi_con_mode = {
  82. .max_mode = 1,
  83. .mode = 0,
  84. .attribute = 0,
  85. .cursor_column = 0,
  86. .cursor_row = 0,
  87. .cursor_visible = 1,
  88. };
  89. static int term_read_reply(int *n, int maxnum, char end_char)
  90. {
  91. char c;
  92. int i = 0;
  93. c = getc();
  94. if (c != cESC)
  95. return -1;
  96. c = getc();
  97. if (c != '[')
  98. return -1;
  99. n[0] = 0;
  100. while (1) {
  101. c = getc();
  102. if (c == ';') {
  103. i++;
  104. if (i >= maxnum)
  105. return -1;
  106. n[i] = 0;
  107. continue;
  108. } else if (c == end_char) {
  109. break;
  110. } else if (c > '9' || c < '0') {
  111. return -1;
  112. }
  113. /* Read one more decimal position */
  114. n[i] *= 10;
  115. n[i] += c - '0';
  116. }
  117. return 0;
  118. }
  119. static efi_status_t EFIAPI efi_cout_reset(
  120. struct efi_simple_text_output_protocol *this,
  121. char extended_verification)
  122. {
  123. EFI_ENTRY("%p, %d", this, extended_verification);
  124. return EFI_EXIT(EFI_UNSUPPORTED);
  125. }
  126. static efi_status_t EFIAPI efi_cout_output_string(
  127. struct efi_simple_text_output_protocol *this,
  128. const efi_string_t string)
  129. {
  130. struct simple_text_output_mode *con = &efi_con_mode;
  131. struct cout_mode *mode = &efi_cout_modes[con->mode];
  132. EFI_ENTRY("%p, %p", this, string);
  133. unsigned int n16 = utf16_strlen(string);
  134. char buf[MAX_UTF8_PER_UTF16 * n16 + 1];
  135. char *p;
  136. *utf16_to_utf8((u8 *)buf, string, n16) = '\0';
  137. fputs(stdout, buf);
  138. for (p = buf; *p; p++) {
  139. switch (*p) {
  140. case '\r': /* carriage-return */
  141. con->cursor_column = 0;
  142. break;
  143. case '\n': /* newline */
  144. con->cursor_column = 0;
  145. con->cursor_row++;
  146. break;
  147. case '\t': /* tab, assume 8 char align */
  148. break;
  149. case '\b': /* backspace */
  150. con->cursor_column = max(0, con->cursor_column - 1);
  151. break;
  152. default:
  153. con->cursor_column++;
  154. break;
  155. }
  156. if (con->cursor_column >= mode->columns) {
  157. con->cursor_column = 0;
  158. con->cursor_row++;
  159. }
  160. con->cursor_row = min(con->cursor_row, (s32)mode->rows - 1);
  161. }
  162. return EFI_EXIT(EFI_SUCCESS);
  163. }
  164. static efi_status_t EFIAPI efi_cout_test_string(
  165. struct efi_simple_text_output_protocol *this,
  166. const efi_string_t string)
  167. {
  168. EFI_ENTRY("%p, %p", this, string);
  169. return EFI_EXIT(EFI_SUCCESS);
  170. }
  171. static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
  172. {
  173. if (!mode->present)
  174. return false;
  175. return (mode->rows == rows) && (mode->columns == cols);
  176. }
  177. static int query_console_serial(int *rows, int *cols)
  178. {
  179. /* Ask the terminal about its size */
  180. int n[3];
  181. u64 timeout;
  182. /* Empty input buffer */
  183. while (tstc())
  184. getc();
  185. printf(ESC"[18t");
  186. /* Check if we have a terminal that understands */
  187. timeout = timer_get_us() + 1000000;
  188. while (!tstc())
  189. if (timer_get_us() > timeout)
  190. return -1;
  191. /* Read {depth,rows,cols} */
  192. if (term_read_reply(n, 3, 't'))
  193. return -1;
  194. *cols = n[2];
  195. *rows = n[1];
  196. return 0;
  197. }
  198. static efi_status_t EFIAPI efi_cout_query_mode(
  199. struct efi_simple_text_output_protocol *this,
  200. unsigned long mode_number, unsigned long *columns,
  201. unsigned long *rows)
  202. {
  203. EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
  204. if (!console_size_queried) {
  205. const char *stdout_name = env_get("stdout");
  206. int rows, cols;
  207. console_size_queried = true;
  208. if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
  209. IS_ENABLED(CONFIG_DM_VIDEO)) {
  210. struct stdio_dev *stdout_dev =
  211. stdio_get_by_name("vidconsole");
  212. struct udevice *dev = stdout_dev->priv;
  213. struct vidconsole_priv *priv =
  214. dev_get_uclass_priv(dev);
  215. rows = priv->rows;
  216. cols = priv->cols;
  217. } else if (query_console_serial(&rows, &cols)) {
  218. goto out;
  219. }
  220. /* Test if we can have Mode 1 */
  221. if (cols >= 80 && rows >= 50) {
  222. efi_cout_modes[1].present = 1;
  223. efi_con_mode.max_mode = 2;
  224. }
  225. /*
  226. * Install our mode as mode 2 if it is different
  227. * than mode 0 or 1 and set it as the currently selected mode
  228. */
  229. if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
  230. !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
  231. efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
  232. efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
  233. efi_cout_modes[EFI_COUT_MODE_2].present = 1;
  234. efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
  235. efi_con_mode.mode = EFI_COUT_MODE_2;
  236. }
  237. }
  238. if (mode_number >= efi_con_mode.max_mode)
  239. return EFI_EXIT(EFI_UNSUPPORTED);
  240. if (efi_cout_modes[mode_number].present != 1)
  241. return EFI_EXIT(EFI_UNSUPPORTED);
  242. out:
  243. if (columns)
  244. *columns = efi_cout_modes[mode_number].columns;
  245. if (rows)
  246. *rows = efi_cout_modes[mode_number].rows;
  247. return EFI_EXIT(EFI_SUCCESS);
  248. }
  249. static efi_status_t EFIAPI efi_cout_set_mode(
  250. struct efi_simple_text_output_protocol *this,
  251. unsigned long mode_number)
  252. {
  253. EFI_ENTRY("%p, %ld", this, mode_number);
  254. if (mode_number > efi_con_mode.max_mode)
  255. return EFI_EXIT(EFI_UNSUPPORTED);
  256. efi_con_mode.mode = mode_number;
  257. efi_con_mode.cursor_column = 0;
  258. efi_con_mode.cursor_row = 0;
  259. return EFI_EXIT(EFI_SUCCESS);
  260. }
  261. static const struct {
  262. unsigned int fg;
  263. unsigned int bg;
  264. } color[] = {
  265. { 30, 40 }, /* 0: black */
  266. { 34, 44 }, /* 1: blue */
  267. { 32, 42 }, /* 2: green */
  268. { 36, 46 }, /* 3: cyan */
  269. { 31, 41 }, /* 4: red */
  270. { 35, 45 }, /* 5: magenta */
  271. { 33, 43 }, /* 6: brown, map to yellow as edk2 does*/
  272. { 37, 47 }, /* 7: light grey, map to white */
  273. };
  274. /* See EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL.SetAttribute(). */
  275. static efi_status_t EFIAPI efi_cout_set_attribute(
  276. struct efi_simple_text_output_protocol *this,
  277. unsigned long attribute)
  278. {
  279. unsigned int bold = EFI_ATTR_BOLD(attribute);
  280. unsigned int fg = EFI_ATTR_FG(attribute);
  281. unsigned int bg = EFI_ATTR_BG(attribute);
  282. EFI_ENTRY("%p, %lx", this, attribute);
  283. if (attribute)
  284. printf(ESC"[%u;%u;%um", bold, color[fg].fg, color[bg].bg);
  285. else
  286. printf(ESC"[0;37;40m");
  287. return EFI_EXIT(EFI_SUCCESS);
  288. }
  289. static efi_status_t EFIAPI efi_cout_clear_screen(
  290. struct efi_simple_text_output_protocol *this)
  291. {
  292. EFI_ENTRY("%p", this);
  293. printf(ESC"[2J");
  294. return EFI_EXIT(EFI_SUCCESS);
  295. }
  296. static efi_status_t EFIAPI efi_cout_set_cursor_position(
  297. struct efi_simple_text_output_protocol *this,
  298. unsigned long column, unsigned long row)
  299. {
  300. EFI_ENTRY("%p, %ld, %ld", this, column, row);
  301. printf(ESC"[%d;%df", (int)row, (int)column);
  302. efi_con_mode.cursor_column = column;
  303. efi_con_mode.cursor_row = row;
  304. return EFI_EXIT(EFI_SUCCESS);
  305. }
  306. static efi_status_t EFIAPI efi_cout_enable_cursor(
  307. struct efi_simple_text_output_protocol *this,
  308. bool enable)
  309. {
  310. EFI_ENTRY("%p, %d", this, enable);
  311. printf(ESC"[?25%c", enable ? 'h' : 'l');
  312. return EFI_EXIT(EFI_SUCCESS);
  313. }
  314. struct efi_simple_text_output_protocol efi_con_out = {
  315. .reset = efi_cout_reset,
  316. .output_string = efi_cout_output_string,
  317. .test_string = efi_cout_test_string,
  318. .query_mode = efi_cout_query_mode,
  319. .set_mode = efi_cout_set_mode,
  320. .set_attribute = efi_cout_set_attribute,
  321. .clear_screen = efi_cout_clear_screen,
  322. .set_cursor_position = efi_cout_set_cursor_position,
  323. .enable_cursor = efi_cout_enable_cursor,
  324. .mode = (void*)&efi_con_mode,
  325. };
  326. static efi_status_t EFIAPI efi_cin_reset(
  327. struct efi_simple_input_interface *this,
  328. bool extended_verification)
  329. {
  330. EFI_ENTRY("%p, %d", this, extended_verification);
  331. return EFI_EXIT(EFI_UNSUPPORTED);
  332. }
  333. static efi_status_t EFIAPI efi_cin_read_key_stroke(
  334. struct efi_simple_input_interface *this,
  335. struct efi_input_key *key)
  336. {
  337. struct efi_input_key pressed_key = {
  338. .scan_code = 0,
  339. .unicode_char = 0,
  340. };
  341. char ch;
  342. EFI_ENTRY("%p, %p", this, key);
  343. /* We don't do interrupts, so check for timers cooperatively */
  344. efi_timer_check();
  345. if (!tstc()) {
  346. /* No key pressed */
  347. return EFI_EXIT(EFI_NOT_READY);
  348. }
  349. ch = getc();
  350. if (ch == cESC) {
  351. /* Escape Sequence */
  352. ch = getc();
  353. switch (ch) {
  354. case cESC: /* ESC */
  355. pressed_key.scan_code = 23;
  356. break;
  357. case 'O': /* F1 - F4 */
  358. pressed_key.scan_code = getc() - 'P' + 11;
  359. break;
  360. case 'a'...'z':
  361. ch = ch - 'a';
  362. break;
  363. case '[':
  364. ch = getc();
  365. switch (ch) {
  366. case 'A'...'D': /* up, down right, left */
  367. pressed_key.scan_code = ch - 'A' + 1;
  368. break;
  369. case 'F': /* End */
  370. pressed_key.scan_code = 6;
  371. break;
  372. case 'H': /* Home */
  373. pressed_key.scan_code = 5;
  374. break;
  375. case '1': /* F5 - F8 */
  376. pressed_key.scan_code = getc() - '0' + 11;
  377. getc();
  378. break;
  379. case '2': /* F9 - F12 */
  380. pressed_key.scan_code = getc() - '0' + 19;
  381. getc();
  382. break;
  383. case '3': /* DEL */
  384. pressed_key.scan_code = 8;
  385. getc();
  386. break;
  387. }
  388. break;
  389. }
  390. } else if (ch == 0x7f) {
  391. /* Backspace */
  392. ch = 0x08;
  393. }
  394. pressed_key.unicode_char = ch;
  395. *key = pressed_key;
  396. return EFI_EXIT(EFI_SUCCESS);
  397. }
  398. struct efi_simple_input_interface efi_con_in = {
  399. .reset = efi_cin_reset,
  400. .read_key_stroke = efi_cin_read_key_stroke,
  401. .wait_for_key = NULL,
  402. };
  403. static struct efi_event *console_timer_event;
  404. static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
  405. {
  406. }
  407. static void EFIAPI efi_console_timer_notify(struct efi_event *event,
  408. void *context)
  409. {
  410. EFI_ENTRY("%p, %p", event, context);
  411. if (tstc()) {
  412. efi_con_in.wait_for_key->is_signaled = true;
  413. efi_signal_event(efi_con_in.wait_for_key);
  414. }
  415. EFI_EXIT(EFI_SUCCESS);
  416. }
  417. /* This gets called from do_bootefi_exec(). */
  418. int efi_console_register(void)
  419. {
  420. efi_status_t r;
  421. struct efi_object *efi_console_control_obj;
  422. struct efi_object *efi_console_output_obj;
  423. struct efi_object *efi_console_input_obj;
  424. /* Create handles */
  425. r = efi_create_handle((efi_handle_t *)&efi_console_control_obj);
  426. if (r != EFI_SUCCESS)
  427. goto out_of_memory;
  428. r = efi_add_protocol(efi_console_control_obj->handle,
  429. &efi_guid_console_control, &efi_console_control);
  430. if (r != EFI_SUCCESS)
  431. goto out_of_memory;
  432. r = efi_create_handle((efi_handle_t *)&efi_console_output_obj);
  433. if (r != EFI_SUCCESS)
  434. goto out_of_memory;
  435. r = efi_add_protocol(efi_console_output_obj->handle,
  436. &efi_guid_text_output_protocol, &efi_con_out);
  437. if (r != EFI_SUCCESS)
  438. goto out_of_memory;
  439. r = efi_create_handle((efi_handle_t *)&efi_console_input_obj);
  440. if (r != EFI_SUCCESS)
  441. goto out_of_memory;
  442. r = efi_add_protocol(efi_console_input_obj->handle,
  443. &efi_guid_text_input_protocol, &efi_con_in);
  444. if (r != EFI_SUCCESS)
  445. goto out_of_memory;
  446. /* Create console events */
  447. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
  448. efi_key_notify, NULL, &efi_con_in.wait_for_key);
  449. if (r != EFI_SUCCESS) {
  450. printf("ERROR: Failed to register WaitForKey event\n");
  451. return r;
  452. }
  453. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  454. efi_console_timer_notify, NULL,
  455. &console_timer_event);
  456. if (r != EFI_SUCCESS) {
  457. printf("ERROR: Failed to register console event\n");
  458. return r;
  459. }
  460. /* 5000 ns cycle is sufficient for 2 MBaud */
  461. r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
  462. if (r != EFI_SUCCESS)
  463. printf("ERROR: Failed to set console timer\n");
  464. return r;
  465. out_of_memory:
  466. printf("ERROR: Out of meemory\n");
  467. return r;
  468. }