Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 237:6f1a429c9d12
prev168:203a72138e16
next245:a1d0655a88d3
author nkeynes
date Thu Dec 14 12:31:38 2006 +0000 (13 years ago)
permissions -rw-r--r--
last change Add preliminary linux native CD driver support
file annotate diff log raw
nkeynes@138
     1
/**
nkeynes@237
     2
 * $Id: gdrom.c,v 1.10 2006-12-14 12:31:38 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@152
   140
	switch( track->mode ) {
nkeynes@152
   141
	case GDROM_MODE1:
nkeynes@152
   142
	case GDROM_MODE2_XA1:
nkeynes@154
   143
	    fseek( disc->file, file_offset, SEEK_SET );
nkeynes@152
   144
	    fread( buf, track->sector_size, sector_count, disc->file );
nkeynes@152
   145
	    break;
nkeynes@152
   146
	case GDROM_MODE2:
nkeynes@152
   147
	    read_len = sector_count * 2048;
nkeynes@154
   148
	    file_offset += 8; /* skip the subheader */
nkeynes@152
   149
	    while( sector_count > 0 ) {
nkeynes@154
   150
		fseek( disc->file, file_offset, SEEK_SET );
nkeynes@152
   151
		fread( buf, 2048, 1, disc->file );
nkeynes@152
   152
		file_offset += track->sector_size;
nkeynes@152
   153
		buf += 2048;
nkeynes@152
   154
		sector_count--;
nkeynes@152
   155
	    }
nkeynes@152
   156
	    break;
nkeynes@152
   157
	default:
nkeynes@152
   158
	    return PKT_ERR_BADREADMODE;
nkeynes@152
   159
	}
nkeynes@152
   160
	break;
nkeynes@152
   161
    default:
nkeynes@143
   162
	return PKT_ERR_BADREADMODE;
nkeynes@152
   163
    }
nkeynes@152
   164
	    
nkeynes@143
   165
    *length = read_len;
nkeynes@143
   166
    return PKT_ERR_OK;
nkeynes@138
   167
}
nkeynes@138
   168
nkeynes@138
   169
uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
nkeynes@143
   170
			     int mode, char *buf, uint32_t *length )
nkeynes@138
   171
{
nkeynes@138
   172
    if( gdrom_disc == NULL )
nkeynes@143
   173
	return PKT_ERR_NODISC; /* No media */
nkeynes@143
   174
    return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, mode, buf, length );
nkeynes@138
   175
}
nkeynes@138
   176
nkeynes@138
   177
nkeynes@167
   178
void gdrom_dump_disc_info( gdrom_disc_t disc ) {
nkeynes@138
   179
    int i;
nkeynes@167
   180
    int last_session = disc->track[disc->track_count-1].session;
nkeynes@167
   181
    gboolean is_bootable = FALSE;
nkeynes@167
   182
nkeynes@138
   183
    INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
nkeynes@138
   184
	  disc->track[disc->track_count-1].session + 1 );
nkeynes@167
   185
    if( last_session > 0 ) {
nkeynes@167
   186
	/* Boot track is the first data track of the last session, provided that it 
nkeynes@167
   187
	 * cannot be a single-session disc.
nkeynes@167
   188
	 */
nkeynes@167
   189
	int boot_track = -1;
nkeynes@167
   190
	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
nkeynes@167
   191
	    if( disc->track[i].flags & TRACK_DATA ) {
nkeynes@167
   192
		boot_track = i;
nkeynes@167
   193
	    }
nkeynes@167
   194
	}
nkeynes@167
   195
	if( boot_track != -1 ) {
nkeynes@167
   196
	    char boot_sector[2048];
nkeynes@167
   197
	    uint32_t length = sizeof(boot_sector);
nkeynes@167
   198
	    if( disc->read_sectors( disc, disc->track[boot_track].lba, 1, GDROM_MODE1,
nkeynes@167
   199
				    boot_sector, &length ) == PKT_ERR_OK ) {
nkeynes@167
   200
		bootstrap_dump(boot_sector, FALSE);
nkeynes@167
   201
		is_bootable = TRUE;
nkeynes@167
   202
	    }
nkeynes@167
   203
	}
nkeynes@138
   204
    }
nkeynes@167
   205
    if( !is_bootable )
nkeynes@167
   206
	WARN( "Disc does not appear to be bootable" );
nkeynes@138
   207
}
nkeynes@138
   208
nkeynes@149
   209
gdrom_error_t gdrom_get_toc( char *buf ) 
nkeynes@138
   210
{
nkeynes@142
   211
    struct gdrom_toc *toc = (struct gdrom_toc *)buf;
nkeynes@142
   212
    int i;
nkeynes@142
   213
nkeynes@138
   214
    if( gdrom_disc == NULL )
nkeynes@149
   215
	return PKT_ERR_NODISC;
nkeynes@142
   216
nkeynes@142
   217
    for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@142
   218
	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
nkeynes@142
   219
    }
nkeynes@142
   220
    toc->first = 0x0100 | gdrom_disc->track[0].flags;
nkeynes@142
   221
    toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
nkeynes@142
   222
    toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
nkeynes@142
   223
	gdrom_disc->track[i-1].flags;
nkeynes@142
   224
    for( ;i<99; i++ )
nkeynes@142
   225
	toc->track[i] = 0xFFFFFFFF;
nkeynes@149
   226
    return PKT_ERR_OK;
nkeynes@149
   227
}
nkeynes@149
   228
nkeynes@152
   229
gdrom_error_t gdrom_get_info( char *buf, int session )
nkeynes@149
   230
{
nkeynes@149
   231
    if( gdrom_disc == NULL )
nkeynes@149
   232
	return PKT_ERR_NODISC;
nkeynes@149
   233
    struct gdrom_track *last_track = &gdrom_disc->track[gdrom_disc->track_count-1];
nkeynes@149
   234
    unsigned int end_of_disc = last_track->lba + last_track->sector_count;
nkeynes@152
   235
    int i;
nkeynes@152
   236
    buf[0] = 0x01; /* Disc status? */
nkeynes@149
   237
    buf[1] = 0;
nkeynes@152
   238
nkeynes@152
   239
    if( session == 0 ) {
nkeynes@152
   240
	buf[2] = last_track->session+1; /* last session */
nkeynes@152
   241
	buf[3] = (end_of_disc >> 16) & 0xFF;
nkeynes@152
   242
	buf[4] = (end_of_disc >> 8) & 0xFF;
nkeynes@152
   243
	buf[5] = end_of_disc & 0xFF;
nkeynes@152
   244
	return PKT_ERR_OK;
nkeynes@152
   245
    } else {
nkeynes@152
   246
	session--;
nkeynes@152
   247
	for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@152
   248
	    if( gdrom_disc->track[i].session == session ) {
nkeynes@152
   249
		buf[2] = i+1; /* first track of session */
nkeynes@152
   250
		buf[3] = (gdrom_disc->track[i].lba >> 16) & 0xFF;
nkeynes@152
   251
		buf[4] = (gdrom_disc->track[i].lba >> 8) & 0xFF;
nkeynes@152
   252
		buf[5] = gdrom_disc->track[i].lba & 0xFF;
nkeynes@152
   253
		return PKT_ERR_OK;
nkeynes@152
   254
	    }
nkeynes@152
   255
	}
nkeynes@152
   256
	return PKT_ERR_BADFIELD; /* No such session */
nkeynes@152
   257
    }
nkeynes@152
   258
	
nkeynes@138
   259
}
nkeynes@138
   260
nkeynes@138
   261
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
   262
{
nkeynes@138
   263
    gdrom_unmount_disc();
nkeynes@138
   264
    gdrom_disc = disc;
nkeynes@138
   265
    idereg.disc = disc->disc_type | IDE_DISC_READY;
nkeynes@167
   266
    gdrom_dump_disc_info( disc );
nkeynes@138
   267
}
nkeynes@138
   268
nkeynes@138
   269
gdrom_disc_t gdrom_mount_image( const gchar *filename )
nkeynes@138
   270
{
nkeynes@138
   271
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@138
   272
    if( disc != NULL )
nkeynes@138
   273
	gdrom_mount_disc( disc );
nkeynes@138
   274
    return disc;
nkeynes@138
   275
}
nkeynes@138
   276
nkeynes@138
   277
void gdrom_unmount_disc( ) 
nkeynes@138
   278
{
nkeynes@138
   279
    if( gdrom_disc != NULL ) {
nkeynes@138
   280
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   281
    }
nkeynes@138
   282
    gdrom_disc = NULL;
nkeynes@138
   283
    idereg.disc = IDE_DISC_NONE;
nkeynes@138
   284
}
nkeynes@138
   285
nkeynes@138
   286
gboolean gdrom_is_mounted( void ) 
nkeynes@138
   287
{
nkeynes@138
   288
    return gdrom_disc != NULL;
nkeynes@138
   289
}
.