Commit | Line | Data |
---|---|---|
ef79bbde P |
1 | /* PPF Patch Support for PCSX-Reloaded |
2 | * Copyright (c) 2009, Wei Mingzhi <whistler_wmz@users.sf.net>. | |
3 | * | |
4 | * Based on P.E.Op.S CDR Plugin by Pete Bernert. | |
5 | * Copyright (c) 2002, Pete Bernert. | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
22 | #include "psxcommon.h" | |
23 | #include "ppf.h" | |
584fc319 | 24 | #include "misc.h" |
ef79bbde P |
25 | #include "cdrom.h" |
26 | ||
27 | typedef struct tagPPF_DATA { | |
28 | s32 addr; | |
29 | s32 pos; | |
30 | s32 anz; | |
31 | struct tagPPF_DATA *pNext; | |
32 | } PPF_DATA; | |
33 | ||
34 | typedef struct tagPPF_CACHE { | |
35 | s32 addr; | |
36 | struct tagPPF_DATA *pNext; | |
37 | } PPF_CACHE; | |
38 | ||
39 | static PPF_CACHE *ppfCache = NULL; | |
40 | static PPF_DATA *ppfHead = NULL, *ppfLast = NULL; | |
41 | static int iPPFNum = 0; | |
42 | ||
43 | // using a linked data list, and address array | |
44 | static void FillPPFCache() { | |
45 | PPF_DATA *p; | |
46 | PPF_CACHE *pc; | |
47 | s32 lastaddr; | |
48 | ||
49 | p = ppfHead; | |
50 | lastaddr = -1; | |
51 | iPPFNum = 0; | |
52 | ||
53 | while (p != NULL) { | |
54 | if (p->addr != lastaddr) iPPFNum++; | |
55 | lastaddr = p->addr; | |
56 | p = p->pNext; | |
57 | } | |
58 | ||
59 | if (iPPFNum <= 0) return; | |
60 | ||
61 | pc = ppfCache = (PPF_CACHE *)malloc(iPPFNum * sizeof(PPF_CACHE)); | |
ab91a058 | 62 | if (pc == NULL) return; |
ef79bbde P |
63 | |
64 | iPPFNum--; | |
65 | p = ppfHead; | |
66 | lastaddr = -1; | |
67 | ||
68 | while (p != NULL) { | |
69 | if (p->addr != lastaddr) { | |
70 | pc->addr = p->addr; | |
71 | pc->pNext = p; | |
72 | pc++; | |
73 | } | |
74 | lastaddr = p->addr; | |
75 | p = p->pNext; | |
76 | } | |
77 | } | |
78 | ||
79 | void FreePPFCache() { | |
80 | PPF_DATA *p = ppfHead; | |
81 | void *pn; | |
82 | ||
83 | while (p != NULL) { | |
84 | pn = p->pNext; | |
85 | free(p); | |
86 | p = (PPF_DATA *)pn; | |
87 | } | |
88 | ppfHead = NULL; | |
89 | ppfLast = NULL; | |
90 | ||
91 | if (ppfCache != NULL) free(ppfCache); | |
92 | ppfCache = NULL; | |
93 | } | |
94 | ||
95 | void CheckPPFCache(unsigned char *pB, unsigned char m, unsigned char s, unsigned char f) { | |
96 | PPF_CACHE *pcstart, *pcend, *pcpos; | |
97 | int addr = MSF2SECT(btoi(m), btoi(s), btoi(f)), pos, anz, start; | |
98 | ||
99 | if (ppfCache == NULL) return; | |
100 | ||
101 | pcstart = ppfCache; | |
102 | if (addr < pcstart->addr) return; | |
103 | pcend = ppfCache + iPPFNum; | |
104 | if (addr > pcend->addr) return; | |
105 | ||
106 | while (1) { | |
107 | if (addr == pcend->addr) { pcpos = pcend; break; } | |
108 | ||
109 | pcpos = pcstart + (pcend - pcstart) / 2; | |
110 | if (pcpos == pcstart) break; | |
111 | if (addr < pcpos->addr) { | |
112 | pcend = pcpos; | |
113 | continue; | |
114 | } | |
115 | if (addr > pcpos->addr) { | |
116 | pcstart = pcpos; | |
117 | continue; | |
118 | } | |
119 | break; | |
120 | } | |
121 | ||
122 | if (addr == pcpos->addr) { | |
123 | PPF_DATA *p = pcpos->pNext; | |
124 | while (p != NULL && p->addr == addr) { | |
125 | pos = p->pos - (CD_FRAMESIZE_RAW - DATA_SIZE); | |
126 | anz = p->anz; | |
127 | if (pos < 0) { start = -pos; pos = 0; anz -= start; } | |
128 | else start = 0; | |
129 | memcpy(pB + pos, (unsigned char *)(p + 1) + start, anz); | |
130 | p = p->pNext; | |
131 | } | |
132 | } | |
133 | } | |
134 | ||
135 | static void AddToPPF(s32 ladr, s32 pos, s32 anz, unsigned char *ppfmem) { | |
136 | if (ppfHead == NULL) { | |
137 | ppfHead = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz); | |
ab91a058 | 138 | if (ppfHead == NULL) return; |
ef79bbde P |
139 | ppfHead->addr = ladr; |
140 | ppfHead->pNext = NULL; | |
141 | ppfHead->pos = pos; | |
142 | ppfHead->anz = anz; | |
143 | memcpy(ppfHead + 1, ppfmem, anz); | |
144 | iPPFNum = 1; | |
145 | ppfLast = ppfHead; | |
146 | } else { | |
147 | PPF_DATA *p = ppfHead; | |
148 | PPF_DATA *plast = NULL; | |
149 | PPF_DATA *padd; | |
150 | ||
151 | if (ladr > ppfLast->addr || (ladr == ppfLast->addr && pos > ppfLast->pos)) { | |
152 | p = NULL; | |
153 | plast = ppfLast; | |
154 | } else { | |
155 | while (p != NULL) { | |
156 | if (ladr < p->addr) break; | |
157 | if (ladr == p->addr) { | |
158 | while (p && ladr == p->addr && pos > p->pos) { | |
159 | plast = p; | |
160 | p = p->pNext; | |
161 | } | |
162 | break; | |
163 | } | |
164 | plast = p; | |
165 | p = p->pNext; | |
166 | } | |
167 | } | |
168 | ||
169 | padd = (PPF_DATA *)malloc(sizeof(PPF_DATA) + anz); | |
ab91a058 | 170 | if (padd == NULL) return; |
ef79bbde P |
171 | padd->addr = ladr; |
172 | padd->pNext = p; | |
173 | padd->pos = pos; | |
174 | padd->anz = anz; | |
175 | memcpy(padd + 1, ppfmem, anz); | |
176 | iPPFNum++; | |
177 | if (plast == NULL) ppfHead = padd; | |
178 | else plast->pNext = padd; | |
179 | ||
180 | if (padd->pNext == NULL) ppfLast = padd; | |
181 | } | |
182 | } | |
183 | ||
184 | void BuildPPFCache() { | |
185 | FILE *ppffile; | |
186 | char buffer[12]; | |
187 | char method, undo = 0, blockcheck = 0; | |
188 | int dizlen, dizyn; | |
189 | unsigned char ppfmem[512]; | |
13fab97c | 190 | char szPPF[MAXPATHLEN * 2]; |
ef79bbde P |
191 | int count, seekpos, pos; |
192 | u32 anz; // use 32-bit to avoid stupid overflows | |
193 | s32 ladr, off, anx; | |
194 | ||
195 | FreePPFCache(); | |
196 | ||
4e44d6f6 | 197 | if (CdromId[0] == '\0') return; |
198 | ||
ef79bbde P |
199 | // Generate filename in the format of SLUS_123.45 |
200 | buffer[0] = toupper(CdromId[0]); | |
201 | buffer[1] = toupper(CdromId[1]); | |
202 | buffer[2] = toupper(CdromId[2]); | |
203 | buffer[3] = toupper(CdromId[3]); | |
204 | buffer[4] = '_'; | |
205 | buffer[5] = CdromId[4]; | |
206 | buffer[6] = CdromId[5]; | |
207 | buffer[7] = CdromId[6]; | |
208 | buffer[8] = '.'; | |
209 | buffer[9] = CdromId[7]; | |
210 | buffer[10] = CdromId[8]; | |
211 | buffer[11] = '\0'; | |
212 | ||
213 | sprintf(szPPF, "%s%s", Config.PatchesDir, buffer); | |
214 | ||
215 | ppffile = fopen(szPPF, "rb"); | |
216 | if (ppffile == NULL) return; | |
217 | ||
218 | memset(buffer, 0, 5); | |
5b568098 | 219 | if (fread(buffer, 1, 3, ppffile) != 3) |
9361a5aa | 220 | goto fail_io; |
ef79bbde P |
221 | |
222 | if (strcmp(buffer, "PPF") != 0) { | |
223 | SysPrintf(_("Invalid PPF patch: %s.\n"), szPPF); | |
224 | fclose(ppffile); | |
225 | return; | |
226 | } | |
227 | ||
228 | fseek(ppffile, 5, SEEK_SET); | |
229 | method = fgetc(ppffile); | |
230 | ||
231 | switch (method) { | |
232 | case 0: // ppf1 | |
233 | fseek(ppffile, 0, SEEK_END); | |
234 | count = ftell(ppffile); | |
235 | count -= 56; | |
236 | seekpos = 56; | |
237 | break; | |
238 | ||
239 | case 1: // ppf2 | |
240 | fseek(ppffile, -8, SEEK_END); | |
241 | ||
242 | memset(buffer, 0, 5); | |
5b568098 | 243 | if (fread(buffer, 1, 4, ppffile) != 4) |
9361a5aa | 244 | goto fail_io; |
ef79bbde P |
245 | |
246 | if (strcmp(".DIZ", buffer) != 0) { | |
247 | dizyn = 0; | |
248 | } else { | |
5b568098 | 249 | if (fread(&dizlen, 1, 4, ppffile) != 4) |
9361a5aa | 250 | goto fail_io; |
ef79bbde P |
251 | dizlen = SWAP32(dizlen); |
252 | dizyn = 1; | |
253 | } | |
254 | ||
255 | fseek(ppffile, 0, SEEK_END); | |
256 | count = ftell(ppffile); | |
257 | ||
258 | if (dizyn == 0) { | |
259 | count -= 1084; | |
260 | seekpos = 1084; | |
261 | } else { | |
262 | count -= 1084; | |
263 | count -= 38; | |
264 | count -= dizlen; | |
265 | seekpos = 1084; | |
266 | } | |
267 | break; | |
268 | ||
269 | case 2: // ppf3 | |
270 | fseek(ppffile, 57, SEEK_SET); | |
271 | blockcheck = fgetc(ppffile); | |
272 | undo = fgetc(ppffile); | |
273 | ||
274 | fseek(ppffile, -6, SEEK_END); | |
275 | memset(buffer, 0, 5); | |
5b568098 | 276 | if (fread(buffer, 1, 4, ppffile) != 4) |
9361a5aa | 277 | goto fail_io; |
ef79bbde P |
278 | dizlen = 0; |
279 | ||
280 | if (strcmp(".DIZ", buffer) == 0) { | |
281 | fseek(ppffile, -2, SEEK_END); | |
9361a5aa | 282 | // TODO: Endian/size unsafe? |
5b568098 | 283 | if (fread(&dizlen, 1, 2, ppffile) != 2) |
9361a5aa | 284 | goto fail_io; |
ef79bbde P |
285 | dizlen = SWAP32(dizlen); |
286 | dizlen += 36; | |
287 | } | |
288 | ||
289 | fseek(ppffile, 0, SEEK_END); | |
290 | count = ftell(ppffile); | |
291 | count -= dizlen; | |
292 | ||
293 | if (blockcheck) { | |
294 | seekpos = 1084; | |
295 | count -= 1084; | |
296 | } else { | |
297 | seekpos = 60; | |
298 | count -= 60; | |
299 | } | |
300 | break; | |
301 | ||
302 | default: | |
303 | fclose(ppffile); | |
304 | SysPrintf(_("Unsupported PPF version (%d).\n"), method + 1); | |
305 | return; | |
306 | } | |
307 | ||
308 | // now do the data reading | |
309 | do { | |
310 | fseek(ppffile, seekpos, SEEK_SET); | |
5b568098 | 311 | if (fread(&pos, 1, sizeof(pos), ppffile) != sizeof(pos)) |
9361a5aa | 312 | goto fail_io; |
ef79bbde P |
313 | pos = SWAP32(pos); |
314 | ||
9361a5aa EC |
315 | if (method == 2) { |
316 | // skip 4 bytes on ppf3 (no int64 support here) | |
5b568098 | 317 | if (fread(buffer, 1, 4, ppffile) != 4) |
9361a5aa EC |
318 | goto fail_io; |
319 | } | |
ef79bbde P |
320 | |
321 | anz = fgetc(ppffile); | |
5b568098 | 322 | if (fread(ppfmem, 1, anz, ppffile) != anz) |
9361a5aa | 323 | goto fail_io; |
ef79bbde P |
324 | |
325 | ladr = pos / CD_FRAMESIZE_RAW; | |
326 | off = pos % CD_FRAMESIZE_RAW; | |
327 | ||
328 | if (off + anz > CD_FRAMESIZE_RAW) { | |
329 | anx = off + anz - CD_FRAMESIZE_RAW; | |
330 | anz -= (unsigned char)anx; | |
331 | AddToPPF(ladr + 1, 0, anx, &ppfmem[anz]); | |
332 | } | |
333 | ||
334 | AddToPPF(ladr, off, anz, ppfmem); // add to link list | |
335 | ||
336 | if (method == 2) { | |
337 | if (undo) anz += anz; | |
338 | anz += 4; | |
339 | } | |
340 | ||
341 | seekpos = seekpos + 5 + anz; | |
342 | count = count - 5 - anz; | |
343 | } while (count != 0); // loop til end | |
344 | ||
345 | fclose(ppffile); | |
346 | ||
347 | FillPPFCache(); // build address array | |
348 | ||
349 | SysPrintf(_("Loaded PPF %d.0 patch: %s.\n"), method + 1, szPPF); | |
9361a5aa EC |
350 | |
351 | fail_io: | |
352 | #ifndef NDEBUG | |
353 | SysPrintf(_("File IO error in <%s:%s>.\n"), __FILE__, __func__); | |
354 | #endif | |
355 | fclose(ppffile); | |
ef79bbde | 356 | } |
ae4e7dc9 | 357 | |
358 | // redump.org SBI files, slightly different handling from PCSX-Reloaded | |
359 | unsigned char *sbi_sectors; | |
cdee2d81 | 360 | int sbi_len; |
ae4e7dc9 | 361 | |
362 | int LoadSBI(const char *fname, int sector_count) { | |
4ae294f0 | 363 | int good_sectors = 0; |
364 | int clean_eof = 0; | |
ab948f7e | 365 | char buffer[16]; |
ae4e7dc9 | 366 | FILE *sbihandle; |
eaa895dc | 367 | u8 sbitime[3], t; |
ae4e7dc9 | 368 | int s; |
369 | ||
370 | sbihandle = fopen(fname, "rb"); | |
371 | if (sbihandle == NULL) | |
372 | return -1; | |
373 | ||
cdee2d81 | 374 | sbi_len = (sector_count + 7) / 8; |
375 | sbi_sectors = calloc(1, sbi_len); | |
4ae294f0 | 376 | if (sbi_sectors == NULL) |
377 | goto end; | |
ae4e7dc9 | 378 | |
379 | // 4-byte SBI header | |
9361a5aa | 380 | if (fread(buffer, 1, 4, sbihandle) != 4) |
4ae294f0 | 381 | goto end; |
9361a5aa | 382 | |
eaa895dc | 383 | while (1) { |
384 | s = fread(sbitime, 1, 3, sbihandle); | |
385 | if (s != 3) | |
4ae294f0 | 386 | { |
387 | if (s == 0) | |
388 | clean_eof = 1; | |
389 | break; | |
390 | } | |
391 | s = MSF2SECT(btoi(sbitime[0]), btoi(sbitime[1]), btoi(sbitime[2])); | |
392 | if (s < sector_count) { | |
393 | sbi_sectors[s >> 3] |= 1 << (s&7); | |
394 | good_sectors++; | |
395 | } | |
396 | else | |
397 | SysPrintf(_("SBI sector %d >= %d?\n"), s, sector_count); | |
398 | ||
399 | // skip to the next record | |
5b568098 | 400 | if (fread(&t, 1, sizeof(t), sbihandle) != sizeof(t)) |
4ae294f0 | 401 | break; |
402 | s = -1; | |
eaa895dc | 403 | switch (t) { |
404 | default: | |
405 | case 1: | |
406 | s = 10; | |
407 | break; | |
408 | case 2: | |
409 | case 3: | |
410 | s = 3; | |
411 | break; | |
412 | } | |
4ae294f0 | 413 | if (s < 0) |
414 | break; | |
415 | if (fseek(sbihandle, s, SEEK_CUR)) | |
416 | break; | |
ae4e7dc9 | 417 | } |
418 | ||
4ae294f0 | 419 | end: |
420 | if (!clean_eof) | |
421 | SysPrintf(_("SBI: parse failure at 0x%lx\n"), ftell(sbihandle)); | |
422 | if (!good_sectors) { | |
423 | free(sbi_sectors); | |
424 | sbi_sectors = NULL; | |
cdee2d81 | 425 | sbi_len = 0; |
4ae294f0 | 426 | } |
9361a5aa | 427 | fclose(sbihandle); |
4ae294f0 | 428 | return sbi_sectors ? 0 : -1; |
ae4e7dc9 | 429 | } |
430 | ||
431 | void UnloadSBI(void) { | |
432 | if (sbi_sectors) { | |
433 | free(sbi_sectors); | |
434 | sbi_sectors = NULL; | |
cdee2d81 | 435 | sbi_len = 0; |
ae4e7dc9 | 436 | } |
437 | } |