2 // CD Interface handler
5 // GCC/SDL port by Niels Wagenaar (Linux/WIN32) and Caz (BeOS)
6 // Cleanups by James L. Hammons
11 /* Added by SDLEMU (http://sdlemu.ngemu.com) */
12 /* Added for GCC UNIX compatibility */
15 #include <sys/types.h>
21 #define CDI_V2 0x80000004
22 #define CDI_V3 0x80000005
23 #define CDI_V35 0x80000006
25 static uint8 cdi_track_start_mark[10] =
26 { 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF };
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;
36 int cdi_open(char * path)
38 WriteLog("CDI: Opening %s\n", path);
39 return open(path, O_BINARY | O_RDONLY);
42 void cdi_close(int fp)
44 WriteLog("CDI: Closing\n");
49 s_cdi_descriptor * cdi_get_descriptor(int fp, FILE * stdfp)
51 s_cdi_descriptor * descriptor;
52 uint32 previous_position = 0;
57 descriptor = (s_cdi_descriptor *)malloc(sizeof(s_cdi_descriptor));
59 if (descriptor == NULL)
62 lseek(fp, 0, SEEK_END);
63 descriptor->length = lseek(fp, 0LL, SEEK_CUR);
65 if (descriptor->length < 8)
68 fprintf(stdfp, "CDI: Image is too short (%i bytes)\n", (int)descriptor->length);
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);
79 descriptor->sessions=(s_cdi_session *)malloc(descriptor->nb_of_sessions * sizeof(s_cdi_session));
81 if (descriptor->sessions == NULL)
88 fprintf(stdfp, "CDI: %i sessions\n", descriptor->nb_of_sessions);
90 uint32 track_position = 0;
91 for(uint16 session=0; session<descriptor->nb_of_sessions; session++)
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));
97 fprintf(stdfp, "CDI:\nCDI: Reading session %i (%i tracks)\n", session, descriptor->sessions[session].nb_of_tracks);
99 for (uint16 track=0; track<descriptor->sessions[session].nb_of_tracks; track++)
101 static char current_start_mark[10];
102 s_cdi_track * current_track=&descriptor->sessions[session].tracks[track];
104 fprintf(stdfp, "CDI:\nCDI: \t\tReading track %i\n",track);
108 read(fp, &temp_value, 4);
111 lseek(fp, 8, SEEK_CUR); // extra data (DJ 3.00.780 and up)
113 read(fp, current_start_mark, 10);
115 if (memcmp(cdi_track_start_mark, current_start_mark, 10))
118 fprintf(stdfp, "CDI: Could not find the track start mark\n");
122 read(fp, current_start_mark, 10);
124 if (memcmp(cdi_track_start_mark, current_start_mark, 10))
127 fprintf(stdfp, "CDI: Could not find the track start mark\n");
131 lseek(fp, 4, SEEK_CUR);
132 read(fp, ¤t_track->filename_length, 1);
133 lseek(fp, current_track->filename_length, SEEK_CUR);
135 lseek(fp, 11, SEEK_CUR);
136 lseek(fp, 4, SEEK_CUR);
137 lseek(fp, 4, SEEK_CUR);
138 read(fp, &temp_value, 4);
140 if (temp_value == 0x80000000)
141 lseek(fp, 8, SEEK_CUR); // DJ4
143 lseek(fp, 2, SEEK_CUR);
145 read(fp, ¤t_track->pregap_length, 4);
147 if (current_track->pregap_length != 150)
148 WriteLog("CDI: Warning: Pregap different than 150\n");
150 read(fp, ¤t_track->length, 4);
153 fprintf(stdfp, "CDI: \t\t\tPregap length: %i\n", (int)current_track->pregap_length);
155 fprintf(stdfp, "CDI: \t\t\tLength: %i\n", (int)current_track->length);
157 lseek(fp, 6, SEEK_CUR);
159 read(fp, ¤t_track->mode, 4);
162 fprintf(stdfp, "CDI: \t\t\tMode: %i\n", (int)current_track->mode);
164 lseek(fp, 12, SEEK_CUR);
165 read(fp, ¤t_track->start_lba, 4);
168 fprintf(stdfp, "CDI: \t\t\tStart LBA: %i\n", (int)current_track->start_lba);
170 read(fp, ¤t_track->total_length, 4);
173 fprintf(stdfp, "CDI: \t\t\tTotal length: %i\n", (int)current_track->total_length);
175 lseek(fp, 16, SEEK_CUR);
176 read(fp, ¤t_track->sector_size_value, 4);
179 fprintf(stdfp, "CDI: \t\t\tSector size: %i\n", (int)current_track->sector_size_value);
181 switch(current_track->sector_size_value)
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;
189 fprintf(stdfp, "CDI: \t\t\tUnsupported %i bytes sector\n", (int)current_track->sector_size_value);
194 if (current_track->mode > 2)
196 fprintf(stdfp, "CDI: \t\t\tTrack mode %i not supported\n", (int)current_track->mode);
198 lseek(fp, 29, SEEK_CUR);
200 if (descriptor->version != CDI_V2)
202 lseek(fp, 5, SEEK_CUR);
203 read(fp, &temp_value, 4);
205 if (temp_value == 0xFFFFFFFF)
206 lseek(fp, 78, SEEK_CUR); // extra data (DJ 3.00.780 and up)
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;
214 lseek(fp, 4, SEEK_CUR);
215 lseek(fp, 8, SEEK_CUR);
217 if (descriptor->version != CDI_V2)
218 lseek(fp, 1, SEEK_CUR);
222 if (descriptor->header_offset == 0)
228 void cdi_dump_descriptor(FILE * fp, s_cdi_descriptor * cdi_descriptor)
230 fprintf(fp, "CDI: %i Mb\n", (int)(cdi_descriptor->length >> 20));
231 fprintf(fp, "CDI: Format version ");
232 switch(cdi_descriptor->version)
241 fprintf(fp, "3.5\n");
244 fprintf(fp, "unknown\n");
246 fprintf(fp, "CDI: %i sessions\n", cdi_descriptor->nb_of_sessions);
249 uint8 * cdi_extract_boot_code(int fp, s_cdi_descriptor * cdi_descriptor)
251 s_cdi_track * boot_track = &cdi_descriptor->sessions[1].tracks[0];
252 uint32 boot_track_size = boot_track->length * boot_track->sector_size;
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);
258 uint32 * boot_track_data_32 = (uint32 *)boot_track_data;
260 while (offset < (boot_track_size >> 2))
262 if (boot_track_data_32[offset] == 0x49205452)
266 if (offset == (boot_track_size >> 2))
268 WriteLog("CDI: Cannot find the boot code\n");
272 //This is likely wrong, but won't get a chance to fix it until I can get a CD image
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");
287 for(uint32 i=0; i<(cdi_code_length >> 1); i++)
289 uint16 sdata = data16[i];
290 sdata = (sdata >> 8) | (sdata << 8);
294 return (uint8 *)data16;
297 void cdi_load_sector(uint32 sector, uint8 * buffer)
299 if (sector == 0xFFFFFFFF)
301 memset(buffer, 0x00, 2352);
307 lseek(cdi_fp, 2+sector, SEEK_SET);
308 read(cdi_fp, buffer, 2352);
310 //This is probably not needed any more...
312 for (uint32 i=0;i<2352;i+=2)
314 uint8 sdata=buffer[i+0];
315 buffer[i+0]=buffer[i+1];