Upgraded GRUB2 to 2.00 release.
[AROS.git] / arch / all-pc / boot / grub2-aros / grub-core / term / i386 / pc / console.c
blob7cf5ffc5f36d0b26cfecec55e9e99e93c916b4ff
1 /*
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 void
26 int10_9 (grub_uint8_t ch, grub_uint16_t n)
28 struct grub_bios_int_registers regs;
30 regs.eax = ch | 0x0900;
31 regs.ebx = grub_console_cur_color & 0xff;
32 regs.ecx = n;
33 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
34 grub_bios_interrupt (0x10, &regs);
38 * BIOS call "INT 10H Function 03h" to get cursor position
39 * Call with %ah = 0x03
40 * %bh = page
41 * Returns %ch = starting scan line
42 * %cl = ending scan line
43 * %dh = row (0 is top)
44 * %dl = column (0 is left)
48 static grub_uint16_t
49 grub_console_getxy (struct grub_term_output *term __attribute__ ((unused)))
51 struct grub_bios_int_registers regs;
53 regs.eax = 0x0300;
54 regs.ebx = 0;
55 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
56 grub_bios_interrupt (0x10, &regs);
58 return ((regs.edx & 0xff) << 8) | ((regs.edx & 0xff00) >> 8);
62 * BIOS call "INT 10H Function 02h" to set cursor position
63 * Call with %ah = 0x02
64 * %bh = page
65 * %dh = row (0 is top)
66 * %dl = column (0 is left)
68 static void
69 grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
70 grub_uint8_t x, grub_uint8_t y)
72 struct grub_bios_int_registers regs;
74 /* set page to 0 */
75 regs.ebx = 0;
76 regs.eax = 0x0200;
77 regs.edx = (y << 8) | x;
78 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
79 grub_bios_interrupt (0x10, &regs);
84 * Put the character C on the console. Because GRUB wants to write a
85 * character with an attribute, this implementation is a bit tricky.
86 * If C is a control character (CR, LF, BEL, BS), use INT 10, AH = 0Eh
87 * (TELETYPE OUTPUT). Otherwise, save the original position, put a space,
88 * save the current position, restore the original position, write the
89 * character and the attribute, and restore the current position.
91 * The reason why this is so complicated is that there is no easy way to
92 * get the height of the screen, and the TELETYPE OUTPUT BIOS call doesn't
93 * support setting a background attribute.
95 static void
96 grub_console_putchar_real (grub_uint8_t c)
98 struct grub_bios_int_registers regs;
99 grub_uint16_t pos;
101 if (c == 7 || c == 8 || c == 0xa || c == 0xd)
103 regs.eax = c | 0x0e00;
104 regs.ebx = 0x0001;
105 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
106 grub_bios_interrupt (0x10, &regs);
107 return;
110 /* get the current position */
111 pos = grub_console_getxy (NULL);
113 /* check the column with the width */
114 if ((pos & 0xff00) >= (79 << 8))
116 grub_console_putchar_real (0x0d);
117 grub_console_putchar_real (0x0a);
118 /* get the current position */
119 pos = grub_console_getxy (NULL);
122 /* write the character with the attribute */
123 int10_9 (c, 1);
125 grub_console_gotoxy (NULL, ((pos & 0xff00) >> 8) + 1, (pos & 0xff));
128 static void
129 grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)),
130 const struct grub_unicode_glyph *c)
132 grub_console_putchar_real (c->base);
136 * BIOS call "INT 10H Function 09h" to write character and attribute
137 * Call with %ah = 0x09
138 * %al = (character)
139 * %bh = (page number)
140 * %bl = (attribute)
141 * %cx = (number of times)
143 static void
144 grub_console_cls (struct grub_term_output *term)
146 /* move the cursor to the beginning */
147 grub_console_gotoxy (term, 0, 0);
149 /* write spaces to the entire screen */
150 int10_9 (' ', 80 * 25);
152 /* move back the cursor */
153 grub_console_gotoxy (term, 0, 0);
157 * void grub_console_setcursor (int on)
158 * BIOS call "INT 10H Function 01h" to set cursor type
159 * Call with %ah = 0x01
160 * %ch = cursor starting scanline
161 * %cl = cursor ending scanline
163 static void
164 grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)),
165 int on)
167 static grub_uint16_t console_cursor_shape = 0;
168 struct grub_bios_int_registers regs;
170 /* check if the standard cursor shape has already been saved */
171 if (!console_cursor_shape)
173 regs.eax = 0x0300;
174 regs.ebx = 0;
175 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
176 grub_bios_interrupt (0x10, &regs);
177 console_cursor_shape = regs.ecx;
178 if ((console_cursor_shape >> 8) >= (console_cursor_shape & 0xff))
179 console_cursor_shape = 0x0d0e;
181 /* set %cx to the designated cursor shape */
182 regs.ecx = on ? console_cursor_shape : 0x2000;
183 regs.eax = 0x0100;
184 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
185 grub_bios_interrupt (0x10, &regs);
189 * if there is a character pending, return it; otherwise return -1
190 * BIOS call "INT 16H Function 01H" to check whether a character is pending
191 * Call with %ah = 0x1
192 * Return:
193 * If key waiting to be input:
194 * %ah = keyboard scan code
195 * %al = ASCII character
196 * Zero flag = clear
197 * else
198 * Zero flag = set
199 * BIOS call "INT 16H Function 00H" to read character from keyboard
200 * Call with %ah = 0x0
201 * Return: %ah = keyboard scan code
202 * %al = ASCII character
205 static int
206 grub_console_getkey (struct grub_term_input *term __attribute__ ((unused)))
208 const grub_uint16_t bypass_table[] = {
209 0x0100 | '\e', 0x0f00 | '\t', 0x0e00 | '\b', 0x1c00 | '\r', 0x1c00 | '\n'
211 struct grub_bios_int_registers regs;
212 unsigned i;
215 * Due to a bug in apple's bootcamp implementation, INT 16/AH = 0 would
216 * cause the machine to hang at the second keystroke. However, we can
217 * work around this problem by ensuring the presence of keystroke with
218 * INT 16/AH = 1 before calling INT 16/AH = 0.
221 regs.eax = 0x0100;
222 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
223 grub_bios_interrupt (0x16, &regs);
224 if (regs.flags & GRUB_CPU_INT_FLAGS_ZERO)
225 return GRUB_TERM_NO_KEY;
227 regs.eax = 0x0000;
228 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
229 grub_bios_interrupt (0x16, &regs);
230 if (!(regs.eax & 0xff))
231 return ((regs.eax >> 8) & 0xff) | GRUB_TERM_EXTENDED;
233 if ((regs.eax & 0xff) >= ' ')
234 return regs.eax & 0xff;
236 for (i = 0; i < ARRAY_SIZE (bypass_table); i++)
237 if (bypass_table[i] == (regs.eax & 0xffff))
238 return regs.eax & 0xff;
240 return (regs.eax & 0xff) + (('a' - 1) | GRUB_TERM_CTRL);
243 static const struct grub_machine_bios_data_area *bios_data_area =
244 (struct grub_machine_bios_data_area *) GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR;
246 static int
247 grub_console_getkeystatus (struct grub_term_input *term __attribute__ ((unused)))
249 /* conveniently GRUB keystatus is modelled after BIOS one. */
250 return bios_data_area->keyboard_flag_lower & ~0x80;
253 static struct grub_term_input grub_console_term_input =
255 .name = "console",
256 .getkey = grub_console_getkey,
257 .getkeystatus = grub_console_getkeystatus
260 static struct grub_term_output grub_console_term_output =
262 .name = "console",
263 .putchar = grub_console_putchar,
264 .getwh = grub_console_getwh,
265 .getxy = grub_console_getxy,
266 .gotoxy = grub_console_gotoxy,
267 .cls = grub_console_cls,
268 .setcolorstate = grub_console_setcolorstate,
269 .setcursor = grub_console_setcursor,
270 .flags = GRUB_TERM_CODE_TYPE_CP437,
271 .normal_color = GRUB_TERM_DEFAULT_NORMAL_COLOR,
272 .highlight_color = GRUB_TERM_DEFAULT_HIGHLIGHT_COLOR,
275 void
276 grub_console_init (void)
278 grub_term_register_output ("console", &grub_console_term_output);
279 grub_term_register_input ("console", &grub_console_term_input);
282 void
283 grub_console_fini (void)
285 grub_term_unregister_input (&grub_console_term_input);
286 grub_term_unregister_output (&grub_console_term_output);