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 (15 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
file annotate diff log raw
nkeynes@138
     1
/**
nkeynes@167
     2
 * $Id: gdrom.c,v 1.8 2006-06-19 11:00:42 nkeynes Exp $
nkeynes@138
     3
 *
nkeynes@138
     4
 * GD-Rom  access functions.
nkeynes@138
     5
 *
nkeynes@138
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@138
     7
 *
nkeynes@138
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@138
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@138
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@138
    11
 * (at your option) any later version.
nkeynes@138
    12
 *
nkeynes@138
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@138
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@138
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@138
    16
 * GNU General Public License for more details.
nkeynes@138
    17
 */
nkeynes@138
    18
nkeynes@138
    19
#include <stdio.h>
nkeynes@138
    20
nkeynes@138
    21
#include "gdrom/ide.h"
nkeynes@138
    22
#include "gdrom/gdrom.h"
nkeynes@143
    23
#include "gdrom/packet.h"
nkeynes@138
    24
#include "dream.h"
nkeynes@138
    25
nkeynes@138
    26
static void gdrom_image_destroy( gdrom_disc_t );
nkeynes@152
    27
static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, int, char *, uint32_t * );
nkeynes@138
    28
nkeynes@138
    29
nkeynes@138
    30
gdrom_disc_t gdrom_disc = NULL;
nkeynes@138
    31
nkeynes@138
    32
char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
nkeynes@138
    33
uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
nkeynes@138
    34
nkeynes@138
    35
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    36
{
nkeynes@138
    37
    return nrg_image_open( filename );
nkeynes@138
    38
}
nkeynes@138
    39
nkeynes@138
    40
nkeynes@138
    41
gdrom_disc_t gdrom_image_new( FILE *file )
nkeynes@138
    42
{
nkeynes@138
    43
    struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
nkeynes@138
    44
    if( disc == NULL )
nkeynes@138
    45
	return NULL;
nkeynes@138
    46
    disc->read_sectors = gdrom_image_read_sectors;
nkeynes@138
    47
    disc->close = gdrom_image_destroy;
nkeynes@138
    48
    disc->disc_type = IDE_DISC_CDROM;
nkeynes@138
    49
    disc->file = file;
nkeynes@138
    50
    return disc;
nkeynes@138
    51
}
nkeynes@138
    52
nkeynes@138
    53
static void gdrom_image_destroy( gdrom_disc_t disc )
nkeynes@138
    54
{
nkeynes@138
    55
    if( disc->file != NULL ) {
nkeynes@138
    56
	fclose(disc->file);
nkeynes@138
    57
	disc->file = NULL;
nkeynes@138
    58
    }
nkeynes@138
    59
    free( disc );
nkeynes@138
    60
}
nkeynes@138
    61
nkeynes@152
    62
static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@152
    63
					       uint32_t sector_count, int mode, char *buf,
nkeynes@152
    64
					       uint32_t *length )
nkeynes@138
    65
{
nkeynes@152
    66
    int i, file_offset, read_len;
nkeynes@152
    67
    struct gdrom_track *track = NULL;
nkeynes@138
    68
nkeynes@138
    69
    for( i=0; i<disc->track_count; i++ ) {
nkeynes@138
    70
	if( disc->track[i].lba <= sector && 
nkeynes@142
    71
	    (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
nkeynes@152
    72
	    track = &disc->track[i];
nkeynes@138
    73
	    break;
nkeynes@138
    74
	}
nkeynes@138
    75
    }
nkeynes@152
    76
    if( track == NULL )
nkeynes@143
    77
	return PKT_ERR_BADREAD;
nkeynes@152
    78
nkeynes@152
    79
    file_offset = track->offset + track->sector_size * (sector - track->lba);
nkeynes@152
    80
    read_len = track->sector_size * sector_count;
nkeynes@152
    81
nkeynes@152
    82
    switch( mode ) {
nkeynes@152
    83
    case GDROM_GD:
nkeynes@158
    84
	// Temporarily comment this out - it's wrong, but...
nkeynes@158
    85
	//	if( track->mode != GDROM_GD ) 
nkeynes@158
    86
	//    return PKT_ERR_BADREADMODE;
nkeynes@158
    87
	// break;
nkeynes@152
    88
    case GDROM_MODE1:
nkeynes@152
    89
	switch( track->mode ) {
nkeynes@152
    90
	case GDROM_MODE1:
nkeynes@152
    91
	case GDROM_MODE2_XA1:
nkeynes@154
    92
	    fseek( disc->file, file_offset, SEEK_SET );
nkeynes@152
    93
	    fread( buf, track->sector_size, sector_count, disc->file );
nkeynes@152
    94
	    break;
nkeynes@152
    95
	case GDROM_MODE2:
nkeynes@152
    96
	    read_len = sector_count * 2048;
nkeynes@154
    97
	    file_offset += 8; /* skip the subheader */
nkeynes@152
    98
	    while( sector_count > 0 ) {
nkeynes@154
    99
		fseek( disc->file, file_offset, SEEK_SET );
nkeynes@152
   100
		fread( buf, 2048, 1, disc->file );
nkeynes@152
   101
		file_offset += track->sector_size;
nkeynes@152
   102
		buf += 2048;
nkeynes@152
   103
		sector_count--;
nkeynes@152
   104
	    }
nkeynes@152
   105
	    break;
nkeynes@152
   106
	default:
nkeynes@152
   107
	    return PKT_ERR_BADREADMODE;
nkeynes@152
   108
	}
nkeynes@152
   109
	break;
nkeynes@152
   110
    default:
nkeynes@143
   111
	return PKT_ERR_BADREADMODE;
nkeynes@152
   112
    }
nkeynes@152
   113
	    
nkeynes@143
   114
    *length = read_len;
nkeynes@143
   115
    return PKT_ERR_OK;
nkeynes@138
   116
}
nkeynes@138
   117
nkeynes@138
   118
uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
nkeynes@143
   119
			     int mode, char *buf, uint32_t *length )
nkeynes@138
   120
{
nkeynes@138
   121
    if( gdrom_disc == NULL )
nkeynes@143
   122
	return PKT_ERR_NODISC; /* No media */
nkeynes@143
   123
    return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
nkeynes@138
   124
}
nkeynes@138
   125
nkeynes@138
   126
nkeynes@167
   127
void gdrom_dump_disc_info( gdrom_disc_t disc ) {
nkeynes@138
   128
    int i;
nkeynes@167
   129
    int last_session = disc->track[disc->track_count-1].session;
nkeynes@167
   130
    gboolean is_bootable = FALSE;
nkeynes@167
   131
nkeynes@138
   132
    INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
nkeynes@138
   133
	  disc->track[disc->track_count-1].session + 1 );
nkeynes@167
   134
    if( last_session > 0 ) {
nkeynes@167
   135
	/* Boot track is the first data track of the last session, provided that it 
nkeynes@167
   136
	 * cannot be a single-session disc.
nkeynes@167
   137
	 */
nkeynes@167
   138
	int boot_track = -1;
nkeynes@167
   139
	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
nkeynes@167
   140
	    if( disc->track[i].flags & TRACK_DATA ) {
nkeynes@167
   141
		boot_track = i;
nkeynes@167
   142
	    }
nkeynes@167
   143
	}
nkeynes@167
   144
	if( boot_track != -1 ) {
nkeynes@167
   145
	    char boot_sector[2048];
nkeynes@167
   146
	    uint32_t length = sizeof(boot_sector);
nkeynes@167
   147
	    if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
nkeynes@167
   148
				    boot_sector, &length ) == PKT_ERR_OK ) {
nkeynes@167
   149
		bootstrap_dump(boot_sector, FALSE);
nkeynes@167
   150
		is_bootable = TRUE;
nkeynes@167
   151
	    }
nkeynes@167
   152
	}
nkeynes@138
   153
    }
nkeynes@167
   154
    if( !is_bootable )
nkeynes@167
   155
	WARN( "Disc does not appear to be bootable" );
nkeynes@138
   156
}
nkeynes@138
   157
nkeynes@149
   158
gdrom_error_t gdrom_get_toc( char *buf ) 
nkeynes@138
   159
{
nkeynes@142
   160
    struct gdrom_toc *toc = (struct gdrom_toc *)buf;
nkeynes@142
   161
    int i;
nkeynes@142
   162
nkeynes@138
   163
    if( gdrom_disc == NULL )
nkeynes@149
   164
	return PKT_ERR_NODISC;
nkeynes@142
   165
nkeynes@142
   166
    for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@142
   167
	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
nkeynes@142
   168
    }
nkeynes@142
   169
    toc->first = 0x0100 | gdrom_disc->track[0].flags;
nkeynes@142
   170
    toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
nkeynes@142
   171
    toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
nkeynes@142
   172
	gdrom_disc->track[i-1].flags;
nkeynes@142
   173
    for( ;i<99; i++ )
nkeynes@142
   174
	toc->track[i] = 0xFFFFFFFF;
nkeynes@149
   175
    return PKT_ERR_OK;
nkeynes@149
   176
}
nkeynes@149
   177
nkeynes@152
   178
gdrom_error_t gdrom_get_info( char *buf, int session )
nkeynes@149
   179
{
nkeynes@149
   180
    if( gdrom_disc == NULL )
nkeynes@149
   181
	return PKT_ERR_NODISC;
nkeynes@149
   182
    struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
nkeynes@149
   183
    unsigned int end_of_disc = last_track->lba + last_track->sector_count;
nkeynes@152
   184
    int i;
nkeynes@152
   185
    buf[0] = 0x01; /* Disc status? */
nkeynes@149
   186
    buf[1] = 0;
nkeynes@152
   187
nkeynes@152
   188
    if( session == 0 ) {
nkeynes@152
   189
	buf[2] = last_track->session+1; /* last session */
nkeynes@152
   190
	buf[3] = (end_of_disc >> 16) & 0xFF;
nkeynes@152
   191
	buf[4] = (end_of_disc >> 8) & 0xFF;
nkeynes@152
   192
	buf[5] = end_of_disc & 0xFF;
nkeynes@152
   193
	return PKT_ERR_OK;
nkeynes@152
   194
    } else {
nkeynes@152
   195
	session--;
nkeynes@152
   196
	for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@152
   197
	    if( gdrom_disc->track[i].session == session ) {
nkeynes@152
   198
		buf[2] = i+1; /* first track of session */
nkeynes@152
   199
		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
nkeynes@152
   200
		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
nkeynes@152
   201
		buf[5] = gdrom_disc->track[i].lba & 0xFF;
nkeynes@152
   202
		return PKT_ERR_OK;
nkeynes@152
   203
	    }
nkeynes@152
   204
	}
nkeynes@152
   205
	return PKT_ERR_BADFIELD; /* No such session */
nkeynes@152
   206
    }
nkeynes@152
   207
	
nkeynes@138
   208
}
nkeynes@138
   209
nkeynes@138
   210
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
   211
{
nkeynes@138
   212
    gdrom_unmount_disc();
nkeynes@138
   213
    gdrom_disc = disc;
nkeynes@138
   214
    idereg.disc = disc->disc_type | IDE_DISC_READY;
nkeynes@167
   215
    gdrom_dump_disc_info( disc );
nkeynes@138
   216
}
nkeynes@138
   217
nkeynes@138
   218
gdrom_disc_t gdrom_mount_image( const gchar *filename )
nkeynes@138
   219
{
nkeynes@138
   220
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@138
   221
    if( disc != NULL )
nkeynes@138
   222
	gdrom_mount_disc( disc );
nkeynes@138
   223
    return disc;
nkeynes@138
   224
}
nkeynes@138
   225
nkeynes@138
   226
void gdrom_unmount_disc( ) 
nkeynes@138
   227
{
nkeynes@138
   228
    if( gdrom_disc != NULL ) {
nkeynes@138
   229
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   230
    }
nkeynes@138
   231
    gdrom_disc = NULL;
nkeynes@138
   232
    idereg.disc = IDE_DISC_NONE;
nkeynes@138
   233
}
nkeynes@138
   234
nkeynes@138
   235
gboolean gdrom_is_mounted( void ) 
nkeynes@138
   236
{
nkeynes@138
   237
    return gdrom_disc != NULL;
nkeynes@138
   238
}
.