[FIX] Resolve problem with warnings
[oweals/u-boot.git] / drivers / bios_emulator / biosemu.c
1 /****************************************************************************
2 *
3 *                        BIOS emulator and interface
4 *                      to Realmode X86 Emulator Library
5 *
6 *  Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved.
7 *  Jason Jin <Jason.jin@freescale.com>
8 *
9 *               Copyright (C) 1996-1999 SciTech Software, Inc.
10 *
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
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.
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 *
33 * Language:     ANSI C
34 * Environment:  Any
35 * Developer:    Kendall Bennett
36 *
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.
40 *
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.
45 *
46 ****************************************************************************/
47
48 #include <malloc.h>
49
50 #if defined(CONFIG_BIOSEMU)
51
52 #include "biosemui.h"
53
54 BE_sysEnv _BE_env = {{0}};
55 static 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
64 static 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
73 #define OFF(addr)       (u16)(((addr) >> 0) & 0xffff)
74 #define SEG(addr)       (u16)(((addr) >> 4) & 0xf000)
75
76 /****************************************************************************
77 PARAMETERS:
78 debugFlags  - Flags to enable debugging options (debug builds only)
79 memSize     - Amount of memory to allocate for real mode machine
80 info        - Pointer to default VGA device information
81
82 REMARKS:
83 This functions initialises the BElib, and uses the passed in
84 BIOS image as the BIOS that is used and emulated at 0xC0000.
85 ****************************************************************************/
86 int 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 /****************************************************************************
121 PARAMETERS:
122 info        - Pointer to VGA device information to make current
123
124 REMARKS:
125 This function sets the VGA BIOS functions in the emulator to point to the
126 specific VGA BIOS in use. This includes swapping the BIOS interrupt
127 vectors, BIOS image and BIOS data area to the new BIOS. This allows the
128 real mode BIOS to be swapped without resetting the entire emulator.
129 ****************************************************************************/
130 void 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 /****************************************************************************
155 PARAMETERS:
156 info        - Pointer to VGA device information to retrieve current
157
158 REMARKS:
159 This function returns the VGA BIOS functions currently active in the
160 emulator, so they can be restored at a later date.
161 ****************************************************************************/
162 void 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 /****************************************************************************
177 PARAMETERS:
178 r_seg   - Segment for pointer to convert
179 r_off   - Offset for pointer to convert
180
181 REMARKS:
182 This function maps a real mode pointer in the emulator memory to a protected
183 mode pointer that can be used to directly access the memory.
184
185 NOTE:   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.
188 ****************************************************************************/
189 void *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 /****************************************************************************
202 PARAMETERS:
203 len     - Return the length of the VESA buffer
204 rseg    - Place to store VESA buffer segment
205 roff    - Place to store VESA buffer offset
206
207 REMARKS:
208 This 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,
210 and located at 15Kb into the start of the real mode memory (16Kb is where
211 we put the real mode code we execute for issuing interrupts).
212
213 NOTE:   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.
216 ****************************************************************************/
217 void *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 /****************************************************************************
226 REMARKS:
227 Cleans up and exits the emulator.
228 ****************************************************************************/
229 void X86API BE_exit(void)
230 {
231         free(M.mem_base);
232         free(_BE_env.busmem_base);
233 }
234
235 /****************************************************************************
236 PARAMETERS:
237 seg     - Segment of code to call
238 off     - Offset of code to call
239 regs    - Real mode registers to load
240 sregs   - Real mode segment registers to load
241
242 REMARKS:
243 This functions calls a real mode far function at the specified address,
244 and loads all the x86 registers from the passed in registers structure.
245 On exit the registers returned from the call are returned in the same
246 structures.
247 ****************************************************************************/
248 void 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 /****************************************************************************
289 PARAMETERS:
290 intno   - Interrupt number to execute
291 in      - Real mode registers to load
292 out     - Place to store resulting real mode registers
293
294 REMARKS:
295 This functions calls a real mode interrupt function at the specified address,
296 and loads all the x86 registers from the passed in registers structure.
297 On exit the registers returned from the call are returned in out stucture.
298 ****************************************************************************/
299 int 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 /****************************************************************************
328 PARAMETERS:
329 intno   - Interrupt number to execute
330 in      - Real mode registers to load
331 out     - Place to store resulting real mode registers
332 sregs   - Real mode segment registers to load
333
334 REMARKS:
335 This functions calls a real mode interrupt function at the specified address,
336 and loads all the x86 registers from the passed in registers structure.
337 On exit the registers returned from the call are returned in out stucture.
338 ****************************************************************************/
339 int 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 }
374 #endif