source: grub-pc/trunk/fuentes/grub-core/term/ieee1275/console.c @ 22

Last change on this file since 22 was 22, checked in by mabarracus, 4 years ago

updated version and apply net.ifnames=0 into debian/rules

File size: 7.4 KB
Line 
1/*  console.c -- Open Firmware console for GRUB.  */
2/*
3 *  GRUB  --  GRand Unified Bootloader
4 *  Copyright (C) 2003,2004,2005,2007,2008,2009  Free Software Foundation, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <grub/term.h>
21#include <grub/types.h>
22#include <grub/misc.h>
23#include <grub/mm.h>
24#include <grub/time.h>
25#include <grub/terminfo.h>
26#include <grub/ieee1275/console.h>
27#include <grub/ieee1275/ieee1275.h>
28
29static grub_ieee1275_ihandle_t stdout_ihandle;
30static grub_ieee1275_ihandle_t stdin_ihandle;
31
32extern struct grub_terminfo_output_state grub_console_terminfo_output;
33
34struct color
35{
36  int red;
37  int green;
38  int blue;
39};
40
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.  */
43static struct color colors[] =
44  {
45    // {R, G, B}
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
54  };
55
56static void
57put (struct grub_term_output *term __attribute__ ((unused)), const int c)
58{
59  char chr = c;
60
61  grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
62}
63
64static int
65readkey (struct grub_term_input *term __attribute__ ((unused)))
66{
67  grub_uint8_t c;
68  grub_ssize_t actual = 0;
69
70  grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
71  if (actual > 0)
72    return c;
73  return -1;
74}
75
76static void
77grub_console_dimensions (void)
78{
79  grub_ieee1275_ihandle_t options;
80  grub_ieee1275_phandle_t stdout_phandle;
81  char val[1024];
82
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,
86                                           &stdout_phandle)
87      && ! grub_ieee1275_package_to_path (stdout_phandle,
88                                          val, sizeof (val) - 1, 0))
89    {
90      grub_ieee1275_ihandle_t stdout_options;
91      val[sizeof (val) - 1] = 0;     
92
93      if (! grub_ieee1275_finddevice (val, &stdout_options)
94          && ! grub_ieee1275_get_property (stdout_options, "device_type",
95                                           val, sizeof (val) - 1, 0))
96        {
97          val[sizeof (val) - 1] = 0;
98          if (grub_strcmp (val, "serial") == 0)
99            {
100              grub_console_terminfo_output.size.x = 80;
101              grub_console_terminfo_output.size.y = 24;
102              return;
103            }
104        }
105    }
106
107  if (! grub_ieee1275_finddevice ("/options", &options)
108      && options != (grub_ieee1275_ihandle_t) -1)
109    {
110      if (! grub_ieee1275_get_property (options, "screen-#columns",
111                                        val, sizeof (val) - 1, 0))
112        {
113          val[sizeof (val) - 1] = 0;
114          grub_console_terminfo_output.size.x
115            = (grub_uint8_t) grub_strtoul (val, 0, 10);
116        }
117      if (! grub_ieee1275_get_property (options, "screen-#rows",
118                                        val, sizeof (val) - 1, 0))
119        {
120          val[sizeof (val) - 1] = 0;
121          grub_console_terminfo_output.size.y
122            = (grub_uint8_t) grub_strtoul (val, 0, 10);
123        }
124    }
125
126  /* Bogus default value on SLOF in QEMU.  */
127  if (grub_console_terminfo_output.size.x == 200
128      && grub_console_terminfo_output.size.y == 200)
129    {
130      grub_console_terminfo_output.size.x = 80;
131      grub_console_terminfo_output.size.y = 24;
132    }
133
134  /* Use a small console by default.  */
135  if (! grub_console_terminfo_output.size.x)
136    grub_console_terminfo_output.size.x = 80;
137  if (! grub_console_terminfo_output.size.y)
138    grub_console_terminfo_output.size.y = 24;
139}
140
141static void
142grub_console_setcursor (struct grub_term_output *term,
143                          int on)
144{
145  grub_terminfo_setcursor (term, on);
146
147  if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF))
148    return;
149
150  /* Understood by the Open Firmware flavour in OLPC.  */
151  if (on)
152    grub_ieee1275_interpret ("cursor-on", 0);
153  else
154    grub_ieee1275_interpret ("cursor-off", 0);
155}
156
157static grub_err_t
158grub_console_init_input (struct grub_term_input *term)
159{
160  grub_ssize_t actual;
161
162  if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdin", &stdin_ihandle,
163                                          sizeof stdin_ihandle, &actual)
164      || actual != sizeof stdin_ihandle)
165    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdin");
166
167  return grub_terminfo_input_init (term);
168}
169
170static grub_err_t
171grub_console_init_output (struct grub_term_output *term)
172{
173  grub_ssize_t actual;
174
175  /* The latest PowerMacs don't actually initialize the screen for us, so we
176   * use this trick to re-open the output device (but we avoid doing this on
177   * platforms where it's known to be broken). */
178  if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT))
179    grub_ieee1275_interpret ("output-device output", 0);
180
181  if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdout", &stdout_ihandle,
182                                          sizeof stdout_ihandle, &actual)
183      || actual != sizeof stdout_ihandle)
184    return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdout");
185
186  /* Initialize colors.  */
187  if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_SET_COLORS))
188    {
189      unsigned col;
190      for (col = 0; col < ARRAY_SIZE (colors); col++)
191        grub_ieee1275_set_color (stdout_ihandle, col, colors[col].red,
192                                 colors[col].green, colors[col].blue);
193
194      /* Set the right fg and bg colors.  */
195      grub_terminfo_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
196    }
197
198  grub_console_dimensions ();
199
200  grub_terminfo_output_init (term);
201
202  return 0;
203}
204
205
206
207struct grub_terminfo_input_state grub_console_terminfo_input =
208  {
209    .readkey = readkey
210  };
211
212struct grub_terminfo_output_state grub_console_terminfo_output =
213  {
214    .put = put,
215    .size = { 80, 24 }
216  };
217
218static struct grub_term_input grub_console_term_input =
219  {
220    .name = "console",
221    .init = grub_console_init_input,
222    .getkey = grub_terminfo_getkey,
223    .data = &grub_console_terminfo_input
224  };
225
226static struct grub_term_output grub_console_term_output =
227  {
228    .name = "console",
229    .init = grub_console_init_output,
230    .putchar = grub_terminfo_putchar,
231    .getxy = grub_terminfo_getxy,
232    .getwh = grub_terminfo_getwh,
233    .gotoxy = grub_terminfo_gotoxy,
234    .cls = grub_terminfo_cls,
235    .setcolorstate = grub_terminfo_setcolorstate,
236    .setcursor = grub_console_setcursor,
237    .flags = GRUB_TERM_CODE_TYPE_ASCII,
238    .data = &grub_console_terminfo_output,
239    .progress_update_divisor = GRUB_PROGRESS_FAST
240  };
241
242void
243grub_console_init_early (void)
244{
245  grub_term_register_input ("console", &grub_console_term_input);
246  grub_term_register_output ("console", &grub_console_term_output);
247}
248
249void
250grub_console_init_lately (void)
251{
252  const char *type;
253
254  if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_ANSI))
255    type = "dumb";
256  else if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CURSORONOFF_ANSI_BROKEN))
257    type = "ieee1275-nocursor";
258  else
259    type = "ieee1275";
260  grub_terminfo_init ();
261  grub_terminfo_output_register (&grub_console_term_output, type);
262}
263
264void
265grub_console_fini (void)
266{
267}
Note: See TracBrowser for help on using the repository browser.