1
/* ofconsole.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/machine/console.h>
25
#include <grub/ieee1275/ieee1275.h>
27
static grub_ieee1275_ihandle_t stdout_ihandle;
28
static grub_ieee1275_ihandle_t stdin_ihandle;
30
static grub_uint8_t grub_ofconsole_width;
31
static grub_uint8_t grub_ofconsole_height;
33
static int grub_curr_x;
34
static int grub_curr_y;
36
static int grub_keybuf;
37
static int grub_buflen;
46
static struct color colors[] =
50
{0x00, 0x00, 0xA8}, // 1 = blue
51
{0x00, 0xA8, 0x00}, // 2 = green
52
{0x00, 0xA8, 0xA8}, // 3 = cyan
53
{0xA8, 0x00, 0x00}, // 4 = red
54
{0xA8, 0x00, 0xA8}, // 5 = magenta
55
{0xFE, 0xFE, 0x54}, // 6 = yellow
56
{0xFE, 0xFE, 0xFE} // 7 = white
59
static grub_uint8_t grub_ofconsole_normal_color = 0x7;
60
static grub_uint8_t grub_ofconsole_highlight_color = 0x70;
62
/* Write control characters to the console. */
64
grub_ofconsole_writeesc (const char *str)
66
if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_ANSI))
72
grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
78
grub_ofconsole_putchar (grub_uint32_t c)
89
if (grub_curr_x > grub_ofconsole_width)
91
grub_ofconsole_putchar ('\n');
95
grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
99
grub_ofconsole_getcharwidth (grub_uint32_t c __attribute__((unused)))
105
grub_ofconsole_setcolorstate (grub_term_color_state state)
113
case GRUB_TERM_COLOR_STANDARD:
114
case GRUB_TERM_COLOR_NORMAL:
115
fg = grub_ofconsole_normal_color & 0x0f;
116
bg = grub_ofconsole_normal_color >> 4;
118
case GRUB_TERM_COLOR_HIGHLIGHT:
119
fg = grub_ofconsole_highlight_color & 0x0f;
120
bg = grub_ofconsole_highlight_color >> 4;
126
setcol = grub_asprintf ("\e[3%dm\e[4%dm", fg, bg);
128
grub_ofconsole_writeesc (setcol);
133
grub_ofconsole_setcolor (grub_uint8_t normal_color,
134
grub_uint8_t highlight_color)
136
/* Discard bright bit. */
137
grub_ofconsole_normal_color = normal_color & 0x77;
138
grub_ofconsole_highlight_color = highlight_color & 0x77;
142
grub_ofconsole_getcolor (grub_uint8_t *normal_color, grub_uint8_t *highlight_color)
144
*normal_color = grub_ofconsole_normal_color;
145
*highlight_color = grub_ofconsole_highlight_color;
149
grub_ofconsole_readkey (int *key)
152
grub_ssize_t actual = 0;
154
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
156
if (actual > 0 && c == '\e')
158
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
168
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
198
grub_ofconsole_checkkey (void)
206
read = grub_ofconsole_readkey (&key);
218
grub_ofconsole_getkey (void)
228
while (! grub_ofconsole_readkey (&key));
234
grub_ofconsole_getxy (void)
236
return ((grub_curr_x - 1) << 8) | grub_curr_y;
240
grub_ofconsole_getwh (void)
242
grub_ieee1275_ihandle_t options;
246
if (grub_ofconsole_width && grub_ofconsole_height)
247
return (grub_ofconsole_width << 8) | grub_ofconsole_height;
249
if (! grub_ieee1275_finddevice ("/options", &options)
250
&& options != (grub_ieee1275_ihandle_t) -1)
252
if (! grub_ieee1275_get_property_length (options, "screen-#columns",
253
&lval) && lval != -1)
255
val = grub_malloc (lval);
258
if (! grub_ieee1275_get_property (options, "screen-#columns",
260
grub_ofconsole_width = (grub_uint8_t) grub_strtoul (val, 0, 10);
265
if (! grub_ieee1275_get_property_length (options, "screen-#rows",
266
&lval) && lval != -1)
268
val = grub_malloc (lval);
271
if (! grub_ieee1275_get_property (options, "screen-#rows",
273
grub_ofconsole_height = (grub_uint8_t) grub_strtoul (val, 0, 10);
280
/* Use a small console by default. */
281
if (! grub_ofconsole_width)
282
grub_ofconsole_width = 80;
283
if (! grub_ofconsole_height)
284
grub_ofconsole_height = 24;
286
return (grub_ofconsole_width << 8) | grub_ofconsole_height;
290
grub_ofconsole_gotoxy (grub_uint8_t x, grub_uint8_t y)
292
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_ANSI))
298
s = grub_asprintf ("\e[%d;%dH", y + 1, x + 1);
300
grub_ofconsole_writeesc (s);
305
if ((y == grub_curr_y) && (x == grub_curr_x - 1))
310
grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
319
grub_ofconsole_cls (void)
321
/* Clear the screen. Using serial console, screen(1) only recognizes the
322
* ANSI escape sequence. Using video console, Apple Open Firmware (version
323
* 3.1.1) only recognizes the literal ^L. So use both. */
324
grub_ofconsole_writeesc ("\e[2J");
325
grub_ofconsole_gotoxy (0, 0);
329
grub_ofconsole_setcursor (int on)
331
/* Understood by the Open Firmware flavour in OLPC. */
333
grub_ieee1275_interpret ("cursor-on", 0);
335
grub_ieee1275_interpret ("cursor-off", 0);
339
grub_ofconsole_refresh (void)
341
/* Do nothing, the current console state is ok. */
345
grub_ofconsole_init_input (void)
349
if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdin", &stdin_ihandle,
350
sizeof stdin_ihandle, &actual)
351
|| actual != sizeof stdin_ihandle)
352
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdin");
358
grub_ofconsole_init_output (void)
362
/* The latest PowerMacs don't actually initialize the screen for us, so we
363
* use this trick to re-open the output device (but we avoid doing this on
364
* platforms where it's known to be broken). */
365
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT))
366
grub_ieee1275_interpret ("output-device output", 0);
368
if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdout", &stdout_ihandle,
369
sizeof stdout_ihandle, &actual)
370
|| actual != sizeof stdout_ihandle)
371
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdout");
373
/* Initialize colors. */
374
if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_SET_COLORS))
377
for (col = 0; col < ARRAY_SIZE (colors); col++)
378
grub_ieee1275_set_color (stdout_ihandle, col, colors[col].red,
379
colors[col].green, colors[col].blue);
381
/* Set the right fg and bg colors. */
382
grub_ofconsole_setcolorstate (GRUB_TERM_COLOR_NORMAL);
389
grub_ofconsole_fini (void)
396
static struct grub_term_input grub_ofconsole_term_input =
399
.init = grub_ofconsole_init_input,
400
.fini = grub_ofconsole_fini,
401
.checkkey = grub_ofconsole_checkkey,
402
.getkey = grub_ofconsole_getkey,
405
static struct grub_term_output grub_ofconsole_term_output =
408
.init = grub_ofconsole_init_output,
409
.fini = grub_ofconsole_fini,
410
.putchar = grub_ofconsole_putchar,
411
.getcharwidth = grub_ofconsole_getcharwidth,
412
.getxy = grub_ofconsole_getxy,
413
.getwh = grub_ofconsole_getwh,
414
.gotoxy = grub_ofconsole_gotoxy,
415
.cls = grub_ofconsole_cls,
416
.setcolorstate = grub_ofconsole_setcolorstate,
417
.setcolor = grub_ofconsole_setcolor,
418
.getcolor = grub_ofconsole_getcolor,
419
.setcursor = grub_ofconsole_setcursor,
420
.refresh = grub_ofconsole_refresh
424
grub_console_init (void)
426
grub_term_register_input ("ofconsole", &grub_ofconsole_term_input);
427
grub_term_register_output ("ofconsole", &grub_ofconsole_term_output);
431
grub_console_fini (void)
433
grub_term_unregister_input (&grub_ofconsole_term_input);
434
grub_term_unregister_output (&grub_ofconsole_term_output);