Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 168:203a72138e16
prev167:71c0cc416a64
next237:6f1a429c9d12
author nkeynes
date Mon Jun 26 10:30:42 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Implement multi-format CD image support
Fix CDI support up to (more or less) work
file annotate diff log raw
nkeynes@138
     1
/**
nkeynes@168
     2
 * $Id: gdrom.c,v 1.9 2006-06-26 10:30: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@168
    20
#include <errno.h>
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@168
    29
gdrom_image_class_t gdrom_image_classes[] = { &nrg_image_class, &cdi_image_class, NULL };
nkeynes@138
    30
nkeynes@138
    31
gdrom_disc_t gdrom_disc = NULL;
nkeynes@138
    32
nkeynes@138
    33
char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
nkeynes@138
    34
uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
nkeynes@138
    35
nkeynes@138
    36
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    37
{
nkeynes@168
    38
    const gchar *ext = strrchr(filename, '.');
nkeynes@168
    39
    gdrom_disc_t disc = NULL;
nkeynes@168
    40
    FILE *f = fopen(filename, "ro");
nkeynes@168
    41
    int i,j;
nkeynes@168
    42
    gdrom_image_class_t extclz = NULL;
nkeynes@168
    43
nkeynes@168
    44
    if( f == NULL ) {
nkeynes@168
    45
	ERROR("Unable to open file '%s': %s", filename, strerror(errno));
nkeynes@168
    46
	return NULL;
nkeynes@168
    47
    }
nkeynes@168
    48
nkeynes@168
    49
    /* try extensions */
nkeynes@168
    50
    if( ext != NULL ) {
nkeynes@168
    51
	ext++; /* Skip the '.' */
nkeynes@168
    52
	for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@168
    53
	    if( strcasecmp( gdrom_image_classes[i]->extension, ext ) == 0 ) {
nkeynes@168
    54
		extclz = gdrom_image_classes[i];
nkeynes@168
    55
		if( extclz->is_valid_file(f) ) {
nkeynes@168
    56
		    disc = extclz->open_image_file(filename, f);
nkeynes@168
    57
		    if( disc != NULL )
nkeynes@168
    58
			return disc;
nkeynes@168
    59
		}
nkeynes@168
    60
		break;
nkeynes@168
    61
	    }
nkeynes@168
    62
	}
nkeynes@168
    63
    }
nkeynes@168
    64
nkeynes@168
    65
    /* Okay, fall back to magic */
nkeynes@168
    66
    for( i=0; gdrom_image_classes[i] != NULL; i++ ) {
nkeynes@168
    67
	if( gdrom_image_classes[i] != extclz &&
nkeynes@168
    68
	    gdrom_image_classes[i]->is_valid_file(f) ) {
nkeynes@168
    69
	    disc = gdrom_image_classes[i]->open_image_file(filename, f);
nkeynes@168
    70
	    if( disc != NULL )
nkeynes@168
    71
		return disc;
nkeynes@168
    72
	}
nkeynes@168
    73
    }
nkeynes@168
    74
nkeynes@168
    75
    fclose(f);
nkeynes@168
    76
    return NULL;
nkeynes@138
    77
}
nkeynes@138
    78
nkeynes@138
    79
nkeynes@138
    80
gdrom_disc_t gdrom_image_new( FILE *file )
nkeynes@138
    81
{
nkeynes@138
    82
    struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
nkeynes@138
    83
    if( disc == NULL )
nkeynes@138
    84
	return NULL;
nkeynes@138
    85
    disc->read_sectors = gdrom_image_read_sectors;
nkeynes@138
    86
    disc->close = gdrom_image_destroy;
nkeynes@138
    87
    disc->disc_type = IDE_DISC_CDROM;
nkeynes@138
    88
    disc->file = file;
nkeynes@138
    89
    return disc;
nkeynes@138
    90
}
nkeynes@138
    91
nkeynes@138
    92
static void gdrom_image_destroy( gdrom_disc_t disc )
nkeynes@138
    93
{
nkeynes@138
    94
    if( disc->file != NULL ) {
nkeynes@138
    95
	fclose(disc->file);
nkeynes@138
    96
	disc->file = NULL;
nkeynes@138
    97
    }
nkeynes@138
    98
    free( disc );
nkeynes@138
    99
}
nkeynes@138
   100
nkeynes@152
   101
static gdrom_error_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@152
   102
					       uint32_t sector_count, int mode, char *buf,
nkeynes@152
   103
					       uint32_t *length )
nkeynes@138
   104
{
nkeynes@152
   105
    int i, file_offset, read_len;
nkeynes@152
   106
    struct gdrom_track *track = NULL;
nkeynes@138
   107
nkeynes@138
   108
    for( i=0; i<disc->track_count; i++ ) {
nkeynes@138
   109
	if( disc->track[i].lba <= sector && 
nkeynes@142
   110
	    (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
nkeynes@152
   111
	    track = &disc->track[i];
nkeynes@138
   112
	    break;
nkeynes@138
   113
	}
nkeynes@138
   114
    }
nkeynes@152
   115
    if( track == NULL )
nkeynes@143
   116
	return PKT_ERR_BADREAD;
nkeynes@152
   117
nkeynes@152
   118
    file_offset = track->offset + track->sector_size * (sector - track->lba);
nkeynes@152
   119
    read_len = track->sector_size * sector_count;
nkeynes@152
   120
nkeynes@152
   121
    switch( mode ) {
nkeynes@152
   122
    case GDROM_GD:
nkeynes@158
   123
	// Temporarily comment this out - it's wrong, but...
nkeynes@158
   124
	//	if( track->mode != GDROM_GD ) 
nkeynes@158
   125
	//    return PKT_ERR_BADREADMODE;
nkeynes@158
   126
	// break;
nkeynes@152
   127
    case GDROM_MODE1:
nkeynes@152
   128
	switch( track->mode ) {
nkeynes@152
   129
	case GDROM_MODE1:
nkeynes@152
   130
	case GDROM_MODE2_XA1:
nkeynes@154
   131
	    fseek( disc->file, file_offset, SEEK_SET );
nkeynes@152
   132
	    fread( buf, track->sector_size, sector_count, disc->file );
nkeynes@152
   133
	    break;
nkeynes@152
   134
	case GDROM_MODE2:
nkeynes@152
   135
	    read_len = sector_count * 2048;
nkeynes@154
   136
	    file_offset += 8; /* skip the subheader */
nkeynes@152
   137
	    while( sector_count > 0 ) {
nkeynes@154
   138
		fseek( disc->file, file_offset, SEEK_SET );
nkeynes@152
   139
		fread( buf, 2048, 1, disc->file );
nkeynes@152
   140
		file_offset += track->sector_size;
nkeynes@152
   141
		buf += 2048;
nkeynes@152
   142
		sector_count--;
nkeynes@152
   143
	    }
nkeynes@152
   144
	    break;
nkeynes@152
   145
	default:
nkeynes@152
   146
	    return PKT_ERR_BADREADMODE;
nkeynes@152
   147
	}
nkeynes@152
   148
	break;
nkeynes@152
   149
    default:
nkeynes@143
   150
	return PKT_ERR_BADREADMODE;
nkeynes@152
   151
    }
nkeynes@152
   152
	    
nkeynes@143
   153
    *length = read_len;
nkeynes@143
   154
    return PKT_ERR_OK;
nkeynes@138
   155
}
nkeynes@138
   156
nkeynes@138
   157
uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
nkeynes@143
   158
			     int mode, char *buf, uint32_t *length )
nkeynes@138
   159
{
nkeynes@138
   160
    if( gdrom_disc == NULL )
nkeynes@143
   161
	return PKT_ERR_NODISC; /* No media */
nkeynes@143
   162
    return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
nkeynes@138
   163
}
nkeynes@138
   164
nkeynes@138
   165
nkeynes@167
   166
void gdrom_dump_disc_info( gdrom_disc_t disc ) {
nkeynes@138
   167
    int i;
nkeynes@167
   168
    int last_session = disc->track[disc->track_count-1].session;
nkeynes@167
   169
    gboolean is_bootable = FALSE;
nkeynes@167
   170
nkeynes@138
   171
    INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
nkeynes@138
   172
	  disc->track[disc->track_count-1].session + 1 );
nkeynes@167
   173
    if( last_session > 0 ) {
nkeynes@167
   174
	/* Boot track is the first data track of the last session, provided that it 
nkeynes@167
   175
	 * cannot be a single-session disc.
nkeynes@167
   176
	 */
nkeynes@167
   177
	int boot_track = -1;
nkeynes@167
   178
	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
nkeynes@167
   179
	    if( disc->track[i].flags & TRACK_DATA ) {
nkeynes@167
   180
		boot_track = i;
nkeynes@167
   181
	    }
nkeynes@167
   182
	}
nkeynes@167
   183
	if( boot_track != -1 ) {
nkeynes@167
   184
	    char boot_sector[2048];
nkeynes@167
   185
	    uint32_t length = sizeof(boot_sector);
nkeynes@167
   186
	    if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
nkeynes@167
   187
				    boot_sector, &length ) == PKT_ERR_OK ) {
nkeynes@167
   188
		bootstrap_dump(boot_sector, FALSE);
nkeynes@167
   189
		is_bootable = TRUE;
nkeynes@167
   190
	    }
nkeynes@167
   191
	}
nkeynes@138
   192
    }
nkeynes@167
   193
    if( !is_bootable )
nkeynes@167
   194
	WARN( "Disc does not appear to be bootable" );
nkeynes@138
   195
}
nkeynes@138
   196
nkeynes@149
   197
gdrom_error_t gdrom_get_toc( char *buf ) 
nkeynes@138
   198
{
nkeynes@142
   199
    struct gdrom_toc *toc = (struct gdrom_toc *)buf;
nkeynes@142
   200
    int i;
nkeynes@142
   201
nkeynes@138
   202
    if( gdrom_disc == NULL )
nkeynes@149
   203
	return PKT_ERR_NODISC;
nkeynes@142
   204
nkeynes@142
   205
    for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@142
   206
	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
nkeynes@142
   207
    }
nkeynes@142
   208
    toc->first = 0x0100 | gdrom_disc->track[0].flags;
nkeynes@142
   209
    toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
nkeynes@142
   210
    toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
nkeynes@142
   211
	gdrom_disc->track[i-1].flags;
nkeynes@142
   212
    for( ;i<99; i++ )
nkeynes@142
   213
	toc->track[i] = 0xFFFFFFFF;
nkeynes@149
   214
    return PKT_ERR_OK;
nkeynes@149
   215
}
nkeynes@149
   216
nkeynes@152
   217
gdrom_error_t gdrom_get_info( char *buf, int session )
nkeynes@149
   218
{
nkeynes@149
   219
    if( gdrom_disc == NULL )
nkeynes@149
   220
	return PKT_ERR_NODISC;
nkeynes@149
   221
    struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
nkeynes@149
   222
    unsigned int end_of_disc = last_track->lba + last_track->sector_count;
nkeynes@152
   223
    int i;
nkeynes@152
   224
    buf[0] = 0x01; /* Disc status? */
nkeynes@149
   225
    buf[1] = 0;
nkeynes@152
   226
nkeynes@152
   227
    if( session == 0 ) {
nkeynes@152
   228
	buf[2] = last_track->session+1; /* last session */
nkeynes@152
   229
	buf[3] = (end_of_disc >> 16) & 0xFF;
nkeynes@152
   230
	buf[4] = (end_of_disc >> 8) & 0xFF;
nkeynes@152
   231
	buf[5] = end_of_disc & 0xFF;
nkeynes@152
   232
	return PKT_ERR_OK;
nkeynes@152
   233
    } else {
nkeynes@152
   234
	session--;
nkeynes@152
   235
	for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@152
   236
	    if( gdrom_disc->track[i].session == session ) {
nkeynes@152
   237
		buf[2] = i+1; /* first track of session */
nkeynes@152
   238
		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
nkeynes@152
   239
		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
nkeynes@152
   240
		buf[5] = gdrom_disc->track[i].lba & 0xFF;
nkeynes@152
   241
		return PKT_ERR_OK;
nkeynes@152
   242
	    }
nkeynes@152
   243
	}
nkeynes@152
   244
	return PKT_ERR_BADFIELD; /* No such session */
nkeynes@152
   245
    }
nkeynes@152
   246
	
nkeynes@138
   247
}
nkeynes@138
   248
nkeynes@138
   249
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
   250
{
nkeynes@138
   251
    gdrom_unmount_disc();
nkeynes@138
   252
    gdrom_disc = disc;
nkeynes@138
   253
    idereg.disc = disc->disc_type | IDE_DISC_READY;
nkeynes@167
   254
    gdrom_dump_disc_info( disc );
nkeynes@138
   255
}
nkeynes@138
   256
nkeynes@138
   257
gdrom_disc_t gdrom_mount_image( const gchar *filename )
nkeynes@138
   258
{
nkeynes@138
   259
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@138
   260
    if( disc != NULL )
nkeynes@138
   261
	gdrom_mount_disc( disc );
nkeynes@138
   262
    return disc;
nkeynes@138
   263
}
nkeynes@138
   264
nkeynes@138
   265
void gdrom_unmount_disc( ) 
nkeynes@138
   266
{
nkeynes@138
   267
    if( gdrom_disc != NULL ) {
nkeynes@138
   268
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   269
    }
nkeynes@138
   270
    gdrom_disc = NULL;
nkeynes@138
   271
    idereg.disc = IDE_DISC_NONE;
nkeynes@138
   272
}
nkeynes@138
   273
nkeynes@138
   274
gboolean gdrom_is_mounted( void ) 
nkeynes@138
   275
{
nkeynes@138
   276
    return gdrom_disc != NULL;
nkeynes@138
   277
}
.