2 * $Id: gdimage.c,v 1.5 2007-10-31 11:53:35 nkeynes Exp $
4 * GD-Rom image-file common functions.
6 * Copyright (c) 2005 Nathan Keynes.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <netinet/in.h>
21 #include "gdrom/gdrom.h"
22 #include "gdrom/packet.h"
23 #include "bootstrap.h"
25 static void gdrom_image_destroy( gdrom_disc_t disc );
26 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode,
27 unsigned char *buf, uint32_t *readlength );
28 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
29 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
30 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
31 static int gdrom_image_drive_status( gdrom_disc_t disc );
35 * Initialize a gdrom_disc structure with the gdrom_image_* methods
37 void gdrom_image_init( gdrom_disc_t disc )
39 memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
40 disc->read_sector = gdrom_image_read_sector;
41 disc->read_toc = gdrom_image_read_toc;
42 disc->read_session = gdrom_image_read_session;
43 disc->read_position = gdrom_image_read_position;
44 disc->drive_status = gdrom_image_drive_status;
45 disc->play_audio = NULL; /* not supported yet */
46 disc->run_time_slice = NULL; /* not needed */
47 disc->close = gdrom_image_destroy;
50 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
52 gdrom_image_t image = (gdrom_image_t)calloc(sizeof(struct gdrom_image), 1);
56 image->disc_type = IDE_DISC_CDROM;
58 gdrom_disc_t disc = (gdrom_disc_t)image;
59 gdrom_image_init(disc);
60 if( filename == NULL ) {
63 disc->name = g_strdup(filename);
69 static void gdrom_image_destroy( gdrom_disc_t disc )
71 gdrom_image_t img = (gdrom_image_t)disc;
72 if( img->file != NULL ) {
76 if( disc->name != NULL ) {
77 g_free( (gpointer)disc->name );
83 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
85 gdrom_image_t img = (gdrom_image_t)disc;
86 if( img->file != NULL ) {
89 if( disc->name != NULL ) {
90 g_free( (gpointer)disc->name );
96 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
99 for( i=0; i<image->track_count; i++ ) {
100 if( image->track[i].lba <= lba &&
101 lba < (image->track[i].lba + image->track[i].sector_count) ) {
108 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
109 int mode, unsigned char *buf, uint32_t *length )
111 gdrom_image_t image = (gdrom_image_t)disc;
112 int file_offset, read_len, track_no;
114 track_no = gdrom_image_get_track_by_lba( image, lba );
115 if( track_no == -1 ) {
116 return PKT_ERR_BADREAD;
118 struct gdrom_track *track = &image->track[track_no-1];
119 file_offset = track->offset + track->sector_size * (lba - track->lba);
120 read_len = track->sector_size;
125 switch( track->mode ) {
127 case GDROM_MODE2_XA1:
128 fseek( image->file, file_offset, SEEK_SET );
129 fread( buf, track->sector_size, 1, image->file );
133 file_offset += 8; /* skip the subheader */
134 fseek( image->file, file_offset, SEEK_SET );
135 fread( buf, 2048, 1, image->file );
138 return PKT_ERR_BADREADMODE;
142 return PKT_ERR_BADREADMODE;
150 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf )
152 gdrom_image_t image = (gdrom_image_t)disc;
153 struct gdrom_toc *toc = (struct gdrom_toc *)buf;
156 for( i=0; i<image->track_count; i++ ) {
157 toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
159 toc->first = 0x0100 | image->track[0].flags;
160 toc->last = (image->track_count<<8) | image->track[i-1].flags;
161 toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
162 image->track[i-1].flags;
164 toc->track[i] = 0xFFFFFFFF;
168 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
170 gdrom_image_t image = (gdrom_image_t)disc;
171 struct gdrom_track *last_track = &image->track[image->track_count-1];
172 unsigned int end_of_disc = last_track->lba + last_track->sector_count;
174 buf[0] = 0x01; /* Disc status? */
178 buf[2] = last_track->session+1; /* last session */
179 buf[3] = (end_of_disc >> 16) & 0xFF;
180 buf[4] = (end_of_disc >> 8) & 0xFF;
181 buf[5] = end_of_disc & 0xFF;
185 for( i=0; i<image->track_count; i++ ) {
186 if( image->track[i].session == session ) {
187 buf[2] = i+1; /* first track of session */
188 buf[3] = (image->track[i].lba >> 16) & 0xFF;
189 buf[4] = (image->track[i].lba >> 8) & 0xFF;
190 buf[5] = image->track[i].lba & 0xFF;
194 return PKT_ERR_BADFIELD; /* No such session */
198 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
200 gdrom_image_t image = (gdrom_image_t)disc;
201 int track_no = gdrom_image_get_track_by_lba( image, lba );
202 if( track_no == -1 ) {
206 struct gdrom_track *track = &image->track[track_no-1];
207 uint32_t offset = lba - track->lba;
208 buf[4] = track->flags;
210 buf[6] = 0x01; /* ?? */
211 buf[7] = (offset >> 16) & 0xFF;
212 buf[8] = (offset >> 8) & 0xFF;
213 buf[9] = offset & 0xFF;
215 buf[11] = (lba >> 16) & 0xFF;
216 buf[12] = (lba >> 8) & 0xFF;
217 buf[13] = lba & 0xFF;
221 static int gdrom_image_drive_status( gdrom_disc_t disc )
223 gdrom_image_t image = (gdrom_image_t)disc;
224 return image->disc_type | IDE_DISC_READY;
227 void gdrom_image_dump_info( gdrom_disc_t d ) {
228 gdrom_image_t disc = (gdrom_image_t)d;
230 int last_session = disc->track[disc->track_count-1].session;
231 gboolean is_bootable = FALSE;
233 INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count,
234 disc->track[disc->track_count-1].session + 1 );
235 if( last_session > 0 ) {
236 /* Boot track is the first data track of the last session, provided that it
237 * cannot be a single-session disc.
240 for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
241 if( disc->track[i].flags & TRACK_DATA ) {
245 if( boot_track != -1 ) {
246 unsigned char boot_sector[MAX_SECTOR_SIZE];
247 uint32_t length = sizeof(boot_sector);
248 if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
249 boot_sector, &length ) == PKT_ERR_OK ) {
250 bootstrap_dump(boot_sector, FALSE);
256 WARN( "Disc does not appear to be bootable" );
.