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