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
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 * ----------------------------------------------------------------------- */
31 * Query the VESA BIOS and select a 640x480x32 mode with local mapping
32 * support, if one exists.
40 #include <syslinux/video.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
)
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
;
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
)
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
)
97 uint16_t mode
, bestmode
, *mode_ptr
;
99 struct vesa_general_info
*gi
;
100 struct vesa_mode_info
*mi
;
101 enum vesa_pixel_format pxf
, bestpxf
;
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
);
119 err
= 10; /* Out of memory */
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
);
131 __intcall(0x10, &rm
, &rm
);
133 if (rm
.eax
.w
[0] != 0x004F) {
134 err
= 1; /* Function call failed */
137 if (gi
->signature
!= VESA_MAGIC
) {
138 err
= 2; /* No magic */
141 if (gi
->version
< 0x0102) {
142 err
= 3; /* VESA 1.2+ required */
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
);
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 */
163 rm
.edi
.w
[0] = OFFS(mi
);
165 __intcall(0x10, &rm
, &rm
);
167 /* Must be a supported mode */
168 if (rm
.eax
.w
[0] != 0x004f)
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.
181 1 - mode information available (mandatory in VBE 1.2+)
182 0 - mode supported by hardware
184 if ((mi
->mode_attr
& 0x001b) != 0x001b)
187 /* Must be the chosen size */
188 if (mi
->h_res
!= x
|| mi
->v_res
!= y
)
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
);
203 /* Must be either a flat-framebuffer mode, or be an acceptable
205 if (!(mi
->mode_attr
& 0x0080) && !vesacon_paged_mode_ok(mi
)) {
206 debug("bad: invalid paged mode\r\n");
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 */
215 (mi
->memory_layout
== 4 ||
216 (mi
->memory_layout
== 6 && mi
->rpos
== 16 && mi
->gpos
== 8 &&
219 else if (mi
->bpp
== 24 &&
220 (mi
->memory_layout
== 4 ||
221 (mi
->memory_layout
== 6 && mi
->rpos
== 16 && mi
->gpos
== 8 &&
224 else if (mi
->bpp
== 16 &&
225 (mi
->memory_layout
== 4 ||
226 (mi
->memory_layout
== 6 && mi
->rpos
== 11 && mi
->gpos
== 5 &&
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 &&
233 pxf
= PXF_LE_RGB15_555
;
236 debug("Best mode so far, pxf = %d\r\n", pxf
);
238 /* Best mode so far... */
243 memcpy(&__vesa_info
.mi
, mi
, sizeof *mi
);
247 if (bestpxf
== PXF_NONE
) {
248 err
= 4; /* No mode found */
252 mi
= &__vesa_info
.mi
;
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 */
276 __intcall(0x10, &rm
, &rm
);
277 if (rm
.eax
.w
[0] != 0x004F) {
278 err
= 9; /* Failed to set mode */
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
;
305 static int init_text_display(void)
308 struct vesa_char
*ptr
;
309 struct vesa_char def_char
= {
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
));
326 vesacon_fill(ptr
, def_char
, nchars
);
327 __vesacon_init_cursor(__vesacon_font_height
);
332 int __vesacon_init(int x
, int y
)
336 /* We need the FPU for graphics, at least libpng et al will need it... */
340 rv
= vesacon_set_mode(x
, y
);
342 /* Try to see if we can just patch the BIOS... */
343 if (__vesacon_i915resolution(x
, y
))
345 if (vesacon_set_mode(x
, y
))
351 debug("Mode set, now drawing at %#p\r\n", __vesa_info
.mi
.lfb_ptr
);
353 __vesacon_init_background();