Adding upstream version 4.00~pre61+dfsg.
[syslinux-debian/hramrach.git] / com32 / mboot / initvesa.c
blobcf2707df16d38af652ce793ea407b34df3e39023
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1999-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or
11 * sell copies of the Software, and to permit persons to whom
12 * the Software is furnished to do so, subject to the following
13 * conditions:
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
27 * ----------------------------------------------------------------------- */
30 * initvesa.c
32 * Query the VESA BIOS and select a 640x480x32 mode with local mapping
33 * support, if one exists.
36 #include <inttypes.h>
37 #include <com32.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <limits.h>
42 #include "vesa.h"
43 #include "mboot.h"
45 struct vesa_info vesa_info;
47 void set_graphics_mode(const struct multiboot_header *mbh,
48 struct multiboot_info *mbi)
50 com32sys_t rm;
51 uint16_t mode, bestmode, *mode_ptr;
52 struct vesa_general_info *gi;
53 struct vesa_mode_info *mi;
54 int pxf, bestpxf;
55 int wantx, wanty;
56 int err, besterr;
57 bool better;
58 addr_t viaddr;
60 /* Only do this if requested by the OS image */
61 if (!(mbh->flags & MULTIBOOT_VIDEO_MODE) || mbh->mode_type != 0)
62 return;
64 /* Allocate space in the bounce buffer for these structures */
65 gi = &((struct vesa_info *)__com32.cs_bounce)->gi;
66 mi = &((struct vesa_info *)__com32.cs_bounce)->mi;
68 memset(&rm, 0, sizeof rm);
69 memset(gi, 0, sizeof *gi);
71 gi->signature = VBE2_MAGIC; /* Get VBE2 extended data */
72 rm.eax.w[0] = 0x4F00; /* Get SVGA general information */
73 rm.edi.w[0] = OFFS(gi);
74 rm.es = SEG(gi);
75 __intcall(0x10, &rm, &rm);
77 if (rm.eax.w[0] != 0x004F)
78 return; /* Function call failed */
79 if (gi->signature != VESA_MAGIC)
80 return; /* No magic */
81 if (gi->version < 0x0102)
82 return; /* VESA 1.2+ required */
84 memcpy(&vesa_info.gi, gi, sizeof *gi);
86 /* Search for a suitable mode with a suitable color and memory model... */
88 mode_ptr = GET_PTR(gi->video_mode_ptr);
89 bestmode = 0;
90 bestpxf = 0;
91 wantx = mbh->width ? mbh->width : 0xffff;
92 wanty = mbh->height ? mbh->height : 0xffff;
93 besterr = wantx + wanty;
95 while ((mode = *mode_ptr++) != 0xFFFF) {
96 mode &= 0x1FF; /* The rest are attributes of sorts */
98 memset(mi, 0, sizeof *mi);
99 rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */
100 rm.ecx.w[0] = mode;
101 rm.edi.w[0] = OFFS(mi);
102 rm.es = SEG(mi);
103 __intcall(0x10, &rm, &rm);
105 /* Must be a supported mode */
106 if (rm.eax.w[0] != 0x004f)
107 continue;
109 /* Must be an LFB color graphics mode supported by the hardware.
111 The bits tested are:
112 7 - linear frame buffer
113 4 - graphics mode
114 3 - color mode
115 1 - mode information available (mandatory in VBE 1.2+)
116 0 - mode supported by hardware
118 if ((mi->mode_attr & 0x009b) != 0x009b)
119 continue;
121 /* We don't support multibank (interlaced memory) modes */
123 * Note: The Bochs VESA BIOS (vbe.c 1.58 2006/08/19) violates the
124 * specification which states that banks == 1 for unbanked modes;
125 * fortunately it does report bank_size == 0 for those.
127 if (mi->banks > 1 && mi->bank_size)
128 continue;
130 /* Must either be a packed-pixel mode or a direct color mode
131 (depending on VESA version ); must be a supported pixel format */
133 if (mi->bpp == 32 &&
134 (mi->memory_layout == 4 ||
135 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
136 mi->bpos == 0)))
137 pxf = 32;
138 else if (mi->bpp == 24 &&
139 (mi->memory_layout == 4 ||
140 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
141 mi->bpos == 0)))
142 pxf = 24;
143 else if (mi->bpp == 16 &&
144 (mi->memory_layout == 4 ||
145 (mi->memory_layout == 6 && mi->rpos == 11 && mi->gpos == 5 &&
146 mi->bpos == 0)))
147 pxf = 16;
148 else if (mi->bpp == 15 &&
149 (mi->memory_layout == 4 ||
150 (mi->memory_layout == 6 && mi->rpos == 10 && mi->gpos == 5 &&
151 mi->bpos == 0)))
152 pxf = 15;
153 else
154 continue;
156 better = false;
157 err = abs(mi->h_res - wantx) + abs(mi->v_res - wanty);
159 #define IS_GOOD(mi, bestx, besty) \
160 ((mi)->h_res >= (bestx) && (mi)->v_res >= (besty))
162 if (!bestpxf)
163 better = true;
164 else if (!IS_GOOD(&vesa_info.mi, wantx, wanty) &&
165 IS_GOOD(mi, wantx, wanty))
166 /* This matches criteria, which the previous one didn't */
167 better = true;
168 else if (IS_GOOD(&vesa_info.mi, wantx, wanty) &&
169 !IS_GOOD(mi, wantx, wanty))
170 /* This doesn't match criteria, and the previous one did */
171 better = false;
172 else if (err < besterr)
173 better = true;
174 else if (err == besterr && (pxf == (int)mbh->depth || pxf > bestpxf))
175 better = true;
177 if (better) {
178 bestmode = mode;
179 bestpxf = pxf;
180 memcpy(&vesa_info.mi, mi, sizeof *mi);
184 if (!bestpxf)
185 return; /* No mode found */
187 mi = &vesa_info.mi;
188 mode = bestmode;
190 /* Now set video mode */
191 rm.eax.w[0] = 0x4F02; /* Set SVGA video mode */
192 mode |= 0x4000; /* Request linear framebuffer */
193 rm.ebx.w[0] = mode;
194 __intcall(0x10, &rm, &rm);
195 if (rm.eax.w[0] != 0x004F)
196 return; /* Failed to set mode */
198 mbi->flags |= MB_INFO_VIDEO_INFO;
199 mbi->vbe_mode = mode;
200 viaddr = map_data(&vesa_info, sizeof vesa_info, 4, 0);
201 mbi->vbe_control_info = viaddr + offsetof(struct vesa_info, gi);
202 mbi->vbe_mode_info = viaddr + offsetof(struct vesa_info, mi);
204 /* Get the VBE 2.x PM entry point if supported */
205 rm.eax.w[0] = 0x4F0A;
206 rm.ebx.w[0] = 0;
207 __intcall(0x10, &rm, &rm);
208 if (rm.eax.w[0] == 0x004F) {
209 mbi->vbe_interface_seg = rm.es;
210 mbi->vbe_interface_off = rm.edi.w[0];
211 mbi->vbe_interface_len = rm.ecx.w[0];
214 /* Tell syslinux we changed video mode */
215 rm.eax.w[0] = 0x0017; /* Report video mode change */
216 /* In theory this should be:
218 rm.ebx.w[0] = (mi->mode_attr & 4) ? 0x0007 : 0x000f;
220 However, that would assume all systems that claim to handle text
221 output in VESA modes actually do that... */
222 rm.ebx.w[0] = 0x000f;
223 rm.ecx.w[0] = vesa_info.mi.h_res;
224 rm.edx.w[0] = vesa_info.mi.v_res;
225 __intcall(0x22, &rm, NULL);