Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 347:e686be8b10df
prev342:850502f0e8de
next422:61a0598e07ff
author nkeynes
date Sun Feb 04 11:30:41 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix handling of (some?) v3.0 cdi files
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@347
     2
 * $Id: cdi.c,v 1.6 2007-02-04 11:30:41 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@31
     4
 * CDI CD-image file support
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@1
    18
nkeynes@1
    19
#include <stdlib.h>
nkeynes@1
    20
#include <stdio.h>
nkeynes@1
    21
#include <stdint.h>
nkeynes@1
    22
#include <fcntl.h>
nkeynes@1
    23
#include <errno.h>
nkeynes@1
    24
#include <sys/stat.h>
nkeynes@168
    25
#include "gdrom/gdrom.h"
nkeynes@1
    26
nkeynes@168
    27
#define CDI_V2_ID 0x80000004
nkeynes@168
    28
#define CDI_V3_ID 0x80000005
nkeynes@168
    29
#define CDI_V35_ID 0x80000006
nkeynes@168
    30
nkeynes@168
    31
nkeynes@168
    32
static gboolean cdi_image_is_valid( FILE *f );
nkeynes@168
    33
static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
nkeynes@168
    34
nkeynes@168
    35
struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi", 
nkeynes@168
    36
					     cdi_image_is_valid, cdi_image_open };
nkeynes@1
    37
nkeynes@347
    38
static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
nkeynes@1
    39
                                       0,0,1,0,0,0,255,255,255,255 };
nkeynes@347
    40
static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
nkeynes@1
    41
nkeynes@1
    42
struct cdi_trailer {
nkeynes@1
    43
    uint32_t cdi_version;
nkeynes@1
    44
    uint32_t header_offset;
nkeynes@1
    45
};
nkeynes@1
    46
nkeynes@1
    47
struct cdi_track_data {
nkeynes@1
    48
    char unknown[0x19];
nkeynes@1
    49
    uint32_t pregap_length;
nkeynes@1
    50
    uint32_t length;
nkeynes@1
    51
    char unknown2[6];
nkeynes@1
    52
    uint32_t mode;
nkeynes@1
    53
    char unknown3[0x0c];
nkeynes@1
    54
    uint32_t start_lba;
nkeynes@1
    55
    uint32_t total_length;
nkeynes@1
    56
    char unknown4[0x10];
nkeynes@1
    57
    uint32_t sector_size;
nkeynes@1
    58
    char unknown5[0x1D];
nkeynes@1
    59
} __attribute__((packed));
nkeynes@1
    60
nkeynes@168
    61
gboolean cdi_image_is_valid( FILE *f )
nkeynes@1
    62
{
nkeynes@168
    63
    int len;
nkeynes@168
    64
    struct cdi_trailer trail;
nkeynes@1
    65
nkeynes@168
    66
    fseek( f, -8, SEEK_END );
nkeynes@168
    67
    len = ftell(f)+8;
nkeynes@168
    68
    fread( &trail, sizeof(trail), 1, f );
nkeynes@168
    69
    if( trail.header_offset >= len ||
nkeynes@168
    70
        trail.header_offset == 0 )
nkeynes@168
    71
	return FALSE;
nkeynes@168
    72
    return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID;
nkeynes@168
    73
}
nkeynes@168
    74
nkeynes@168
    75
gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
nkeynes@168
    76
{
nkeynes@168
    77
    gdrom_disc_t disc = NULL;
nkeynes@342
    78
    gdrom_image_t image;
nkeynes@1
    79
    int fd = -1, i,j, tmp;
nkeynes@168
    80
    uint16_t session_count;
nkeynes@168
    81
    uint16_t track_count;
nkeynes@168
    82
    int total_tracks = 0;
nkeynes@1
    83
    int posn = 0, hdr;
nkeynes@1
    84
    long len;
nkeynes@1
    85
    struct cdi_trailer trail;
nkeynes@1
    86
    uint32_t new_fmt;
nkeynes@1
    87
    char tmpc;
nkeynes@1
    88
    char marker[20];
nkeynes@1
    89
nkeynes@168
    90
    fseek( f, -8, SEEK_END );
nkeynes@168
    91
    len = ftell(f)+8;
nkeynes@168
    92
    fread( &trail, sizeof(trail), 1, f );
nkeynes@168
    93
    if( trail.header_offset >= len ||
nkeynes@1
    94
        trail.header_offset == 0 )
nkeynes@168
    95
	return NULL;
nkeynes@1
    96
nkeynes@168
    97
    if( trail.cdi_version == CDI_V2_ID ) trail.cdi_version = 2;
nkeynes@168
    98
    else if( trail.cdi_version == CDI_V3_ID ) trail.cdi_version = 3;
nkeynes@168
    99
    else return NULL; 
nkeynes@1
   100
nkeynes@168
   101
    fseek( f, trail.header_offset, SEEK_SET );
nkeynes@168
   102
    fread( &session_count, sizeof(session_count), 1, f );
nkeynes@1
   103
    
nkeynes@168
   104
    disc = gdrom_image_new(f);
nkeynes@342
   105
    if( disc == NULL ) {
nkeynes@342
   106
	fclose(f);
nkeynes@342
   107
	ERROR("Unable to allocate memory!");
nkeynes@342
   108
	return NULL;
nkeynes@342
   109
    }
nkeynes@342
   110
    image = (gdrom_image_t)disc;
nkeynes@1
   111
nkeynes@168
   112
    for( i=0; i< session_count; i++ ) {        
nkeynes@168
   113
	fread( &track_count, sizeof(track_count), 1, f );
nkeynes@347
   114
	if( track_count + total_tracks > 99 ) {
nkeynes@347
   115
	    ERROR( "Invalid number of tracks, bad cdi image\n" );
nkeynes@347
   116
	    disc->close(disc);
nkeynes@347
   117
	    return NULL;
nkeynes@347
   118
	}
nkeynes@168
   119
        for( j=0; j<track_count; j++ ) {
nkeynes@1
   120
            struct cdi_track_data trk;
nkeynes@168
   121
            uint32_t new_fmt = 0;
nkeynes@168
   122
	    uint8_t fnamelen = 0;
nkeynes@168
   123
            fread( &new_fmt, sizeof(new_fmt), 1, f );
nkeynes@1
   124
            if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
nkeynes@168
   125
		fseek( f, 8, SEEK_CUR ); /* Skip */
nkeynes@1
   126
            }
nkeynes@168
   127
            fread( marker, 20, 1, f );
nkeynes@347
   128
            if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
nkeynes@168
   129
		ERROR( "Track start marker not found, error reading cdi image\n" );
nkeynes@342
   130
		disc->close(disc);
nkeynes@168
   131
		return NULL;
nkeynes@168
   132
	    }
nkeynes@168
   133
	    fseek( f, 4, SEEK_CUR );
nkeynes@168
   134
            fread( &fnamelen, 1, 1, f );
nkeynes@168
   135
            fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
nkeynes@168
   136
            fread( &trk, sizeof(trk), 1, f );
nkeynes@342
   137
	    image->track[total_tracks].session = i;
nkeynes@342
   138
	    image->track[total_tracks].lba = trk.start_lba + 150;
nkeynes@342
   139
	    image->track[total_tracks].sector_count = trk.length;
nkeynes@168
   140
	    switch( trk.mode ) {
nkeynes@168
   141
	    case 0:
nkeynes@342
   142
		image->track[total_tracks].mode = GDROM_CDDA;
nkeynes@342
   143
		image->track[total_tracks].sector_size = 2352;
nkeynes@342
   144
		image->track[total_tracks].flags = 0x01;
nkeynes@168
   145
		if( trk.sector_size != 2 ) {
nkeynes@168
   146
		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
nkeynes@342
   147
		    disc->close(disc);
nkeynes@168
   148
		    return NULL;
nkeynes@168
   149
		}
nkeynes@168
   150
		break;
nkeynes@168
   151
	    case 1:
nkeynes@342
   152
		image->track[total_tracks].mode = GDROM_MODE1;
nkeynes@342
   153
		image->track[total_tracks].sector_size = 2048;
nkeynes@342
   154
		image->track[total_tracks].flags = 0x41;
nkeynes@168
   155
		if( trk.sector_size != 0 ) {
nkeynes@168
   156
		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
nkeynes@342
   157
		    disc->close(disc);
nkeynes@168
   158
		    return NULL;
nkeynes@168
   159
		}
nkeynes@168
   160
		break;
nkeynes@168
   161
	    case 2:
nkeynes@342
   162
		image->track[total_tracks].flags = 0x41;
nkeynes@168
   163
		switch( trk.sector_size ) {
nkeynes@168
   164
		case 0:
nkeynes@342
   165
		    image->track[total_tracks].mode = GDROM_MODE2_XA1;
nkeynes@342
   166
		    image->track[total_tracks].sector_size = 2048;
nkeynes@168
   167
		    break;
nkeynes@168
   168
		case 1:
nkeynes@342
   169
		    image->track[total_tracks].mode = GDROM_MODE2;
nkeynes@342
   170
		    image->track[total_tracks].sector_size = 2336;
nkeynes@168
   171
		    break;
nkeynes@168
   172
		case 2:
nkeynes@168
   173
		default:
nkeynes@168
   174
		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
nkeynes@342
   175
		    disc->close(disc);
nkeynes@168
   176
		    return NULL;
nkeynes@168
   177
		}
nkeynes@168
   178
		break;
nkeynes@168
   179
	    default:
nkeynes@168
   180
		ERROR( "Unsupported track mode %d", trk.mode );
nkeynes@342
   181
		disc->close(disc);
nkeynes@168
   182
		return NULL;
nkeynes@168
   183
	    }
nkeynes@342
   184
	    image->track[total_tracks].offset = posn + 
nkeynes@342
   185
		trk.pregap_length * image->track[total_tracks].sector_size ;
nkeynes@342
   186
	    posn += trk.total_length * image->track[total_tracks].sector_size;
nkeynes@168
   187
	    total_tracks++;
nkeynes@347
   188
	    fread( marker, 1, 9, f );
nkeynes@347
   189
	    if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
nkeynes@347
   190
		fseek( f, 91, SEEK_CUR );
nkeynes@168
   191
	    } else {
nkeynes@347
   192
		fseek( f, 3, SEEK_CUR );
nkeynes@168
   193
	    }
nkeynes@168
   194
	}
nkeynes@1
   195
    }
nkeynes@342
   196
    image->track_count = total_tracks;
nkeynes@342
   197
    image->filename = filename;
nkeynes@168
   198
    return disc;
nkeynes@1
   199
}
.