Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 422:61a0598e07ff
prev342:850502f0e8de
next464:8e099fad42a6
author nkeynes
date Sat Oct 06 09:03:24 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix compilation warnings
file annotate diff log raw
nkeynes@342
     1
/**
nkeynes@422
     2
 * $Id: gdimage.c,v 1.2 2007-10-06 08:58:00 nkeynes Exp $
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@342
    33
nkeynes@342
    34
/**
nkeynes@342
    35
 * Initialize a gdrom_disc structure with the gdrom_image_* methods
nkeynes@342
    36
 */
nkeynes@342
    37
void gdrom_image_init( gdrom_disc_t disc )
nkeynes@342
    38
{
nkeynes@342
    39
    memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
nkeynes@342
    40
    disc->read_sector = gdrom_image_read_sector;
nkeynes@342
    41
    disc->read_toc = gdrom_image_read_toc;
nkeynes@342
    42
    disc->read_session = gdrom_image_read_session;
nkeynes@342
    43
    disc->read_position = gdrom_image_read_position;
nkeynes@342
    44
    disc->drive_status = gdrom_image_drive_status;
nkeynes@342
    45
    disc->play_audio = NULL; /* not supported yet */
nkeynes@342
    46
    disc->run_time_slice = NULL; /* not needed */
nkeynes@342
    47
    disc->close = gdrom_image_destroy;
nkeynes@342
    48
}
nkeynes@342
    49
nkeynes@342
    50
gdrom_disc_t gdrom_image_new( FILE *f )
nkeynes@342
    51
{
nkeynes@342
    52
    gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
nkeynes@342
    53
    if( image == NULL ) {
nkeynes@342
    54
	return NULL;
nkeynes@342
    55
    }
nkeynes@342
    56
    image->disc_type = IDE_DISC_CDROM;
nkeynes@342
    57
    image->file = f;
nkeynes@342
    58
nkeynes@342
    59
    gdrom_disc_t disc = (gdrom_disc_t)image;
nkeynes@342
    60
    gdrom_image_init(disc);
nkeynes@342
    61
    return disc;
nkeynes@342
    62
}
nkeynes@342
    63
nkeynes@342
    64
static void gdrom_image_destroy( gdrom_disc_t disc )
nkeynes@342
    65
{
nkeynes@342
    66
    gdrom_image_t img = (gdrom_image_t)disc;
nkeynes@342
    67
    if( img->file != NULL ) {
nkeynes@342
    68
	fclose(img->file);
nkeynes@342
    69
	img->file = NULL;
nkeynes@342
    70
    }
nkeynes@342
    71
    free( disc );
nkeynes@342
    72
}
nkeynes@342
    73
nkeynes@342
    74
static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
nkeynes@342
    75
{
nkeynes@342
    76
    int i;
nkeynes@342
    77
    for( i=0; i<image->track_count; i++ ) {
nkeynes@342
    78
	if( image->track[i].lba <= lba && 
nkeynes@342
    79
	    lba < (image->track[i].lba + image->track[i].sector_count) ) {
nkeynes@342
    80
	    return i+1;
nkeynes@342
    81
	}
nkeynes@342
    82
    }
nkeynes@342
    83
    return -1;
nkeynes@342
    84
}
nkeynes@342
    85
nkeynes@342
    86
static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
nkeynes@422
    87
					      int mode, unsigned char *buf, uint32_t *length )
nkeynes@342
    88
{
nkeynes@342
    89
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@422
    90
    int file_offset, read_len, track_no;
nkeynes@342
    91
nkeynes@342
    92
    track_no = gdrom_image_get_track_by_lba( image, lba );
nkeynes@342
    93
    if( track_no == -1 ) {
nkeynes@342
    94
	return PKT_ERR_BADREAD;
nkeynes@342
    95
    }
nkeynes@342
    96
    struct gdrom_track *track = &image->track[track_no-1];
nkeynes@342
    97
    file_offset = track->offset + track->sector_size * (lba - track->lba);
nkeynes@342
    98
    read_len = track->sector_size;
nkeynes@342
    99
nkeynes@342
   100
    switch( mode ) {
nkeynes@342
   101
    case 0x24:
nkeynes@342
   102
    case 0x28:
nkeynes@342
   103
	switch( track->mode ) {
nkeynes@342
   104
	case GDROM_MODE1:
nkeynes@342
   105
	case GDROM_MODE2_XA1:
nkeynes@342
   106
	    fseek( image->file, file_offset, SEEK_SET );
nkeynes@342
   107
	    fread( buf, track->sector_size, 1, image->file );
nkeynes@342
   108
	    break;
nkeynes@342
   109
	case GDROM_MODE2:
nkeynes@342
   110
	    read_len = 2048;
nkeynes@342
   111
	    file_offset += 8; /* skip the subheader */
nkeynes@342
   112
	    fseek( image->file, file_offset, SEEK_SET );
nkeynes@342
   113
	    fread( buf, 2048, 1, image->file );
nkeynes@342
   114
	    break;
nkeynes@342
   115
	default:
nkeynes@342
   116
	    return PKT_ERR_BADREADMODE;
nkeynes@342
   117
	}
nkeynes@342
   118
	break;
nkeynes@342
   119
    default:
nkeynes@342
   120
	return PKT_ERR_BADREADMODE;
nkeynes@342
   121
    }
nkeynes@342
   122
	    
nkeynes@342
   123
    *length = read_len;
nkeynes@342
   124
    return PKT_ERR_OK;
nkeynes@342
   125
nkeynes@342
   126
}
nkeynes@342
   127
nkeynes@422
   128
static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
nkeynes@342
   129
{
nkeynes@342
   130
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   131
    struct gdrom_toc *toc = (struct gdrom_toc *)buf;
nkeynes@342
   132
    int i;
nkeynes@342
   133
nkeynes@342
   134
    for( i=0; i<image->track_count; i++ ) {
nkeynes@342
   135
	toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
nkeynes@342
   136
    }
nkeynes@342
   137
    toc->first = 0x0100 | image->track[0].flags;
nkeynes@342
   138
    toc->last = (image->track_count<<8) | image->track[i-1].flags;
nkeynes@342
   139
    toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
nkeynes@342
   140
	image->track[i-1].flags;
nkeynes@342
   141
    for( ;i<99; i++ )
nkeynes@342
   142
	toc->track[i] = 0xFFFFFFFF;
nkeynes@342
   143
    return PKT_ERR_OK;
nkeynes@342
   144
}
nkeynes@342
   145
nkeynes@422
   146
static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
nkeynes@342
   147
{
nkeynes@342
   148
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   149
    struct gdrom_track *last_track = &image->track[image->track_count-1];
nkeynes@342
   150
    unsigned int end_of_disc = last_track->lba + last_track->sector_count;
nkeynes@342
   151
    int i;
nkeynes@342
   152
    buf[0] = 0x01; /* Disc status? */
nkeynes@342
   153
    buf[1] = 0;
nkeynes@342
   154
nkeynes@342
   155
    if( session == 0 ) {
nkeynes@342
   156
	buf[2] = last_track->session+1; /* last session */
nkeynes@342
   157
	buf[3] = (end_of_disc >> 16) & 0xFF;
nkeynes@342
   158
	buf[4] = (end_of_disc >> 8) & 0xFF;
nkeynes@342
   159
	buf[5] = end_of_disc & 0xFF;
nkeynes@342
   160
	return PKT_ERR_OK;
nkeynes@342
   161
    } else {
nkeynes@342
   162
	session--;
nkeynes@342
   163
	for( i=0; i<image->track_count; i++ ) {
nkeynes@342
   164
	    if( image->track[i].session == session ) {
nkeynes@342
   165
		buf[2] = i+1; /* first track of session */
nkeynes@342
   166
		buf[3] = (image->track[i].lba >> 16) & 0xFF;
nkeynes@342
   167
		buf[4] = (image->track[i].lba >> 8) & 0xFF;
nkeynes@342
   168
		buf[5] = image->track[i].lba & 0xFF;
nkeynes@342
   169
		return PKT_ERR_OK;
nkeynes@342
   170
	    }
nkeynes@342
   171
	}
nkeynes@342
   172
	return PKT_ERR_BADFIELD; /* No such session */
nkeynes@342
   173
    }
nkeynes@342
   174
}
nkeynes@342
   175
nkeynes@422
   176
static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
nkeynes@342
   177
{
nkeynes@342
   178
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   179
    int track_no = gdrom_image_get_track_by_lba( image, lba );
nkeynes@342
   180
    if( track_no == -1 ) {
nkeynes@342
   181
	track_no = 1;
nkeynes@342
   182
	lba = 150;
nkeynes@342
   183
    }
nkeynes@342
   184
    struct gdrom_track *track = &image->track[track_no-1];
nkeynes@342
   185
    uint32_t offset = lba - track->lba;
nkeynes@342
   186
    buf[4] = track->flags;
nkeynes@342
   187
    buf[5] = track_no;
nkeynes@342
   188
    buf[6] = 0x01; /* ?? */
nkeynes@342
   189
    buf[7] = (offset >> 16) & 0xFF;
nkeynes@342
   190
    buf[8] = (offset >> 8) & 0xFF;
nkeynes@342
   191
    buf[9] = offset & 0xFF;
nkeynes@342
   192
    buf[10] = 0;
nkeynes@342
   193
    buf[11] = (lba >> 16) & 0xFF;
nkeynes@342
   194
    buf[12] = (lba >> 8) & 0xFF;
nkeynes@342
   195
    buf[13] = lba & 0xFF;
nkeynes@422
   196
    return PKT_ERR_OK;
nkeynes@342
   197
}
nkeynes@342
   198
nkeynes@342
   199
static int gdrom_image_drive_status( gdrom_disc_t disc ) 
nkeynes@342
   200
{
nkeynes@342
   201
    gdrom_image_t image = (gdrom_image_t)disc;
nkeynes@342
   202
    return image->disc_type | IDE_DISC_READY;
nkeynes@342
   203
}
nkeynes@342
   204
nkeynes@342
   205
void gdrom_image_dump_info( gdrom_disc_t d ) {
nkeynes@342
   206
    gdrom_image_t disc = (gdrom_image_t)d;
nkeynes@342
   207
    int i;
nkeynes@342
   208
    int last_session = disc->track[disc->track_count-1].session;
nkeynes@342
   209
    gboolean is_bootable = FALSE;
nkeynes@342
   210
nkeynes@342
   211
    INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
nkeynes@342
   212
	  disc->track[disc->track_count-1].session + 1 );
nkeynes@342
   213
    if( last_session > 0 ) {
nkeynes@342
   214
	/* Boot track is the first data track of the last session, provided that it 
nkeynes@342
   215
	 * cannot be a single-session disc.
nkeynes@342
   216
	 */
nkeynes@342
   217
	int boot_track = -1;
nkeynes@342
   218
	for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
nkeynes@342
   219
	    if( disc->track[i].flags & TRACK_DATA ) {
nkeynes@342
   220
		boot_track = i;
nkeynes@342
   221
	    }
nkeynes@342
   222
	}
nkeynes@342
   223
	if( boot_track != -1 ) {
nkeynes@422
   224
	    unsigned char boot_sector[MAX_SECTOR_SIZE];
nkeynes@342
   225
	    uint32_t length = sizeof(boot_sector);
nkeynes@342
   226
	    if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
nkeynes@342
   227
				   boot_sector, &length ) == PKT_ERR_OK ) {
nkeynes@342
   228
		bootstrap_dump(boot_sector, FALSE);
nkeynes@342
   229
		is_bootable = TRUE;
nkeynes@342
   230
	    }
nkeynes@342
   231
	}
nkeynes@342
   232
    }
nkeynes@342
   233
    if( !is_bootable ) {
nkeynes@342
   234
	WARN( "Disc does not appear to be bootable" );
nkeynes@342
   235
    }
nkeynes@342
   236
}
nkeynes@342
   237
.