Debian Codegraph @

grub-core/term/i386/pc/vga_text.c

raw
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2007, 2008, 2010 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/dl.h>
20 #include <grub/cpu/io.h>
21 #include <grub/types.h>
22 #include <grub/vga.h>
23 #include <grub/term.h>
24
25 #if defined (GRUB_MACHINE_COREBOOT)
26 #include <grub/machine/console.h>
27 #endif
28
29 /* MODESET is used for testing to force monochrome or colour mode.
30 You shouldn't use mda_text on vga.
31 */
32 #ifdef MODESET
33 #include <grub/machine/int.h>
34 #endif
35
36 #if defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_QEMU) || defined (GRUB_MACHINE_MIPS_QEMU_MIPS) || defined (GRUB_MACHINE_MULTIBOOT)
37 #include <grub/machine/console.h>
38 #endif
39
40 GRUB_MOD_LICENSE ("GPLv3+");
41
42 #define COLS 80
43 #define ROWS 25
44
45 static struct grub_term_coordinate grub_curr_pos;
46
47 #ifdef __mips__
48 #define VGA_TEXT_SCREEN ((grub_uint16_t *) grub_absolute_pointer (0xb00b8000))
49 #define cr_read grub_vga_cr_read
50 #define cr_write grub_vga_cr_write
51 #elif defined (MODE_MDA)
52 #define VGA_TEXT_SCREEN ((grub_uint16_t *) grub_absolute_pointer (0xb0000))
53 #define cr_read grub_vga_cr_bw_read
54 #define cr_write grub_vga_cr_bw_write
55 #else
56 #define VGA_TEXT_SCREEN ((grub_uint16_t *) grub_absolute_pointer (0xb8000))
57 #define cr_read grub_vga_cr_read
58 #define cr_write grub_vga_cr_write
59 #endif
60
61 static grub_uint8_t cur_color = 0x7;
62
63 static void
64 screen_write_char (int x, int y, short c)
65 {
66 if (x < COLS && y < ROWS && x >= 0 && y >= 0)
67 VGA_TEXT_SCREEN[y * COLS + x] = grub_cpu_to_le16 (c);
68 }
69
70 static short
71 screen_read_char (int x, int y)
72 {
73 return grub_le_to_cpu16 (VGA_TEXT_SCREEN[y * COLS + x]);
74 }
75
76 static void
77 update_cursor (void)
78 {
79 unsigned int pos = grub_curr_pos.y * COLS + grub_curr_pos.x;
80 cr_write (pos >> 8, GRUB_VGA_CR_CURSOR_ADDR_HIGH);
81 cr_write (pos & 0xFF, GRUB_VGA_CR_CURSOR_ADDR_LOW);
82 }
83
84 static void
85 inc_y (void)
86 {
87 grub_curr_pos.x = 0;
88 if (grub_curr_pos.y < ROWS - 1)
89 grub_curr_pos.y++;
90 else
91 {
92 int x, y;
93 for (y = 0; y < ROWS - 1; y++)
94 for (x = 0; x < COLS; x++)
95 screen_write_char (x, y, screen_read_char (x, y + 1));
96 for (x = 0; x < COLS; x++)
97 screen_write_char (x, ROWS - 1, ' ' | (cur_color << 8));
98 }
99 }
100
101 static void
102 inc_x (void)
103 {
104 if (grub_curr_pos.x >= COLS - 1)
105 inc_y ();
106 else
107 grub_curr_pos.x++;
108 }
109
110 static void
111 grub_vga_text_putchar (struct grub_term_output *term __attribute__ ((unused)),
112 const struct grub_unicode_glyph *c)
113 {
114 switch (c->base)
115 {
116 case '\b':
117 if (grub_curr_pos.x != 0)
118 screen_write_char (grub_curr_pos.x--, grub_curr_pos.y, ' ');
119 break;
120 case '\n':
121 inc_y ();
122 break;
123 case '\r':
124 grub_curr_pos.x = 0;
125 break;
126 default:
127 screen_write_char (grub_curr_pos.x, grub_curr_pos.y,
128 c->base | (cur_color << 8));
129 inc_x ();
130 }
131
132 update_cursor ();
133 }
134
135 static struct grub_term_coordinate
136 grub_vga_text_getxy (struct grub_term_output *term __attribute__ ((unused)))
137 {
138 return grub_curr_pos;
139 }
140
141 static void
142 grub_vga_text_gotoxy (struct grub_term_output *term __attribute__ ((unused)),
143 struct grub_term_coordinate pos)
144 {
145 grub_curr_pos = pos;
146 update_cursor ();
147 }
148
149 static void
150 grub_vga_text_cls (struct grub_term_output *term)
151 {
152 int i;
153 for (i = 0; i < ROWS * COLS; i++)
154 VGA_TEXT_SCREEN[i] = grub_cpu_to_le16 (' ' | (cur_color << 8));
155 grub_vga_text_gotoxy (term, (struct grub_term_coordinate) { 0, 0 });
156 }
157
158 static void
159 grub_vga_text_setcursor (struct grub_term_output *term __attribute__ ((unused)),
160 int on)
161 {
162 grub_uint8_t old;
163 old = cr_read (GRUB_VGA_CR_CURSOR_START);
164 if (on)
165 cr_write (old & ~GRUB_VGA_CR_CURSOR_START_DISABLE,
166 GRUB_VGA_CR_CURSOR_START);
167 else
168 cr_write (old | GRUB_VGA_CR_CURSOR_START_DISABLE,
169 GRUB_VGA_CR_CURSOR_START);
170 }
171
172 static grub_err_t
173 grub_vga_text_init_real (struct grub_term_output *term)
174 {
175 #ifdef MODESET
176 struct grub_bios_int_registers regs;
177 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
178
179 #ifdef MODE_MDA
180 regs.eax = 7;
181 #else
182 regs.eax = 3;
183 #endif
184 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
185 grub_bios_interrupt (0x10, &regs);
186 #endif
187 grub_vga_text_cls (term);
188 return 0;
189 }
190
191 static grub_err_t
192 grub_vga_text_fini_real (struct grub_term_output *term)
193 {
194 #ifdef MODESET
195 struct grub_bios_int_registers regs;
196 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
197
198 regs.eax = 3;
199 regs.flags = GRUB_CPU_INT_FLAGS_DEFAULT;
200 grub_bios_interrupt (0x10, &regs);
201 #endif
202 grub_vga_text_cls (term);
203 return 0;
204 }
205
206 static struct grub_term_coordinate
207 grub_vga_text_getwh (struct grub_term_output *term __attribute__ ((unused)))
208 {
209 return (struct grub_term_coordinate) { 80, 25 };
210 }
211
212 #ifndef MODE_MDA
213
214 static void
215 grub_vga_text_setcolorstate (struct grub_term_output *term __attribute__ ((unused)),
216 grub_term_color_state state)
217 {
218 switch (state) {
219 case GRUB_TERM_COLOR_STANDARD:
220 cur_color = GRUB_TERM_DEFAULT_STANDARD_COLOR & 0x7f;
221 break;
222 case GRUB_TERM_COLOR_NORMAL:
223 cur_color = grub_term_normal_color & 0x7f;
224 break;
225 case GRUB_TERM_COLOR_HIGHLIGHT:
226 cur_color = grub_term_highlight_color & 0x7f;
227 break;
228 default:
229 break;
230 }
231 }
232
233 #else
234 static void
235 grub_vga_text_setcolorstate (struct grub_term_output *term __attribute__ ((unused)),
236 grub_term_color_state state)
237 {
238 switch (state) {
239 case GRUB_TERM_COLOR_STANDARD:
240 cur_color = 0x07;
241 break;
242 case GRUB_TERM_COLOR_NORMAL:
243 cur_color = 0x07;
244 break;
245 case GRUB_TERM_COLOR_HIGHLIGHT:
246 cur_color = 0x70;
247 break;
248 default:
249 break;
250 }
251 }
252 #endif
253
254 static struct grub_term_output grub_vga_text_term =
255 {
256 #ifdef MODE_MDA
257 .name = "mda_text",
258 #else
259 .name = "vga_text",
260 #endif
261 .init = grub_vga_text_init_real,
262 .fini = grub_vga_text_fini_real,
263 .putchar = grub_vga_text_putchar,
264 .getwh = grub_vga_text_getwh,
265 .getxy = grub_vga_text_getxy,
266 .gotoxy = grub_vga_text_gotoxy,
267 .cls = grub_vga_text_cls,
268 .setcolorstate = grub_vga_text_setcolorstate,
269 .setcursor = grub_vga_text_setcursor,
270 .flags = GRUB_TERM_CODE_TYPE_CP437,
271 .progress_update_divisor = GRUB_PROGRESS_FAST
272 };
273
274 #ifndef MODE_MDA
275
276 GRUB_MOD_INIT(vga_text)
277 {
278 #ifdef GRUB_MACHINE_COREBOOT
279 if (!grub_video_coreboot_fbtable)
280 #endif
281 grub_term_register_output ("vga_text", &grub_vga_text_term);
282 }
283
284 GRUB_MOD_FINI(vga_text)
285 {
286 grub_term_unregister_output (&grub_vga_text_term);
287 }
288
289 #endif