Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 31:495e480360d7
prev1:eea311cfd33e
next168:203a72138e16
author nkeynes
date Wed Mar 22 14:27:40 2006 +0000 (18 years ago)
permissions -rw-r--r--
last change Fix FMOV for writes to the back bank (ie FMOV @R1, XD2)
view annotate diff log raw
     1 /**
     2  * $Id: cdi.c,v 1.2 2005-12-25 08:24:11 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 "cdi.h"
    27 #define CDI_V2 0x80000004
    28 #define CDI_V3 0x80000005
    30 static char track_start_marker[20] = { 0,0,1,0,0,0,255,255,255,255,
    31                                        0,0,1,0,0,0,255,255,255,255 };
    33 struct cdi_trailer {
    34     uint32_t cdi_version;
    35     uint32_t header_offset;
    36 };
    38 struct cdi_track {
    39     int file_posn;
    40     int length; /* bytes */
    41     int pregap; /* sectors */
    42     int mode;
    43     int sector_size;
    44     int session;
    45     struct cdi_track *next;
    46 };
    48 struct cdi_handle {
    49     int fd;
    50     uint16_t num_sessions;
    51     struct cdi_track *tracks;
    52 };
    56 struct cdi_track_data {
    57     char unknown[0x19];
    58     uint32_t pregap_length;
    59     uint32_t length;
    60     char unknown2[6];
    61     uint32_t mode;
    62     char unknown3[0x0c];
    63     uint32_t start_lba;
    64     uint32_t total_length;
    65     char unknown4[0x10];
    66     uint32_t sector_size;
    67     char unknown5[0x1D];
    68 } __attribute__((packed));
    70 cdi_t cdi_open( char *filename )
    71 {
    72 #define BAIL( x, ... ) { fprintf( stderr, x, __VA_ARGS__ ); if( fd != -1 ) close(fd); return NULL; }
    74     struct stat st;
    75     int fd = -1, i,j, tmp;
    76     int posn = 0, hdr;
    77     long len;
    78     struct cdi_trailer trail;
    79     struct cdi_handle cdi;
    80     uint16_t tracks;
    81     uint32_t new_fmt;
    82     char tmpc;
    83     char marker[20];
    85     fd = open( filename, O_RDONLY );
    86     if( fd == -1 )
    87         BAIL( "Unable to open file: %s (%s)\n", filename, strerror(errno) );
    88     fstat( fd, &st );
    89     if( st.st_size < 8 )
    90         BAIL( "File is too small to be a valid CDI image: %s\n", filename );
    91     len = lseek( fd, -8, SEEK_END );
    92     read( fd, &trail, sizeof(trail) );
    93     if( trail.header_offset > st.st_size ||
    94         trail.header_offset == 0 )
    95         BAIL( "Not a valid CDI image: %s\n", filename );
    97     if( trail.cdi_version == CDI_V2 ) trail.cdi_version = 2;
    98     else if( trail.cdi_version == CDI_V3 ) trail.cdi_version = 3;
    99     else BAIL( "Unknown CDI version code %08x in %s\n", trail.cdi_version,
   100                filename );
   102     lseek( fd, trail.header_offset, SEEK_SET );
   103     read( fd, &cdi.num_sessions, 2 );
   106     printf( "CDI version: %d\n", trail.cdi_version );
   107     printf( "Sessions: %d\n\n", cdi.num_sessions );
   108     for( i=0; i< cdi.num_sessions; i++ ) {        
   109         read( fd, &tracks, 2 );
   110         printf( "Session %d - %d tracks:\n", i+1, tracks );
   111         for( j=0; j<tracks; j++ ) {
   112             struct cdi_track_data trk;
   114             read( fd, &new_fmt, 4 );
   115             if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
   116                 printf( "Note: CDI image has 3.00.780+ flag set\n" );
   117                 lseek( fd, 8, SEEK_CUR );
   118             }
   119             read( fd, marker, 20 );
   120             if( memcmp( marker, track_start_marker, 20) != 0 )
   121                 BAIL( "Track start marker not found, error reading cdi\n",NULL );
   122             read( fd, &tmp, 4 );
   123             printf( "Unknown 4 bytes: %08x\n", tmp );
   124             read( fd, &tmpc, 1 );
   125             lseek( fd, (int)tmpc, SEEK_CUR ); /* skip over the filename */
   126             read( fd, &trk, sizeof(trk) );
   127             switch( trk.sector_size ) {
   128                 case 0: trk.sector_size = 2048; hdr=0; break;
   129                 case 1: trk.sector_size = 2336; hdr=8; break;
   130                 case 2:
   131                     trk.sector_size = 2352;
   132                     if( trk.mode == 2 ) hdr=24;
   133                     else hdr=16;
   134                     break;
   135                 default: BAIL( "Unknown sector size: %d\n", trk.sector_size );
   136             }
   137             posn += hdr;
   138             len = trk.length*trk.sector_size;
   139             printf( "  Track %d\n", j+1 );
   140             printf( "    Pregap: %08x\n", trk.pregap_length );
   141             printf( "    Length: %08x\n", trk.length );
   142             printf( "    Mode: %d\n", trk.mode );
   143             printf( "    Sector size: %d\n", trk.sector_size );
   144             printf( "    Start LBA: %08x\n", trk.start_lba );
   145             printf( "    Total length: %08x\n", trk.total_length );
   146             printf( "   ---\n" );
   147             printf( "    File position: %08x\n", posn+trk.pregap_length*trk.sector_size );
   148             printf( "    File length: %d\n", len );
   149             posn += trk.total_length*trk.sector_size;
   150             lseek( fd, 12, SEEK_CUR );
   151             if( new_fmt )
   152                 lseek( fd, 78, SEEK_CUR );
   153             tmp = lseek( fd, 0, SEEK_CUR );
   154             printf( "(Header offset: %x)\n", tmp - trail.header_offset );
   155         }
   156     }
   158     return NULL;
   159 #undef BAIL(x, ...)
   160 }
   162 int main(int argc, char *argv[] )
   163 {
   164     int i;
   166     for( i=1; i<argc; i++ ) {
   167         cdi_open(argv[i]);
   168     }
   169     return 0;
   170 }
.