Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 640:01b50fd48035
prev561:533f6b478071
next644:ccae4bfa5f82
author nkeynes
date Thu Feb 21 06:12:00 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Quick fix to get dclinux.nrg running. Real fix to follow later
file annotate diff log raw
nkeynes@342
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@342
     3
 *
nkeynes@342
     4
 * GD-Rom image-file common functions. 
nkeynes@342
     5
 *
nkeynes@342
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@342
     7
 *
nkeynes@342
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@342
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@342
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@342
    11
 * (at your option) any later version.
nkeynes@342
    12
 *
nkeynes@342
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@342
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@342
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@342
    16
 * GNU General Public License for more details.
nkeynes@342
    17
 */
nkeynes@342
    18
nkeynes@422
    19
#include <netinet/in.h>
nkeynes@422
    20
nkeynes@342
    21
#include "gdrom/gdrom.h"
nkeynes@342
    22
#include "gdrom/packet.h"
nkeynes@422
    23
#include "bootstrap.h"
nkeynes@342
    24
nkeynes@342
    25
static void gdrom_image_destroy( gdrom_disc_t disc );
nkeynes@342
    26
static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode, 
nkeynes@422
    27
					      unsigned char *buf, uint32_t *readlength );
nkeynes@422
    28
static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
nkeynes@422
    29
static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
nkeynes@422
    30
static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
nkeynes@342
    31
static int gdrom_image_drive_status( gdrom_disc_t disc );
nkeynes@342
    32
nkeynes@498
    33
struct cdrom_sector_header {
nkeynes@498
    34
    uint8_t sync[12];
nkeynes@498
    35
    uint8_t msf[3];
nkeynes@498
    36
    uint8_t mode;
nkeynes@498
    37
};
nkeynes@342
    38
nkeynes@342
    39
/**
nkeynes@342
    40
 * Initialize a gdrom_disc structure with the gdrom_image_* methods
nkeynes@342
    41
 */
nkeynes@342
    42
void gdrom_image_init( gdrom_disc_t disc )
nkeynes@342
    43
{
nkeynes@342
    44
    memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
nkeynes@342
    45
    disc->read_sector = gdrom_image_read_sector;
nkeynes@342
    46
    disc->read_toc = gdrom_image_read_toc;
nkeynes@342
    47
    disc->read_session = gdrom_image_read_session;
nkeynes@342
    48
    disc->read_position = gdrom_image_read_position;
nkeynes@342
    49
    disc->drive_status = gdrom_image_drive_status;
nkeynes@342
    50
    disc->play_audio = NULL; /* not supported yet */
nkeynes@342
    51
    disc->run_time_slice = NULL; /* not needed */
nkeynes@342
    52
    disc->close = gdrom_image_destroy;
nkeynes@342
    53
}
nkeynes@342
    54
nkeynes@464
    55
gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
nkeynes@342
    56
{
nkeynes@342
    57
    gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
nkeynes@342
    58
    if( image == NULL ) {
nkeynes@342
    59
	return NULL;
nkeynes@342
    60
    }
nkeynes@342
    61
    image->disc_type = IDE_DISC_CDROM;
nkeynes@342
    62
    image->file = f;
nkeynes@342
    63
    gdrom_disc_t disc = (gdrom_disc_t)image;
nkeynes@342
    64
    gdrom_image_init(disc);
nkeynes@464
    65
    if( filename == NULL ) {
nkeynes@464
    66
	disc->name = NULL;
nkeynes@464
    67
    } else {
nkeynes@464
    68
	disc->name = g_strdup(filename);
nkeynes@464
    69
    }
nkeynes@464
    70
nkeynes@342
    71
    return disc;
nkeynes@342
    72
}
nkeynes@342
    73
nkeynes@342
    74
static void gdrom_image_destroy( gdrom_disc_t disc )
nkeynes@342
    75
{
nkeynes@492
    76
    int i;
nkeynes@492
    77
    FILE *lastfile = NULL;
nkeynes@342
    78
    gdrom_image_t img = (gdrom_image_t)disc;
nkeynes@342
    79
    if( img->file != NULL ) {
nkeynes@342
    80
	fclose(img->file);
nkeynes@342
    81
	img->file = NULL;
nkeynes@342
    82
    }
nkeynes@492
    83
    for( i=0; i<img->track_count; i++ ) {
nkeynes@492
    84
	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
nkeynes@492
    85
	    lastfile = img->track[i].file;
nkeynes@492
    86
	    fclose(lastfile);
nkeynes@492
    87
	    img->track[i].file = NULL;
nkeynes@492
    88
	}
nkeynes@492
    89
    }
nkeynes@464
    90
    if( disc->name != NULL ) {
nkeynes@480
    91
	g_free( (gpointer)disc->name );
nkeynes@464
    92
	disc->name = NULL;
nkeynes@464
    93
    }
nkeynes@342
    94
    free( disc );
nkeynes@342
    95
}
nkeynes@342
    96
nkeynes@468
    97
void gdrom_image_destroy_no_close( gdrom_disc_t disc )
nkeynes@468
    98
{
nkeynes@492
    99
    int i;
nkeynes@492
   100
    FILE *lastfile = NULL;
nkeynes@468
   101
    gdrom_image_t img = (gdrom_image_t)disc;
nkeynes@468
   102
    if( img->file != NULL ) {
nkeynes@468
   103
	img->file = NULL;
nkeynes@468
   104
    }
nkeynes@492
   105
    for( i=0; i<img->track_count; i++ ) {
nkeynes@492
   106
	if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
nkeynes@492
   107
	    lastfile = img->track[i].file;
nkeynes@492
   108
	    fclose(lastfile);
nkeynes@492
   109
	    img->track[i].file = NULL;
nkeynes@492
   110
	}
nkeynes@492
   111
    }
nkeynes@468
   112
    if( disc->name != NULL ) {
nkeynes@480
   113
	g_free( (gpointer)disc->name );
nkeynes@468
   114
	disc->name = NULL;
nkeynes@468
   115
    }
nkeynes@468
   116
    free( disc );
nkeynes@468
   117
}
nkeynes@468
   118
nkeynes@342
   119
static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
nkeynes@342
   120
{
nkeynes@342
   121
    int i;
nkeynes@342
   122
    for( i=0; i<image->track_count; i++ ) {
nkeynes@342
   123
	if( image->track[i].lba <= lba && 
nkeynes@342
   124
	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
nkeynes@342
   125
	    return i+1;
nkeynes@342
   126
	}
nkeynes@342
   127
    }
nkeynes@342
   128
    return -1;
nkeynes@342
   129
}
nkeynes@342
   130
nkeynes@492
   131
/**
nkeynes@492
   132
 * Read a block from an image file, handling negative file offsets
nkeynes@492
   133
 * with 0-fill.
nkeynes@492
   134
 */
nkeynes@498
   135
static void gdrom_read_block( unsigned char *buf, int file_offset, int length, FILE *f )
nkeynes@492
   136
{
nkeynes@492
   137
    if( file_offset < 0 ) {
nkeynes@492
   138
	int size = -file_offset;
nkeynes@492
   139
	if( size >= length ) {
nkeynes@492
   140
	    memset( buf, 0, length );
nkeynes@492
   141
	    return;
nkeynes@492
   142
	} else {
nkeynes@492
   143
	    memset( buf, 0, size );
nkeynes@492
   144
	    file_offset = 0;
nkeynes@492
   145
	    length -= size;
nkeynes@492
   146
	}
nkeynes@492
   147
    }
nkeynes@492
   148
    fseek( f, file_offset, SEEK_SET );
nkeynes@492
   149
    fread( buf, length, 1, f );
nkeynes@492
   150
}
nkeynes@492
   151
nkeynes@342
   152
static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
nkeynes@422
   153
					      int mode, unsigned char *buf, uint32_t *length )
nkeynes@342
   154
{
nkeynes@342
   155
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@498
   156
    struct cdrom_sector_header secthead;
nkeynes@422
   157
    int file_offset, read_len, track_no;
nkeynes@498
   158
nkeynes@492
   159
    FILE *f;
nkeynes@342
   160
nkeynes@342
   161
    track_no = gdrom_image_get_track_by_lba( image, lba );
nkeynes@342
   162
    if( track_no == -1 ) {
nkeynes@342
   163
	return PKT_ERR_BADREAD;
nkeynes@342
   164
    }
nkeynes@342
   165
    struct gdrom_track *track = &image->track[track_no-1];
nkeynes@342
   166
    file_offset = track->offset + track->sector_size * (lba - track->lba);
nkeynes@342
   167
    read_len = track->sector_size;
nkeynes@492
   168
    if( track->file != NULL ) {
nkeynes@492
   169
	f = track->file;
nkeynes@492
   170
    } else {
nkeynes@492
   171
	f = image->file;
nkeynes@492
   172
    }
nkeynes@492
   173
nkeynes@492
   174
    
nkeynes@342
   175
nkeynes@342
   176
    switch( mode ) {
nkeynes@640
   177
    case 0x20: /* Actually, read anything, but for now... */
nkeynes@342
   178
    case 0x24:
nkeynes@342
   179
    case 0x28:
nkeynes@498
   180
	read_len = 2048;
nkeynes@342
   181
	switch( track->mode ) {
nkeynes@342
   182
	case GDROM_MODE1:
nkeynes@342
   183
	case GDROM_MODE2_XA1:
nkeynes@492
   184
	    gdrom_read_block( buf, file_offset, track->sector_size, f );
nkeynes@342
   185
	    break;
nkeynes@342
   186
	case GDROM_MODE2:
nkeynes@342
   187
	    file_offset += 8; /* skip the subheader */
nkeynes@492
   188
	    gdrom_read_block( buf, file_offset, 2048, f );
nkeynes@342
   189
	    break;
nkeynes@498
   190
	case GDROM_RAW:
nkeynes@498
   191
	    gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
nkeynes@498
   192
	    switch( secthead.mode ) {
nkeynes@498
   193
	    case 1:
nkeynes@498
   194
		file_offset += 16;
nkeynes@498
   195
		break;
nkeynes@498
   196
	    case 2:
nkeynes@498
   197
		file_offset += 24;
nkeynes@498
   198
		break;
nkeynes@498
   199
	    default:
nkeynes@498
   200
		return PKT_ERR_BADREADMODE;
nkeynes@498
   201
	    }
nkeynes@498
   202
	    gdrom_read_block( buf, file_offset, 2048, f );
nkeynes@498
   203
	    break;
nkeynes@342
   204
	default:
nkeynes@342
   205
	    return PKT_ERR_BADREADMODE;
nkeynes@342
   206
	}
nkeynes@342
   207
	break;
nkeynes@342
   208
    default:
nkeynes@342
   209
	return PKT_ERR_BADREADMODE;
nkeynes@342
   210
    }
nkeynes@342
   211
	    
nkeynes@342
   212
    *length = read_len;
nkeynes@342
   213
    return PKT_ERR_OK;
nkeynes@342
   214
nkeynes@342
   215
}
nkeynes@342
   216
nkeynes@422
   217
static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
nkeynes@342
   218
{
nkeynes@342
   219
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   220
    struct gdrom_toc *toc = (struct gdrom_toc *)buf;
nkeynes@342
   221
    int i;
nkeynes@342
   222
nkeynes@342
   223
    for( i=0; i<image->track_count; i++ ) {
nkeynes@342
   224
	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
nkeynes@342
   225
    }
nkeynes@342
   226
    toc->first = 0x0100 | image->track[0].flags;
nkeynes@342
   227
    toc->last = (image->track_count<<8) | image->track[i-1].flags;
nkeynes@342
   228
    toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
nkeynes@342
   229
	image->track[i-1].flags;
nkeynes@342
   230
    for( ;i<99; i++ )
nkeynes@342
   231
	toc->track[i] = 0xFFFFFFFF;
nkeynes@342
   232
    return PKT_ERR_OK;
nkeynes@342
   233
}
nkeynes@342
   234
nkeynes@422
   235
static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
nkeynes@342
   236
{
nkeynes@342
   237
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   238
    struct gdrom_track *last_track = &image->track[image->track_count-1];
nkeynes@342
   239
    unsigned int end_of_disc = last_track->lba + last_track->sector_count;
nkeynes@342
   240
    int i;
nkeynes@342
   241
    buf[0] = 0x01; /* Disc status? */
nkeynes@342
   242
    buf[1] = 0;
nkeynes@342
   243
nkeynes@342
   244
    if( session == 0 ) {
nkeynes@342
   245
	buf[2] = last_track->session+1; /* last session */
nkeynes@342
   246
	buf[3] = (end_of_disc >> 16) & 0xFF;
nkeynes@342
   247
	buf[4] = (end_of_disc >> 8) & 0xFF;
nkeynes@342
   248
	buf[5] = end_of_disc & 0xFF;
nkeynes@342
   249
	return PKT_ERR_OK;
nkeynes@342
   250
    } else {
nkeynes@342
   251
	session--;
nkeynes@342
   252
	for( i=0; i<image->track_count; i++ ) {
nkeynes@342
   253
	    if( image->track[i].session == session ) {
nkeynes@342
   254
		buf[2] = i+1; /* first track of session */
nkeynes@342
   255
		buf[3] = (image->track[i].lba >> 16) & 0xFF;
nkeynes@342
   256
		buf[4] = (image->track[i].lba >> 8) & 0xFF;
nkeynes@342
   257
		buf[5] = image->track[i].lba & 0xFF;
nkeynes@342
   258
		return PKT_ERR_OK;
nkeynes@342
   259
	    }
nkeynes@342
   260
	}
nkeynes@342
   261
	return PKT_ERR_BADFIELD; /* No such session */
nkeynes@342
   262
    }
nkeynes@342
   263
}
nkeynes@342
   264
nkeynes@422
   265
static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
nkeynes@342
   266
{
nkeynes@342
   267
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   268
    int track_no = gdrom_image_get_track_by_lba( image, lba );
nkeynes@342
   269
    if( track_no == -1 ) {
nkeynes@342
   270
	track_no = 1;
nkeynes@342
   271
	lba = 150;
nkeynes@342
   272
    }
nkeynes@342
   273
    struct gdrom_track *track = &image->track[track_no-1];
nkeynes@342
   274
    uint32_t offset = lba - track->lba;
nkeynes@342
   275
    buf[4] = track->flags;
nkeynes@342
   276
    buf[5] = track_no;
nkeynes@342
   277
    buf[6] = 0x01; /* ?? */
nkeynes@342
   278
    buf[7] = (offset >> 16) & 0xFF;
nkeynes@342
   279
    buf[8] = (offset >> 8) & 0xFF;
nkeynes@342
   280
    buf[9] = offset & 0xFF;
nkeynes@342
   281
    buf[10] = 0;
nkeynes@342
   282
    buf[11] = (lba >> 16) & 0xFF;
nkeynes@342
   283
    buf[12] = (lba >> 8) & 0xFF;
nkeynes@342
   284
    buf[13] = lba & 0xFF;
nkeynes@422
   285
    return PKT_ERR_OK;
nkeynes@342
   286
}
nkeynes@342
   287
nkeynes@342
   288
static int gdrom_image_drive_status( gdrom_disc_t disc ) 
nkeynes@342
   289
{
nkeynes@342
   290
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   291
    return image->disc_type | IDE_DISC_READY;
nkeynes@342
   292
}
nkeynes@342
   293
nkeynes@342
   294
void gdrom_image_dump_info( gdrom_disc_t d ) {
nkeynes@342
   295
    gdrom_image_t disc = (gdrom_image_t)d;
nkeynes@342
   296
    int i;
nkeynes@342
   297
    int last_session = disc->track[disc->track_count-1].session;
nkeynes@342
   298
    gboolean is_bootable = FALSE;
nkeynes@342
   299
nkeynes@342
   300
    INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
nkeynes@342
   301
	  disc->track[disc->track_count-1].session + 1 );
nkeynes@342
   302
    if( last_session > 0 ) {
nkeynes@342
   303
	/* Boot track is the first data track of the last session, provided that it 
nkeynes@342
   304
	 * cannot be a single-session disc.
nkeynes@342
   305
	 */
nkeynes@342
   306
	int boot_track = -1;
nkeynes@342
   307
	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
nkeynes@342
   308
	    if( disc->track[i].flags & TRACK_DATA ) {
nkeynes@342
   309
		boot_track = i;
nkeynes@342
   310
	    }
nkeynes@342
   311
	}
nkeynes@342
   312
	if( boot_track != -1 ) {
nkeynes@422
   313
	    unsigned char boot_sector[MAX_SECTOR_SIZE];
nkeynes@342
   314
	    uint32_t length = sizeof(boot_sector);
nkeynes@342
   315
	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
nkeynes@342
   316
				   boot_sector, &length ) == PKT_ERR_OK ) {
nkeynes@342
   317
		bootstrap_dump(boot_sector, FALSE);
nkeynes@342
   318
		is_bootable = TRUE;
nkeynes@342
   319
	    }
nkeynes@342
   320
	}
nkeynes@342
   321
    }
nkeynes@342
   322
    if( !is_bootable ) {
nkeynes@342
   323
	WARN( "Disc does not appear to be bootable" );
nkeynes@342
   324
    }
nkeynes@342
   325
}
nkeynes@342
   326
.