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