Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 709:18c39a8e504c
prev678:35eb00945316
next720:b5594d1ac80a
author nkeynes
date Wed Jun 25 10:40:45 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Commit OSX CD-ROM driver work-in-progress
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/gddriver.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 TRUE;
   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     if( !gdrom_is_compatible_read_mode(sector_mode, READ_CD_MODE(mode)) ) {
   292 	return PKT_ERR_BADREADMODE;
   293     }
   295     /* Ok, we've got a valid sector, check what parts of the sector we need to
   296      * return - header | subhead | data | everything
   297      */
   298     int channels = READ_CD_CHANNELS(mode);
   300     if( channels == 0 ) {
   301 	// legal, if weird
   302 	*length = 0;
   303 	return PKT_ERR_OK;
   304     } else if( channels == 0xA0 && 
   305 	(sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2 )) {
   306 	// caller requested a non-contiguous region
   307 	return PKT_ERR_BADFIELD;
   308     } else if( READ_CD_RAW(channels) ) {
   309 	channels = 0xF0; // implies everything
   310     }
   312     read_len = 0;
   313     int start, size;
   314     switch( track->mode ) {
   315     case GDROM_CDDA:
   316 	// audio is nice and simple (assume perfect reads for now)
   317 	*length = 2352;
   318 	gdrom_read_block( buf, file_offset, track->sector_size, f );
   319 	return PKT_ERR_OK;
   320     case GDROM_RAW_XA:
   321     case GDROM_RAW_NONXA:
   322 	gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   323 	gdrom_read_block( buf, file_offset+start, size, f );
   324 	read_len = size;
   325 	break;
   326     case GDROM_SEMIRAW_MODE2:
   327 	gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   328 	if( READ_CD_HEADER(channels) ) {
   329 	    gdrom_build_sector_header( buf, lba, sector_mode );
   330 	    read_len += SECTOR_HEADER_SIZE;
   331 	    size -= SECTOR_HEADER_SIZE;
   332 	} else {
   333 	    start -= SECTOR_HEADER_SIZE;
   334 	}
   335 	gdrom_read_block( buf + read_len, file_offset+start, size, f );
   336 	read_len += size;
   337 	break;
   338     default: // Data track w/ data only in file
   339 	if( READ_CD_RAW(channels) ) {
   340 	    gdrom_read_block( buf + gdrom_data_offset[track->mode], file_offset, 
   341 			      track->sector_size, f );
   342 	    do_encode_L2( buf, sector_mode, lba );
   343 	    read_len = 2352;
   344 	} else {
   345 	    if( READ_CD_HEADER(channels) ) {
   346 		gdrom_build_sector_header( buf, lba, sector_mode );
   347 		read_len += SECTOR_HEADER_SIZE;
   348 	    }
   349 	    if( READ_CD_SUBHEAD(channels) && 
   350 		(sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   351 		if( sector_mode == GDROM_MODE2_FORM1 ) {
   352 		    *((uint32_t *)(buf+read_len)) = 0;
   353 		    *((uint32_t *)(buf+read_len+4)) = 0;
   354 		} else {
   355 		    *((uint32_t *)(buf+read_len)) = 0x00200000;
   356 		    *((uint32_t *)(buf+read_len+4)) = 0x00200000;
   357 		}
   358 		read_len += 8;
   359 	    }
   360 	    if( READ_CD_DATA(channels) ) {
   361 		gdrom_read_block( buf+read_len, file_offset, track->sector_size, f );
   362 		read_len += track->sector_size;
   363 	    }
   364 	}
   365     }
   366     *length = read_len;
   367     return PKT_ERR_OK;
   368 }
   370 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   371 {
   372     gdrom_image_t image = (gdrom_image_t)disc;
   373     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   374     int i;
   376     for( i=0; i<image->track_count; i++ ) {
   377 	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   378     }
   379     toc->first = 0x0100 | image->track[0].flags;
   380     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   381     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   382 	image->track[i-1].flags;
   383     for( ;i<99; i++ )
   384 	toc->track[i] = 0xFFFFFFFF;
   385     return PKT_ERR_OK;
   386 }
   388 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   389 {
   390     gdrom_image_t image = (gdrom_image_t)disc;
   391     struct gdrom_track *last_track = &image->track[image->track_count-1];
   392     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   393     int i;
   394     buf[0] = 0x01; /* Disc status? */
   395     buf[1] = 0;
   397     if( session == 0 ) {
   398 	buf[2] = last_track->session+1; /* last session */
   399 	buf[3] = (end_of_disc >> 16) & 0xFF;
   400 	buf[4] = (end_of_disc >> 8) & 0xFF;
   401 	buf[5] = end_of_disc & 0xFF;
   402 	return PKT_ERR_OK;
   403     } else {
   404 	session--;
   405 	for( i=0; i<image->track_count; i++ ) {
   406 	    if( image->track[i].session == session ) {
   407 		buf[2] = i+1; /* first track of session */
   408 		buf[3] = (image->track[i].lba >> 16) & 0xFF;
   409 		buf[4] = (image->track[i].lba >> 8) & 0xFF;
   410 		buf[5] = image->track[i].lba & 0xFF;
   411 		return PKT_ERR_OK;
   412 	    }
   413 	}
   414 	return PKT_ERR_BADFIELD; /* No such session */
   415     }
   416 }
   418 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   419 {
   420     gdrom_image_t image = (gdrom_image_t)disc;
   421     int track_no = gdrom_image_get_track_by_lba( image, lba );
   422     if( track_no == -1 ) {
   423 	track_no = 1;
   424 	lba = 150;
   425     }
   426     struct gdrom_track *track = &image->track[track_no-1];
   427     uint32_t offset = lba - track->lba;
   428     buf[4] = track->flags;
   429     buf[5] = track_no;
   430     buf[6] = 0x01; /* ?? */
   431     buf[7] = (offset >> 16) & 0xFF;
   432     buf[8] = (offset >> 8) & 0xFF;
   433     buf[9] = offset & 0xFF;
   434     buf[10] = 0;
   435     buf[11] = (lba >> 16) & 0xFF;
   436     buf[12] = (lba >> 8) & 0xFF;
   437     buf[13] = lba & 0xFF;
   438     return PKT_ERR_OK;
   439 }
   441 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   442 {
   443     gdrom_image_t image = (gdrom_image_t)disc;
   444     return image->disc_type | IDE_DISC_READY;
   445 }
   447 void gdrom_image_dump_info( gdrom_disc_t d ) {
   448     gdrom_image_t disc = (gdrom_image_t)d;
   449     int i;
   450     int last_session = disc->track[disc->track_count-1].session;
   451     gboolean is_bootable = FALSE;
   453     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   454 	  disc->track[disc->track_count-1].session + 1 );
   455     if( last_session > 0 ) {
   456 	/* Boot track is the first data track of the last session, provided that it 
   457 	 * cannot be a single-session disc.
   458 	 */
   459 	int boot_track = -1;
   460 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   461 	    if( disc->track[i].flags & TRACK_DATA ) {
   462 		boot_track = i;
   463 	    }
   464 	}
   465 	if( boot_track != -1 ) {
   466 	    unsigned char boot_sector[MAX_SECTOR_SIZE];
   467 	    uint32_t length = sizeof(boot_sector);
   468 	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   469 				   boot_sector, &length ) == PKT_ERR_OK ) {
   470 		bootstrap_dump(boot_sector, FALSE);
   471 		is_bootable = TRUE;
   472 	    }
   473 	}
   474     }
   475     if( !is_bootable ) {
   476 	WARN( "Disc does not appear to be bootable" );
   477     }
   478 }
   480 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name )
   481 {
   482     struct gdrom_device *dev = g_malloc0( sizeof(struct gdrom_device) );
   483     dev->name = g_strdup(name);
   484     dev->device_name = g_strdup(dev_name);
   485     return dev;
   486 }
   488 void gdrom_device_destroy( gdrom_device_t dev )
   489 {
   490     if( dev->name != NULL ) {
   491         g_free( dev->name );
   492         dev->name = NULL;
   493     }
   494     if( dev->device_name != NULL ) {
   495         g_free( dev->device_name );
   496         dev->device_name = NULL;
   497     }
   498     g_free( dev );
   499 }
.