]> Shamusworld >> Repos - virtualjaguar/blob - src/cdi.cpp
Changed to allow no sound system init
[virtualjaguar] / src / cdi.cpp
1 //
2 // CD Interface handler
3 //
4 // by David Raingeard
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
7 //
8
9 #include "cdi.h"
10
11 /* Added by SDLEMU (http://sdlemu.ngemu.com) */
12 /* Added for GCC UNIX compatibility          */
13 #ifdef __GCCUNIX__
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <fcntl.h>
17
18 #define O_BINARY (0)
19 #endif
20
21 #define CDI_V2  0x80000004
22 #define CDI_V3  0x80000005
23 #define CDI_V35 0x80000006
24
25 static uint8 cdi_track_start_mark[10] =
26         { 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
27
28 int cdi_fp;
29 uint32 cdi_load_address;
30 uint32 cdi_code_length;
31 s_cdi_descriptor * cdi_descriptor;
32 s_cdi_track ** cdi_tracks;
33 uint32 cdi_tracks_count;
34
35
36 int cdi_open(char * path)
37 {
38         WriteLog("CDI: Opening %s\n", path);
39         return open(path, O_BINARY | O_RDONLY);
40 }
41
42 void cdi_close(int fp)
43 {
44         WriteLog("CDI: Closing\n");
45         close(fp);
46 }
47
48
49 s_cdi_descriptor * cdi_get_descriptor(int fp, FILE * stdfp)
50 {
51         s_cdi_descriptor * descriptor;
52         uint32 previous_position = 0;
53
54         if (fp == -1)
55                 return 0;
56
57         descriptor = (s_cdi_descriptor *)malloc(sizeof(s_cdi_descriptor));
58
59         if (descriptor == NULL)
60                 return 0;
61
62         lseek(fp, 0, SEEK_END);
63         descriptor->length = lseek(fp, 0LL, SEEK_CUR);
64
65         if (descriptor->length < 8)
66         {
67                 if (stdfp)
68                         fprintf(stdfp, "CDI: Image is too short (%i bytes)\n", (int)descriptor->length);
69                 free(descriptor);
70                 return 0;
71         }
72
73         lseek(fp, descriptor->length - 8, SEEK_SET);
74         read(fp, &descriptor->version, 4);
75         read(fp, &descriptor->header_offset, 4);
76         lseek(fp, descriptor->header_offset, SEEK_SET);
77         read(fp, &descriptor->nb_of_sessions, 2);
78
79         descriptor->sessions=(s_cdi_session *)malloc(descriptor->nb_of_sessions * sizeof(s_cdi_session));
80
81         if (descriptor->sessions == NULL)
82         {
83                 free(descriptor);
84                 return 0;
85         }
86
87         if (stdfp)
88                 fprintf(stdfp, "CDI: %i sessions\n", descriptor->nb_of_sessions);
89
90         uint32 track_position = 0;
91         for(uint16 session=0; session<descriptor->nb_of_sessions; session++)
92         {
93                 read(fp, &descriptor->sessions[session].nb_of_tracks, 2);
94                 descriptor->sessions[session].tracks = (s_cdi_track *)malloc(descriptor->sessions[session].nb_of_tracks * sizeof(s_cdi_track));
95
96                 if (stdfp)
97                         fprintf(stdfp, "CDI:\nCDI: Reading session %i (%i tracks)\n", session, descriptor->sessions[session].nb_of_tracks);
98
99                 for (uint16 track=0; track<descriptor->sessions[session].nb_of_tracks; track++)
100                 {
101                         static char current_start_mark[10];
102                         s_cdi_track * current_track=&descriptor->sessions[session].tracks[track];
103                         if (stdfp)
104                                 fprintf(stdfp, "CDI:\nCDI: \t\tReading track %i\n",track);
105
106                         uint32 temp_value;
107
108                         read(fp, &temp_value, 4);
109
110                         if (temp_value != 0)
111                                 lseek(fp, 8, SEEK_CUR); // extra data (DJ 3.00.780 and up)
112         
113                         read(fp, current_start_mark, 10);
114
115                         if (memcmp(cdi_track_start_mark, current_start_mark, 10)) 
116                         {
117                                 if (stdfp)
118                                         fprintf(stdfp, "CDI: Could not find the track start mark\n");
119                                 return 0;
120                         }
121
122                         read(fp, current_start_mark, 10);
123
124                         if (memcmp(cdi_track_start_mark, current_start_mark, 10)) 
125                         {
126                                 if (stdfp)
127                                         fprintf(stdfp, "CDI: Could not find the track start mark\n");
128                                 return 0;
129                         }
130
131                         lseek(fp, 4, SEEK_CUR);
132                         read(fp, &current_track->filename_length, 1);
133                         lseek(fp, current_track->filename_length, SEEK_CUR);
134
135                         lseek(fp, 11, SEEK_CUR);
136                         lseek(fp, 4, SEEK_CUR);
137                         lseek(fp, 4, SEEK_CUR);
138                         read(fp, &temp_value, 4);
139
140                         if (temp_value == 0x80000000)
141                                 lseek(fp, 8, SEEK_CUR); // DJ4
142
143                         lseek(fp, 2, SEEK_CUR);
144                         
145                         read(fp, &current_track->pregap_length, 4);
146
147                         if (current_track->pregap_length != 150)
148                                 WriteLog("CDI: Warning: Pregap different than 150\n");
149
150                         read(fp, &current_track->length, 4);
151
152                         if (stdfp)
153                                 fprintf(stdfp, "CDI: \t\t\tPregap length: %i\n", (int)current_track->pregap_length);
154                         if (stdfp)
155                                 fprintf(stdfp, "CDI: \t\t\tLength: %i\n", (int)current_track->length);
156
157                         lseek(fp, 6, SEEK_CUR);
158                         
159                         read(fp, &current_track->mode, 4);
160
161                         if (stdfp)
162                                 fprintf(stdfp, "CDI: \t\t\tMode: %i\n", (int)current_track->mode);
163
164                         lseek(fp, 12, SEEK_CUR);
165                         read(fp, &current_track->start_lba, 4);
166
167                         if (stdfp)
168                                 fprintf(stdfp, "CDI: \t\t\tStart LBA: %i\n", (int)current_track->start_lba);
169
170                         read(fp, &current_track->total_length, 4);
171
172                         if (stdfp)
173                                 fprintf(stdfp, "CDI: \t\t\tTotal length: %i\n", (int)current_track->total_length);
174
175                         lseek(fp, 16, SEEK_CUR);
176                         read(fp, &current_track->sector_size_value, 4);
177
178                         if (stdfp)
179                                 fprintf(stdfp, "CDI: \t\t\tSector size: %i\n", (int)current_track->sector_size_value);
180
181                         switch(current_track->sector_size_value)
182                         {
183                         case 0: current_track->sector_size = 2048; break;
184                         case 1: current_track->sector_size = 2336; break;
185                         case 2: current_track->sector_size = 2352; break;
186                         default:
187                         {
188                                 if (stdfp)
189                                         fprintf(stdfp, "CDI: \t\t\tUnsupported %i bytes sector\n", (int)current_track->sector_size_value);
190                                 return 0;
191                         }
192                         }
193                         
194                         if (current_track->mode > 2)
195                                 if (stdfp)
196                                         fprintf(stdfp, "CDI: \t\t\tTrack mode %i not supported\n", (int)current_track->mode);
197
198                         lseek(fp, 29, SEEK_CUR);
199
200                         if (descriptor->version != CDI_V2)
201                         {
202                                 lseek(fp, 5, SEEK_CUR);
203                                 read(fp, &temp_value, 4);
204
205                                 if (temp_value == 0xFFFFFFFF)
206                                         lseek(fp, 78, SEEK_CUR); // extra data (DJ 3.00.780 and up)
207                         }
208
209                         current_track->position = track_position;
210                         track_position += (current_track->pregap_length+current_track->length) * current_track->sector_size;
211                         previous_position = track_position;
212                 }
213
214                 lseek(fp, 4, SEEK_CUR);
215                 lseek(fp, 8, SEEK_CUR);
216
217                 if (descriptor->version != CDI_V2)
218                         lseek(fp, 1, SEEK_CUR);
219
220         }
221
222     if (descriptor->header_offset == 0) 
223                 return 0;
224
225         return descriptor;
226 }
227
228 void cdi_dump_descriptor(FILE * fp, s_cdi_descriptor * cdi_descriptor)
229 {
230         fprintf(fp, "CDI: %i Mb\n", (int)(cdi_descriptor->length >> 20));
231         fprintf(fp, "CDI: Format version ");
232         switch(cdi_descriptor->version)
233         {
234         case CDI_V2:
235                 fprintf(fp, "2\n");
236                 break;
237         case CDI_V3:
238                 fprintf(fp, "3\n");
239                 break;
240         case CDI_V35:
241                 fprintf(fp, "3.5\n");
242                 break;
243         default:
244                 fprintf(fp, "unknown\n");
245         }
246         fprintf(fp, "CDI: %i sessions\n", cdi_descriptor->nb_of_sessions);
247 }
248
249 uint8 * cdi_extract_boot_code(int fp, s_cdi_descriptor * cdi_descriptor)
250 {
251         s_cdi_track * boot_track = &cdi_descriptor->sessions[1].tracks[0];
252         uint32 boot_track_size = boot_track->length * boot_track->sector_size;
253
254         uint8 * boot_track_data = (uint8 *)malloc(boot_track_size);
255         lseek(fp, 2 + (boot_track->position), SEEK_SET);
256         read(fp, boot_track_data, boot_track_size);
257         
258         uint32 * boot_track_data_32 = (uint32 *)boot_track_data;
259         uint32 offset = 0;
260         while (offset < (boot_track_size >> 2))
261         {
262                 if (boot_track_data_32[offset] == 0x49205452)
263                         break;
264                 offset++;
265         }
266         if (offset == (boot_track_size >> 2))
267         {
268                 WriteLog("CDI: Cannot find the boot code\n");
269                 return NULL;
270         }
271
272 //This is likely wrong, but won't get a chance to fix it until I can get a CD image
273 //to work with...
274         offset = (offset << 2) + 4;
275         uint16 * data16 = (uint16 *)&boot_track_data[offset];
276         cdi_load_address = *data16++;
277         cdi_load_address <<= 16;
278         cdi_load_address |= *data16++;
279         cdi_code_length = *data16++;
280         cdi_code_length <<= 16;
281         cdi_code_length |= *data16++;
282         WriteLog("CDI: Load address: %08X\n", cdi_load_address);
283         WriteLog("CDI: Length: %08X\n", cdi_code_length);
284 //No need for byte swapping any more...
285 /*      WriteLog("cdi: byte swapping boot code\n");
286
287         for(uint32 i=0; i<(cdi_code_length >> 1); i++)
288         {
289                 uint16 sdata = data16[i];
290                 sdata = (sdata >> 8) | (sdata << 8);
291                 data16[i] = sdata;
292         }*/
293
294         return (uint8 *)data16;
295 }
296
297 void cdi_load_sector(uint32 sector, uint8 * buffer)
298 {
299         if (sector == 0xFFFFFFFF)
300         {
301                 memset(buffer, 0x00, 2352);
302                 return;
303         }
304
305         sector *= 2352;
306
307         lseek(cdi_fp, 2+sector, SEEK_SET);
308         read(cdi_fp, buffer, 2352);
309
310 //This is probably not needed any more...
311 /*      // byte swap
312         for (uint32 i=0;i<2352;i+=2)
313         {
314                 uint8 sdata=buffer[i+0];
315                 buffer[i+0]=buffer[i+1];
316                 buffer[i+1]=sdata;
317         }*/
318 }