Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 342:850502f0e8de
next422:61a0598e07ff
author nkeynes
date Wed Jan 31 10:58:42 2007 +0000 (15 years ago)
permissions -rw-r--r--
last change Refactor gdrom module to be more conducive to real device support
view annotate diff log raw
     1 /**
     2  * $Id: gdimage.c,v 1.1 2007-01-31 10:58:42 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 "gdrom/gdrom.h"
    20 #include "gdrom/packet.h"
    22 static void gdrom_image_destroy( gdrom_disc_t disc );
    23 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode, 
    24 					      char *buf, uint32_t *readlength );
    25 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, char *buf );
    26 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, char *buf );
    27 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, char *buf );
    28 static int gdrom_image_drive_status( gdrom_disc_t disc );
    31 /**
    32  * Initialize a gdrom_disc structure with the gdrom_image_* methods
    33  */
    34 void gdrom_image_init( gdrom_disc_t disc )
    35 {
    36     memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
    37     disc->read_sector = gdrom_image_read_sector;
    38     disc->read_toc = gdrom_image_read_toc;
    39     disc->read_session = gdrom_image_read_session;
    40     disc->read_position = gdrom_image_read_position;
    41     disc->drive_status = gdrom_image_drive_status;
    42     disc->play_audio = NULL; /* not supported yet */
    43     disc->run_time_slice = NULL; /* not needed */
    44     disc->close = gdrom_image_destroy;
    45 }
    47 gdrom_disc_t gdrom_image_new( FILE *f )
    48 {
    49     gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
    50     if( image == NULL ) {
    51 	return NULL;
    52     }
    53     image->disc_type = IDE_DISC_CDROM;
    54     image->file = f;
    56     gdrom_disc_t disc = (gdrom_disc_t)image;
    57     gdrom_image_init(disc);
    58     return disc;
    59 }
    61 static void gdrom_image_destroy( gdrom_disc_t disc )
    62 {
    63     gdrom_image_t img = (gdrom_image_t)disc;
    64     if( img->file != NULL ) {
    65 	fclose(img->file);
    66 	img->file = NULL;
    67     }
    68     free( disc );
    69 }
    71 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
    72 {
    73     int i;
    74     for( i=0; i<image->track_count; i++ ) {
    75 	if( image->track[i].lba <= lba && 
    76 	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
    77 	    return i+1;
    78 	}
    79     }
    80     return -1;
    81 }
    83 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
    84 					      int mode, char *buf, uint32_t *length )
    85 {
    86     gdrom_image_t image = (gdrom_image_t)disc;
    87     int i, file_offset, read_len, track_no;
    89     track_no = gdrom_image_get_track_by_lba( image, lba );
    90     if( track_no == -1 ) {
    91 	return PKT_ERR_BADREAD;
    92     }
    93     struct gdrom_track *track = &image->track[track_no-1];
    94     file_offset = track->offset + track->sector_size * (lba - track->lba);
    95     read_len = track->sector_size;
    97     switch( mode ) {
    98     case 0x24:
    99     case 0x28:
   100 	switch( track->mode ) {
   101 	case GDROM_MODE1:
   102 	case GDROM_MODE2_XA1:
   103 	    fseek( image->file, file_offset, SEEK_SET );
   104 	    fread( buf, track->sector_size, 1, image->file );
   105 	    break;
   106 	case GDROM_MODE2:
   107 	    read_len = 2048;
   108 	    file_offset += 8; /* skip the subheader */
   109 	    fseek( image->file, file_offset, SEEK_SET );
   110 	    fread( buf, 2048, 1, image->file );
   111 	    break;
   112 	default:
   113 	    return PKT_ERR_BADREADMODE;
   114 	}
   115 	break;
   116     default:
   117 	return PKT_ERR_BADREADMODE;
   118     }
   120     *length = read_len;
   121     return PKT_ERR_OK;
   123 }
   125 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, char *buf ) 
   126 {
   127     gdrom_image_t image = (gdrom_image_t)disc;
   128     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   129     int i;
   131     for( i=0; i<image->track_count; i++ ) {
   132 	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   133     }
   134     toc->first = 0x0100 | image->track[0].flags;
   135     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   136     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   137 	image->track[i-1].flags;
   138     for( ;i<99; i++ )
   139 	toc->track[i] = 0xFFFFFFFF;
   140     return PKT_ERR_OK;
   141 }
   143 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, char *buf )
   144 {
   145     gdrom_image_t image = (gdrom_image_t)disc;
   146     struct gdrom_track *last_track = &image->track[image->track_count-1];
   147     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   148     int i;
   149     buf[0] = 0x01; /* Disc status? */
   150     buf[1] = 0;
   152     if( session == 0 ) {
   153 	buf[2] = last_track->session+1; /* last session */
   154 	buf[3] = (end_of_disc >> 16) & 0xFF;
   155 	buf[4] = (end_of_disc >> 8) & 0xFF;
   156 	buf[5] = end_of_disc & 0xFF;
   157 	return PKT_ERR_OK;
   158     } else {
   159 	session--;
   160 	for( i=0; i<image->track_count; i++ ) {
   161 	    if( image->track[i].session == session ) {
   162 		buf[2] = i+1; /* first track of session */
   163 		buf[3] = (image->track[i].lba >> 16) & 0xFF;
   164 		buf[4] = (image->track[i].lba >> 8) & 0xFF;
   165 		buf[5] = image->track[i].lba & 0xFF;
   166 		return PKT_ERR_OK;
   167 	    }
   168 	}
   169 	return PKT_ERR_BADFIELD; /* No such session */
   170     }
   171 }
   173 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, char *buf )
   174 {
   175     gdrom_image_t image = (gdrom_image_t)disc;
   176     int track_no = gdrom_image_get_track_by_lba( image, lba );
   177     if( track_no == -1 ) {
   178 	track_no = 1;
   179 	lba = 150;
   180     }
   181     struct gdrom_track *track = &image->track[track_no-1];
   182     uint32_t offset = lba - track->lba;
   183     buf[4] = track->flags;
   184     buf[5] = track_no;
   185     buf[6] = 0x01; /* ?? */
   186     buf[7] = (offset >> 16) & 0xFF;
   187     buf[8] = (offset >> 8) & 0xFF;
   188     buf[9] = offset & 0xFF;
   189     buf[10] = 0;
   190     buf[11] = (lba >> 16) & 0xFF;
   191     buf[12] = (lba >> 8) & 0xFF;
   192     buf[13] = lba & 0xFF;
   193 }
   195 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   196 {
   197     gdrom_image_t image = (gdrom_image_t)disc;
   198     return image->disc_type | IDE_DISC_READY;
   199 }
   201 void gdrom_image_dump_info( gdrom_disc_t d ) {
   202     gdrom_image_t disc = (gdrom_image_t)d;
   203     int i;
   204     int last_session = disc->track[disc->track_count-1].session;
   205     gboolean is_bootable = FALSE;
   207     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   208 	  disc->track[disc->track_count-1].session + 1 );
   209     if( last_session > 0 ) {
   210 	/* Boot track is the first data track of the last session, provided that it 
   211 	 * cannot be a single-session disc.
   212 	 */
   213 	int boot_track = -1;
   214 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   215 	    if( disc->track[i].flags & TRACK_DATA ) {
   216 		boot_track = i;
   217 	    }
   218 	}
   219 	if( boot_track != -1 ) {
   220 	    char boot_sector[MAX_SECTOR_SIZE];
   221 	    uint32_t length = sizeof(boot_sector);
   222 	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   223 				   boot_sector, &length ) == PKT_ERR_OK ) {
   224 		bootstrap_dump(boot_sector, FALSE);
   225 		is_bootable = TRUE;
   226 	    }
   227 	}
   228     }
   229     if( !is_bootable ) {
   230 	WARN( "Disc does not appear to be bootable" );
   231     }
   232 }
.