make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / term / efi / console.c
blobaf198e56e24d6e394c9a8f34ccef1a71cc79aa4a
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2006,2007 Free Software Foundation, Inc.
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.
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.
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/>.
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>
27 static grub_uint8_t
28 grub_console_standard_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_YELLOW,
29 GRUB_EFI_BACKGROUND_BLACK);
30 static grub_uint8_t
31 grub_console_normal_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_LIGHTGRAY,
32 GRUB_EFI_BACKGROUND_BLACK);
33 static grub_uint8_t
34 grub_console_highlight_color = GRUB_EFI_TEXT_ATTR (GRUB_EFI_BLACK,
35 GRUB_EFI_BACKGROUND_LIGHTGRAY);
37 static int read_key = -1;
39 static void
40 grub_console_putchar (grub_uint32_t c)
42 grub_efi_char16_t str[2];
43 grub_efi_simple_text_output_interface_t *o;
45 o = grub_efi_system_table->con_out;
47 /* For now, do not try to use a surrogate pair. */
48 if (c > 0xffff)
49 c = '?';
51 str[0] = (grub_efi_char16_t) (c & 0xffff);
52 str[1] = 0;
54 /* Should this test be cached? */
55 if (c > 0x7f && o->test_string (o, str) != GRUB_EFI_SUCCESS)
56 return;
58 o->output_string (o, str);
61 static grub_ssize_t
62 grub_console_getcharwidth (grub_uint32_t c __attribute__ ((unused)))
64 /* For now, every printable character has the width 1. */
65 return 1;
68 static int
69 grub_console_checkkey (void)
71 grub_efi_simple_input_interface_t *i;
72 grub_efi_input_key_t key;
73 grub_efi_status_t status;
75 if (read_key >= 0)
76 return 1;
78 i = grub_efi_system_table->con_in;
79 status = i->read_key_stroke (i, &key);
80 #if 1
81 switch (status)
83 case GRUB_EFI_SUCCESS:
85 grub_uint16_t xy;
87 xy = grub_getxy ();
88 grub_gotoxy (0, 0);
89 grub_printf ("scan_code=%x,unicode_char=%x ",
90 (unsigned) key.scan_code,
91 (unsigned) key.unicode_char);
92 grub_gotoxy (xy >> 8, xy & 0xff);
94 break;
96 case GRUB_EFI_NOT_READY:
97 //grub_printf ("not ready ");
98 break;
100 default:
101 //grub_printf ("device error ");
102 break;
104 #endif
106 if (status == GRUB_EFI_SUCCESS)
108 switch (key.scan_code)
110 case 0x00:
111 read_key = key.unicode_char;
112 break;
113 case 0x01:
114 read_key = 16;
115 break;
116 case 0x02:
117 read_key = 14;
118 break;
119 case 0x03:
120 read_key = 6;
121 break;
122 case 0x04:
123 read_key = 2;
124 break;
125 case 0x05:
126 read_key = 1;
127 break;
128 case 0x06:
129 read_key = 5;
130 break;
131 case 0x07:
132 break;
133 case 0x08:
134 read_key = 4;
135 break;
136 case 0x09:
137 break;
138 case 0x0a:
139 break;
140 case 0x17:
141 read_key = '\e';
142 break;
143 default:
144 break;
148 return read_key;
151 static int
152 grub_console_getkey (void)
154 grub_efi_simple_input_interface_t *i;
155 grub_efi_boot_services_t *b;
156 grub_efi_uintn_t index;
157 grub_efi_status_t status;
158 int key;
160 if (read_key >= 0)
162 key = read_key;
163 read_key = -1;
164 return key;
167 i = grub_efi_system_table->con_in;
168 b = grub_efi_system_table->boot_services;
172 status = b->wait_for_event (1, &(i->wait_for_key), &index);
173 if (status != GRUB_EFI_SUCCESS)
174 return -1;
176 grub_console_checkkey ();
178 while (read_key < 0);
180 key = read_key;
181 read_key = -1;
182 return key;
185 static grub_uint16_t
186 grub_console_getwh (void)
188 grub_efi_simple_text_output_interface_t *o;
189 grub_efi_uintn_t columns, rows;
191 o = grub_efi_system_table->con_out;
192 if (o->query_mode (o, o->mode->mode, &columns, &rows) != GRUB_EFI_SUCCESS)
194 /* Why does this fail? */
195 columns = 80;
196 rows = 25;
199 return ((columns << 8) | rows);
202 static grub_uint16_t
203 grub_console_getxy (void)
205 grub_efi_simple_text_output_interface_t *o;
207 o = grub_efi_system_table->con_out;
208 return ((o->mode->cursor_column << 8) | o->mode->cursor_row);
211 static void
212 grub_console_gotoxy (grub_uint8_t x, grub_uint8_t y)
214 grub_efi_simple_text_output_interface_t *o;
216 o = grub_efi_system_table->con_out;
217 o->set_cursor_position (o, x, y);
220 static void
221 grub_console_cls (void)
223 grub_efi_simple_text_output_interface_t *o;
224 grub_efi_int32_t orig_attr;
226 o = grub_efi_system_table->con_out;
227 orig_attr = o->mode->attribute;
228 o->set_attributes (o, GRUB_EFI_BACKGROUND_BLACK);
229 o->clear_screen (o);
230 o->set_attributes (o, orig_attr);
233 static void
234 grub_console_setcolorstate (grub_term_color_state state)
236 grub_efi_simple_text_output_interface_t *o;
238 o = grub_efi_system_table->con_out;
240 switch (state) {
241 case GRUB_TERM_COLOR_STANDARD:
242 o->set_attributes (o, grub_console_standard_color);
243 break;
244 case GRUB_TERM_COLOR_NORMAL:
245 o->set_attributes (o, grub_console_normal_color);
246 break;
247 case GRUB_TERM_COLOR_HIGHLIGHT:
248 o->set_attributes (o, grub_console_highlight_color);
249 break;
250 default:
251 break;
255 static void
256 grub_console_setcolor (grub_uint8_t normal_color, grub_uint8_t highlight_color)
258 grub_console_normal_color = normal_color;
259 grub_console_highlight_color = highlight_color;
262 static void
263 grub_console_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
265 *normal_color = grub_console_normal_color;
266 *highlight_color = grub_console_highlight_color;
269 static void
270 grub_console_setcursor (int on)
272 grub_efi_simple_text_output_interface_t *o;
274 o = grub_efi_system_table->con_out;
275 o->enable_cursor (o, on);
278 static struct grub_term grub_console_term =
280 .name = "console",
281 .init = 0,
282 .fini = 0,
283 .putchar = grub_console_putchar,
284 .getcharwidth = grub_console_getcharwidth,
285 .checkkey = grub_console_checkkey,
286 .getkey = grub_console_getkey,
287 .getwh = grub_console_getwh,
288 .getxy = grub_console_getxy,
289 .gotoxy = grub_console_gotoxy,
290 .cls = grub_console_cls,
291 .setcolorstate = grub_console_setcolorstate,
292 .setcolor = grub_console_setcolor,
293 .getcolor = grub_console_getcolor,
294 .setcursor = grub_console_setcursor,
295 .flags = 0,
296 .next = 0
299 void
300 grub_console_init (void)
302 /* FIXME: it is necessary to consider the case where no console control
303 is present but the default is already in text mode. */
304 if (! grub_efi_set_text_mode (1))
306 grub_error (GRUB_ERR_BAD_DEVICE, "cannot set text mode");
307 return;
310 grub_term_register (&grub_console_term);
311 grub_term_set_current (&grub_console_term);
314 void
315 grub_console_fini (void)
317 grub_term_unregister (&grub_console_term);