Added screenshot feature
[clinton/Virtual-Jaguar-Rx.git] / src / file.cpp
1 //
2 // FILE.CPP
3 //
4 // File support
5 // by James Hammons
6 // (C) 2010 Underground Software
7 //
8 // JLH = James Hammons <jlhamm@acm.org>
9 // JPM = Jean-Paul Mari <djipi.mari@gmail.com>
10 //
11 // Who When What
12 // --- ---------- ------------------------------------------------------------
13 // JLH 01/16/2010 Created this log ;-)
14 // JLH 02/28/2010 Added functions to look inside .ZIP files and handle
15 // contents
16 // JLH 06/01/2012 Added function to check ZIP file CRCs against file DB
17 // JPM 06/06/2016 Visual Studio support
18 // JPM 06/15/2016 ELF format support
19 // JPM 06/19/2016 Soft debugger support
20 // JPM 07/15/2016 DWARF format support
21 //
22
23 #include "file.h"
24 #if defined(_MSC_VER)
25 #include "_MSC_VER/config.h"
26 #endif // _MSC_VER
27 #include <stdarg.h>
28 #include <string.h>
29 #include "crc32.h"
30 #include "filedb.h"
31 #include "eeprom.h"
32 #include "jaguar.h"
33 #include "log.h"
34 #include "memory.h"
35 #include "universalhdr.h"
36 #include "unzip.h"
37 #include "zlib.h"
38 #include "libelf/libelf.h"
39 #include "libelf/gelf.h"
40 #include "libdwarf.h"
41 #include "debugger/ELFManager.h"
42 #include "debugger/DBGManager.h"
43
44
45 // Private function prototypes
46
47 static int gzfilelength(gzFile gd);
48 //#if defined(_MSC_VER) || defined(__MINGW64__)|| defined(__MINGW32__) || defined(__CYGWIN__)
49 static bool CheckExtension(const uint8_t *filename, const char *ext);
50 //#else
51 //static bool CheckExtension(const char * filename, const char * ext);
52 //#endif // _MSC_VER
53 //static int ParseFileType(uint8_t header1, uint8_t header2, uint32_t size);
54
55 // Private variables/enums
56
57
58 //
59 // Generic ROM loading
60 //
61 uint32_t JaguarLoadROM(uint8_t * &rom, char * path)
62 {
63 // We really should have some kind of sanity checking for the ROM size here to prevent
64 // a buffer overflow... !!! FIX !!!
65 #if defined(_MSC_VER)
66 #pragma message("Warning: !!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!")
67 #else
68 #warning "!!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!"
69 #endif // _MSC_VER
70 uint32_t romSize = 0;
71
72 WriteLog("FILE: JaguarLoadROM attempting to load file '%s'...", path);
73 char * ext = strrchr(path, '.');
74
75 // No filename extension == YUO FAIL IT (it is loading the file).
76 // This is naive, but it works. But should probably come up with something a little
77 // more robust, to prevent problems with dopes trying to exploit this.
78 if (ext == NULL)
79 {
80 WriteLog("FAILED!\n");
81 return 0;
82 }
83
84 WriteLog("\nFILE: Succeeded in finding extension (%s)!\n", ext);
85 WriteLog("FILE: Loading \"%s\"...", path);
86
87 if (strcasecmp(ext, ".zip") == 0)
88 {
89 // Handle ZIP file loading here...
90 WriteLog("(ZIPped)...");
91
92 // uint8_t * buffer = NULL;
93 // romSize = GetFileFromZIP(path, FT_SOFTWARE, buffer);
94 romSize = GetFileFromZIP(path, FT_SOFTWARE, rom);
95
96 if (romSize == 0)
97 {
98 WriteLog("Failed!\n");
99 return 0;
100 }
101
102 // memcpy(rom, buffer, romSize);
103 // delete[] buffer;
104 }
105 else
106 {
107 // Handle gzipped files transparently [Adam Green]...
108
109 gzFile fp = gzopen(path, "rb");
110
111 if (fp == NULL)
112 {
113 WriteLog("Failed!\n");
114 return 0;
115 }
116
117 romSize = gzfilelength(fp);
118 rom = new uint8_t[romSize];
119 gzseek(fp, 0, SEEK_SET);
120 gzread(fp, rom, romSize);
121 gzclose(fp);
122 }
123
124 WriteLog("OK (%i bytes)\n", romSize);
125
126 return romSize;
127 }
128
129
130 //
131 // Jaguar file loading
132 // We do a more intelligent file analysis here instead of relying on (possible
133 // false) file extensions which people don't seem to give two shits about
134 // anyway. :-(
135 //
136 bool JaguarLoadFile(char * path)
137 {
138 Elf *ElfMem;
139 GElf_Ehdr ElfEhdr, *PtrGElfEhdr;
140 Elf_Scn *PtrElfScn;
141 Elf_Data *PtrElfData;
142 GElf_Shdr GElfShdr, *PtrGElfShdr;
143 size_t NbrSect;
144 uint8_t *buffer = NULL;
145 char *NameSection;
146 size_t ElfSectionNameType;
147 int DBGType = DBG_NO_TYPE;
148 bool error;
149 int err;
150
151 jaguarROMSize = JaguarLoadROM(buffer, path);
152
153 if (jaguarROMSize == 0)
154 {
155 // It's up to the GUI to report errors, not us. :-)
156 WriteLog("FILE: Could not load ROM from file \"%s\"...\nAborting load!\n", path);
157 return false;
158 }
159
160 jaguarMainROMCRC32 = crc32_calcCheckSum(buffer, jaguarROMSize);
161 WriteLog("CRC: %08X\n", (unsigned int)jaguarMainROMCRC32);
162 // TODO: Check for EEPROM file in ZIP file. If there is no EEPROM in the user's EEPROM
163 // directory, copy the one from the ZIP file, if it exists.
164 EepromInit();
165 jaguarRunAddress = 0x802000; // For non-BIOS runs, this is true
166 int fileType = ParseFileType(buffer, jaguarROMSize);
167 jaguarCartInserted = false;
168 DBGManager_Reset();
169
170 if (fileType == JST_ROM)
171 {
172 jaguarCartInserted = true;
173 memcpy(jagMemSpace + 0x800000, buffer, jaguarROMSize);
174 // Checking something...
175 jaguarRunAddress = GET32(jagMemSpace, 0x800404);
176 WriteLog("FILE: Cartridge run address is reported as $%X...\n", jaguarRunAddress);
177 delete[] buffer;
178 return true;
179 }
180 else if (fileType == JST_ALPINE)
181 {
182 // File extension ".ROM": Alpine image that loads/runs at $802000
183 WriteLog("FILE: Setting up Alpine ROM... Run address: 00802000, length: %08X\n", jaguarROMSize);
184 memset(jagMemSpace + 0x800000, 0xFF, 0x2000);
185 memcpy(jagMemSpace + 0x802000, buffer, jaguarROMSize);
186 delete[] buffer;
187
188 // Maybe instead of this, we could try requiring the STUBULATOR ROM? Just a thought...
189 // Try setting the vector to say, $1000 and putting an instruction there that loops forever:
190 // This kludge works! Yeah!
191 SET32(jaguarMainRAM, 0x10, 0x00001000);
192 SET16(jaguarMainRAM, 0x1000, 0x60FE); // Here: bra Here
193 return true;
194 }
195 else if (fileType == JST_ELF32)
196 {
197 DBGType = DBG_ELF;
198
199 char *PtrELFExe = (char *)ELFManager_ExeCopy(buffer, jaguarROMSize);
200
201 if (PtrELFExe != NULL)
202 {
203 if ((elf_version(EV_CURRENT) != EV_NONE) && (ElfMem = ELFManager_MemOpen(PtrELFExe, jaguarROMSize)))
204 {
205 if (ELFManager_DwarfInit(ElfMem))
206 {
207 DBGType |= DBG_ELFDWARF;
208 }
209
210 if (!elf_getshdrnum(ElfMem, &NbrSect))
211 {
212 if (((PtrGElfEhdr = gelf_getehdr(ElfMem, &ElfEhdr)) != NULL) && ((PtrElfScn = elf_getscn(ElfMem, 0)) != NULL))
213 {
214 for (error = false; (PtrElfScn != NULL) && (error == false); PtrElfScn = elf_nextscn(ElfMem, PtrElfScn))
215 {
216 PtrElfData = NULL;
217
218 if ((PtrGElfShdr = gelf_getshdr(PtrElfScn, &GElfShdr)) == NULL)
219 {
220 error = true;
221 }
222 else
223 {
224 NameSection = elf_strptr(ElfMem, PtrGElfEhdr->e_shstrndx, (size_t)PtrGElfShdr->sh_name);
225 WriteLog("FILE: ELF Section name: %s\n", NameSection);
226
227 if ((ElfSectionNameType = ELFManager_GetSectionType(NameSection)) == ELF_NO_TYPE)
228 {
229 WriteLog("FILE: ELF Section not listed\n");
230 error = true;
231 }
232 else
233 {
234 switch (PtrGElfShdr->sh_type)
235 {
236 case SHT_NULL:
237 break;
238
239 case SHT_PROGBITS:
240 if ((PtrGElfShdr->sh_flags & (SHF_ALLOC | SHF_WRITE | SHF_EXECINSTR)))
241 {
242 if (PtrGElfShdr->sh_addr >= 0x800000)
243 {
244 memcpy(jagMemSpace + PtrGElfShdr->sh_addr, buffer + PtrGElfShdr->sh_offset, PtrGElfShdr->sh_size);
245 //error = false;
246 }
247 else
248 {
249 memcpy(jaguarMainRAM + PtrGElfShdr->sh_addr, buffer + PtrGElfShdr->sh_offset, PtrGElfShdr->sh_size);
250 }
251 }
252 else
253 {
254 switch (ElfSectionNameType)
255 {
256 case ELF_debug_aranges_TYPE:
257 case ELF_debug_info_TYPE:
258 case ELF_debug_abbrev_TYPE:
259 case ELF_debug_line_TYPE:
260 case ELF_debug_frame_TYPE:
261 case ELF_debug_ranges_TYPE:
262 case ELF_debug_str_TYPE:
263 case ELF_debug_loc_TYPE:
264 break;
265
266 case ELF_heap_TYPE:
267 break;
268
269 case ELF_comment_TYPE:
270 break;
271
272 default:
273 error = true;
274 break;
275 }
276 }
277 break;
278
279 case SHT_NOBITS:
280 break;
281
282 case SHT_STRTAB:
283 case SHT_SYMTAB:
284 while ((error == false) && ((PtrElfData = elf_getdata(PtrElfScn, PtrElfData)) != NULL))
285 {
286 if (!ELFManager_AddTab(PtrElfData, ElfSectionNameType))
287 {
288 error = true;
289 }
290 }
291 break;
292
293 default:
294 error = true;
295 break;
296 }
297 }
298 }
299 }
300
301 jaguarRunAddress = (uint32_t)PtrGElfEhdr->e_entry;
302 WriteLog("FILE: Setting up ELF 32bits... Run address: %08X\n", jaguarRunAddress);
303 }
304 else
305 {
306 error = true;
307 }
308 }
309 else
310 {
311 error = true;
312 }
313 }
314 else
315 {
316 error = true;
317 WriteLog("FILE: libelf version is not recognized or libelf memory cannot be opened\n");
318 }
319 }
320 else
321 {
322 error = true;
323 WriteLog("FILE: ELFManager cannot allocate memory\n");
324 }
325
326 delete[] buffer;
327
328 if (error)
329 {
330 WriteLog("FILE: ELF parsing error\n");
331
332 if ((err = elf_errno()))
333 {
334 WriteLog("FILE: ELF error: %s\n", elf_errmsg(err));
335 }
336
337 return false;
338 }
339 else
340 {
341 DBGManager_SetType(DBGType);
342 return true;
343 }
344 }
345 else if (fileType == JST_ABS_TYPE1)
346 {
347 // For ABS type 1, run address == load address
348 uint32_t loadAddress = GET32(buffer, 0x16),
349 codeSize = GET32(buffer, 0x02) + GET32(buffer, 0x06);
350 WriteLog("FILE: Setting up homebrew (ABS-1)... Run address: %08X, length: %08X\n", loadAddress, codeSize);
351 memcpy(jagMemSpace + loadAddress, buffer + 0x24, codeSize);
352 delete[] buffer;
353 jaguarRunAddress = loadAddress;
354 return true;
355 }
356 else if (fileType == JST_ABS_TYPE2)
357 {
358 uint32_t loadAddress = GET32(buffer, 0x28), runAddress = GET32(buffer, 0x24),
359 codeSize = GET32(buffer, 0x18) + GET32(buffer, 0x1C);
360 WriteLog("FILE: Setting up homebrew (ABS-2)... Run address: %08X, length: %08X\n", runAddress, codeSize);
361 memcpy(jagMemSpace + loadAddress, buffer + 0xA8, codeSize);
362 delete[] buffer;
363 jaguarRunAddress = runAddress;
364 return true;
365 }
366 // NB: This is *wrong*
367 /*
368 Basically, if there is no "JAG" at position $1C, then the long there is the load/start
369 address in LITTLE ENDIAN.
370 If "JAG" is present, the the next character ("R" or "L") determines the size of the
371 JagServer command (2 bytes vs. 4). Following that are the commands themselves;
372 typically it will either be 2 (load) or 3 (load & run). Command headers go like so:
373 2:
374 Load address (long)
375 Length (long)
376 payload
377 3:
378 Load address (long)
379 Length (long)
380 Run address (long)
381 payload
382 5: (Reset)
383 [command only]
384 7: (Run at address)
385 Run address (long)
386 [no payload]
387 9: (Clear memory)
388 Start address (long)
389 End address (long)
390 [no payload]
391 10: (Poll for commands)
392 [command only]
393 12: (Load & run user program)
394 filname, terminated with NULL
395 [no payload]
396 $FFFF: (Halt)
397 [no payload]
398 */
399 else if (fileType == JST_JAGSERVER)
400 {
401 // This kind of shiaut should be in the detection code below...
402 // (and now it is! :-)
403 // if (buffer[0x1C] == 'J' && buffer[0x1D] == 'A' && buffer[0x1E] == 'G')
404 // {
405 // Still need to do some checking here for type 2 vs. type 3. This assumes 3
406 // Also, JAGR vs. JAGL (word command size vs. long command size)
407 uint32_t loadAddress = GET32(buffer, 0x22), runAddress = GET32(buffer, 0x2A);
408 WriteLog("FILE: Setting up homebrew (Jag Server)... Run address: $%X, length: $%X\n", runAddress, jaguarROMSize - 0x2E);
409 memcpy(jagMemSpace + loadAddress, buffer + 0x2E, jaguarROMSize - 0x2E);
410 delete[] buffer;
411 jaguarRunAddress = runAddress;
412
413 // Hmm. Is this kludge necessary?
414 SET32(jaguarMainRAM, 0x10, 0x00001000); // Set Exception #4 (Illegal Instruction)
415 SET16(jaguarMainRAM, 0x1000, 0x60FE); // Here: bra Here
416
417 return true;
418 // }
419 // else // Special WTFOMGBBQ type here...
420 // {
421 // uint32_t loadAddress = (buffer[0x1F] << 24) | (buffer[0x1E] << 16) | (buffer[0x1D] << 8) | buffer[0x1C];
422 // WriteLog("FILE: Setting up homebrew (GEMDOS WTFOMGBBQ type)... Run address: $%X, length: $%X\n", loadAddress, jaguarROMSize - 0x20);
423 // memcpy(jagMemSpace + loadAddress, buffer + 0x20, jaguarROMSize - 0x20);
424 // delete[] buffer;
425 // jaguarRunAddress = loadAddress;
426 // return true;
427 // }
428 }
429 else if (fileType == JST_WTFOMGBBQ)
430 {
431 uint32_t loadAddress = (buffer[0x1F] << 24) | (buffer[0x1E] << 16) | (buffer[0x1D] << 8) | buffer[0x1C];
432 WriteLog("FILE: Setting up homebrew (GEMDOS WTFOMGBBQ type)... Run address: $%X, length: $%X\n", loadAddress, jaguarROMSize - 0x20);
433 memcpy(jagMemSpace + loadAddress, buffer + 0x20, jaguarROMSize - 0x20);
434 delete[] buffer;
435 jaguarRunAddress = loadAddress;
436 return true;
437 }
438
439 // We can assume we have JST_NONE at this point. :-P
440 WriteLog("FILE: Failed to load headerless file.\n");
441 return false;
442 }
443
444
445 //
446 // "Debugger" file loading
447 // To keep the things separate between "Debugger" and "Alpine" loading until usage clarification has been done
448 //
449 bool DebuggerLoadFile(char * path)
450 {
451 return (AlpineLoadFile(path));
452 }
453
454
455 //
456 // "Alpine" file loading
457 // Since the developers were coming after us with torches and pitchforks, we
458 // decided to allow this kind of thing. ;-) But ONLY FOR THE DEVS, DAMMIT! >:-U
459 // O_O
460 //
461 bool AlpineLoadFile(char * path)
462 {
463 uint8_t * buffer = NULL;
464 jaguarROMSize = JaguarLoadROM(buffer, path);
465
466 if (jaguarROMSize == 0)
467 {
468 // It's up to the GUI to deal with failure, not us. ;-)
469 WriteLog("FILE: Could not load Alpine from file \"%s\"...\nAborting load!\n", path);
470 return false;
471 }
472
473 jaguarMainROMCRC32 = crc32_calcCheckSum(buffer, jaguarROMSize);
474 WriteLog("FILE: CRC is %08X\n", (unsigned int)jaguarMainROMCRC32);
475 EepromInit();
476
477 jaguarRunAddress = 0x802000;
478
479 WriteLog("FILE: Setting up Alpine ROM with non-standard length... Run address: 00802000, length: %08X\n", jaguarROMSize);
480
481 memset(jagMemSpace + 0x800000, 0xFF, 0x2000);
482 memcpy(jagMemSpace + 0x802000, buffer, jaguarROMSize);
483 delete[] buffer;
484
485 // Maybe instead of this, we could try requiring the STUBULATOR ROM? Just a thought...
486 // Try setting the vector to say, $1000 and putting an instruction there
487 // that loops forever:
488 // This kludge works! Yeah!
489 SET32(jaguarMainRAM, 0x10, 0x00001000); // Set Exception #4 (Illegal Instruction)
490 SET16(jaguarMainRAM, 0x1000, 0x60FE); // Here: bra Here
491
492 return true;
493 }
494
495
496 //
497 // Get the length of a (possibly) gzipped file
498 //
499 static int gzfilelength(gzFile gd)
500 {
501 int size = 0, length = 0;
502 unsigned char buffer[0x10000];
503
504 gzrewind(gd);
505
506 do
507 {
508 // Read in chunks until EOF
509 size = gzread(gd, buffer, 0x10000);
510
511 if (size <= 0)
512 break;
513
514 length += size;
515 }
516 while (!gzeof(gd));
517
518 gzrewind(gd);
519 return length;
520 }
521
522
523 //
524 // Compare extension to passed in filename. If equal, return true; otherwise false.
525 //
526 //#if defined(_MSC_VER) || defined(__MINGW64__)|| defined(__MINGW32__) || defined(__CYGWIN__)
527 static bool CheckExtension(const uint8_t *filename, const char *ext)
528 //#else
529 //static bool CheckExtension(const char * filename, const char * ext)
530 //#endif // _MSC_VER
531 {
532 // Sanity checking...
533 if ((filename == NULL) || (ext == NULL))
534 return false;
535
536 const char * filenameExt = strrchr((const char *)filename, '.'); // Get the file's extension (if any)
537
538 if (filenameExt == NULL)
539 return false;
540
541 return (strcasecmp(filenameExt, ext) == 0 ? true : false);
542 }
543
544
545 //
546 // Get file from .ZIP
547 // Returns the size of the file inside the .ZIP file that we're looking at
548 // NOTE: If the thing we're looking for is found, it allocates it in the passed in buffer.
549 // Which means we have to deallocate it later.
550 //
551 uint32_t GetFileFromZIP(const char * zipFile, FileType type, uint8_t * &buffer)
552 {
553 // NOTE: We could easily check for this by discarding anything that's larger than the RAM/ROM
554 // size of the Jaguar console.
555 #if defined(_MSC_VER)
556 #pragma message("Warning: !!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!")
557 #else
558 #warning "!!! FIX !!! Should have sanity checking for ROM size to prevent buffer overflow!"
559 #endif // _MSC_VER
560 const char ftStrings[5][32] = { "Software", "EEPROM", "Label", "Box Art", "Controller Overlay" };
561 // ZIP * zip = openzip(0, 0, zipFile);
562 FILE * zip = fopen(zipFile, "rb");
563
564 if (zip == NULL)
565 {
566 WriteLog("FILE: Could not open file '%s'!\n", zipFile);
567 return 0;
568 }
569
570 // zipent * ze;
571 ZipFileEntry ze;
572 bool found = false;
573
574 // The order is here is important: If the file is found, we need to short-circuit the
575 // readzip() call because otherwise, 'ze' will be pointing to the wrong file!
576 // while (!found && readzip(zip))
577 while (!found && GetZIPHeader(zip, ze))
578 {
579 // ze = &zip->ent;
580
581 // Here we simply rely on the file extension to tell the truth, but we know
582 // that extensions lie like sons-a-bitches. So this is naive, we need to do
583 // something a little more robust to keep bad things from happening here.
584 #if defined(_MSC_VER)
585 #pragma message("Warning: !!! Checking for image by extension can be fooled !!!")
586 #else
587 #warning "!!! Checking for image by extension can be fooled !!!"
588 #endif // _MSC_VER
589 if ((type == FT_LABEL) && (CheckExtension(ze.filename, ".png") || CheckExtension(ze.filename, ".jpg") || CheckExtension(ze.filename, ".gif")))
590 {
591 found = true;
592 WriteLog("FILE: Found image file '%s'.\n", ze.filename);
593 }
594
595 if ((type == FT_SOFTWARE) && (CheckExtension(ze.filename, ".j64")
596 || CheckExtension(ze.filename, ".rom") || CheckExtension(ze.filename, ".abs")
597 || CheckExtension(ze.filename, ".cof") || CheckExtension(ze.filename, ".coff")
598 || CheckExtension(ze.filename, ".jag") || CheckExtension(ze.filename, ".elf")))
599 {
600 found = true;
601 WriteLog("FILE: Found software file '%s'.\n", ze.filename);
602 }
603
604 if ((type == FT_EEPROM) && (CheckExtension(ze.filename, ".eep") || CheckExtension(ze.filename, ".eeprom")))
605 {
606 found = true;
607 WriteLog("FILE: Found EEPROM file '%s'.\n", ze.filename);
608 }
609
610 if (!found)
611 fseek(zip, ze.compressedSize, SEEK_CUR);
612 }
613
614 uint32_t fileSize = 0;
615
616 if (found)
617 {
618 WriteLog("FILE: Uncompressing...");
619 // Insert file size sanity check here...
620 buffer = new uint8_t[ze.uncompressedSize];
621
622 // if (readuncompresszip(zip, ze.compressedSize, buffer) == 0)
623 // if (UncompressFileFromZIP(zip, ze.compressedSize, buffer) == 0)
624 if (UncompressFileFromZIP(zip, ze, buffer) == 0)
625 {
626 fileSize = ze.uncompressedSize;
627 WriteLog("success! (%u bytes)\n", fileSize);
628 }
629 else
630 {
631 delete[] buffer;
632 buffer = NULL;
633 WriteLog("FAILED!\n");
634 }
635 }
636 else
637 // Didn't find what we're looking for...
638 WriteLog("FILE: Failed to find file of type %s...\n", ftStrings[type]);
639
640 // closezip(zip);
641 fclose(zip);
642 return fileSize;
643 }
644
645
646 uint32_t GetFileDBIdentityFromZIP(const char * zipFile)
647 {
648 FILE * zip = fopen(zipFile, "rb");
649
650 if (zip == NULL)
651 {
652 WriteLog("FILE: Could not open file '%s'!\n", zipFile);
653 return 0;
654 }
655
656 ZipFileEntry ze;
657
658 // Loop through all files in the zip file under consideration
659 while (GetZIPHeader(zip, ze))
660 {
661 // & loop through all known CRC32s in our file DB to see if it's there!
662 uint32_t index = 0;
663
664 while (romList[index].crc32 != 0xFFFFFF)
665 {
666 if (romList[index].crc32 == ze.crc32)
667 {
668 fclose(zip);
669 return index;
670 }
671
672 index++;
673 }
674
675 // We didn't find it, so skip the compressed data...
676 fseek(zip, ze.compressedSize, SEEK_CUR);
677 }
678
679 fclose(zip);
680 return (uint32_t )-1;
681 }
682
683
684 bool FindFileInZIPWithCRC32(const char * zipFile, uint32_t crc)
685 {
686 FILE * zip = fopen(zipFile, "rb");
687
688 if (zip == NULL)
689 {
690 WriteLog("FILE: Could not open file '%s'!\n", zipFile);
691 return 0;
692 }
693
694 ZipFileEntry ze;
695
696 // Loop through all files in the zip file under consideration
697 while (GetZIPHeader(zip, ze))
698 {
699 if (ze.crc32 == crc)
700 {
701 fclose(zip);
702 return true;
703 }
704
705 fseek(zip, ze.compressedSize, SEEK_CUR);
706 }
707
708 fclose(zip);
709 return false;
710 }
711
712
713 //
714 // Parse the file type based upon file size and/or headers.
715 //
716 uint32_t ParseFileType(uint8_t * buffer, uint32_t size)
717 {
718 // Check headers first...
719
720 // ELF 32bits
721 if (buffer[EI_CLASS] == ELFCLASS32)
722 {
723 if (((BigToLittleEndian16(((Elf32_Ehdr *)buffer)->e_machine) & 0xFF) == EM_68K) && (BigToLittleEndian16(((Elf32_Ehdr *)buffer)->e_type) == ET_EXEC) && (buffer[0] == ELFMAG0) && (buffer[1] == ELFMAG1) && (buffer[2] == ELFMAG2) && (buffer[3] == ELFMAG3))
724 return JST_ELF32;
725 }
726
727 // ABS/COFF type 1
728 if (buffer[0] == 0x60 && buffer[1] == 0x1B)
729 return JST_ABS_TYPE1;
730
731 // ABS/COFF type 2
732 if (buffer[0] == 0x01 && buffer[1] == 0x50)
733 return JST_ABS_TYPE2;
734
735 // Jag Server & other old shite
736 if (buffer[0] == 0x60 && buffer[1] == 0x1A)
737 {
738 if (buffer[0x1C] == 'J' && buffer[0x1D] == 'A' && buffer[0x1E] == 'G')
739 return JST_JAGSERVER;
740 else
741 return JST_WTFOMGBBQ;
742 }
743
744 // And if that fails, try file sizes...
745
746 // If the file size is divisible by 1M, we probably have an regular ROM.
747 // We can also check our CRC32 against the internal ROM database to be sure.
748 // (We also check for the Memory Track cartridge size here as well...)
749 if ((size % 1048576) == 0 || size == 131072)
750 return JST_ROM;
751
752 // If the file size + 8192 bytes is divisible by 1M, we probably have an
753 // Alpine format ROM.
754 if (((size + 8192) % 1048576) == 0)
755 return JST_ALPINE;
756
757 // Headerless crap
758 return JST_NONE;
759 }
760
761 //
762 // Check for universal header
763 //
764 bool HasUniversalHeader(uint8_t * rom, uint32_t romSize)
765 {
766 // Sanity check
767 if (romSize < 8192)
768 return false;
769
770 for(int i=0; i<8192; i++)
771 if (rom[i] != universalCartHeader[i])
772 return false;
773
774 return true;
775 }
776
777 #if 0
778 // Misc. doco
779
780 /*
781 Stubulator ROM vectors...
782 handler 001 at $00E00008
783 handler 002 at $00E008DE
784 handler 003 at $00E008E2
785 handler 004 at $00E008E6
786 handler 005 at $00E008EA
787 handler 006 at $00E008EE
788 handler 007 at $00E008F2
789 handler 008 at $00E0054A
790 handler 009 at $00E008FA
791 handler 010 at $00000000
792 handler 011 at $00000000
793 handler 012 at $00E008FE
794 handler 013 at $00E00902
795 handler 014 at $00E00906
796 handler 015 at $00E0090A
797 handler 016 at $00E0090E
798 handler 017 at $00E00912
799 handler 018 at $00E00916
800 handler 019 at $00E0091A
801 handler 020 at $00E0091E
802 handler 021 at $00E00922
803 handler 022 at $00E00926
804 handler 023 at $00E0092A
805 handler 024 at $00E0092E
806 handler 025 at $00E0107A
807 handler 026 at $00E0107A
808 handler 027 at $00E0107A
809 handler 028 at $00E008DA
810 handler 029 at $00E0107A
811 handler 030 at $00E0107A
812 handler 031 at $00E0107A
813 handler 032 at $00000000
814
815 Let's try setting up the illegal instruction vector for a stubulated jaguar...
816
817 SET32(jaguar_mainRam, 0x08, 0x00E008DE);
818 SET32(jaguar_mainRam, 0x0C, 0x00E008E2);
819 SET32(jaguar_mainRam, 0x10, 0x00E008E6); // <-- Should be here (it is)...
820 SET32(jaguar_mainRam, 0x14, 0x00E008EA);//*/
821
822 /*
823 ABS Format sleuthing (LBUGDEMO.ABS):
824
825 000000 60 1B 00 00 05 0C 00 04 62 C0 00 00 04 28 00 00
826 000010 12 A6 00 00 00 00 00 80 20 00 FF FF 00 80 25 0C
827 000020 00 00 40 00
828
829 DRI-format file detected...
830 Text segment size = 0x0000050c bytes
831 Data segment size = 0x000462c0 bytes
832 BSS Segment size = 0x00000428 bytes
833 Symbol Table size = 0x000012a6 bytes
834 Absolute Address for text segment = 0x00802000
835 Absolute Address for data segment = 0x0080250c
836 Absolute Address for BSS segment = 0x00004000
837
838 (CRZDEMO.ABS):
839 000000 01 50 00 03 00 00 00 00 00 03 83 10 00 00 05 3b
840 000010 00 1c 00 03 00 00 01 07 00 00 1d d0 00 03 64 98
841 000020 00 06 8b 80 00 80 20 00 00 80 20 00 00 80 3d d0
842
843 000030 2e 74 78 74 00 00 00 00 00 80 20 00 00 80 20 00 .txt (+36 bytes)
844 000040 00 00 1d d0 00 00 00 a8 00 00 00 00 00 00 00 00
845 000050 00 00 00 00 00 00 00 20
846 000058 2e 64 74 61 00 00 00 00 00 80 3d d0 00 80 3d d0 .dta (+36 bytes)
847 000068 00 03 64 98 00 00 1e 78 00 00 00 00 00 00 00 00
848 000078 00 00 00 00 00 00 00 40
849 000080 2e 62 73 73 00 00 00 00 00 00 50 00 00 00 50 00 .bss (+36 bytes)
850 000090 00 06 8b 80 00 03 83 10 00 00 00 00 00 00 00 00
851 0000a0 00 00 00 00 00 00 00 80
852
853 Header size is $A8 bytes...
854
855 BSD/COFF format file detected...
856 3 sections specified
857 Symbol Table offset = 230160 ($00038310)
858 Symbol Table contains 1339 symbol entries ($0000053B)
859 The additional header size is 28 bytes ($001C)
860 Magic Number for RUN_HDR = 0x00000107
861 Text Segment Size = 7632 ($00001DD0)
862 Data Segment Size = 222360 ($00036498)
863 BSS Segment Size = 428928 ($00068B80)
864 Starting Address for executable = 0x00802000
865 Start of Text Segment = 0x00802000
866 Start of Data Segment = 0x00803dd0
867 */
868 #endif