| 1 |
/* |
| 2 |
* GRUB -- GRand Unified Bootloader |
| 3 |
* Copyright (C) 2002,2003,2005,2007,2008,2009 Free Software Foundation, Inc. |
| 4 |
* |
| 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. |
| 9 |
* |
| 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. |
| 14 |
* |
| 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/>. |
| 17 |
*/ |
| 18 |
|
| 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> |
| 24 |
|
| 25 |
static grub_uint8_t grub_console_cur_color = 0x7; |
| 26 |
|
| 27 |
static void |
| 28 |
int10_9 (grub_uint8_t ch, grub_uint16_t n) |
| 29 |
{ |
| 30 |
struct grub_bios_int_registers regs; |
| 31 |
|
| 32 |
regs.eax = ch | 0x0900; |
| 33 |
regs.ebx = grub_console_cur_color & 0xff; |
| 34 |
regs.ecx = n; |
| 35 |
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; |
| 36 |
grub_bios_interrupt (0x10, ®s); |
| 37 |
} |
| 38 |
|
| 39 |
/* |
| 40 |
* BIOS call "INT 10H Function 03h" to get cursor position |
| 41 |
* Call with %ah = 0x03 |
| 42 |
* %bh = page |
| 43 |
* Returns %ch = starting scan line |
| 44 |
* %cl = ending scan line |
| 45 |
* %dh = row (0 is top) |
| 46 |
* %dl = column (0 is left) |
| 47 |
*/ |
| 48 |
|
| 49 |
|
| 50 |
static struct grub_term_coordinate |
| 51 |
grub_console_getxy (struct grub_term_output *term __attribute__ ((unused))) |
| 52 |
{ |
| 53 |
struct grub_bios_int_registers regs; |
| 54 |
|
| 55 |
regs.eax = 0x0300; |
| 56 |
regs.ebx = 0; |
| 57 |
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; |
| 58 |
grub_bios_interrupt (0x10, ®s); |
| 59 |
|
| 60 |
return (struct grub_term_coordinate) { |
| 61 |
(regs.edx & 0xff), ((regs.edx & 0xff00) >> 8) }; |
| 62 |
} |
| 63 |
|
| 64 |
/* |
| 65 |
* BIOS call "INT 10H Function 02h" to set cursor position |
| 66 |
* Call with %ah = 0x02 |
| 67 |
* %bh = page |
| 68 |
* %dh = row (0 is top) |
| 69 |
* %dl = column (0 is left) |
| 70 |
*/ |
| 71 |
static void |
| 72 |
grub_console_gotoxy (struct grub_term_output *term __attribute__ ((unused)), |
| 73 |
struct grub_term_coordinate pos) |
| 74 |
{ |
| 75 |
struct grub_bios_int_registers regs; |
| 76 |
|
| 77 |
/* set page to 0 */ |
| 78 |
regs.ebx = 0; |
| 79 |
regs.eax = 0x0200; |
| 80 |
regs.edx = (pos.y << 8) | pos.x; |
| 81 |
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; |
| 82 |
grub_bios_interrupt (0x10, ®s); |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* |
| 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. |
| 93 |
*/ |
| 94 |
static void |
| 95 |
grub_console_putchar_real (grub_uint8_t c) |
| 96 |
{ |
| 97 |
struct grub_bios_int_registers regs; |
| 98 |
struct grub_term_coordinate pos; |
| 99 |
|
| 100 |
if (c == 7 || c == 8 || c == 0xa || c == 0xd) |
| 101 |
{ |
| 102 |
regs.eax = c | 0x0e00; |
| 103 |
regs.ebx = 0x0001; |
| 104 |
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; |
| 105 |
grub_bios_interrupt (0x10, ®s); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
/* get the current position */ |
| 110 |
pos = grub_console_getxy (NULL); |
| 111 |
|
| 112 |
/* write the character with the attribute */ |
| 113 |
int10_9 (c, 1); |
| 114 |
|
| 115 |
/* check the column with the width */ |
| 116 |
if (pos.x >= 79) |
| 117 |
{ |
| 118 |
grub_console_putchar_real (0x0d); |
| 119 |
grub_console_putchar_real (0x0a); |
| 120 |
} |
| 121 |
else |
| 122 |
grub_console_gotoxy (NULL, (struct grub_term_coordinate) { pos.x + 1, |
| 123 |
pos.y }); |
| 124 |
} |
| 125 |
|
| 126 |
static void |
| 127 |
grub_console_putchar (struct grub_term_output *term __attribute__ ((unused)), |
| 128 |
const struct grub_unicode_glyph *c) |
| 129 |
{ |
| 130 |
grub_console_putchar_real (c->base); |
| 131 |
} |
| 132 |
|
| 133 |
/* |
| 134 |
* BIOS call "INT 10H Function 09h" to write character and attribute |
| 135 |
* Call with %ah = 0x09 |
| 136 |
* %al = (character) |
| 137 |
* %bh = (page number) |
| 138 |
* %bl = (attribute) |
| 139 |
* %cx = (number of times) |
| 140 |
*/ |
| 141 |
static void |
| 142 |
grub_console_cls (struct grub_term_output *term) |
| 143 |
{ |
| 144 |
/* move the cursor to the beginning */ |
| 145 |
grub_console_gotoxy (term, (struct grub_term_coordinate) { 0, 0 }); |
| 146 |
|
| 147 |
/* write spaces to the entire screen */ |
| 148 |
int10_9 (' ', 80 * 25); |
| 149 |
|
| 150 |
/* move back the cursor */ |
| 151 |
grub_console_gotoxy (term, (struct grub_term_coordinate) { 0, 0 }); |
| 152 |
} |
| 153 |
|
| 154 |
/* |
| 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 |
| 160 |
*/ |
| 161 |
static void |
| 162 |
grub_console_setcursor (struct grub_term_output *term __attribute__ ((unused)), |
| 163 |
int on) |
| 164 |
{ |
| 165 |
static grub_uint16_t console_cursor_shape = 0; |
| 166 |
struct grub_bios_int_registers regs; |
| 167 |
|
| 168 |
/* check if the standard cursor shape has already been saved */ |
| 169 |
if (!console_cursor_shape) |
| 170 |
{ |
| 171 |
regs.eax = 0x0300; |
| 172 |
regs.ebx = 0; |
| 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; |
| 178 |
} |
| 179 |
/* set %cx to the designated cursor shape */ |
| 180 |
regs.ecx = on ? console_cursor_shape : 0x2000; |
| 181 |
regs.eax = 0x0100; |
| 182 |
regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT; |
| 183 |
grub_bios_interrupt (0x10, ®s); |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 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 |
| 190 |
* Return: |
| 191 |
* If key waiting to be input: |
| 192 |
* %ah = keyboard scan code |
| 193 |
* %al = ASCII character |
| 194 |
* Zero flag = clear |
| 195 |
* else |
| 196 |
* Zero flag = set |
| 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 |
| 201 |
*/ |
| 202 |
|
| 203 |
static int |
| 204 |
grub_console_getkey (struct grub_term_input *term __attribute__ ((unused))) |
| 205 |
{ |
| 206 |
const grub_uint16_t bypass_table[] = { |
| 207 |
0x0100 | GRUB_TERM_ESC, 0x0f00 | GRUB_TERM_TAB, 0x0e00 | GRUB_TERM_BACKSPACE, 0x1c00 | '\r', 0x1c00 | '\n' |
| 208 |
}; |
| 209 |
struct grub_bios_int_registers regs; |
| 210 |
unsigned i; |
| 211 |
|
| 212 |
/* |
| 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. |
| 217 |
*/ |
| 218 |
|
| 219 |
regs.eax = 0x0100; |
| 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; |
| 224 |
|
| 225 |
regs.eax = 0x0000; |
| 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; |
| 230 |
|
| 231 |
if ((regs.eax & 0xff) >= ' ') |
| 232 |
return regs.eax & 0xff; |
| 233 |
|
| 234 |
for (i = 0; i < ARRAY_SIZE (bypass_table); i++) |
| 235 |
if (bypass_table[i] == (regs.eax & 0xffff)) |
| 236 |
return regs.eax & 0xff; |
| 237 |
|
| 238 |
return (regs.eax & 0xff) + (('a' - 1) | GRUB_TERM_CTRL); |
| 239 |
} |
| 240 |
|
| 241 |
static int |
| 242 |
grub_console_getkeystatus (struct grub_term_input *term __attribute__ ((unused))) |
| 243 |
{ |
| 244 |
const struct grub_machine_bios_data_area *bios_data_area = |
| 245 |
(struct grub_machine_bios_data_area *) grub_absolute_pointer (GRUB_MEMORY_MACHINE_BIOS_DATA_AREA_ADDR); |
| 246 |
/* conveniently GRUB keystatus is modelled after BIOS one. */ |
| 247 |
return bios_data_area->keyboard_flag_lower & ~0x80; |
| 248 |
} |
| 249 |
|
| 250 |
static struct grub_term_coordinate |
| 251 |
grub_console_getwh (struct grub_term_output *term __attribute__ ((unused))) |
| 252 |
{ |
| 253 |
return (struct grub_term_coordinate) { 80, 25 }; |
| 254 |
} |
| 255 |
|
| 256 |
static void |
| 257 |
grub_console_setcolorstate (struct grub_term_output *term |
| 258 |
__attribute__ ((unused)), |
| 259 |
grub_term_color_state state) |
| 260 |
{ |
| 261 |
switch (state) { |
| 262 |
case GRUB_TERM_COLOR_STANDARD: |
| 263 |
grub_console_cur_color = GRUB_TERM_DEFAULT_STANDARD_COLOR & 0x7f; |
| 264 |
break; |
| 265 |
case GRUB_TERM_COLOR_NORMAL: |
| 266 |
grub_console_cur_color = grub_term_normal_color & 0x7f; |
| 267 |
break; |
| 268 |
case GRUB_TERM_COLOR_HIGHLIGHT: |
| 269 |
grub_console_cur_color = grub_term_highlight_color & 0x7f; |
| 270 |
break; |
| 271 |
default: |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
static struct grub_term_input grub_console_term_input = |
| 277 |
{ |
| 278 |
.name = "console", |
| 279 |
.getkey = grub_console_getkey, |
| 280 |
.getkeystatus = grub_console_getkeystatus |
| 281 |
}; |
| 282 |
|
| 283 |
static struct grub_term_output grub_console_term_output = |
| 284 |
{ |
| 285 |
.name = "console", |
| 286 |
.putchar = grub_console_putchar, |
| 287 |
.getwh = grub_console_getwh, |
| 288 |
.getxy = grub_console_getxy, |
| 289 |
.gotoxy = grub_console_gotoxy, |
| 290 |
.cls = grub_console_cls, |
| 291 |
.setcolorstate = grub_console_setcolorstate, |
| 292 |
.setcursor = grub_console_setcursor, |
| 293 |
.flags = GRUB_TERM_CODE_TYPE_CP437, |
| 294 |
.progress_update_divisor = GRUB_PROGRESS_FAST |
| 295 |
}; |
| 296 |
|
| 297 |
void |
| 298 |
grub_console_init (void) |
| 299 |
{ |
| 300 |
grub_term_register_output ("console", &grub_console_term_output); |
| 301 |
grub_term_register_input ("console", &grub_console_term_input); |
| 302 |
} |
| 303 |
|
| 304 |
void |
| 305 |
grub_console_fini (void) |
| 306 |
{ |
| 307 |
grub_term_unregister_input (&grub_console_term_input); |
| 308 |
grub_term_unregister_output (&grub_console_term_output); |
| 309 |
} |