Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
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 (15 years ago)
permissions -rw-r--r--
last change Add Nero file format handler and general gdrom disc hooks
view annotate diff log raw
     1 /**
     2  * $Id: gdrom.c,v 1.1 2006-04-30 01:51:08 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 "dream.h"
    25 static void gdrom_image_destroy( gdrom_disc_t );
    26 static uint32_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, char * );
    29 gdrom_disc_t gdrom_disc = NULL;
    31 char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
    32 uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
    34 gdrom_disc_t gdrom_image_open( const gchar *filename )
    35 {
    36     return nrg_image_open( filename );
    37 }
    40 gdrom_disc_t gdrom_image_new( FILE *file )
    41 {
    42     struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
    43     if( disc == NULL )
    44 	return NULL;
    45     disc->read_sectors = gdrom_image_read_sectors;
    46     disc->close = gdrom_image_destroy;
    47     disc->disc_type = IDE_DISC_CDROM;
    48     disc->file = file;
    49     return disc;
    50 }
    52 static void gdrom_image_destroy( gdrom_disc_t disc )
    53 {
    54     if( disc->file != NULL ) {
    55 	fclose(disc->file);
    56 	disc->file = NULL;
    57     }
    58     free( disc );
    59 }
    61 static uint32_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
    62 				   uint32_t sector_count, char *buf )
    63 {
    64     int i, track = -1, track_offset, read_len;
    66     for( i=0; i<disc->track_count; i++ ) {
    67 	if( disc->track[i].lba <= sector && 
    68 	    disc->track[i].lba + disc->track[i].sector_count <= sector + sector_count ) {
    69 	    track = i;
    70 	    break;
    71 	}
    72     }
    73     if( track == -1 )
    74 	return 0;
    76     track_offset = disc->track[track].sector_size * (sector - disc->track[track].lba);
    77     read_len = disc->track[track].sector_size * sector_count;
    78     fseek( disc->file, disc->track[track].offset + track_offset, SEEK_SET );
    79     fread( buf, disc->track[track].sector_size, sector_count, disc->file );
    80     return read_len;
    81 }
    83 uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
    84 			     char *buf )
    85 {
    86     if( gdrom_disc == NULL )
    87 	return 0; /* No media */
    88     return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, buf );
    89 }
    92 void gdrom_dump_disc( gdrom_disc_t disc ) {
    93     int i;
    94     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
    95 	  disc->track[disc->track_count-1].session + 1 );
    96     for( i=0; i<disc->track_count; i++ ) {
    97 	INFO( "Sess %d Trk %d: Start %06X Length %06X, %s",
    98 	      disc->track[i].session+1, i+1, disc->track[i].lba,
    99 	      disc->track[i].sector_count, gdrom_mode_names[disc->track[i].mode] );
   100     }
   101 }
   103 gboolean gdrom_get_toc( char *buf ) 
   104 {
   105     if( gdrom_disc == NULL )
   106 	return FALSE;
   107     return TRUE;
   108 }
   110 void gdrom_mount_disc( gdrom_disc_t disc ) 
   111 {
   112     gdrom_unmount_disc();
   113     gdrom_disc = disc;
   114     idereg.disc = disc->disc_type | IDE_DISC_READY;
   115     gdrom_dump_disc( disc );
   116 }
   118 gdrom_disc_t gdrom_mount_image( const gchar *filename )
   119 {
   120     gdrom_disc_t disc = gdrom_image_open(filename);
   121     if( disc != NULL )
   122 	gdrom_mount_disc( disc );
   123     return disc;
   124 }
   126 void gdrom_unmount_disc( ) 
   127 {
   128     if( gdrom_disc != NULL ) {
   129 	gdrom_disc->close(gdrom_disc);
   130     }
   131     gdrom_disc = NULL;
   132     idereg.disc = IDE_DISC_NONE;
   133 }
   135 gboolean gdrom_is_mounted( void ) 
   136 {
   137     return gdrom_disc != NULL;
   138 }
.