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