Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 1 | /**************************************************************************** |
| 2 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 3 | * BIOS emulator and interface |
| 4 | * to Realmode X86 Emulator Library |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 2007 Freescale Semiconductor, Inc. All rights reserved. |
| 7 | * Jason Jin <Jason.jin@freescale.com> |
| 8 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 9 | * Copyright (C) 1996-1999 SciTech Software, Inc. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 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 |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 19 | * without specific, written prior permission. The authors makes no |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 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 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 33 | * Language: ANSI C |
| 34 | * Environment: Any |
| 35 | * Developer: Kendall Bennett |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 36 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 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. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 40 | * |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 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. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 45 | * |
| 46 | ****************************************************************************/ |
| 47 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 48 | #include <malloc.h> |
Michal Simek | c73a477 | 2007-08-16 10:46:28 +0200 | [diff] [blame] | 49 | #include <common.h> |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 50 | |
Jason Jin | 7ed5cd9 | 2007-08-08 08:33:11 +0800 | [diff] [blame] | 51 | #if defined(CONFIG_BIOSEMU) |
| 52 | |
Michal Simek | 952d861 | 2007-08-15 21:15:05 +0200 | [diff] [blame] | 53 | #include "biosemui.h" |
| 54 | |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 55 | BE_sysEnv _BE_env = {{0}}; |
| 56 | static X86EMU_memFuncs _BE_mem __attribute__((section(".got2"))) = { |
| 57 | BE_rdb, |
| 58 | BE_rdw, |
| 59 | BE_rdl, |
| 60 | BE_wrb, |
| 61 | BE_wrw, |
| 62 | BE_wrl, |
| 63 | }; |
| 64 | |
| 65 | static X86EMU_pioFuncs _BE_pio __attribute__((section(".got2"))) = { |
| 66 | BE_inb, |
| 67 | BE_inw, |
| 68 | BE_inl, |
| 69 | BE_outb, |
| 70 | BE_outw, |
| 71 | BE_outl, |
| 72 | }; |
| 73 | |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 74 | #define OFF(addr) (u16)(((addr) >> 0) & 0xffff) |
| 75 | #define SEG(addr) (u16)(((addr) >> 4) & 0xf000) |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 76 | |
| 77 | /**************************************************************************** |
| 78 | PARAMETERS: |
| 79 | debugFlags - Flags to enable debugging options (debug builds only) |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 80 | memSize - Amount of memory to allocate for real mode machine |
| 81 | info - Pointer to default VGA device information |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 82 | |
| 83 | REMARKS: |
| 84 | This functions initialises the BElib, and uses the passed in |
| 85 | BIOS image as the BIOS that is used and emulated at 0xC0000. |
| 86 | ****************************************************************************/ |
| 87 | int X86API BE_init(u32 debugFlags, int memSize, BE_VGAInfo * info, int shared) |
| 88 | { |
| 89 | #if !defined(__DRIVER__) && !defined(__KERNEL__) |
| 90 | |
| 91 | PM_init(); |
| 92 | #endif |
| 93 | memset(&M, 0, sizeof(M)); |
| 94 | if (memSize < 20480){ |
| 95 | printf("Emulator requires at least 20Kb of memory!\n"); |
| 96 | return 0; |
| 97 | } |
| 98 | |
Wolfgang Denk | 9225411 | 2007-11-18 16:36:27 +0100 | [diff] [blame] | 99 | M.mem_base = malloc(memSize); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 100 | |
| 101 | if (M.mem_base == NULL){ |
| 102 | printf("Biosemu:Out of memory!"); |
| 103 | return 0; |
| 104 | } |
| 105 | M.mem_size = memSize; |
| 106 | |
| 107 | _BE_env.emulateVGA = 0; |
| 108 | _BE_env.busmem_base = (unsigned long)malloc(128 * 1024); |
Wolfgang Denk | 9225411 | 2007-11-18 16:36:27 +0100 | [diff] [blame] | 109 | if ((void *)_BE_env.busmem_base == NULL){ |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 110 | printf("Biosemu:Out of memory!"); |
| 111 | return 0; |
| 112 | } |
| 113 | M.x86.debug = debugFlags; |
| 114 | _BE_bios_init((u32*)info->LowMem); |
| 115 | X86EMU_setupMemFuncs(&_BE_mem); |
| 116 | X86EMU_setupPioFuncs(&_BE_pio); |
| 117 | BE_setVGA(info); |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | /**************************************************************************** |
| 122 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 123 | info - Pointer to VGA device information to make current |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 124 | |
| 125 | REMARKS: |
| 126 | This function sets the VGA BIOS functions in the emulator to point to the |
| 127 | specific VGA BIOS in use. This includes swapping the BIOS interrupt |
| 128 | vectors, BIOS image and BIOS data area to the new BIOS. This allows the |
| 129 | real mode BIOS to be swapped without resetting the entire emulator. |
| 130 | ****************************************************************************/ |
| 131 | void X86API BE_setVGA(BE_VGAInfo * info) |
| 132 | { |
| 133 | |
| 134 | #ifdef __KERNEL__ |
| 135 | _BE_env.vgaInfo.function = info->function; |
| 136 | _BE_env.vgaInfo.device = info->device; |
| 137 | _BE_env.vgaInfo.bus = info->bus; |
| 138 | _BE_env.vgaInfo.pcidev = info->pcidev; |
| 139 | #else |
| 140 | _BE_env.vgaInfo.pciInfo = info->pciInfo; |
| 141 | #endif |
| 142 | _BE_env.vgaInfo.BIOSImage = info->BIOSImage; |
| 143 | if (info->BIOSImage) { |
| 144 | _BE_env.biosmem_base = (ulong) info->BIOSImage; |
| 145 | _BE_env.biosmem_limit = 0xC0000 + info->BIOSImageLen - 1; |
| 146 | } else { |
| 147 | _BE_env.biosmem_base = _BE_env.busmem_base + 0x20000; |
| 148 | _BE_env.biosmem_limit = 0xC7FFF; |
| 149 | } |
| 150 | if (*((u32 *) info->LowMem) == 0) |
| 151 | _BE_bios_init((u32 *) info->LowMem); |
| 152 | memcpy((u8 *) M.mem_base, info->LowMem, sizeof(info->LowMem)); |
| 153 | } |
| 154 | |
| 155 | /**************************************************************************** |
| 156 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 157 | info - Pointer to VGA device information to retrieve current |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 158 | |
| 159 | REMARKS: |
| 160 | This function returns the VGA BIOS functions currently active in the |
| 161 | emulator, so they can be restored at a later date. |
| 162 | ****************************************************************************/ |
| 163 | void X86API BE_getVGA(BE_VGAInfo * info) |
| 164 | { |
| 165 | #ifdef __KERNEL__ |
| 166 | info->function = _BE_env.vgaInfo.function; |
| 167 | info->device = _BE_env.vgaInfo.device; |
| 168 | info->bus = _BE_env.vgaInfo.bus; |
| 169 | info->pcidev = _BE_env.vgaInfo.pcidev; |
| 170 | #else |
| 171 | info->pciInfo = _BE_env.vgaInfo.pciInfo; |
| 172 | #endif |
| 173 | info->BIOSImage = _BE_env.vgaInfo.BIOSImage; |
| 174 | memcpy(info->LowMem, (u8 *) M.mem_base, sizeof(info->LowMem)); |
| 175 | } |
| 176 | |
| 177 | /**************************************************************************** |
| 178 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 179 | r_seg - Segment for pointer to convert |
| 180 | r_off - Offset for pointer to convert |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 181 | |
| 182 | REMARKS: |
| 183 | This function maps a real mode pointer in the emulator memory to a protected |
| 184 | mode pointer that can be used to directly access the memory. |
| 185 | |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 186 | NOTE: The memory is *always* in little endian format, son on non-x86 |
| 187 | systems you will need to do endian translations to access this |
| 188 | memory. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 189 | ****************************************************************************/ |
| 190 | void *X86API BE_mapRealPointer(uint r_seg, uint r_off) |
| 191 | { |
| 192 | u32 addr = ((u32) r_seg << 4) + r_off; |
| 193 | |
| 194 | if (addr >= 0xC0000 && addr <= _BE_env.biosmem_limit) { |
| 195 | return (void *)(_BE_env.biosmem_base + addr - 0xC0000); |
| 196 | } else if (addr >= 0xA0000 && addr <= 0xFFFFF) { |
| 197 | return (void *)(_BE_env.busmem_base + addr - 0xA0000); |
| 198 | } |
| 199 | return (void *)(M.mem_base + addr); |
| 200 | } |
| 201 | |
| 202 | /**************************************************************************** |
| 203 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 204 | len - Return the length of the VESA buffer |
| 205 | rseg - Place to store VESA buffer segment |
| 206 | roff - Place to store VESA buffer offset |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 207 | |
| 208 | REMARKS: |
| 209 | This function returns the address of the VESA transfer buffer in real |
| 210 | _BE_piomode emulator memory. The VESA transfer buffer is always 1024 bytes long, |
| 211 | and located at 15Kb into the start of the real mode memory (16Kb is where |
| 212 | we put the real mode code we execute for issuing interrupts). |
| 213 | |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 214 | NOTE: The memory is *always* in little endian format, son on non-x86 |
| 215 | systems you will need to do endian translations to access this |
| 216 | memory. |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 217 | ****************************************************************************/ |
| 218 | void *X86API BE_getVESABuf(uint * len, uint * rseg, uint * roff) |
| 219 | { |
| 220 | *len = 1024; |
| 221 | *rseg = SEG(0x03C00); |
| 222 | *roff = OFF(0x03C00); |
| 223 | return (void *)(M.mem_base + ((u32) * rseg << 4) + *roff); |
| 224 | } |
| 225 | |
| 226 | /**************************************************************************** |
| 227 | REMARKS: |
| 228 | Cleans up and exits the emulator. |
| 229 | ****************************************************************************/ |
| 230 | void X86API BE_exit(void) |
| 231 | { |
| 232 | free(M.mem_base); |
Wolfgang Denk | 9225411 | 2007-11-18 16:36:27 +0100 | [diff] [blame] | 233 | free((void *)_BE_env.busmem_base); |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | /**************************************************************************** |
| 237 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 238 | seg - Segment of code to call |
| 239 | off - Offset of code to call |
| 240 | regs - Real mode registers to load |
| 241 | sregs - Real mode segment registers to load |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 242 | |
| 243 | REMARKS: |
| 244 | This functions calls a real mode far function at the specified address, |
| 245 | and loads all the x86 registers from the passed in registers structure. |
| 246 | On exit the registers returned from the call are returned in the same |
| 247 | structures. |
| 248 | ****************************************************************************/ |
| 249 | void X86API BE_callRealMode(uint seg, uint off, RMREGS * regs, RMSREGS * sregs) |
| 250 | { |
| 251 | M.x86.R_EAX = regs->e.eax; |
| 252 | M.x86.R_EBX = regs->e.ebx; |
| 253 | M.x86.R_ECX = regs->e.ecx; |
| 254 | M.x86.R_EDX = regs->e.edx; |
| 255 | M.x86.R_ESI = regs->e.esi; |
| 256 | M.x86.R_EDI = regs->e.edi; |
| 257 | M.x86.R_DS = sregs->ds; |
| 258 | M.x86.R_ES = sregs->es; |
| 259 | M.x86.R_FS = sregs->fs; |
| 260 | M.x86.R_GS = sregs->gs; |
| 261 | |
| 262 | ((u8 *) M.mem_base)[0x4000] = 0x9A; |
| 263 | ((u8 *) M.mem_base)[0x4001] = (u8) off; |
| 264 | ((u8 *) M.mem_base)[0x4002] = (u8) (off >> 8); |
| 265 | ((u8 *) M.mem_base)[0x4003] = (u8) seg; |
| 266 | ((u8 *) M.mem_base)[0x4004] = (u8) (seg >> 8); |
| 267 | ((u8 *) M.mem_base)[0x4005] = 0xF1; /* Illegal op-code */ |
| 268 | M.x86.R_CS = SEG(0x04000); |
| 269 | M.x86.R_IP = OFF(0x04000); |
| 270 | |
| 271 | M.x86.R_SS = SEG(M.mem_size - 2); |
| 272 | M.x86.R_SP = OFF(M.mem_size - 2) + 2; |
| 273 | |
| 274 | X86EMU_exec(); |
| 275 | |
| 276 | regs->e.cflag = M.x86.R_EFLG & F_CF; |
| 277 | regs->e.eax = M.x86.R_EAX; |
| 278 | regs->e.ebx = M.x86.R_EBX; |
| 279 | regs->e.ecx = M.x86.R_ECX; |
| 280 | regs->e.edx = M.x86.R_EDX; |
| 281 | regs->e.esi = M.x86.R_ESI; |
| 282 | regs->e.edi = M.x86.R_EDI; |
| 283 | sregs->ds = M.x86.R_DS; |
| 284 | sregs->es = M.x86.R_ES; |
| 285 | sregs->fs = M.x86.R_FS; |
| 286 | sregs->gs = M.x86.R_GS; |
| 287 | } |
| 288 | |
| 289 | /**************************************************************************** |
| 290 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 291 | intno - Interrupt number to execute |
| 292 | in - Real mode registers to load |
| 293 | out - Place to store resulting real mode registers |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 294 | |
| 295 | REMARKS: |
| 296 | This functions calls a real mode interrupt function at the specified address, |
| 297 | and loads all the x86 registers from the passed in registers structure. |
| 298 | On exit the registers returned from the call are returned in out stucture. |
| 299 | ****************************************************************************/ |
| 300 | int X86API BE_int86(int intno, RMREGS * in, RMREGS * out) |
| 301 | { |
| 302 | M.x86.R_EAX = in->e.eax; |
| 303 | M.x86.R_EBX = in->e.ebx; |
| 304 | M.x86.R_ECX = in->e.ecx; |
| 305 | M.x86.R_EDX = in->e.edx; |
| 306 | M.x86.R_ESI = in->e.esi; |
| 307 | M.x86.R_EDI = in->e.edi; |
| 308 | ((u8 *) M.mem_base)[0x4000] = 0xCD; |
| 309 | ((u8 *) M.mem_base)[0x4001] = (u8) intno; |
| 310 | ((u8 *) M.mem_base)[0x4002] = 0xF1; |
| 311 | M.x86.R_CS = SEG(0x04000); |
| 312 | M.x86.R_IP = OFF(0x04000); |
| 313 | |
| 314 | M.x86.R_SS = SEG(M.mem_size - 1); |
| 315 | M.x86.R_SP = OFF(M.mem_size - 1) - 1; |
| 316 | |
| 317 | X86EMU_exec(); |
| 318 | out->e.cflag = M.x86.R_EFLG & F_CF; |
| 319 | out->e.eax = M.x86.R_EAX; |
| 320 | out->e.ebx = M.x86.R_EBX; |
| 321 | out->e.ecx = M.x86.R_ECX; |
| 322 | out->e.edx = M.x86.R_EDX; |
| 323 | out->e.esi = M.x86.R_ESI; |
| 324 | out->e.edi = M.x86.R_EDI; |
| 325 | return out->x.ax; |
| 326 | } |
| 327 | |
| 328 | /**************************************************************************** |
| 329 | PARAMETERS: |
Wolfgang Denk | 96bb2e0 | 2007-08-06 02:17:36 +0200 | [diff] [blame] | 330 | intno - Interrupt number to execute |
| 331 | in - Real mode registers to load |
| 332 | out - Place to store resulting real mode registers |
| 333 | sregs - Real mode segment registers to load |
Jason Jin | a63ce95 | 2007-07-06 08:34:56 +0800 | [diff] [blame] | 334 | |
| 335 | REMARKS: |
| 336 | This functions calls a real mode interrupt function at the specified address, |
| 337 | and loads all the x86 registers from the passed in registers structure. |
| 338 | On exit the registers returned from the call are returned in out stucture. |
| 339 | ****************************************************************************/ |
| 340 | int X86API BE_int86x(int intno, RMREGS * in, RMREGS * out, RMSREGS * sregs) |
| 341 | { |
| 342 | M.x86.R_EAX = in->e.eax; |
| 343 | M.x86.R_EBX = in->e.ebx; |
| 344 | M.x86.R_ECX = in->e.ecx; |
| 345 | M.x86.R_EDX = in->e.edx; |
| 346 | M.x86.R_ESI = in->e.esi; |
| 347 | M.x86.R_EDI = in->e.edi; |
| 348 | M.x86.R_DS = sregs->ds; |
| 349 | M.x86.R_ES = sregs->es; |
| 350 | M.x86.R_FS = sregs->fs; |
| 351 | M.x86.R_GS = sregs->gs; |
| 352 | ((u8 *) M.mem_base)[0x4000] = 0xCD; |
| 353 | ((u8 *) M.mem_base)[0x4001] = (u8) intno; |
| 354 | ((u8 *) M.mem_base)[0x4002] = 0xF1; |
| 355 | M.x86.R_CS = SEG(0x04000); |
| 356 | M.x86.R_IP = OFF(0x04000); |
| 357 | |
| 358 | M.x86.R_SS = SEG(M.mem_size - 1); |
| 359 | M.x86.R_SP = OFF(M.mem_size - 1) - 1; |
| 360 | |
| 361 | X86EMU_exec(); |
| 362 | out->e.cflag = M.x86.R_EFLG & F_CF; |
| 363 | out->e.eax = M.x86.R_EAX; |
| 364 | out->e.ebx = M.x86.R_EBX; |
| 365 | out->e.ecx = M.x86.R_ECX; |
| 366 | out->e.edx = M.x86.R_EDX; |
| 367 | out->e.esi = M.x86.R_ESI; |
| 368 | out->e.edi = M.x86.R_EDI; |
| 369 | sregs->ds = M.x86.R_DS; |
| 370 | sregs->es = M.x86.R_ES; |
| 371 | sregs->fs = M.x86.R_FS; |
| 372 | sregs->gs = M.x86.R_GS; |
| 373 | return out->x.ax; |
| 374 | } |
Jason Jin | 7ed5cd9 | 2007-08-08 08:33:11 +0800 | [diff] [blame] | 375 | #endif |