1 |
/* PCSX2 - PS2 Emulator for PCs |
2 |
* Copyright (C) 2002-2010 PCSX2 Dev Team |
3 |
* |
4 |
* PCSX2 is free software: you can redistribute it and/or modify it under the terms |
5 |
* of the GNU Lesser General Public License as published by the Free Software Found- |
6 |
* ation, either version 3 of the License, or (at your option) any later version. |
7 |
* |
8 |
* PCSX2 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
9 |
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
10 |
* PURPOSE. See the GNU General Public License for more details. |
11 |
* |
12 |
* You should have received a copy of the GNU General Public License along with PCSX2. |
13 |
* If not, see <http://www.gnu.org/licenses/>. |
14 |
*/ |
15 |
|
16 |
#include "PrecompiledHeader.h" |
17 |
#include "Common.h" |
18 |
#include "BiosTools.h" |
19 |
|
20 |
#include "wx/file.h" |
21 |
|
22 |
#include "AppConfig.h" |
23 |
|
24 |
#define DIRENTRY_SIZE 16 |
25 |
|
26 |
// -------------------------------------------------------------------------------------- |
27 |
// romdir structure (packing required!) |
28 |
// -------------------------------------------------------------------------------------- |
29 |
// |
30 |
#if defined(_MSC_VER) |
31 |
# pragma pack(1) |
32 |
#endif |
33 |
|
34 |
struct romdir |
35 |
{ |
36 |
char fileName[10]; |
37 |
u16 extInfoSize; |
38 |
u32 fileSize; |
39 |
} __packed; // +22 |
40 |
|
41 |
#ifdef _MSC_VER |
42 |
# pragma pack() |
43 |
#endif |
44 |
|
45 |
u32 BiosVersion; // Used in Memory, Misc, CDVD |
46 |
|
47 |
// -------------------------------------------------------------------------------------- |
48 |
// Exception::BiosLoadFailed (implementations) |
49 |
// -------------------------------------------------------------------------------------- |
50 |
Exception::BiosLoadFailed::BiosLoadFailed( const wxString& filename ) |
51 |
{ |
52 |
m_message_diag = L"BIOS has not been configured, or the configuration has been corrupted."; |
53 |
m_message_user = _("The PS2 BIOS could not be loaded. The BIOS has not been configured, or the configuration has been corrupted. Please re-configure."); |
54 |
|
55 |
StreamName = filename; |
56 |
} |
57 |
|
58 |
// Returns the version information of the bios currently loaded into memory. |
59 |
static u32 GetBiosVersion() |
60 |
{ |
61 |
int i; |
62 |
|
63 |
for (i=0; i<512*1024; i++) |
64 |
{ |
65 |
if( strncmp( (char*)&psRu8(i), "RESET", 5 ) == 0 ) |
66 |
break; /* found romdir */ |
67 |
} |
68 |
if (i == 512*1024) return -1; |
69 |
|
70 |
const romdir* rd = (romdir*)&psRu8(i); |
71 |
uint fileOffset = 0; |
72 |
|
73 |
while(strlen(rd->fileName) > 0) |
74 |
{ |
75 |
if (strcmp(rd->fileName, "ROMVER") == 0) |
76 |
{ |
77 |
char vermaj[8]; |
78 |
char vermin[8]; |
79 |
u32 version; |
80 |
|
81 |
const s8 *ROMVER = &psRs8(fileOffset); |
82 |
|
83 |
strncpy(vermaj, (char *)(ROMVER+ 0), 2); vermaj[2] = 0; |
84 |
strncpy(vermin, (char *)(ROMVER+ 2), 2); vermin[2] = 0; |
85 |
version = strtol(vermaj, (char**)NULL, 0) << 8; |
86 |
version|= strtol(vermin, (char**)NULL, 0); |
87 |
|
88 |
return version; |
89 |
} |
90 |
|
91 |
if ((rd->fileSize % 0x10)==0) |
92 |
fileOffset += rd->fileSize; |
93 |
else |
94 |
fileOffset += (rd->fileSize + 0x10) & 0xfffffff0; |
95 |
|
96 |
rd++; |
97 |
} |
98 |
|
99 |
return -1; |
100 |
} |
101 |
|
102 |
// Attempts to load a BIOS rom sub-component, by trying multiple combinations of base |
103 |
// filename and extension. The bios specified in the user's configuration is used as |
104 |
// the base. |
105 |
// |
106 |
// Parameters: |
107 |
// ext - extension of the sub-component to load. Valid options are rom1, rom2, AND erom. |
108 |
// |
109 |
static void loadBiosRom( const wxChar *ext, u8 *dest, s64 maxSize ) |
110 |
{ |
111 |
wxString Bios1; |
112 |
s64 filesize; |
113 |
|
114 |
// Try first a basic extension concatenation (normally results in something like name.bin.rom1) |
115 |
const wxString Bios( g_Conf->FullpathToBios() ); |
116 |
Bios1.Printf( L"%s.%s", Bios.c_str(), ext); |
117 |
|
118 |
if( (filesize=Path::GetFileSize( Bios1 ) ) <= 0 ) |
119 |
{ |
120 |
// Try the name properly extensioned next (name.rom1) |
121 |
Bios1 = Path::ReplaceExtension( Bios, ext ); |
122 |
if( (filesize=Path::GetFileSize( Bios1 ) ) <= 0 ) |
123 |
{ |
124 |
Console.Warning( L"Load Bios Warning: %s module not found, skipping... (this is not an error)", ext ); |
125 |
return; |
126 |
} |
127 |
} |
128 |
|
129 |
// if we made it this far, we have a successful file found: |
130 |
|
131 |
wxFile fp( Bios1 ); |
132 |
fp.Read( dest, min( maxSize, filesize ) ); |
133 |
} |
134 |
|
135 |
// Loads the configured bios rom file into PS2 memory. PS2 memory must be allocated prior to |
136 |
// this method being called. |
137 |
// |
138 |
// Remarks: |
139 |
// This function does not fail if rom1, rom2, or erom files are missing, since none are |
140 |
// explicitly required for most emulation tasks. |
141 |
// |
142 |
// Exceptions: |
143 |
// FileNotFound - Thrown if the primary bios file (usually .bin) is not found. |
144 |
// |
145 |
void LoadBIOS() |
146 |
{ |
147 |
pxAssertDev( eeMem->ROM != NULL, "PS2 system memory has not been initialized yet." ); |
148 |
|
149 |
wxString Bios( g_Conf->FullpathToBios() ); |
150 |
if( !g_Conf->BaseFilenames.Bios.IsOk() || g_Conf->BaseFilenames.Bios.IsDir() ) |
151 |
throw Exception::BiosLoadFailed( Bios ); |
152 |
|
153 |
s64 filesize = Path::GetFileSize( Bios ); |
154 |
if( filesize <= 0 ) |
155 |
{ |
156 |
throw Exception::BiosLoadFailed( Bios ) |
157 |
.SetDiagMsg(L"Configured BIOS file does not exist.") |
158 |
.SetUserMsg(_("The configured BIOS file does not exist. Please re-configure.")); |
159 |
} |
160 |
|
161 |
wxFile fp( Bios ); |
162 |
fp.Read( eeMem->ROM, min( (s64)Ps2MemSize::Rom, filesize ) ); |
163 |
|
164 |
BiosVersion = GetBiosVersion(); |
165 |
if( BiosVersion == -1 ) |
166 |
{ |
167 |
throw Exception::BiosLoadFailed( Bios ) |
168 |
.SetDiagMsg(L"Configured BIOS file is not a valid PS2 BIOS.") |
169 |
.SetUserMsg(_("The configured BIOS file is not a valid PS2 BIOS. Please re-configure.")); |
170 |
} |
171 |
|
172 |
Console.WriteLn("Bios Version %d.%d", BiosVersion >> 8, BiosVersion & 0xff); |
173 |
|
174 |
//injectIRX("host.irx"); //not fully tested; still buggy |
175 |
|
176 |
loadBiosRom( L"rom1", eeMem->ROM1, Ps2MemSize::Rom1 ); |
177 |
loadBiosRom( L"rom2", eeMem->ROM2, Ps2MemSize::Rom2 ); |
178 |
loadBiosRom( L"erom", eeMem->EROM, Ps2MemSize::ERom ); |
179 |
} |
180 |
|
181 |
bool IsBIOS(const wxString& filename, wxString& description) |
182 |
{ |
183 |
uint fileOffset=0; |
184 |
romdir rd; |
185 |
|
186 |
wxFileName Bios( g_Conf->Folders.Bios + filename ); |
187 |
wxFile fp( Bios.GetFullPath() ); |
188 |
|
189 |
if( !fp.IsOpened() ) return FALSE; |
190 |
|
191 |
int biosFileSize = fp.Length(); |
192 |
if( biosFileSize < 1024*4096) return FALSE; // valid bios must be at least 4mb. |
193 |
|
194 |
while( (fp.Tell() < 512*1024) && (fp.Read( &rd, DIRENTRY_SIZE ) == DIRENTRY_SIZE) ) |
195 |
{ |
196 |
if (strcmp(rd.fileName, "RESET") == 0) |
197 |
break; // found romdir |
198 |
} |
199 |
|
200 |
if ((strcmp(rd.fileName, "RESET") != 0) || (rd.fileSize == 0)) |
201 |
return FALSE; //Unable to locate ROMDIR structure in file or a ioprpXXX.img |
202 |
|
203 |
bool found = false; |
204 |
|
205 |
while(strlen(rd.fileName) > 0) |
206 |
{ |
207 |
if (strcmp(rd.fileName, "ROMVER") == 0) // found romver |
208 |
{ |
209 |
char aROMVER[14+1]; // ascii version loaded from disk. |
210 |
|
211 |
uint filepos = fp.Tell(); |
212 |
fp.Seek( fileOffset ); |
213 |
if( fp.Read( &aROMVER, 14 ) == 0 ) break; |
214 |
fp.Seek( filepos ); //go back |
215 |
|
216 |
aROMVER[14] = 0; |
217 |
|
218 |
const char zonefail[2] = { aROMVER[4], '\0' }; // the default "zone" (unknown code) |
219 |
const char* zone = zonefail; |
220 |
|
221 |
switch(aROMVER[4]) |
222 |
{ |
223 |
case 'T': zone = "T10K"; break; |
224 |
case 'X': zone = "Test"; break; |
225 |
case 'J': zone = "Japan"; break; |
226 |
case 'A': zone = "USA"; break; |
227 |
case 'E': zone = "Europe"; break; |
228 |
case 'H': zone = "HK"; break; |
229 |
case 'P': zone = "Free"; break; |
230 |
case 'C': zone = "China"; break; |
231 |
} |
232 |
|
233 |
const wxString romver( fromUTF8(aROMVER) ); |
234 |
|
235 |
description.Printf( L"%-7s v%c%c.%c%c(%c%c/%c%c/%c%c%c%c) %s", |
236 |
fromUTF8(zone).c_str(), |
237 |
romver[0], romver[1], // ver major |
238 |
romver[2], romver[3], // ver minor |
239 |
romver[12], romver[13], // day |
240 |
romver[10], romver[11], // month |
241 |
romver[6], romver[7], romver[8], romver[9], // year! |
242 |
(aROMVER[5]=='C') ? L"Console" : (aROMVER[5]=='D') ? L"Devel" : wxEmptyString |
243 |
); |
244 |
found = true; |
245 |
} |
246 |
|
247 |
if ((rd.fileSize % 0x10)==0) |
248 |
fileOffset += rd.fileSize; |
249 |
else |
250 |
fileOffset += (rd.fileSize + 0x10) & 0xfffffff0; |
251 |
|
252 |
if (fp.Read( &rd, DIRENTRY_SIZE ) != DIRENTRY_SIZE) break; |
253 |
} |
254 |
fileOffset -= ((rd.fileSize + 0x10) & 0xfffffff0) - rd.fileSize; |
255 |
|
256 |
if (found) |
257 |
{ |
258 |
if ( biosFileSize < (int)fileOffset) |
259 |
{ |
260 |
description += wxsFormat( L" %d%%", ((biosFileSize*100) / (int)fileOffset) ); |
261 |
// we force users to have correct bioses, |
262 |
// not that lame scph10000 of 513KB ;-) |
263 |
} |
264 |
return true; |
265 |
} |
266 |
|
267 |
return false; //fail quietly |
268 |
} |