Ensure all lines that should be omitted from public includes are marked
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / term / ieee1275 / ofconsole.c
blobab74f21da99e6085031cd4d1c6e29e409121b0c2
1 /* ofconsole.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.
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>
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>
29 static grub_ieee1275_ihandle_t stdout_ihandle;
30 static grub_ieee1275_ihandle_t stdin_ihandle;
32 extern struct grub_terminfo_output_state grub_ofconsole_terminfo_output;
34 struct color
36 int red;
37 int green;
38 int blue;
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[] =
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
56 static void
57 put (struct grub_term_output *term __attribute__ ((unused)), const int c)
59 char chr = c;
61 grub_ieee1275_write (stdout_ihandle, &chr, 1, 0);
64 static int
65 readkey (struct grub_term_input *term __attribute__ ((unused)))
67 grub_uint8_t c;
68 grub_ssize_t actual = 0;
70 grub_ieee1275_read (stdin_ihandle, &c, 1, &actual);
71 if (actual > 0)
72 return c;
73 return -1;
76 static void
77 grub_ofconsole_dimensions (void)
79 grub_ieee1275_ihandle_t options;
80 grub_ssize_t lval;
82 if (! grub_ieee1275_finddevice ("/options", &options)
83 && options != (grub_ieee1275_ihandle_t) -1)
85 if (! grub_ieee1275_get_property_length (options, "screen-#columns",
86 &lval)
87 && lval >= 0 && lval < 1024)
89 char val[lval];
91 if (! grub_ieee1275_get_property (options, "screen-#columns",
92 val, lval, 0))
93 grub_ofconsole_terminfo_output.width
94 = (grub_uint8_t) grub_strtoul (val, 0, 10);
96 if (! grub_ieee1275_get_property_length (options, "screen-#rows", &lval)
97 && lval >= 0 && lval < 1024)
99 char val[lval];
100 if (! grub_ieee1275_get_property (options, "screen-#rows",
101 val, lval, 0))
102 grub_ofconsole_terminfo_output.height
103 = (grub_uint8_t) grub_strtoul (val, 0, 10);
107 /* Use a small console by default. */
108 if (! grub_ofconsole_terminfo_output.width)
109 grub_ofconsole_terminfo_output.width = 80;
110 if (! grub_ofconsole_terminfo_output.height)
111 grub_ofconsole_terminfo_output.height = 24;
114 static void
115 grub_ofconsole_setcursor (struct grub_term_output *term,
116 int on)
118 grub_terminfo_setcursor (term, on);
120 if (!grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_HAS_CURSORONOFF))
121 return;
123 /* Understood by the Open Firmware flavour in OLPC. */
124 if (on)
125 grub_ieee1275_interpret ("cursor-on", 0);
126 else
127 grub_ieee1275_interpret ("cursor-off", 0);
130 static grub_err_t
131 grub_ofconsole_init_input (struct grub_term_input *term)
133 grub_ssize_t actual;
135 if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdin", &stdin_ihandle,
136 sizeof stdin_ihandle, &actual)
137 || actual != sizeof stdin_ihandle)
138 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdin");
140 return grub_terminfo_input_init (term);
143 static grub_err_t
144 grub_ofconsole_init_output (struct grub_term_output *term)
146 grub_ssize_t actual;
148 /* The latest PowerMacs don't actually initialize the screen for us, so we
149 * use this trick to re-open the output device (but we avoid doing this on
150 * platforms where it's known to be broken). */
151 if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_BROKEN_OUTPUT))
152 grub_ieee1275_interpret ("output-device output", 0);
154 if (grub_ieee1275_get_integer_property (grub_ieee1275_chosen, "stdout", &stdout_ihandle,
155 sizeof stdout_ihandle, &actual)
156 || actual != sizeof stdout_ihandle)
157 return grub_error (GRUB_ERR_UNKNOWN_DEVICE, "cannot find stdout");
159 /* Initialize colors. */
160 if (! grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_CANNOT_SET_COLORS))
162 unsigned col;
163 for (col = 0; col < ARRAY_SIZE (colors); col++)
164 grub_ieee1275_set_color (stdout_ihandle, col, colors[col].red,
165 colors[col].green, colors[col].blue);
167 /* Set the right fg and bg colors. */
168 grub_terminfo_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
171 grub_ofconsole_dimensions ();
173 grub_terminfo_output_init (term);
175 return 0;
180 struct grub_terminfo_input_state grub_ofconsole_terminfo_input =
182 .readkey = readkey
185 struct grub_terminfo_output_state grub_ofconsole_terminfo_output =
187 .put = put,
188 .width = 80,
189 .height = 24
192 static struct grub_term_input grub_ofconsole_term_input =
194 .name = "ofconsole",
195 .init = grub_ofconsole_init_input,
196 .getkey = grub_terminfo_getkey,
197 .data = &grub_ofconsole_terminfo_input
200 static struct grub_term_output grub_ofconsole_term_output =
202 .name = "ofconsole",
203 .init = grub_ofconsole_init_output,
204 .putchar = grub_terminfo_putchar,
205 .getxy = grub_terminfo_getxy,
206 .getwh = grub_terminfo_getwh,
207 .gotoxy = grub_terminfo_gotoxy,
208 .cls = grub_terminfo_cls,
209 .setcolorstate = grub_terminfo_setcolorstate,
210 .setcursor = grub_ofconsole_setcursor,
211 .flags = GRUB_TERM_CODE_TYPE_ASCII,
212 .data = &grub_ofconsole_terminfo_output,
213 .normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
214 .highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
217 void grub_terminfo_fini (void);
218 void grub_terminfo_init (void);
220 void
221 grub_console_init_early (void)
223 grub_term_register_input ("ofconsole", &grub_ofconsole_term_input);
224 grub_term_register_output ("ofconsole", &grub_ofconsole_term_output);
227 void
228 grub_console_init_lately (void)
230 const char *type;
232 if (grub_ieee1275_test_flag (GRUB_IEEE1275_FLAG_NO_ANSI))
233 type = "dumb";
234 else
235 type = "ieee1275";
237 grub_terminfo_init ();
238 grub_terminfo_output_register (&grub_ofconsole_term_output, type);
241 void
242 grub_console_fini (void)
244 grub_term_unregister_input (&grub_ofconsole_term_input);
245 grub_term_unregister_output (&grub_ofconsole_term_output);
246 grub_terminfo_output_unregister (&grub_ofconsole_term_output);
248 grub_terminfo_fini ();