Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 149:d88dd2e9a190
prev143:9446fb6df0c5
next152:d42a4c5cc709
author nkeynes
date Sat May 20 06:24:49 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Add disc info + spin up (probably seek) commands
view annotate diff log raw
     1 /**
     2  * $Id: gdrom.c,v 1.4 2006-05-20 06:24:49 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 uint32_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 uint32_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, track = -1, track_offset, read_len;
    68     for( i=0; i<disc->track_count; i++ ) {
    69 	if( disc->track[i].lba <= sector && 
    70 	    (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
    71 	    track = i;
    72 	    break;
    73 	}
    74     }
    75     if( track == -1 )
    76 	return PKT_ERR_BADREAD;
    77     if( mode == GDROM_GD && disc->track[i].mode != GDROM_GD )
    78 	return PKT_ERR_BADREADMODE;
    80     track_offset = disc->track[track].sector_size * (sector - disc->track[track].lba);
    81     read_len = disc->track[track].sector_size * sector_count;
    82     fseek( disc->file, disc->track[track].offset + track_offset, SEEK_SET );
    83     fread( buf, disc->track[track].sector_size, sector_count, disc->file );
    84     *length = read_len;
    85     return PKT_ERR_OK;
    86 }
    88 uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
    89 			     int mode, char *buf, uint32_t *length )
    90 {
    91     if( gdrom_disc == NULL )
    92 	return PKT_ERR_NODISC; /* No media */
    93     return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
    94 }
    97 void gdrom_dump_disc( gdrom_disc_t disc ) {
    98     int i;
    99     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   100 	  disc->track[disc->track_count-1].session + 1 );
   101     for( i=0; i<disc->track_count; i++ ) {
   102 	INFO( "Sess %d Trk %d: Start %06X Length %06X, %s",
   103 	      disc->track[i].session+1, i+1, disc->track[i].lba,
   104 	      disc->track[i].sector_count, gdrom_mode_names[disc->track[i].mode] );
   105     }
   106 }
   108 gdrom_error_t gdrom_get_toc( char *buf ) 
   109 {
   110     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   111     int i;
   113     if( gdrom_disc == NULL )
   114 	return PKT_ERR_NODISC;
   116     for( i=0; i<gdrom_disc->track_count; i++ ) {
   117 	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
   118     }
   119     toc->first = 0x0100 | gdrom_disc->track[0].flags;
   120     toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
   121     toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
   122 	gdrom_disc->track[i-1].flags;
   123     for( ;i<99; i++ )
   124 	toc->track[i] = 0xFFFFFFFF;
   125     return PKT_ERR_OK;
   126 }
   128 gdrom_error_t gdrom_get_info( char *buf )
   129 {
   130     if( gdrom_disc == NULL )
   131 	return PKT_ERR_NODISC;
   132     struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
   133     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   135     buf[0] = 0x01; /* Unknown. First session? */
   136     buf[1] = 0;
   137     buf[2] = last_track->session+1; /* last session */
   138     buf[3] = (end_of_disc >> 16) & 0xFF;
   139     buf[4] = (end_of_disc >> 8) & 0xFF;
   140     buf[5] = end_of_disc & 0xFF;
   141     return PKT_ERR_OK;
   142 }
   144 void gdrom_mount_disc( gdrom_disc_t disc ) 
   145 {
   146     gdrom_unmount_disc();
   147     gdrom_disc = disc;
   148     idereg.disc = disc->disc_type | IDE_DISC_READY;
   149     gdrom_dump_disc( disc );
   150 }
   152 gdrom_disc_t gdrom_mount_image( const gchar *filename )
   153 {
   154     gdrom_disc_t disc = gdrom_image_open(filename);
   155     if( disc != NULL )
   156 	gdrom_mount_disc( disc );
   157     return disc;
   158 }
   160 void gdrom_unmount_disc( ) 
   161 {
   162     if( gdrom_disc != NULL ) {
   163 	gdrom_disc->close(gdrom_disc);
   164     }
   165     gdrom_disc = NULL;
   166     idereg.disc = IDE_DISC_NONE;
   167 }
   169 gboolean gdrom_is_mounted( void ) 
   170 {
   171     return gdrom_disc != NULL;
   172 }
.