1 /*
2  * PicoDrive
3  * (C) notaz, 2006-2010,2013
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 
9 #include <string.h>
10 #include "pico_int.h"
11 #include "cd/cue.h"
12 
13 unsigned char media_id_header[0x100];
14 
strlwr_(char * string)15 static void strlwr_(char *string)
16 {
17   char *p;
18   for (p = string; *p; p++)
19     if ('A' <= *p && *p <= 'Z')
20       *p += 'a' - 'A';
21 }
22 
get_ext(const char * file,char * ext)23 static void get_ext(const char *file, char *ext)
24 {
25   const char *p;
26 
27   p = file + strlen(file) - 4;
28   if (p < file) p = file;
29   strncpy(ext, p, 4);
30   ext[4] = 0;
31   strlwr_(ext);
32 }
33 
detect_media(const char * fname)34 static int detect_media(const char *fname)
35 {
36   static const short sms_offsets[] = { 0x7ff0, 0x3ff0, 0x1ff0 };
37   static const char *sms_exts[] = { "sms", "gg", "sg" };
38   static const char *md_exts[] = { "gen", "bin", "smd" };
39   char buff0[32], buff[32];
40   unsigned short *d16;
41   pm_file *pmf;
42   char ext[5];
43   int i;
44 
45   get_ext(fname, ext);
46 
47   // detect wrong extensions
48   if (!strcmp(ext, ".srm") || !strcmp(ext, "s.gz") || !strcmp(ext, ".mds")) // s.gz ~ .mds.gz
49     return PM_BAD_DETECT;
50 
51   /* don't believe in extensions, except .cue */
52   if (strcasecmp(ext, ".cue") == 0)
53     return PM_CD;
54 
55   pmf = pm_open(fname);
56   if (pmf == NULL)
57     return PM_BAD_DETECT;
58 
59   if (pm_read(buff0, 32, pmf) != 32) {
60     pm_close(pmf);
61     return PM_BAD_DETECT;
62   }
63 
64   if (strncasecmp("SEGADISCSYSTEM", buff0 + 0x00, 14) == 0 ||
65       strncasecmp("SEGADISCSYSTEM", buff0 + 0x10, 14) == 0) {
66     pm_close(pmf);
67     return PM_CD;
68   }
69 
70   /* check for SMD evil */
71   if (pmf->size >= 0x4200 && (pmf->size & 0x3fff) == 0x200) {
72     if (pm_seek(pmf, sms_offsets[0] + 0x200, SEEK_SET) == sms_offsets[0] + 0x200 &&
73         pm_read(buff, 16, pmf) == 16 &&
74         strncmp("TMR SEGA", buff, 8) == 0)
75       goto looks_like_sms;
76 
77     /* could parse further but don't bother */
78     goto extension_check;
79   }
80 
81   /* MD header? Act as TMSS BIOS here */
82   if (pm_seek(pmf, 0x100, SEEK_SET) == 0x100 && pm_read(buff, 16, pmf) == 16) {
83     if (strncmp(buff, "SEGA", 4) == 0 || strncmp(buff, " SEG", 4) == 0)
84       goto looks_like_md;
85   }
86 
87   for (i = 0; i < ARRAY_SIZE(sms_offsets); i++) {
88     if (pm_seek(pmf, sms_offsets[i], SEEK_SET) != sms_offsets[i])
89       continue;
90 
91     if (pm_read(buff, 16, pmf) != 16)
92       continue;
93 
94     if (strncmp("TMR SEGA", buff, 8) == 0)
95       goto looks_like_sms;
96   }
97 
98 extension_check:
99   /* probably some headerless thing. Maybe check the extension after all. */
100   for (i = 0; i < ARRAY_SIZE(md_exts); i++)
101     if (strcasecmp(pmf->ext, md_exts[i]) == 0)
102       goto looks_like_md;
103 
104   for (i = 0; i < ARRAY_SIZE(sms_exts); i++)
105     if (strcasecmp(pmf->ext, sms_exts[i]) == 0)
106       goto looks_like_sms;
107 
108   /* If everything else fails, make a guess on the reset vector */
109   d16 = (unsigned short *)(buff0 + 4);
110   if ((((d16[0] << 16) | d16[1]) & 0xffffff) >= pmf->size) {
111     lprintf("bad MD reset vector, assuming SMS\n");
112     goto looks_like_sms;
113   }
114 
115 looks_like_md:
116   pm_close(pmf);
117   return PM_MD_CART;
118 
119 looks_like_sms:
120   pm_close(pmf);
121   return PM_MARK3;
122 }
123 
124 /* checks if fname points to valid MegaCD image */
PicoCdCheck(const char * fname_in,int * pregion)125 int PicoCdCheck(const char *fname_in, int *pregion)
126 {
127   const char *fname = fname_in;
128   unsigned char buf[32];
129   pm_file *cd_f;
130   int region = 4; // 1: Japan, 4: US, 8: Europe
131   char ext[5];
132   cue_track_type type = CT_UNKNOWN;
133   cue_data_t *cue_data = NULL;
134 
135   // opens a cue, or searches for one
136   cue_data = cue_parse(fname_in);
137   if (cue_data != NULL) {
138     fname = cue_data->tracks[1].fname;
139     type  = cue_data->tracks[1].type;
140   }
141   else {
142     get_ext(fname_in, ext);
143     if (strcasecmp(ext, ".cue") == 0)
144       return -1;
145   }
146 
147   cd_f = pm_open(fname);
148   if (cue_data != NULL)
149     cue_destroy(cue_data);
150 
151   if (cd_f == NULL) return 0; // let the upper level handle this
152 
153   if (pm_read(buf, 32, cd_f) != 32) {
154     pm_close(cd_f);
155     return -1;
156   }
157 
158   if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x00, 14)) {
159     if (type && type != CT_ISO)
160       elprintf(EL_STATUS, ".cue has wrong type: %i", type);
161     type = CT_ISO;       // Sega CD (ISO)
162   }
163   if (!strncasecmp("SEGADISCSYSTEM", (char *)buf+0x10, 14)) {
164     if (type && type != CT_BIN)
165       elprintf(EL_STATUS, ".cue has wrong type: %i", type);
166     type = CT_BIN;       // Sega CD (BIN)
167   }
168 
169   if (type == CT_UNKNOWN) {
170     pm_close(cd_f);
171     return 0;
172   }
173 
174   pm_seek(cd_f, (type == CT_ISO) ? 0x100 : 0x110, SEEK_SET);
175   pm_read(media_id_header, sizeof(media_id_header), cd_f);
176 
177   /* it seems we have a CD image here. Try to detect region now.. */
178   pm_seek(cd_f, (type == CT_ISO) ? 0x100+0x10B : 0x110+0x10B, SEEK_SET);
179   pm_read(buf, 1, cd_f);
180   pm_close(cd_f);
181 
182   if (buf[0] == 0x64) region = 8; // EU
183   if (buf[0] == 0xa1) region = 1; // JAP
184 
185   lprintf("detected %s Sega/Mega CD image with %s region\n",
186     type == CT_BIN ? "BIN" : "ISO", region != 4 ? (region == 8 ? "EU" : "JAP") : "USA");
187 
188   if (pregion != NULL)
189     *pregion = region;
190 
191   return type;
192 }
193 
PicoLoadMedia(const char * filename,const char * carthw_cfg_fname,const char * (* get_bios_filename)(int * region,const char * cd_fname),void (* do_region_override)(const char * media_filename))194 enum media_type_e PicoLoadMedia(const char *filename,
195   const char *carthw_cfg_fname,
196   const char *(*get_bios_filename)(int *region, const char *cd_fname),
197   void (*do_region_override)(const char *media_filename))
198 {
199   const char *rom_fname = filename;
200   enum media_type_e media_type;
201   enum cd_img_type cd_img_type = CIT_NOT_CD;
202   unsigned char *rom_data = NULL;
203   unsigned int rom_size = 0;
204   pm_file *rom = NULL;
205   int cd_region = 0;
206   int ret;
207 
208   media_type = detect_media(filename);
209   if (media_type == PM_BAD_DETECT)
210     goto out;
211 
212   if ((PicoIn.AHW & PAHW_MCD) && Pico_mcd != NULL)
213     cdd_unload();
214   PicoCartUnload();
215   PicoIn.AHW = 0;
216   PicoIn.quirks = 0;
217 
218   if (media_type == PM_CD)
219   {
220     // check for MegaCD image
221     cd_img_type = PicoCdCheck(filename, &cd_region);
222     if ((int)cd_img_type >= 0 && cd_img_type != CIT_NOT_CD)
223     {
224       // valid CD image, ask frontend for BIOS..
225       rom_fname = NULL;
226       if (get_bios_filename != NULL)
227         rom_fname = get_bios_filename(&cd_region, filename);
228       if (rom_fname == NULL) {
229         media_type = PM_BAD_CD_NO_BIOS;
230         goto out;
231       }
232 
233       PicoIn.AHW |= PAHW_MCD;
234     }
235     else {
236       media_type = PM_BAD_CD;
237       goto out;
238     }
239   }
240   else if (media_type == PM_MARK3) {
241     lprintf("detected SMS ROM\n");
242     PicoIn.AHW = PAHW_SMS;
243   }
244 
245   rom = pm_open(rom_fname);
246   if (rom == NULL) {
247     lprintf("Failed to open ROM\n");
248     media_type = PM_ERROR;
249     goto out;
250   }
251 
252   ret = PicoCartLoad(rom, &rom_data, &rom_size, (PicoIn.AHW & PAHW_SMS) ? 1 : 0);
253   pm_close(rom);
254   if (ret != 0) {
255     if      (ret == 2) lprintf("Out of memory\n");
256     else if (ret == 3) lprintf("Read failed\n");
257     else               lprintf("PicoCartLoad() failed.\n");
258     media_type = PM_ERROR;
259     goto out;
260   }
261 
262   // detect wrong files
263   if (strncmp((char *)rom_data, "Pico", 4) == 0) {
264     lprintf("savestate selected?\n");
265     media_type = PM_BAD_DETECT;
266     goto out;
267   }
268 
269   if (!(PicoIn.AHW & PAHW_SMS)) {
270     unsigned short *d = (unsigned short *)(rom_data + 4);
271     if ((((d[0] << 16) | d[1]) & 0xffffff) >= (int)rom_size) {
272       lprintf("bad reset vector\n");
273       media_type = PM_BAD_DETECT;
274       goto out;
275     }
276   }
277 
278   // load config for this ROM (do this before insert to get correct region)
279   if (!(PicoIn.AHW & PAHW_MCD)) {
280     memcpy(media_id_header, rom_data + 0x100, sizeof(media_id_header));
281     if (do_region_override != NULL)
282       do_region_override(filename);
283   }
284 
285   if (PicoCartInsert(rom_data, rom_size, carthw_cfg_fname)) {
286     media_type = PM_ERROR;
287     goto out;
288   }
289   rom_data = NULL; // now belongs to PicoCart
290   Pico.m.ncart_in = 0;
291 
292   // insert CD if it was detected
293   if (cd_img_type != CIT_NOT_CD) {
294     ret = cdd_load(filename, cd_img_type);
295     if (ret != 0) {
296       PicoCartUnload();
297       media_type = PM_BAD_CD;
298       goto out;
299     }
300     Pico.m.ncart_in = 1;
301   }
302 
303   if (PicoIn.quirks & PQUIRK_FORCE_6BTN)
304     PicoSetInputDevice(0, PICO_INPUT_PAD_6BTN);
305 
306 out:
307   if (rom_data)
308     free(rom_data);
309   return media_type;
310 }
311 
312 // vim:shiftwidth=2:ts=2:expandtab
313