efi_console.c 11 KB

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