1
/* ofconsole.c -- Open Firmware console for GRUB. */
3
* GRUB -- GRand Unified Bootloader
4
* Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
6
* This program 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 2 of the License, or
9
* (at your option) any later version.
11
* This program 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 this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21
#include <grub/term.h>
22
#include <grub/types.h>
23
#include <grub/misc.h>
25
#include <grub/machine/console.h>
26
#include <grub/ieee1275/ieee1275.h>
28
static grub_ieee1275_ihandle_t stdout_ihandle;
29
static grub_ieee1275_ihandle_t stdin_ihandle;
31
static grub_uint8_t grub_ofconsole_width;
32
static grub_uint8_t grub_ofconsole_height;
34
static int grub_curr_x;
35
static int grub_curr_y;
37
static int grub_keybuf;
38
static int grub_buflen;
48
static struct color colors[8] =
60
static int fgcolor = 7;
61
static int bgcolor = 0;
63
/* Write control characters to the console. */
65
grub_ofconsole_writeesc (const char *str)
70
grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
76
grub_ofconsole_putchar (grub_uint32_t c)
87
if (grub_curr_x > grub_ofconsole_width)
90
grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
94
grub_ofconsole_getcharwidth (grub_uint32_t c __attribute__((unused)))
100
grub_ofconsole_setcolorstate (grub_term_color_state state)
108
case GRUB_TERM_COLOR_STANDARD:
109
case GRUB_TERM_COLOR_NORMAL:
113
case GRUB_TERM_COLOR_HIGHLIGHT:
121
grub_sprintf (setcol, "\e[3%dm\e[4%dm", fg, bg);
122
grub_ofconsole_writeesc (setcol);
126
grub_ofconsole_setcolor (grub_uint8_t normal_color,
127
grub_uint8_t highlight_color)
129
fgcolor = normal_color;
130
bgcolor = highlight_color;
134
grub_ofconsole_readkey (int *key)
137
grub_ssize_t actual = 0;
139
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
141
if (actual > 0 && c == '\e')
143
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
153
grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
183
grub_ofconsole_checkkey (void)
191
read = grub_ofconsole_readkey (&key);
203
grub_ofconsole_getkey (void)
213
while (! grub_ofconsole_readkey (&key));
219
grub_ofconsole_getxy (void)
221
return ((grub_curr_x - 1) << 8) | grub_curr_y;
225
grub_ofconsole_getwh (void)
227
grub_ieee1275_ihandle_t options;
231
if (grub_ofconsole_width && grub_ofconsole_height)
232
return (grub_ofconsole_width << 8) | grub_ofconsole_height;
234
if (! grub_ieee1275_finddevice ("/options", &options)
235
&& options != (grub_ieee1275_ihandle_t) -1)
237
if (! grub_ieee1275_get_property_length (options, "screen-#columns",
238
&lval) && lval != -1)
240
val = grub_malloc (lval);
243
if (! grub_ieee1275_get_property (options, "screen-#columns",
245
grub_ofconsole_width = (grub_uint8_t) grub_strtoul (val, 0, 10);
250
if (! grub_ieee1275_get_property_length (options, "screen-#rows",
251
&lval) && lval != -1)
253
val = grub_malloc (lval);
256
if (! grub_ieee1275_get_property (options, "screen-#rows",
258
grub_ofconsole_height = (grub_uint8_t) grub_strtoul (val, 0, 10);
265
/* Use a small console by default. */
266
if (! grub_ofconsole_width)
267
grub_ofconsole_width = 80;
268
if (! grub_ofconsole_height)
269
grub_ofconsole_height = 24;
271
return (grub_ofconsole_width << 8) | grub_ofconsole_height;
275
grub_ofconsole_gotoxy (grub_uint8_t x, grub_uint8_t y)
277
char s[11]; /* 5 + 3 + 3. */
281
grub_sprintf (s, "\e[%d;%dH", y + 1, x + 1);
282
grub_ofconsole_writeesc (s);
286
grub_ofconsole_cls (void)
288
/* Clear the screen. Using serial console, screen(1) only recognizes the
289
* ANSI escape sequence. Using video console, Apple Open Firmware (version
290
* 3.1.1) only recognizes the literal ^L. So use both. */
291
grub_ofconsole_writeesc ("\e[2J");
296
grub_ofconsole_setcursor (int on __attribute ((unused)))
298
/* XXX: Not supported. */
302
grub_ofconsole_refresh (void)
304
/* Do nothing, the current console state is ok. */
308
grub_ofconsole_init (void)
310
unsigned char data[4];
314
if (grub_ieee1275_get_property (grub_ieee1275_chosen, "stdout", data,
315
sizeof data, &actual)
316
|| actual != sizeof data)
317
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Cannot find stdout");
319
stdout_ihandle = grub_ieee1275_decode_int_4 (data);
321
if (grub_ieee1275_get_property (grub_ieee1275_chosen, "stdin", data,
322
sizeof data, &actual)
323
|| actual != sizeof data)
324
return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "Cannot find stdin");
326
stdin_ihandle = grub_ieee1275_decode_int_4 (data);
328
/* Initialize colors. */
329
for (col = 0; col < 7; col++)
330
grub_ieee1275_set_color (stdout_ihandle, col, colors[col].red,
331
colors[col].green, colors[col].blue);
333
/* Set the right fg and bg colors. */
334
grub_ofconsole_setcolorstate (GRUB_TERM_COLOR_NORMAL);
340
grub_ofconsole_fini (void)
347
static struct grub_term grub_ofconsole_term =
350
.init = grub_ofconsole_init,
351
.fini = grub_ofconsole_fini,
352
.putchar = grub_ofconsole_putchar,
353
.getcharwidth = grub_ofconsole_getcharwidth,
354
.checkkey = grub_ofconsole_checkkey,
355
.getkey = grub_ofconsole_getkey,
356
.getxy = grub_ofconsole_getxy,
357
.getwh = grub_ofconsole_getwh,
358
.gotoxy = grub_ofconsole_gotoxy,
359
.cls = grub_ofconsole_cls,
360
.setcolorstate = grub_ofconsole_setcolorstate,
361
.setcolor = grub_ofconsole_setcolor,
362
.setcursor = grub_ofconsole_setcursor,
363
.refresh = grub_ofconsole_refresh,
369
grub_console_init (void)
371
grub_term_register (&grub_ofconsole_term);
372
grub_term_set_current (&grub_ofconsole_term);
376
grub_console_fini (void)
378
grub_term_unregister (&grub_ofconsole_term);