Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 561:533f6b478071
prev498:10d5ba99a778
next640:01b50fd48035
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
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 <netinet/in.h>
    21 #include "gdrom/gdrom.h"
    22 #include "gdrom/packet.h"
    23 #include "bootstrap.h"
    25 static void gdrom_image_destroy( gdrom_disc_t disc );
    26 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode, 
    27 					      unsigned char *buf, uint32_t *readlength );
    28 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
    29 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
    30 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
    31 static int gdrom_image_drive_status( gdrom_disc_t disc );
    33 struct cdrom_sector_header {
    34     uint8_t sync[12];
    35     uint8_t msf[3];
    36     uint8_t mode;
    37 };
    39 /**
    40  * Initialize a gdrom_disc structure with the gdrom_image_* methods
    41  */
    42 void gdrom_image_init( gdrom_disc_t disc )
    43 {
    44     memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
    45     disc->read_sector = gdrom_image_read_sector;
    46     disc->read_toc = gdrom_image_read_toc;
    47     disc->read_session = gdrom_image_read_session;
    48     disc->read_position = gdrom_image_read_position;
    49     disc->drive_status = gdrom_image_drive_status;
    50     disc->play_audio = NULL; /* not supported yet */
    51     disc->run_time_slice = NULL; /* not needed */
    52     disc->close = gdrom_image_destroy;
    53 }
    55 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
    56 {
    57     gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
    58     if( image == NULL ) {
    59 	return NULL;
    60     }
    61     image->disc_type = IDE_DISC_CDROM;
    62     image->file = f;
    63     gdrom_disc_t disc = (gdrom_disc_t)image;
    64     gdrom_image_init(disc);
    65     if( filename == NULL ) {
    66 	disc->name = NULL;
    67     } else {
    68 	disc->name = g_strdup(filename);
    69     }
    71     return disc;
    72 }
    74 static void gdrom_image_destroy( gdrom_disc_t disc )
    75 {
    76     int i;
    77     FILE *lastfile = NULL;
    78     gdrom_image_t img = (gdrom_image_t)disc;
    79     if( img->file != NULL ) {
    80 	fclose(img->file);
    81 	img->file = NULL;
    82     }
    83     for( i=0; i<img->track_count; i++ ) {
    84 	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
    85 	    lastfile = img->track[i].file;
    86 	    fclose(lastfile);
    87 	    img->track[i].file = NULL;
    88 	}
    89     }
    90     if( disc->name != NULL ) {
    91 	g_free( (gpointer)disc->name );
    92 	disc->name = NULL;
    93     }
    94     free( disc );
    95 }
    97 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
    98 {
    99     int i;
   100     FILE *lastfile = NULL;
   101     gdrom_image_t img = (gdrom_image_t)disc;
   102     if( img->file != NULL ) {
   103 	img->file = NULL;
   104     }
   105     for( i=0; i<img->track_count; i++ ) {
   106 	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
   107 	    lastfile = img->track[i].file;
   108 	    fclose(lastfile);
   109 	    img->track[i].file = NULL;
   110 	}
   111     }
   112     if( disc->name != NULL ) {
   113 	g_free( (gpointer)disc->name );
   114 	disc->name = NULL;
   115     }
   116     free( disc );
   117 }
   119 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
   120 {
   121     int i;
   122     for( i=0; i<image->track_count; i++ ) {
   123 	if( image->track[i].lba <= lba && 
   124 	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
   125 	    return i+1;
   126 	}
   127     }
   128     return -1;
   129 }
   131 /**
   132  * Read a block from an image file, handling negative file offsets
   133  * with 0-fill.
   134  */
   135 static void gdrom_read_block( unsigned char *buf, int file_offset, int length, FILE *f )
   136 {
   137     if( file_offset < 0 ) {
   138 	int size = -file_offset;
   139 	if( size >= length ) {
   140 	    memset( buf, 0, length );
   141 	    return;
   142 	} else {
   143 	    memset( buf, 0, size );
   144 	    file_offset = 0;
   145 	    length -= size;
   146 	}
   147     }
   148     fseek( f, file_offset, SEEK_SET );
   149     fread( buf, length, 1, f );
   150 }
   152 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
   153 					      int mode, unsigned char *buf, uint32_t *length )
   154 {
   155     gdrom_image_t image = (gdrom_image_t)disc;
   156     struct cdrom_sector_header secthead;
   157     int file_offset, read_len, track_no;
   159     FILE *f;
   161     track_no = gdrom_image_get_track_by_lba( image, lba );
   162     if( track_no == -1 ) {
   163 	return PKT_ERR_BADREAD;
   164     }
   165     struct gdrom_track *track = &image->track[track_no-1];
   166     file_offset = track->offset + track->sector_size * (lba - track->lba);
   167     read_len = track->sector_size;
   168     if( track->file != NULL ) {
   169 	f = track->file;
   170     } else {
   171 	f = image->file;
   172     }
   176     switch( mode ) {
   177     case 0x24:
   178     case 0x28:
   179 	read_len = 2048;
   180 	switch( track->mode ) {
   181 	case GDROM_MODE1:
   182 	case GDROM_MODE2_XA1:
   183 	    gdrom_read_block( buf, file_offset, track->sector_size, f );
   184 	    break;
   185 	case GDROM_MODE2:
   186 	    file_offset += 8; /* skip the subheader */
   187 	    gdrom_read_block( buf, file_offset, 2048, f );
   188 	    break;
   189 	case GDROM_RAW:
   190 	    gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   191 	    switch( secthead.mode ) {
   192 	    case 1:
   193 		file_offset += 16;
   194 		break;
   195 	    case 2:
   196 		file_offset += 24;
   197 		break;
   198 	    default:
   199 		return PKT_ERR_BADREADMODE;
   200 	    }
   201 	    gdrom_read_block( buf, file_offset, 2048, f );
   202 	    break;
   203 	default:
   204 	    return PKT_ERR_BADREADMODE;
   205 	}
   206 	break;
   207     default:
   208 	return PKT_ERR_BADREADMODE;
   209     }
   211     *length = read_len;
   212     return PKT_ERR_OK;
   214 }
   216 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   217 {
   218     gdrom_image_t image = (gdrom_image_t)disc;
   219     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   220     int i;
   222     for( i=0; i<image->track_count; i++ ) {
   223 	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   224     }
   225     toc->first = 0x0100 | image->track[0].flags;
   226     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   227     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   228 	image->track[i-1].flags;
   229     for( ;i<99; i++ )
   230 	toc->track[i] = 0xFFFFFFFF;
   231     return PKT_ERR_OK;
   232 }
   234 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   235 {
   236     gdrom_image_t image = (gdrom_image_t)disc;
   237     struct gdrom_track *last_track = &image->track[image->track_count-1];
   238     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   239     int i;
   240     buf[0] = 0x01; /* Disc status? */
   241     buf[1] = 0;
   243     if( session == 0 ) {
   244 	buf[2] = last_track->session+1; /* last session */
   245 	buf[3] = (end_of_disc >> 16) & 0xFF;
   246 	buf[4] = (end_of_disc >> 8) & 0xFF;
   247 	buf[5] = end_of_disc & 0xFF;
   248 	return PKT_ERR_OK;
   249     } else {
   250 	session--;
   251 	for( i=0; i<image->track_count; i++ ) {
   252 	    if( image->track[i].session == session ) {
   253 		buf[2] = i+1; /* first track of session */
   254 		buf[3] = (image->track[i].lba >> 16) & 0xFF;
   255 		buf[4] = (image->track[i].lba >> 8) & 0xFF;
   256 		buf[5] = image->track[i].lba & 0xFF;
   257 		return PKT_ERR_OK;
   258 	    }
   259 	}
   260 	return PKT_ERR_BADFIELD; /* No such session */
   261     }
   262 }
   264 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   265 {
   266     gdrom_image_t image = (gdrom_image_t)disc;
   267     int track_no = gdrom_image_get_track_by_lba( image, lba );
   268     if( track_no == -1 ) {
   269 	track_no = 1;
   270 	lba = 150;
   271     }
   272     struct gdrom_track *track = &image->track[track_no-1];
   273     uint32_t offset = lba - track->lba;
   274     buf[4] = track->flags;
   275     buf[5] = track_no;
   276     buf[6] = 0x01; /* ?? */
   277     buf[7] = (offset >> 16) & 0xFF;
   278     buf[8] = (offset >> 8) & 0xFF;
   279     buf[9] = offset & 0xFF;
   280     buf[10] = 0;
   281     buf[11] = (lba >> 16) & 0xFF;
   282     buf[12] = (lba >> 8) & 0xFF;
   283     buf[13] = lba & 0xFF;
   284     return PKT_ERR_OK;
   285 }
   287 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   288 {
   289     gdrom_image_t image = (gdrom_image_t)disc;
   290     return image->disc_type | IDE_DISC_READY;
   291 }
   293 void gdrom_image_dump_info( gdrom_disc_t d ) {
   294     gdrom_image_t disc = (gdrom_image_t)d;
   295     int i;
   296     int last_session = disc->track[disc->track_count-1].session;
   297     gboolean is_bootable = FALSE;
   299     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   300 	  disc->track[disc->track_count-1].session + 1 );
   301     if( last_session > 0 ) {
   302 	/* Boot track is the first data track of the last session, provided that it 
   303 	 * cannot be a single-session disc.
   304 	 */
   305 	int boot_track = -1;
   306 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   307 	    if( disc->track[i].flags & TRACK_DATA ) {
   308 		boot_track = i;
   309 	    }
   310 	}
   311 	if( boot_track != -1 ) {
   312 	    unsigned char boot_sector[MAX_SECTOR_SIZE];
   313 	    uint32_t length = sizeof(boot_sector);
   314 	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   315 				   boot_sector, &length ) == PKT_ERR_OK ) {
   316 		bootstrap_dump(boot_sector, FALSE);
   317 		is_bootable = TRUE;
   318 	    }
   319 	}
   320     }
   321     if( !is_bootable ) {
   322 	WARN( "Disc does not appear to be bootable" );
   323     }
   324 }
.