1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2
* Mupen64plus - rom.c *
3
* Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ *
4
* Copyright (C) 2008 Tillin9 *
5
* Copyright (C) 2002 Hacktarux *
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. *
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. *
17
* You should have received a copy of the GNU General Public License *
18
* along with this program; if not, write to the *
19
* Free Software Foundation, Inc., *
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
21
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
30
#include "zip/unzip.h"
31
#include "bzip2/bzlib.h"
32
#include "lzma/lzmadec.h"
33
#include "7zip/7zExtract.h"
34
#include "7zip/7zCrc.h"
39
#include "translate.h"
43
#include "../memory/memory.h"
44
#include "../opengl/osd.h"
46
#define CHUNKSIZE 1024*128 /* Read files 128KB at a time. */
52
/* Global loaded rom memory space. */
54
/* Global loaded rom size. */
57
/* TODO: Replace with a glocal cache_entry. */
58
rom_header* ROM_HEADER;
59
rom_settings ROM_SETTINGS;
61
/* Tests if a file is a valid N64 rom by checking the first 4 bytes. */
62
int is_valid_rom(unsigned char buffer[4])
64
/* Test if rom is a native .z64 image with header 0x80371240. [ABCD] */
65
if((buffer[0]==0x80)&&(buffer[1]==0x37)&&(buffer[2]==0x12)&&(buffer[3]==0x40))
67
/* Test if rom is a byteswapped .v64 image with header 0x37804012. [BADC] */
68
else if((buffer[0]==0x37)&&(buffer[1]==0x80)&&(buffer[2]==0x40)&&(buffer[3]==0x12))
70
/* Test if rom is a wordswapped .n64 image with header 0x40123780. [DCBA] */
71
else if((buffer[0]==0x40)&&(buffer[1]==0x12)&&(buffer[2]==0x37)&&(buffer[3]==0x80))
77
/* If rom is a .v64 or .n64 image, byteswap or wordswap loadlength amount of
78
* rom data to native .z64 before forwarding. Makes sure that data extraction
79
* and MD5ing routines always deal with a .z64 image.
81
void swap_rom(unsigned char* localrom, unsigned char* imagetype, int loadlength)
86
/* Btyeswap if .v64 image. */
89
*imagetype = V64IMAGE;
90
for (i = 0; i < loadlength; i+=2)
93
localrom[i]=localrom[i+1];
97
/* Wordswap if .n64 image. */
98
else if(localrom[0]==0x40)
100
*imagetype = N64IMAGE;
101
for (i = 0; i < loadlength; i+=4)
104
localrom[i]=localrom[1+3];
107
localrom[i+1]=localrom[i+2];
112
*imagetype = Z64IMAGE;
115
/* Open a file and test if its an uncompressed rom, bzip2ed rom, lzma stream compressed
116
* rom, or gzipped rom. If so, set compressiontype and load *loadlength of the rom
117
* into the returned pointer. On failure return NULL.
119
unsigned char* load_single_rom(const char* filename, int* romsize, unsigned char* compressiontype, int* loadlength)
122
unsigned short romread = 0;
123
unsigned char buffer[4];
124
unsigned char* localrom;
127
/* Uncompressed roms. */
128
romfile=fopen(filename, "rb");
131
fread(buffer, 1, 4, romfile);
132
if(is_valid_rom(buffer))
134
*compressiontype = UNCOMPRESSED;
135
fseek(romfile, 0L, SEEK_END);
136
*romsize=ftell(romfile);
137
fseek(romfile, 0L, SEEK_SET);
138
localrom = (unsigned char*)malloc(*loadlength*sizeof(unsigned char));
141
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
144
fread(localrom, 1, *loadlength, romfile);
152
fseek(romfile, 0L, SEEK_SET);
153
bzromfile = BZ2_bzReadOpen(&bzerror, romfile, 0, 0, NULL, 0);
155
BZ2_bzRead(&bzerror, bzromfile, buffer, 4);
158
if(is_valid_rom(buffer))
160
*compressiontype = BZIP2_COMPRESSION;
161
fseek(romfile, 0L, SEEK_SET);
162
bzromfile = BZ2_bzReadOpen(&bzerror, romfile, 0, 0, NULL, 0);
165
for( i = 0; bzerror==BZ_OK; ++i )
167
localrom = (unsigned char*)realloc(localrom, (i+1)*CHUNKSIZE*sizeof(unsigned char));
170
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
173
*romsize += BZ2_bzRead(&bzerror, bzromfile, localrom+(i*CHUNKSIZE), CHUNKSIZE);
175
if(bzerror==BZ_STREAM_END)
177
localrom = (unsigned char*)realloc(localrom,*loadlength*sizeof(unsigned char));
184
BZ2_bzReadClose(&bzerror, bzromfile);
189
fseek(romfile, 0L, SEEK_SET);
191
lzmadec_stream stream;
192
stream.lzma_alloc = NULL;
193
stream.lzma_free = NULL;
194
stream.opaque = NULL;
196
stream.next_in = NULL;
198
/* Minimum size to get decoded blocks back is 45.
199
LZMA has 13 byte headers, likely 32 byte internal buffer. */
200
unsigned char* buffer_in = (unsigned char*)malloc(45*sizeof(unsigned char));
201
unsigned char* buffer_out = (unsigned char*)malloc(45*128*sizeof(unsigned char));
202
if(buffer_in==NULL||buffer_out==NULL||lzmadec_init(&stream)!=LZMADEC_OK)
204
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
208
fread(buffer_in, sizeof(unsigned char), 45, romfile);
210
stream.next_in = buffer_in;
211
stream.avail_in = 45;
212
stream.next_out = buffer_out;
213
stream.avail_out = 45;
215
lzmastatus = lzmadec_decode (&stream, (stream.avail_in==0));
216
if(lzmastatus==LZMADEC_OK&&is_valid_rom(buffer_out))
218
*compressiontype = LZMA_COMPRESSION;
220
*romsize = 45 - stream.avail_out;
222
buffer_in = (unsigned char*)realloc(buffer_in,CHUNKSIZE*sizeof(unsigned char));
223
buffer_out = (unsigned char*)realloc(buffer_out,CHUNKSIZE*128*sizeof(unsigned char));
224
localrom = (unsigned char*)malloc(*romsize*sizeof(unsigned char));
225
if(buffer_in==NULL||buffer_out==NULL||localrom==NULL)
227
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
231
memcpy(localrom,buffer_out, *romsize);
232
while(lzmastatus==LZMADEC_OK)
234
fread(buffer_in, sizeof(unsigned char), CHUNKSIZE, romfile);
235
stream.next_in = buffer_in;
236
stream.avail_in = CHUNKSIZE;
237
stream.next_out = buffer_out;
238
stream.avail_out = CHUNKSIZE*128;
239
lzmastatus = lzmadec_decode (&stream, (stream.avail_in==0));
242
*romsize += CHUNKSIZE*128-stream.avail_out;
244
localrom = (unsigned char*)realloc(localrom,*romsize*sizeof(unsigned char));
247
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
250
memcpy(localrom+oldsize,buffer_out,CHUNKSIZE*128-stream.avail_out);
253
if(lzmastatus==LZMADEC_STREAM_END)
255
lzmadec_end(&stream);
256
localrom = (unsigned char*)realloc(localrom,*loadlength*sizeof(unsigned char));
270
gzromfile=gzopen(filename, "rb");
273
gzread(gzromfile, buffer, 4);
274
if(is_valid_rom(buffer))
276
*compressiontype = GZIP_COMPRESSION;
277
gzseek(gzromfile, 0L, SEEK_SET);
280
for( i = 0; !gzeof(gzromfile); ++i )
282
localrom = (unsigned char*)realloc(localrom, (i+1)*CHUNKSIZE*sizeof(unsigned char));
285
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
288
*romsize += gzread(gzromfile, localrom+(i*CHUNKSIZE), CHUNKSIZE);
292
localrom = (unsigned char*)realloc(localrom,*loadlength*sizeof(unsigned char));
302
/* File invalid, or valid rom not found in file. */
309
/* Open a file and test if its a zip or 7zip archive. If so load check if *archivefile
310
* is a rom and load *loadlength into the returned pointer. If *archivefile is not a rom
311
* in a many file archive, function will keep checking until a rom is found or the
312
* end of the archive is reached. Returns NULL if unable to find a rom. The large function
313
* API is to allow for persistent 7zip memory space.
315
unsigned char* load_archive_rom(const char* filename, int* romsize, unsigned char* compressiontype, int* loadlength, unsigned int* archivefile, UInt32* blockIndex, Byte** outBuffer, size_t* outBufferSize, CFileInStream* archiveStream, CArchiveDatabaseEx* db)
318
unsigned int filecounter = 0;
319
unsigned short romread = 0;
320
unsigned char* localrom;
323
unsigned char buffer[4];
325
unz_file_info fileinfo;
326
char szFileName[256], szExtraField[256], szComment[256];
327
zipromfile = unzOpen(filename);
330
if(unzGoToFirstFile(zipromfile)==UNZ_OK)
332
/* Get first valid rom in archive. */
335
if(unzGetCurrentFileInfo(zipromfile, &fileinfo, szFileName, 255,
336
szExtraField, 255, szComment, 255)!=UNZ_OK)
338
if(unzOpenCurrentFile(zipromfile)!=UNZ_OK)
341
if(fileinfo.uncompressed_size>=4&&filecounter>=*archivefile)
343
if(unzReadCurrentFile(zipromfile, buffer, 4)!=4)
345
if(is_valid_rom(buffer))
347
unzOpenCurrentFile(zipromfile);
348
*romsize = fileinfo.uncompressed_size;
349
localrom = (unsigned char*)malloc(*loadlength*sizeof(unsigned char));
352
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
356
if(unzReadCurrentFile(zipromfile, localrom, *loadlength)==*loadlength)
358
*compressiontype = ZIP_COMPRESSION;
359
unzCloseCurrentFile(zipromfile);
369
while ((status=unzGoToNextFile(zipromfile)) != UNZ_END_OF_LIST_OF_FILE);
371
unzClose(zipromfile);
378
allocImp.Free = SzFree;
379
allocImp.Alloc = SzAlloc;
381
ISzAlloc allocTempImp;
382
allocTempImp.Alloc = SzAllocTemp;
383
allocTempImp.Free = SzFreeTemp;
385
if(archiveStream->File==NULL)
387
archiveStream->File = fopen(filename, "rb");
388
if(archiveStream->File==NULL)
390
status = SzArchiveOpen(&(archiveStream->InStream), db, &allocImp, &allocTempImp);
392
printf("Deflating 7zip archive.\n");
399
for (filecounter = *archivefile; filecounter < db->Database.NumFiles; ++filecounter)
402
size_t outSizeProcessed;
404
CFileItem *f = db->Database.Files + filecounter;
405
status = SzExtract(&(archiveStream->InStream), db, filecounter, blockIndex, outBuffer, outBufferSize, &offset, &outSizeProcessed, &allocImp, &allocTempImp);
408
if(status==SZE_NOTIMPL)
409
printf("7zip decoder doesn't support this archive.");
410
else if(status==SZE_OUTOFMEMORY)
411
printf("7zip decoder can not allocate memory\n");
412
else if(status==SZE_CRC_ERROR)
413
printf("7zip CRC error");
415
printf("7zip Error# %d\n", status);
418
if(is_valid_rom(*outBuffer+offset))
421
*compressiontype = SZIP_COMPRESSION;
422
localrom = (unsigned char*)malloc(*loadlength*sizeof(unsigned char));
425
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
428
memcpy(localrom,*outBuffer+offset,*loadlength);
436
/* File invalid, or valid rom not found in file. */
440
*archivefile = filecounter;
444
static int ask_bad(void)
448
return gui_message(GUI_MESSAGE_CONFIRM,
449
tr("The rom you are trying to load is probably a bad dump!\n"
450
"Be warned that this will probably give unexpected results.\n"
451
"Do you still want to run it?"));
454
printf(tr("The rom you are trying to load is probably a bad dump!\n"
455
"Be warned that this will probably give unexpected results.\n"));
460
static int ask_hack(void)
464
return gui_message(GUI_MESSAGE_CONFIRM, tr("The rom you are trying to load is probably a hack!\n"
465
"Be warned that this will probably give unexpected results.\n"
466
"Do you still want to run it?"));
469
printf(tr("The rom you are trying to load is probably a hack!\n"
470
"Be warned that this will probably give unexpected results.\n"));
475
int open_rom(const char* filename, unsigned int archivefile)
477
if(g_EmulatorRunning)
482
if(!gui_message(GUI_MESSAGE_CONFIRM,
483
tr("Emulation is running. Do you want to\n"
484
"stop it and load the selected rom?")))
492
md5_byte_t digest[16];
493
romdatabase_entry* entry;
494
char buffer[PATH_MAX];
495
unsigned char compressiontype, imagetype;
501
/* Clear Byte-swapped flag, since ROM is now deleted. */
502
g_MemHasBeenBSwapped = 0;
504
UInt32 blockIndex = 0xFFFFFFFF;
505
Byte* outBuffer = NULL;
506
size_t outBufferSize = 0;
508
CFileInStream archiveStream;
509
archiveStream.File = NULL;
510
archiveStream.InStream.Read = SzFileReadImp;
511
archiveStream.InStream.Seek = SzFileSeekImp;
513
CArchiveDatabaseEx db;
517
strncpy(buffer, filename, PATH_MAX-1);
518
buffer[PATH_MAX-1] = 0;
519
if ((rom=load_single_rom(filename, &taille_rom, &compressiontype, &taille_rom))==NULL)
521
if((rom=load_archive_rom(filename, &taille_rom, &compressiontype, &taille_rom, &archivefile, &blockIndex, &outBuffer, &outBufferSize, &archiveStream, &db))==NULL)
523
error_message(tr("Couldn't load Rom!"));
530
if(archiveStream.File)
531
fclose(archiveStream.File);
532
SzArDbExFree(&db, free);
534
swap_rom(rom, &imagetype, taille_rom);
536
compressionstring(compressiontype, buffer);
537
printf("Compression: %s\n", buffer);
539
imagestring(imagetype, buffer);
540
printf("Imagetype: %s\n", buffer);
542
printf("Rom size: %d bytes (or %d Mb or %d Megabits)\n",
543
taille_rom, taille_rom/1024/1024, taille_rom/1024/1024*8);
545
/* TODO: Replace the following validation code with fill_entry(). */
547
/* Load rom settings and check if it's a good dump. */
549
md5_append(&state, (const md5_byte_t*)rom, taille_rom);
550
md5_finish(&state, digest);
551
for ( i = 0; i < 16; ++i )
552
sprintf(buffer+i*2, "%02X", digest[i]);
554
strcpy(ROM_SETTINGS.MD5, buffer);
555
printf("MD5: %s\n", buffer);
559
ROM_HEADER = malloc(sizeof(rom_header));
562
fprintf(stderr, "%s, %d: Out of memory!\n", __FILE__, __LINE__);
565
memcpy(ROM_HEADER, rom, sizeof(rom_header));
566
trim((char*)ROM_HEADER->nom); /* Remove trailing whitespace from Rom name. */
568
printf("%x %x %x %x\n", ROM_HEADER->init_PI_BSB_DOM1_LAT_REG,
569
ROM_HEADER->init_PI_BSB_DOM1_PGS_REG,
570
ROM_HEADER->init_PI_BSB_DOM1_PWD_REG,
571
ROM_HEADER->init_PI_BSB_DOM1_PGS_REG2);
572
printf("ClockRate = %x\n", sl((unsigned int)ROM_HEADER->ClockRate));
573
printf("Version: %x\n", sl((unsigned int)ROM_HEADER->Release));
574
printf("CRC: %x %x\n", sl((unsigned int)ROM_HEADER->CRC1), sl((unsigned int)ROM_HEADER->CRC2));
575
printf ("Name: %s\n", ROM_HEADER->nom);
576
if(sl(ROM_HEADER->Manufacturer_ID) == 'N')
577
printf ("Manufacturer: Nintendo\n");
579
printf("Manufacturer: %x\n", (unsigned int)(ROM_HEADER->Manufacturer_ID));
580
printf("Cartridge_ID: %x\n", ROM_HEADER->Cartridge_ID);
582
countrycodestring(ROM_HEADER->Country_code, buffer);
583
printf("Country: %s\n", buffer);
584
printf ("PC = %x\n", sl((unsigned int)ROM_HEADER->PC));
586
if((entry=ini_search_by_md5(digest))==&empty_entry)
588
if((entry=ini_search_by_crc(sl(ROM_HEADER->CRC1),sl(ROM_HEADER->CRC2)))==&empty_entry)
590
strcpy(ROM_SETTINGS.goodname, (char*)ROM_HEADER->nom);
591
strcat(ROM_SETTINGS.goodname, " (unknown rom)");
592
printf("%s\n", ROM_SETTINGS.goodname);
593
ROM_SETTINGS.eeprom_16kb = 0;
598
unsigned short close = 0;
599
char* s = entry->goodname;
602
for ( i = strlen(s); i > 1; --i )
605
if(s[i-1]=='['&&(s[i]=='T'||s[i]=='t'||s[i]=='h'||s[i]=='f'))
610
else if(s[i-1]=='['&&s[i]=='b')
624
main_message(0, 1, 0, OSD_BOTTOM_LEFT, tr("Rom closed."));
628
strncpy(ROM_SETTINGS.goodname, entry->goodname, 255);
629
ROM_SETTINGS.goodname[255] = '\0';
631
if(entry->savetype==EEPROM_16KB)
632
ROM_SETTINGS.eeprom_16kb = 1;
633
printf("EEPROM type: %d\n", ROM_SETTINGS.eeprom_16kb);
639
if(g_EmulatorRunning)
656
/* Clear Byte-swapped flag, since ROM is now deleted. */
657
g_MemHasBeenBSwapped = 0;
658
main_message(0, 1, 0, OSD_BOTTOM_LEFT, tr("Rom closed."));