Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 759:f16975739abc
prev736:a02d1475ccfd
next999:3d19e3597d9b
author nkeynes
date Mon Jul 21 01:01:39 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Fix batch of -Wall warnings
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@561
     2
 * $Id$
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@759
    22
#include <string.h>
nkeynes@1
    23
#include <fcntl.h>
nkeynes@1
    24
#include <errno.h>
nkeynes@1
    25
#include <sys/stat.h>
nkeynes@678
    26
#include "gdrom/gddriver.h"
nkeynes@1
    27
nkeynes@168
    28
#define CDI_V2_ID 0x80000004
nkeynes@168
    29
#define CDI_V3_ID 0x80000005
nkeynes@168
    30
#define CDI_V35_ID 0x80000006
nkeynes@168
    31
nkeynes@168
    32
nkeynes@168
    33
static gboolean cdi_image_is_valid( FILE *f );
nkeynes@168
    34
static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
nkeynes@168
    35
nkeynes@168
    36
struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi", 
nkeynes@736
    37
        cdi_image_is_valid, cdi_image_open };
nkeynes@1
    38
nkeynes@347
    39
static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
nkeynes@736
    40
        0,0,1,0,0,0,255,255,255,255 };
nkeynes@347
    41
static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
nkeynes@1
    42
nkeynes@1
    43
struct cdi_trailer {
nkeynes@1
    44
    uint32_t cdi_version;
nkeynes@1
    45
    uint32_t header_offset;
nkeynes@1
    46
};
nkeynes@1
    47
nkeynes@1
    48
struct cdi_track_data {
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@736
    70
            trail.header_offset == 0 )
nkeynes@736
    71
        return FALSE;
nkeynes@453
    72
    return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID ||
nkeynes@736
    73
    trail.cdi_version == CDI_V35_ID;
nkeynes@168
    74
}
nkeynes@168
    75
nkeynes@168
    76
gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
nkeynes@168
    77
{
nkeynes@168
    78
    gdrom_disc_t disc = NULL;
nkeynes@342
    79
    gdrom_image_t image;
nkeynes@422
    80
    int i,j;
nkeynes@168
    81
    uint16_t session_count;
nkeynes@168
    82
    uint16_t track_count;
nkeynes@168
    83
    int total_tracks = 0;
nkeynes@422
    84
    int posn = 0;
nkeynes@1
    85
    long len;
nkeynes@1
    86
    struct cdi_trailer trail;
nkeynes@1
    87
    char marker[20];
nkeynes@1
    88
nkeynes@168
    89
    fseek( f, -8, SEEK_END );
nkeynes@168
    90
    len = ftell(f)+8;
nkeynes@168
    91
    fread( &trail, sizeof(trail), 1, f );
nkeynes@168
    92
    if( trail.header_offset >= len ||
nkeynes@736
    93
            trail.header_offset == 0 )
nkeynes@736
    94
        return NULL;
nkeynes@736
    95
nkeynes@453
    96
    if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
nkeynes@736
    97
            trail.cdi_version != CDI_V35_ID ) {
nkeynes@736
    98
        return NULL;
nkeynes@453
    99
    }
nkeynes@1
   100
nkeynes@453
   101
    if( trail.cdi_version == CDI_V35_ID ) {
nkeynes@736
   102
        fseek( f, -trail.header_offset, SEEK_END );
nkeynes@453
   103
    } else {
nkeynes@736
   104
        fseek( f, trail.header_offset, SEEK_SET );
nkeynes@453
   105
    }
nkeynes@168
   106
    fread( &session_count, sizeof(session_count), 1, f );
nkeynes@736
   107
nkeynes@464
   108
    disc = gdrom_image_new(filename, f);
nkeynes@342
   109
    if( disc == NULL ) {
nkeynes@736
   110
        ERROR("Unable to allocate memory!");
nkeynes@736
   111
        return NULL;
nkeynes@342
   112
    }
nkeynes@342
   113
    image = (gdrom_image_t)disc;
nkeynes@1
   114
nkeynes@168
   115
    for( i=0; i< session_count; i++ ) {        
nkeynes@736
   116
        fread( &track_count, sizeof(track_count), 1, f );
nkeynes@736
   117
        if( track_count + total_tracks > 99 ) {
nkeynes@736
   118
            ERROR( "Invalid number of tracks, bad cdi image\n" );
nkeynes@736
   119
            gdrom_image_destroy_no_close(disc);
nkeynes@736
   120
            return NULL;
nkeynes@736
   121
        }
nkeynes@168
   122
        for( j=0; j<track_count; j++ ) {
nkeynes@1
   123
            struct cdi_track_data trk;
nkeynes@168
   124
            uint32_t new_fmt = 0;
nkeynes@736
   125
            uint8_t fnamelen = 0;
nkeynes@168
   126
            fread( &new_fmt, sizeof(new_fmt), 1, f );
nkeynes@1
   127
            if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
nkeynes@736
   128
                fseek( f, 8, SEEK_CUR ); /* Skip */
nkeynes@1
   129
            }
nkeynes@168
   130
            fread( marker, 20, 1, f );
nkeynes@347
   131
            if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
nkeynes@736
   132
                ERROR( "Track start marker not found, error reading cdi image\n" );
nkeynes@736
   133
                gdrom_image_destroy_no_close(disc);
nkeynes@736
   134
                return NULL;
nkeynes@736
   135
            }
nkeynes@736
   136
            fseek( f, 4, SEEK_CUR );
nkeynes@168
   137
            fread( &fnamelen, 1, 1, f );
nkeynes@168
   138
            fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
nkeynes@736
   139
            fseek( f, 19, SEEK_CUR );
nkeynes@736
   140
            fread( &new_fmt, sizeof(new_fmt), 1, f );
nkeynes@736
   141
            if( new_fmt == 0x80000000 ) {
nkeynes@736
   142
                fseek( f, 10, SEEK_CUR );
nkeynes@736
   143
            } else {
nkeynes@736
   144
                fseek( f, 2, SEEK_CUR );
nkeynes@736
   145
            }
nkeynes@168
   146
            fread( &trk, sizeof(trk), 1, f );
nkeynes@736
   147
            image->track[total_tracks].session = i;
nkeynes@736
   148
            image->track[total_tracks].lba = trk.start_lba + 150;
nkeynes@736
   149
            image->track[total_tracks].sector_count = trk.length;
nkeynes@736
   150
            switch( trk.mode ) {
nkeynes@736
   151
            case 0:
nkeynes@736
   152
                image->track[total_tracks].mode = GDROM_CDDA;
nkeynes@736
   153
                image->track[total_tracks].sector_size = 2352;
nkeynes@736
   154
                image->track[total_tracks].flags = 0x01;
nkeynes@736
   155
                if( trk.sector_size != 2 ) {
nkeynes@736
   156
                    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
nkeynes@736
   157
                    gdrom_image_destroy_no_close(disc);
nkeynes@736
   158
                    return NULL;
nkeynes@736
   159
                }
nkeynes@736
   160
                break;
nkeynes@736
   161
            case 1:
nkeynes@736
   162
                image->track[total_tracks].mode = GDROM_MODE1;
nkeynes@736
   163
                image->track[total_tracks].sector_size = 2048;
nkeynes@736
   164
                image->track[total_tracks].flags = 0x41;
nkeynes@736
   165
                if( trk.sector_size != 0 ) {
nkeynes@736
   166
                    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
nkeynes@736
   167
                    gdrom_image_destroy_no_close(disc);
nkeynes@736
   168
                    return NULL;
nkeynes@736
   169
                }
nkeynes@736
   170
                break;
nkeynes@736
   171
            case 2:
nkeynes@736
   172
                image->track[total_tracks].flags = 0x41;
nkeynes@736
   173
                switch( trk.sector_size ) {
nkeynes@736
   174
                case 0:
nkeynes@736
   175
                    image->track[total_tracks].mode = GDROM_MODE2_FORM1;
nkeynes@736
   176
                    image->track[total_tracks].sector_size = 2048;
nkeynes@736
   177
                    break;
nkeynes@736
   178
                case 1:
nkeynes@736
   179
                    image->track[total_tracks].mode = GDROM_SEMIRAW_MODE2;
nkeynes@736
   180
                    image->track[total_tracks].sector_size = 2336;
nkeynes@736
   181
                    break;
nkeynes@736
   182
                case 2:
nkeynes@736
   183
                default:
nkeynes@736
   184
                    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
nkeynes@736
   185
                    gdrom_image_destroy_no_close(disc);
nkeynes@736
   186
                    return NULL;
nkeynes@736
   187
                }
nkeynes@736
   188
                break;
nkeynes@736
   189
                default:
nkeynes@736
   190
                    ERROR( "Unsupported track mode %d", trk.mode );
nkeynes@736
   191
                    gdrom_image_destroy_no_close(disc);
nkeynes@736
   192
                    return NULL;
nkeynes@736
   193
            }
nkeynes@736
   194
            image->track[total_tracks].offset = posn + 
nkeynes@736
   195
            trk.pregap_length * image->track[total_tracks].sector_size ;
nkeynes@736
   196
            posn += trk.total_length * image->track[total_tracks].sector_size;
nkeynes@736
   197
            total_tracks++;
nkeynes@736
   198
            fread( marker, 1, 9, f );
nkeynes@736
   199
            if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
nkeynes@736
   200
                fseek( f, 79, SEEK_CUR );
nkeynes@736
   201
            } else {
nkeynes@736
   202
                fseek( f, -9, SEEK_CUR );
nkeynes@736
   203
            }
nkeynes@736
   204
        }
nkeynes@736
   205
        fseek( f, 12, SEEK_CUR );
nkeynes@1
   206
    }
nkeynes@342
   207
    image->track_count = total_tracks;
nkeynes@168
   208
    return disc;
nkeynes@1
   209
}
.