efi_console.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 void print_unicode_in_utf8(u16 c)
  123. {
  124. char utf8[MAX_UTF8_PER_UTF16] = { 0 };
  125. utf16_to_utf8((u8 *)utf8, &c, 1);
  126. puts(utf8);
  127. }
  128. static efi_status_t EFIAPI efi_cout_output_string(
  129. struct efi_simple_text_output_protocol *this,
  130. const unsigned short *string)
  131. {
  132. struct cout_mode *mode;
  133. u16 ch;
  134. mode = &efi_cout_modes[efi_con_mode.mode];
  135. EFI_ENTRY("%p, %p", this, string);
  136. for (;(ch = *string); string++) {
  137. print_unicode_in_utf8(ch);
  138. efi_con_mode.cursor_column++;
  139. if (ch == '\n') {
  140. efi_con_mode.cursor_column = 1;
  141. efi_con_mode.cursor_row++;
  142. } else if (efi_con_mode.cursor_column > mode->columns) {
  143. efi_con_mode.cursor_column = 1;
  144. efi_con_mode.cursor_row++;
  145. }
  146. if (efi_con_mode.cursor_row > mode->rows)
  147. efi_con_mode.cursor_row = mode->rows;
  148. }
  149. return EFI_EXIT(EFI_SUCCESS);
  150. }
  151. static efi_status_t EFIAPI efi_cout_test_string(
  152. struct efi_simple_text_output_protocol *this,
  153. const unsigned short *string)
  154. {
  155. EFI_ENTRY("%p, %p", this, string);
  156. return EFI_EXIT(EFI_SUCCESS);
  157. }
  158. static bool cout_mode_matches(struct cout_mode *mode, int rows, int cols)
  159. {
  160. if (!mode->present)
  161. return false;
  162. return (mode->rows == rows) && (mode->columns == cols);
  163. }
  164. static int query_console_serial(int *rows, int *cols)
  165. {
  166. /* Ask the terminal about its size */
  167. int n[3];
  168. u64 timeout;
  169. /* Empty input buffer */
  170. while (tstc())
  171. getc();
  172. printf(ESC"[18t");
  173. /* Check if we have a terminal that understands */
  174. timeout = timer_get_us() + 1000000;
  175. while (!tstc())
  176. if (timer_get_us() > timeout)
  177. return -1;
  178. /* Read {depth,rows,cols} */
  179. if (term_read_reply(n, 3, 't'))
  180. return -1;
  181. *cols = n[2];
  182. *rows = n[1];
  183. return 0;
  184. }
  185. static efi_status_t EFIAPI efi_cout_query_mode(
  186. struct efi_simple_text_output_protocol *this,
  187. unsigned long mode_number, unsigned long *columns,
  188. unsigned long *rows)
  189. {
  190. EFI_ENTRY("%p, %ld, %p, %p", this, mode_number, columns, rows);
  191. if (!console_size_queried) {
  192. const char *stdout_name = env_get("stdout");
  193. int rows, cols;
  194. console_size_queried = true;
  195. if (stdout_name && !strcmp(stdout_name, "vidconsole") &&
  196. IS_ENABLED(CONFIG_DM_VIDEO)) {
  197. struct stdio_dev *stdout_dev =
  198. stdio_get_by_name("vidconsole");
  199. struct udevice *dev = stdout_dev->priv;
  200. struct vidconsole_priv *priv =
  201. dev_get_uclass_priv(dev);
  202. rows = priv->rows;
  203. cols = priv->cols;
  204. } else if (query_console_serial(&rows, &cols)) {
  205. goto out;
  206. }
  207. /* Test if we can have Mode 1 */
  208. if (cols >= 80 && rows >= 50) {
  209. efi_cout_modes[1].present = 1;
  210. efi_con_mode.max_mode = 2;
  211. }
  212. /*
  213. * Install our mode as mode 2 if it is different
  214. * than mode 0 or 1 and set it as the currently selected mode
  215. */
  216. if (!cout_mode_matches(&efi_cout_modes[0], rows, cols) &&
  217. !cout_mode_matches(&efi_cout_modes[1], rows, cols)) {
  218. efi_cout_modes[EFI_COUT_MODE_2].columns = cols;
  219. efi_cout_modes[EFI_COUT_MODE_2].rows = rows;
  220. efi_cout_modes[EFI_COUT_MODE_2].present = 1;
  221. efi_con_mode.max_mode = EFI_MAX_COUT_MODE;
  222. efi_con_mode.mode = EFI_COUT_MODE_2;
  223. }
  224. }
  225. if (mode_number >= efi_con_mode.max_mode)
  226. return EFI_EXIT(EFI_UNSUPPORTED);
  227. if (efi_cout_modes[mode_number].present != 1)
  228. return EFI_EXIT(EFI_UNSUPPORTED);
  229. out:
  230. if (columns)
  231. *columns = efi_cout_modes[mode_number].columns;
  232. if (rows)
  233. *rows = efi_cout_modes[mode_number].rows;
  234. return EFI_EXIT(EFI_SUCCESS);
  235. }
  236. static efi_status_t EFIAPI efi_cout_set_mode(
  237. struct efi_simple_text_output_protocol *this,
  238. unsigned long mode_number)
  239. {
  240. EFI_ENTRY("%p, %ld", this, mode_number);
  241. if (mode_number > efi_con_mode.max_mode)
  242. return EFI_EXIT(EFI_UNSUPPORTED);
  243. efi_con_mode.mode = mode_number;
  244. efi_con_mode.cursor_column = 0;
  245. efi_con_mode.cursor_row = 0;
  246. return EFI_EXIT(EFI_SUCCESS);
  247. }
  248. static efi_status_t EFIAPI efi_cout_set_attribute(
  249. struct efi_simple_text_output_protocol *this,
  250. unsigned long attribute)
  251. {
  252. EFI_ENTRY("%p, %lx", this, attribute);
  253. /* Just ignore attributes (colors) for now */
  254. return EFI_EXIT(EFI_UNSUPPORTED);
  255. }
  256. static efi_status_t EFIAPI efi_cout_clear_screen(
  257. struct efi_simple_text_output_protocol *this)
  258. {
  259. EFI_ENTRY("%p", this);
  260. printf(ESC"[2J");
  261. return EFI_EXIT(EFI_SUCCESS);
  262. }
  263. static efi_status_t EFIAPI efi_cout_set_cursor_position(
  264. struct efi_simple_text_output_protocol *this,
  265. unsigned long column, unsigned long row)
  266. {
  267. EFI_ENTRY("%p, %ld, %ld", this, column, row);
  268. printf(ESC"[%d;%df", (int)row, (int)column);
  269. efi_con_mode.cursor_column = column;
  270. efi_con_mode.cursor_row = row;
  271. return EFI_EXIT(EFI_SUCCESS);
  272. }
  273. static efi_status_t EFIAPI efi_cout_enable_cursor(
  274. struct efi_simple_text_output_protocol *this,
  275. bool enable)
  276. {
  277. EFI_ENTRY("%p, %d", this, enable);
  278. printf(ESC"[?25%c", enable ? 'h' : 'l');
  279. return EFI_EXIT(EFI_SUCCESS);
  280. }
  281. const struct efi_simple_text_output_protocol efi_con_out = {
  282. .reset = efi_cout_reset,
  283. .output_string = efi_cout_output_string,
  284. .test_string = efi_cout_test_string,
  285. .query_mode = efi_cout_query_mode,
  286. .set_mode = efi_cout_set_mode,
  287. .set_attribute = efi_cout_set_attribute,
  288. .clear_screen = efi_cout_clear_screen,
  289. .set_cursor_position = efi_cout_set_cursor_position,
  290. .enable_cursor = efi_cout_enable_cursor,
  291. .mode = (void*)&efi_con_mode,
  292. };
  293. static efi_status_t EFIAPI efi_cin_reset(
  294. struct efi_simple_input_interface *this,
  295. bool extended_verification)
  296. {
  297. EFI_ENTRY("%p, %d", this, extended_verification);
  298. return EFI_EXIT(EFI_UNSUPPORTED);
  299. }
  300. static efi_status_t EFIAPI efi_cin_read_key_stroke(
  301. struct efi_simple_input_interface *this,
  302. struct efi_input_key *key)
  303. {
  304. struct efi_input_key pressed_key = {
  305. .scan_code = 0,
  306. .unicode_char = 0,
  307. };
  308. char ch;
  309. EFI_ENTRY("%p, %p", this, key);
  310. /* We don't do interrupts, so check for timers cooperatively */
  311. efi_timer_check();
  312. if (!tstc()) {
  313. /* No key pressed */
  314. return EFI_EXIT(EFI_NOT_READY);
  315. }
  316. ch = getc();
  317. if (ch == cESC) {
  318. /* Escape Sequence */
  319. ch = getc();
  320. switch (ch) {
  321. case cESC: /* ESC */
  322. pressed_key.scan_code = 23;
  323. break;
  324. case 'O': /* F1 - F4 */
  325. pressed_key.scan_code = getc() - 'P' + 11;
  326. break;
  327. case 'a'...'z':
  328. ch = ch - 'a';
  329. break;
  330. case '[':
  331. ch = getc();
  332. switch (ch) {
  333. case 'A'...'D': /* up, down right, left */
  334. pressed_key.scan_code = ch - 'A' + 1;
  335. break;
  336. case 'F': /* End */
  337. pressed_key.scan_code = 6;
  338. break;
  339. case 'H': /* Home */
  340. pressed_key.scan_code = 5;
  341. break;
  342. case '1': /* F5 - F8 */
  343. pressed_key.scan_code = getc() - '0' + 11;
  344. getc();
  345. break;
  346. case '2': /* F9 - F12 */
  347. pressed_key.scan_code = getc() - '0' + 19;
  348. getc();
  349. break;
  350. case '3': /* DEL */
  351. pressed_key.scan_code = 8;
  352. getc();
  353. break;
  354. }
  355. break;
  356. }
  357. } else if (ch == 0x7f) {
  358. /* Backspace */
  359. ch = 0x08;
  360. }
  361. pressed_key.unicode_char = ch;
  362. *key = pressed_key;
  363. return EFI_EXIT(EFI_SUCCESS);
  364. }
  365. struct efi_simple_input_interface efi_con_in = {
  366. .reset = efi_cin_reset,
  367. .read_key_stroke = efi_cin_read_key_stroke,
  368. .wait_for_key = NULL,
  369. };
  370. static struct efi_event *console_timer_event;
  371. static void EFIAPI efi_key_notify(struct efi_event *event, void *context)
  372. {
  373. }
  374. static void EFIAPI efi_console_timer_notify(struct efi_event *event,
  375. void *context)
  376. {
  377. EFI_ENTRY("%p, %p", event, context);
  378. if (tstc()) {
  379. efi_con_in.wait_for_key->signaled = 1;
  380. efi_signal_event(efi_con_in.wait_for_key);
  381. }
  382. EFI_EXIT(EFI_SUCCESS);
  383. }
  384. static struct efi_object efi_console_control_obj =
  385. EFI_PROTOCOL_OBJECT(efi_guid_console_control, &efi_console_control);
  386. static struct efi_object efi_console_output_obj =
  387. EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL_GUID, &efi_con_out);
  388. static struct efi_object efi_console_input_obj =
  389. EFI_PROTOCOL_OBJECT(EFI_SIMPLE_TEXT_INPUT_PROTOCOL_GUID, &efi_con_in);
  390. /* This gets called from do_bootefi_exec(). */
  391. int efi_console_register(void)
  392. {
  393. efi_status_t r;
  394. /* Hook up to the device list */
  395. list_add_tail(&efi_console_control_obj.link, &efi_obj_list);
  396. list_add_tail(&efi_console_output_obj.link, &efi_obj_list);
  397. list_add_tail(&efi_console_input_obj.link, &efi_obj_list);
  398. r = efi_create_event(EVT_NOTIFY_WAIT, TPL_CALLBACK,
  399. efi_key_notify, NULL, &efi_con_in.wait_for_key);
  400. if (r != EFI_SUCCESS) {
  401. printf("ERROR: Failed to register WaitForKey event\n");
  402. return r;
  403. }
  404. r = efi_create_event(EVT_TIMER | EVT_NOTIFY_SIGNAL, TPL_CALLBACK,
  405. efi_console_timer_notify, NULL,
  406. &console_timer_event);
  407. if (r != EFI_SUCCESS) {
  408. printf("ERROR: Failed to register console event\n");
  409. return r;
  410. }
  411. /* 5000 ns cycle is sufficient for 2 MBaud */
  412. r = efi_set_timer(console_timer_event, EFI_TIMER_PERIODIC, 50);
  413. if (r != EFI_SUCCESS)
  414. printf("ERROR: Failed to set console timer\n");
  415. return r;
  416. }