Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 492:84e33e4dda1c
prev480:d28c2992f5ee
next498:10d5ba99a778
author nkeynes
date Tue Nov 06 08:35:16 2007 +0000 (15 years ago)
permissions -rw-r--r--
last change Issue #37: Add nulldc GDI format
view annotate diff log raw
     1 /**
     2  * $Id: gdimage.c,v 1.6 2007-11-06 08:35:16 nkeynes Exp $
     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 );
    34 /**
    35  * Initialize a gdrom_disc structure with the gdrom_image_* methods
    36  */
    37 void gdrom_image_init( gdrom_disc_t disc )
    38 {
    39     memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
    40     disc->read_sector = gdrom_image_read_sector;
    41     disc->read_toc = gdrom_image_read_toc;
    42     disc->read_session = gdrom_image_read_session;
    43     disc->read_position = gdrom_image_read_position;
    44     disc->drive_status = gdrom_image_drive_status;
    45     disc->play_audio = NULL; /* not supported yet */
    46     disc->run_time_slice = NULL; /* not needed */
    47     disc->close = gdrom_image_destroy;
    48 }
    50 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
    51 {
    52     gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
    53     if( image == NULL ) {
    54 	return NULL;
    55     }
    56     image->disc_type = IDE_DISC_CDROM;
    57     image->file = f;
    58     gdrom_disc_t disc = (gdrom_disc_t)image;
    59     gdrom_image_init(disc);
    60     if( filename == NULL ) {
    61 	disc->name = NULL;
    62     } else {
    63 	disc->name = g_strdup(filename);
    64     }
    66     return disc;
    67 }
    69 static void gdrom_image_destroy( gdrom_disc_t disc )
    70 {
    71     int i;
    72     FILE *lastfile = NULL;
    73     gdrom_image_t img = (gdrom_image_t)disc;
    74     if( img->file != NULL ) {
    75 	fclose(img->file);
    76 	img->file = NULL;
    77     }
    78     for( i=0; i<img->track_count; i++ ) {
    79 	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
    80 	    lastfile = img->track[i].file;
    81 	    fclose(lastfile);
    82 	    img->track[i].file = NULL;
    83 	}
    84     }
    85     if( disc->name != NULL ) {
    86 	g_free( (gpointer)disc->name );
    87 	disc->name = NULL;
    88     }
    89     free( disc );
    90 }
    92 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
    93 {
    94     int i;
    95     FILE *lastfile = NULL;
    96     gdrom_image_t img = (gdrom_image_t)disc;
    97     if( img->file != NULL ) {
    98 	img->file = NULL;
    99     }
   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;
   103 	    fclose(lastfile);
   104 	    img->track[i].file = NULL;
   105 	}
   106     }
   107     if( disc->name != NULL ) {
   108 	g_free( (gpointer)disc->name );
   109 	disc->name = NULL;
   110     }
   111     free( disc );
   112 }
   114 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
   115 {
   116     int i;
   117     for( i=0; i<image->track_count; i++ ) {
   118 	if( image->track[i].lba <= lba && 
   119 	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
   120 	    return i+1;
   121 	}
   122     }
   123     return -1;
   124 }
   126 /**
   127  * Read a block from an image file, handling negative file offsets
   128  * with 0-fill.
   129  */
   130 static void gdrom_read_block( char *buf, int file_offset, int length, FILE *f )
   131 {
   132     if( file_offset < 0 ) {
   133 	int size = -file_offset;
   134 	if( size >= length ) {
   135 	    memset( buf, 0, length );
   136 	    return;
   137 	} else {
   138 	    memset( buf, 0, size );
   139 	    file_offset = 0;
   140 	    length -= size;
   141 	}
   142     }
   143     fseek( f, file_offset, SEEK_SET );
   144     fread( buf, length, 1, f );
   145 }
   147 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
   148 					      int mode, unsigned char *buf, uint32_t *length )
   149 {
   150     gdrom_image_t image = (gdrom_image_t)disc;
   151     int file_offset, read_len, track_no;
   152     FILE *f;
   154     track_no = gdrom_image_get_track_by_lba( image, lba );
   155     if( track_no == -1 ) {
   156 	return PKT_ERR_BADREAD;
   157     }
   158     struct gdrom_track *track = &image->track[track_no-1];
   159     file_offset = track->offset + track->sector_size * (lba - track->lba);
   160     read_len = track->sector_size;
   161     if( track->file != NULL ) {
   162 	f = track->file;
   163     } else {
   164 	f = image->file;
   165     }
   169     switch( mode ) {
   170     case 0x24:
   171     case 0x28:
   172 	switch( track->mode ) {
   173 	case GDROM_MODE1:
   174 	case GDROM_MODE2_XA1:
   175 	    gdrom_read_block( buf, file_offset, track->sector_size, f );
   176 	    break;
   177 	case GDROM_MODE2:
   178 	    read_len = 2048;
   179 	    file_offset += 8; /* skip the subheader */
   180 	    gdrom_read_block( buf, file_offset, 2048, f );
   181 	    break;
   182 	default:
   183 	    return PKT_ERR_BADREADMODE;
   184 	}
   185 	break;
   186     default:
   187 	return PKT_ERR_BADREADMODE;
   188     }
   190     *length = read_len;
   191     return PKT_ERR_OK;
   193 }
   195 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   196 {
   197     gdrom_image_t image = (gdrom_image_t)disc;
   198     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   199     int i;
   201     for( i=0; i<image->track_count; i++ ) {
   202 	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   203     }
   204     toc->first = 0x0100 | image->track[0].flags;
   205     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   206     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   207 	image->track[i-1].flags;
   208     for( ;i<99; i++ )
   209 	toc->track[i] = 0xFFFFFFFF;
   210     return PKT_ERR_OK;
   211 }
   213 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   214 {
   215     gdrom_image_t image = (gdrom_image_t)disc;
   216     struct gdrom_track *last_track = &image->track[image->track_count-1];
   217     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   218     int i;
   219     buf[0] = 0x01; /* Disc status? */
   220     buf[1] = 0;
   222     if( session == 0 ) {
   223 	buf[2] = last_track->session+1; /* last session */
   224 	buf[3] = (end_of_disc >> 16) & 0xFF;
   225 	buf[4] = (end_of_disc >> 8) & 0xFF;
   226 	buf[5] = end_of_disc & 0xFF;
   227 	return PKT_ERR_OK;
   228     } else {
   229 	session--;
   230 	for( i=0; i<image->track_count; i++ ) {
   231 	    if( image->track[i].session == session ) {
   232 		buf[2] = i+1; /* first track of session */
   233 		buf[3] = (image->track[i].lba >> 16) & 0xFF;
   234 		buf[4] = (image->track[i].lba >> 8) & 0xFF;
   235 		buf[5] = image->track[i].lba & 0xFF;
   236 		return PKT_ERR_OK;
   237 	    }
   238 	}
   239 	return PKT_ERR_BADFIELD; /* No such session */
   240     }
   241 }
   243 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   244 {
   245     gdrom_image_t image = (gdrom_image_t)disc;
   246     int track_no = gdrom_image_get_track_by_lba( image, lba );
   247     if( track_no == -1 ) {
   248 	track_no = 1;
   249 	lba = 150;
   250     }
   251     struct gdrom_track *track = &image->track[track_no-1];
   252     uint32_t offset = lba - track->lba;
   253     buf[4] = track->flags;
   254     buf[5] = track_no;
   255     buf[6] = 0x01; /* ?? */
   256     buf[7] = (offset >> 16) & 0xFF;
   257     buf[8] = (offset >> 8) & 0xFF;
   258     buf[9] = offset & 0xFF;
   259     buf[10] = 0;
   260     buf[11] = (lba >> 16) & 0xFF;
   261     buf[12] = (lba >> 8) & 0xFF;
   262     buf[13] = lba & 0xFF;
   263     return PKT_ERR_OK;
   264 }
   266 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   267 {
   268     gdrom_image_t image = (gdrom_image_t)disc;
   269     return image->disc_type | IDE_DISC_READY;
   270 }
   272 void gdrom_image_dump_info( gdrom_disc_t d ) {
   273     gdrom_image_t disc = (gdrom_image_t)d;
   274     int i;
   275     int last_session = disc->track[disc->track_count-1].session;
   276     gboolean is_bootable = FALSE;
   278     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   279 	  disc->track[disc->track_count-1].session + 1 );
   280     if( last_session > 0 ) {
   281 	/* Boot track is the first data track of the last session, provided that it 
   282 	 * cannot be a single-session disc.
   283 	 */
   284 	int boot_track = -1;
   285 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   286 	    if( disc->track[i].flags & TRACK_DATA ) {
   287 		boot_track = i;
   288 	    }
   289 	}
   290 	if( boot_track != -1 ) {
   291 	    unsigned char boot_sector[MAX_SECTOR_SIZE];
   292 	    uint32_t length = sizeof(boot_sector);
   293 	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   294 				   boot_sector, &length ) == PKT_ERR_OK ) {
   295 		bootstrap_dump(boot_sector, FALSE);
   296 		is_bootable = TRUE;
   297 	    }
   298 	}
   299     }
   300     if( !is_bootable ) {
   301 	WARN( "Disc does not appear to be bootable" );
   302     }
   303 }
.