blob: 5ab9e74c38c69d47e57b428d5868288110941617 [file] [log] [blame]
Jason Jina63ce952007-07-06 08:34:56 +08001/****************************************************************************
2*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +02003* BIOS emulator and interface
4* to Realmode X86 Emulator Library
Jason Jina63ce952007-07-06 08:34:56 +08005*
6* Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
7* Jason Jin <Jason.jin@freescale.com>
8*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +02009* Copyright (C) 1996-1999 SciTech Software, Inc.
Jason Jina63ce952007-07-06 08:34:56 +080010*
11* ========================================================================
12*
13* Permission to use, copy, modify, distribute, and sell this software and
14* its documentation for any purpose is hereby granted without fee,
15* provided that the above copyright notice appear in all copies and that
16* both that copyright notice and this permission notice appear in
17* supporting documentation, and that the name of the authors not be used
18* in advertising or publicity pertaining to distribution of the software
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020019* without specific, written prior permission. The authors makes no
Jason Jina63ce952007-07-06 08:34:56 +080020* representations about the suitability of this software for any purpose.
21* It is provided "as is" without express or implied warranty.
22*
23* THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
24* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
25* EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
26* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
27* USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
28* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
29* PERFORMANCE OF THIS SOFTWARE.
30*
31* ========================================================================
32*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020033* Language: ANSI C
34* Environment: Any
35* Developer: Kendall Bennett
Jason Jina63ce952007-07-06 08:34:56 +080036*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020037* Description: Module implementing the system specific functions. This
38* module is always compiled and linked in the OS depedent
39* libraries, and never in a binary portable driver.
Jason Jina63ce952007-07-06 08:34:56 +080040*
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020041* Jason ported this file to u-boot to run the ATI video card BIOS
42* in u-boot. Made all the video memory be emulated during the
43* BIOS runing process which may affect the VGA function but the
44* frambuffer function can work after run the BIOS.
Jason Jina63ce952007-07-06 08:34:56 +080045*
46****************************************************************************/
47
Jason Jina63ce952007-07-06 08:34:56 +080048#include <malloc.h>
49
Jason Jin7ed5cd92007-08-08 08:33:11 +080050#if defined(CONFIG_BIOSEMU)
51
Michal Simek952d8612007-08-15 21:15:05 +020052#include "biosemui.h"
53
Jason Jina63ce952007-07-06 08:34:56 +080054BE_sysEnv _BE_env = {{0}};
55static X86EMU_memFuncs _BE_mem __attribute__((section(".got2"))) = {
56 BE_rdb,
57 BE_rdw,
58 BE_rdl,
59 BE_wrb,
60 BE_wrw,
61 BE_wrl,
62 };
63
64static X86EMU_pioFuncs _BE_pio __attribute__((section(".got2"))) = {
65 BE_inb,
66 BE_inw,
67 BE_inl,
68 BE_outb,
69 BE_outw,
70 BE_outl,
71 };
72
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020073#define OFF(addr) (u16)(((addr) >> 0) & 0xffff)
74#define SEG(addr) (u16)(((addr) >> 4) & 0xf000)
Jason Jina63ce952007-07-06 08:34:56 +080075
76/****************************************************************************
77PARAMETERS:
78debugFlags - Flags to enable debugging options (debug builds only)
Wolfgang Denk96bb2e02007-08-06 02:17:36 +020079memSize - Amount of memory to allocate for real mode machine
80info - Pointer to default VGA device information
Jason Jina63ce952007-07-06 08:34:56 +080081
82REMARKS:
83This functions initialises the BElib, and uses the passed in
84BIOS image as the BIOS that is used and emulated at 0xC0000.
85****************************************************************************/
86int X86API BE_init(u32 debugFlags, int memSize, BE_VGAInfo * info, int shared)
87{
88#if !defined(__DRIVER__) && !defined(__KERNEL__)
89
90 PM_init();
91#endif
92 memset(&M, 0, sizeof(M));
93 if (memSize < 20480){
94 printf("Emulator requires at least 20Kb of memory!\n");
95 return 0;
96 }
97
98 M.mem_base = (unsigned long)malloc(memSize);
99
100 if (M.mem_base == NULL){
101 printf("Biosemu:Out of memory!");
102 return 0;
103 }
104 M.mem_size = memSize;
105
106 _BE_env.emulateVGA = 0;
107 _BE_env.busmem_base = (unsigned long)malloc(128 * 1024);
108 if (_BE_env.busmem_base == NULL){
109 printf("Biosemu:Out of memory!");
110 return 0;
111 }
112 M.x86.debug = debugFlags;
113 _BE_bios_init((u32*)info->LowMem);
114 X86EMU_setupMemFuncs(&_BE_mem);
115 X86EMU_setupPioFuncs(&_BE_pio);
116 BE_setVGA(info);
117 return 1;
118}
119
120/****************************************************************************
121PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200122info - Pointer to VGA device information to make current
Jason Jina63ce952007-07-06 08:34:56 +0800123
124REMARKS:
125This function sets the VGA BIOS functions in the emulator to point to the
126specific VGA BIOS in use. This includes swapping the BIOS interrupt
127vectors, BIOS image and BIOS data area to the new BIOS. This allows the
128real mode BIOS to be swapped without resetting the entire emulator.
129****************************************************************************/
130void X86API BE_setVGA(BE_VGAInfo * info)
131{
132
133#ifdef __KERNEL__
134 _BE_env.vgaInfo.function = info->function;
135 _BE_env.vgaInfo.device = info->device;
136 _BE_env.vgaInfo.bus = info->bus;
137 _BE_env.vgaInfo.pcidev = info->pcidev;
138#else
139 _BE_env.vgaInfo.pciInfo = info->pciInfo;
140#endif
141 _BE_env.vgaInfo.BIOSImage = info->BIOSImage;
142 if (info->BIOSImage) {
143 _BE_env.biosmem_base = (ulong) info->BIOSImage;
144 _BE_env.biosmem_limit = 0xC0000 + info->BIOSImageLen - 1;
145 } else {
146 _BE_env.biosmem_base = _BE_env.busmem_base + 0x20000;
147 _BE_env.biosmem_limit = 0xC7FFF;
148 }
149 if (*((u32 *) info->LowMem) == 0)
150 _BE_bios_init((u32 *) info->LowMem);
151 memcpy((u8 *) M.mem_base, info->LowMem, sizeof(info->LowMem));
152}
153
154/****************************************************************************
155PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200156info - Pointer to VGA device information to retrieve current
Jason Jina63ce952007-07-06 08:34:56 +0800157
158REMARKS:
159This function returns the VGA BIOS functions currently active in the
160emulator, so they can be restored at a later date.
161****************************************************************************/
162void X86API BE_getVGA(BE_VGAInfo * info)
163{
164#ifdef __KERNEL__
165 info->function = _BE_env.vgaInfo.function;
166 info->device = _BE_env.vgaInfo.device;
167 info->bus = _BE_env.vgaInfo.bus;
168 info->pcidev = _BE_env.vgaInfo.pcidev;
169#else
170 info->pciInfo = _BE_env.vgaInfo.pciInfo;
171#endif
172 info->BIOSImage = _BE_env.vgaInfo.BIOSImage;
173 memcpy(info->LowMem, (u8 *) M.mem_base, sizeof(info->LowMem));
174}
175
176/****************************************************************************
177PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200178r_seg - Segment for pointer to convert
179r_off - Offset for pointer to convert
Jason Jina63ce952007-07-06 08:34:56 +0800180
181REMARKS:
182This function maps a real mode pointer in the emulator memory to a protected
183mode pointer that can be used to directly access the memory.
184
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200185NOTE: The memory is *always* in little endian format, son on non-x86
186 systems you will need to do endian translations to access this
187 memory.
Jason Jina63ce952007-07-06 08:34:56 +0800188****************************************************************************/
189void *X86API BE_mapRealPointer(uint r_seg, uint r_off)
190{
191 u32 addr = ((u32) r_seg << 4) + r_off;
192
193 if (addr >= 0xC0000 && addr <= _BE_env.biosmem_limit) {
194 return (void *)(_BE_env.biosmem_base + addr - 0xC0000);
195 } else if (addr >= 0xA0000 && addr <= 0xFFFFF) {
196 return (void *)(_BE_env.busmem_base + addr - 0xA0000);
197 }
198 return (void *)(M.mem_base + addr);
199}
200
201/****************************************************************************
202PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200203len - Return the length of the VESA buffer
204rseg - Place to store VESA buffer segment
205roff - Place to store VESA buffer offset
Jason Jina63ce952007-07-06 08:34:56 +0800206
207REMARKS:
208This function returns the address of the VESA transfer buffer in real
209_BE_piomode emulator memory. The VESA transfer buffer is always 1024 bytes long,
210and located at 15Kb into the start of the real mode memory (16Kb is where
211we put the real mode code we execute for issuing interrupts).
212
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200213NOTE: The memory is *always* in little endian format, son on non-x86
214 systems you will need to do endian translations to access this
215 memory.
Jason Jina63ce952007-07-06 08:34:56 +0800216****************************************************************************/
217void *X86API BE_getVESABuf(uint * len, uint * rseg, uint * roff)
218{
219 *len = 1024;
220 *rseg = SEG(0x03C00);
221 *roff = OFF(0x03C00);
222 return (void *)(M.mem_base + ((u32) * rseg << 4) + *roff);
223}
224
225/****************************************************************************
226REMARKS:
227Cleans up and exits the emulator.
228****************************************************************************/
229void X86API BE_exit(void)
230{
231 free(M.mem_base);
232 free(_BE_env.busmem_base);
233}
234
235/****************************************************************************
236PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200237seg - Segment of code to call
238off - Offset of code to call
239regs - Real mode registers to load
240sregs - Real mode segment registers to load
Jason Jina63ce952007-07-06 08:34:56 +0800241
242REMARKS:
243This functions calls a real mode far function at the specified address,
244and loads all the x86 registers from the passed in registers structure.
245On exit the registers returned from the call are returned in the same
246structures.
247****************************************************************************/
248void X86API BE_callRealMode(uint seg, uint off, RMREGS * regs, RMSREGS * sregs)
249{
250 M.x86.R_EAX = regs->e.eax;
251 M.x86.R_EBX = regs->e.ebx;
252 M.x86.R_ECX = regs->e.ecx;
253 M.x86.R_EDX = regs->e.edx;
254 M.x86.R_ESI = regs->e.esi;
255 M.x86.R_EDI = regs->e.edi;
256 M.x86.R_DS = sregs->ds;
257 M.x86.R_ES = sregs->es;
258 M.x86.R_FS = sregs->fs;
259 M.x86.R_GS = sregs->gs;
260
261 ((u8 *) M.mem_base)[0x4000] = 0x9A;
262 ((u8 *) M.mem_base)[0x4001] = (u8) off;
263 ((u8 *) M.mem_base)[0x4002] = (u8) (off >> 8);
264 ((u8 *) M.mem_base)[0x4003] = (u8) seg;
265 ((u8 *) M.mem_base)[0x4004] = (u8) (seg >> 8);
266 ((u8 *) M.mem_base)[0x4005] = 0xF1; /* Illegal op-code */
267 M.x86.R_CS = SEG(0x04000);
268 M.x86.R_IP = OFF(0x04000);
269
270 M.x86.R_SS = SEG(M.mem_size - 2);
271 M.x86.R_SP = OFF(M.mem_size - 2) + 2;
272
273 X86EMU_exec();
274
275 regs->e.cflag = M.x86.R_EFLG & F_CF;
276 regs->e.eax = M.x86.R_EAX;
277 regs->e.ebx = M.x86.R_EBX;
278 regs->e.ecx = M.x86.R_ECX;
279 regs->e.edx = M.x86.R_EDX;
280 regs->e.esi = M.x86.R_ESI;
281 regs->e.edi = M.x86.R_EDI;
282 sregs->ds = M.x86.R_DS;
283 sregs->es = M.x86.R_ES;
284 sregs->fs = M.x86.R_FS;
285 sregs->gs = M.x86.R_GS;
286}
287
288/****************************************************************************
289PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200290intno - Interrupt number to execute
291in - Real mode registers to load
292out - Place to store resulting real mode registers
Jason Jina63ce952007-07-06 08:34:56 +0800293
294REMARKS:
295This functions calls a real mode interrupt function at the specified address,
296and loads all the x86 registers from the passed in registers structure.
297On exit the registers returned from the call are returned in out stucture.
298****************************************************************************/
299int X86API BE_int86(int intno, RMREGS * in, RMREGS * out)
300{
301 M.x86.R_EAX = in->e.eax;
302 M.x86.R_EBX = in->e.ebx;
303 M.x86.R_ECX = in->e.ecx;
304 M.x86.R_EDX = in->e.edx;
305 M.x86.R_ESI = in->e.esi;
306 M.x86.R_EDI = in->e.edi;
307 ((u8 *) M.mem_base)[0x4000] = 0xCD;
308 ((u8 *) M.mem_base)[0x4001] = (u8) intno;
309 ((u8 *) M.mem_base)[0x4002] = 0xF1;
310 M.x86.R_CS = SEG(0x04000);
311 M.x86.R_IP = OFF(0x04000);
312
313 M.x86.R_SS = SEG(M.mem_size - 1);
314 M.x86.R_SP = OFF(M.mem_size - 1) - 1;
315
316 X86EMU_exec();
317 out->e.cflag = M.x86.R_EFLG & F_CF;
318 out->e.eax = M.x86.R_EAX;
319 out->e.ebx = M.x86.R_EBX;
320 out->e.ecx = M.x86.R_ECX;
321 out->e.edx = M.x86.R_EDX;
322 out->e.esi = M.x86.R_ESI;
323 out->e.edi = M.x86.R_EDI;
324 return out->x.ax;
325}
326
327/****************************************************************************
328PARAMETERS:
Wolfgang Denk96bb2e02007-08-06 02:17:36 +0200329intno - Interrupt number to execute
330in - Real mode registers to load
331out - Place to store resulting real mode registers
332sregs - Real mode segment registers to load
Jason Jina63ce952007-07-06 08:34:56 +0800333
334REMARKS:
335This functions calls a real mode interrupt function at the specified address,
336and loads all the x86 registers from the passed in registers structure.
337On exit the registers returned from the call are returned in out stucture.
338****************************************************************************/
339int X86API BE_int86x(int intno, RMREGS * in, RMREGS * out, RMSREGS * sregs)
340{
341 M.x86.R_EAX = in->e.eax;
342 M.x86.R_EBX = in->e.ebx;
343 M.x86.R_ECX = in->e.ecx;
344 M.x86.R_EDX = in->e.edx;
345 M.x86.R_ESI = in->e.esi;
346 M.x86.R_EDI = in->e.edi;
347 M.x86.R_DS = sregs->ds;
348 M.x86.R_ES = sregs->es;
349 M.x86.R_FS = sregs->fs;
350 M.x86.R_GS = sregs->gs;
351 ((u8 *) M.mem_base)[0x4000] = 0xCD;
352 ((u8 *) M.mem_base)[0x4001] = (u8) intno;
353 ((u8 *) M.mem_base)[0x4002] = 0xF1;
354 M.x86.R_CS = SEG(0x04000);
355 M.x86.R_IP = OFF(0x04000);
356
357 M.x86.R_SS = SEG(M.mem_size - 1);
358 M.x86.R_SP = OFF(M.mem_size - 1) - 1;
359
360 X86EMU_exec();
361 out->e.cflag = M.x86.R_EFLG & F_CF;
362 out->e.eax = M.x86.R_EAX;
363 out->e.ebx = M.x86.R_EBX;
364 out->e.ecx = M.x86.R_ECX;
365 out->e.edx = M.x86.R_EDX;
366 out->e.esi = M.x86.R_ESI;
367 out->e.edi = M.x86.R_EDI;
368 sregs->ds = M.x86.R_DS;
369 sregs->es = M.x86.R_ES;
370 sregs->fs = M.x86.R_FS;
371 sregs->gs = M.x86.R_GS;
372 return out->x.ax;
373}
Jason Jin7ed5cd92007-08-08 08:33:11 +0800374#endif