1 /* ----------------------------------------------------------------------- *
3 * Copyright 1999-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009 Intel Corporation; author: H. Peter Anvin
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
15 * The above copyright notice and this permission notice shall
16 * be included in all copies or substantial portions of the Software.
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.
27 * ----------------------------------------------------------------------- */
32 * Query the VESA BIOS and select a 640x480x32 mode with local mapping
33 * support, if one exists.
46 struct vesa_info vesa_info
;
48 void set_graphics_mode(const struct multiboot_header
*mbh
,
49 struct multiboot_info
*mbi
)
52 uint16_t mode
, bestmode
, *mode_ptr
;
53 struct vesa_general_info
*gi
;
54 struct vesa_mode_info
*mi
;
61 /* Only do this if requested by the OS image */
62 if (!(mbh
->flags
& MULTIBOOT_VIDEO_MODE
) || mbh
->mode_type
!= 0)
65 gi
= lmalloc(sizeof *gi
);
69 mi
= lmalloc(sizeof *mi
);
73 memset(&rm
, 0, sizeof rm
);
74 memset(gi
, 0, sizeof *gi
);
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
);
80 __intcall(0x10, &rm
, &rm
);
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 */
89 memcpy(&vesa_info
.gi
, gi
, sizeof *gi
);
91 /* Search for a suitable mode with a suitable color and memory model... */
93 mode_ptr
= GET_PTR(gi
->video_mode_ptr
);
96 wantx
= mbh
->width
? mbh
->width
: 0xffff;
97 wanty
= mbh
->height
? mbh
->height
: 0xffff;
98 besterr
= wantx
+ wanty
;
100 while ((mode
= *mode_ptr
++) != 0xFFFF) {
101 mode
&= 0x1FF; /* The rest are attributes of sorts */
103 memset(mi
, 0, sizeof *mi
);
104 rm
.eax
.w
[0] = 0x4F01; /* Get SVGA mode information */
106 rm
.edi
.w
[0] = OFFS(mi
);
108 __intcall(0x10, &rm
, &rm
);
110 /* Must be a supported mode */
111 if (rm
.eax
.w
[0] != 0x004f)
114 /* Must be an LFB color graphics mode supported by the hardware.
117 7 - linear frame buffer
120 1 - mode information available (mandatory in VBE 1.2+)
121 0 - mode supported by hardware
123 if ((mi
->mode_attr
& 0x009b) != 0x009b)
126 /* We don't support multibank (interlaced memory) modes */
128 * Note: The Bochs VESA BIOS (vbe.c 1.58 2006/08/19) violates the
129 * specification which states that banks == 1 for unbanked modes;
130 * fortunately it does report bank_size == 0 for those.
132 if (mi
->banks
> 1 && mi
->bank_size
)
135 /* Must either be a packed-pixel mode or a direct color mode
136 (depending on VESA version ); must be a supported pixel format */
139 (mi
->memory_layout
== 4 ||
140 (mi
->memory_layout
== 6 && mi
->rpos
== 16 && mi
->gpos
== 8 &&
143 else if (mi
->bpp
== 24 &&
144 (mi
->memory_layout
== 4 ||
145 (mi
->memory_layout
== 6 && mi
->rpos
== 16 && mi
->gpos
== 8 &&
148 else if (mi
->bpp
== 16 &&
149 (mi
->memory_layout
== 4 ||
150 (mi
->memory_layout
== 6 && mi
->rpos
== 11 && mi
->gpos
== 5 &&
153 else if (mi
->bpp
== 15 &&
154 (mi
->memory_layout
== 4 ||
155 (mi
->memory_layout
== 6 && mi
->rpos
== 10 && mi
->gpos
== 5 &&
162 err
= abs(mi
->h_res
- wantx
) + abs(mi
->v_res
- wanty
);
164 #define IS_GOOD(mi, bestx, besty) \
165 ((mi)->h_res >= (bestx) && (mi)->v_res >= (besty))
169 else if (!IS_GOOD(&vesa_info
.mi
, wantx
, wanty
) &&
170 IS_GOOD(mi
, wantx
, wanty
))
171 /* This matches criteria, which the previous one didn't */
173 else if (IS_GOOD(&vesa_info
.mi
, wantx
, wanty
) &&
174 !IS_GOOD(mi
, wantx
, wanty
))
175 /* This doesn't match criteria, and the previous one did */
177 else if (err
< besterr
)
179 else if (err
== besterr
&& (pxf
== (int)mbh
->depth
|| pxf
> bestpxf
))
185 memcpy(&vesa_info
.mi
, mi
, sizeof *mi
);
190 goto out
; /* No mode found */
195 /* Now set video mode */
196 rm
.eax
.w
[0] = 0x4F02; /* Set SVGA video mode */
197 mode
|= 0x4000; /* Request linear framebuffer */
199 __intcall(0x10, &rm
, &rm
);
200 if (rm
.eax
.w
[0] != 0x004F)
201 goto out
; /* Failed to set mode */
203 mbi
->flags
|= MB_INFO_VIDEO_INFO
;
204 mbi
->vbe_mode
= mode
;
205 viaddr
= map_data(&vesa_info
, sizeof vesa_info
, 4, 0);
206 mbi
->vbe_control_info
= viaddr
+ offsetof(struct vesa_info
, gi
);
207 mbi
->vbe_mode_info
= viaddr
+ offsetof(struct vesa_info
, mi
);
209 /* Get the VBE 2.x PM entry point if supported */
210 rm
.eax
.w
[0] = 0x4F0A;
212 __intcall(0x10, &rm
, &rm
);
213 if (rm
.eax
.w
[0] == 0x004F) {
214 mbi
->vbe_interface_seg
= rm
.es
;
215 mbi
->vbe_interface_off
= rm
.edi
.w
[0];
216 mbi
->vbe_interface_len
= rm
.ecx
.w
[0];
219 /* In theory this should be:
221 * UsingVga = (mi->mode_attr & 4) ? 0x0007 : 0x000f;
223 * However, that would assume all systems that claim to handle text
224 * output in VESA modes actually do that...
226 graphics_using_vga(0x0F, vesa_info
.mi
.h_res
, vesa_info
.mi
.v_res
);