vgabios: Move vbe code from vgabios.c to new file vbe.c.
[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 "bochsvga.h" // bochsvga_set_mode
15
16 static void
17 vbe_104f00(struct bregs *regs)
18 {
19     u16 seg = regs->es;
20     struct vbe_info *info = (void*)(regs->di+0);
21
22     if (GET_FARVAR(seg, info->signature) == VBE2_SIGNATURE) {
23         dprintf(4, "Get VBE Controller: VBE2 Signature found\n");
24     } else if (GET_FARVAR(seg, info->signature) == VESA_SIGNATURE) {
25         dprintf(4, "Get VBE Controller: VESA Signature found\n");
26     } else {
27         dprintf(4, "Get VBE Controller: Invalid Signature\n");
28     }
29
30     memset_far(seg, info, 0, sizeof(*info));
31
32     SET_FARVAR(seg, info->signature, VESA_SIGNATURE);
33
34     SET_FARVAR(seg, info->version, 0x0200);
35
36     SET_FARVAR(seg, info->oem_string,
37             SEGOFF(get_global_seg(), (u32)VBE_OEM_STRING));
38     SET_FARVAR(seg, info->capabilities, 0x1); /* 8BIT DAC */
39
40     /* We generate our mode list in the reserved field of the info block */
41     SET_FARVAR(seg, info->video_mode, SEGOFF(seg, regs->di + 34));
42
43     /* Total memory (in 64 blocks) */
44     SET_FARVAR(seg, info->total_memory, bochsvga_total_mem());
45
46     SET_FARVAR(seg, info->oem_vendor_string,
47             SEGOFF(get_global_seg(), (u32)VBE_VENDOR_STRING));
48     SET_FARVAR(seg, info->oem_product_string,
49             SEGOFF(get_global_seg(), (u32)VBE_PRODUCT_STRING));
50     SET_FARVAR(seg, info->oem_revision_string,
51             SEGOFF(get_global_seg(), (u32)VBE_REVISION_STRING));
52
53     /* Fill list of modes */
54     bochsvga_list_modes(seg, regs->di + 32);
55
56     regs->al = regs->ah; /* 0x4F, Function supported */
57     regs->ah = 0x0; /* 0x0, Function call successful */
58 }
59
60 static void
61 vbe_104f01(struct bregs *regs)
62 {
63     u16 seg = regs->es;
64     struct vbe_mode_info *info = (void*)(regs->di+0);
65     u16 mode = regs->cx;
66     struct vbe_modeinfo modeinfo;
67     int rc;
68
69     dprintf(1, "VBE mode info request: %x\n", mode);
70
71     rc = bochsvga_mode_info(mode, &modeinfo);
72     if (rc) {
73         dprintf(1, "VBE mode %x not found\n", mode);
74         regs->ax = 0x100;
75         return;
76     }
77
78     u16 mode_attr = VBE_MODE_ATTRIBUTE_SUPPORTED |
79                     VBE_MODE_ATTRIBUTE_EXTENDED_INFORMATION_AVAILABLE |
80                     VBE_MODE_ATTRIBUTE_COLOR_MODE |
81                     VBE_MODE_ATTRIBUTE_GRAPHICS_MODE;
82     if (modeinfo.depth == 4)
83         mode_attr |= VBE_MODE_ATTRIBUTE_TTY_BIOS_SUPPORT;
84     else
85         mode_attr |= VBE_MODE_ATTRIBUTE_LINEAR_FRAME_BUFFER_MODE;
86     SET_FARVAR(seg, info->mode_attributes, mode_attr);
87     SET_FARVAR(seg, info->winA_attributes,
88                VBE_WINDOW_ATTRIBUTE_RELOCATABLE |
89                VBE_WINDOW_ATTRIBUTE_READABLE |
90                VBE_WINDOW_ATTRIBUTE_WRITEABLE);
91     SET_FARVAR(seg, info->winB_attributes, 0);
92     SET_FARVAR(seg, info->win_granularity, 64); /* Bank size 64K */
93     SET_FARVAR(seg, info->win_size, 64); /* Bank size 64K */
94     SET_FARVAR(seg, info->winA_seg, 0xA000);
95     SET_FARVAR(seg, info->winB_seg, 0x0);
96     SET_FARVAR(seg, info->win_func_ptr.segoff, 0x0);
97     SET_FARVAR(seg, info->bytes_per_scanline, modeinfo.linesize);
98     SET_FARVAR(seg, info->xres, modeinfo.width);
99     SET_FARVAR(seg, info->yres, modeinfo.height);
100     SET_FARVAR(seg, info->xcharsize, 8);
101     SET_FARVAR(seg, info->ycharsize, 16);
102     if (modeinfo.depth == 4)
103         SET_FARVAR(seg, info->planes, 4);
104     else
105         SET_FARVAR(seg, info->planes, 1);
106     SET_FARVAR(seg, info->bits_per_pixel, modeinfo.depth);
107     SET_FARVAR(seg, info->banks,
108             (modeinfo.linesize * modeinfo.height + 65535) / 65536);
109     if (modeinfo.depth == 4)
110         SET_FARVAR(seg, info->mem_model, VBE_MEMORYMODEL_PLANAR);
111     else if (modeinfo.depth == 8)
112         SET_FARVAR(seg, info->mem_model, VBE_MEMORYMODEL_PACKED_PIXEL);
113     else
114         SET_FARVAR(seg, info->mem_model, VBE_MEMORYMODEL_DIRECT_COLOR);
115     SET_FARVAR(seg, info->bank_size, 0);
116     u32 pages = modeinfo.vram_size / (modeinfo.height * modeinfo.linesize);
117     if (modeinfo.depth == 4)
118         SET_FARVAR(seg, info->pages, (pages / 4) - 1);
119     else
120         SET_FARVAR(seg, info->pages, pages - 1);
121     SET_FARVAR(seg, info->reserved0, 1);
122
123     u8 r_size, r_pos, g_size, g_pos, b_size, b_pos, a_size, a_pos;
124
125     switch (modeinfo.depth) {
126     case 15: r_size = 5; r_pos = 10; g_size = 5; g_pos = 5;
127              b_size = 5; b_pos = 0; a_size = 1; a_pos = 15; break;
128     case 16: r_size = 5; r_pos = 11; g_size = 6; g_pos = 5;
129              b_size = 5; b_pos = 0; a_size = 0; a_pos = 0; break;
130     case 24: r_size = 8; r_pos = 16; g_size = 8; g_pos = 8;
131              b_size = 8; b_pos = 0; a_size = 0; a_pos = 0; break;
132     case 32: r_size = 8; r_pos = 16; g_size = 8; g_pos = 8;
133              b_size = 8; b_pos = 0; a_size = 8; a_pos = 24; break;
134     default: r_size = 0; r_pos = 0; g_size = 0; g_pos = 0;
135              b_size = 0; b_pos = 0; a_size = 0; a_pos = 0; break;
136     }
137
138     SET_FARVAR(seg, info->red_size, r_size);
139     SET_FARVAR(seg, info->red_pos, r_pos);
140     SET_FARVAR(seg, info->green_size, g_size);
141     SET_FARVAR(seg, info->green_pos, g_pos);
142     SET_FARVAR(seg, info->blue_size, b_size);
143     SET_FARVAR(seg, info->blue_pos, b_pos);
144     SET_FARVAR(seg, info->alpha_size, a_size);
145     SET_FARVAR(seg, info->alpha_pos, a_pos);
146
147     if (modeinfo.depth == 32)
148         SET_FARVAR(seg, info->directcolor_info,
149                    VBE_DIRECTCOLOR_RESERVED_BITS_AVAILABLE);
150     else
151         SET_FARVAR(seg, info->directcolor_info, 0);
152
153     if (modeinfo.depth > 4)
154         SET_FARVAR(seg, info->phys_base, modeinfo.phys_base);
155     else
156         SET_FARVAR(seg, info->phys_base, 0);
157
158     SET_FARVAR(seg, info->reserved1, 0);
159     SET_FARVAR(seg, info->reserved2, 0);
160     SET_FARVAR(seg, info->linear_bytes_per_scanline, modeinfo.linesize);
161     SET_FARVAR(seg, info->bank_pages, 0);
162     SET_FARVAR(seg, info->linear_pages, 0);
163     SET_FARVAR(seg, info->linear_red_size, r_size);
164     SET_FARVAR(seg, info->linear_red_pos, r_pos);
165     SET_FARVAR(seg, info->linear_green_size, g_size);
166     SET_FARVAR(seg, info->linear_green_pos, g_pos);
167     SET_FARVAR(seg, info->linear_blue_size, b_size);
168     SET_FARVAR(seg, info->linear_blue_pos, b_pos);
169     SET_FARVAR(seg, info->linear_alpha_size, a_size);
170     SET_FARVAR(seg, info->linear_alpha_pos, a_pos);
171     SET_FARVAR(seg, info->pixclock_max, 0);
172
173     regs->al = regs->ah; /* 0x4F, Function supported */
174     regs->ah = 0x0; /* 0x0, Function call successful */
175 }
176
177 static void
178 vbe_104f02(struct bregs *regs)
179 {
180     //u16 seg = regs->es;
181     //struct vbe_crtc_info *crtc_info = (void*)(regs->di+0);
182     u16 mode = regs->bx;
183     struct vbe_modeinfo modeinfo;
184     int rc;
185
186     dprintf(1, "VBE mode set: %x\n", mode);
187
188     if (mode < 0x100) { /* VGA */
189         dprintf(1, "set VGA mode %x\n", mode);
190
191         bochsvga_hires_enable(0);
192         vga_set_mode(mode, 0);
193     } else { /* VBE */
194         rc = bochsvga_mode_info(mode & 0x1ff, &modeinfo);
195         if (rc) {
196             dprintf(1, "VBE mode %x not found\n", mode & 0x1ff);
197             regs->ax = 0x100;
198             return;
199         }
200         bochsvga_hires_enable(1);
201         bochsvga_set_mode(mode & 0x1ff, &modeinfo);
202
203         if (mode & 0x4000) {
204             /* Linear frame buffer */
205             /* XXX: ??? */
206         }
207         if (!(mode & 0x8000)) {
208             bochsvga_clear_scr();
209         }
210     }
211
212     regs->al = regs->ah; /* 0x4F, Function supported */
213     regs->ah = 0x0; /* 0x0, Function call successful */
214 }
215
216 static void
217 vbe_104f03(struct bregs *regs)
218 {
219     if (!bochsvga_hires_enabled()) {
220         regs->bx = GET_BDA(video_mode);
221     } else {
222         regs->bx = bochsvga_curr_mode();
223     }
224
225     dprintf(1, "VBE current mode=%x\n", regs->bx);
226
227     regs->al = regs->ah; /* 0x4F, Function supported */
228     regs->ah = 0x0; /* 0x0, Function call successful */
229 }
230
231 static void
232 vbe_104f04(struct bregs *regs)
233 {
234     debug_stub(regs);
235     regs->ax = 0x0100;
236 }
237
238 static void
239 vbe_104f05(struct bregs *regs)
240 {
241     debug_stub(regs);
242     regs->ax = 0x0100;
243 }
244
245 static void
246 vbe_104f06(struct bregs *regs)
247 {
248     debug_stub(regs);
249     regs->ax = 0x0100;
250 }
251
252 static void
253 vbe_104f07(struct bregs *regs)
254 {
255     debug_stub(regs);
256     regs->ax = 0x0100;
257 }
258
259 static void
260 vbe_104f08(struct bregs *regs)
261 {
262     debug_stub(regs);
263     regs->ax = 0x0100;
264 }
265
266 static void
267 vbe_104f0a(struct bregs *regs)
268 {
269     debug_stub(regs);
270     regs->ax = 0x0100;
271 }
272
273 static void
274 vbe_104fXX(struct bregs *regs)
275 {
276     debug_stub(regs);
277     regs->ax = 0x0100;
278 }
279
280 void
281 handle_104f(struct bregs *regs)
282 {
283     if (!bochsvga_enabled()) {
284         vbe_104fXX(regs);
285         return;
286     }
287
288     switch (regs->al) {
289     case 0x00: vbe_104f00(regs); break;
290     case 0x01: vbe_104f01(regs); break;
291     case 0x02: vbe_104f02(regs); break;
292     case 0x03: vbe_104f03(regs); break;
293     case 0x04: vbe_104f04(regs); break;
294     case 0x05: vbe_104f05(regs); break;
295     case 0x06: vbe_104f06(regs); break;
296     case 0x07: vbe_104f07(regs); break;
297     case 0x08: vbe_104f08(regs); break;
298     case 0x0a: vbe_104f0a(regs); break;
299     default:   vbe_104fXX(regs); break;
300     }
301 }