Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 236:581a9f8bf6a6
prev168:203a72138e16
next342:850502f0e8de
author nkeynes
date Fri Dec 15 10:18:39 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Initial implementation of the NOP (00h) command
view annotate diff log raw
     1 /**
     2  * $Id: cdi.c,v 1.4 2006-12-14 11:58:18 nkeynes Exp $
     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 <fcntl.h>
    23 #include <errno.h>
    24 #include <sys/stat.h>
    25 #include "gdrom/gdrom.h"
    27 #define CDI_V2_ID 0x80000004
    28 #define CDI_V3_ID 0x80000005
    29 #define CDI_V35_ID 0x80000006
    32 static gboolean cdi_image_is_valid( FILE *f );
    33 static gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f );
    35 struct gdrom_image_class cdi_image_class = { "DiscJuggler", "cdi", 
    36 					     cdi_image_is_valid, cdi_image_open };
    38 static char track_start_marker[20] = { 0,0,1,0,0,0,255,255,255,255,
    39                                        0,0,1,0,0,0,255,255,255,255 };
    41 struct cdi_trailer {
    42     uint32_t cdi_version;
    43     uint32_t header_offset;
    44 };
    46 struct cdi_track_data {
    47     char unknown[0x19];
    48     uint32_t pregap_length;
    49     uint32_t length;
    50     char unknown2[6];
    51     uint32_t mode;
    52     char unknown3[0x0c];
    53     uint32_t start_lba;
    54     uint32_t total_length;
    55     char unknown4[0x10];
    56     uint32_t sector_size;
    57     char unknown5[0x1D];
    58 } __attribute__((packed));
    60 gboolean cdi_image_is_valid( FILE *f )
    61 {
    62     int len;
    63     struct cdi_trailer trail;
    65     fseek( f, -8, SEEK_END );
    66     len = ftell(f)+8;
    67     fread( &trail, sizeof(trail), 1, f );
    68     if( trail.header_offset >= len ||
    69         trail.header_offset == 0 )
    70 	return FALSE;
    71     return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID;
    72 }
    74 gdrom_disc_t cdi_image_open( const gchar *filename, FILE *f )
    75 {
    76     gdrom_disc_t disc = NULL;
    77     int fd = -1, i,j, tmp;
    78     uint16_t session_count;
    79     uint16_t track_count;
    80     int total_tracks = 0;
    81     int posn = 0, hdr;
    82     long len;
    83     struct cdi_trailer trail;
    84     uint32_t new_fmt;
    85     char tmpc;
    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 = 2;
    96     else if( trail.cdi_version == CDI_V3_ID ) trail.cdi_version = 3;
    97     else return NULL; 
    99     fseek( f, trail.header_offset, SEEK_SET );
   100     fread( &session_count, sizeof(session_count), 1, f );
   102     disc = gdrom_image_new(f);
   104     for( i=0; i< session_count; i++ ) {        
   105 	fread( &track_count, sizeof(track_count), 1, f );
   106         for( j=0; j<track_count; j++ ) {
   107             struct cdi_track_data trk;
   108             uint32_t new_fmt = 0;
   109 	    uint8_t fnamelen = 0;
   110             fread( &new_fmt, sizeof(new_fmt), 1, f );
   111             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   112 		fseek( f, 8, SEEK_CUR ); /* Skip */
   113             }
   114             fread( marker, 20, 1, f );
   115             if( memcmp( marker, track_start_marker, 20) != 0 ) {
   116 		ERROR( "Track start marker not found, error reading cdi image\n" );
   117 		free(disc);
   118 		return NULL;
   119 	    }
   120 	    fseek( f, 4, SEEK_CUR );
   121             fread( &fnamelen, 1, 1, f );
   122             fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
   123             fread( &trk, sizeof(trk), 1, f );
   124 	    disc->track[total_tracks].session = i;
   125 	    disc->track[total_tracks].lba = trk.start_lba + 150;
   126 	    disc->track[total_tracks].sector_count = trk.length;
   127 	    switch( trk.mode ) {
   128 	    case 0:
   129 		disc->track[total_tracks].mode = GDROM_CDDA;
   130 		disc->track[total_tracks].sector_size = 2352;
   131 		disc->track[total_tracks].flags = 0x01;
   132 		if( trk.sector_size != 2 ) {
   133 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   134 		    free(disc);
   135 		    return NULL;
   136 		}
   137 		break;
   138 	    case 1:
   139 		disc->track[total_tracks].mode = GDROM_MODE1;
   140 		disc->track[total_tracks].sector_size = 2048;
   141 		disc->track[total_tracks].flags = 0x41;
   142 		if( trk.sector_size != 0 ) {
   143 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   144 		    free(disc);
   145 		    return NULL;
   146 		}
   147 		break;
   148 	    case 2:
   149 		disc->track[total_tracks].flags = 0x41;
   150 		switch( trk.sector_size ) {
   151 		case 0:
   152 		    disc->track[total_tracks].mode = GDROM_MODE2_XA1;
   153 		    disc->track[total_tracks].sector_size = 2048;
   154 		    break;
   155 		case 1:
   156 		    disc->track[total_tracks].mode = GDROM_MODE2;
   157 		    disc->track[total_tracks].sector_size = 2336;
   158 		    break;
   159 		case 2:
   160 		default:
   161 		    ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   162 		    free(disc);
   163 		    return NULL;
   164 		}
   165 		break;
   166 	    default:
   167 		ERROR( "Unsupported track mode %d", trk.mode );
   168 		free(disc);
   169 		return NULL;
   170 	    }
   171 	    disc->track[total_tracks].offset = posn + 
   172 		trk.pregap_length * disc->track[total_tracks].sector_size ;
   173 	    posn += trk.total_length * disc->track[total_tracks].sector_size;
   174 	    total_tracks++;
   175             lseek( fd, 12, SEEK_CUR );
   176             if( new_fmt ) {
   177                 fseek( f, 90, SEEK_CUR );
   178 	    } else {
   179 		fseek( f, 12, SEEK_CUR );
   180 	    }
   181 	}
   182     }
   183     disc->track_count = total_tracks;
   184     disc->file = f;
   185     disc->filename = filename;
   186     return disc;
   187 }
.