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