Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 422:61a0598e07ff
prev342:850502f0e8de
next464:8e099fad42a6
author nkeynes
date Sat Oct 06 09:03:24 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
view annotate diff log raw
     1 /**
     2  * $Id: gdimage.c,v 1.2 2007-10-06 08:58:00 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( 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;
    59     gdrom_disc_t disc = (gdrom_disc_t)image;
    60     gdrom_image_init(disc);
    61     return disc;
    62 }
    64 static void gdrom_image_destroy( gdrom_disc_t disc )
    65 {
    66     gdrom_image_t img = (gdrom_image_t)disc;
    67     if( img->file != NULL ) {
    68 	fclose(img->file);
    69 	img->file = NULL;
    70     }
    71     free( disc );
    72 }
    74 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
    75 {
    76     int i;
    77     for( i=0; i<image->track_count; i++ ) {
    78 	if( image->track[i].lba <= lba && 
    79 	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
    80 	    return i+1;
    81 	}
    82     }
    83     return -1;
    84 }
    86 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
    87 					      int mode, unsigned char *buf, uint32_t *length )
    88 {
    89     gdrom_image_t image = (gdrom_image_t)disc;
    90     int file_offset, read_len, track_no;
    92     track_no = gdrom_image_get_track_by_lba( image, lba );
    93     if( track_no == -1 ) {
    94 	return PKT_ERR_BADREAD;
    95     }
    96     struct gdrom_track *track = &image->track[track_no-1];
    97     file_offset = track->offset + track->sector_size * (lba - track->lba);
    98     read_len = track->sector_size;
   100     switch( mode ) {
   101     case 0x24:
   102     case 0x28:
   103 	switch( track->mode ) {
   104 	case GDROM_MODE1:
   105 	case GDROM_MODE2_XA1:
   106 	    fseek( image->file, file_offset, SEEK_SET );
   107 	    fread( buf, track->sector_size, 1, image->file );
   108 	    break;
   109 	case GDROM_MODE2:
   110 	    read_len = 2048;
   111 	    file_offset += 8; /* skip the subheader */
   112 	    fseek( image->file, file_offset, SEEK_SET );
   113 	    fread( buf, 2048, 1, image->file );
   114 	    break;
   115 	default:
   116 	    return PKT_ERR_BADREADMODE;
   117 	}
   118 	break;
   119     default:
   120 	return PKT_ERR_BADREADMODE;
   121     }
   123     *length = read_len;
   124     return PKT_ERR_OK;
   126 }
   128 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   129 {
   130     gdrom_image_t image = (gdrom_image_t)disc;
   131     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   132     int i;
   134     for( i=0; i<image->track_count; i++ ) {
   135 	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   136     }
   137     toc->first = 0x0100 | image->track[0].flags;
   138     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   139     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   140 	image->track[i-1].flags;
   141     for( ;i<99; i++ )
   142 	toc->track[i] = 0xFFFFFFFF;
   143     return PKT_ERR_OK;
   144 }
   146 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   147 {
   148     gdrom_image_t image = (gdrom_image_t)disc;
   149     struct gdrom_track *last_track = &image->track[image->track_count-1];
   150     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   151     int i;
   152     buf[0] = 0x01; /* Disc status? */
   153     buf[1] = 0;
   155     if( session == 0 ) {
   156 	buf[2] = last_track->session+1; /* last session */
   157 	buf[3] = (end_of_disc >> 16) & 0xFF;
   158 	buf[4] = (end_of_disc >> 8) & 0xFF;
   159 	buf[5] = end_of_disc & 0xFF;
   160 	return PKT_ERR_OK;
   161     } else {
   162 	session--;
   163 	for( i=0; i<image->track_count; i++ ) {
   164 	    if( image->track[i].session == session ) {
   165 		buf[2] = i+1; /* first track of session */
   166 		buf[3] = (image->track[i].lba >> 16) & 0xFF;
   167 		buf[4] = (image->track[i].lba >> 8) & 0xFF;
   168 		buf[5] = image->track[i].lba & 0xFF;
   169 		return PKT_ERR_OK;
   170 	    }
   171 	}
   172 	return PKT_ERR_BADFIELD; /* No such session */
   173     }
   174 }
   176 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   177 {
   178     gdrom_image_t image = (gdrom_image_t)disc;
   179     int track_no = gdrom_image_get_track_by_lba( image, lba );
   180     if( track_no == -1 ) {
   181 	track_no = 1;
   182 	lba = 150;
   183     }
   184     struct gdrom_track *track = &image->track[track_no-1];
   185     uint32_t offset = lba - track->lba;
   186     buf[4] = track->flags;
   187     buf[5] = track_no;
   188     buf[6] = 0x01; /* ?? */
   189     buf[7] = (offset >> 16) & 0xFF;
   190     buf[8] = (offset >> 8) & 0xFF;
   191     buf[9] = offset & 0xFF;
   192     buf[10] = 0;
   193     buf[11] = (lba >> 16) & 0xFF;
   194     buf[12] = (lba >> 8) & 0xFF;
   195     buf[13] = lba & 0xFF;
   196     return PKT_ERR_OK;
   197 }
   199 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   200 {
   201     gdrom_image_t image = (gdrom_image_t)disc;
   202     return image->disc_type | IDE_DISC_READY;
   203 }
   205 void gdrom_image_dump_info( gdrom_disc_t d ) {
   206     gdrom_image_t disc = (gdrom_image_t)d;
   207     int i;
   208     int last_session = disc->track[disc->track_count-1].session;
   209     gboolean is_bootable = FALSE;
   211     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   212 	  disc->track[disc->track_count-1].session + 1 );
   213     if( last_session > 0 ) {
   214 	/* Boot track is the first data track of the last session, provided that it 
   215 	 * cannot be a single-session disc.
   216 	 */
   217 	int boot_track = -1;
   218 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   219 	    if( disc->track[i].flags & TRACK_DATA ) {
   220 		boot_track = i;
   221 	    }
   222 	}
   223 	if( boot_track != -1 ) {
   224 	    unsigned char boot_sector[MAX_SECTOR_SIZE];
   225 	    uint32_t length = sizeof(boot_sector);
   226 	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   227 				   boot_sector, &length ) == PKT_ERR_OK ) {
   228 		bootstrap_dump(boot_sector, FALSE);
   229 		is_bootable = TRUE;
   230 	    }
   231 	}
   232     }
   233     if( !is_bootable ) {
   234 	WARN( "Disc does not appear to be bootable" );
   235     }
   236 }
.