7d31095b1ef8d01886a0516343f2e5c5601265dd
[grub.git] / grub-core / term / efi / console.c
1 /*
2  *  GRUB  --  GRand Unified Bootloader
3  *  Copyright (C) 2006,2007,2008  Free Software Foundation, Inc.
4  *
5  *  GRUB is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation, either version 3 of the License, or
8  *  (at your option) any later version.
9  *
10  *  GRUB is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <grub/term.h>
20 #include <grub/misc.h>
21 #include <grub/types.h>
22 #include <grub/err.h>
23 #include <grub/efi/efi.h>
24 #include <grub/efi/api.h>
25 #include <grub/efi/console.h>
26
27 static grub_uint32_t
28 map_char (grub_uint32_t c)
29 {
30   /* Map some unicode characters to the EFI character.  */
31   switch (c)
32     {
33     case GRUB_UNICODE_LEFTARROW:
34       c = GRUB_UNICODE_BLACK_LEFT_TRIANGLE;
35       break;
36     case GRUB_UNICODE_UPARROW:
37       c = GRUB_UNICODE_BLACK_UP_TRIANGLE;
38       break;
39     case GRUB_UNICODE_RIGHTARROW:
40       c = GRUB_UNICODE_BLACK_RIGHT_TRIANGLE;
41       break;
42     case GRUB_UNICODE_DOWNARROW:
43       c = GRUB_UNICODE_BLACK_DOWN_TRIANGLE;
44       break;
45     case GRUB_UNICODE_HLINE:
46       c = GRUB_UNICODE_LIGHT_HLINE;
47       break;
48     case GRUB_UNICODE_VLINE:
49       c = GRUB_UNICODE_LIGHT_VLINE;
50       break;
51     case GRUB_UNICODE_CORNER_UL:
52       c = GRUB_UNICODE_LIGHT_CORNER_UL;
53       break;
54     case GRUB_UNICODE_CORNER_UR:
55       c = GRUB_UNICODE_LIGHT_CORNER_UR;
56       break;
57     case GRUB_UNICODE_CORNER_LL:
58       c = GRUB_UNICODE_LIGHT_CORNER_LL;
59       break;
60     case GRUB_UNICODE_CORNER_LR:
61       c = GRUB_UNICODE_LIGHT_CORNER_LR;
62       break;
63     }
64
65   return c;
66 }
67
68 static void
69 grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
70                       const struct grub_unicode_glyph *c)
71 {
72   grub_efi_char16_t str[2 + 30];
73   grub_efi_simple_text_output_interface_t *o;
74   unsigned i, j;
75
76   if (grub_efi_is_finished)
77     return;
78
79   o = grub_efi_system_table->con_out;
80
81   /* For now, do not try to use a surrogate pair.  */
82   if (c->base > 0xffff)
83     str[0] = '?';
84   else
85     str[0] = (grub_efi_char16_t)  map_char (c->base & 0xffff);
86   j = 1;
87   for (i = 0; i < c->ncomb && j + 1 < ARRAY_SIZE (str); i++)
88     if (c->base < 0xffff)
89       str[j++] = grub_unicode_get_comb (c)[i].code;
90   str[j] = 0;
91
92   /* Should this test be cached?  */
93   if ((c->base > 0x7f || c->ncomb)
94       && efi_call_2 (o->test_string, o, str) != GRUB_EFI_SUCCESS)
95     return;
96
97   efi_call_2 (o->output_string, o, str);
98 }
99
100 const unsigned efi_codes[] =
101   {
102     0, GRUB_TERM_KEY_UP, GRUB_TERM_KEY_DOWN, GRUB_TERM_KEY_RIGHT,
103     GRUB_TERM_KEY_LEFT, GRUB_TERM_KEY_HOME, GRUB_TERM_KEY_END, GRUB_TERM_KEY_INSERT,
104     GRUB_TERM_KEY_DC, GRUB_TERM_KEY_PPAGE, GRUB_TERM_KEY_NPAGE, GRUB_TERM_KEY_F1,
105     GRUB_TERM_KEY_F2, GRUB_TERM_KEY_F3, GRUB_TERM_KEY_F4, GRUB_TERM_KEY_F5,
106     GRUB_TERM_KEY_F6, GRUB_TERM_KEY_F7, GRUB_TERM_KEY_F8, GRUB_TERM_KEY_F9,
107     GRUB_TERM_KEY_F10, GRUB_TERM_KEY_F11, GRUB_TERM_KEY_F12, '\e'
108   };
109
110 static int
111 grub_efi_translate_key (grub_efi_input_key_t key)
112 {
113   if (key.scan_code == 0)
114     {
115       /* Some firmware implementations use VT100-style codes against the spec.
116          This is especially likely if driven by serial.
117        */
118       if (key.unicode_char < 0x20 && key.unicode_char != 0
119           && key.unicode_char != '\t' && key.unicode_char != '\b'
120           && key.unicode_char != '\n' && key.unicode_char != '\r')
121         return GRUB_TERM_CTRL | (key.unicode_char - 1 + 'a');
122       else
123         return key.unicode_char;
124     }
125   else if (key.scan_code < ARRAY_SIZE (efi_codes))
126     return efi_codes[key.scan_code];
127
128   if ((key.unicode_char >= 0x20 && key.unicode_char <= 0x7f)
129       || key.unicode_char == '\t' || key.unicode_char == '\b'
130       || key.unicode_char == '\n' || key.unicode_char == '\r')
131     return key.unicode_char;
132
133   return GRUB_TERM_NO_KEY;
134 }
135
136 static int
137 grub_console_getkey_con (struct grub_term_input *term __attribute__ ((unused)))
138 {
139   grub_efi_simple_input_interface_t *i;
140   grub_efi_input_key_t key;
141   grub_efi_status_t status;
142
143   i = grub_efi_system_table->con_in;
144   status = efi_call_2 (i->read_key_stroke, i, &key);
145
146   if (status != GRUB_EFI_SUCCESS)
147     return GRUB_TERM_NO_KEY;
148
149   return grub_efi_translate_key(key);
150 }
151
152 static int
153 grub_console_getkey_ex(struct grub_term_input *term)
154 {
155   grub_efi_key_data_t key_data;
156   grub_efi_status_t status;
157   grub_efi_uint32_t kss;
158   int key = -1;
159
160   grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
161
162   status = efi_call_2 (text_input->read_key_stroke, text_input, &key_data);
163
164   if (status != GRUB_EFI_SUCCESS)
165     return GRUB_TERM_NO_KEY;
166
167   kss = key_data.key_state.key_shift_state;
168   key = grub_efi_translate_key(key_data.key);
169
170   if (key == GRUB_TERM_NO_KEY)
171     return GRUB_TERM_NO_KEY;
172
173   if (kss & GRUB_EFI_SHIFT_STATE_VALID)
174     {
175       if ((kss & GRUB_EFI_LEFT_SHIFT_PRESSED
176            || kss & GRUB_EFI_RIGHT_SHIFT_PRESSED)
177           && (key & GRUB_TERM_EXTENDED))
178         key |= GRUB_TERM_SHIFT;
179       if (kss & GRUB_EFI_LEFT_ALT_PRESSED || kss & GRUB_EFI_RIGHT_ALT_PRESSED)
180         key |= GRUB_TERM_ALT;
181       if (kss & GRUB_EFI_LEFT_CONTROL_PRESSED
182           || kss & GRUB_EFI_RIGHT_CONTROL_PRESSED)
183         key |= GRUB_TERM_CTRL;
184     }
185
186   return key;
187 }
188
189 static grub_err_t
190 grub_efi_console_input_init (struct grub_term_input *term)
191 {
192   grub_efi_guid_t text_input_ex_guid =
193     GRUB_EFI_SIMPLE_TEXT_INPUT_EX_PROTOCOL_GUID;
194
195   if (grub_efi_is_finished)
196     return 0;
197
198   grub_efi_simple_text_input_ex_interface_t *text_input = term->data;
199   if (text_input)
200     return 0;
201
202   text_input = grub_efi_open_protocol(grub_efi_system_table->console_in_handler,
203                                       &text_input_ex_guid,
204                                       GRUB_EFI_OPEN_PROTOCOL_GET_PROTOCOL);
205   term->data = (void *)text_input;
206
207   return 0;
208 }
209
210 static int
211 grub_console_getkey (struct grub_term_input *term)
212 {
213   if (grub_efi_is_finished)
214     return 0;
215
216   if (term->data)
217     return grub_console_getkey_ex(term);
218   else
219     return grub_console_getkey_con(term);
220 }
221
222 static struct grub_term_coordinate
223 grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
224 {
225   grub_efi_simple_text_output_interface_t *o;
226   grub_efi_uintn_t columns, rows;
227
228   o = grub_efi_system_table->con_out;
229   if (grub_efi_is_finished || efi_call_4 (o->query_mode, o, o->mode->mode,
230                                           &columns, &rows) != GRUB_EFI_SUCCESS)
231     {
232       /* Why does this fail?  */
233       columns = 80;
234       rows = 25;
235     }
236
237   return (struct grub_term_coordinate) { columns, rows };
238 }
239
240 static struct grub_term_coordinate
241 grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
242 {
243   grub_efi_simple_text_output_interface_t *o;
244
245   if (grub_efi_is_finished)
246     return (struct grub_term_coordinate) { 0, 0 };
247
248   o = grub_efi_system_table->con_out;
249   return (struct grub_term_coordinate) { o->mode->cursor_column, o->mode->cursor_row };
250 }
251
252 static void
253 grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
254                      struct grub_term_coordinate pos)
255 {
256   grub_efi_simple_text_output_interface_t *o;
257
258   if (grub_efi_is_finished)
259     return;
260
261   o = grub_efi_system_table->con_out;
262   efi_call_3 (o->set_cursor_position, o, pos.x, pos.y);
263 }
264
265 static void
266 grub_console_cls (struct grub_term_output *term __attribute__ ((unused)))
267 {
268   grub_efi_simple_text_output_interface_t *o;
269   grub_efi_int32_t orig_attr;
270
271   if (grub_efi_is_finished)
272     return;
273
274   o = grub_efi_system_table->con_out;
275   orig_attr = o->mode->attribute;
276   efi_call_2 (o->set_attributes, o, GRUB_EFI_BACKGROUND_BLACK);
277   efi_call_1 (o->clear_screen, o);
278   efi_call_2 (o->set_attributes, o, orig_attr);
279 }
280
281 static void
282 grub_console_setcolorstate (struct grub_term_output *term
283                             __attribute__ ((unused)),
284                             grub_term_color_state state)
285 {
286   grub_efi_simple_text_output_interface_t *o;
287
288   if (grub_efi_is_finished)
289     return;
290
291   o = grub_efi_system_table->con_out;
292
293   switch (state) {
294     case GRUB_TERM_COLOR_STANDARD:
295       efi_call_2 (o->set_attributes, o, GRUB_TERM_DEFAULT_STANDARD_COLOR
296                   & 0x7f);
297       break;
298     case GRUB_TERM_COLOR_NORMAL:
299       efi_call_2 (o->set_attributes, o, grub_term_normal_color & 0x7f);
300       break;
301     case GRUB_TERM_COLOR_HIGHLIGHT:
302       efi_call_2 (o->set_attributes, o, grub_term_highlight_color & 0x7f);
303       break;
304     default:
305       break;
306   }
307 }
308
309 static void
310 grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
311                         int on)
312 {
313   grub_efi_simple_text_output_interface_t *o;
314
315   if (grub_efi_is_finished)
316     return;
317
318   o = grub_efi_system_table->con_out;
319   efi_call_2 (o->enable_cursor, o, on);
320 }
321
322 static grub_err_t
323 grub_efi_console_output_init (struct grub_term_output *term)
324 {
325   grub_efi_set_text_mode (1);
326   grub_console_setcursor (term, 1);
327   return 0;
328 }
329
330 static grub_err_t
331 grub_efi_console_output_fini (struct grub_term_output *term)
332 {
333   grub_console_setcursor (term, 0);
334   grub_efi_set_text_mode (0);
335   return 0;
336 }
337
338 static struct grub_term_input grub_console_term_input =
339   {
340     .name = "console",
341     .getkey = grub_console_getkey,
342     .init = grub_efi_console_input_init,
343   };
344
345 static struct grub_term_output grub_console_term_output =
346   {
347     .name = "console",
348     .init = grub_efi_console_output_init,
349     .fini = grub_efi_console_output_fini,
350     .putchar = grub_console_putchar,
351     .getwh = grub_console_getwh,
352     .getxy = grub_console_getxy,
353     .gotoxy = grub_console_gotoxy,
354     .cls = grub_console_cls,
355     .setcolorstate = grub_console_setcolorstate,
356     .setcursor = grub_console_setcursor,
357     .flags = GRUB_TERM_CODE_TYPE_VISUAL_GLYPHS,
358     .progress_update_divisor = GRUB_PROGRESS_FAST
359   };
360
361 void
362 grub_console_init (void)
363 {
364   /* FIXME: it is necessary to consider the case where no console control
365      is present but the default is already in text mode.  */
366   if (! grub_efi_set_text_mode (1))
367     {
368       grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
369       return;
370     }
371
372   grub_term_register_output ("console", &grub_console_term_output);
373   grub_term_register_input ("console", &grub_console_term_input);
374 }
375
376 void
377 grub_console_fini (void)
378 {
379   grub_term_unregister_input (&grub_console_term_input);
380   grub_term_unregister_output (&grub_console_term_output);
381 }