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.
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
)
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
;
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
)
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)
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
);
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
);
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 */
139 rm
.edi
.w
[0] = OFFS(mi
);
141 __intcall(0x10, &rm
, &rm
);
143 /* Must be a supported mode */
144 if ( rm
.eax
.w
[0] != 0x004f )
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.
156 1 - mode information available (mandatory in VBE 1.2+)
157 0 - mode supported by hardware
159 if ( (mi
->mode_attr
& 0x001b) != 0x001b )
162 /* Must be 640x480 */
163 if ( mi
->h_res
!= VIDEO_X_SIZE
|| mi
->v_res
!= VIDEO_Y_SIZE
)
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
);
178 /* Must be either a flat-framebuffer mode, or be an acceptable
180 if ( !(mi
->mode_attr
& 0x0080) && !vesacon_paged_mode_ok(mi
) ) {
181 debug("bad: invalid paged mode\r\n");
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 */
190 (mi
->memory_layout
== 4 ||
191 (mi
->memory_layout
== 6 && mi
->rpos
== 16 && mi
->gpos
== 8 &&
194 else if (mi
->bpp
== 24 &&
195 (mi
->memory_layout
== 4 ||
196 (mi
->memory_layout
== 6 && mi
->rpos
== 16 && mi
->gpos
== 8 &&
199 else if (mi
->bpp
== 16 &&
200 (mi
->memory_layout
== 4 ||
201 (mi
->memory_layout
== 6 && mi
->rpos
== 11 && mi
->gpos
== 5 &&
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 &&
208 pxf
= PXF_LE_RGB15_555
;
211 debug("Best mode so far, pxf = %d\r\n", pxf
);
213 /* Best mode so far... */
218 memcpy(&__vesa_info
.mi
, mi
, sizeof *mi
);
222 if (bestpxf
== PXF_NONE
)
223 return 4; /* No mode found */
225 mi
= &__vesa_info
.mi
;
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]);
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 */
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
;
278 static int init_text_display(void)
281 struct vesa_char
*ptr
;
282 struct vesa_char def_char
= {
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
));
296 vesacon_fill(ptr
, def_char
, nchars
);
301 int __vesacon_init(void)
305 /* We need the FPU for graphics, at least libpng et al will need it... */
309 rv
= vesacon_set_mode();
315 debug("Mode set, now drawing at %#p\r\n", __vesa_info
.mi
.lfb_ptr
);
317 __vesacon_init_background();