Add memtest support.
[syslinux-debian/hramrach.git] / com32 / mboot / initvesa.c
blobbd869e3d8ac66fbfc0681ae57bdf89054b27477c
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>
41 #include <graphics.h>
43 #include "vesa.h"
44 #include "mboot.h"
46 struct vesa_info vesa_info;
48 void set_graphics_mode(const struct multiboot_header *mbh,
49 struct multiboot_info *mbi)
51 com32sys_t rm;
52 uint16_t mode, bestmode, *mode_ptr;
53 struct vesa_general_info *gi;
54 struct vesa_mode_info *mi;
55 int pxf, bestpxf;
56 int wantx, wanty;
57 int err, besterr;
58 bool better;
59 addr_t viaddr;
61 /* Only do this if requested by the OS image */
62 if (!(mbh->flags & MULTIBOOT_VIDEO_MODE) || mbh->mode_type != 0)
63 return;
65 gi = lmalloc(sizeof *gi);
66 if (!gi)
67 return;
69 mi = lmalloc(sizeof *mi);
70 if (!mi)
71 goto out;
73 memset(&rm, 0, sizeof rm);
74 memset(gi, 0, sizeof *gi);
76 gi->signature = VBE2_MAGIC; /* Get VBE2 extended data */
77 rm.eax.w[0] = 0x4F00; /* Get SVGA general information */
78 rm.edi.w[0] = OFFS(gi);
79 rm.es = SEG(gi);
80 __intcall(0x10, &rm, &rm);
82 if (rm.eax.w[0] != 0x004F)
83 goto out; /* Function call failed */
84 if (gi->signature != VESA_MAGIC)
85 goto out; /* No magic */
86 if (gi->version < 0x0102)
87 goto out; /* VESA 1.2+ required */
89 memcpy(&vesa_info.gi, gi, sizeof *gi);
91 /* Search for a suitable mode with a suitable color and memory model... */
93 mode_ptr = GET_PTR(gi->video_mode_ptr);
94 bestmode = 0;
95 bestpxf = 0;
96 wantx = mbh->width ? mbh->width : 0xffff;
97 wanty = mbh->height ? mbh->height : 0xffff;
98 besterr = wantx + wanty;
100 while ((mode = *mode_ptr++) != 0xFFFF) {
101 mode &= 0x1FF; /* The rest are attributes of sorts */
103 memset(mi, 0, sizeof *mi);
104 rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */
105 rm.ecx.w[0] = mode;
106 rm.edi.w[0] = OFFS(mi);
107 rm.es = SEG(mi);
108 __intcall(0x10, &rm, &rm);
110 /* Must be a supported mode */
111 if (rm.eax.w[0] != 0x004f)
112 continue;
114 /* Must be an LFB color graphics mode supported by the hardware.
116 The bits tested are:
117 7 - linear frame buffer
118 4 - graphics mode
119 3 - color mode
120 1 - mode information available (mandatory in VBE 1.2+)
121 0 - mode supported by hardware
123 if ((mi->mode_attr & 0x009b) != 0x009b)
124 continue;
126 /* We don't support multibank (interlaced memory) modes */
128 * Note: The Bochs VESA BIOS (vbe.c 1.58 2006/08/19) violates the
129 * specification which states that banks == 1 for unbanked modes;
130 * fortunately it does report bank_size == 0 for those.
132 if (mi->banks > 1 && mi->bank_size)
133 continue;
135 /* Must either be a packed-pixel mode or a direct color mode
136 (depending on VESA version ); must be a supported pixel format */
138 if (mi->bpp == 32 &&
139 (mi->memory_layout == 4 ||
140 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
141 mi->bpos == 0)))
142 pxf = 32;
143 else if (mi->bpp == 24 &&
144 (mi->memory_layout == 4 ||
145 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
146 mi->bpos == 0)))
147 pxf = 24;
148 else if (mi->bpp == 16 &&
149 (mi->memory_layout == 4 ||
150 (mi->memory_layout == 6 && mi->rpos == 11 && mi->gpos == 5 &&
151 mi->bpos == 0)))
152 pxf = 16;
153 else if (mi->bpp == 15 &&
154 (mi->memory_layout == 4 ||
155 (mi->memory_layout == 6 && mi->rpos == 10 && mi->gpos == 5 &&
156 mi->bpos == 0)))
157 pxf = 15;
158 else
159 continue;
161 better = false;
162 err = abs(mi->h_res - wantx) + abs(mi->v_res - wanty);
164 #define IS_GOOD(mi, bestx, besty) \
165 ((mi)->h_res >= (bestx) && (mi)->v_res >= (besty))
167 if (!bestpxf)
168 better = true;
169 else if (!IS_GOOD(&vesa_info.mi, wantx, wanty) &&
170 IS_GOOD(mi, wantx, wanty))
171 /* This matches criteria, which the previous one didn't */
172 better = true;
173 else if (IS_GOOD(&vesa_info.mi, wantx, wanty) &&
174 !IS_GOOD(mi, wantx, wanty))
175 /* This doesn't match criteria, and the previous one did */
176 better = false;
177 else if (err < besterr)
178 better = true;
179 else if (err == besterr && (pxf == (int)mbh->depth || pxf > bestpxf))
180 better = true;
182 if (better) {
183 bestmode = mode;
184 bestpxf = pxf;
185 memcpy(&vesa_info.mi, mi, sizeof *mi);
189 if (!bestpxf)
190 goto out; /* No mode found */
192 mi = &vesa_info.mi;
193 mode = bestmode;
195 /* Now set video mode */
196 rm.eax.w[0] = 0x4F02; /* Set SVGA video mode */
197 mode |= 0x4000; /* Request linear framebuffer */
198 rm.ebx.w[0] = mode;
199 __intcall(0x10, &rm, &rm);
200 if (rm.eax.w[0] != 0x004F)
201 goto out; /* Failed to set mode */
203 mbi->flags |= MB_INFO_VIDEO_INFO;
204 mbi->vbe_mode = mode;
205 viaddr = map_data(&vesa_info, sizeof vesa_info, 4, 0);
206 mbi->vbe_control_info = viaddr + offsetof(struct vesa_info, gi);
207 mbi->vbe_mode_info = viaddr + offsetof(struct vesa_info, mi);
209 /* Get the VBE 2.x PM entry point if supported */
210 rm.eax.w[0] = 0x4F0A;
211 rm.ebx.w[0] = 0;
212 __intcall(0x10, &rm, &rm);
213 if (rm.eax.w[0] == 0x004F) {
214 mbi->vbe_interface_seg = rm.es;
215 mbi->vbe_interface_off = rm.edi.w[0];
216 mbi->vbe_interface_len = rm.ecx.w[0];
219 /* In theory this should be:
221 * UsingVga = (mi->mode_attr & 4) ? 0x0007 : 0x000f;
223 * However, that would assume all systems that claim to handle text
224 * output in VESA modes actually do that...
226 graphics_using_vga(0x0F, vesa_info.mi.h_res, vesa_info.mi.v_res);
228 out:
229 lfree(mi);
230 lfree(gi);