biosemu.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /****************************************************************************
  2. *
  3. * BIOS emulator and interface
  4. * to Realmode X86 Emulator Library
  5. *
  6. * Copyright (C) 2007 Freescale Semiconductor, Inc.
  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 <malloc.h>
  48. #include <common.h>
  49. #include "biosemui.h"
  50. BE_sysEnv _BE_env = {{0}};
  51. static X86EMU_memFuncs _BE_mem __section(GOT2_TYPE) = {
  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 __section(GOT2_TYPE) = {
  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 = 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 ((void *)_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 ((info->LowMem[0] == 0) && (info->LowMem[1] == 0) &&
  135. (info->LowMem[2] == 0) && (info->LowMem[3] == 0))
  136. _BE_bios_init((u32 *) info->LowMem);
  137. memcpy((u8 *) M.mem_base, info->LowMem, sizeof(info->LowMem));
  138. }
  139. /****************************************************************************
  140. PARAMETERS:
  141. info - Pointer to VGA device information to retrieve current
  142. REMARKS:
  143. This function returns the VGA BIOS functions currently active in the
  144. emulator, so they can be restored at a later date.
  145. ****************************************************************************/
  146. void X86API BE_getVGA(BE_VGAInfo * info)
  147. {
  148. #ifdef __KERNEL__
  149. info->function = _BE_env.vgaInfo.function;
  150. info->device = _BE_env.vgaInfo.device;
  151. info->bus = _BE_env.vgaInfo.bus;
  152. info->pcidev = _BE_env.vgaInfo.pcidev;
  153. #else
  154. info->pciInfo = _BE_env.vgaInfo.pciInfo;
  155. #endif
  156. info->BIOSImage = _BE_env.vgaInfo.BIOSImage;
  157. memcpy(info->LowMem, (u8 *) M.mem_base, sizeof(info->LowMem));
  158. }
  159. /****************************************************************************
  160. PARAMETERS:
  161. r_seg - Segment for pointer to convert
  162. r_off - Offset for pointer to convert
  163. REMARKS:
  164. This function maps a real mode pointer in the emulator memory to a protected
  165. mode pointer that can be used to directly access the memory.
  166. NOTE: The memory is *always* in little endian format, son on non-x86
  167. systems you will need to do endian translations to access this
  168. memory.
  169. ****************************************************************************/
  170. void *X86API BE_mapRealPointer(uint r_seg, uint r_off)
  171. {
  172. u32 addr = ((u32) r_seg << 4) + r_off;
  173. if (addr >= 0xC0000 && addr <= _BE_env.biosmem_limit) {
  174. return (void *)(_BE_env.biosmem_base + addr - 0xC0000);
  175. } else if (addr >= 0xA0000 && addr <= 0xFFFFF) {
  176. return (void *)(_BE_env.busmem_base + addr - 0xA0000);
  177. }
  178. return (void *)(M.mem_base + addr);
  179. }
  180. /****************************************************************************
  181. PARAMETERS:
  182. len - Return the length of the VESA buffer
  183. rseg - Place to store VESA buffer segment
  184. roff - Place to store VESA buffer offset
  185. REMARKS:
  186. This function returns the address of the VESA transfer buffer in real
  187. _BE_piomode emulator memory. The VESA transfer buffer is always 1024 bytes long,
  188. and located at 15Kb into the start of the real mode memory (16Kb is where
  189. we put the real mode code we execute for issuing interrupts).
  190. NOTE: The memory is *always* in little endian format, son on non-x86
  191. systems you will need to do endian translations to access this
  192. memory.
  193. ****************************************************************************/
  194. void *X86API BE_getVESABuf(uint * len, uint * rseg, uint * roff)
  195. {
  196. *len = 1024;
  197. *rseg = SEG(0x03C00);
  198. *roff = OFF(0x03C00);
  199. return (void *)(M.mem_base + ((u32) * rseg << 4) + *roff);
  200. }
  201. /****************************************************************************
  202. REMARKS:
  203. Cleans up and exits the emulator.
  204. ****************************************************************************/
  205. void X86API BE_exit(void)
  206. {
  207. free(M.mem_base);
  208. free((void *)_BE_env.busmem_base);
  209. }
  210. /****************************************************************************
  211. PARAMETERS:
  212. seg - Segment of code to call
  213. off - Offset of code to call
  214. regs - Real mode registers to load
  215. sregs - Real mode segment registers to load
  216. REMARKS:
  217. This functions calls a real mode far function at the specified address,
  218. and loads all the x86 registers from the passed in registers structure.
  219. On exit the registers returned from the call are returned in the same
  220. structures.
  221. ****************************************************************************/
  222. void X86API BE_callRealMode(uint seg, uint off, RMREGS * regs, RMSREGS * sregs)
  223. {
  224. M.x86.R_EAX = regs->e.eax;
  225. M.x86.R_EBX = regs->e.ebx;
  226. M.x86.R_ECX = regs->e.ecx;
  227. M.x86.R_EDX = regs->e.edx;
  228. M.x86.R_ESI = regs->e.esi;
  229. M.x86.R_EDI = regs->e.edi;
  230. M.x86.R_DS = sregs->ds;
  231. M.x86.R_ES = sregs->es;
  232. M.x86.R_FS = sregs->fs;
  233. M.x86.R_GS = sregs->gs;
  234. ((u8 *) M.mem_base)[0x4000] = 0x9A;
  235. ((u8 *) M.mem_base)[0x4001] = (u8) off;
  236. ((u8 *) M.mem_base)[0x4002] = (u8) (off >> 8);
  237. ((u8 *) M.mem_base)[0x4003] = (u8) seg;
  238. ((u8 *) M.mem_base)[0x4004] = (u8) (seg >> 8);
  239. ((u8 *) M.mem_base)[0x4005] = 0xF1; /* Illegal op-code */
  240. M.x86.R_CS = SEG(0x04000);
  241. M.x86.R_IP = OFF(0x04000);
  242. M.x86.R_SS = SEG(M.mem_size - 2);
  243. M.x86.R_SP = OFF(M.mem_size - 2) + 2;
  244. X86EMU_exec();
  245. regs->e.cflag = M.x86.R_EFLG & F_CF;
  246. regs->e.eax = M.x86.R_EAX;
  247. regs->e.ebx = M.x86.R_EBX;
  248. regs->e.ecx = M.x86.R_ECX;
  249. regs->e.edx = M.x86.R_EDX;
  250. regs->e.esi = M.x86.R_ESI;
  251. regs->e.edi = M.x86.R_EDI;
  252. sregs->ds = M.x86.R_DS;
  253. sregs->es = M.x86.R_ES;
  254. sregs->fs = M.x86.R_FS;
  255. sregs->gs = M.x86.R_GS;
  256. }
  257. /****************************************************************************
  258. PARAMETERS:
  259. intno - Interrupt number to execute
  260. in - Real mode registers to load
  261. out - Place to store resulting real mode registers
  262. REMARKS:
  263. This functions calls a real mode interrupt function at the specified address,
  264. and loads all the x86 registers from the passed in registers structure.
  265. On exit the registers returned from the call are returned in out stucture.
  266. ****************************************************************************/
  267. int X86API BE_int86(int intno, RMREGS * in, RMREGS * out)
  268. {
  269. M.x86.R_EAX = in->e.eax;
  270. M.x86.R_EBX = in->e.ebx;
  271. M.x86.R_ECX = in->e.ecx;
  272. M.x86.R_EDX = in->e.edx;
  273. M.x86.R_ESI = in->e.esi;
  274. M.x86.R_EDI = in->e.edi;
  275. ((u8 *) M.mem_base)[0x4000] = 0xCD;
  276. ((u8 *) M.mem_base)[0x4001] = (u8) intno;
  277. ((u8 *) M.mem_base)[0x4002] = 0xF1;
  278. M.x86.R_CS = SEG(0x04000);
  279. M.x86.R_IP = OFF(0x04000);
  280. M.x86.R_SS = SEG(M.mem_size - 1);
  281. M.x86.R_SP = OFF(M.mem_size - 1) - 1;
  282. X86EMU_exec();
  283. out->e.cflag = M.x86.R_EFLG & F_CF;
  284. out->e.eax = M.x86.R_EAX;
  285. out->e.ebx = M.x86.R_EBX;
  286. out->e.ecx = M.x86.R_ECX;
  287. out->e.edx = M.x86.R_EDX;
  288. out->e.esi = M.x86.R_ESI;
  289. out->e.edi = M.x86.R_EDI;
  290. return out->x.ax;
  291. }
  292. /****************************************************************************
  293. PARAMETERS:
  294. intno - Interrupt number to execute
  295. in - Real mode registers to load
  296. out - Place to store resulting real mode registers
  297. sregs - Real mode segment registers to load
  298. REMARKS:
  299. This functions calls a real mode interrupt function at the specified address,
  300. and loads all the x86 registers from the passed in registers structure.
  301. On exit the registers returned from the call are returned in out stucture.
  302. ****************************************************************************/
  303. int X86API BE_int86x(int intno, RMREGS * in, RMREGS * out, RMSREGS * sregs)
  304. {
  305. M.x86.R_EAX = in->e.eax;
  306. M.x86.R_EBX = in->e.ebx;
  307. M.x86.R_ECX = in->e.ecx;
  308. M.x86.R_EDX = in->e.edx;
  309. M.x86.R_ESI = in->e.esi;
  310. M.x86.R_EDI = in->e.edi;
  311. M.x86.R_DS = sregs->ds;
  312. M.x86.R_ES = sregs->es;
  313. M.x86.R_FS = sregs->fs;
  314. M.x86.R_GS = sregs->gs;
  315. ((u8 *) M.mem_base)[0x4000] = 0xCD;
  316. ((u8 *) M.mem_base)[0x4001] = (u8) intno;
  317. ((u8 *) M.mem_base)[0x4002] = 0xF1;
  318. M.x86.R_CS = SEG(0x04000);
  319. M.x86.R_IP = OFF(0x04000);
  320. M.x86.R_SS = SEG(M.mem_size - 1);
  321. M.x86.R_SP = OFF(M.mem_size - 1) - 1;
  322. X86EMU_exec();
  323. out->e.cflag = M.x86.R_EFLG & F_CF;
  324. out->e.eax = M.x86.R_EAX;
  325. out->e.ebx = M.x86.R_EBX;
  326. out->e.ecx = M.x86.R_ECX;
  327. out->e.edx = M.x86.R_EDX;
  328. out->e.esi = M.x86.R_ESI;
  329. out->e.edi = M.x86.R_EDI;
  330. sregs->ds = M.x86.R_DS;
  331. sregs->es = M.x86.R_ES;
  332. sregs->fs = M.x86.R_FS;
  333. sregs->gs = M.x86.R_GS;
  334. return out->x.ax;
  335. }