Blame view

arch/x86/boot/video.c 7.08 KB
97873a3da   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
5e8ddcbe8   H. Peter Anvin   Video mode probin...
2
3
4
5
  /* -*- linux-c -*- ------------------------------------------------------- *
   *
   *   Copyright (C) 1991, 1992 Linus Torvalds
   *   Copyright 2007 rPath, Inc. - All Rights Reserved
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
6
   *   Copyright 2009 Intel Corporation; author H. Peter Anvin
5e8ddcbe8   H. Peter Anvin   Video mode probin...
7
   *
5e8ddcbe8   H. Peter Anvin   Video mode probin...
8
9
10
   * ----------------------------------------------------------------------- */
  
  /*
5e8ddcbe8   H. Peter Anvin   Video mode probin...
11
12
   * Select video mode
   */
70f152874   Kirill A. Shutemov   x86/mm: Fix regre...
13
  #include <uapi/asm/boot.h>
5e8ddcbe8   H. Peter Anvin   Video mode probin...
14
15
16
  #include "boot.h"
  #include "video.h"
  #include "vesa.h"
79287cf87   Alexander Kuleshov   x86/boot/video: M...
17
  static u16 video_segment;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
18
19
  static void store_cursor_position(void)
  {
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
20
  	struct biosregs ireg, oreg;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
21

cf06de7b9   H. Peter Anvin   x86, setup: "glov...
22
23
24
  	initregs(&ireg);
  	ireg.ah = 0x03;
  	intcall(0x10, &ireg, &oreg);
5e8ddcbe8   H. Peter Anvin   Video mode probin...
25

cf06de7b9   H. Peter Anvin   x86, setup: "glov...
26
27
  	boot_params.screen_info.orig_x = oreg.dl;
  	boot_params.screen_info.orig_y = oreg.dh;
d9b263528   Matthew Garrett   x86, setup: Store...
28
29
30
31
32
33
  
  	if (oreg.ch & 0x20)
  		boot_params.screen_info.flags |= VIDEO_FLAGS_NOCURSOR;
  
  	if ((oreg.ch & 0x1f) > (oreg.cl & 0x1f))
  		boot_params.screen_info.flags |= VIDEO_FLAGS_NOCURSOR;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
34
35
36
37
  }
  
  static void store_video_mode(void)
  {
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
38
  	struct biosregs ireg, oreg;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
39
40
41
  
  	/* N.B.: the saving of the video page here is a bit silly,
  	   since we pretty much assume page 0 everywhere. */
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
42
43
44
  	initregs(&ireg);
  	ireg.ah = 0x0f;
  	intcall(0x10, &ireg, &oreg);
5e8ddcbe8   H. Peter Anvin   Video mode probin...
45
46
  
  	/* Not all BIOSes are clean with respect to the top bit */
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
47
48
  	boot_params.screen_info.orig_video_mode = oreg.al & 0x7f;
  	boot_params.screen_info.orig_video_page = oreg.bh;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  }
  
  /*
   * Store the video mode parameters for later usage by the kernel.
   * This is done by asking the BIOS except for the rows/columns
   * parameters in the default 80x25 mode -- these are set directly,
   * because some very obscure BIOSes supply insane values.
   */
  static void store_mode_params(void)
  {
  	u16 font_size;
  	int x, y;
  
  	/* For graphics mode, it is up to the mode-setting driver
  	   (currently only video-vesa.c) to store the parameters */
  	if (graphic_mode)
  		return;
  
  	store_cursor_position();
  	store_video_mode();
  
  	if (boot_params.screen_info.orig_video_mode == 0x07) {
  		/* MDA, HGC, or VGA in monochrome mode */
  		video_segment = 0xb000;
  	} else {
  		/* CGA, EGA, VGA and so forth */
  		video_segment = 0xb800;
  	}
  
  	set_fs(0);
  	font_size = rdfs16(0x485); /* Font size, BIOS area */
  	boot_params.screen_info.orig_video_points = font_size;
  
  	x = rdfs16(0x44a);
  	y = (adapter == ADAPTER_CGA) ? 25 : rdfs8(0x484)+1;
  
  	if (force_x)
  		x = force_x;
  	if (force_y)
  		y = force_y;
  
  	boot_params.screen_info.orig_video_cols  = x;
  	boot_params.screen_info.orig_video_lines = y;
  }
5e8ddcbe8   H. Peter Anvin   Video mode probin...
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  static unsigned int get_entry(void)
  {
  	char entry_buf[4];
  	int i, len = 0;
  	int key;
  	unsigned int v;
  
  	do {
  		key = getchar();
  
  		if (key == '\b') {
  			if (len > 0) {
  				puts("\b \b");
  				len--;
  			}
  		} else if ((key >= '0' && key <= '9') ||
  			   (key >= 'A' && key <= 'Z') ||
  			   (key >= 'a' && key <= 'z')) {
0e96f31ea   Jordan Borgner   x86: Clean up 'si...
111
  			if (len < sizeof(entry_buf)) {
5e8ddcbe8   H. Peter Anvin   Video mode probin...
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
  				entry_buf[len++] = key;
  				putchar(key);
  			}
  		}
  	} while (key != '\r');
  	putchar('
  ');
  
  	if (len == 0)
  		return VIDEO_CURRENT_MODE; /* Default */
  
  	v = 0;
  	for (i = 0; i < len; i++) {
  		v <<= 4;
  		key = entry_buf[i] | 0x20;
  		v += (key > '9') ? key-'a'+10 : key-'0';
  	}
  
  	return v;
  }
  
  static void display_menu(void)
  {
  	struct card_info *card;
  	struct mode_info *mi;
  	char ch;
  	int i;
1cac5004e   H. Peter Anvin   x86 setup: displa...
139
140
141
  	int nmodes;
  	int modes_per_line;
  	int col;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
142

1cac5004e   H. Peter Anvin   x86 setup: displa...
143
144
145
  	nmodes = 0;
  	for (card = video_cards; card < video_cards_end; card++)
  		nmodes += card->nmodes;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
146

1cac5004e   H. Peter Anvin   x86 setup: displa...
147
148
149
150
151
152
153
154
155
156
  	modes_per_line = 1;
  	if (nmodes >= 20)
  		modes_per_line = 3;
  
  	for (col = 0; col < modes_per_line; col++)
  		puts("Mode: Resolution:  Type: ");
  	putchar('
  ');
  
  	col = 0;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
157
158
159
160
  	ch = '0';
  	for (card = video_cards; card < video_cards_end; card++) {
  		mi = card->modes;
  		for (i = 0; i < card->nmodes; i++, mi++) {
1cac5004e   H. Peter Anvin   x86 setup: displa...
161
  			char resbuf[32];
5e8ddcbe8   H. Peter Anvin   Video mode probin...
162
163
164
165
166
167
  			int visible = mi->x && mi->y;
  			u16 mode_id = mi->mode ? mi->mode :
  				(mi->y << 8)+mi->x;
  
  			if (!visible)
  				continue; /* Hidden mode */
1cac5004e   H. Peter Anvin   x86 setup: displa...
168
169
170
171
172
173
174
175
176
177
178
179
180
  			if (mi->depth)
  				sprintf(resbuf, "%dx%d", mi->y, mi->depth);
  			else
  				sprintf(resbuf, "%d", mi->y);
  
  			printf("%c %03X %4dx%-7s %-6s",
  			       ch, mode_id, mi->x, resbuf, card->card_name);
  			col++;
  			if (col >= modes_per_line) {
  				putchar('
  ');
  				col = 0;
  			}
5e8ddcbe8   H. Peter Anvin   Video mode probin...
181
182
183
184
185
186
187
188
189
  
  			if (ch == '9')
  				ch = 'a';
  			else if (ch == 'z' || ch == ' ')
  				ch = ' '; /* Out of keys... */
  			else
  				ch++;
  		}
  	}
1cac5004e   H. Peter Anvin   x86 setup: displa...
190
191
192
  	if (col)
  		putchar('
  ');
5e8ddcbe8   H. Peter Anvin   Video mode probin...
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
  }
  
  #define H(x)	((x)-'a'+10)
  #define SCAN	((H('s')<<12)+(H('c')<<8)+(H('a')<<4)+H('n'))
  
  static unsigned int mode_menu(void)
  {
  	int key;
  	unsigned int sel;
  
  	puts("Press <ENTER> to see video modes available, "
  	     "<SPACE> to continue, or wait 30 sec
  ");
  
  	kbd_flush();
  	while (1) {
  		key = getchar_timeout();
  		if (key == ' ' || key == 0)
  			return VIDEO_CURRENT_MODE; /* Default */
  		if (key == '\r')
  			break;
  		putchar('\a');	/* Beep! */
  	}
  
  
  	for (;;) {
  		display_menu();
  
  		puts("Enter a video mode or \"scan\" to scan for "
  		     "additional modes: ");
  		sel = get_entry();
  		if (sel != SCAN)
  			return sel;
  
  		probe_cards(1);
  	}
  }
5e8ddcbe8   H. Peter Anvin   Video mode probin...
230
  /* Save screen content to the heap */
a1a00b588   Hannes Eder   x86: boot - fix s...
231
  static struct saved_screen {
5e8ddcbe8   H. Peter Anvin   Video mode probin...
232
233
234
235
236
237
238
239
240
241
242
243
  	int x, y;
  	int curx, cury;
  	u16 *data;
  } saved;
  
  static void save_screen(void)
  {
  	/* Should be called after store_mode_params() */
  	saved.x = boot_params.screen_info.orig_video_cols;
  	saved.y = boot_params.screen_info.orig_video_lines;
  	saved.curx = boot_params.screen_info.orig_x;
  	saved.cury = boot_params.screen_info.orig_y;
e6e1ace99   H. Peter Anvin   x86 setup: sizeof...
244
  	if (!heap_free(saved.x*saved.y*sizeof(u16)+512))
5e8ddcbe8   H. Peter Anvin   Video mode probin...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  		return;		/* Not enough heap to save the screen */
  
  	saved.data = GET_HEAP(u16, saved.x*saved.y);
  
  	set_fs(video_segment);
  	copy_from_fs(saved.data, 0, saved.x*saved.y*sizeof(u16));
  }
  
  static void restore_screen(void)
  {
  	/* Should be called after store_mode_params() */
  	int xs = boot_params.screen_info.orig_video_cols;
  	int ys = boot_params.screen_info.orig_video_lines;
  	int y;
  	addr_t dst = 0;
  	u16 *src = saved.data;
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
261
  	struct biosregs ireg;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
  
  	if (graphic_mode)
  		return;		/* Can't restore onto a graphic mode */
  
  	if (!src)
  		return;		/* No saved screen contents */
  
  	/* Restore screen contents */
  
  	set_fs(video_segment);
  	for (y = 0; y < ys; y++) {
  		int npad;
  
  		if (y < saved.y) {
  			int copy = (xs < saved.x) ? xs : saved.x;
  			copy_to_fs(dst, src, copy*sizeof(u16));
  			dst += copy*sizeof(u16);
  			src += saved.x;
  			npad = (xs < saved.x) ? 0 : xs-saved.x;
  		} else {
  			npad = xs;
  		}
  
  		/* Writes "npad" blank characters to
  		   video_segment:dst and advances dst */
  		asm volatile("pushw %%es ; "
  			     "movw %2,%%es ; "
  			     "shrw %%cx ; "
  			     "jnc 1f ; "
  			     "stosw 
  \t"
  			     "1: rep;stosl ; "
  			     "popw %%es"
  			     : "+D" (dst), "+c" (npad)
5593eaa85   H. Peter Anvin   [x86 setup] Fix a...
296
  			     : "bdS" (video_segment),
5e8ddcbe8   H. Peter Anvin   Video mode probin...
297
298
299
300
  			       "a" (0x07200720));
  	}
  
  	/* Restore cursor position */
f1f6baf8f   H. Peter Anvin   x86, setup: When ...
301
302
303
304
  	if (saved.curx >= xs)
  		saved.curx = xs-1;
  	if (saved.cury >= ys)
  		saved.cury = ys-1;
cf06de7b9   H. Peter Anvin   x86, setup: "glov...
305
306
307
308
309
  	initregs(&ireg);
  	ireg.ah = 0x02;		/* Set cursor position */
  	ireg.dh = saved.cury;
  	ireg.dl = saved.curx;
  	intcall(0x10, &ireg, NULL);
f1f6baf8f   H. Peter Anvin   x86, setup: When ...
310
311
  
  	store_cursor_position();
5e8ddcbe8   H. Peter Anvin   Video mode probin...
312
  }
5e8ddcbe8   H. Peter Anvin   Video mode probin...
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
  
  void set_video(void)
  {
  	u16 mode = boot_params.hdr.vid_mode;
  
  	RESET_HEAP();
  
  	store_mode_params();
  	save_screen();
  	probe_cards(0);
  
  	for (;;) {
  		if (mode == ASK_VGA)
  			mode = mode_menu();
  
  		if (!set_mode(mode))
  			break;
  
  		printf("Undefined video mode number: %x
  ", mode);
  		mode = ASK_VGA;
  	}
e44b7b752   Pavel Machek   x86: move suspend...
335
  	boot_params.hdr.vid_mode = mode;
5e8ddcbe8   H. Peter Anvin   Video mode probin...
336
337
338
339
340
341
  	vesa_store_edid();
  	store_mode_params();
  
  	if (do_restore)
  		restore_screen();
  }