Adding upstream version 4.00~pre53+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / sys / vesa / initvesa.c
blob9a1ae384698112235bae28b85c015e2574239988
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1999-2008 H. Peter Anvin - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
12 * conditions:
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * ----------------------------------------------------------------------- */
29 * initvesa.c
31 * Query the VESA BIOS and select a 640x480x32 mode with local mapping
32 * support, if one exists.
35 #include <inttypes.h>
36 #include <com32.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/fpu.h>
40 #include <syslinux/video.h>
41 #include "vesa.h"
42 #include "video.h"
43 #include "fill.h"
44 #include "debug.h"
46 struct vesa_info __vesa_info;
48 struct vesa_char *__vesacon_text_display;
50 int __vesacon_font_height;
51 int __vesacon_text_rows;
52 int __vesacon_text_cols;
53 enum vesa_pixel_format __vesacon_pixel_format = PXF_NONE;
54 unsigned int __vesacon_bytes_per_pixel;
55 uint8_t __vesacon_graphics_font[FONT_MAX_CHARS][FONT_MAX_HEIGHT];
57 uint32_t *__vesacon_background, *__vesacon_shadowfb;
59 static void unpack_font(uint8_t * dst, uint8_t * src, int height)
61 int i;
63 for (i = 0; i < FONT_MAX_CHARS; i++) {
64 memcpy(dst, src, height);
65 memset(dst + height, 0, FONT_MAX_HEIGHT - height);
67 dst += FONT_MAX_HEIGHT;
68 src += height;
72 static int __constfunc is_power_of_2(unsigned int x)
74 return x && !(x & (x - 1));
77 static int vesacon_paged_mode_ok(const struct vesa_mode_info *mi)
79 int i;
81 if (!is_power_of_2(mi->win_size) ||
82 !is_power_of_2(mi->win_grain) || mi->win_grain > mi->win_size)
83 return 0; /* Impossible... */
85 for (i = 0; i < 2; i++) {
86 if ((mi->win_attr[i] & 0x05) == 0x05 && mi->win_seg[i])
87 return 1; /* Usable window */
90 return 0; /* Nope... */
93 static int vesacon_set_mode(int x, int y)
95 com32sys_t rm;
96 uint8_t *rom_font;
97 uint16_t mode, bestmode, *mode_ptr;
98 struct vesa_info *vi;
99 struct vesa_general_info *gi;
100 struct vesa_mode_info *mi;
101 enum vesa_pixel_format pxf, bestpxf;
102 int err = 0;
104 debug("Hello, World!\r\n");
106 /* Free any existing data structures */
107 if (__vesacon_background) {
108 free(__vesacon_background);
109 __vesacon_background = NULL;
111 if (__vesacon_shadowfb) {
112 free(__vesacon_shadowfb);
113 __vesacon_shadowfb = NULL;
116 /* Allocate space in the bounce buffer for these structures */
117 vi = lzalloc(sizeof *vi);
118 if (!vi) {
119 err = 10; /* Out of memory */
120 goto exit;
122 gi = &vi->gi;
123 mi = &vi->mi;
125 memset(&rm, 0, sizeof rm);
127 gi->signature = VBE2_MAGIC; /* Get VBE2 extended data */
128 rm.eax.w[0] = 0x4F00; /* Get SVGA general information */
129 rm.edi.w[0] = OFFS(gi);
130 rm.es = SEG(gi);
131 __intcall(0x10, &rm, &rm);
133 if (rm.eax.w[0] != 0x004F) {
134 err = 1; /* Function call failed */
135 goto exit;
137 if (gi->signature != VESA_MAGIC) {
138 err = 2; /* No magic */
139 goto exit;
141 if (gi->version < 0x0102) {
142 err = 3; /* VESA 1.2+ required */
143 goto exit;
146 /* Copy general info */
147 memcpy(&__vesa_info.gi, gi, sizeof *gi);
149 /* Search for the proper mode with a suitable color and memory model... */
151 mode_ptr = GET_PTR(gi->video_mode_ptr);
152 bestmode = 0;
153 bestpxf = PXF_NONE;
155 while ((mode = *mode_ptr++) != 0xFFFF) {
156 mode &= 0x1FF; /* The rest are attributes of sorts */
158 debug("Found mode: 0x%04x\r\n", mode);
160 memset(mi, 0, sizeof *mi);
161 rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */
162 rm.ecx.w[0] = mode;
163 rm.edi.w[0] = OFFS(mi);
164 rm.es = SEG(mi);
165 __intcall(0x10, &rm, &rm);
167 /* Must be a supported mode */
168 if (rm.eax.w[0] != 0x004f)
169 continue;
171 debug
172 ("mode_attr 0x%04x, h_res = %4d, v_res = %4d, bpp = %2d, layout = %d (%d,%d,%d)\r\n",
173 mi->mode_attr, mi->h_res, mi->v_res, mi->bpp, mi->memory_layout,
174 mi->rpos, mi->gpos, mi->bpos);
176 /* Must be an LFB color graphics mode supported by the hardware.
178 The bits tested are:
179 4 - graphics mode
180 3 - color mode
181 1 - mode information available (mandatory in VBE 1.2+)
182 0 - mode supported by hardware
184 if ((mi->mode_attr & 0x001b) != 0x001b)
185 continue;
187 /* Must be the chosen size */
188 if (mi->h_res != x || mi->v_res != y)
189 continue;
191 /* We don't support multibank (interlaced memory) modes */
193 * Note: The Bochs VESA BIOS (vbe.c 1.58 2006/08/19) violates the
194 * specification which states that banks == 1 for unbanked modes;
195 * fortunately it does report bank_size == 0 for those.
197 if (mi->banks > 1 && mi->bank_size) {
198 debug("bad: banks = %d, banksize = %d, pages = %d\r\n",
199 mi->banks, mi->bank_size, mi->image_pages);
200 continue;
203 /* Must be either a flat-framebuffer mode, or be an acceptable
204 paged mode */
205 if (!(mi->mode_attr & 0x0080) && !vesacon_paged_mode_ok(mi)) {
206 debug("bad: invalid paged mode\r\n");
207 continue;
210 /* Must either be a packed-pixel mode or a direct color mode
211 (depending on VESA version ); must be a supported pixel format */
212 pxf = PXF_NONE; /* Not usable */
214 if (mi->bpp == 32 &&
215 (mi->memory_layout == 4 ||
216 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
217 mi->bpos == 0)))
218 pxf = PXF_BGRA32;
219 else if (mi->bpp == 24 &&
220 (mi->memory_layout == 4 ||
221 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
222 mi->bpos == 0)))
223 pxf = PXF_BGR24;
224 else if (mi->bpp == 16 &&
225 (mi->memory_layout == 4 ||
226 (mi->memory_layout == 6 && mi->rpos == 11 && mi->gpos == 5 &&
227 mi->bpos == 0)))
228 pxf = PXF_LE_RGB16_565;
229 else if (mi->bpp == 15 &&
230 (mi->memory_layout == 4 ||
231 (mi->memory_layout == 6 && mi->rpos == 10 && mi->gpos == 5 &&
232 mi->bpos == 0)))
233 pxf = PXF_LE_RGB15_555;
235 if (pxf < bestpxf) {
236 debug("Best mode so far, pxf = %d\r\n", pxf);
238 /* Best mode so far... */
239 bestmode = mode;
240 bestpxf = pxf;
242 /* Copy mode info */
243 memcpy(&__vesa_info.mi, mi, sizeof *mi);
247 if (bestpxf == PXF_NONE) {
248 err = 4; /* No mode found */
249 goto exit;
252 mi = &__vesa_info.mi;
253 mode = bestmode;
254 __vesacon_bytes_per_pixel = (mi->bpp + 7) >> 3;
255 __vesacon_format_pixels = __vesacon_format_pixels_list[bestpxf];
257 /* Download the SYSLINUX- or BIOS-provided font */
258 __vesacon_font_height = syslinux_font_query(&rom_font);
259 if (!__vesacon_font_height) {
260 /* Get BIOS 8x16 font */
262 rm.eax.w[0] = 0x1130; /* Get Font Information */
263 rm.ebx.w[0] = 0x0600; /* Get 8x16 ROM font */
264 __intcall(0x10, &rm, &rm);
265 rom_font = MK_PTR(rm.es, rm.ebp.w[0]);
266 __vesacon_font_height = 16;
268 unpack_font((uint8_t *) __vesacon_graphics_font, rom_font,
269 __vesacon_font_height);
271 /* Now set video mode */
272 rm.eax.w[0] = 0x4F02; /* Set SVGA video mode */
273 if (mi->mode_attr & 0x0080)
274 mode |= 0x4000; /* Request linear framebuffer if supported */
275 rm.ebx.w[0] = mode;
276 __intcall(0x10, &rm, &rm);
277 if (rm.eax.w[0] != 0x004F) {
278 err = 9; /* Failed to set mode */
279 goto exit;
282 __vesacon_background = calloc(mi->h_res*mi->v_res, 4);
283 __vesacon_shadowfb = calloc(mi->h_res*mi->v_res, 4);
285 __vesacon_init_copy_to_screen();
287 /* Tell syslinux we changed video mode */
288 /* In theory this should be:
290 flags = (mi->mode_attr & 4) ? 0x0007 : 0x000f;
292 However, that would assume all systems that claim to handle text
293 output in VESA modes actually do that... */
294 syslinux_report_video_mode(0x000f, mi->h_res, mi->v_res);
296 __vesacon_pixel_format = bestpxf;
298 exit:
299 if (vi)
300 lfree(vi);
302 return err;
305 static int init_text_display(void)
307 size_t nchars;
308 struct vesa_char *ptr;
309 struct vesa_char def_char = {
310 .ch = ' ',
311 .attr = 0,
314 if (__vesacon_text_display)
315 free(__vesacon_text_display);
317 __vesacon_text_cols = TEXT_PIXEL_COLS / FONT_WIDTH;
318 __vesacon_text_rows = TEXT_PIXEL_ROWS / __vesacon_font_height;
319 nchars = (__vesacon_text_cols+2) * (__vesacon_text_rows+2);
321 __vesacon_text_display = ptr = malloc(nchars * sizeof(struct vesa_char));
323 if (!ptr)
324 return -1;
326 vesacon_fill(ptr, def_char, nchars);
327 __vesacon_init_cursor(__vesacon_font_height);
329 return 0;
332 int __vesacon_init(int x, int y)
334 int rv;
336 /* We need the FPU for graphics, at least libpng et al will need it... */
337 if (x86_init_fpu())
338 return 10;
340 rv = vesacon_set_mode(x, y);
341 if (rv) {
342 /* Try to see if we can just patch the BIOS... */
343 if (__vesacon_i915resolution(x, y))
344 return rv;
345 if (vesacon_set_mode(x, y))
346 return rv;
349 init_text_display();
351 debug("Mode set, now drawing at %#p\r\n", __vesa_info.mi.lfb_ptr);
353 __vesacon_init_background();
355 debug("Ready!\r\n");
356 return 0;