2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/machine/memory.h>
20 #include <grub/machine/console.h>
21 #include <grub/term.h>
22 #include <grub/types.h>
23 #include <grub/machine/int.h>
25 static grub_uint8_t grub_console_cur_color = 0x7;
28 int10_9 (grub_uint8_t ch, grub_uint16_t n)
30 struct grub_bios_int_registers regs;
32 regs.eax = ch | 0x0900;
33 regs.ebx = grub_console_cur_color & 0xff;
35 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
36 grub_bios_interrupt (0x10, ®s);
40 * BIOS call "INT 10H Function 03h" to get cursor position
41 * Call with %ah = 0x03
43 * Returns %ch = starting scan line
44 * %cl = ending scan line
45 * %dh = row (0 is top)
46 * %dl = column (0 is left)
50 static struct grub_term_coordinate
51 grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
53 struct grub_bios_int_registers regs;
57 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
58 grub_bios_interrupt (0x10, ®s);
60 return (struct grub_term_coordinate) {
61 (regs.edx & 0xff), ((regs.edx & 0xff00) >> 8) };
65 * BIOS call "INT 10H Function 02h" to set cursor position
66 * Call with %ah = 0x02
68 * %dh = row (0 is top)
69 * %dl = column (0 is left)
72 grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
73 struct grub_term_coordinate pos)
75 struct grub_bios_int_registers regs;
80 regs.edx = (pos.y << 8) | pos.x;
81 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
82 grub_bios_interrupt (0x10, ®s);
87 * Put the character C on the console. Because GRUB wants to write a
88 * character with an attribute, this implementation is a bit tricky.
89 * If C is a control character (CR, LF, BEL, BS), use INT 10, AH = 0Eh
90 * (TELETYPE OUTPUT). Otherwise, use INT 10, AH = 9 to write character
91 * with attributes and advance cursor. If we are on the last column,
92 * let BIOS to wrap line correctly.
95 grub_console_putchar_real (grub_uint8_t c)
97 struct grub_bios_int_registers regs;
98 struct grub_term_coordinate pos;
100 if (c == 7 || c == 8 || c == 0xa || c == 0xd)
102 regs.eax = c | 0x0e00;
104 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
105 grub_bios_interrupt (0x10, ®s);
109 /* get the current position */
110 pos = grub_console_getxy (NULL);
112 /* write the character with the attribute */
115 /* check the column with the width */
118 grub_console_putchar_real (0x0d);
119 grub_console_putchar_real (0x0a);
122 grub_console_gotoxy (NULL, (struct grub_term_coordinate) { pos.x + 1,
127 grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
128 const struct grub_unicode_glyph *c)
130 grub_console_putchar_real (c->base);
134 * BIOS call "INT 10H Function 09h" to write character and attribute
135 * Call with %ah = 0x09
137 * %bh = (page number)
139 * %cx = (number of times)
142 grub_console_cls (struct grub_term_output *term)
144 /* move the cursor to the beginning */
145 grub_console_gotoxy (term, (struct grub_term_coordinate) { 0, 0 });
147 /* write spaces to the entire screen */
148 int10_9 (' ', 80 * 25);
150 /* move back the cursor */
151 grub_console_gotoxy (term, (struct grub_term_coordinate) { 0, 0 });
155 * void grub_console_setcursor (int on)
156 * BIOS call "INT 10H Function 01h" to set cursor type
157 * Call with %ah = 0x01
158 * %ch = cursor starting scanline
159 * %cl = cursor ending scanline
162 grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
165 static grub_uint16_t console_cursor_shape = 0;
166 struct grub_bios_int_registers regs;
168 /* check if the standard cursor shape has already been saved */
169 if (!console_cursor_shape)
173 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
174 grub_bios_interrupt (0x10, ®s);
175 console_cursor_shape = regs.ecx;
176 if ((console_cursor_shape >> 8) >= (console_cursor_shape & 0xff))
177 console_cursor_shape = 0x0d0e;
179 /* set %cx to the designated cursor shape */
180 regs.ecx = on ? console_cursor_shape : 0x2000;
182 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
183 grub_bios_interrupt (0x10, ®s);
187 * if there is a character pending, return it; otherwise return -1
188 * BIOS call "INT 16H Function 01H" to check whether a character is pending
189 * Call with %ah = 0x1
191 * If key waiting to be input:
192 * %ah = keyboard scan code
193 * %al = ASCII character
197 * BIOS call "INT 16H Function 00H" to read character from keyboard
198 * Call with %ah = 0x0
199 * Return: %ah = keyboard scan code
200 * %al = ASCII character
204 grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
206 const grub_uint16_t bypass_table[] = {
207 0x0100 | GRUB_TERM_ESC, 0x0f00 | GRUB_TERM_TAB, 0x0e00 | GRUB_TERM_BACKSPACE, 0x1c00 | '\r', 0x1c00 | '\n'
209 struct grub_bios_int_registers regs;
213 * Due to a bug in apple's bootcamp implementation, INT 16/AH = 0 would
214 * cause the machine to hang at the second keystroke. However, we can
215 * work around this problem by ensuring the presence of keystroke with
216 * INT 16/AH = 1 before calling INT 16/AH = 0.
220 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
221 grub_bios_interrupt (0x16, ®s);
222 if (regs.flags & GRUB_CPU_INT_FLAGS_ZERO)
223 return GRUB_TERM_NO_KEY;
226 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
227 grub_bios_interrupt (0x16, ®s);
228 if (!(regs.eax & 0xff))
229 return ((regs.eax >> 8) & 0xff) | GRUB_TERM_EXTENDED;
231 if ((regs.eax & 0xff) >= ' ')
232 return regs.eax & 0xff;
234 for (i = 0; i < ARRAY_SIZE (bypass_table); i++)
235 if (bypass_table[i] == (regs.eax & 0xffff))
236 return regs.eax & 0xff;
238 return (regs.eax & 0xff) + (('a' - 1) | GRUB_TERM_CTRL);
241 static const struct grub_machine_bios_data_area *bios_data_area =
242 (struct grub_machine_bios_data_area *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
245 grub_console_getkeystatus (struct grub_term_input *term __attribute__ ((unused)))
247 /* conveniently GRUB keystatus is modelled after BIOS one. */
248 return bios_data_area->keyboard_flag_lower & ~0x80;
251 static struct grub_term_coordinate
252 grub_console_getwh (struct grub_term_output *term __attribute__ ((unused)))
254 return (struct grub_term_coordinate) { 80, 25 };
258 grub_console_setcolorstate (struct grub_term_output *term
259 __attribute__ ((unused)),
260 grub_term_color_state state)
263 case GRUB_TERM_COLOR_STANDARD:
264 grub_console_cur_color = GRUB_TERM_DEFAULT_STANDARD_COLOR & 0x7f;
266 case GRUB_TERM_COLOR_NORMAL:
267 grub_console_cur_color = grub_term_normal_color & 0x7f;
269 case GRUB_TERM_COLOR_HIGHLIGHT:
270 grub_console_cur_color = grub_term_highlight_color & 0x7f;
277 static struct grub_term_input grub_console_term_input =
280 .getkey = grub_console_getkey,
281 .getkeystatus = grub_console_getkeystatus
284 static struct grub_term_output grub_console_term_output =
287 .putchar = grub_console_putchar,
288 .getwh = grub_console_getwh,
289 .getxy = grub_console_getxy,
290 .gotoxy = grub_console_gotoxy,
291 .cls = grub_console_cls,
292 .setcolorstate = grub_console_setcolorstate,
293 .setcursor = grub_console_setcursor,
294 .flags = GRUB_TERM_CODE_TYPE_CP437,
295 .progress_update_divisor = GRUB_PROGRESS_FAST
299 grub_console_init (void)
301 grub_term_register_output ("console", &grub_console_term_output);
302 grub_term_register_input ("console", &grub_console_term_input);
306 grub_console_fini (void)
308 grub_term_unregister_input (&grub_console_term_input);
309 grub_term_unregister_output (&grub_console_term_output);