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>
23 #include <grub/efi/efi.h>
24 #include <grub/efi/api.h>
25 #include <grub/efi/console.h>
28 grub_console_standard_color
= GRUB_EFI_TEXT_ATTR (GRUB_EFI_YELLOW
,
29 GRUB_EFI_BACKGROUND_BLACK
);
31 grub_console_normal_color
= GRUB_EFI_TEXT_ATTR (GRUB_EFI_LIGHTGRAY
,
32 GRUB_EFI_BACKGROUND_BLACK
);
34 grub_console_highlight_color
= GRUB_EFI_TEXT_ATTR (GRUB_EFI_BLACK
,
35 GRUB_EFI_BACKGROUND_LIGHTGRAY
);
37 static int read_key
= -1;
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. */
51 str
[0] = (grub_efi_char16_t
) (c
& 0xffff);
54 /* Should this test be cached? */
55 if (c
> 0x7f && o
->test_string (o
, str
) != GRUB_EFI_SUCCESS
)
58 o
->output_string (o
, str
);
62 grub_console_getcharwidth (grub_uint32_t c
__attribute__ ((unused
)))
64 /* For now, every printable character has the width 1. */
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
;
78 i
= grub_efi_system_table
->con_in
;
79 status
= i
->read_key_stroke (i
, &key
);
83 case GRUB_EFI_SUCCESS
:
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);
96 case GRUB_EFI_NOT_READY
:
97 //grub_printf ("not ready ");
101 //grub_printf ("device error ");
106 if (status
== GRUB_EFI_SUCCESS
)
108 switch (key
.scan_code
)
111 read_key
= key
.unicode_char
;
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
;
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
)
176 grub_console_checkkey ();
178 while (read_key
< 0);
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? */
199 return ((columns
<< 8) | rows
);
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
);
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
);
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
);
230 o
->set_attributes (o
, orig_attr
);
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
;
241 case GRUB_TERM_COLOR_STANDARD
:
242 o
->set_attributes (o
, grub_console_standard_color
);
244 case GRUB_TERM_COLOR_NORMAL
:
245 o
->set_attributes (o
, grub_console_normal_color
);
247 case GRUB_TERM_COLOR_HIGHLIGHT
:
248 o
->set_attributes (o
, grub_console_highlight_color
);
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
;
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
;
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
=
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
,
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");
310 grub_term_register (&grub_console_term
);
311 grub_term_set_current (&grub_console_term
);
315 grub_console_fini (void)
317 grub_term_unregister (&grub_console_term
);