biosemu.c 12 KB

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