2 * COPYRIGHT: GPL - See COPYING in the top level directory
3 * PROJECT: ReactOS Virtual DOS Machine
5 * PURPOSE: ROM Support Functions
6 * PROGRAMMERS: Hermes Belusca-Maito (hermes.belusca@sfr.fr)
9 /* INCLUDES *******************************************************************/
14 #include "cpu/callback.h"
19 /* PRIVATE FUNCTIONS **********************************************************/
22 OpenRomFile(IN PCSTR RomFileName
,
23 OUT PULONG RomSize OPTIONAL
)
28 /* Open the ROM image file */
29 hRomFile
= FileOpen(RomFileName
, &ulRomSize
);
31 /* If we failed, bail out */
32 if (hRomFile
== NULL
) return NULL
;
35 * The size of the ROM image file is at most 256kB. For instance,
36 * the SeaBIOS image, which includes also expansion ROMs inside it,
37 * covers the range C000:0000 to F000:FFFF .
39 if (ulRomSize
> 0x40000)
41 /* We failed, bail out */
42 DPRINT1("ROM image size 0x%lx too large, expected at most 0x40000 (256kB)", ulRomSize
);
47 /* Success, return file handle and size if needed */
48 if (RomSize
) *RomSize
= ulRomSize
;
53 LoadRomFileByHandle(IN HANDLE RomFileHandle
,
59 * The size of the ROM image file is at most 256kB. For instance,
60 * the SeaBIOS image, which includes also expansion ROMs inside it,
61 * covers the range C000:0000 to F000:FFFF .
63 if (RomSize
> 0x40000)
65 DPRINT1("ROM image size 0x%lx too large, expected at most 0x40000 (256kB)", RomSize
);
69 /* Attempt to load the ROM image file into memory */
70 return FileLoadByHandle(RomFileHandle
,
71 REAL_TO_PHYS(RomLocation
),
77 ComputeChecksum(IN ULONG RomLocation
,
80 ULONG RomLastAddress
= RomLocation
+ RomSize
;
81 UCHAR Sum
= 0x00; // Using a UCHAR guarantees that we wrap at 0xFF i.e. we do a sum modulo 0x100.
83 while (RomLocation
< RomLastAddress
)
85 Sum
+= *(PUCHAR
)REAL_TO_PHYS(RomLocation
);
93 InitRomRange(IN PCALLBACK16 Context
,
98 ULONG Address
, AddressBoot
;
102 for (Address
= Start
; Address
< End
; Address
+= Increment
)
104 /* Does the ROM have a valid signature? */
105 if (*(PUSHORT
)REAL_TO_PHYS(Address
) == OPTION_ROM_SIGNATURE
)
107 /* Check the control sum of the ROM */
110 * If this is an adapter ROM (Start: C8000, End: E0000), its
111 * reported size is stored in byte 2 of the ROM.
113 * If this is an expansion ROM (Start: E0000, End: F0000),
114 * its real length is 64kB.
116 RomSize
= *(PUCHAR
)REAL_TO_PHYS(Address
+ 2) * 512;
117 if (Address
>= 0xE0000) RomSize
= 0x10000;
119 Checksum
= ComputeChecksum(Address
, RomSize
);
120 if (Checksum
== 0x00)
122 AddressBoot
= Address
+ 3;
123 DPRINT1("Going to run @ address 0x%p\n", AddressBoot
);
125 AddressBoot
= MAKELONG((AddressBoot
& 0xFFFF), (AddressBoot
& 0xF0000) >> 4);
126 // setDS((Address & 0xF0000) >> 4);
127 setDS((Address
& 0xFF000) >> 4);
128 RunCallback16(Context
, AddressBoot
);
129 // Call16((AddressBoot & 0xF0000) >> 4, (AddressBoot & 0xFFFF));
131 DPRINT1("Rom @ address 0x%p initialized\n", Address
);
135 DPRINT1("Rom @ address 0x%p has invalid checksum of 0x%02x\n", Address
, Checksum
);
141 /* PUBLIC FUNCTIONS ***********************************************************/
144 LoadBios(IN PCSTR BiosFileName
,
145 OUT PVOID
* BiosLocation OPTIONAL
,
146 OUT PULONG BiosSize OPTIONAL
)
150 ULONG ulBiosSize
= 0;
153 /* Open the BIOS image file */
154 hBiosFile
= OpenRomFile(BiosFileName
, &ulBiosSize
);
156 /* If we failed, bail out */
157 if (hBiosFile
== NULL
) return FALSE
;
159 /* BIOS location needs to be aligned on 32-bit boundary */
160 // (PVOID)((ULONG_PTR)BaseAddress + ROM_AREA_END + 1 - ulBiosSize)
161 pBiosLocation
= MEM_ALIGN_DOWN(TO_LINEAR(0xF000, 0xFFFF) + 1 - ulBiosSize
, sizeof(ULONG
));
163 /* Attempt to load the BIOS image file into memory */
164 Success
= LoadRomFileByHandle(hBiosFile
,
168 DPRINT1("BIOS loading %s ; GetLastError() = %u\n", Success
? "succeeded" : "failed", GetLastError());
170 /* Close the BIOS image file */
171 FileClose(hBiosFile
);
173 /* In case of success, return BIOS location and size if needed */
176 if (BiosLocation
) *BiosLocation
= pBiosLocation
;
177 if (BiosSize
) *BiosSize
= ulBiosSize
;
184 LoadRom(IN PCSTR RomFileName
,
185 IN PVOID RomLocation
,
186 OUT PULONG RomSize OPTIONAL
)
192 /* Open the ROM image file */
193 hRomFile
= OpenRomFile(RomFileName
, &ulRomSize
);
195 /* If we failed, bail out */
196 if (hRomFile
== NULL
) return FALSE
;
198 /* Attempt to load the ROM image file into memory */
199 Success
= LoadRomFileByHandle(hRomFile
,
203 DPRINT1("ROM loading %s ; GetLastError() = %u\n", Success
? "succeeded" : "failed", GetLastError());
205 /* Close the ROM image file and return */
208 /* In case of success, return ROM size if needed */
211 if (RomSize
) *RomSize
= ulRomSize
;
218 SearchAndInitRoms(IN PCALLBACK16 Context
)
220 /* Adapters ROMs -- Start: C8000, End: E0000, 2kB blocks */
221 InitRomRange(Context
, 0xC8000, 0xE0000, 0x0800);
223 /* Expansion ROM -- Start: E0000, End: F0000, 64kB block */
224 InitRomRange(Context
, 0xE0000, 0xEFFFF, 0x10000);