vgabios: Fix linelength calculations in bochsvga and vbe.
[seabios.git] / vgasrc / vbe.c
1 // Video Bios Extensions handlers
2 //
3 // Copyright (C) 2012  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2011  Julian Pidancet <julian.pidancet@citrix.com>
5 // Copyright (C) 2001-2008 the LGPL VGABios developers Team
6 //
7 // This file may be distributed under the terms of the GNU LGPLv3 license.
8
9 #include "vgabios.h" // handle_104f
10 #include "config.h" // CONFIG_*
11 #include "bregs.h" // struct bregs
12 #include "vbe.h" // struct vbe_info
13 #include "util.h" // dprintf
14 #include "biosvar.h" // get_global_set
15 #include "vgahw.h" // vgahw_set_mode
16
17 u32 VBE_total_memory VAR16 = 256 * 1024;
18 u32 VBE_capabilities VAR16;
19 u32 VBE_framebuffer VAR16;
20 u16 VBE_win_granularity VAR16 = 64;
21
22 static void
23 vbe_104f00(struct bregs *regs)
24 {
25     u16 seg = regs->es;
26     struct vbe_info *info = (void*)(regs->di+0);
27
28     if (GET_FARVAR(seg, info->signature) == VBE2_SIGNATURE) {
29         dprintf(4, "Get VBE Controller: VBE2 Signature found\n");
30     } else if (GET_FARVAR(seg, info->signature) == VESA_SIGNATURE) {
31         dprintf(4, "Get VBE Controller: VESA Signature found\n");
32     } else {
33         dprintf(4, "Get VBE Controller: Invalid Signature\n");
34     }
35
36     memset_far(seg, info, 0, sizeof(*info));
37
38     SET_FARVAR(seg, info->signature, VESA_SIGNATURE);
39
40     SET_FARVAR(seg, info->version, 0x0200);
41
42     SET_FARVAR(seg, info->oem_string,
43             SEGOFF(get_global_seg(), (u32)VBE_OEM_STRING));
44     SET_FARVAR(seg, info->capabilities, GET_GLOBAL(VBE_capabilities));
45
46     /* We generate our mode list in the reserved field of the info block */
47     u16 *destmode = (void*)info->reserved;
48     SET_FARVAR(seg, info->video_mode, SEGOFF(seg, (u32)destmode));
49
50     /* Total memory (in 64 blocks) */
51     SET_FARVAR(seg, info->total_memory
52                , GET_GLOBAL(VBE_total_memory) / (64*1024));
53
54     SET_FARVAR(seg, info->oem_vendor_string,
55             SEGOFF(get_global_seg(), (u32)VBE_VENDOR_STRING));
56     SET_FARVAR(seg, info->oem_product_string,
57             SEGOFF(get_global_seg(), (u32)VBE_PRODUCT_STRING));
58     SET_FARVAR(seg, info->oem_revision_string,
59             SEGOFF(get_global_seg(), (u32)VBE_REVISION_STRING));
60
61     /* Fill list of modes */
62     u16 *last = (void*)&info->reserved[sizeof(info->reserved)];
63     vgahw_list_modes(seg, destmode, last - 1);
64
65     regs->ax = 0x004f;
66 }
67
68 static void
69 vbe_104f01(struct bregs *regs)
70 {
71     u16 seg = regs->es;
72     struct vbe_mode_info *info = (void*)(regs->di+0);
73     u16 mode = regs->cx;
74
75     dprintf(1, "VBE mode info request: %x\n", mode);
76
77     struct vgamode_s *vmode_g = vgahw_find_mode(mode);
78     if (! vmode_g) {
79         dprintf(1, "VBE mode %x not found\n", mode);
80         regs->ax = 0x0100;
81         return;
82     }
83
84     memset_far(seg, info, 0, sizeof(*info));
85     u16 mode_attr = VBE_MODE_ATTRIBUTE_SUPPORTED |
86                     VBE_MODE_ATTRIBUTE_EXTENDED_INFORMATION_AVAILABLE |
87                     VBE_MODE_ATTRIBUTE_COLOR_MODE |
88                     VBE_MODE_ATTRIBUTE_GRAPHICS_MODE |
89                     VBE_MODE_ATTRIBUTE_NOT_VGA_COMPATIBLE;
90     u32 framebuffer = GET_GLOBAL(VBE_framebuffer);
91     int depth = GET_GLOBAL(vmode_g->depth);
92     if (depth == 4)
93         mode_attr |= VBE_MODE_ATTRIBUTE_TTY_BIOS_SUPPORT;
94     else if (framebuffer)
95         mode_attr |= VBE_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER_MODE;
96     SET_FARVAR(seg, info->mode_attributes, mode_attr);
97     SET_FARVAR(seg, info->winA_attributes,
98                VBE_WINDOW_ATTRIBUTE_RELOCATABLE |
99                VBE_WINDOW_ATTRIBUTE_READABLE |
100                VBE_WINDOW_ATTRIBUTE_WRITEABLE);
101     SET_FARVAR(seg, info->winB_attributes, 0);
102     SET_FARVAR(seg, info->win_granularity, GET_GLOBAL(VBE_win_granularity));
103     SET_FARVAR(seg, info->win_size, 64); /* Bank size 64K */
104     SET_FARVAR(seg, info->winA_seg, GET_GLOBAL(vmode_g->sstart));
105     SET_FARVAR(seg, info->winB_seg, 0x0);
106     extern void entry_104f05(void);
107     SET_FARVAR(seg, info->win_func_ptr
108                , SEGOFF(get_global_seg(), (u32)entry_104f05));
109     int width = GET_GLOBAL(vmode_g->width);
110     int height = GET_GLOBAL(vmode_g->height);
111     int linesize = DIV_ROUND_UP(width * vga_bpp(vmode_g), 8);
112     SET_FARVAR(seg, info->bytes_per_scanline, linesize);
113     SET_FARVAR(seg, info->xres, width);
114     SET_FARVAR(seg, info->yres, height);
115     SET_FARVAR(seg, info->xcharsize, GET_GLOBAL(vmode_g->cwidth));
116     SET_FARVAR(seg, info->ycharsize, GET_GLOBAL(vmode_g->cheight));
117     int planes = (depth == 4) ? 4 : 1;
118     SET_FARVAR(seg, info->planes, planes);
119     SET_FARVAR(seg, info->bits_per_pixel, depth);
120     SET_FARVAR(seg, info->banks, 1);
121     SET_FARVAR(seg, info->mem_model, GET_GLOBAL(vmode_g->memmodel));
122     SET_FARVAR(seg, info->bank_size, 0);
123     u32 pages = GET_GLOBAL(VBE_total_memory) / (height * linesize);
124     SET_FARVAR(seg, info->pages, (pages / planes) - 1);
125     SET_FARVAR(seg, info->reserved0, 1);
126
127     u8 r_size, r_pos, g_size, g_pos, b_size, b_pos, a_size, a_pos;
128
129     switch (depth) {
130     case 15: r_size = 5; r_pos = 10; g_size = 5; g_pos = 5;
131              b_size = 5; b_pos = 0; a_size = 1; a_pos = 15; break;
132     case 16: r_size = 5; r_pos = 11; g_size = 6; g_pos = 5;
133              b_size = 5; b_pos = 0; a_size = 0; a_pos = 0; break;
134     case 24: r_size = 8; r_pos = 16; g_size = 8; g_pos = 8;
135              b_size = 8; b_pos = 0; a_size = 0; a_pos = 0; break;
136     case 32: r_size = 8; r_pos = 16; g_size = 8; g_pos = 8;
137              b_size = 8; b_pos = 0; a_size = 8; a_pos = 24; break;
138     default: r_size = 0; r_pos = 0; g_size = 0; g_pos = 0;
139              b_size = 0; b_pos = 0; a_size = 0; a_pos = 0; break;
140     }
141
142     SET_FARVAR(seg, info->red_size, r_size);
143     SET_FARVAR(seg, info->red_pos, r_pos);
144     SET_FARVAR(seg, info->green_size, g_size);
145     SET_FARVAR(seg, info->green_pos, g_pos);
146     SET_FARVAR(seg, info->blue_size, b_size);
147     SET_FARVAR(seg, info->blue_pos, b_pos);
148     SET_FARVAR(seg, info->alpha_size, a_size);
149     SET_FARVAR(seg, info->alpha_pos, a_pos);
150
151     if (depth == 32)
152         SET_FARVAR(seg, info->directcolor_info,
153                    VBE_DIRECTCOLOR_RESERVED_BITS_AVAILABLE);
154     else
155         SET_FARVAR(seg, info->directcolor_info, 0);
156
157     if (depth > 4)
158         SET_FARVAR(seg, info->phys_base, GET_GLOBAL(VBE_framebuffer));
159     else
160         SET_FARVAR(seg, info->phys_base, 0);
161
162     SET_FARVAR(seg, info->reserved1, 0);
163     SET_FARVAR(seg, info->reserved2, 0);
164     SET_FARVAR(seg, info->linear_bytes_per_scanline, linesize);
165     SET_FARVAR(seg, info->bank_pages, 0);
166     SET_FARVAR(seg, info->linear_pages, 0);
167     SET_FARVAR(seg, info->linear_red_size, r_size);
168     SET_FARVAR(seg, info->linear_red_pos, r_pos);
169     SET_FARVAR(seg, info->linear_green_size, g_size);
170     SET_FARVAR(seg, info->linear_green_pos, g_pos);
171     SET_FARVAR(seg, info->linear_blue_size, b_size);
172     SET_FARVAR(seg, info->linear_blue_pos, b_pos);
173     SET_FARVAR(seg, info->linear_alpha_size, a_size);
174     SET_FARVAR(seg, info->linear_alpha_pos, a_pos);
175     SET_FARVAR(seg, info->pixclock_max, 0);
176
177     regs->ax = 0x004f;
178 }
179
180 static void
181 vbe_104f02(struct bregs *regs)
182 {
183     dprintf(1, "VBE mode set: %x\n", regs->bx);
184
185     int mode = regs->bx & ~MF_VBEFLAGS;
186     int flags = regs->bx & MF_VBEFLAGS;
187     int ret = vga_set_mode(mode, flags);
188
189     regs->ah = ret;
190     regs->al = 0x4f;
191 }
192
193 static void
194 vbe_104f03(struct bregs *regs)
195 {
196     regs->bx = GET_BDA(vbe_mode);
197     dprintf(1, "VBE current mode=%x\n", regs->bx);
198     regs->ax = 0x004f;
199 }
200
201 static void
202 vbe_104f04(struct bregs *regs)
203 {
204     debug_stub(regs);
205     regs->ax = 0x0100;
206 }
207
208 void VISIBLE16
209 vbe_104f05(struct bregs *regs)
210 {
211     if (regs->bh > 1 || regs->bl > 1)
212         goto fail;
213     if (GET_BDA(vbe_mode) & MF_LINEARFB) {
214         regs->ah = VBE_RETURN_STATUS_INVALID;
215         return;
216     }
217     struct vgamode_s *vmode_g = get_current_mode();
218     if (! vmode_g)
219         goto fail;
220     if (regs->bh) {
221         int ret = vgahw_get_window(vmode_g, regs->bl);
222         if (ret < 0)
223             goto fail;
224         regs->dx = ret;
225         regs->ax = 0x004f;
226         return;
227     }
228     int ret = vgahw_set_window(vmode_g, regs->bl, regs->dx);
229     if (ret)
230         goto fail;
231     regs->ax = 0x004f;
232     return;
233 fail:
234     regs->ax = 0x0100;
235 }
236
237 static void
238 vbe_104f06(struct bregs *regs)
239 {
240     if (regs->bl > 0x02)
241         goto fail;
242     struct vgamode_s *vmode_g = get_current_mode();
243     if (! vmode_g)
244         goto fail;
245     int bpp = vga_bpp(vmode_g);
246
247     if (regs->bl == 0x00) {
248         int ret = vgahw_set_linelength(vmode_g, DIV_ROUND_UP(regs->cx * bpp, 8));
249         if (ret)
250             goto fail;
251     } else if (regs->bl == 0x02) {
252         int ret = vgahw_set_linelength(vmode_g, regs->cx);
253         if (ret)
254             goto fail;
255     }
256     int linelength = vgahw_get_linelength(vmode_g);
257     if (linelength < 0)
258         goto fail;
259
260     regs->bx = linelength;
261     regs->cx = (linelength * 8) / bpp;
262     regs->dx = GET_GLOBAL(VBE_total_memory) / linelength;
263     regs->ax = 0x004f;
264     return;
265 fail:
266     regs->ax = 0x014f;
267 }
268
269 static void
270 vbe_104f07(struct bregs *regs)
271 {
272     struct vgamode_s *vmode_g = get_current_mode();
273     if (! vmode_g)
274         goto fail;
275     int bpp = vga_bpp(vmode_g);
276     int linelength = vgahw_get_linelength(vmode_g);
277     if (linelength < 0)
278         goto fail;
279
280     int ret;
281     switch (regs->bl) {
282     case 0x80:
283     case 0x00:
284         ret = vgahw_set_displaystart(
285             vmode_g, DIV_ROUND_UP(regs->cx * bpp, 8) + linelength * regs->dx);
286         if (ret)
287             goto fail;
288         break;
289     case 0x01:
290         ret = vgahw_get_displaystart(vmode_g);
291         if (ret < 0)
292             goto fail;
293         regs->dx = ret / linelength;
294         regs->cx = (ret % linelength) * 8 / bpp;
295         break;
296     default:
297         goto fail;
298     }
299     regs->ax = 0x004f;
300     return;
301 fail:
302     regs->ax = 0x014f;
303 }
304
305 static void
306 vbe_104f08(struct bregs *regs)
307 {
308     debug_stub(regs);
309     regs->ax = 0x0100;
310 }
311
312 static void
313 vbe_104f0a(struct bregs *regs)
314 {
315     debug_stub(regs);
316     regs->ax = 0x0100;
317 }
318
319 static void
320 vbe_104f10(struct bregs *regs)
321 {
322     switch (regs->bl) {
323     case 0x00:
324         regs->bx = 0x0f30;
325         break;
326     case 0x01:
327         SET_BDA(vbe_flag, regs->bh);
328         break;
329     case 0x02:
330         regs->bh = GET_BDA(vbe_flag);
331         break;
332     default:
333         regs->ax = 0x014f;
334         return;
335     }
336     regs->ax = 0x004f;
337 }
338
339 static void
340 vbe_104fXX(struct bregs *regs)
341 {
342     debug_stub(regs);
343     regs->ax = 0x0100;
344 }
345
346 void
347 handle_104f(struct bregs *regs)
348 {
349     if (!CONFIG_VGA_VBE) {
350         vbe_104fXX(regs);
351         return;
352     }
353
354     switch (regs->al) {
355     case 0x00: vbe_104f00(regs); break;
356     case 0x01: vbe_104f01(regs); break;
357     case 0x02: vbe_104f02(regs); break;
358     case 0x03: vbe_104f03(regs); break;
359     case 0x04: vbe_104f04(regs); break;
360     case 0x05: vbe_104f05(regs); break;
361     case 0x06: vbe_104f06(regs); break;
362     case 0x07: vbe_104f07(regs); break;
363     case 0x08: vbe_104f08(regs); break;
364     case 0x0a: vbe_104f0a(regs); break;
365     case 0x10: vbe_104f10(regs); break;
366     default:   vbe_104fXX(regs); break;
367     }
368 }