Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 1030:864417a57662
prev1023:264e2fd90be8
author nkeynes
date Mon Jun 22 01:13:16 2009 +0000 (11 years ago)
permissions -rw-r--r--
last change Fix disc type breakage introduced in last refactor
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * CDI CD-image file support
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     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.
    12  *
    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.
    17  */
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <stdint.h>
    22 #include <string.h>
    23 #include <fcntl.h>
    24 #include <errno.h>
    25 #include <sys/stat.h>
    26 #include "gdrom/gddriver.h"
    28 #define CDI_V2_ID 0x80000004
    29 #define CDI_V3_ID 0x80000005
    30 #define CDI_V35_ID 0x80000006
    33 static gboolean cdi_image_is_valid( FILE *f );
    34 static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
    36 struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi", 
    37         cdi_image_is_valid, cdi_image_open };
    39 static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
    40         0,0,1,0,0,0,255,255,255,255 };
    41 static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
    43 struct cdi_trailer {
    44     uint32_t cdi_version;
    45     uint32_t header_offset;
    46 };
    48 struct cdi_track_data {
    49     uint32_t pregap_length;
    50     uint32_t length;
    51     char unknown2[6];
    52     uint32_t mode;
    53     char unknown3[0x0c];
    54     uint32_t start_lba;
    55     uint32_t total_length;
    56     char unknown4[0x10];
    57     uint32_t sector_size;
    58     char unknown5[0x1D];
    59 } __attribute__((packed));
    61 gboolean cdi_image_is_valid( FILE *f )
    62 {
    63     int len;
    64     struct cdi_trailer trail;
    66     fseek( f, -8, SEEK_END );
    67     len = ftell(f)+8;
    68     fread( &trail, sizeof(trail), 1, f );
    69     if( trail.header_offset >= len ||
    70             trail.header_offset == 0 )
    71         return FALSE;
    72     return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID ||
    73     trail.cdi_version == CDI_V35_ID;
    74 }
    76 gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
    77 {
    78     gdrom_disc_t disc = NULL;
    79     int i,j;
    80     uint16_t session_count;
    81     uint16_t track_count;
    82     int total_tracks = 0;
    83     int posn = 0;
    84     long len;
    85     struct cdi_trailer trail;
    86     char marker[20];
    88     fseek( f, -8, SEEK_END );
    89     len = ftell(f)+8;
    90     fread( &trail, sizeof(trail), 1, f );
    91     if( trail.header_offset >= len ||
    92             trail.header_offset == 0 )
    93         return NULL;
    95     if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
    96             trail.cdi_version != CDI_V35_ID ) {
    97         return NULL;
    98     }
   100     if( trail.cdi_version == CDI_V35_ID ) {
   101         fseek( f, -(long)trail.header_offset, SEEK_END );
   102     } else {
   103         fseek( f, trail.header_offset, SEEK_SET );
   104     }
   105     fread( &session_count, sizeof(session_count), 1, f );
   107     disc = gdrom_image_new(filename, f);
   108     if( disc == NULL ) {
   109         ERROR("Unable to allocate memory!");
   110         return NULL;
   111     }
   113     for( i=0; i< session_count; i++ ) {        
   114         fread( &track_count, sizeof(track_count), 1, f );
   115         if( track_count + total_tracks > 99 ) {
   116             ERROR( "Invalid number of tracks, bad cdi image\n" );
   117             disc->destroy(disc,FALSE);
   118             return NULL;
   119         }
   120         for( j=0; j<track_count; j++ ) {
   121             struct cdi_track_data trk;
   122             uint32_t new_fmt = 0;
   123             uint8_t fnamelen = 0;
   124             fread( &new_fmt, sizeof(new_fmt), 1, f );
   125             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   126                 fseek( f, 8, SEEK_CUR ); /* Skip */
   127             }
   128             fread( marker, 20, 1, f );
   129             if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
   130                 ERROR( "Track start marker not found, error reading cdi image\n" );
   131                 disc->destroy(disc,FALSE);
   132                 return NULL;
   133             }
   134             fseek( f, 4, SEEK_CUR );
   135             fread( &fnamelen, 1, 1, f );
   136             fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
   137             fseek( f, 19, SEEK_CUR );
   138             fread( &new_fmt, sizeof(new_fmt), 1, f );
   139             if( new_fmt == 0x80000000 ) {
   140                 fseek( f, 10, SEEK_CUR );
   141             } else {
   142                 fseek( f, 2, SEEK_CUR );
   143             }
   144             fread( &trk, sizeof(trk), 1, f );
   145             disc->track[total_tracks].session = i;
   146             disc->track[total_tracks].lba = trk.start_lba + 150;
   147             disc->track[total_tracks].sector_count = trk.length;
   148             switch( trk.mode ) {
   149             case 0:
   150                 disc->track[total_tracks].mode = GDROM_CDDA;
   151                 disc->track[total_tracks].sector_size = 2352;
   152                 disc->track[total_tracks].flags = 0x01;
   153                 if( trk.sector_size != 2 ) {
   154                     ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   155                     disc->destroy(disc,FALSE);
   156                     return NULL;
   157                 }
   158                 break;
   159             case 1:
   160                 disc->track[total_tracks].mode = GDROM_MODE1;
   161                 disc->track[total_tracks].sector_size = 2048;
   162                 disc->track[total_tracks].flags = 0x41;
   163                 if( trk.sector_size != 0 ) {
   164                     ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   165                     disc->destroy(disc,FALSE);
   166                     return NULL;
   167                 }
   168                 break;
   169             case 2:
   170                 disc->track[total_tracks].flags = 0x41;
   171                 switch( trk.sector_size ) {
   172                 case 0:
   173                     disc->track[total_tracks].mode = GDROM_MODE2_FORM1;
   174                     disc->track[total_tracks].sector_size = 2048;
   175                     break;
   176                 case 1:
   177                     disc->track[total_tracks].mode = GDROM_SEMIRAW_MODE2;
   178                     disc->track[total_tracks].sector_size = 2336;
   179                     break;
   180                 case 2:
   181                 default:
   182                     ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   183                     disc->destroy(disc,FALSE);
   184                     return NULL;
   185                 }
   186                 break;
   187                 default:
   188                     ERROR( "Unsupported track mode %d", trk.mode );
   189                     disc->destroy(disc,FALSE);
   190                     return NULL;
   191             }
   192             disc->track[total_tracks].offset = posn + 
   193             trk.pregap_length * disc->track[total_tracks].sector_size ;
   194             posn += trk.total_length * disc->track[total_tracks].sector_size;
   195             total_tracks++;
   196             fread( marker, 1, 9, f );
   197             if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
   198                 fseek( f, 79, SEEK_CUR );
   199             } else {
   200                 fseek( f, -9, SEEK_CUR );
   201             }
   202         }
   203         fseek( f, 12, SEEK_CUR );
   204     }
   205     disc->track_count = total_tracks;
   206     gdrom_set_disc_type(disc);
   207     return disc;
   208 }
.