Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 245:a1d0655a88d3
prev237:6f1a429c9d12
next342:850502f0e8de
author nkeynes
date Tue Dec 19 09:52:56 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Work in progress: 0x40,1 (read status)
view annotate diff log raw
     1 /**
     2  * $Id: gdrom.c,v 1.11 2006-12-19 09:52:56 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     case GDROM_MODE2_XA1:
   141 	switch( track->mode ) {
   142 	case GDROM_MODE1:
   143 	case GDROM_MODE2_XA1:
   144 	    fseek( disc->file, file_offset, SEEK_SET );
   145 	    fread( buf, track->sector_size, sector_count, disc->file );
   146 	    break;
   147 	case GDROM_MODE2:
   148 	    read_len = sector_count * 2048;
   149 	    file_offset += 8; /* skip the subheader */
   150 	    while( sector_count > 0 ) {
   151 		fseek( disc->file, file_offset, SEEK_SET );
   152 		fread( buf, 2048, 1, disc->file );
   153 		file_offset += track->sector_size;
   154 		buf += 2048;
   155 		sector_count--;
   156 	    }
   157 	    break;
   158 	default:
   159 	    return PKT_ERR_BADREADMODE;
   160 	}
   161 	break;
   162     default:
   163 	return PKT_ERR_BADREADMODE;
   164     }
   166     *length = read_len;
   167     return PKT_ERR_OK;
   168 }
   170 uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
   171 			     int mode, char *buf, uint32_t *length )
   172 {
   173     if( gdrom_disc == NULL )
   174 	return PKT_ERR_NODISC; /* No media */
   175     return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
   176 }
   179 void gdrom_dump_disc_info( gdrom_disc_t disc ) {
   180     int i;
   181     int last_session = disc->track[disc->track_count-1].session;
   182     gboolean is_bootable = FALSE;
   184     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   185 	  disc->track[disc->track_count-1].session + 1 );
   186     if( last_session > 0 ) {
   187 	/* Boot track is the first data track of the last session, provided that it 
   188 	 * cannot be a single-session disc.
   189 	 */
   190 	int boot_track = -1;
   191 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   192 	    if( disc->track[i].flags & TRACK_DATA ) {
   193 		boot_track = i;
   194 	    }
   195 	}
   196 	if( boot_track != -1 ) {
   197 	    char boot_sector[2048];
   198 	    uint32_t length = sizeof(boot_sector);
   199 	    if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
   200 				    boot_sector, &length ) == PKT_ERR_OK ) {
   201 		bootstrap_dump(boot_sector, FALSE);
   202 		is_bootable = TRUE;
   203 	    }
   204 	}
   205     }
   206     if( !is_bootable )
   207 	WARN( "Disc does not appear to be bootable" );
   208 }
   210 gdrom_error_t gdrom_get_toc( char *buf ) 
   211 {
   212     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   213     int i;
   215     if( gdrom_disc == NULL )
   216 	return PKT_ERR_NODISC;
   218     for( i=0; i<gdrom_disc->track_count; i++ ) {
   219 	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
   220     }
   221     toc->first = 0x0100 | gdrom_disc->track[0].flags;
   222     toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
   223     toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
   224 	gdrom_disc->track[i-1].flags;
   225     for( ;i<99; i++ )
   226 	toc->track[i] = 0xFFFFFFFF;
   227     return PKT_ERR_OK;
   228 }
   230 gdrom_error_t gdrom_get_info( char *buf, int session )
   231 {
   232     if( gdrom_disc == NULL )
   233 	return PKT_ERR_NODISC;
   234     struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
   235     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   236     int i;
   237     buf[0] = 0x01; /* Disc status? */
   238     buf[1] = 0;
   240     if( session == 0 ) {
   241 	buf[2] = last_track->session+1; /* last session */
   242 	buf[3] = (end_of_disc >> 16) & 0xFF;
   243 	buf[4] = (end_of_disc >> 8) & 0xFF;
   244 	buf[5] = end_of_disc & 0xFF;
   245 	return PKT_ERR_OK;
   246     } else {
   247 	session--;
   248 	for( i=0; i<gdrom_disc->track_count; i++ ) {
   249 	    if( gdrom_disc->track[i].session == session ) {
   250 		buf[2] = i+1; /* first track of session */
   251 		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
   252 		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
   253 		buf[5] = gdrom_disc->track[i].lba & 0xFF;
   254 		return PKT_ERR_OK;
   255 	    }
   256 	}
   257 	return PKT_ERR_BADFIELD; /* No such session */
   258     }
   260 }
   262 gdrom_track_t gdrom_get_track( int trackno ) {
   263     if( gdrom_disc == NULL || trackno < 1 || trackno > 99 ) {
   264 	return NULL;
   265     } else {
   266 	return &gdrom_disc->track[trackno-1];
   267     }
   268 }
   270 uint8_t gdrom_get_track_no_by_lba( uint32_t lba ) {
   271     int i;
   272     if( gdrom_disc != NULL ) {
   273 	for( i=0; i<gdrom_disc->track_count; i++ ) {
   274 	    if( gdrom_disc->track[i].lba <= lba && 
   275 		lba <= (gdrom_disc->track[i].lba + gdrom_disc->track[i].sector_count) ) {
   276 		return i+1;
   277 	    }
   278 	}
   279     }
   280     return -1;
   281 }
   283 void gdrom_mount_disc( gdrom_disc_t disc ) 
   284 {
   285     gdrom_unmount_disc();
   286     gdrom_disc = disc;
   287     idereg.disc = disc->disc_type | IDE_DISC_READY;
   288     gdrom_dump_disc_info( disc );
   289 }
   291 gdrom_disc_t gdrom_mount_image( const gchar *filename )
   292 {
   293     gdrom_disc_t disc = gdrom_image_open(filename);
   294     if( disc != NULL )
   295 	gdrom_mount_disc( disc );
   296     return disc;
   297 }
   299 void gdrom_unmount_disc( ) 
   300 {
   301     if( gdrom_disc != NULL ) {
   302 	gdrom_disc->close(gdrom_disc);
   303     }
   304     gdrom_disc = NULL;
   305     idereg.disc = IDE_DISC_NONE;
   306 }
   308 gboolean gdrom_is_mounted( void ) 
   309 {
   310     return gdrom_disc != NULL;
   311 }
.