~ubuntu-branches/debian/sid/grub2/sid-200907171837

« back to all changes in this revision

Viewing changes to term/ieee1275/ofconsole.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2006-01-05 15:20:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060105152040-1ab076d4n3y2o5yf
Tags: 1.92-1
* New upstream release.
  - Add support for GPT partition table format.
  - Add a new command "play" to play an audio file on PC.
  - Add support for Linux/ADFS partition table format.
  - Add support for BASH-like scripting.
  - Add support for Apple HFS+ filesystems.
* 01_fix_grub-install.patch: Added. Fix grub-install to use
  /bin/grub-mkimage instead of /sbin/grub-mkimage. Closes: #338824
* Do not use CDBS tarball mode anymore. Closes: #344272  

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  ofconsole.c -- Open Firmware console for GRUB.  */
 
2
/*
 
3
 *  GRUB  --  GRand Unified Bootloader
 
4
 *  Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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.
 
19
 */
 
20
 
 
21
#include <grub/term.h>
 
22
#include <grub/types.h>
 
23
#include <grub/misc.h>
 
24
#include <grub/mm.h>
 
25
#include <grub/machine/console.h>
 
26
#include <grub/ieee1275/ieee1275.h>
 
27
 
 
28
static grub_ieee1275_ihandle_t stdout_ihandle;
 
29
static grub_ieee1275_ihandle_t stdin_ihandle;
 
30
 
 
31
static grub_uint8_t grub_ofconsole_width;
 
32
static grub_uint8_t grub_ofconsole_height;
 
33
 
 
34
static int grub_curr_x;
 
35
static int grub_curr_y;
 
36
 
 
37
static int grub_keybuf;
 
38
static int grub_buflen;
 
39
 
 
40
struct color
 
41
{
 
42
  int red;
 
43
  int green;
 
44
  int blue;
 
45
};
 
46
 
 
47
#define MAX 0xff
 
48
static struct color colors[8] =
 
49
  {
 
50
    { 0,   0,   0},
 
51
    { MAX, 0,   0},
 
52
    { 0,   MAX, 0},
 
53
    { MAX, MAX, 0},
 
54
    { 0,   0,   MAX},
 
55
    { MAX, 0,   MAX},
 
56
    { 0,   MAX, MAX},
 
57
    { MAX, MAX, MAX}
 
58
  };
 
59
 
 
60
static int fgcolor = 7;
 
61
static int bgcolor = 0;
 
62
 
 
63
/* Write control characters to the console.  */
 
64
static void
 
65
grub_ofconsole_writeesc (const char *str)
 
66
{
 
67
  while (*str)
 
68
    {
 
69
      char chr = *(str++);
 
70
      grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
 
71
    }
 
72
  
 
73
}
 
74
 
 
75
static void
 
76
grub_ofconsole_putchar (grub_uint32_t c)
 
77
{
 
78
  char chr = c;
 
79
  if (c == '\n')
 
80
    {
 
81
      grub_curr_y++;
 
82
      grub_curr_x = 0;
 
83
    }
 
84
  else
 
85
    {
 
86
      grub_curr_x++;
 
87
      if (grub_curr_x > grub_ofconsole_width)
 
88
        grub_putcode ('\n');
 
89
    }
 
90
  grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
 
91
}
 
92
 
 
93
static grub_ssize_t
 
94
grub_ofconsole_getcharwidth (grub_uint32_t c __attribute__((unused)))
 
95
{
 
96
  return 1;
 
97
}
 
98
 
 
99
static void
 
100
grub_ofconsole_setcolorstate (grub_term_color_state state)
 
101
{
 
102
  char setcol[20];
 
103
  int fg;
 
104
  int bg;
 
105
 
 
106
  switch (state)
 
107
    {
 
108
    case GRUB_TERM_COLOR_STANDARD:
 
109
    case GRUB_TERM_COLOR_NORMAL:
 
110
      fg = fgcolor;
 
111
      bg = bgcolor;
 
112
      break;
 
113
    case GRUB_TERM_COLOR_HIGHLIGHT:
 
114
      fg = bgcolor;
 
115
      bg = fgcolor;
 
116
      break;
 
117
    default:
 
118
      return;
 
119
    }
 
120
 
 
121
  grub_sprintf (setcol, "\e[3%dm\e[4%dm", fg, bg);
 
122
  grub_ofconsole_writeesc (setcol);
 
123
}
 
124
 
 
125
static void
 
126
grub_ofconsole_setcolor (grub_uint8_t normal_color,
 
127
                         grub_uint8_t highlight_color)
 
128
{
 
129
  fgcolor = normal_color;
 
130
  bgcolor = highlight_color;
 
131
}
 
132
 
 
133
static int
 
134
grub_ofconsole_readkey (int *key)
 
135
{
 
136
  char c;
 
137
  grub_ssize_t actual = 0;
 
138
 
 
139
  grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
 
140
 
 
141
  if (actual > 0 && c == '\e')
 
142
    {
 
143
      grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
 
144
      if (actual <= 0)
 
145
        {
 
146
          *key = '\e';
 
147
          return 1;
 
148
        }
 
149
      
 
150
      if (c != 91)
 
151
        return 0;
 
152
      
 
153
      grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
 
154
      if (actual <= 0)
 
155
        return 0;
 
156
      
 
157
      switch (c)
 
158
        {
 
159
        case 65:
 
160
          /* Up: Ctrl-p.  */
 
161
          c = 16; 
 
162
          break;
 
163
        case 66:
 
164
          /* Down: Ctrl-n.  */
 
165
          c = 14;
 
166
          break;
 
167
        case 67:
 
168
          /* Right: Ctrl-f.  */
 
169
          c = 6;
 
170
          break;
 
171
        case 68:
 
172
          /* Left: Ctrl-b.  */
 
173
          c = 2;
 
174
          break;
 
175
        }
 
176
    }
 
177
  
 
178
  *key = c;
 
179
  return actual > 0;
 
180
}
 
181
 
 
182
static int
 
183
grub_ofconsole_checkkey (void)
 
184
{
 
185
  int key;
 
186
  int read;
 
187
  
 
188
  if (grub_buflen)
 
189
    return 1;
 
190
 
 
191
  read = grub_ofconsole_readkey (&key);
 
192
  if (read)
 
193
    {
 
194
      grub_keybuf = key;
 
195
      grub_buflen = 1;
 
196
      return 1;
 
197
    }
 
198
    
 
199
  return -1;
 
200
}
 
201
 
 
202
static int
 
203
grub_ofconsole_getkey (void)
 
204
{
 
205
  int key;
 
206
 
 
207
  if (grub_buflen)
 
208
    {
 
209
      grub_buflen  =0;
 
210
      return grub_keybuf;
 
211
    }
 
212
 
 
213
  while (! grub_ofconsole_readkey (&key));
 
214
  
 
215
  return key;
 
216
}
 
217
 
 
218
static grub_uint16_t
 
219
grub_ofconsole_getxy (void)
 
220
{
 
221
  return ((grub_curr_x - 1) << 8) | grub_curr_y;
 
222
}
 
223
 
 
224
static grub_uint16_t
 
225
grub_ofconsole_getwh (void)
 
226
{
 
227
  grub_ieee1275_ihandle_t options;
 
228
  char *val;
 
229
  grub_ssize_t lval;
 
230
 
 
231
  if (grub_ofconsole_width && grub_ofconsole_height)
 
232
    return (grub_ofconsole_width << 8) | grub_ofconsole_height;
 
233
 
 
234
  if (! grub_ieee1275_finddevice ("/options", &options)
 
235
      && options != (grub_ieee1275_ihandle_t) -1)
 
236
    {
 
237
      if (! grub_ieee1275_get_property_length (options, "screen-#columns",
 
238
                                               &lval) && lval != -1)
 
239
        {
 
240
          val = grub_malloc (lval);
 
241
          if (val)
 
242
            {
 
243
              if (! grub_ieee1275_get_property (options, "screen-#columns",
 
244
                                                val, lval, 0))
 
245
                grub_ofconsole_width = (grub_uint8_t) grub_strtoul (val, 0, 10);
 
246
 
 
247
              grub_free (val);
 
248
            }
 
249
        }
 
250
      if (! grub_ieee1275_get_property_length (options, "screen-#rows",
 
251
                                               &lval) && lval != -1)
 
252
        {
 
253
          val = grub_malloc (lval);
 
254
          if (val)
 
255
            {
 
256
              if (! grub_ieee1275_get_property (options, "screen-#rows",
 
257
                                                val, lval, 0))
 
258
                grub_ofconsole_height = (grub_uint8_t) grub_strtoul (val, 0, 10);
 
259
 
 
260
              grub_free (val);
 
261
            }
 
262
        }
 
263
    }
 
264
 
 
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;
 
270
 
 
271
  return (grub_ofconsole_width << 8) | grub_ofconsole_height;
 
272
}
 
273
 
 
274
static void
 
275
grub_ofconsole_gotoxy (grub_uint8_t x, grub_uint8_t y)
 
276
{
 
277
  char s[11]; /* 5 + 3 + 3.  */
 
278
  grub_curr_x = x;
 
279
  grub_curr_y = y;
 
280
 
 
281
  grub_sprintf (s, "\e[%d;%dH", y + 1, x + 1);
 
282
  grub_ofconsole_writeesc (s);
 
283
}
 
284
 
 
285
static void
 
286
grub_ofconsole_cls (void)
 
287
{
 
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");
 
292
  grub_gotoxy (0, 0);
 
293
}
 
294
 
 
295
static void
 
296
grub_ofconsole_setcursor (int on __attribute ((unused)))
 
297
{
 
298
  /* XXX: Not supported.  */
 
299
}
 
300
 
 
301
static void
 
302
grub_ofconsole_refresh (void)
 
303
{
 
304
  /* Do nothing, the current console state is ok.  */
 
305
}
 
306
 
 
307
static grub_err_t
 
308
grub_ofconsole_init (void)
 
309
{
 
310
  unsigned char data[4];
 
311
  grub_ssize_t actual;
 
312
  int col;
 
313
 
 
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");
 
318
 
 
319
  stdout_ihandle = grub_ieee1275_decode_int_4 (data);
 
320
  
 
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");
 
325
 
 
326
  stdin_ihandle = grub_ieee1275_decode_int_4 (data);
 
327
 
 
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);
 
332
 
 
333
  /* Set the right fg and bg colors.  */
 
334
  grub_ofconsole_setcolorstate (GRUB_TERM_COLOR_NORMAL);
 
335
 
 
336
  return 0;
 
337
}
 
338
 
 
339
static grub_err_t
 
340
grub_ofconsole_fini (void)
 
341
{
 
342
  return 0;
 
343
}
 
344
 
 
345
 
 
346
 
 
347
static struct grub_term grub_ofconsole_term =
 
348
  {
 
349
    .name = "ofconsole",
 
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,
 
364
    .flags = 0,
 
365
    .next = 0
 
366
  };
 
367
 
 
368
void
 
369
grub_console_init (void)
 
370
{
 
371
  grub_term_register (&grub_ofconsole_term);
 
372
  grub_term_set_current (&grub_ofconsole_term);
 
373
}
 
374
 
 
375
void
 
376
grub_console_fini (void)
 
377
{
 
378
  grub_term_unregister (&grub_ofconsole_term);
 
379
}