1 /* console.c -- Open Firmware console for GRUB. */
3 * GRUB -- GRand Unified Bootloader
4 * Copyright (C) 2003,2004,2005,2007,2008,2009 Free Software Foundation, Inc.
6 * GRUB is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * GRUB is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
20 #include <grub/term.h>
21 #include <grub/types.h>
22 #include <grub/misc.h>
24 #include <grub/time.h>
25 #include <grub/terminfo.h>
26 #include <grub/ieee1275/console.h>
27 #include <grub/ieee1275/ieee1275.h>
29 static grub_ieee1275_ihandle_t stdout_ihandle
;
30 static grub_ieee1275_ihandle_t stdin_ihandle
;
32 extern struct grub_terminfo_output_state grub_console_terminfo_output
;
41 /* Use serial colors as they are default on most firmwares and some firmwares
42 ignore set-color!. Additionally output may be redirected to serial. */
43 static struct color colors
[] =
46 {0x00, 0x00, 0x00}, // 0 = black
47 {0xA8, 0x00, 0x00}, // 1 = red
48 {0x00, 0xA8, 0x00}, // 2 = green
49 {0xFE, 0xFE, 0x54}, // 3 = yellow
50 {0x00, 0x00, 0xA8}, // 4 = blue
51 {0xA8, 0x00, 0xA8}, // 5 = magenta
52 {0x00, 0xA8, 0xA8}, // 6 = cyan
53 {0xFE, 0xFE, 0xFE} // 7 = white
57 put (struct grub_term_output
*term
__attribute__ ((unused
)), const int c
)
61 grub_ieee1275_write (stdout_ihandle
, &chr
, 1, 0);
65 readkey (struct grub_term_input
*term
__attribute__ ((unused
)))
68 grub_ssize_t actual
= 0;
70 grub_ieee1275_read (stdin_ihandle
, &c
, 1, &actual
);
77 grub_console_dimensions (void)
79 grub_ieee1275_ihandle_t options
;
80 grub_ieee1275_phandle_t stdout_phandle
;
83 /* Always assume 80x24 on serial since screen-#rows/screen-#columns is often
84 garbage for such devices. */
85 if (! grub_ieee1275_instance_to_package (stdout_ihandle
,
87 && ! grub_ieee1275_package_to_path (stdout_phandle
,
88 val
, sizeof (val
) - 1, 0))
90 grub_ieee1275_ihandle_t stdout_options
;
91 val
[sizeof (val
) - 1] = 0;
93 if (! grub_ieee1275_finddevice (val
, &stdout_options
)
94 && ! grub_ieee1275_get_property (stdout_options
, "device_type",
95 val
, sizeof (val
) - 1, 0))
97 val
[sizeof (val
) - 1] = 0;
98 if (grub_strcmp (val
, "serial") == 0)
100 grub_console_terminfo_output
.width
= 80;
101 grub_console_terminfo_output
.height
= 24;
107 if (! grub_ieee1275_finddevice ("/options", &options
)
108 && options
!= (grub_ieee1275_ihandle_t
) -1)
110 if (! grub_ieee1275_get_property (options
, "screen-#columns",
111 val
, sizeof (val
) - 1, 0))
113 val
[sizeof (val
) - 1] = 0;
114 grub_console_terminfo_output
.width
115 = (grub_uint8_t
) grub_strtoul (val
, 0, 10);
117 if (! grub_ieee1275_get_property (options
, "screen-#rows",
118 val
, sizeof (val
) - 1, 0))
120 val
[sizeof (val
) - 1] = 0;
121 grub_console_terminfo_output
.height
122 = (grub_uint8_t
) grub_strtoul (val
, 0, 10);
126 /* Use a small console by default. */
127 if (! grub_console_terminfo_output
.width
)
128 grub_console_terminfo_output
.width
= 80;
129 if (! grub_console_terminfo_output
.height
)
130 grub_console_terminfo_output
.height
= 24;
134 grub_console_setcursor (struct grub_term_output
*term
,
137 grub_terminfo_setcursor (term
, on
);
139 if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF
))
142 /* Understood by the Open Firmware flavour in OLPC. */
144 grub_ieee1275_interpret ("cursor-on", 0);
146 grub_ieee1275_interpret ("cursor-off", 0);
150 grub_console_init_input (struct grub_term_input
*term
)
154 if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen
, "stdin", &stdin_ihandle
,
155 sizeof stdin_ihandle
, &actual
)
156 || actual
!= sizeof stdin_ihandle
)
157 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "cannot find stdin");
159 return grub_terminfo_input_init (term
);
163 grub_console_init_output (struct grub_term_output
*term
)
167 /* The latest PowerMacs don't actually initialize the screen for us, so we
168 * use this trick to re-open the output device (but we avoid doing this on
169 * platforms where it's known to be broken). */
170 if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT
))
171 grub_ieee1275_interpret ("output-device output", 0);
173 if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen
, "stdout", &stdout_ihandle
,
174 sizeof stdout_ihandle
, &actual
)
175 || actual
!= sizeof stdout_ihandle
)
176 return grub_error (GRUB_ERR_UNKNOWN_DEVICE
, "cannot find stdout");
178 /* Initialize colors. */
179 if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_SET_COLORS
))
182 for (col
= 0; col
< ARRAY_SIZE (colors
); col
++)
183 grub_ieee1275_set_color (stdout_ihandle
, col
, colors
[col
].red
,
184 colors
[col
].green
, colors
[col
].blue
);
186 /* Set the right fg and bg colors. */
187 grub_terminfo_setcolorstate (term
, GRUB_TERM_COLOR_NORMAL
);
190 grub_console_dimensions ();
192 grub_terminfo_output_init (term
);
199 struct grub_terminfo_input_state grub_console_terminfo_input
=
204 struct grub_terminfo_output_state grub_console_terminfo_output
=
211 static struct grub_term_input grub_console_term_input
=
214 .init
= grub_console_init_input
,
215 .getkey
= grub_terminfo_getkey
,
216 .data
= &grub_console_terminfo_input
219 static struct grub_term_output grub_console_term_output
=
222 .init
= grub_console_init_output
,
223 .putchar
= grub_terminfo_putchar
,
224 .getxy
= grub_terminfo_getxy
,
225 .getwh
= grub_terminfo_getwh
,
226 .gotoxy
= grub_terminfo_gotoxy
,
227 .cls
= grub_terminfo_cls
,
228 .setcolorstate
= grub_terminfo_setcolorstate
,
229 .setcursor
= grub_console_setcursor
,
230 .flags
= GRUB_TERM_CODE_TYPE_ASCII
,
231 .data
= &grub_console_terminfo_output
,
232 .normal_color
= GRUB_TERM_DEFAULT_NORMAL_COLOR
,
233 .highlight_color
= GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR
,
237 grub_console_init_early (void)
239 grub_term_register_input ("console", &grub_console_term_input
);
240 grub_term_register_output ("console", &grub_console_term_output
);
244 grub_console_init_lately (void)
248 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_ANSI
))
253 grub_terminfo_init ();
254 grub_terminfo_output_register (&grub_console_term_output
, type
);
258 grub_console_fini (void)