ReactOS 0.4.16-dev-297-gc569aee
rom.c File Reference
#include "ntvdm.h"
#include <debug.h>
#include "emulator.h"
#include "memory.h"
#include "cpu/callback.h"
#include "rom.h"
#include "utils.h"
Include dependency graph for rom.c:

Go to the source code of this file.

Macros

#define NDEBUG
 

Functions

static BOOLEAN FASTCALL ShadowRomWrite (ULONG Address, PVOID Buffer, ULONG Size)
 
static HANDLE OpenRomFile (IN PCSTR RomFileName, OUT PULONG RomSize OPTIONAL)
 
static BOOLEAN LoadRomFileByHandle (IN HANDLE RomFileHandle, IN PVOID RomLocation, IN ULONG RomSize, OUT PULONG BytesRead)
 
static VOID InitRomRange (IN PCALLBACK16 Context, IN ULONG Start, IN ULONG End, IN ULONG Increment)
 
BOOLEAN WriteProtectRom (IN PVOID RomLocation, IN ULONG RomSize)
 
BOOLEAN WriteUnProtectRom (IN PVOID RomLocation, IN ULONG RomSize)
 
UCHAR CalcRomChecksum (IN ULONG RomLocation, IN ULONG RomSize)
 
BOOLEAN LoadBios (IN PCSTR BiosFileName, OUT PVOID *BiosLocation OPTIONAL, OUT PULONG BiosSize OPTIONAL)
 
BOOLEAN LoadRom (IN PCSTR RomFileName, IN PVOID RomLocation, OUT PULONG RomSize OPTIONAL)
 
VOID SearchAndInitRoms (IN PCALLBACK16 Context)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file rom.c.

Function Documentation

◆ CalcRomChecksum()

UCHAR CalcRomChecksum ( IN ULONG  RomLocation,
IN ULONG  RomSize 
)

Definition at line 153 of file rom.c.

155{
156 ULONG RomLastAddress = RomLocation + RomSize;
157 UCHAR Sum = 0x00; // Using a UCHAR guarantees that we wrap at 0xFF i.e. we do a sum modulo 0x100.
158
159 while (RomLocation < RomLastAddress)
160 {
161 Sum += *(PUCHAR)REAL_TO_PHYS(RomLocation);
162 ++RomLocation;
163 }
164
165 return Sum;
166}
#define REAL_TO_PHYS(ptr)
Definition: emulator.h:37
unsigned char * PUCHAR
Definition: typedefs.h:53
uint32_t ULONG
Definition: typedefs.h:59
unsigned char UCHAR
Definition: xmlstorage.h:181

Referenced by InitRomRange(), and VidBiosInitialize().

◆ InitRomRange()

static VOID InitRomRange ( IN PCALLBACK16  Context,
IN ULONG  Start,
IN ULONG  End,
IN ULONG  Increment 
)
static

Definition at line 87 of file rom.c.

91{
92 ULONG Address, EntryPoint;
93 ULONG RomSize;
94 UCHAR Checksum;
95
96 for (Address = Start; Address < End; Address += Increment)
97 {
98 /* Does the ROM have a valid signature? */
100 {
101 /* Check the control sum of the ROM */
102
103 /*
104 * If this is an adapter ROM (Start: C8000, End: E0000),
105 * its reported size is stored in byte 2 of the ROM.
106 *
107 * If this is an expansion ROM (Start: E0000, End: F0000),
108 * its real length is 64kB.
109 */
110 RomSize = *(PUCHAR)REAL_TO_PHYS(Address + 2) * 512; // Size in blocks of 512 bytes
111 if (Address >= 0xE0000) RomSize = 0x10000;
112
113 Checksum = CalcRomChecksum(Address, RomSize);
114 if (Checksum == 0x00)
115 {
116 EntryPoint = Address + 3;
117 DPRINT1("Going to run @ address 0x%p\n", EntryPoint);
118
119 EntryPoint = MAKELONG((EntryPoint & 0xFFFF), (EntryPoint & 0xF0000) >> 4);
120 // setDS((Address & 0xF0000) >> 4);
121 setDS((Address & 0xFF000) >> 4);
122 RunCallback16(Context, EntryPoint);
123 // Call16((EntryPoint & 0xF0000) >> 4, (EntryPoint & 0xFFFF));
124
125 DPRINT1("ROM @ address 0x%p initialized\n", Address);
126 }
127 else
128 {
129 DPRINT1("ROM @ address 0x%p has invalid checksum of 0x%02x\n", Address, Checksum);
130 }
131 }
132 }
133}
#define DPRINT1
Definition: precomp.h:8
IN OUT PLONG IN OUT PLONG Addend IN OUT PLONG IN LONG Increment
Definition: CrNtStubs.h:46
return pTarget Start()
static WCHAR Address[46]
Definition: ping.c:68
UCHAR CalcRomChecksum(IN ULONG RomLocation, IN ULONG RomSize)
Definition: rom.c:153
#define OPTION_ROM_SIGNATURE
Definition: rom.h:17
VOID RunCallback16(IN PCALLBACK16 Context, IN ULONG FarPtr)
Definition: callback.c:93
uint16_t * PUSHORT
Definition: typedefs.h:56
#define MAKELONG(a, b)
Definition: typedefs.h:249
VOID WINAPI setDS(USHORT)
Definition: registers.c:515

Referenced by SearchAndInitRoms().

◆ LoadBios()

BOOLEAN LoadBios ( IN PCSTR  BiosFileName,
OUT PVOID *BiosLocation  OPTIONAL,
OUT PULONG BiosSize  OPTIONAL 
)

Definition at line 169 of file rom.c.

172{
174 HANDLE hBiosFile;
175 ULONG ulBiosSize = 0;
176 PVOID pBiosLocation;
177
178 /* Open the BIOS image file */
179 hBiosFile = OpenRomFile(BiosFileName, &ulBiosSize);
180
181 /* If we failed, bail out */
182 if (hBiosFile == NULL) return FALSE;
183
184 /* BIOS location needs to be aligned on 32-bit boundary */
185 // (PVOID)((ULONG_PTR)BaseAddress + ROM_AREA_END + 1 - ulBiosSize)
186 pBiosLocation = MEM_ALIGN_DOWN(TO_LINEAR(0xF000, 0xFFFF) + 1 - ulBiosSize, sizeof(ULONG));
187
188 /* Attempt to load the BIOS image file into memory */
189 Success = LoadRomFileByHandle(hBiosFile,
190 pBiosLocation,
191 ulBiosSize,
192 &ulBiosSize);
193 DPRINT1("BIOS loading %s ; GetLastError() = %u\n", Success ? "succeeded" : "failed", GetLastError());
194
195 /* Close the BIOS image file */
196 FileClose(hBiosFile);
197
198 /*
199 * In case of success, write-protect the BIOS location
200 * and return the BIOS location and its size if needed.
201 */
202 if (Success)
203 {
204 WriteProtectRom(pBiosLocation, ulBiosSize);
205
206 if (BiosLocation) *BiosLocation = pBiosLocation;
207 if (BiosSize) *BiosSize = ulBiosSize;
208 }
209
210 return Success;
211}
unsigned char BOOLEAN
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define TO_LINEAR(seg, off)
Definition: emulator.h:26
#define MEM_ALIGN_DOWN(ptr, align)
Definition: emulator.h:23
@ Success
Definition: eventcreate.c:712
static HANDLE OpenRomFile(IN PCSTR RomFileName, OUT PULONG RomSize OPTIONAL)
Definition: rom.c:32
BOOLEAN WriteProtectRom(IN PVOID RomLocation, IN ULONG RomSize)
Definition: rom.c:138
static BOOLEAN LoadRomFileByHandle(IN HANDLE RomFileHandle, IN PVOID RomLocation, IN ULONG RomSize, OUT PULONG BytesRead)
Definition: rom.c:63
VOID FileClose(IN HANDLE FileHandle)
Definition: utils.c:21
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

Referenced by BiosInitialize().

◆ LoadRom()

BOOLEAN LoadRom ( IN PCSTR  RomFileName,
IN PVOID  RomLocation,
OUT PULONG RomSize  OPTIONAL 
)

Definition at line 214 of file rom.c.

217{
219 HANDLE hRomFile;
220 ULONG ulRomSize = 0;
221
222 /* Open the ROM image file */
223 hRomFile = OpenRomFile(RomFileName, &ulRomSize);
224
225 /* If we failed, bail out */
226 if (hRomFile == NULL) return FALSE;
227
228 /* Attempt to load the ROM image file into memory */
229 Success = LoadRomFileByHandle(hRomFile,
230 RomLocation,
231 ulRomSize,
232 &ulRomSize);
233 DPRINT1("ROM loading %s ; GetLastError() = %u\n", Success ? "succeeded" : "failed", GetLastError());
234
235 /* Close the ROM image file and return */
236 FileClose(hRomFile);
237
238 /*
239 * In case of success, write-protect the ROM location
240 * and return the ROM size if needed.
241 */
242 if (Success)
243 {
244 WriteProtectRom(RomLocation, ulRomSize);
245 if (RomSize) *RomSize = ulRomSize;
246 }
247
248 return Success;
249}

Referenced by BiosInitialize().

◆ LoadRomFileByHandle()

static BOOLEAN LoadRomFileByHandle ( IN HANDLE  RomFileHandle,
IN PVOID  RomLocation,
IN ULONG  RomSize,
OUT PULONG  BytesRead 
)
static

Definition at line 63 of file rom.c.

67{
68 /*
69 * The size of the ROM image file is at most 256kB. For instance,
70 * the SeaBIOS image, which includes also expansion ROMs inside it,
71 * covers the range C000:0000 to F000:FFFF .
72 */
73 if (RomSize > 0x40000)
74 {
75 DPRINT1("ROM image size 0x%lx too large, expected at most 0x40000 (256kB)\n", RomSize);
76 return FALSE;
77 }
78
79 /* Attempt to load the ROM image file into memory */
80 return FileLoadByHandle(RomFileHandle,
81 REAL_TO_PHYS(RomLocation),
82 RomSize,
83 BytesRead);
84}
BOOLEAN FileLoadByHandle(IN HANDLE FileHandle, IN PVOID Location, IN ULONG FileSize, OUT PULONG BytesRead)
Definition: utils.c:69
_Must_inspect_result_ _In_ WDFIOTARGET _In_opt_ WDFREQUEST _In_opt_ PWDF_MEMORY_DESCRIPTOR _In_opt_ PLONGLONG _In_opt_ PWDF_REQUEST_SEND_OPTIONS _Out_opt_ PULONG_PTR BytesRead
Definition: wdfiotarget.h:870

Referenced by LoadBios(), and LoadRom().

◆ OpenRomFile()

static HANDLE OpenRomFile ( IN PCSTR  RomFileName,
OUT PULONG RomSize  OPTIONAL 
)
static

Definition at line 32 of file rom.c.

34{
35 HANDLE hRomFile;
36 ULONG ulRomSize = 0;
37
38 /* Open the ROM image file */
39 hRomFile = FileOpen(RomFileName, &ulRomSize);
40
41 /* If we failed, bail out */
42 if (hRomFile == NULL) return NULL;
43
44 /*
45 * The size of the ROM image file is at most 256kB. For instance,
46 * the SeaBIOS image, which includes also expansion ROMs inside it,
47 * covers the range C000:0000 to F000:FFFF .
48 */
49 if (ulRomSize > 0x40000)
50 {
51 /* We failed, bail out */
52 DPRINT1("ROM image size 0x%lx too large, expected at most 0x40000 (256kB)\n", ulRomSize);
53 FileClose(hRomFile);
54 return NULL;
55 }
56
57 /* Success, return file handle and size if needed */
58 if (RomSize) *RomSize = ulRomSize;
59 return hRomFile;
60}
HANDLE FileOpen(IN PCSTR FileName, OUT PULONG FileSize OPTIONAL)
Definition: utils.c:27

Referenced by LoadBios(), and LoadRom().

◆ SearchAndInitRoms()

VOID SearchAndInitRoms ( IN PCALLBACK16  Context)

Definition at line 252 of file rom.c.

253{
254 /* Video ROMs -- Start: C0000, End: C8000, 2kB blocks */
255 InitRomRange(Context, 0xC0000, 0xC8000, 0x0800);
256
257 /* Adapters ROMs -- Start: C8000, End: E0000, 2kB blocks */
258 InitRomRange(Context, 0xC8000, 0xE0000, 0x0800);
259
260 /* Expansion ROM -- Start: E0000, End: F0000, 64kB block */
261 InitRomRange(Context, 0xE0000, 0xEFFFF, 0x10000);
262}
static VOID InitRomRange(IN PCALLBACK16 Context, IN ULONG Start, IN ULONG End, IN ULONG Increment)
Definition: rom.c:87

Referenced by Bios32Post().

◆ ShadowRomWrite()

static BOOLEAN FASTCALL ShadowRomWrite ( ULONG  Address,
PVOID  Buffer,
ULONG  Size 
)
static

Definition at line 25 of file rom.c.

26{
27 /* Prevent writing to ROM */
28 return FALSE;
29}

Referenced by WriteProtectRom().

◆ WriteProtectRom()

BOOLEAN WriteProtectRom ( IN PVOID  RomLocation,
IN ULONG  RomSize 
)

Definition at line 138 of file rom.c.

140{
141 return MemInstallFastMemoryHook(RomLocation, RomSize,
143}
static BOOLEAN FASTCALL ShadowRomWrite(ULONG Address, PVOID Buffer, ULONG Size)
Definition: rom.c:25
BOOL MemInstallFastMemoryHook(PVOID Address, ULONG Size, PMEMORY_READ_HANDLER ReadHandler, PMEMORY_WRITE_HANDLER WriteHandler)
Definition: memory.c:299

Referenced by Bios32Initialize(), LoadBios(), LoadRom(), and VidBiosInitialize().

◆ WriteUnProtectRom()

BOOLEAN WriteUnProtectRom ( IN PVOID  RomLocation,
IN ULONG  RomSize 
)

Definition at line 146 of file rom.c.

148{
149 return MemRemoveFastMemoryHook(RomLocation, RomSize);
150}
BOOL MemRemoveFastMemoryHook(PVOID Address, ULONG Size)
Definition: memory.c:353