xref: /haiku/src/system/boot/platform/bios_ia32/video.cpp (revision 820dca4df6c7bf955c46e8f6521b9408f50b2900)
1 /*
2  * Copyright 2004-2009, Axel Dörfler, axeld@pinc-software.de.
3  * Copyright 2008, Stephan Aßmus <superstippi@gmx.de>
4  * Copyright 2008, Philippe Saint-Pierre <stpere@gmail.com>
5  * Copyright 2011, Rene Gollent, rene@gollent.com.
6  * Distributed under the terms of the MIT License.
7  */
8 
9 
10 #include "video.h"
11 #include "bios.h"
12 #include "vesa.h"
13 #include "vesa_info.h"
14 #include "vga.h"
15 #include "mmu.h"
16 
17 #include <edid.h>
18 
19 #include <arch/cpu.h>
20 #include <boot/stage2.h>
21 #include <boot/platform.h>
22 #include <boot/menu.h>
23 #include <boot/kernel_args.h>
24 #include <boot/platform/generic/video.h>
25 #include <util/list.h>
26 #include <drivers/driver_settings.h>
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 
33 #define TRACE_VIDEO
34 #ifdef TRACE_VIDEO
35 #	define TRACE(x) dprintf x
36 #else
37 #	define TRACE(x) ;
38 #endif
39 
40 
41 struct video_mode {
42 	list_link	link;
43 	uint16		mode;
44 	uint16		width, height, bits_per_pixel;
45 	uint32		bytes_per_row;
46 	crtc_info_block* timing;
47 };
48 
49 static vbe_info_block sInfo;
50 static video_mode *sMode, *sDefaultMode;
51 static bool sVesaCompatible;
52 static struct list sModeList;
53 static uint32 sModeCount;
54 static addr_t sFrameBuffer;
55 static bool sModeChosen;
56 static bool sSettingsLoaded;
57 
58 
59 static int
60 compare_video_modes(video_mode *a, video_mode *b)
61 {
62 	int compare = a->width - b->width;
63 	if (compare != 0)
64 		return compare;
65 
66 	compare = a->height - b->height;
67 	if (compare != 0)
68 		return compare;
69 
70 	// TODO: compare video_mode::mode?
71 	return a->bits_per_pixel - b->bits_per_pixel;
72 }
73 
74 
75 /*!	Insert the video mode into the list, sorted by resolution and bit depth.
76 	Higher resolutions/depths come first.
77 */
78 static void
79 add_video_mode(video_mode *videoMode)
80 {
81 	video_mode *mode = NULL;
82 	while ((mode = (video_mode *)list_get_next_item(&sModeList, mode))
83 			!= NULL) {
84 		int compare = compare_video_modes(videoMode, mode);
85 		if (compare == 0) {
86 			// mode already exists
87 			return;
88 		}
89 
90 		if (compare > 0)
91 			break;
92 	}
93 
94 	list_insert_item_before(&sModeList, mode, videoMode);
95 	sModeCount++;
96 }
97 
98 
99 /*! \brief Finds a video mode with the given resolution.
100 	If \a allowPalette is true, 8-bit modes are considered, too.
101 	If \a height is \c -1, the height is ignored, and only the width
102 	matters.
103 */
104 static video_mode *
105 find_video_mode(int32 width, int32 height, bool allowPalette)
106 {
107 	video_mode *found = NULL;
108 	video_mode *mode = NULL;
109 	while ((mode = (video_mode *)list_get_next_item(&sModeList, mode))
110 			!= NULL) {
111 		if (mode->width == width && (height == -1 || mode->height == height)
112 			&& (mode->bits_per_pixel > 8 || allowPalette)) {
113 			if (found == NULL || found->bits_per_pixel < mode->bits_per_pixel)
114 				found = mode;
115 		}
116 	}
117 
118 	return found;
119 }
120 
121 
122 /*! Returns the VESA mode closest to the one specified, with a width less or
123 	equal as specified.
124 	The height as well as the depth may vary in both directions, though.
125 */
126 static video_mode *
127 closest_video_mode(int32 width, int32 height, int32 depth)
128 {
129 	video_mode *bestMode = NULL;
130 	uint32 bestDiff = 0;
131 
132 	video_mode *mode = NULL;
133 	while ((mode = (video_mode *)list_get_next_item(&sModeList, mode))
134 			!= NULL) {
135 		if (mode->width > width) {
136 			// Only choose modes with a width less or equal than the searched
137 			// one; or else it might well be that the monitor cannot keep up.
138 			continue;
139 		}
140 
141 		uint32 diff = 2 * abs(mode->width - width) + abs(mode->height - height)
142 			+ abs(mode->bits_per_pixel - depth);
143 
144 		if (bestMode == NULL || bestDiff > diff) {
145 			bestMode = mode;
146 			bestDiff = diff;
147 		}
148 	}
149 
150 	return bestMode;
151 }
152 
153 
154 static crtc_info_block*
155 get_crtc_info_block(edid1_detailed_timing& timing)
156 {
157 	// This feature is only available on chipsets supporting VBE3 and up
158 	if (sInfo.version.major < 3)
159 		return NULL;
160 
161 	// Copy timing structure to set the mode with
162 	crtc_info_block* crtcInfo = (crtc_info_block*)malloc(
163 		sizeof(crtc_info_block));
164 	if (crtcInfo == NULL)
165 		return NULL;
166 
167 	memset(crtcInfo, 0, sizeof(crtc_info_block));
168 	crtcInfo->horizontal_sync_start = timing.h_active + timing.h_sync_off;
169 	crtcInfo->horizontal_sync_end = crtcInfo->horizontal_sync_start
170 		+ timing.h_sync_width;
171 	crtcInfo->horizontal_total = timing.h_active + timing.h_blank;
172 	crtcInfo->vertical_sync_start = timing.v_active + timing.v_sync_off;
173 	crtcInfo->vertical_sync_end = crtcInfo->vertical_sync_start
174 		+ timing.v_sync_width;
175 	crtcInfo->vertical_total = timing.v_active + timing.v_blank;
176 	crtcInfo->pixel_clock = timing.pixel_clock * 10000L;
177 	crtcInfo->refresh_rate = crtcInfo->pixel_clock
178 		/ (crtcInfo->horizontal_total / 10)
179 		/ (crtcInfo->vertical_total / 10);
180 
181 	TRACE(("crtc: h %u/%u/%u, v %u/%u/%u, pixel clock %lu, refresh %u\n",
182 		crtcInfo->horizontal_sync_start, crtcInfo->horizontal_sync_end,
183 		crtcInfo->horizontal_total, crtcInfo->vertical_sync_start,
184 		crtcInfo->vertical_sync_end, crtcInfo->vertical_total,
185 		crtcInfo->pixel_clock, crtcInfo->refresh_rate));
186 
187 	crtcInfo->flags = 0;
188 	if (timing.sync == 3) {
189 		// TODO: this switches the default sync when sync != 3 (compared to
190 		// create_display_modes().
191 		if ((timing.misc & 1) == 0)
192 			crtcInfo->flags |= CRTC_NEGATIVE_HSYNC;
193 		if ((timing.misc & 2) == 0)
194 			crtcInfo->flags |= CRTC_NEGATIVE_VSYNC;
195 	}
196 	if (timing.interlaced)
197 		crtcInfo->flags |= CRTC_INTERLACED;
198 
199 	return crtcInfo;
200 }
201 
202 
203 static crtc_info_block*
204 get_crtc_info_block(edid1_std_timing& timing)
205 {
206 	// TODO: implement me!
207 	return NULL;
208 }
209 
210 
211 static video_mode*
212 find_edid_mode(edid1_info& info, bool allowPalette)
213 {
214 	video_mode *mode = NULL;
215 
216 	// try detailed timing first
217 	for (int32 i = 0; i < EDID1_NUM_DETAILED_MONITOR_DESC; i++) {
218 		edid1_detailed_monitor& monitor = info.detailed_monitor[i];
219 
220 		if (monitor.monitor_desc_type == EDID1_IS_DETAILED_TIMING) {
221 			mode = find_video_mode(monitor.data.detailed_timing.h_active,
222 				monitor.data.detailed_timing.v_active, allowPalette);
223 			if (mode != NULL) {
224 				mode->timing
225 					= get_crtc_info_block(monitor.data.detailed_timing);
226 				return mode;
227 			}
228 		}
229 	}
230 
231 	int32 best = -1;
232 
233 	// try standard timings next
234 	for (int32 i = 0; i < EDID1_NUM_STD_TIMING; i++) {
235 		if (info.std_timing[i].h_size <= 256)
236 			continue;
237 
238 		video_mode* found = find_video_mode(info.std_timing[i].h_size,
239 			info.std_timing[i].v_size, allowPalette);
240 		if (found != NULL) {
241 			if (mode != NULL) {
242 				// prefer higher resolutions
243 				if (found->width > mode->width) {
244 					mode = found;
245 					best = i;
246 				}
247 			} else {
248 				mode = found;
249 				best = i;
250 			}
251 		}
252 	}
253 
254 	if (best >= 0)
255 		mode->timing = get_crtc_info_block(info.std_timing[best]);
256 
257 	return mode;
258 }
259 
260 
261 static bool
262 get_mode_from_settings(void)
263 {
264 	if (sSettingsLoaded)
265 		return true;
266 
267 	void *handle = load_driver_settings("vesa");
268 	if (handle == NULL)
269 		return false;
270 
271 	bool found = false;
272 
273 	const driver_settings *settings = get_driver_settings(handle);
274 	if (settings == NULL)
275 		goto out;
276 
277 	sSettingsLoaded = true;
278 
279 	for (int32 i = 0; i < settings->parameter_count; i++) {
280 		driver_parameter &parameter = settings->parameters[i];
281 
282 		if (!strcmp(parameter.name, "mode") && parameter.value_count > 2) {
283 			// parameter found, now get its values
284 			int32 width = strtol(parameter.values[0], NULL, 0);
285 			int32 height = strtol(parameter.values[1], NULL, 0);
286 			int32 depth = strtol(parameter.values[2], NULL, 0);
287 
288 			// search mode that fits
289 
290 			video_mode *mode = closest_video_mode(width, height, depth);
291 			if (mode != NULL) {
292 				found = true;
293 				sMode = mode;
294 			}
295 		}
296 	}
297 
298 out:
299 	unload_driver_settings(handle);
300 	return found;
301 }
302 
303 
304 //	#pragma mark - vga
305 
306 
307 static void
308 vga_set_palette(const uint8 *palette, int32 firstIndex, int32 numEntries)
309 {
310 	out8(firstIndex, VGA_COLOR_WRITE_MODE);
311 	// write VGA palette
312 	for (int32 i = firstIndex; i < numEntries; i++) {
313 		// VGA (usually) has only 6 bits per gun
314 		out8(palette[i * 3 + 0] >> 2, VGA_COLOR_DATA);
315 		out8(palette[i * 3 + 1] >> 2, VGA_COLOR_DATA);
316 		out8(palette[i * 3 + 2] >> 2, VGA_COLOR_DATA);
317 	}
318 }
319 
320 
321 static void
322 vga_enable_bright_background_colors(void)
323 {
324 	// reset attribute controller
325 	in8(VGA_INPUT_STATUS_1);
326 
327 	// select mode control register
328 	out8(0x30, VGA_ATTRIBUTE_WRITE);
329 
330 	// read mode control register, change it (we need to clear bit 3), and write it back
331 	uint8 mode = in8(VGA_ATTRIBUTE_READ) & 0xf7;
332 	out8(mode, VGA_ATTRIBUTE_WRITE);
333 }
334 
335 
336 //	#pragma mark - vesa
337 
338 
339 static status_t
340 vesa_get_edid(edid1_info *info)
341 {
342 	struct bios_regs regs;
343 	regs.eax = 0x4f15;
344 	regs.ebx = 0;
345 		// report DDC service
346 	regs.ecx = 0;
347 	regs.es = 0;
348 	regs.edi = 0;
349 	call_bios(0x10, &regs);
350 
351 	TRACE(("EDID1: %lx\n", regs.eax));
352 	// %ah contains the error code
353 	// %al determines whether or not the function is supported
354 	if (regs.eax != 0x4f)
355 		return B_NOT_SUPPORTED;
356 
357 	TRACE(("EDID2: ebx %lx\n", regs.ebx));
358 	// test if DDC is supported by the monitor
359 	if ((regs.ebx & 3) == 0)
360 		return B_NOT_SUPPORTED;
361 
362 	edid1_raw edidRaw;
363 
364 	regs.eax = 0x4f15;
365 	regs.ebx = 1;
366 		// read EDID
367 	regs.ecx = 0;
368 	regs.edx = 0;
369 	regs.es = ADDRESS_SEGMENT(&edidRaw);
370 	regs.edi = ADDRESS_OFFSET(&edidRaw);
371 	call_bios(0x10, &regs);
372 	TRACE(("EDID3: %lx\n", regs.eax));
373 
374 	if (regs.eax != 0x4f)
375 		return B_NOT_SUPPORTED;
376 
377 	// retrieved EDID - now parse it
378 	edid_decode(info, &edidRaw);
379 
380 #ifdef TRACE_VIDEO
381 	edid_dump(info);
382 #endif
383 	return B_OK;
384 }
385 
386 
387 static status_t
388 vesa_get_mode_info(uint16 mode, struct vbe_mode_info *modeInfo)
389 {
390 	memset(modeInfo, 0, sizeof(vbe_mode_info));
391 
392 	struct bios_regs regs;
393 	regs.eax = 0x4f01;
394 	regs.ecx = mode;
395 	regs.es = ADDRESS_SEGMENT(modeInfo);
396 	regs.edi = ADDRESS_OFFSET(modeInfo);
397 	call_bios(0x10, &regs);
398 
399 	// %ah contains the error code
400 	if ((regs.eax & 0xff00) != 0)
401 		return B_ENTRY_NOT_FOUND;
402 
403 	return B_OK;
404 }
405 
406 
407 static status_t
408 vesa_get_vbe_info_block(vbe_info_block *info)
409 {
410 	memset(info, 0, sizeof(vbe_info_block));
411 	info->signature = VBE2_SIGNATURE;
412 
413 	struct bios_regs regs;
414 	regs.eax = 0x4f00;
415 	regs.es = ADDRESS_SEGMENT(info);
416 	regs.edi = ADDRESS_OFFSET(info);
417 	call_bios(0x10, &regs);
418 
419 	// %ah contains the error code
420 	if ((regs.eax & 0xff00) != 0)
421 		return B_ERROR;
422 
423 	if (info->signature != VESA_SIGNATURE)
424 		return B_ERROR;
425 
426 	dprintf("VESA version = %d.%d, capabilities %lx\n", info->version.major,
427 		info->version.minor, info->capabilities);
428 
429 	if (info->version.major < 2) {
430 		dprintf("VESA support too old\n");
431 		return B_ERROR;
432 	}
433 
434 	info->oem_string = SEGMENTED_TO_LINEAR(info->oem_string);
435 	info->mode_list = SEGMENTED_TO_LINEAR(info->mode_list);
436 	dprintf("OEM string: %s\n", (const char *)info->oem_string);
437 
438 	return B_OK;
439 }
440 
441 
442 static status_t
443 vesa_init(vbe_info_block *info, video_mode **_standardMode)
444 {
445 	if (vesa_get_vbe_info_block(info) != B_OK)
446 		return B_ERROR;
447 
448 	// fill mode list and find standard video mode
449 
450 	video_mode *standardMode = NULL;
451 
452 	for (int32 i = 0; true; i++) {
453 		uint16 mode = ((uint16 *)info->mode_list)[i];
454 		if (mode == 0xffff)
455 			break;
456 
457 		struct vbe_mode_info modeInfo;
458 		if (vesa_get_mode_info(mode, &modeInfo) == B_OK) {
459 			TRACE((" 0x%03x: %u x %u x %u (a = %d, mem = %d, phy = %lx, p = %d, b = %d)\n", mode,
460 				   modeInfo.width, modeInfo.height, modeInfo.bits_per_pixel, modeInfo.attributes,
461 				   modeInfo.memory_model, modeInfo.physical_base, modeInfo.num_planes,
462 				   modeInfo.num_banks));
463 			TRACE(("	mask: r: %d %d g: %d %d b: %d %d dcmi: %d\n",
464 				   modeInfo.red_mask_size, modeInfo.red_field_position,
465 				   modeInfo.green_mask_size, modeInfo.green_field_position,
466 				   modeInfo.blue_mask_size, modeInfo.blue_field_position,
467 				   modeInfo.direct_color_mode_info));
468 
469 			const uint32 requiredAttributes = MODE_ATTR_AVAILABLE
470 				| MODE_ATTR_GRAPHICS_MODE | MODE_ATTR_COLOR_MODE
471 				| MODE_ATTR_LINEAR_BUFFER;
472 
473 			if (modeInfo.width >= 640
474 				&& modeInfo.physical_base != 0
475 				&& modeInfo.num_planes == 1
476 				&& (modeInfo.memory_model == MODE_MEMORY_PACKED_PIXEL
477 					|| modeInfo.memory_model == MODE_MEMORY_DIRECT_COLOR)
478 				&& (modeInfo.attributes & requiredAttributes)
479 					== requiredAttributes) {
480 				// this mode fits our needs
481 				video_mode *videoMode = (video_mode *)malloc(
482 					sizeof(struct video_mode));
483 				if (videoMode == NULL)
484 					continue;
485 
486 				videoMode->mode = mode;
487 				videoMode->bytes_per_row = modeInfo.bytes_per_row;
488 				videoMode->width = modeInfo.width;
489 				videoMode->height = modeInfo.height;
490 				videoMode->bits_per_pixel = modeInfo.bits_per_pixel;
491 				videoMode->timing = NULL;
492 
493 				if (modeInfo.bits_per_pixel == 16
494 					&& modeInfo.red_mask_size + modeInfo.green_mask_size
495 						+ modeInfo.blue_mask_size == 15) {
496 					// this is really a 15-bit mode
497 					videoMode->bits_per_pixel = 15;
498 				}
499 
500 				add_video_mode(videoMode);
501 			}
502 		} else
503 			TRACE((" 0x%03x: (failed)\n", mode));
504 	}
505 
506 	// Choose default resolution (when no EDID information is available)
507 	const uint32 kPreferredWidth = 1024;
508 	const uint32 kFallbackWidth = 800;
509 
510 	standardMode = find_video_mode(kPreferredWidth, -1, false);
511 	if (standardMode == NULL) {
512 		standardMode = find_video_mode(kFallbackWidth, -1, false);
513 		if (standardMode == NULL) {
514 			standardMode = find_video_mode(kPreferredWidth, -1, true);
515 			if (standardMode == NULL)
516 				standardMode = find_video_mode(kFallbackWidth, -1, true);
517 		}
518 	}
519 	if (standardMode == NULL) {
520 		// just take any mode
521 		standardMode = (video_mode *)list_get_first_item(&sModeList);
522 	}
523 
524 	if (standardMode == NULL) {
525 		// no usable VESA mode found...
526 		return B_ERROR;
527 	}
528 
529 	TRACE(("Using mode 0x%03x\n", standardMode->mode));
530 	*_standardMode = standardMode;
531 	return B_OK;
532 }
533 
534 
535 #if 0
536 static status_t
537 vesa_get_mode(uint16 *_mode)
538 {
539 	struct bios_regs regs;
540 	regs.eax = 0x4f03;
541 	call_bios(0x10, &regs);
542 
543 	if ((regs.eax & 0xffff) != 0x4f)
544 		return B_ERROR;
545 
546 	*_mode = regs.ebx & 0xffff;
547 	return B_OK;
548 }
549 #endif
550 
551 
552 static status_t
553 vesa_set_mode(video_mode* mode, bool useTiming)
554 {
555 	struct bios_regs regs;
556 	regs.eax = 0x4f02;
557 	regs.ebx = (mode->mode & SET_MODE_MASK) | SET_MODE_LINEAR_BUFFER;
558 
559 	if (useTiming && mode->timing != NULL) {
560 		regs.ebx |= SET_MODE_SPECIFY_CRTC;
561 		regs.es = ADDRESS_SEGMENT(mode->timing);
562 		regs.edi = ADDRESS_OFFSET(mode->timing);
563 	}
564 
565 	call_bios(0x10, &regs);
566 
567 	if ((regs.eax & 0xffff) != 0x4f)
568 		return B_ERROR;
569 
570 #if 0
571 	// make sure we have 8 bits per color channel
572 	regs.eax = 0x4f08;
573 	regs.ebx = 8 << 8;
574 	call_bios(0x10, &regs);
575 #endif
576 
577 	return B_OK;
578 }
579 
580 
581 static status_t
582 vesa_set_palette(const uint8 *palette, int32 firstIndex, int32 numEntries)
583 {
584 	// is this an 8 bit indexed color mode?
585 	if (gKernelArgs.frame_buffer.depth != 8)
586 		return B_BAD_TYPE;
587 
588 #if 0
589 	struct bios_regs regs;
590 	regs.eax = 0x4f09;
591 	regs.ebx = 0;
592 	regs.ecx = numEntries;
593 	regs.edx = firstIndex;
594 	regs.es = (addr_t)palette >> 4;
595 	regs.edi = (addr_t)palette & 0xf;
596 	call_bios(0x10, &regs);
597 
598 	if ((regs.eax & 0xffff) != 0x4f) {
599 #endif
600 		// the VESA call does not work, just try good old VGA mechanism
601 		vga_set_palette(palette, firstIndex, numEntries);
602 #if 0
603 		return B_ERROR;
604 	}
605 #endif
606 	return B_OK;
607 }
608 
609 
610 //	#pragma mark -
611 
612 
613 bool
614 video_mode_hook(Menu *menu, MenuItem *item)
615 {
616 	// find selected mode
617 	video_mode *mode = NULL;
618 
619 	menu = item->Submenu();
620 	item = menu->FindMarked();
621 	if (item != NULL) {
622 		switch (menu->IndexOf(item)) {
623 			case 0:
624 				// "Default" mode special
625 				sMode = sDefaultMode;
626 				sModeChosen = false;
627 				return true;
628 			case 1:
629 				// "Standard VGA" mode special
630 				// sets sMode to NULL which triggers VGA mode
631 				break;
632 			default:
633 				mode = (video_mode *)item->Data();
634 				break;
635 		}
636 	}
637 
638 	if (mode != sMode) {
639 		// update standard mode
640 		// ToDo: update fb settings!
641 		sMode = mode;
642 	}
643 
644 	sModeChosen = true;
645 	return true;
646 }
647 
648 
649 Menu *
650 video_mode_menu()
651 {
652 	Menu *menu = new(nothrow) Menu(CHOICE_MENU, "Select Video Mode");
653 	MenuItem *item;
654 
655 	menu->AddItem(item = new(nothrow) MenuItem("Default"));
656 	item->SetMarked(true);
657 	item->Select(true);
658 	item->SetHelpText("The Default video mode is the one currently configured "
659 		"in the system. If there is no mode configured yet, a viable mode will "
660 		"be chosen automatically.");
661 
662 	menu->AddItem(new(nothrow) MenuItem("Standard VGA"));
663 
664 	video_mode *mode = NULL;
665 	while ((mode = (video_mode *)list_get_next_item(&sModeList, mode)) != NULL) {
666 		char label[64];
667 		snprintf(label, sizeof(label), "%ux%u %u bit", mode->width,
668 			mode->height, mode->bits_per_pixel);
669 
670 		menu->AddItem(item = new(nothrow) MenuItem(label));
671 		item->SetData(mode);
672 	}
673 
674 	menu->AddSeparatorItem();
675 	menu->AddItem(item = new(nothrow) MenuItem("Return to main menu"));
676 	item->SetType(MENU_ITEM_NO_CHOICE);
677 
678 	return menu;
679 }
680 
681 
682 static void
683 set_vga_mode(void)
684 {
685 	// sets 640x480 16 colors graphics mode
686 	bios_regs regs;
687 	regs.eax = 0x0012;
688 	call_bios(0x10, &regs);
689 }
690 
691 
692 static void
693 set_text_mode(void)
694 {
695 	// sets 80x25 text console
696 	bios_regs regs;
697 	regs.eax = 0x0003;
698 	call_bios(0x10, &regs);
699 
700 	video_hide_text_cursor();
701 }
702 
703 
704 void
705 video_move_text_cursor(int x, int y)
706 {
707 	bios_regs regs;
708 	regs.eax = 0x0200;
709 	regs.ebx = 0;
710 	regs.edx = (y << 8) | x;
711 	call_bios(0x10, &regs);
712 }
713 
714 
715 void
716 video_show_text_cursor(void)
717 {
718 	bios_regs regs;
719 	regs.eax = 0x0100;
720 	regs.ecx = 0x0607;
721 	call_bios(0x10, &regs);
722 }
723 
724 
725 void
726 video_hide_text_cursor(void)
727 {
728 	bios_regs regs;
729 	regs.eax = 0x0100;
730 	regs.ecx = 0x2000;
731 	call_bios(0x10, &regs);
732 }
733 
734 
735 //	#pragma mark - blit
736 
737 
738 void
739 platform_blit4(addr_t frameBuffer, const uint8 *data,
740 	uint16 width, uint16 height, uint16 imageWidth, uint16 left, uint16 top)
741 {
742 	if (!data)
743 		return;
744 	// ToDo: no boot logo yet in VGA mode
745 #if 1
746 // this draws 16 big rectangles in all the available colors
747 	uint8 *bits = (uint8 *)frameBuffer;
748 	uint32 bytesPerRow = 80;
749 	for (int32 i = 0; i < 32; i++) {
750 		bits[9 * bytesPerRow + i + 2] = 0x55;
751 		bits[30 * bytesPerRow + i + 2] = 0xaa;
752 	}
753 
754 	for (int32 y = 10; y < 30; y++) {
755 		for (int32 i = 0; i < 16; i++) {
756 			out16((15 << 8) | 0x02, VGA_SEQUENCER_INDEX);
757 			bits[32 * bytesPerRow + i*2 + 2] = i;
758 
759 			if (i & 1) {
760 				out16((1 << 8) | 0x02, VGA_SEQUENCER_INDEX);
761 				bits[y * bytesPerRow + i*2 + 2] = 0xff;
762 				bits[y * bytesPerRow + i*2 + 3] = 0xff;
763 			}
764 			if (i & 2) {
765 				out16((2 << 8) | 0x02, VGA_SEQUENCER_INDEX);
766 				bits[y * bytesPerRow + i*2 + 2] = 0xff;
767 				bits[y * bytesPerRow + i*2 + 3] = 0xff;
768 			}
769 			if (i & 4) {
770 				out16((4 << 8) | 0x02, VGA_SEQUENCER_INDEX);
771 				bits[y * bytesPerRow + i*2 + 2] = 0xff;
772 				bits[y * bytesPerRow + i*2 + 3] = 0xff;
773 			}
774 			if (i & 8) {
775 				out16((8 << 8) | 0x02, VGA_SEQUENCER_INDEX);
776 				bits[y * bytesPerRow + i*2 + 2] = 0xff;
777 				bits[y * bytesPerRow + i*2 + 3] = 0xff;
778 			}
779 		}
780 	}
781 
782 	// enable all planes again
783 	out16((15 << 8) | 0x02, VGA_SEQUENCER_INDEX);
784 #endif
785 }
786 
787 
788 extern "C" void
789 platform_set_palette(const uint8 *palette)
790 {
791 	switch (gKernelArgs.frame_buffer.depth) {
792 		case 4:
793 			//vga_set_palette((const uint8 *)kPalette16, 0, 16);
794 			break;
795 		case 8:
796 			if (vesa_set_palette((const uint8 *)palette, 0, 256) != B_OK)
797 				dprintf("set palette failed!\n");
798 
799 			break;
800 		default:
801 			break;
802 	}
803 }
804 
805 
806 //	#pragma mark -
807 
808 extern "C" void
809 platform_switch_to_logo(void)
810 {
811 	// in debug mode, we'll never show the logo
812 	if ((platform_boot_options() & BOOT_OPTION_DEBUG_OUTPUT) != 0)
813 		return;
814 
815 	addr_t lastBase = gKernelArgs.frame_buffer.physical_buffer.start;
816 	size_t lastSize = gKernelArgs.frame_buffer.physical_buffer.size;
817 
818 	if (sVesaCompatible && sMode != NULL) {
819 		if (!sModeChosen)
820 			get_mode_from_settings();
821 
822 		// On some BIOS / chipset / monitor combinations, there seems to be a
823 		// timing issue between getting the EDID data and setting the video
824 		// mode. As such we wait here briefly to give everything enough time
825 		// to settle.
826 		spin(1000);
827 
828 		if ((sMode->timing == NULL || vesa_set_mode(sMode, true) != B_OK)
829 			&& vesa_set_mode(sMode, false) != B_OK)
830 			goto fallback;
831 
832 		struct vbe_mode_info modeInfo;
833 		if (vesa_get_mode_info(sMode->mode, &modeInfo) != B_OK)
834 			goto fallback;
835 
836 		gKernelArgs.frame_buffer.width = modeInfo.width;
837 		gKernelArgs.frame_buffer.height = modeInfo.height;
838 		gKernelArgs.frame_buffer.bytes_per_row = modeInfo.bytes_per_row;
839 		gKernelArgs.frame_buffer.depth = modeInfo.bits_per_pixel;
840 		gKernelArgs.frame_buffer.physical_buffer.size
841 			= (phys_size_t)modeInfo.bytes_per_row
842 				* (phys_size_t)gKernelArgs.frame_buffer.height;
843 		gKernelArgs.frame_buffer.physical_buffer.start = modeInfo.physical_base;
844 	} else {
845 fallback:
846 		// use standard VGA mode 640x480x4
847 		set_vga_mode();
848 
849 		gKernelArgs.frame_buffer.width = 640;
850 		gKernelArgs.frame_buffer.height = 480;
851 		gKernelArgs.frame_buffer.bytes_per_row = 80;
852 		gKernelArgs.frame_buffer.depth = 4;
853 		gKernelArgs.frame_buffer.physical_buffer.size
854 			= (phys_size_t)gKernelArgs.frame_buffer.width
855 				* (phys_size_t)gKernelArgs.frame_buffer.height / 2;
856 		gKernelArgs.frame_buffer.physical_buffer.start = 0xa0000;
857 	}
858 
859 	dprintf("video mode: %ux%ux%u\n", gKernelArgs.frame_buffer.width,
860 		gKernelArgs.frame_buffer.height, gKernelArgs.frame_buffer.depth);
861 
862 	gKernelArgs.frame_buffer.enabled = true;
863 
864 	// If the new frame buffer is either larger than the old one or located at
865 	// a different address, we need to remap it, so we first have to throw
866 	// away its previous mapping
867 	if (lastBase != 0
868 		&& (lastBase != gKernelArgs.frame_buffer.physical_buffer.start
869 			|| lastSize < gKernelArgs.frame_buffer.physical_buffer.size)) {
870 		mmu_free((void *)sFrameBuffer, lastSize);
871 		lastBase = 0;
872 	}
873 	if (lastBase == 0) {
874 		// the graphics memory has not been mapped yet!
875 		sFrameBuffer = mmu_map_physical_memory(
876 			gKernelArgs.frame_buffer.physical_buffer.start,
877 			gKernelArgs.frame_buffer.physical_buffer.size, kDefaultPageFlags);
878 	}
879 
880 	video_display_splash(sFrameBuffer);
881 }
882 
883 
884 extern "C" void
885 platform_switch_to_text_mode(void)
886 {
887 	if (!gKernelArgs.frame_buffer.enabled) {
888 		vga_enable_bright_background_colors();
889 		return;
890 	}
891 
892 	set_text_mode();
893 	gKernelArgs.frame_buffer.enabled = 0;
894 
895 	vga_enable_bright_background_colors();
896 }
897 
898 
899 extern "C" status_t
900 platform_init_video(void)
901 {
902 	gKernelArgs.frame_buffer.enabled = 0;
903 	list_init(&sModeList);
904 
905 	set_text_mode();
906 		// You may wonder why we do this here:
907 		// Obviously, some graphics card BIOS implementations don't
908 		// report all available modes unless you've done this before
909 		// getting the VESA information.
910 		// One example of those is the SiS 630 chipset in my laptop.
911 
912 	sVesaCompatible = vesa_init(&sInfo, &sDefaultMode) == B_OK;
913 	if (!sVesaCompatible) {
914 		TRACE(("No VESA compatible graphics!\n"));
915 		gKernelArgs.vesa_capabilities = 0;
916 		return B_ERROR;
917 	}
918 
919 	gKernelArgs.vesa_capabilities = sInfo.capabilities;
920 
921 	TRACE(("VESA compatible graphics!\n"));
922 
923 	// store VESA modes into kernel args
924 	vesa_mode *modes = (vesa_mode *)kernel_args_malloc(
925 		sModeCount * sizeof(vesa_mode));
926 	if (modes != NULL) {
927 		video_mode *mode = NULL;
928 		uint32 i = 0;
929 		while ((mode = (video_mode *)list_get_next_item(&sModeList, mode))
930 				!= NULL) {
931 			modes[i].mode = mode->mode;
932 			modes[i].width = mode->width;
933 			modes[i].height = mode->height;
934 			modes[i].bits_per_pixel = mode->bits_per_pixel;
935 			i++;
936 		}
937 
938 		gKernelArgs.vesa_modes = modes;
939 		gKernelArgs.vesa_modes_size = sModeCount * sizeof(vesa_mode);
940 	}
941 
942 	edid1_info info;
943 	// Note, we currently ignore EDID information for VBE2 - while the EDID
944 	// information itself seems to be reliable, older chips often seem to
945 	// use very strange default timings with higher modes.
946 	// TODO: Maybe add a setting to enable it anyway?
947 	if (sInfo.version.major >= 3 && vesa_get_edid(&info) == B_OK) {
948 		// we got EDID information from the monitor, try to find a new default
949 		// mode
950 		video_mode *defaultMode = find_edid_mode(info, false);
951 		if (defaultMode == NULL)
952 			defaultMode = find_edid_mode(info, true);
953 
954 		if (defaultMode != NULL) {
955 			// We found a new default mode to use!
956 			sDefaultMode = defaultMode;
957 		}
958 
959 		gKernelArgs.edid_info = kernel_args_malloc(sizeof(edid1_info));
960 		if (gKernelArgs.edid_info != NULL)
961 			memcpy(gKernelArgs.edid_info, &info, sizeof(edid1_info));
962 	}
963 
964 	sMode = sDefaultMode;
965 	return B_OK;
966 }
967 
968