4 * GD-Rom image-file common functions.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
23 #include <sys/types.h>
24 #include <netinet/in.h>
26 #include "gdrom/gddriver.h"
27 #include "gdrom/packet.h"
29 #include "bootstrap.h"
31 static void gdrom_image_destroy( gdrom_disc_t disc );
32 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode,
33 unsigned char *buf, uint32_t *readlength );
34 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
35 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
36 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
37 static int gdrom_image_drive_status( gdrom_disc_t disc );
39 static uint8_t gdrom_default_sync[12] = { 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0 };
41 #define SECTOR_HEADER_SIZE 16
42 #define SECTOR_SUBHEADER_SIZE 8
44 /* Data offset (from start of raw sector) by sector mode */
45 static int gdrom_data_offset[] = { 16, 16, 16, 24, 24, 0, 8, 0, 0 };
49 struct cdrom_sector_header {
53 uint8_t subhead[8]; // Mode-2 XA sectors only
57 * Initialize a gdrom_disc structure with the gdrom_image_* methods
59 void gdrom_image_init( gdrom_disc_t disc )
61 memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
62 disc->read_sector = gdrom_image_read_sector;
63 disc->read_toc = gdrom_image_read_toc;
64 disc->read_session = gdrom_image_read_session;
65 disc->read_position = gdrom_image_read_position;
66 disc->drive_status = gdrom_image_drive_status;
67 disc->play_audio = NULL; /* not supported yet */
68 disc->run_time_slice = NULL; /* not needed */
69 disc->close = gdrom_image_destroy;
72 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
74 gdrom_image_t image = (gdrom_image_t)g_malloc0(sizeof(struct gdrom_image));
78 image->disc_type = IDE_DISC_CDROMXA;
80 gdrom_disc_t disc = (gdrom_disc_t)image;
81 gdrom_image_init(disc);
82 if( filename == NULL ) {
85 disc->name = g_strdup(filename);
91 static void gdrom_image_destroy( gdrom_disc_t disc )
94 FILE *lastfile = NULL;
95 gdrom_image_t img = (gdrom_image_t)disc;
96 if( img->file != NULL ) {
100 for( i=0; i<img->track_count; i++ ) {
101 if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
102 lastfile = img->track[i].file;
104 img->track[i].file = NULL;
107 if( disc->name != NULL ) {
108 g_free( (gpointer)disc->name );
114 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
117 FILE *lastfile = NULL;
118 gdrom_image_t img = (gdrom_image_t)disc;
119 if( img->file != NULL ) {
122 for( i=0; i<img->track_count; i++ ) {
123 if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
124 lastfile = img->track[i].file;
126 img->track[i].file = NULL;
129 if( disc->name != NULL ) {
130 g_free( (gpointer)disc->name );
136 int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
139 for( i=0; i<image->track_count; i++ ) {
140 if( image->track[i].lba <= lba &&
141 lba < (image->track[i].lba + image->track[i].sector_count) ) {
149 * Read a block from an image file, handling negative file offsets
152 static gboolean gdrom_read_block( unsigned char *buf, int file_offset, int length, FILE *f )
154 if( file_offset < 0 ) {
155 int size = -file_offset;
156 if( size >= length ) {
157 memset( buf, 0, length );
160 memset( buf, 0, size );
165 fseek( f, file_offset, SEEK_SET );
166 return fread( buf, length, 1, f ) == 1;
169 static void gdrom_build_sector_header( unsigned char *buf, uint32_t lba,
170 gdrom_track_mode_t sector_mode )
172 memcpy( buf, gdrom_default_sync, 12 );
173 cd_build_address( buf, sector_mode, lba );
177 * Return TRUE if the given read mode + track modes are compatible,
179 * @param track_mode one of the GDROM_MODE* constants
180 * @param read_mode the READ_CD_MODE from the read request
182 static gboolean gdrom_is_compatible_read_mode( int track_mode, int read_mode )
184 switch( read_mode ) {
185 case READ_CD_MODE_ANY:
187 case READ_CD_MODE_CDDA:
188 return track_mode == GDROM_CDDA;
190 return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
191 case READ_CD_MODE_2_FORM_1:
192 return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
193 case READ_CD_MODE_2_FORM_2:
194 return track_mode == GDROM_MODE2_FORM2;
196 return track_mode == GDROM_MODE2_FORMLESS;
203 * Determine the start position in a raw sector, and the amount of data to read
204 * in bytes, for a given combination of sector mode and read mode.
206 static void gdrom_get_read_bounds( int sector_mode, int read_mode, int *start, int *size )
208 if( READ_CD_RAW(read_mode) ) {
214 if( READ_CD_DATA(read_mode) ) {
215 *start = gdrom_data_offset[sector_mode];
216 *size = gdrom_sector_size[sector_mode];
219 if( READ_CD_SUBHEAD(read_mode) &&
220 (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
221 *start = SECTOR_HEADER_SIZE;
222 *size += SECTOR_SUBHEADER_SIZE;
225 if( READ_CD_HEADER(read_mode) ) {
226 *size += SECTOR_HEADER_SIZE;
233 void gdrom_extract_raw_data_sector( char *sector_data, int channels, unsigned char *buf, uint32_t *length )
237 struct cdrom_sector_header *secthead = (struct cdrom_sector_header *)sector_data;
238 if( secthead->mode == 1 ) {
239 sector_mode = GDROM_MODE1;
241 sector_mode = ((secthead->subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
243 gdrom_get_read_bounds( sector_mode, channels, &start, &size );
245 memcpy( buf, sector_data+start, size );
250 * Read a single sector from a disc image. If you thought this would be simple,
251 * I have just one thing to say to you: Bwahahahahahahahah.
253 * Once we've decided that there's a real sector at the requested lba, there's
254 * really two things we need to care about:
255 * 1. Is the sector mode compatible with the requested read mode
256 * 2. Which parts of the sector do we need to return?
257 * (header/subhead/data/raw sector)
259 * Also note that the disc image may supply us with just the data (most common
260 * case), or may have the full raw sector. In the former case we may need to
261 * generate the missing data on the fly, for which we use libedc to compute the
262 * data correction codes.
264 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
265 int mode, unsigned char *buf, uint32_t *length )
267 gdrom_image_t image = (gdrom_image_t)disc;
268 struct cdrom_sector_header secthead;
269 int file_offset, read_len, track_no;
273 track_no = gdrom_image_get_track_by_lba( image, lba );
274 if( track_no == -1 ) {
275 return PKT_ERR_BADREAD;
277 struct gdrom_track *track = &image->track[track_no-1];
278 file_offset = track->offset + track->sector_size * (lba - track->lba);
279 read_len = track->sector_size;
280 if( track->file != NULL ) {
286 /* First figure out what the real sector mode is for raw/semiraw sectors */
288 switch( track->mode ) {
289 case GDROM_RAW_NONXA:
290 gdrom_read_block( (unsigned char *)(§head), file_offset, sizeof(secthead), f );
291 sector_mode = (secthead.mode == 1) ? GDROM_MODE1 : GDROM_MODE2_FORMLESS;
294 gdrom_read_block( (unsigned char *)(§head), file_offset, sizeof(secthead), f );
295 if( secthead.mode == 1 ) {
296 sector_mode = GDROM_MODE1;
298 sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
301 case GDROM_SEMIRAW_MODE2:
302 gdrom_read_block( secthead.subhead, file_offset, 8, f );
303 sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
306 /* In the other cases, the track mode completely defines the sector mode */
307 sector_mode = track->mode;
311 if( !gdrom_is_compatible_read_mode(sector_mode, READ_CD_MODE(mode)) ) {
312 return PKT_ERR_BADREADMODE;
315 /* Ok, we've got a valid sector, check what parts of the sector we need to
316 * return - header | subhead | data | everything
318 int channels = READ_CD_CHANNELS(mode);
320 if( channels == 0 ) {
324 } else if( channels == 0xA0 &&
325 (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2 )) {
326 // caller requested a non-contiguous region
327 return PKT_ERR_BADFIELD;
328 } else if( READ_CD_RAW(channels) ) {
329 channels = 0xF0; // implies everything
334 switch( track->mode ) {
336 // audio is nice and simple (assume perfect reads for now)
338 gdrom_read_block( buf, file_offset, track->sector_size, f );
341 case GDROM_RAW_NONXA:
342 gdrom_get_read_bounds( sector_mode, channels, &start, &size );
343 gdrom_read_block( buf, file_offset+start, size, f );
346 case GDROM_SEMIRAW_MODE2:
347 gdrom_get_read_bounds( sector_mode, channels, &start, &size );
348 if( READ_CD_HEADER(channels) ) {
349 gdrom_build_sector_header( buf, lba, sector_mode );
350 read_len += SECTOR_HEADER_SIZE;
351 size -= SECTOR_HEADER_SIZE;
353 start -= SECTOR_HEADER_SIZE;
355 gdrom_read_block( buf + read_len, file_offset+start, size, f );
358 default: // Data track w/ data only in file
359 if( READ_CD_RAW(channels) ) {
360 gdrom_read_block( buf + gdrom_data_offset[track->mode], file_offset,
361 track->sector_size, f );
362 do_encode_L2( buf, sector_mode, lba );
365 if( READ_CD_HEADER(channels) ) {
366 gdrom_build_sector_header( buf, lba, sector_mode );
367 read_len += SECTOR_HEADER_SIZE;
369 if( READ_CD_SUBHEAD(channels) &&
370 (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
371 if( sector_mode == GDROM_MODE2_FORM1 ) {
372 *((uint32_t *)(buf+read_len)) = 0;
373 *((uint32_t *)(buf+read_len+4)) = 0;
375 *((uint32_t *)(buf+read_len)) = 0x00200000;
376 *((uint32_t *)(buf+read_len+4)) = 0x00200000;
380 if( READ_CD_DATA(channels) ) {
381 gdrom_read_block( buf+read_len, file_offset, track->sector_size, f );
382 read_len += track->sector_size;
390 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf )
392 gdrom_image_t image = (gdrom_image_t)disc;
393 struct gdrom_toc *toc = (struct gdrom_toc *)buf;
396 for( i=0; i<image->track_count; i++ ) {
397 toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
399 toc->first = 0x0100 | image->track[0].flags;
400 toc->last = (image->track_count<<8) | image->track[i-1].flags;
401 toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
402 image->track[i-1].flags;
404 toc->track[i] = 0xFFFFFFFF;
408 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
410 gdrom_image_t image = (gdrom_image_t)disc;
411 struct gdrom_track *last_track = &image->track[image->track_count-1];
412 unsigned int end_of_disc = last_track->lba + last_track->sector_count;
414 buf[0] = 0x01; /* Disc status? */
418 buf[2] = last_track->session+1; /* last session */
419 buf[3] = (end_of_disc >> 16) & 0xFF;
420 buf[4] = (end_of_disc >> 8) & 0xFF;
421 buf[5] = end_of_disc & 0xFF;
425 for( i=0; i<image->track_count; i++ ) {
426 if( image->track[i].session == session ) {
427 buf[2] = i+1; /* first track of session */
428 buf[3] = (image->track[i].lba >> 16) & 0xFF;
429 buf[4] = (image->track[i].lba >> 8) & 0xFF;
430 buf[5] = image->track[i].lba & 0xFF;
434 return PKT_ERR_BADFIELD; /* No such session */
438 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
440 gdrom_image_t image = (gdrom_image_t)disc;
441 int track_no = gdrom_image_get_track_by_lba( image, lba );
442 if( track_no == -1 ) {
446 struct gdrom_track *track = &image->track[track_no-1];
447 uint32_t offset = lba - track->lba;
448 buf[4] = track->flags;
450 buf[6] = 0x01; /* ?? */
451 buf[7] = (offset >> 16) & 0xFF;
452 buf[8] = (offset >> 8) & 0xFF;
453 buf[9] = offset & 0xFF;
455 buf[11] = (lba >> 16) & 0xFF;
456 buf[12] = (lba >> 8) & 0xFF;
457 buf[13] = lba & 0xFF;
461 static int gdrom_image_drive_status( gdrom_disc_t disc )
463 gdrom_image_t image = (gdrom_image_t)disc;
464 if( image->disc_type == IDE_DISC_NONE ) {
465 return IDE_DISC_NONE;
467 return image->disc_type | IDE_DISC_READY;
471 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name )
473 struct gdrom_device *dev = g_malloc0( sizeof(struct gdrom_device) );
474 dev->name = g_strdup(name);
475 dev->device_name = g_strdup(dev_name);
479 void gdrom_device_destroy( gdrom_device_t dev )
481 if( dev->name != NULL ) {
485 if( dev->device_name != NULL ) {
486 g_free( dev->device_name );
487 dev->device_name = NULL;
493 * Check the disc for a useable DC bootstrap, and update the disc
494 * with the title accordingly.
495 * @return TRUE if we found a bootstrap, otherwise FALSE.
497 gboolean gdrom_image_read_info( gdrom_disc_t d ) {
498 gdrom_image_t disc = (gdrom_image_t)d;
499 if( disc->track_count > 0 ) {
500 /* Find the first data track of the last session */
501 int last_session = disc->track[disc->track_count-1].session;
502 int i, boot_track = -1;
503 for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
504 if( disc->track[i].flags & TRACK_DATA ) {
508 if( boot_track != -1 ) {
509 unsigned char boot_sector[MAX_SECTOR_SIZE];
510 uint32_t length = sizeof(boot_sector);
511 if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
512 boot_sector, &length ) == PKT_ERR_OK ) {
513 if( memcmp( boot_sector, "SEGA SEGAKATANA SEGA ENTERPRISES", 32) == 0 ) {
515 memcpy( d->title, boot_sector+128, 128 );
516 for( i=127; i>=0; i-- ) {
517 if( !isspace(d->title[i]) )
520 d->title[i+1] = '\0';
522 bootstrap_dump(boot_sector, FALSE);
.