1 /* -*- linux-c -*- ------------------------------------------------------- *
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright 2007 rPath, Inc. - All Rights Reserved
6 * This file is part of the Linux kernel, and is made available under
7 * the terms of the GNU General Public License version 2.
9 * ----------------------------------------------------------------------- */
12 * arch/i386/boot/video.c
24 static struct card_info cards[]; /* List of cards to probe for */
29 int adapter; /* 0=CGA/MDA/HGC, 1=EGA, 2=VGA+ */
31 int force_x, force_y; /* Don't query the BIOS for cols/rows */
33 int do_restore = 0; /* Screen contents changed during mode flip */
34 int graphic_mode; /* Graphic mode with linear frame buffer */
36 static void store_cursor_position(void)
44 : "=d" (curpos), "+a" (ax), "+b" (bx)
45 : : "ecx", "esi", "edi");
47 boot_params.screen_info.orig_x = curpos;
48 boot_params.screen_info.orig_y = curpos >> 8;
51 static void store_video_mode(void)
55 /* N.B.: the saving of the video page here is a bit silly,
56 since we pretty much assume page 0 everywhere. */
59 : "+a" (ax), "=b" (page)
60 : : "ecx", "edx", "esi", "edi");
62 /* Not all BIOSes are clean with respect to the top bit */
63 boot_params.screen_info.orig_video_mode = ax & 0x7f;
64 boot_params.screen_info.orig_video_page = page >> 8;
68 * Store the video mode parameters for later usage by the kernel.
69 * This is done by asking the BIOS except for the rows/columns
70 * parameters in the default 80x25 mode -- these are set directly,
71 * because some very obscure BIOSes supply insane values.
73 static void store_mode_params(void)
78 /* For graphics mode, it is up to the mode-setting driver
79 (currently only video-vesa.c) to store the parameters */
83 store_cursor_position();
86 if (boot_params.screen_info.orig_video_mode == 0x07) {
87 /* MDA, HGC, or VGA in monochrome mode */
88 video_segment = 0xb000;
90 /* CGA, EGA, VGA and so forth */
91 video_segment = 0xb800;
95 font_size = rdfs16(0x485); /* Font size, BIOS area */
96 boot_params.screen_info.orig_video_points = font_size;
99 y = (adapter == ADAPTER_CGA) ? 25 : rdfs8(0x484)+1;
106 boot_params.screen_info.orig_video_cols = x;
107 boot_params.screen_info.orig_video_lines = y;
110 /* Probe the video drivers and have them generate their mode lists. */
111 static void probe_cards(int unsafe)
113 struct card_info *card;
121 for (card = video_cards; card < video_cards_end; card++) {
122 if (card->unsafe == unsafe) {
124 card->nmodes = card->probe();
131 /* Test if a mode is defined */
132 int mode_defined(u16 mode)
134 struct card_info *card;
135 struct mode_info *mi;
138 for (card = video_cards; card < video_cards_end; card++) {
140 for (i = 0; i < card->nmodes; i++, mi++) {
141 if (mi->mode == mode)
149 /* Set mode (without recalc) */
150 static int raw_set_mode(u16 mode, u16 *real_mode)
153 struct card_info *card;
154 struct mode_info *mi;
156 /* Drop the recalc bit if set */
157 mode &= ~VIDEO_RECALC;
159 /* Scan for mode based on fixed ID, position, or resolution */
161 for (card = video_cards; card < video_cards_end; card++) {
163 for (i = 0; i < card->nmodes; i++, mi++) {
164 int visible = mi->x || mi->y;
166 if ((mode == nmode && visible) ||
168 mode == (mi->y << 8)+mi->x) {
169 *real_mode = mi->mode;
170 return card->set_mode(mi);
178 /* Nothing found? Is it an "exceptional" (unprobed) mode? */
179 for (card = video_cards; card < video_cards_end; card++) {
180 if (mode >= card->xmode_first &&
181 mode < card->xmode_first+card->xmode_n) {
182 struct mode_info mix;
183 *real_mode = mix.mode = mode;
185 return card->set_mode(&mix);
189 /* Otherwise, failure... */
194 * Recalculate the vertical video cutoff (hack!)
196 static void vga_recalc_vertical(void)
198 unsigned int font_size, rows;
203 font_size = rdfs8(0x485); /* BIOS: font size (pixels) */
204 rows = force_y ? force_y : rdfs8(0x484)+1; /* Text rows */
206 rows *= font_size; /* Visible scan lines */
207 rows--; /* ... minus one */
211 pt = in_idx(crtc, 0x11);
212 pt &= ~0x80; /* Unlock CR0-7 */
213 out_idx(pt, crtc, 0x11);
215 out_idx((u8)rows, crtc, 0x12); /* Lower height register */
217 ov = in_idx(crtc, 0x07); /* Overflow register */
219 ov |= (rows >> (8-1)) & 0x02;
220 ov |= (rows >> (9-6)) & 0x40;
221 out_idx(ov, crtc, 0x07);
224 /* Set mode (with recalc if specified) */
225 static int set_mode(u16 mode)
230 /* Very special mode numbers... */
231 if (mode == VIDEO_CURRENT_MODE)
232 return 0; /* Nothing to do... */
233 else if (mode == NORMAL_VGA)
235 else if (mode == EXTENDED_VGA)
238 rv = raw_set_mode(mode, &real_mode);
242 if (mode & VIDEO_RECALC)
243 vga_recalc_vertical();
245 /* Save the canonical mode number for the kernel, not
246 an alias, size specification or menu position */
247 boot_params.hdr.vid_mode = real_mode;
251 static unsigned int get_entry(void)
266 } else if ((key >= '0' && key <= '9') ||
267 (key >= 'A' && key <= 'Z') ||
268 (key >= 'a' && key <= 'z')) {
269 if (len < sizeof entry_buf) {
270 entry_buf[len++] = key;
274 } while (key != '\r');
278 return VIDEO_CURRENT_MODE; /* Default */
281 for (i = 0; i < len; i++) {
283 key = entry_buf[i] | 0x20;
284 v += (key > '9') ? key-'a'+10 : key-'0';
290 static void display_menu(void)
292 struct card_info *card;
293 struct mode_info *mi;
297 puts("Mode: COLSxROWS:\n");
300 for (card = video_cards; card < video_cards_end; card++) {
302 for (i = 0; i < card->nmodes; i++, mi++) {
303 int visible = mi->x && mi->y;
304 u16 mode_id = mi->mode ? mi->mode :
308 continue; /* Hidden mode */
310 printf("%c %04X %3dx%-3d %s\n",
311 ch, mode_id, mi->x, mi->y, card->card_name);
315 else if (ch == 'z' || ch == ' ')
316 ch = ' '; /* Out of keys... */
323 #define H(x) ((x)-'a'+10)
324 #define SCAN ((H('s')<<12)+(H('c')<<8)+(H('a')<<4)+H('n'))
326 static unsigned int mode_menu(void)
331 puts("Press <ENTER> to see video modes available, "
332 "<SPACE> to continue, or wait 30 sec\n");
336 key = getchar_timeout();
337 if (key == ' ' || key == 0)
338 return VIDEO_CURRENT_MODE; /* Default */
341 putchar('\a'); /* Beep! */
348 puts("Enter a video mode or \"scan\" to scan for "
349 "additional modes: ");
358 #ifdef CONFIG_VIDEO_RETAIN
359 /* Save screen content to the heap */
360 struct saved_screen {
366 static void save_screen(void)
368 /* Should be called after store_mode_params() */
369 saved.x = boot_params.screen_info.orig_video_cols;
370 saved.y = boot_params.screen_info.orig_video_lines;
371 saved.curx = boot_params.screen_info.orig_x;
372 saved.cury = boot_params.screen_info.orig_y;
374 if (heap_free() < saved.x*saved.y*sizeof(u16)+512)
375 return; /* Not enough heap to save the screen */
377 saved.data = GET_HEAP(u16, saved.x*saved.y);
379 set_fs(video_segment);
380 copy_from_fs(saved.data, 0, saved.x*saved.y*sizeof(u16));
383 static void restore_screen(void)
385 /* Should be called after store_mode_params() */
386 int xs = boot_params.screen_info.orig_video_cols;
387 int ys = boot_params.screen_info.orig_video_lines;
390 u16 *src = saved.data;
394 return; /* Can't restore onto a graphic mode */
397 return; /* No saved screen contents */
399 /* Restore screen contents */
401 set_fs(video_segment);
402 for (y = 0; y < ys; y++) {
406 int copy = (xs < saved.x) ? xs : saved.x;
407 copy_to_fs(dst, src, copy*sizeof(u16));
408 dst += copy*sizeof(u16);
410 npad = (xs < saved.x) ? 0 : xs-saved.x;
415 /* Writes "npad" blank characters to
416 video_segment:dst and advances dst */
417 asm volatile("pushw %%es ; "
424 : "+D" (dst), "+c" (npad)
425 : "bdS" (video_segment),
429 /* Restore cursor position */
430 ax = 0x0200; /* Set cursor position */
431 bx = 0; /* Page number (<< 8) */
432 dx = (saved.cury << 8)+saved.curx;
434 : "+a" (ax), "+b" (bx), "+d" (dx)
435 : : "ecx", "esi", "edi");
438 #define save_screen() ((void)0)
439 #define restore_screen() ((void)0)
444 u16 mode = boot_params.hdr.vid_mode;
459 printf("Undefined video mode number: %x\n", mode);