Search
lxdream.org :: lxdream/src/gdrom/gdrom.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 138:afabd7e6d26d
next142:2f631c3a3946
author nkeynes
date Sun Apr 30 01:51:08 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Add Nero file format handler and general gdrom disc hooks
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/gdrom/gdrom.c Sun Apr 30 01:51:08 2006 +0000
1.3 @@ -0,0 +1,138 @@
1.4 +/**
1.5 + * $Id: gdrom.c,v 1.1 2006-04-30 01:51:08 nkeynes Exp $
1.6 + *
1.7 + * GD-Rom access functions.
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <stdio.h>
1.23 +
1.24 +#include "gdrom/ide.h"
1.25 +#include "gdrom/gdrom.h"
1.26 +#include "dream.h"
1.27 +
1.28 +static void gdrom_image_destroy( gdrom_disc_t );
1.29 +static uint32_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, char * );
1.30 +
1.31 +
1.32 +gdrom_disc_t gdrom_disc = NULL;
1.33 +
1.34 +char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
1.35 +uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
1.36 +
1.37 +gdrom_disc_t gdrom_image_open( const gchar *filename )
1.38 +{
1.39 + return nrg_image_open( filename );
1.40 +}
1.41 +
1.42 +
1.43 +gdrom_disc_t gdrom_image_new( FILE *file )
1.44 +{
1.45 + struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
1.46 + if( disc == NULL )
1.47 + return NULL;
1.48 + disc->read_sectors = gdrom_image_read_sectors;
1.49 + disc->close = gdrom_image_destroy;
1.50 + disc->disc_type = IDE_DISC_CDROM;
1.51 + disc->file = file;
1.52 + return disc;
1.53 +}
1.54 +
1.55 +static void gdrom_image_destroy( gdrom_disc_t disc )
1.56 +{
1.57 + if( disc->file != NULL ) {
1.58 + fclose(disc->file);
1.59 + disc->file = NULL;
1.60 + }
1.61 + free( disc );
1.62 +}
1.63 +
1.64 +static uint32_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
1.65 + uint32_t sector_count, char *buf )
1.66 +{
1.67 + int i, track = -1, track_offset, read_len;
1.68 +
1.69 + for( i=0; i<disc->track_count; i++ ) {
1.70 + if( disc->track[i].lba <= sector &&
1.71 + disc->track[i].lba + disc->track[i].sector_count <= sector + sector_count ) {
1.72 + track = i;
1.73 + break;
1.74 + }
1.75 + }
1.76 + if( track == -1 )
1.77 + return 0;
1.78 +
1.79 + track_offset = disc->track[track].sector_size * (sector - disc->track[track].lba);
1.80 + read_len = disc->track[track].sector_size * sector_count;
1.81 + fseek( disc->file, disc->track[track].offset + track_offset, SEEK_SET );
1.82 + fread( buf, disc->track[track].sector_size, sector_count, disc->file );
1.83 + return read_len;
1.84 +}
1.85 +
1.86 +uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
1.87 + char *buf )
1.88 +{
1.89 + if( gdrom_disc == NULL )
1.90 + return 0; /* No media */
1.91 + return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, buf );
1.92 +}
1.93 +
1.94 +
1.95 +void gdrom_dump_disc( gdrom_disc_t disc ) {
1.96 + int i;
1.97 + INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count,
1.98 + disc->track[disc->track_count-1].session + 1 );
1.99 + for( i=0; i<disc->track_count; i++ ) {
1.100 + INFO( "Sess %d Trk %d: Start %06X Length %06X, %s",
1.101 + disc->track[i].session+1, i+1, disc->track[i].lba,
1.102 + disc->track[i].sector_count, gdrom_mode_names[disc->track[i].mode] );
1.103 + }
1.104 +}
1.105 +
1.106 +gboolean gdrom_get_toc( char *buf )
1.107 +{
1.108 + if( gdrom_disc == NULL )
1.109 + return FALSE;
1.110 + return TRUE;
1.111 +}
1.112 +
1.113 +void gdrom_mount_disc( gdrom_disc_t disc )
1.114 +{
1.115 + gdrom_unmount_disc();
1.116 + gdrom_disc = disc;
1.117 + idereg.disc = disc->disc_type | IDE_DISC_READY;
1.118 + gdrom_dump_disc( disc );
1.119 +}
1.120 +
1.121 +gdrom_disc_t gdrom_mount_image( const gchar *filename )
1.122 +{
1.123 + gdrom_disc_t disc = gdrom_image_open(filename);
1.124 + if( disc != NULL )
1.125 + gdrom_mount_disc( disc );
1.126 + return disc;
1.127 +}
1.128 +
1.129 +void gdrom_unmount_disc( )
1.130 +{
1.131 + if( gdrom_disc != NULL ) {
1.132 + gdrom_disc->close(gdrom_disc);
1.133 + }
1.134 + gdrom_disc = NULL;
1.135 + idereg.disc = IDE_DISC_NONE;
1.136 +}
1.137 +
1.138 +gboolean gdrom_is_mounted( void )
1.139 +{
1.140 + return gdrom_disc != NULL;
1.141 +}
.