78f78c14d9115a7775642c27e7b0a17f69379b17
[reactos.git] / reactos / subsystems / mvdm / ntvdm / bios / bios.c
1 /*
2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
4 * FILE: bios.c
5 * PURPOSE: VDM BIOS Support Library
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
7 */
8
9 /* INCLUDES *******************************************************************/
10
11 #define NDEBUG
12
13 #include "emulator.h"
14 #include "memory.h"
15 #include "cpu/callback.h"
16 #include "cpu/bop.h"
17
18 #include "bios.h"
19 #include "bios32/bios32.h"
20
21 #include "rom.h"
22
23 #include "io.h"
24 #include "hardware/cmos.h"
25
26 /* DEFINES ********************************************************************/
27
28 /* BOP Identifiers */
29 #define BOP_RESET 0x00 // Windows NTVDM (SoftPC) BIOS calls BOP 0x00
30 // to let the virtual machine initialize itself
31 // the IVT and its hardware.
32 #define BOP_EQUIPLIST 0x11
33 #define BOP_GETMEMSIZE 0x12
34
35 /* PRIVATE VARIABLES **********************************************************/
36
37 static BOOLEAN Bios32Loaded = FALSE;
38
39 PBIOS_DATA_AREA Bda;
40 PBIOS_CONFIG_TABLE Bct;
41
42 /* PRIVATE FUNCTIONS **********************************************************/
43
44 static BOOLEAN NTAPI BiosRomWrite(ULONG Address, PVOID Buffer, ULONG Size)
45 {
46 /* Prevent writing to ROM */
47 return FALSE;
48 }
49
50 /* PUBLIC FUNCTIONS ***********************************************************/
51
52 VOID WINAPI BiosEquipmentService(LPWORD Stack)
53 {
54 /* Return the equipment list */
55 setAX(Bda->EquipmentList);
56 }
57
58 VOID WINAPI BiosGetMemorySize(LPWORD Stack)
59 {
60 /* Return the conventional memory size in kB, typically 640 kB */
61 setAX(Bda->MemorySize);
62 }
63
64 BOOLEAN
65 BiosInitialize(IN LPCSTR BiosFileName)
66 {
67 BOOLEAN Success = FALSE;
68
69 /* Disable interrupts */
70 setIF(0);
71
72 /* Initialize the BDA and the BCT pointers */
73 Bda = (PBIOS_DATA_AREA)SEG_OFF_TO_PTR(BDA_SEGMENT, 0x0000);
74 // The BCT is found at F000:E6F5 for 100% compatible BIOSes.
75 Bct = (PBIOS_CONFIG_TABLE)SEG_OFF_TO_PTR(BIOS_SEGMENT, 0xE6F5);
76
77 /**** HACK! HACK! for Windows NTVDM BIOS ****/
78 // WinNtVdmBiosSupportInitialize();
79
80 // /* Register the BIOS support BOPs */
81 // RegisterBop(BOP_EQUIPLIST , BiosEquipmentService);
82 // RegisterBop(BOP_GETMEMSIZE, BiosGetMemorySize);
83
84 MemInstallFastMemoryHook((PVOID)ROM_AREA_START,
85 ROM_AREA_END - ROM_AREA_START + 1,
86 NULL,
87 BiosRomWrite);
88
89 if (BiosFileName && BiosFileName[0] != '\0')
90 {
91 PVOID BiosLocation = NULL;
92 DWORD BiosSize = 0;
93
94 Success = LoadBios(BiosFileName, &BiosLocation, &BiosSize);
95 DPRINT1("BIOS loading %s ; GetLastError() = %u\n", Success ? "succeeded" : "failed", GetLastError());
96
97 if (!Success) return FALSE;
98
99 DisplayMessage(L"First bytes at 0x%p: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n"
100 L"3 last bytes at 0x%p: 0x%02x 0x%02x 0x%02x",
101 BiosLocation,
102 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 0),
103 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 1),
104 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 2),
105 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 3),
106 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 4),
107 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 5),
108 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 6),
109 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 7),
110 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 8),
111 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + 9),
112
113 (PVOID)((ULONG_PTR)BiosLocation + BiosSize - 2),
114 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + BiosSize - 2),
115 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + BiosSize - 1),
116 *(PCHAR)((ULONG_PTR)REAL_TO_PHYS(BiosLocation) + BiosSize - 0));
117
118 DisplayMessage(L"POST at 0x%p: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
119 TO_LINEAR(getCS(), getIP()),
120 *(PCHAR)((ULONG_PTR)SEG_OFF_TO_PTR(getCS(), getIP()) + 0),
121 *(PCHAR)((ULONG_PTR)SEG_OFF_TO_PTR(getCS(), getIP()) + 1),
122 *(PCHAR)((ULONG_PTR)SEG_OFF_TO_PTR(getCS(), getIP()) + 2),
123 *(PCHAR)((ULONG_PTR)SEG_OFF_TO_PTR(getCS(), getIP()) + 3),
124 *(PCHAR)((ULONG_PTR)SEG_OFF_TO_PTR(getCS(), getIP()) + 4));
125
126 /* Boot it up */
127
128 /*
129 * The CPU is already in reset-mode so that
130 * CS:IP points to F000:FFF0 as required.
131 */
132 DisplayMessage(L"CS=0x%p ; IP=0x%p", getCS(), getIP());
133 // setCS(0xF000);
134 // setIP(0xFFF0);
135
136 Success = TRUE;
137 }
138 else
139 {
140 Success = Bios32Loaded = Bios32Initialize();
141 }
142
143 // /* Enable interrupts */
144 // setIF(1);
145
146 return Success;
147 }
148
149 VOID
150 BiosCleanup(VOID)
151 {
152 if (Bios32Loaded) Bios32Cleanup();
153 }
154
155 /* EOF */