efi_console.c 9.1 KB

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