1 /****************************************************************************
3 * BIOS emulator and interface
4 * to Realmode X86 Emulator Library
6 * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
7 * Jason Jin <Jason.jin@freescale.com>
9 * Copyright (C) 1996-1999 SciTech Software, Inc.
11 * ========================================================================
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
19 * without specific, written prior permission. The authors makes no
20 * representations about the suitability of this software for any purpose.
21 * It is provided "as is" without express or implied warranty.
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.
31 * ========================================================================
35 * Developer: Kendall Bennett
37 * 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.
41 * 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.
46 ****************************************************************************/
51 BE_sysEnv _BE_env = {{0}};
52 static X86EMU_memFuncs _BE_mem __attribute__((section(".got2"))) = {
61 static X86EMU_pioFuncs _BE_pio __attribute__((section(".got2"))) = {
70 #define OFF(addr) (u16)(((addr) >> 0) & 0xffff)
71 #define SEG(addr) (u16)(((addr) >> 4) & 0xf000)
73 /****************************************************************************
75 debugFlags - Flags to enable debugging options (debug builds only)
76 memSize - Amount of memory to allocate for real mode machine
77 info - Pointer to default VGA device information
80 This functions initialises the BElib, and uses the passed in
81 BIOS image as the BIOS that is used and emulated at 0xC0000.
82 ****************************************************************************/
83 int X86API BE_init(u32 debugFlags, int memSize, BE_VGAInfo * info, int shared)
85 #if !defined(__DRIVER__) && !defined(__KERNEL__)
89 memset(&M, 0, sizeof(M));
91 printf("Emulator requires at least 20Kb of memory!\n");
95 M.mem_base = (unsigned long)malloc(memSize);
97 if (M.mem_base == NULL){
98 printf("Biosemu:Out of memory!");
101 M.mem_size = memSize;
103 _BE_env.emulateVGA = 0;
104 _BE_env.busmem_base = (unsigned long)malloc(128 * 1024);
105 if (_BE_env.busmem_base == NULL){
106 printf("Biosemu:Out of memory!");
109 M.x86.debug = debugFlags;
110 _BE_bios_init((u32*)info->LowMem);
111 X86EMU_setupMemFuncs(&_BE_mem);
112 X86EMU_setupPioFuncs(&_BE_pio);
117 /****************************************************************************
119 info - Pointer to VGA device information to make current
122 This function sets the VGA BIOS functions in the emulator to point to the
123 specific VGA BIOS in use. This includes swapping the BIOS interrupt
124 vectors, BIOS image and BIOS data area to the new BIOS. This allows the
125 real mode BIOS to be swapped without resetting the entire emulator.
126 ****************************************************************************/
127 void X86API BE_setVGA(BE_VGAInfo * info)
131 _BE_env.vgaInfo.function = info->function;
132 _BE_env.vgaInfo.device = info->device;
133 _BE_env.vgaInfo.bus = info->bus;
134 _BE_env.vgaInfo.pcidev = info->pcidev;
136 _BE_env.vgaInfo.pciInfo = info->pciInfo;
138 _BE_env.vgaInfo.BIOSImage = info->BIOSImage;
139 if (info->BIOSImage) {
140 _BE_env.biosmem_base = (ulong) info->BIOSImage;
141 _BE_env.biosmem_limit = 0xC0000 + info->BIOSImageLen - 1;
143 _BE_env.biosmem_base = _BE_env.busmem_base + 0x20000;
144 _BE_env.biosmem_limit = 0xC7FFF;
146 if (*((u32 *) info->LowMem) == 0)
147 _BE_bios_init((u32 *) info->LowMem);
148 memcpy((u8 *) M.mem_base, info->LowMem, sizeof(info->LowMem));
151 /****************************************************************************
153 info - Pointer to VGA device information to retrieve current
156 This function returns the VGA BIOS functions currently active in the
157 emulator, so they can be restored at a later date.
158 ****************************************************************************/
159 void X86API BE_getVGA(BE_VGAInfo * info)
162 info->function = _BE_env.vgaInfo.function;
163 info->device = _BE_env.vgaInfo.device;
164 info->bus = _BE_env.vgaInfo.bus;
165 info->pcidev = _BE_env.vgaInfo.pcidev;
167 info->pciInfo = _BE_env.vgaInfo.pciInfo;
169 info->BIOSImage = _BE_env.vgaInfo.BIOSImage;
170 memcpy(info->LowMem, (u8 *) M.mem_base, sizeof(info->LowMem));
173 /****************************************************************************
175 r_seg - Segment for pointer to convert
176 r_off - Offset for pointer to convert
179 This function maps a real mode pointer in the emulator memory to a protected
180 mode pointer that can be used to directly access the memory.
182 NOTE: The memory is *always* in little endian format, son on non-x86
183 systems you will need to do endian translations to access this
185 ****************************************************************************/
186 void *X86API BE_mapRealPointer(uint r_seg, uint r_off)
188 u32 addr = ((u32) r_seg << 4) + r_off;
190 if (addr >= 0xC0000 && addr <= _BE_env.biosmem_limit) {
191 return (void *)(_BE_env.biosmem_base + addr - 0xC0000);
192 } else if (addr >= 0xA0000 && addr <= 0xFFFFF) {
193 return (void *)(_BE_env.busmem_base + addr - 0xA0000);
195 return (void *)(M.mem_base + addr);
198 /****************************************************************************
200 len - Return the length of the VESA buffer
201 rseg - Place to store VESA buffer segment
202 roff - Place to store VESA buffer offset
205 This function returns the address of the VESA transfer buffer in real
206 _BE_piomode emulator memory. The VESA transfer buffer is always 1024 bytes long,
207 and located at 15Kb into the start of the real mode memory (16Kb is where
208 we put the real mode code we execute for issuing interrupts).
210 NOTE: The memory is *always* in little endian format, son on non-x86
211 systems you will need to do endian translations to access this
213 ****************************************************************************/
214 void *X86API BE_getVESABuf(uint * len, uint * rseg, uint * roff)
217 *rseg = SEG(0x03C00);
218 *roff = OFF(0x03C00);
219 return (void *)(M.mem_base + ((u32) * rseg << 4) + *roff);
222 /****************************************************************************
224 Cleans up and exits the emulator.
225 ****************************************************************************/
226 void X86API BE_exit(void)
229 free(_BE_env.busmem_base);
232 /****************************************************************************
234 seg - Segment of code to call
235 off - Offset of code to call
236 regs - Real mode registers to load
237 sregs - Real mode segment registers to load
240 This functions calls a real mode far function at the specified address,
241 and loads all the x86 registers from the passed in registers structure.
242 On exit the registers returned from the call are returned in the same
244 ****************************************************************************/
245 void X86API BE_callRealMode(uint seg, uint off, RMREGS * regs, RMSREGS * sregs)
247 M.x86.R_EAX = regs->e.eax;
248 M.x86.R_EBX = regs->e.ebx;
249 M.x86.R_ECX = regs->e.ecx;
250 M.x86.R_EDX = regs->e.edx;
251 M.x86.R_ESI = regs->e.esi;
252 M.x86.R_EDI = regs->e.edi;
253 M.x86.R_DS = sregs->ds;
254 M.x86.R_ES = sregs->es;
255 M.x86.R_FS = sregs->fs;
256 M.x86.R_GS = sregs->gs;
258 ((u8 *) M.mem_base)[0x4000] = 0x9A;
259 ((u8 *) M.mem_base)[0x4001] = (u8) off;
260 ((u8 *) M.mem_base)[0x4002] = (u8) (off >> 8);
261 ((u8 *) M.mem_base)[0x4003] = (u8) seg;
262 ((u8 *) M.mem_base)[0x4004] = (u8) (seg >> 8);
263 ((u8 *) M.mem_base)[0x4005] = 0xF1; /* Illegal op-code */
264 M.x86.R_CS = SEG(0x04000);
265 M.x86.R_IP = OFF(0x04000);
267 M.x86.R_SS = SEG(M.mem_size - 2);
268 M.x86.R_SP = OFF(M.mem_size - 2) + 2;
272 regs->e.cflag = M.x86.R_EFLG & F_CF;
273 regs->e.eax = M.x86.R_EAX;
274 regs->e.ebx = M.x86.R_EBX;
275 regs->e.ecx = M.x86.R_ECX;
276 regs->e.edx = M.x86.R_EDX;
277 regs->e.esi = M.x86.R_ESI;
278 regs->e.edi = M.x86.R_EDI;
279 sregs->ds = M.x86.R_DS;
280 sregs->es = M.x86.R_ES;
281 sregs->fs = M.x86.R_FS;
282 sregs->gs = M.x86.R_GS;
285 /****************************************************************************
287 intno - Interrupt number to execute
288 in - Real mode registers to load
289 out - Place to store resulting real mode registers
292 This functions calls a real mode interrupt function at the specified address,
293 and loads all the x86 registers from the passed in registers structure.
294 On exit the registers returned from the call are returned in out stucture.
295 ****************************************************************************/
296 int X86API BE_int86(int intno, RMREGS * in, RMREGS * out)
298 M.x86.R_EAX = in->e.eax;
299 M.x86.R_EBX = in->e.ebx;
300 M.x86.R_ECX = in->e.ecx;
301 M.x86.R_EDX = in->e.edx;
302 M.x86.R_ESI = in->e.esi;
303 M.x86.R_EDI = in->e.edi;
304 ((u8 *) M.mem_base)[0x4000] = 0xCD;
305 ((u8 *) M.mem_base)[0x4001] = (u8) intno;
306 ((u8 *) M.mem_base)[0x4002] = 0xF1;
307 M.x86.R_CS = SEG(0x04000);
308 M.x86.R_IP = OFF(0x04000);
310 M.x86.R_SS = SEG(M.mem_size - 1);
311 M.x86.R_SP = OFF(M.mem_size - 1) - 1;
314 out->e.cflag = M.x86.R_EFLG & F_CF;
315 out->e.eax = M.x86.R_EAX;
316 out->e.ebx = M.x86.R_EBX;
317 out->e.ecx = M.x86.R_ECX;
318 out->e.edx = M.x86.R_EDX;
319 out->e.esi = M.x86.R_ESI;
320 out->e.edi = M.x86.R_EDI;
324 /****************************************************************************
326 intno - Interrupt number to execute
327 in - Real mode registers to load
328 out - Place to store resulting real mode registers
329 sregs - Real mode segment registers to load
332 This functions calls a real mode interrupt function at the specified address,
333 and loads all the x86 registers from the passed in registers structure.
334 On exit the registers returned from the call are returned in out stucture.
335 ****************************************************************************/
336 int X86API BE_int86x(int intno, RMREGS * in, RMREGS * out, RMSREGS * sregs)
338 M.x86.R_EAX = in->e.eax;
339 M.x86.R_EBX = in->e.ebx;
340 M.x86.R_ECX = in->e.ecx;
341 M.x86.R_EDX = in->e.edx;
342 M.x86.R_ESI = in->e.esi;
343 M.x86.R_EDI = in->e.edi;
344 M.x86.R_DS = sregs->ds;
345 M.x86.R_ES = sregs->es;
346 M.x86.R_FS = sregs->fs;
347 M.x86.R_GS = sregs->gs;
348 ((u8 *) M.mem_base)[0x4000] = 0xCD;
349 ((u8 *) M.mem_base)[0x4001] = (u8) intno;
350 ((u8 *) M.mem_base)[0x4002] = 0xF1;
351 M.x86.R_CS = SEG(0x04000);
352 M.x86.R_IP = OFF(0x04000);
354 M.x86.R_SS = SEG(M.mem_size - 1);
355 M.x86.R_SP = OFF(M.mem_size - 1) - 1;
358 out->e.cflag = M.x86.R_EFLG & F_CF;
359 out->e.eax = M.x86.R_EAX;
360 out->e.ebx = M.x86.R_EBX;
361 out->e.ecx = M.x86.R_ECX;
362 out->e.edx = M.x86.R_EDX;
363 out->e.esi = M.x86.R_ESI;
364 out->e.edi = M.x86.R_EDI;
365 sregs->ds = M.x86.R_DS;
366 sregs->es = M.x86.R_ES;
367 sregs->fs = M.x86.R_FS;
368 sregs->gs = M.x86.R_GS;