efi_console.c 12 KB

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