Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 644:ccae4bfa5f82
prev640:01b50fd48035
next669:ab344e42bca9
author nkeynes
date Sun Mar 02 11:38:08 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Bug #59: Add provisional support for all CD read modes (not validated)
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GD-Rom image-file common functions. 
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <assert.h>
    20 #include <netinet/in.h>
    22 #include "gdrom/gdrom.h"
    23 #include "gdrom/packet.h"
    24 #include "ecc.h"
    25 #include "bootstrap.h"
    27 static void gdrom_image_destroy( gdrom_disc_t disc );
    28 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode, 
    29 					      unsigned char *buf, uint32_t *readlength );
    30 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
    31 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
    32 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
    33 static int gdrom_image_drive_status( gdrom_disc_t disc );
    35 static uint8_t gdrom_default_sync[12] = { 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0 };
    37 #define SECTOR_HEADER_SIZE 16
    38 #define SECTOR_SUBHEADER_SIZE 8
    40 /* Data offset (from start of raw sector) by sector mode */
    41 static int gdrom_data_offset[] = { 16, 16, 16, 24, 24, 0, 8, 0, 0 };
    45 struct cdrom_sector_header {
    46     uint8_t sync[12];
    47     uint8_t msf[3];
    48     uint8_t mode;
    49     uint8_t subhead[8]; // Mode-2 XA sectors only
    50 };
    52 /**
    53  * Initialize a gdrom_disc structure with the gdrom_image_* methods
    54  */
    55 void gdrom_image_init( gdrom_disc_t disc )
    56 {
    57     memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
    58     disc->read_sector = gdrom_image_read_sector;
    59     disc->read_toc = gdrom_image_read_toc;
    60     disc->read_session = gdrom_image_read_session;
    61     disc->read_position = gdrom_image_read_position;
    62     disc->drive_status = gdrom_image_drive_status;
    63     disc->play_audio = NULL; /* not supported yet */
    64     disc->run_time_slice = NULL; /* not needed */
    65     disc->close = gdrom_image_destroy;
    66 }
    68 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
    69 {
    70     gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
    71     if( image == NULL ) {
    72 	return NULL;
    73     }
    74     image->disc_type = IDE_DISC_CDROMXA;
    75     image->file = f;
    76     gdrom_disc_t disc = (gdrom_disc_t)image;
    77     gdrom_image_init(disc);
    78     if( filename == NULL ) {
    79 	disc->name = NULL;
    80     } else {
    81 	disc->name = g_strdup(filename);
    82     }
    84     return disc;
    85 }
    87 static void gdrom_image_destroy( gdrom_disc_t disc )
    88 {
    89     int i;
    90     FILE *lastfile = NULL;
    91     gdrom_image_t img = (gdrom_image_t)disc;
    92     if( img->file != NULL ) {
    93 	fclose(img->file);
    94 	img->file = NULL;
    95     }
    96     for( i=0; i<img->track_count; i++ ) {
    97 	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
    98 	    lastfile = img->track[i].file;
    99 	    fclose(lastfile);
   100 	    img->track[i].file = NULL;
   101 	}
   102     }
   103     if( disc->name != NULL ) {
   104 	g_free( (gpointer)disc->name );
   105 	disc->name = NULL;
   106     }
   107     free( disc );
   108 }
   110 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
   111 {
   112     int i;
   113     FILE *lastfile = NULL;
   114     gdrom_image_t img = (gdrom_image_t)disc;
   115     if( img->file != NULL ) {
   116 	img->file = NULL;
   117     }
   118     for( i=0; i<img->track_count; i++ ) {
   119 	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
   120 	    lastfile = img->track[i].file;
   121 	    fclose(lastfile);
   122 	    img->track[i].file = NULL;
   123 	}
   124     }
   125     if( disc->name != NULL ) {
   126 	g_free( (gpointer)disc->name );
   127 	disc->name = NULL;
   128     }
   129     free( disc );
   130 }
   132 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
   133 {
   134     int i;
   135     for( i=0; i<image->track_count; i++ ) {
   136 	if( image->track[i].lba <= lba && 
   137 	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
   138 	    return i+1;
   139 	}
   140     }
   141     return -1;
   142 }
   144 /**
   145  * Read a block from an image file, handling negative file offsets
   146  * with 0-fill.
   147  */
   148 static gboolean gdrom_read_block( unsigned char *buf, int file_offset, int length, FILE *f )
   149 {
   150     if( file_offset < 0 ) {
   151 	int size = -file_offset;
   152 	if( size >= length ) {
   153 	    memset( buf, 0, length );
   154 	    return;
   155 	} else {
   156 	    memset( buf, 0, size );
   157 	    file_offset = 0;
   158 	    length -= size;
   159 	}
   160     }
   161     fseek( f, file_offset, SEEK_SET );
   162     return fread( buf, length, 1, f ) == 1;
   163 }
   165 static void gdrom_build_sector_header( unsigned char *buf, uint32_t lba, 
   166 				       gdrom_track_mode_t sector_mode )
   167 {
   168     memcpy( buf, gdrom_default_sync, 12 );
   169     cd_build_address( buf, sector_mode, lba );
   170 }
   172 /**
   173  * Return TRUE if the given read mode + track modes are compatible,
   174  * otherwise FALSE.
   175  * @param track_mode one of the GDROM_MODE* constants
   176  * @param read_mode the READ_CD_MODE from the read request
   177  */
   178 static gboolean gdrom_is_compatible_read_mode( int track_mode, int read_mode )
   179 {
   180     switch( read_mode ) {
   181     case READ_CD_MODE_ANY:
   182 	return TRUE;
   183     case READ_CD_MODE_CDDA:
   184 	return track_mode == GDROM_CDDA;
   185     case READ_CD_MODE_1:
   186 	return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
   187     case READ_CD_MODE_2_FORM_1:
   188 	return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
   189     case READ_CD_MODE_2_FORM_2:
   190 	return track_mode == GDROM_MODE2_FORM2;
   191     case READ_CD_MODE_2:
   192 	return track_mode == GDROM_MODE2_FORMLESS;
   193     default:
   194 	return FALSE;
   195     }
   196 }
   198 /**
   199  * Determine the start position in a raw sector, and the amount of data to read
   200  * in bytes, for a given combination of sector mode and read mode.
   201  */ 
   202 static void gdrom_get_read_bounds( int sector_mode, int read_mode, int *start, int *size )
   203 {
   204     if( READ_CD_RAW(read_mode) ) {
   205 	// whole sector
   206 	*start = 0;
   207 	*size = 2352;
   208     } else {
   209 	*size = 0;
   210 	if( READ_CD_DATA(read_mode) ) {
   211 	    *start = gdrom_data_offset[sector_mode];
   212 	    *size = gdrom_sector_size[sector_mode];
   213 	}
   215 	if( READ_CD_SUBHEAD(read_mode) && 
   216 	    (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   217 	    *start = SECTOR_HEADER_SIZE;
   218 	    *size += SECTOR_SUBHEADER_SIZE;
   219 	}
   221 	if( READ_CD_HEADER(read_mode) ) {
   222 	    *size += SECTOR_HEADER_SIZE;
   223 	    *start = 0;
   224 	}
   226     }
   227 }
   229 /**
   230  * Read a single sector from a disc image. If you thought this would be simple, 
   231  * I have just one thing to say to you: Bwahahahahahahahah.
   232  *
   233  * Once we've decided that there's a real sector at the requested lba, there's 
   234  * really two things we need to care about:
   235  *   1. Is the sector mode compatible with the requested read mode
   236  *   2. Which parts of the sector do we need to return? 
   237  *      (header/subhead/data/raw sector)
   238  *
   239  * Also note that the disc image may supply us with just the data (most common 
   240  * case), or may have the full raw sector. In the former case we may need to 
   241  * generate the missing data on the fly, for which we use libedc to compute the
   242  * data correction codes.
   243  */
   244 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
   245 					      int mode, unsigned char *buf, uint32_t *length )
   246 {
   247     gdrom_image_t image = (gdrom_image_t)disc;
   248     struct cdrom_sector_header secthead;
   249     int file_offset, read_len, track_no;
   251     FILE *f;
   253     track_no = gdrom_image_get_track_by_lba( image, lba );
   254     if( track_no == -1 ) {
   255 	return PKT_ERR_BADREAD;
   256     }
   257     struct gdrom_track *track = &image->track[track_no-1];
   258     file_offset = track->offset + track->sector_size * (lba - track->lba);
   259     read_len = track->sector_size;
   260     if( track->file != NULL ) {
   261 	f = track->file;
   262     } else {
   263 	f = image->file;
   264     }
   266     /* First figure out what the real sector mode is for raw/semiraw sectors */
   267     int sector_mode;
   268     switch( track->mode ) {
   269     case GDROM_RAW_NONXA:
   270 	gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   271 	sector_mode = (secthead.mode == 1) ? GDROM_MODE1 : GDROM_MODE2_FORMLESS;
   272 	break;
   273     case GDROM_RAW_XA:
   274 	gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   275 	if( secthead.mode == 1 ) {
   276 	    sector_mode = GDROM_MODE1;
   277 	} else {
   278 	    sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   279 	}
   280 	break;
   281     case GDROM_SEMIRAW_MODE2:
   282 	gdrom_read_block( secthead.subhead, file_offset, 8, f );
   283 	sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   284 	break;
   285     default:
   286 	/* In the other cases, the track mode completely defines the sector mode */
   287 	sector_mode = track->mode;
   288 	break;
   289     }
   291     int mode_restrict = READ_CD_MODE(mode);
   292     if( !gdrom_is_compatible_read_mode(sector_mode, READ_CD_MODE(mode)) ) {
   293 	return PKT_ERR_BADREADMODE;
   294     }
   296     /* Ok, we've got a valid sector, check what parts of the sector we need to
   297      * return - header | subhead | data | everything
   298      */
   299     int channels = READ_CD_CHANNELS(mode);
   301     if( channels == 0 ) {
   302 	// legal, if weird
   303 	*length = 0;
   304 	return PKT_ERR_OK;
   305     } else if( channels == 0xA0 && 
   306 	(sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2 )) {
   307 	// caller requested a non-contiguous region
   308 	return PKT_ERR_BADFIELD;
   309     } else if( READ_CD_RAW(channels) ) {
   310 	channels = 0xF0; // implies everything
   311     }
   313     read_len = 0;
   314     int start, size;
   315     switch( track->mode ) {
   316     case GDROM_CDDA:
   317 	// audio is nice and simple (assume perfect reads for now)
   318 	*length = 2352;
   319 	gdrom_read_block( buf, file_offset, track->sector_size, f );
   320 	return PKT_ERR_OK;
   321     case GDROM_RAW_XA:
   322     case GDROM_RAW_NONXA:
   323 	gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   324 	gdrom_read_block( buf, file_offset+start, size, f );
   325 	read_len = size;
   326 	break;
   327     case GDROM_SEMIRAW_MODE2:
   328 	gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   329 	if( READ_CD_HEADER(channels) ) {
   330 	    gdrom_build_sector_header( buf, lba, sector_mode );
   331 	    read_len += SECTOR_HEADER_SIZE;
   332 	    size -= SECTOR_HEADER_SIZE;
   333 	} else {
   334 	    start -= SECTOR_HEADER_SIZE;
   335 	}
   336 	gdrom_read_block( buf + read_len, file_offset+start, size, f );
   337 	read_len += size;
   338 	break;
   339     default: // Data track w/ data only in file
   340 	if( READ_CD_RAW(channels) ) {
   341 	    gdrom_read_block( buf + gdrom_data_offset[track->mode], file_offset, 
   342 			      track->sector_size, f );
   343 	    do_encode_L2( buf, sector_mode, lba );
   344 	    read_len = 2352;
   345 	} else {
   346 	    if( READ_CD_HEADER(channels) ) {
   347 		gdrom_build_sector_header( buf, lba, sector_mode );
   348 		read_len += SECTOR_HEADER_SIZE;
   349 	    }
   350 	    if( READ_CD_SUBHEAD(channels) && 
   351 		(sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   352 		if( sector_mode == GDROM_MODE2_FORM1 ) {
   353 		    *((uint32_t *)(buf+read_len)) = 0;
   354 		    *((uint32_t *)(buf+read_len+4)) = 0;
   355 		} else {
   356 		    *((uint32_t *)(buf+read_len)) = 0x00200000;
   357 		    *((uint32_t *)(buf+read_len+4)) = 0x00200000;
   358 		}
   359 		read_len += 8;
   360 	    }
   361 	    if( READ_CD_DATA(channels) ) {
   362 		gdrom_read_block( buf+read_len, file_offset, track->sector_size, f );
   363 		read_len += track->sector_size;
   364 	    }
   365 	}
   366     }
   367     *length = read_len;
   368     return PKT_ERR_OK;
   369 }
   371 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   372 {
   373     gdrom_image_t image = (gdrom_image_t)disc;
   374     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   375     int i;
   377     for( i=0; i<image->track_count; i++ ) {
   378 	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   379     }
   380     toc->first = 0x0100 | image->track[0].flags;
   381     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   382     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   383 	image->track[i-1].flags;
   384     for( ;i<99; i++ )
   385 	toc->track[i] = 0xFFFFFFFF;
   386     return PKT_ERR_OK;
   387 }
   389 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   390 {
   391     gdrom_image_t image = (gdrom_image_t)disc;
   392     struct gdrom_track *last_track = &image->track[image->track_count-1];
   393     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   394     int i;
   395     buf[0] = 0x01; /* Disc status? */
   396     buf[1] = 0;
   398     if( session == 0 ) {
   399 	buf[2] = last_track->session+1; /* last session */
   400 	buf[3] = (end_of_disc >> 16) & 0xFF;
   401 	buf[4] = (end_of_disc >> 8) & 0xFF;
   402 	buf[5] = end_of_disc & 0xFF;
   403 	return PKT_ERR_OK;
   404     } else {
   405 	session--;
   406 	for( i=0; i<image->track_count; i++ ) {
   407 	    if( image->track[i].session == session ) {
   408 		buf[2] = i+1; /* first track of session */
   409 		buf[3] = (image->track[i].lba >> 16) & 0xFF;
   410 		buf[4] = (image->track[i].lba >> 8) & 0xFF;
   411 		buf[5] = image->track[i].lba & 0xFF;
   412 		return PKT_ERR_OK;
   413 	    }
   414 	}
   415 	return PKT_ERR_BADFIELD; /* No such session */
   416     }
   417 }
   419 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   420 {
   421     gdrom_image_t image = (gdrom_image_t)disc;
   422     int track_no = gdrom_image_get_track_by_lba( image, lba );
   423     if( track_no == -1 ) {
   424 	track_no = 1;
   425 	lba = 150;
   426     }
   427     struct gdrom_track *track = &image->track[track_no-1];
   428     uint32_t offset = lba - track->lba;
   429     buf[4] = track->flags;
   430     buf[5] = track_no;
   431     buf[6] = 0x01; /* ?? */
   432     buf[7] = (offset >> 16) & 0xFF;
   433     buf[8] = (offset >> 8) & 0xFF;
   434     buf[9] = offset & 0xFF;
   435     buf[10] = 0;
   436     buf[11] = (lba >> 16) & 0xFF;
   437     buf[12] = (lba >> 8) & 0xFF;
   438     buf[13] = lba & 0xFF;
   439     return PKT_ERR_OK;
   440 }
   442 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   443 {
   444     gdrom_image_t image = (gdrom_image_t)disc;
   445     return image->disc_type | IDE_DISC_READY;
   446 }
   448 void gdrom_image_dump_info( gdrom_disc_t d ) {
   449     gdrom_image_t disc = (gdrom_image_t)d;
   450     int i;
   451     int last_session = disc->track[disc->track_count-1].session;
   452     gboolean is_bootable = FALSE;
   454     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   455 	  disc->track[disc->track_count-1].session + 1 );
   456     if( last_session > 0 ) {
   457 	/* Boot track is the first data track of the last session, provided that it 
   458 	 * cannot be a single-session disc.
   459 	 */
   460 	int boot_track = -1;
   461 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   462 	    if( disc->track[i].flags & TRACK_DATA ) {
   463 		boot_track = i;
   464 	    }
   465 	}
   466 	if( boot_track != -1 ) {
   467 	    unsigned char boot_sector[MAX_SECTOR_SIZE];
   468 	    uint32_t length = sizeof(boot_sector);
   469 	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   470 				   boot_sector, &length ) == PKT_ERR_OK ) {
   471 		bootstrap_dump(boot_sector, FALSE);
   472 		is_bootable = TRUE;
   473 	    }
   474 	}
   475     }
   476     if( !is_bootable ) {
   477 	WARN( "Disc does not appear to be bootable" );
   478     }
   479 }
.