Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 167:71c0cc416a64
prev158:a0a82246b44e
next168:203a72138e16
author nkeynes
date Mon Jun 19 11:00:42 2006 +0000 (17 years ago)
permissions -rw-r--r--
last change Add config value retrieval for pathnames
Implement default path for disc + save state loaders
Dump disc ID when mounting a CD
view annotate diff log raw
     1 /**
     2  * $Id: gdrom.c,v 1.8 2006-06-19 11:00: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>
    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 * );
    30 gdrom_disc_t gdrom_disc = NULL;
    32 char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
    33 uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
    35 gdrom_disc_t gdrom_image_open( const gchar *filename )
    36 {
    37     return nrg_image_open( filename );
    38 }
    41 gdrom_disc_t gdrom_image_new( FILE *file )
    42 {
    43     struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
    44     if( disc == NULL )
    45 	return NULL;
    46     disc->read_sectors = gdrom_image_read_sectors;
    47     disc->close = gdrom_image_destroy;
    48     disc->disc_type = IDE_DISC_CDROM;
    49     disc->file = file;
    50     return disc;
    51 }
    53 static void gdrom_image_destroy( gdrom_disc_t disc )
    54 {
    55     if( disc->file != NULL ) {
    56 	fclose(disc->file);
    57 	disc->file = NULL;
    58     }
    59     free( disc );
    60 }
    62 static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
    63 					       uint32_t sector_count, int mode, char *buf,
    64 					       uint32_t *length )
    65 {
    66     int i, file_offset, read_len;
    67     struct gdrom_track *track = NULL;
    69     for( i=0; i<disc->track_count; i++ ) {
    70 	if( disc->track[i].lba <= sector && 
    71 	    (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
    72 	    track = &disc->track[i];
    73 	    break;
    74 	}
    75     }
    76     if( track == NULL )
    77 	return PKT_ERR_BADREAD;
    79     file_offset = track->offset + track->sector_size * (sector - track->lba);
    80     read_len = track->sector_size * sector_count;
    82     switch( mode ) {
    83     case GDROM_GD:
    84 	// Temporarily comment this out - it's wrong, but...
    85 	//	if( track->mode != GDROM_GD ) 
    86 	//    return PKT_ERR_BADREADMODE;
    87 	// break;
    88     case GDROM_MODE1:
    89 	switch( track->mode ) {
    90 	case GDROM_MODE1:
    91 	case GDROM_MODE2_XA1:
    92 	    fseek( disc->file, file_offset, SEEK_SET );
    93 	    fread( buf, track->sector_size, sector_count, disc->file );
    94 	    break;
    95 	case GDROM_MODE2:
    96 	    read_len = sector_count * 2048;
    97 	    file_offset += 8; /* skip the subheader */
    98 	    while( sector_count > 0 ) {
    99 		fseek( disc->file, file_offset, SEEK_SET );
   100 		fread( buf, 2048, 1, disc->file );
   101 		file_offset += track->sector_size;
   102 		buf += 2048;
   103 		sector_count--;
   104 	    }
   105 	    break;
   106 	default:
   107 	    return PKT_ERR_BADREADMODE;
   108 	}
   109 	break;
   110     default:
   111 	return PKT_ERR_BADREADMODE;
   112     }
   114     *length = read_len;
   115     return PKT_ERR_OK;
   116 }
   118 uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
   119 			     int mode, char *buf, uint32_t *length )
   120 {
   121     if( gdrom_disc == NULL )
   122 	return PKT_ERR_NODISC; /* No media */
   123     return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
   124 }
   127 void gdrom_dump_disc_info( gdrom_disc_t disc ) {
   128     int i;
   129     int last_session = disc->track[disc->track_count-1].session;
   130     gboolean is_bootable = FALSE;
   132     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   133 	  disc->track[disc->track_count-1].session + 1 );
   134     if( last_session > 0 ) {
   135 	/* Boot track is the first data track of the last session, provided that it 
   136 	 * cannot be a single-session disc.
   137 	 */
   138 	int boot_track = -1;
   139 	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   140 	    if( disc->track[i].flags & TRACK_DATA ) {
   141 		boot_track = i;
   142 	    }
   143 	}
   144 	if( boot_track != -1 ) {
   145 	    char boot_sector[2048];
   146 	    uint32_t length = sizeof(boot_sector);
   147 	    if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
   148 				    boot_sector, &length ) == PKT_ERR_OK ) {
   149 		bootstrap_dump(boot_sector, FALSE);
   150 		is_bootable = TRUE;
   151 	    }
   152 	}
   153     }
   154     if( !is_bootable )
   155 	WARN( "Disc does not appear to be bootable" );
   156 }
   158 gdrom_error_t gdrom_get_toc( char *buf ) 
   159 {
   160     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   161     int i;
   163     if( gdrom_disc == NULL )
   164 	return PKT_ERR_NODISC;
   166     for( i=0; i<gdrom_disc->track_count; i++ ) {
   167 	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
   168     }
   169     toc->first = 0x0100 | gdrom_disc->track[0].flags;
   170     toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
   171     toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
   172 	gdrom_disc->track[i-1].flags;
   173     for( ;i<99; i++ )
   174 	toc->track[i] = 0xFFFFFFFF;
   175     return PKT_ERR_OK;
   176 }
   178 gdrom_error_t gdrom_get_info( char *buf, int session )
   179 {
   180     if( gdrom_disc == NULL )
   181 	return PKT_ERR_NODISC;
   182     struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
   183     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   184     int i;
   185     buf[0] = 0x01; /* Disc status? */
   186     buf[1] = 0;
   188     if( session == 0 ) {
   189 	buf[2] = last_track->session+1; /* last session */
   190 	buf[3] = (end_of_disc >> 16) & 0xFF;
   191 	buf[4] = (end_of_disc >> 8) & 0xFF;
   192 	buf[5] = end_of_disc & 0xFF;
   193 	return PKT_ERR_OK;
   194     } else {
   195 	session--;
   196 	for( i=0; i<gdrom_disc->track_count; i++ ) {
   197 	    if( gdrom_disc->track[i].session == session ) {
   198 		buf[2] = i+1; /* first track of session */
   199 		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
   200 		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
   201 		buf[5] = gdrom_disc->track[i].lba & 0xFF;
   202 		return PKT_ERR_OK;
   203 	    }
   204 	}
   205 	return PKT_ERR_BADFIELD; /* No such session */
   206     }
   208 }
   210 void gdrom_mount_disc( gdrom_disc_t disc ) 
   211 {
   212     gdrom_unmount_disc();
   213     gdrom_disc = disc;
   214     idereg.disc = disc->disc_type | IDE_DISC_READY;
   215     gdrom_dump_disc_info( disc );
   216 }
   218 gdrom_disc_t gdrom_mount_image( const gchar *filename )
   219 {
   220     gdrom_disc_t disc = gdrom_image_open(filename);
   221     if( disc != NULL )
   222 	gdrom_mount_disc( disc );
   223     return disc;
   224 }
   226 void gdrom_unmount_disc( ) 
   227 {
   228     if( gdrom_disc != NULL ) {
   229 	gdrom_disc->close(gdrom_disc);
   230     }
   231     gdrom_disc = NULL;
   232     idereg.disc = IDE_DISC_NONE;
   233 }
   235 gboolean gdrom_is_mounted( void ) 
   236 {
   237     return gdrom_disc != NULL;
   238 }
.