Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 168:203a72138e16
prev167:71c0cc416a64
next237:6f1a429c9d12
author nkeynes
date Mon Jun 26 10:30:42 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Implement multi-format CD image support
Fix CDI support up to (more or less) work
view annotate diff log raw
     1 /**
     2  * $Id: gdrom.c,v 1.9 2006-06-26 10:30:42 nkeynes Exp $
     3  *
     4  * GD-Rom  access 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 <stdio.h>
    20 #include <errno.h>
    21 #include "gdrom/ide.h"
    22 #include "gdrom/gdrom.h"
    23 #include "gdrom/packet.h"
    24 #include "dream.h"
    26 static void gdrom_image_destroy( gdrom_disc_t );
    27 static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, int, char *, uint32_t * );
    29 gdrom_image_class_t gdrom_image_classes[] = { &nrg_image_class, &cdi_image_class, NULL };
    31 gdrom_disc_t gdrom_disc = NULL;
    33 char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
    34 uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
    36 gdrom_disc_t gdrom_image_open( const gchar *filename )
    37 {
    38     const gchar *ext = strrchr(filename, '.');
    39     gdrom_disc_t disc = NULL;
    40     FILE *f = fopen(filename, "ro");
    41     int i,j;
    42     gdrom_image_class_t extclz = NULL;
    44     if( f == NULL ) {
    45 	ERROR("Unable to open file '%s': %s", filename, strerror(errno));
    46 	return NULL;
    47     }
    49     /* try extensions */
    50     if( ext != NULL ) {
    51 	ext++; /* Skip the '.' */
    52 	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    53 	    if( strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
    54 		extclz = gdrom_image_classes[i];
    55 		if( extclz->is_valid_file(f) ) {
    56 		    disc = extclz->open_image_file(filename, f);
    57 		    if( disc != NULL )
    58 			return disc;
    59 		}
    60 		break;
    61 	    }
    62 	}
    63     }
    65     /* Okay, fall back to magic */
    66     for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
    67 	if( gdrom_image_classes[i] != extclz &&
    68 	    gdrom_image_classes[i]->is_valid_file(f) ) {
    69 	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
    70 	    if( disc != NULL )
    71 		return disc;
    72 	}
    73     }
    75     fclose(f);
    76     return NULL;
    77 }
    80 gdrom_disc_t gdrom_image_new( FILE *file )
    81 {
    82     struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
    83     if( disc == NULL )
    84 	return NULL;
    85     disc->read_sectors = gdrom_image_read_sectors;
    86     disc->close = gdrom_image_destroy;
    87     disc->disc_type = IDE_DISC_CDROM;
    88     disc->file = file;
    89     return disc;
    90 }
    92 static void gdrom_image_destroy( gdrom_disc_t disc )
    93 {
    94     if( disc->file != NULL ) {
    95 	fclose(disc->file);
    96 	disc->file = NULL;
    97     }
    98     free( disc );
    99 }
   101 static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
   102 					       uint32_t sector_count, int mode, char *buf,
   103 					       uint32_t *length )
   104 {
   105     int i, file_offset, read_len;
   106     struct gdrom_track *track = NULL;
   108     for( i=0; i<disc->track_count; i++ ) {
   109 	if( disc->track[i].lba <= sector && 
   110 	    (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
   111 	    track = &disc->track[i];
   112 	    break;
   113 	}
   114     }
   115     if( track == NULL )
   116 	return PKT_ERR_BADREAD;
   118     file_offset = track->offset + track->sector_size * (sector - track->lba);
   119     read_len = track->sector_size * sector_count;
   121     switch( mode ) {
   122     case GDROM_GD:
   123 	// Temporarily comment this out - it's wrong, but...
   124 	//	if( track->mode != GDROM_GD ) 
   125 	//    return PKT_ERR_BADREADMODE;
   126 	// break;
   127     case GDROM_MODE1:
   128 	switch( track->mode ) {
   129 	case GDROM_MODE1:
   130 	case GDROM_MODE2_XA1:
   131 	    fseek( disc->file, file_offset, SEEK_SET );
   132 	    fread( buf, track->sector_size, sector_count, disc->file );
   133 	    break;
   134 	case GDROM_MODE2:
   135 	    read_len = sector_count * 2048;
   136 	    file_offset += 8; /* skip the subheader */
   137 	    while( sector_count > 0 ) {
   138 		fseek( disc->file, file_offset, SEEK_SET );
   139 		fread( buf, 2048, 1, disc->file );
   140 		file_offset += track->sector_size;
   141 		buf += 2048;
   142 		sector_count--;
   143 	    }
   144 	    break;
   145 	default:
   146 	    return PKT_ERR_BADREADMODE;
   147 	}
   148 	break;
   149     default:
   150 	return PKT_ERR_BADREADMODE;
   151     }
   153     *length = read_len;
   154     return PKT_ERR_OK;
   155 }
   157 uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
   158 			     int mode, char *buf, uint32_t *length )
   159 {
   160     if( gdrom_disc == NULL )
   161 	return PKT_ERR_NODISC; /* No media */
   162     return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
   163 }
   166 void gdrom_dump_disc_info( gdrom_disc_t disc ) {
   167     int i;
   168     int last_session = disc->track[disc->track_count-1].session;
   169     gboolean is_bootable = FALSE;
   171     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   172 	  disc->track[disc->track_count-1].session + 1 );
   173     if( last_session > 0 ) {
   174 	/* Boot track is the first data track of the last session, provided that it 
   175 	 * cannot be a single-session disc.
   176 	 */
   177 	int boot_track = -1;
   178 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   179 	    if( disc->track[i].flags & TRACK_DATA ) {
   180 		boot_track = i;
   181 	    }
   182 	}
   183 	if( boot_track != -1 ) {
   184 	    char boot_sector[2048];
   185 	    uint32_t length = sizeof(boot_sector);
   186 	    if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
   187 				    boot_sector, &length ) == PKT_ERR_OK ) {
   188 		bootstrap_dump(boot_sector, FALSE);
   189 		is_bootable = TRUE;
   190 	    }
   191 	}
   192     }
   193     if( !is_bootable )
   194 	WARN( "Disc does not appear to be bootable" );
   195 }
   197 gdrom_error_t gdrom_get_toc( char *buf ) 
   198 {
   199     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   200     int i;
   202     if( gdrom_disc == NULL )
   203 	return PKT_ERR_NODISC;
   205     for( i=0; i<gdrom_disc->track_count; i++ ) {
   206 	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
   207     }
   208     toc->first = 0x0100 | gdrom_disc->track[0].flags;
   209     toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
   210     toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
   211 	gdrom_disc->track[i-1].flags;
   212     for( ;i<99; i++ )
   213 	toc->track[i] = 0xFFFFFFFF;
   214     return PKT_ERR_OK;
   215 }
   217 gdrom_error_t gdrom_get_info( char *buf, int session )
   218 {
   219     if( gdrom_disc == NULL )
   220 	return PKT_ERR_NODISC;
   221     struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
   222     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   223     int i;
   224     buf[0] = 0x01; /* Disc status? */
   225     buf[1] = 0;
   227     if( session == 0 ) {
   228 	buf[2] = last_track->session+1; /* last session */
   229 	buf[3] = (end_of_disc >> 16) & 0xFF;
   230 	buf[4] = (end_of_disc >> 8) & 0xFF;
   231 	buf[5] = end_of_disc & 0xFF;
   232 	return PKT_ERR_OK;
   233     } else {
   234 	session--;
   235 	for( i=0; i<gdrom_disc->track_count; i++ ) {
   236 	    if( gdrom_disc->track[i].session == session ) {
   237 		buf[2] = i+1; /* first track of session */
   238 		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
   239 		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
   240 		buf[5] = gdrom_disc->track[i].lba & 0xFF;
   241 		return PKT_ERR_OK;
   242 	    }
   243 	}
   244 	return PKT_ERR_BADFIELD; /* No such session */
   245     }
   247 }
   249 void gdrom_mount_disc( gdrom_disc_t disc ) 
   250 {
   251     gdrom_unmount_disc();
   252     gdrom_disc = disc;
   253     idereg.disc = disc->disc_type | IDE_DISC_READY;
   254     gdrom_dump_disc_info( disc );
   255 }
   257 gdrom_disc_t gdrom_mount_image( const gchar *filename )
   258 {
   259     gdrom_disc_t disc = gdrom_image_open(filename);
   260     if( disc != NULL )
   261 	gdrom_mount_disc( disc );
   262     return disc;
   263 }
   265 void gdrom_unmount_disc( ) 
   266 {
   267     if( gdrom_disc != NULL ) {
   268 	gdrom_disc->close(gdrom_disc);
   269     }
   270     gdrom_disc = NULL;
   271     idereg.disc = IDE_DISC_NONE;
   272 }
   274 gboolean gdrom_is_mounted( void ) 
   275 {
   276     return gdrom_disc != NULL;
   277 }
.