Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 152:d42a4c5cc709
prev149:d88dd2e9a190
next154:d62f7b49934b
author nkeynes
date Tue May 23 13:11:45 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Clean up the buffer and i/o handling
Implement save/load state
view annotate diff log raw
     1 /**
     2  * $Id: gdrom.c,v 1.5 2006-05-23 13:11:45 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;
    81     fseek( disc->file, file_offset, SEEK_SET );
    83     switch( mode ) {
    84     case GDROM_GD:
    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 	    fread( buf, track->sector_size, sector_count, disc->file );
    93 	    break;
    94 	case GDROM_MODE2:
    95 	    read_len = sector_count * 2048;
    96 	    while( sector_count > 0 ) {
    97 		fread( buf, 2048, 1, disc->file );
    98 		file_offset += track->sector_size;
    99 		buf += 2048;
   100 		fseek( disc->file, file_offset, SEEK_SET );
   101 		sector_count--;
   102 	    }
   103 	    break;
   104 	default:
   105 	    return PKT_ERR_BADREADMODE;
   106 	}
   107 	break;
   108     default:
   109 	return PKT_ERR_BADREADMODE;
   110     }
   112     *length = read_len;
   113     return PKT_ERR_OK;
   114 }
   116 uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
   117 			     int mode, char *buf, uint32_t *length )
   118 {
   119     if( gdrom_disc == NULL )
   120 	return PKT_ERR_NODISC; /* No media */
   121     return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
   122 }
   125 void gdrom_dump_disc( gdrom_disc_t disc ) {
   126     int i;
   127     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   128 	  disc->track[disc->track_count-1].session + 1 );
   129     for( i=0; i<disc->track_count; i++ ) {
   130 	INFO( "Sess %d Trk %d: Start %06X Length %06X, %s",
   131 	      disc->track[i].session+1, i+1, disc->track[i].lba,
   132 	      disc->track[i].sector_count, gdrom_mode_names[disc->track[i].mode] );
   133     }
   134 }
   136 gdrom_error_t gdrom_get_toc( char *buf ) 
   137 {
   138     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   139     int i;
   141     if( gdrom_disc == NULL )
   142 	return PKT_ERR_NODISC;
   144     for( i=0; i<gdrom_disc->track_count; i++ ) {
   145 	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
   146     }
   147     toc->first = 0x0100 | gdrom_disc->track[0].flags;
   148     toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
   149     toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
   150 	gdrom_disc->track[i-1].flags;
   151     for( ;i<99; i++ )
   152 	toc->track[i] = 0xFFFFFFFF;
   153     return PKT_ERR_OK;
   154 }
   156 gdrom_error_t gdrom_get_info( char *buf, int session )
   157 {
   158     if( gdrom_disc == NULL )
   159 	return PKT_ERR_NODISC;
   160     struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
   161     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   162     int i;
   163     buf[0] = 0x01; /* Disc status? */
   164     buf[1] = 0;
   166     if( session == 0 ) {
   167 	buf[2] = last_track->session+1; /* last session */
   168 	buf[3] = (end_of_disc >> 16) & 0xFF;
   169 	buf[4] = (end_of_disc >> 8) & 0xFF;
   170 	buf[5] = end_of_disc & 0xFF;
   171 	return PKT_ERR_OK;
   172     } else {
   173 	session--;
   174 	for( i=0; i<gdrom_disc->track_count; i++ ) {
   175 	    if( gdrom_disc->track[i].session == session ) {
   176 		buf[2] = i+1; /* first track of session */
   177 		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
   178 		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
   179 		buf[5] = gdrom_disc->track[i].lba & 0xFF;
   180 		return PKT_ERR_OK;
   181 	    }
   182 	}
   183 	return PKT_ERR_BADFIELD; /* No such session */
   184     }
   186 }
   188 void gdrom_mount_disc( gdrom_disc_t disc ) 
   189 {
   190     gdrom_unmount_disc();
   191     gdrom_disc = disc;
   192     idereg.disc = disc->disc_type | IDE_DISC_READY;
   193     gdrom_dump_disc( disc );
   194 }
   196 gdrom_disc_t gdrom_mount_image( const gchar *filename )
   197 {
   198     gdrom_disc_t disc = gdrom_image_open(filename);
   199     if( disc != NULL )
   200 	gdrom_mount_disc( disc );
   201     return disc;
   202 }
   204 void gdrom_unmount_disc( ) 
   205 {
   206     if( gdrom_disc != NULL ) {
   207 	gdrom_disc->close(gdrom_disc);
   208     }
   209     gdrom_disc = NULL;
   210     idereg.disc = IDE_DISC_NONE;
   211 }
   213 gboolean gdrom_is_mounted( void ) 
   214 {
   215     return gdrom_disc != NULL;
   216 }
.