Search
lxdream.org :: lxdream/src/gdrom/gdimage.c :: diff
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 (13 years ago)
permissions -rw-r--r--
last change Refactor gdrom module to be more conducive to real device support
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gdrom/gdimage.c Wed Jan 31 10:58:42 2007 +0000
1.3 @@ -0,0 +1,233 @@
1.4 +/**
1.5 + * $Id: gdimage.c,v 1.1 2007-01-31 10:58:42 nkeynes Exp $
1.6 + *
1.7 + * GD-Rom image-file common functions.
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include "gdrom/gdrom.h"
1.23 +#include "gdrom/packet.h"
1.24 +
1.25 +static void gdrom_image_destroy( gdrom_disc_t disc );
1.26 +static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode,
1.27 + char *buf, uint32_t *readlength );
1.28 +static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, char *buf );
1.29 +static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, char *buf );
1.30 +static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, char *buf );
1.31 +static int gdrom_image_drive_status( gdrom_disc_t disc );
1.32 +
1.33 +
1.34 +/**
1.35 + * Initialize a gdrom_disc structure with the gdrom_image_* methods
1.36 + */
1.37 +void gdrom_image_init( gdrom_disc_t disc )
1.38 +{
1.39 + memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
1.40 + disc->read_sector = gdrom_image_read_sector;
1.41 + disc->read_toc = gdrom_image_read_toc;
1.42 + disc->read_session = gdrom_image_read_session;
1.43 + disc->read_position = gdrom_image_read_position;
1.44 + disc->drive_status = gdrom_image_drive_status;
1.45 + disc->play_audio = NULL; /* not supported yet */
1.46 + disc->run_time_slice = NULL; /* not needed */
1.47 + disc->close = gdrom_image_destroy;
1.48 +}
1.49 +
1.50 +gdrom_disc_t gdrom_image_new( FILE *f )
1.51 +{
1.52 + gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
1.53 + if( image == NULL ) {
1.54 + return NULL;
1.55 + }
1.56 + image->disc_type = IDE_DISC_CDROM;
1.57 + image->file = f;
1.58 +
1.59 + gdrom_disc_t disc = (gdrom_disc_t)image;
1.60 + gdrom_image_init(disc);
1.61 + return disc;
1.62 +}
1.63 +
1.64 +static void gdrom_image_destroy( gdrom_disc_t disc )
1.65 +{
1.66 + gdrom_image_t img = (gdrom_image_t)disc;
1.67 + if( img->file != NULL ) {
1.68 + fclose(img->file);
1.69 + img->file = NULL;
1.70 + }
1.71 + free( disc );
1.72 +}
1.73 +
1.74 +static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
1.75 +{
1.76 + int i;
1.77 + for( i=0; i<image->track_count; i++ ) {
1.78 + if( image->track[i].lba <= lba &&
1.79 + lba < (image->track[i].lba + image->track[i].sector_count) ) {
1.80 + return i+1;
1.81 + }
1.82 + }
1.83 + return -1;
1.84 +}
1.85 +
1.86 +static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
1.87 + int mode, char *buf, uint32_t *length )
1.88 +{
1.89 + gdrom_image_t image = (gdrom_image_t)disc;
1.90 + int i, file_offset, read_len, track_no;
1.91 +
1.92 + track_no = gdrom_image_get_track_by_lba( image, lba );
1.93 + if( track_no == -1 ) {
1.94 + return PKT_ERR_BADREAD;
1.95 + }
1.96 + struct gdrom_track *track = &image->track[track_no-1];
1.97 + file_offset = track->offset + track->sector_size * (lba - track->lba);
1.98 + read_len = track->sector_size;
1.99 +
1.100 + switch( mode ) {
1.101 + case 0x24:
1.102 + case 0x28:
1.103 + switch( track->mode ) {
1.104 + case GDROM_MODE1:
1.105 + case GDROM_MODE2_XA1:
1.106 + fseek( image->file, file_offset, SEEK_SET );
1.107 + fread( buf, track->sector_size, 1, image->file );
1.108 + break;
1.109 + case GDROM_MODE2:
1.110 + read_len = 2048;
1.111 + file_offset += 8; /* skip the subheader */
1.112 + fseek( image->file, file_offset, SEEK_SET );
1.113 + fread( buf, 2048, 1, image->file );
1.114 + break;
1.115 + default:
1.116 + return PKT_ERR_BADREADMODE;
1.117 + }
1.118 + break;
1.119 + default:
1.120 + return PKT_ERR_BADREADMODE;
1.121 + }
1.122 +
1.123 + *length = read_len;
1.124 + return PKT_ERR_OK;
1.125 +
1.126 +}
1.127 +
1.128 +static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, char *buf )
1.129 +{
1.130 + gdrom_image_t image = (gdrom_image_t)disc;
1.131 + struct gdrom_toc *toc = (struct gdrom_toc *)buf;
1.132 + int i;
1.133 +
1.134 + for( i=0; i<image->track_count; i++ ) {
1.135 + toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
1.136 + }
1.137 + toc->first = 0x0100 | image->track[0].flags;
1.138 + toc->last = (image->track_count<<8) | image->track[i-1].flags;
1.139 + toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
1.140 + image->track[i-1].flags;
1.141 + for( ;i<99; i++ )
1.142 + toc->track[i] = 0xFFFFFFFF;
1.143 + return PKT_ERR_OK;
1.144 +}
1.145 +
1.146 +static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, char *buf )
1.147 +{
1.148 + gdrom_image_t image = (gdrom_image_t)disc;
1.149 + struct gdrom_track *last_track = &image->track[image->track_count-1];
1.150 + unsigned int end_of_disc = last_track->lba + last_track->sector_count;
1.151 + int i;
1.152 + buf[0] = 0x01; /* Disc status? */
1.153 + buf[1] = 0;
1.154 +
1.155 + if( session == 0 ) {
1.156 + buf[2] = last_track->session+1; /* last session */
1.157 + buf[3] = (end_of_disc >> 16) & 0xFF;
1.158 + buf[4] = (end_of_disc >> 8) & 0xFF;
1.159 + buf[5] = end_of_disc & 0xFF;
1.160 + return PKT_ERR_OK;
1.161 + } else {
1.162 + session--;
1.163 + for( i=0; i<image->track_count; i++ ) {
1.164 + if( image->track[i].session == session ) {
1.165 + buf[2] = i+1; /* first track of session */
1.166 + buf[3] = (image->track[i].lba >> 16) & 0xFF;
1.167 + buf[4] = (image->track[i].lba >> 8) & 0xFF;
1.168 + buf[5] = image->track[i].lba & 0xFF;
1.169 + return PKT_ERR_OK;
1.170 + }
1.171 + }
1.172 + return PKT_ERR_BADFIELD; /* No such session */
1.173 + }
1.174 +}
1.175 +
1.176 +static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, char *buf )
1.177 +{
1.178 + gdrom_image_t image = (gdrom_image_t)disc;
1.179 + int track_no = gdrom_image_get_track_by_lba( image, lba );
1.180 + if( track_no == -1 ) {
1.181 + track_no = 1;
1.182 + lba = 150;
1.183 + }
1.184 + struct gdrom_track *track = &image->track[track_no-1];
1.185 + uint32_t offset = lba - track->lba;
1.186 + buf[4] = track->flags;
1.187 + buf[5] = track_no;
1.188 + buf[6] = 0x01; /* ?? */
1.189 + buf[7] = (offset >> 16) & 0xFF;
1.190 + buf[8] = (offset >> 8) & 0xFF;
1.191 + buf[9] = offset & 0xFF;
1.192 + buf[10] = 0;
1.193 + buf[11] = (lba >> 16) & 0xFF;
1.194 + buf[12] = (lba >> 8) & 0xFF;
1.195 + buf[13] = lba & 0xFF;
1.196 +}
1.197 +
1.198 +static int gdrom_image_drive_status( gdrom_disc_t disc )
1.199 +{
1.200 + gdrom_image_t image = (gdrom_image_t)disc;
1.201 + return image->disc_type | IDE_DISC_READY;
1.202 +}
1.203 +
1.204 +void gdrom_image_dump_info( gdrom_disc_t d ) {
1.205 + gdrom_image_t disc = (gdrom_image_t)d;
1.206 + int i;
1.207 + int last_session = disc->track[disc->track_count-1].session;
1.208 + gboolean is_bootable = FALSE;
1.209 +
1.210 + INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count,
1.211 + disc->track[disc->track_count-1].session + 1 );
1.212 + if( last_session > 0 ) {
1.213 + /* Boot track is the first data track of the last session, provided that it
1.214 + * cannot be a single-session disc.
1.215 + */
1.216 + int boot_track = -1;
1.217 + for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
1.218 + if( disc->track[i].flags & TRACK_DATA ) {
1.219 + boot_track = i;
1.220 + }
1.221 + }
1.222 + if( boot_track != -1 ) {
1.223 + char boot_sector[MAX_SECTOR_SIZE];
1.224 + uint32_t length = sizeof(boot_sector);
1.225 + if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
1.226 + boot_sector, &length ) == PKT_ERR_OK ) {
1.227 + bootstrap_dump(boot_sector, FALSE);
1.228 + is_bootable = TRUE;
1.229 + }
1.230 + }
1.231 + }
1.232 + if( !is_bootable ) {
1.233 + WARN( "Disc does not appear to be bootable" );
1.234 + }
1.235 +}
1.236 +
.