1
2
3
4
5
6
7
8#include <common.h>
9#include <bios_emul.h>
10#include <irq_func.h>
11#include <vbe.h>
12#include <linux/linkage.h>
13#include <asm/cache.h>
14#include <asm/processor.h>
15#include <asm/i8259.h>
16#include <asm/io.h>
17#include <asm/post.h>
18#include "bios.h"
19
20
21static int (*int_handler[256])(void);
22
23
24X86EMU_sysEnv _X86EMU_env;
25
26asmlinkage void (*realmode_call)(u32 addr, u32 eax, u32 ebx, u32 ecx, u32 edx,
27 u32 esi, u32 edi);
28
29asmlinkage void (*realmode_interrupt)(u32 intno, u32 eax, u32 ebx, u32 ecx,
30 u32 edx, u32 esi, u32 edi);
31
32static void setup_realmode_code(void)
33{
34 memcpy((void *)REALMODE_BASE, &asm_realmode_code,
35 asm_realmode_code_size);
36
37
38 realmode_call = PTR_TO_REAL_MODE(asm_realmode_call);
39 realmode_interrupt = PTR_TO_REAL_MODE(__realmode_interrupt);
40
41 debug("Real mode stub @%x: %d bytes\n", REALMODE_BASE,
42 asm_realmode_code_size);
43}
44
45static void setup_rombios(void)
46{
47 const char date[] = "06/11/99";
48 memcpy((void *)0xffff5, &date, 8);
49
50 const char ident[] = "PCI_ISA";
51 memcpy((void *)0xfffd9, &ident, 7);
52
53
54 writeb(0xfc, 0xffffe);
55}
56
57static int int_exception_handler(void)
58{
59
60 struct eregs reg_info = {
61 .eax = M.x86.R_EAX,
62 .ecx = M.x86.R_ECX,
63 .edx = M.x86.R_EDX,
64 .ebx = M.x86.R_EBX,
65 .esp = M.x86.R_ESP,
66 .ebp = M.x86.R_EBP,
67 .esi = M.x86.R_ESI,
68 .edi = M.x86.R_EDI,
69 .vector = M.x86.intno,
70 .error_code = 0,
71 .eip = M.x86.R_EIP,
72 .cs = M.x86.R_CS,
73 .eflags = M.x86.R_EFLG
74 };
75 struct eregs *regs = ®_info;
76
77 debug("Oops, exception %d while executing option rom\n", regs->vector);
78 cpu_hlt();
79
80 return 0;
81}
82
83static int int_unknown_handler(void)
84{
85 debug("Unsupported software interrupt #0x%x eax 0x%x\n",
86 M.x86.intno, M.x86.R_EAX);
87
88 return -1;
89}
90
91
92void bios_set_interrupt_handler(int intnum, int (*int_func)(void))
93{
94 int_handler[intnum] = int_func;
95}
96
97static void setup_interrupt_handlers(void)
98{
99 int i;
100
101
102
103
104
105 for (i = 0; i < 0x10; i++)
106 int_handler[i] = &int_exception_handler;
107
108
109 for (i = 0x10; i < 0x100; i++) {
110
111 if (int_handler[i])
112 continue;
113
114
115
116
117
118
119 switch (i) {
120 case 0x10:
121 int_handler[0x10] = &int10_handler;
122 break;
123 case 0x12:
124 int_handler[0x12] = &int12_handler;
125 break;
126 case 0x16:
127 int_handler[0x16] = &int16_handler;
128 break;
129 case 0x1a:
130 int_handler[0x1a] = &int1a_handler;
131 break;
132 default:
133 int_handler[i] = &int_unknown_handler;
134 break;
135 }
136 }
137}
138
139static void write_idt_stub(void *target, u8 intnum)
140{
141 unsigned char *codeptr;
142
143 codeptr = (unsigned char *)target;
144 memcpy(codeptr, &__idt_handler, __idt_handler_size);
145 codeptr[3] = intnum;
146}
147
148static void setup_realmode_idt(void)
149{
150 struct realmode_idt *idts = NULL;
151 int i;
152
153
154
155
156
157 for (i = 0; i < 256; i++) {
158 idts[i].cs = 0;
159 idts[i].offset = 0x1000 + (i * __idt_handler_size);
160 write_idt_stub((void *)((ulong)idts[i].offset), i);
161 }
162
163
164
165
166
167
168
169 write_idt_stub((void *)0xff065, 0x42);
170
171 write_idt_stub((void *)0xff84d, 0x11);
172
173 write_idt_stub((void *)0xff841, 0x12);
174
175 write_idt_stub((void *)0xfec59, 0x13);
176
177 write_idt_stub((void *)0xfe739, 0x14);
178
179 write_idt_stub((void *)0xff859, 0x15);
180
181 write_idt_stub((void *)0xfe82e, 0x16);
182
183 write_idt_stub((void *)0xfefd2, 0x17);
184
185 write_idt_stub((void *)0xffe6e, 0x1a);
186}
187
188#ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
189static u8 vbe_get_mode_info(struct vbe_mode_info *mi)
190{
191 u16 buffer_seg;
192 u16 buffer_adr;
193 char *buffer;
194
195 debug("VBE: Getting information about VESA mode %04x\n",
196 mi->video_mode);
197 buffer = PTR_TO_REAL_MODE(asm_realmode_buffer);
198 buffer_seg = (((unsigned long)buffer) >> 4) & 0xff00;
199 buffer_adr = ((unsigned long)buffer) & 0xffff;
200
201 realmode_interrupt(0x10, VESA_GET_MODE_INFO, 0x0000, mi->video_mode,
202 0x0000, buffer_seg, buffer_adr);
203 memcpy(mi->mode_info_block, buffer, sizeof(struct vbe_mode_info));
204 mi->valid = true;
205
206 return 0;
207}
208
209static u8 vbe_set_mode(struct vbe_mode_info *mi)
210{
211 int video_mode = mi->video_mode;
212
213 debug("VBE: Setting VESA mode %#04x\n", video_mode);
214
215 video_mode |= (1 << 14);
216
217 video_mode |= (1 << 15);
218 realmode_interrupt(0x10, VESA_SET_MODE, video_mode,
219 0x0000, 0x0000, 0x0000, 0x0000);
220
221 return 0;
222}
223
224static void vbe_set_graphics(int vesa_mode, struct vbe_mode_info *mode_info)
225{
226 unsigned char *framebuffer;
227
228 mode_info->video_mode = (1 << 14) | vesa_mode;
229 vbe_get_mode_info(mode_info);
230
231 framebuffer = (unsigned char *)(ulong)mode_info->vesa.phys_base_ptr;
232 debug("VBE: resolution: %dx%d@%d\n",
233 le16_to_cpu(mode_info->vesa.x_resolution),
234 le16_to_cpu(mode_info->vesa.y_resolution),
235 mode_info->vesa.bits_per_pixel);
236 debug("VBE: framebuffer: %p\n", framebuffer);
237 if (!framebuffer) {
238 debug("VBE: Mode does not support linear framebuffer\n");
239 return;
240 }
241
242 mode_info->video_mode &= 0x3ff;
243 vbe_set_mode(mode_info);
244}
245#endif
246
247void bios_run_on_x86(struct udevice *dev, unsigned long addr, int vesa_mode,
248 struct vbe_mode_info *mode_info)
249{
250 pci_dev_t pcidev = dm_pci_get_bdf(dev);
251 u32 num_dev;
252
253 num_dev = PCI_BUS(pcidev) << 8 | PCI_DEV(pcidev) << 3 |
254 PCI_FUNC(pcidev);
255
256
257 interrupt_init();
258
259
260 setup_rombios();
261
262
263 setup_interrupt_handlers();
264
265
266 setup_realmode_idt();
267
268
269 setup_realmode_code();
270
271 debug("Calling Option ROM at %lx, pci device %#x...", addr, num_dev);
272
273
274 realmode_call(addr + 0x0003, num_dev, 0xffff, 0x0000, 0xffff, 0x0,
275 0x0);
276 debug("done\n");
277
278#ifdef CONFIG_FRAMEBUFFER_SET_VESA_MODE
279 if (vesa_mode != -1)
280 vbe_set_graphics(vesa_mode, mode_info);
281#endif
282}
283
284asmlinkage int interrupt_handler(u32 intnumber, u32 gsfs, u32 dses,
285 u32 edi, u32 esi, u32 ebp, u32 esp,
286 u32 ebx, u32 edx, u32 ecx, u32 eax,
287 u32 cs_ip, u16 stackflags)
288{
289 u32 ip;
290 u32 cs;
291 u32 flags;
292 int ret = 0;
293
294 ip = cs_ip & 0xffff;
295 cs = cs_ip >> 16;
296 flags = stackflags;
297
298#ifdef CONFIG_REALMODE_DEBUG
299 debug("oprom: INT# 0x%x\n", intnumber);
300 debug("oprom: eax: %08x ebx: %08x ecx: %08x edx: %08x\n",
301 eax, ebx, ecx, edx);
302 debug("oprom: ebp: %08x esp: %08x edi: %08x esi: %08x\n",
303 ebp, esp, edi, esi);
304 debug("oprom: ip: %04x cs: %04x flags: %08x\n",
305 ip, cs, flags);
306 debug("oprom: stackflags = %04x\n", stackflags);
307#endif
308
309
310
311
312
313 M.x86.R_EAX = eax;
314 M.x86.R_ECX = ecx;
315 M.x86.R_EDX = edx;
316 M.x86.R_EBX = ebx;
317 M.x86.R_ESP = esp;
318 M.x86.R_EBP = ebp;
319 M.x86.R_ESI = esi;
320 M.x86.R_EDI = edi;
321 M.x86.intno = intnumber;
322 M.x86.R_EIP = ip;
323 M.x86.R_CS = cs;
324 M.x86.R_EFLG = flags;
325
326
327 ret = int_handler[intnumber]();
328
329
330
331
332
333
334
335
336
337 *(volatile u32 *)&eax = M.x86.R_EAX;
338 *(volatile u32 *)&ecx = M.x86.R_ECX;
339 *(volatile u32 *)&edx = M.x86.R_EDX;
340 *(volatile u32 *)&ebx = M.x86.R_EBX;
341 *(volatile u32 *)&esi = M.x86.R_ESI;
342 *(volatile u32 *)&edi = M.x86.R_EDI;
343 flags = M.x86.R_EFLG;
344
345
346 if (ret) {
347 flags &= ~1;
348 } else {
349 debug("int%02x call returned error\n", intnumber);
350 flags |= 1;
351 }
352 *(volatile u16 *)&stackflags = flags;
353
354 return ret;
355}
356