Adding upstream version 3.62+dfsg.
[syslinux-debian/hramrach.git] / com32 / lib / sys / vesa / initvesa.c
blob0c2574dbd63d4fdfa96767c764e2a158a6d708ba
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 "vesa.h"
41 #include "video.h"
42 #include "fill.h"
43 #include "debug.h"
45 struct vesa_info __vesa_info;
47 struct vesa_char *__vesacon_text_display;
49 int __vesacon_font_height, __vesacon_text_rows;
50 enum vesa_pixel_format __vesacon_pixel_format = PXF_NONE;
51 unsigned int __vesacon_bytes_per_pixel;
52 uint8_t __vesacon_graphics_font[FONT_MAX_CHARS][FONT_MAX_HEIGHT];
54 uint32_t __vesacon_background[VIDEO_Y_SIZE][VIDEO_X_SIZE];
56 static void unpack_font(uint8_t *dst, uint8_t *src, int height)
58 int i;
60 for (i = 0; i < FONT_MAX_CHARS; i++) {
61 memcpy(dst, src, height);
62 memset(dst+height, 0, FONT_MAX_HEIGHT-height);
64 dst += FONT_MAX_HEIGHT;
65 src += height;
69 static int __constfunc is_power_of_2(unsigned int x)
71 return x && !(x & (x-1));
74 static int vesacon_paged_mode_ok(const struct vesa_mode_info *mi)
76 int i;
78 if (!is_power_of_2(mi->win_size) ||
79 !is_power_of_2(mi->win_grain) ||
80 mi->win_grain > mi->win_size)
81 return 0; /* Impossible... */
83 for (i = 0; i < 2; i++) {
84 if ((mi->win_attr[i] & 0x05) == 0x05 && mi->win_seg[i])
85 return 1; /* Usable window */
88 return 0; /* Nope... */
91 static int vesacon_set_mode(void)
93 com32sys_t rm;
94 uint8_t *rom_font;
95 uint16_t mode, bestmode, *mode_ptr;
96 struct vesa_general_info *gi;
97 struct vesa_mode_info *mi;
98 enum vesa_pixel_format pxf, bestpxf;
100 /* Allocate space in the bounce buffer for these structures */
101 gi = &((struct vesa_info *)__com32.cs_bounce)->gi;
102 mi = &((struct vesa_info *)__com32.cs_bounce)->mi;
104 debug("Hello, World!\r\n");
106 memset(&rm, 0, sizeof rm);
107 memset(gi, 0, sizeof *gi);
109 gi->signature = VBE2_MAGIC; /* Get VBE2 extended data */
110 rm.eax.w[0] = 0x4F00; /* Get SVGA general information */
111 rm.edi.w[0] = OFFS(gi);
112 rm.es = SEG(gi);
113 __intcall(0x10, &rm, &rm);
115 if ( rm.eax.w[0] != 0x004F )
116 return 1; /* Function call failed */
117 if ( gi->signature != VESA_MAGIC )
118 return 2; /* No magic */
119 if ( gi->version < 0x0102 )
120 return 3; /* VESA 1.2+ required */
122 /* Copy general info */
123 memcpy(&__vesa_info.gi, gi, sizeof *gi);
125 /* Search for a 640x480 mode with a suitable color and memory model... */
127 mode_ptr = GET_PTR(gi->video_mode_ptr);
128 bestmode = 0;
129 bestpxf = PXF_NONE;
131 while ((mode = *mode_ptr++) != 0xFFFF) {
132 mode &= 0x1FF; /* The rest are attributes of sorts */
134 debug("Found mode: 0x%04x\r\n", mode);
136 memset(mi, 0, sizeof *mi);
137 rm.eax.w[0] = 0x4F01; /* Get SVGA mode information */
138 rm.ecx.w[0] = mode;
139 rm.edi.w[0] = OFFS(mi);
140 rm.es = SEG(mi);
141 __intcall(0x10, &rm, &rm);
143 /* Must be a supported mode */
144 if ( rm.eax.w[0] != 0x004f )
145 continue;
147 debug("mode_attr 0x%04x, h_res = %4d, v_res = %4d, bpp = %2d, layout = %d (%d,%d,%d)\r\n",
148 mi->mode_attr, mi->h_res, mi->v_res, mi->bpp, mi->memory_layout,
149 mi->rpos, mi->gpos, mi->bpos);
151 /* Must be an LFB color graphics mode supported by the hardware.
153 The bits tested are:
154 4 - graphics mode
155 3 - color mode
156 1 - mode information available (mandatory in VBE 1.2+)
157 0 - mode supported by hardware
159 if ( (mi->mode_attr & 0x001b) != 0x001b )
160 continue;
162 /* Must be 640x480 */
163 if ( mi->h_res != VIDEO_X_SIZE || mi->v_res != VIDEO_Y_SIZE )
164 continue;
166 /* We don't support multibank (interlaced memory) modes */
168 * Note: The Bochs VESA BIOS (vbe.c 1.58 2006/08/19) violates the
169 * specification which states that banks == 1 for unbanked modes;
170 * fortunately it does report bank_size == 0 for those.
172 if ( mi->banks > 1 && mi->bank_size ) {
173 debug("bad: banks = %d, banksize = %d, pages = %d\r\n",
174 mi->banks, mi->bank_size, mi->image_pages);
175 continue;
178 /* Must be either a flat-framebuffer mode, or be an acceptable
179 paged mode */
180 if ( !(mi->mode_attr & 0x0080) && !vesacon_paged_mode_ok(mi) ) {
181 debug("bad: invalid paged mode\r\n");
182 continue;
185 /* Must either be a packed-pixel mode or a direct color mode
186 (depending on VESA version ); must be a supported pixel format */
187 pxf = PXF_NONE; /* Not usable */
189 if (mi->bpp == 32 &&
190 (mi->memory_layout == 4 ||
191 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
192 mi->bpos == 0)))
193 pxf = PXF_BGRA32;
194 else if (mi->bpp == 24 &&
195 (mi->memory_layout == 4 ||
196 (mi->memory_layout == 6 && mi->rpos == 16 && mi->gpos == 8 &&
197 mi->bpos == 0)))
198 pxf = PXF_BGR24;
199 else if (mi->bpp == 16 &&
200 (mi->memory_layout == 4 ||
201 (mi->memory_layout == 6 && mi->rpos == 11 && mi->gpos == 5 &&
202 mi->bpos == 0)))
203 pxf = PXF_LE_RGB16_565;
204 else if (mi->bpp == 15 &&
205 (mi->memory_layout == 4 ||
206 (mi->memory_layout == 6 && mi->rpos == 10 && mi->gpos == 5 &&
207 mi->bpos == 0)))
208 pxf = PXF_LE_RGB15_555;
210 if (pxf < bestpxf) {
211 debug("Best mode so far, pxf = %d\r\n", pxf);
213 /* Best mode so far... */
214 bestmode = mode;
215 bestpxf = pxf;
217 /* Copy mode info */
218 memcpy(&__vesa_info.mi, mi, sizeof *mi);
222 if (bestpxf == PXF_NONE)
223 return 4; /* No mode found */
225 mi = &__vesa_info.mi;
226 mode = bestmode;
227 __vesacon_bytes_per_pixel = (mi->bpp+7) >> 3;
228 __vesacon_format_pixels = __vesacon_format_pixels_list[bestpxf];
230 /* Download the SYSLINUX- or BIOS-provided font */
231 rm.eax.w[0] = 0x0018; /* Query custom font */
232 __intcall(0x22, &rm, &rm);
233 if (!(rm.eflags.l & EFLAGS_CF) && rm.eax.b[0]) {
234 __vesacon_font_height = rm.eax.b[0];
235 rom_font = MK_PTR(rm.es, rm.ebx.w[0]);
236 } else {
237 rm.eax.w[0] = 0x1130; /* Get Font Information */
238 rm.ebx.w[0] = 0x0600; /* Get 8x16 ROM font */
239 __intcall(0x10, &rm, &rm);
240 rom_font = MK_PTR(rm.es, rm.ebp.w[0]);
241 __vesacon_font_height = 16;
243 unpack_font((uint8_t *)__vesacon_graphics_font, rom_font,
244 __vesacon_font_height);
245 __vesacon_text_rows = (VIDEO_Y_SIZE-2*VIDEO_BORDER)/__vesacon_font_height;
246 __vesacon_init_cursor(__vesacon_font_height);
248 /* Now set video mode */
249 rm.eax.w[0] = 0x4F02; /* Set SVGA video mode */
250 if (mi->mode_attr & 0x0080)
251 mode |= 0x4000; /* Request linear framebuffer if supported */
252 rm.ebx.w[0] = mode;
253 __intcall(0x10, &rm, &rm);
254 if ( rm.eax.w[0] != 0x004F )
255 return 9; /* Failed to set mode */
257 __vesacon_init_copy_to_screen();
259 /* Tell syslinux we changed video mode */
260 rm.eax.w[0] = 0x0017; /* Report video mode change */
261 /* In theory this should be:
263 rm.ebx.w[0] = (mi->mode_attr & 4) ? 0x0007 : 0x000f;
265 However, that would assume all systems that claim to handle text
266 output in VESA modes actually do that... */
267 rm.ebx.w[0] = 0x000f;
268 rm.ecx.w[0] = VIDEO_X_SIZE;
269 rm.edx.w[0] = VIDEO_Y_SIZE;
270 __intcall(0x22, &rm, NULL);
272 __vesacon_pixel_format = bestpxf;
274 return 0;
278 static int init_text_display(void)
280 size_t nchars;
281 struct vesa_char *ptr;
282 struct vesa_char def_char = {
283 .ch = ' ',
284 .attr = 0,
287 nchars = (TEXT_PIXEL_ROWS/__vesacon_font_height+2)*
288 (TEXT_PIXEL_COLS/FONT_WIDTH+2);
290 __vesacon_text_display = ptr = malloc(nchars*sizeof(struct vesa_char));
292 if (!ptr)
293 return -1;
296 vesacon_fill(ptr, def_char, nchars);
298 return 0;
301 int __vesacon_init(void)
303 int rv;
305 /* We need the FPU for graphics, at least libpng et al will need it... */
306 if (x86_init_fpu())
307 return 10;
309 rv = vesacon_set_mode();
310 if (rv)
311 return rv;
313 init_text_display();
315 debug("Mode set, now drawing at %#p\r\n", __vesa_info.mi.lfb_ptr);
317 __vesacon_init_background();
319 debug("Ready!\r\n");
320 return 0;