1 | /* ----------------------------------------------------------------------- * |
---|
2 | * |
---|
3 | * Copyright 1999-2008 H. Peter Anvin - All Rights Reserved |
---|
4 | * Copyright 2009 Intel Corporation; author: H. Peter Anvin |
---|
5 | * |
---|
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: |
---|
14 | * |
---|
15 | * The above copyright notice and this permission notice shall |
---|
16 | * be included in all copies or substantial portions of the Software. |
---|
17 | * |
---|
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. |
---|
26 | * |
---|
27 | * ----------------------------------------------------------------------- */ |
---|
28 | |
---|
29 | /* |
---|
30 | * initvesa.c |
---|
31 | * |
---|
32 | * Query the VESA BIOS and select a 640x480x32 mode with local mapping |
---|
33 | * support, if one exists. |
---|
34 | */ |
---|
35 | |
---|
36 | #include <inttypes.h> |
---|
37 | #include <com32.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <string.h> |
---|
40 | #include <limits.h> |
---|
41 | #include <graphics.h> |
---|
42 | |
---|
43 | #include "vesa.h" |
---|
44 | #include "mboot.h" |
---|
45 | |
---|
46 | struct vesa_info vesa_info; |
---|
47 | |
---|
48 | void set_graphics_mode(const struct multiboot_header *mbh, |
---|
49 | struct multiboot_info *mbi) |
---|
50 | { |
---|
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; |
---|
60 | |
---|
61 | /* Only do this if requested by the OS image */ |
---|
62 | if (!(mbh->flags & MULTIBOOT_VIDEO_MODE) || mbh->mode_type != 0) |
---|
63 | return; |
---|
64 | |
---|
65 | gi = lmalloc(sizeof *gi); |
---|
66 | if (!gi) |
---|
67 | return; |
---|
68 | |
---|
69 | mi = lmalloc(sizeof *mi); |
---|
70 | if (!mi) |
---|
71 | goto out; |
---|
72 | |
---|
73 | memset(&rm, 0, sizeof rm); |
---|
74 | memset(gi, 0, sizeof *gi); |
---|
75 | |
---|
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); |
---|
81 | |
---|
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 */ |
---|
88 | |
---|
89 | memcpy(&vesa_info.gi, gi, sizeof *gi); |
---|
90 | |
---|
91 | /* Search for a suitable mode with a suitable color and memory model... */ |
---|
92 | |
---|
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; |
---|
99 | |
---|
100 | while ((mode = *mode_ptr++) != 0xFFFF) { |
---|
101 | mode &= 0x1FF; /* The rest are attributes of sorts */ |
---|
102 | |
---|
103 | memset(&rm, 0, sizeof rm); |
---|
104 | memset(mi, 0, sizeof *mi); |
---|
105 | rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */ |
---|
106 | rm.ecx.w[0] = mode; |
---|
107 | rm.edi.w[0] = OFFS(mi); |
---|
108 | rm.es = SEG(mi); |
---|
109 | __intcall(0x10, &rm, &rm); |
---|
110 | |
---|
111 | /* Must be a supported mode */ |
---|
112 | if (rm.eax.w[0] != 0x004f) |
---|
113 | continue; |
---|
114 | |
---|
115 | /* Must be an LFB color graphics mode supported by the hardware. |
---|
116 | |
---|
117 | The bits tested are: |
---|
118 | 7 - linear frame buffer |
---|
119 | 4 - graphics mode |
---|
120 | 3 - color mode |
---|
121 | 1 - mode information available (mandatory in VBE 1.2+) |
---|
122 | 0 - mode supported by hardware |
---|
123 | */ |
---|
124 | if ((mi->mode_attr & 0x009b) != 0x009b) |
---|
125 | continue; |
---|
126 | |
---|
127 | /* We don't support multibank (interlaced memory) modes */ |
---|
128 | /* |
---|
129 | * Note: The Bochs VESA BIOS (vbe.c 1.58 2006/08/19) violates the |
---|
130 | * specification which states that banks == 1 for unbanked modes; |
---|
131 | * fortunately it does report bank_size == 0 for those. |
---|
132 | */ |
---|
133 | if (mi->banks > 1 && mi->bank_size) |
---|
134 | continue; |
---|
135 | |
---|
136 | /* Must either be a packed-pixel mode or a direct color mode |
---|
137 | (depending on VESA version ); must be a supported pixel format */ |
---|
138 | |
---|
139 | if (mi->bpp == 32 && |
---|
140 | (mi->memory_layout == 4 || |
---|
141 | (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 && |
---|
142 | mi->bpos == 0))) |
---|
143 | pxf = 32; |
---|
144 | else if (mi->bpp == 24 && |
---|
145 | (mi->memory_layout == 4 || |
---|
146 | (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 && |
---|
147 | mi->bpos == 0))) |
---|
148 | pxf = 24; |
---|
149 | else if (mi->bpp == 16 && |
---|
150 | (mi->memory_layout == 4 || |
---|
151 | (mi->memory_layout == 6 && mi->rpos == 11 && mi->gpos == 5 && |
---|
152 | mi->bpos == 0))) |
---|
153 | pxf = 16; |
---|
154 | else if (mi->bpp == 15 && |
---|
155 | (mi->memory_layout == 4 || |
---|
156 | (mi->memory_layout == 6 && mi->rpos == 10 && mi->gpos == 5 && |
---|
157 | mi->bpos == 0))) |
---|
158 | pxf = 15; |
---|
159 | else |
---|
160 | continue; |
---|
161 | |
---|
162 | better = false; |
---|
163 | err = abs(mi->h_res - wantx) + abs(mi->v_res - wanty); |
---|
164 | |
---|
165 | #define IS_GOOD(mi, bestx, besty) \ |
---|
166 | ((mi)->h_res >= (bestx) && (mi)->v_res >= (besty)) |
---|
167 | |
---|
168 | if (!bestpxf) |
---|
169 | better = true; |
---|
170 | else if (!IS_GOOD(&vesa_info.mi, wantx, wanty) && |
---|
171 | IS_GOOD(mi, wantx, wanty)) |
---|
172 | /* This matches criteria, which the previous one didn't */ |
---|
173 | better = true; |
---|
174 | else if (IS_GOOD(&vesa_info.mi, wantx, wanty) && |
---|
175 | !IS_GOOD(mi, wantx, wanty)) |
---|
176 | /* This doesn't match criteria, and the previous one did */ |
---|
177 | better = false; |
---|
178 | else if (err < besterr) |
---|
179 | better = true; |
---|
180 | else if (err == besterr && (pxf == (int)mbh->depth || pxf > bestpxf)) |
---|
181 | better = true; |
---|
182 | |
---|
183 | if (better) { |
---|
184 | bestmode = mode; |
---|
185 | bestpxf = pxf; |
---|
186 | memcpy(&vesa_info.mi, mi, sizeof *mi); |
---|
187 | } |
---|
188 | } |
---|
189 | |
---|
190 | if (!bestpxf) |
---|
191 | goto out; /* No mode found */ |
---|
192 | |
---|
193 | mi = &vesa_info.mi; |
---|
194 | mode = bestmode; |
---|
195 | |
---|
196 | /* Now set video mode */ |
---|
197 | memset(&rm, 0, sizeof rm); |
---|
198 | rm.eax.w[0] = 0x4F02; /* Set SVGA video mode */ |
---|
199 | mode |= 0x4000; /* Request linear framebuffer */ |
---|
200 | rm.ebx.w[0] = mode; |
---|
201 | __intcall(0x10, &rm, &rm); |
---|
202 | if (rm.eax.w[0] != 0x004F) |
---|
203 | goto out; /* Failed to set mode */ |
---|
204 | |
---|
205 | mbi->flags |= MB_INFO_VIDEO_INFO; |
---|
206 | mbi->vbe_mode = mode; |
---|
207 | viaddr = map_data(&vesa_info, sizeof vesa_info, 4, 0); |
---|
208 | mbi->vbe_control_info = viaddr + offsetof(struct vesa_info, gi); |
---|
209 | mbi->vbe_mode_info = viaddr + offsetof(struct vesa_info, mi); |
---|
210 | |
---|
211 | /* Get the VBE 2.x PM entry point if supported */ |
---|
212 | rm.eax.w[0] = 0x4F0A; |
---|
213 | rm.ebx.w[0] = 0; |
---|
214 | __intcall(0x10, &rm, &rm); |
---|
215 | if (rm.eax.w[0] == 0x004F) { |
---|
216 | mbi->vbe_interface_seg = rm.es; |
---|
217 | mbi->vbe_interface_off = rm.edi.w[0]; |
---|
218 | mbi->vbe_interface_len = rm.ecx.w[0]; |
---|
219 | } |
---|
220 | |
---|
221 | /* In theory this should be: |
---|
222 | * |
---|
223 | * UsingVga = (mi->mode_attr & 4) ? 0x0007 : 0x000f; |
---|
224 | * |
---|
225 | * However, that would assume all systems that claim to handle text |
---|
226 | * output in VESA modes actually do that... |
---|
227 | */ |
---|
228 | graphics_using_vga(0x0F, vesa_info.mi.h_res, vesa_info.mi.v_res); |
---|
229 | |
---|
230 | out: |
---|
231 | lfree(mi); |
---|
232 | lfree(gi); |
---|
233 | } |
---|