Search
lxdream.org :: lxdream/src/drivers/cdrom/cd_cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/cd_cdi.c
changeset 1298:d0eb2307b847
prev1180:f2b9f4ef1987
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
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 "drivers/cdrom/cdimpl.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 gboolean cdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
    36 struct cdrom_disc_factory cdi_disc_factory = { "DiscJuggler", "cdi",
    37         cdi_image_is_valid, NULL, cdi_image_read_toc };
    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 #define RETURN_PARSE_ERROR( ... ) do { SET_ERROR(err, LX_ERR_FILE_INVALID, __VA_ARGS__); return FALSE; } while(0)
    78 static gboolean cdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
    79 {
    80     int i,j;
    81     uint16_t session_count;
    82     uint16_t track_count;
    83     int total_tracks = 0;
    84     int posn = 0;
    85     long len;
    86     struct cdi_trailer trail;
    87     char marker[20];
    89     FILE *f = cdrom_disc_get_base_file(disc);
    90     fseek( f, -8, SEEK_END );
    91     len = ftell(f)+8;
    92     fread( &trail, sizeof(trail), 1, f );
    93     if( trail.header_offset >= len ||
    94             trail.header_offset == 0 ) {
    95         RETURN_PARSE_ERROR( "Invalid CDI image" );
    96     }
    98     if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
    99             trail.cdi_version != CDI_V35_ID ) {
   100         RETURN_PARSE_ERROR( "Invalid CDI image" );
   101     }
   103     if( trail.cdi_version == CDI_V35_ID ) {
   104         fseek( f, -(long)trail.header_offset, SEEK_END );
   105     } else {
   106         fseek( f, trail.header_offset, SEEK_SET );
   107     }
   108     fread( &session_count, sizeof(session_count), 1, f );
   110     for( i=0; i< session_count; i++ ) {        
   111         fread( &track_count, sizeof(track_count), 1, f );
   112         if( (i != session_count-1 && track_count < 1) || track_count > 99 ) {
   113             RETURN_PARSE_ERROR("Invalid number of tracks (%d), bad cdi image", track_count);
   114         }
   115         if( track_count + total_tracks > 99 ) {
   116             RETURN_PARSE_ERROR("Invalid number of tracks in disc, bad cdi image" );
   117         }
   118         for( j=0; j<track_count; j++ ) {
   119             struct cdi_track_data trk;
   120             uint32_t new_fmt = 0;
   121             uint8_t fnamelen = 0;
   122             fread( &new_fmt, sizeof(new_fmt), 1, f );
   123             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   124                 fseek( f, 8, SEEK_CUR ); /* Skip */
   125             }
   126             fread( marker, 20, 1, f );
   127             if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
   128                 RETURN_PARSE_ERROR( "Track start marker not found, error reading cdi image" );
   129             }
   130             fseek( f, 4, SEEK_CUR );
   131             fread( &fnamelen, 1, 1, f );
   132             fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
   133             fseek( f, 19, SEEK_CUR );
   134             fread( &new_fmt, sizeof(new_fmt), 1, f );
   135             if( new_fmt == 0x80000000 ) {
   136                 fseek( f, 10, SEEK_CUR );
   137             } else {
   138                 fseek( f, 2, SEEK_CUR );
   139             }
   140             fread( &trk, sizeof(trk), 1, f );
   141             disc->track[total_tracks].sessionno = i+1;
   142             disc->track[total_tracks].lba = trk.start_lba;
   143             cdrom_count_t sector_count = trk.length;
   144             sector_mode_t mode;
   145             switch( trk.mode ) {
   146             case 0:
   147                 mode = SECTOR_CDDA;
   148                 disc->track[total_tracks].flags = 0x01;
   149                 if( trk.sector_size != 2 ) {
   150                     RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   151                 }
   152                 break;
   153             case 1:
   154                 mode = SECTOR_MODE1;
   155                 disc->track[total_tracks].flags = 0x41;
   156                 if( trk.sector_size != 0 ) {
   157                     RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   158                 }
   159                 break;
   160             case 2:
   161                 disc->track[total_tracks].flags = 0x41;
   162                 switch( trk.sector_size ) {
   163                 case 0:
   164                     mode = SECTOR_MODE2_FORM1;
   165                     break;
   166                 case 1:
   167                     mode = SECTOR_SEMIRAW_MODE2;
   168                     break;
   169                 case 2:
   170                 default:
   171                     RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
   172                 }
   173                 break;
   174             default:
   175                 RETURN_PARSE_ERROR( "Unsupported track mode %d", trk.mode );
   176             }
   177             uint32_t offset = posn +
   178                     trk.pregap_length * CDROM_SECTOR_SIZE(mode);
   179             disc->track[total_tracks].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
   180             posn += trk.total_length * CDROM_SECTOR_SIZE(mode);
   181             total_tracks++;
   182             if( trail.cdi_version != CDI_V2_ID ) {
   183                 uint32_t extmarker;
   184                 fseek( f, 5, SEEK_CUR );
   185                 fread( &extmarker, sizeof(extmarker), 1, f);
   186                 if( extmarker == 0xFFFFFFFF )  {
   187                     fseek( f, 78, SEEK_CUR );
   188                 }
   189             }
   190         }
   191         fseek( f, 12, SEEK_CUR );
   192         if( trail.cdi_version != CDI_V2_ID ) {
   193             fseek( f, 1, SEEK_CUR );
   194         }
   195     }
   197     disc->track_count = total_tracks;
   198     disc->session_count = session_count;
   199     return TRUE;
   200 }
.