Search
lxdream.org :: lxdream/src/gdrom/cdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/cdi.c
changeset 1:eea311cfd33e
next31:495e480360d7
author nkeynes
date Sat Mar 13 00:03:32 2004 +0000 (15 years ago)
permissions -rw-r--r--
last change This commit was generated by cvs2svn to compensate for changes in r2,
which included commits to RCS files with non-trunk default branches.
file annotate diff log raw
nkeynes@1
     1
nkeynes@1
     2
#include <stdlib.h>
nkeynes@1
     3
#include <stdio.h>
nkeynes@1
     4
#include <stdint.h>
nkeynes@1
     5
#include <fcntl.h>
nkeynes@1
     6
#include <errno.h>
nkeynes@1
     7
#include <sys/stat.h>
nkeynes@1
     8
#include "cdi.h"
nkeynes@1
     9
nkeynes@1
    10
#define CDI_V2 0x80000004
nkeynes@1
    11
#define CDI_V3 0x80000005
nkeynes@1
    12
nkeynes@1
    13
static char track_start_marker[20] = { 0,0,1,0,0,0,255,255,255,255,
nkeynes@1
    14
                                       0,0,1,0,0,0,255,255,255,255 };
nkeynes@1
    15
nkeynes@1
    16
struct cdi_trailer {
nkeynes@1
    17
    uint32_t cdi_version;
nkeynes@1
    18
    uint32_t header_offset;
nkeynes@1
    19
};
nkeynes@1
    20
nkeynes@1
    21
struct cdi_track {
nkeynes@1
    22
    int file_posn;
nkeynes@1
    23
    int length; /* bytes */
nkeynes@1
    24
    int pregap; /* sectors */
nkeynes@1
    25
    int mode;
nkeynes@1
    26
    int sector_size;
nkeynes@1
    27
    int session;
nkeynes@1
    28
    struct cdi_track *next;
nkeynes@1
    29
};
nkeynes@1
    30
nkeynes@1
    31
struct cdi_handle {
nkeynes@1
    32
    int fd;
nkeynes@1
    33
    uint16_t num_sessions;
nkeynes@1
    34
    struct cdi_track *tracks;
nkeynes@1
    35
};
nkeynes@1
    36
nkeynes@1
    37
nkeynes@1
    38
nkeynes@1
    39
struct cdi_track_data {
nkeynes@1
    40
    char unknown[0x19];
nkeynes@1
    41
    uint32_t pregap_length;
nkeynes@1
    42
    uint32_t length;
nkeynes@1
    43
    char unknown2[6];
nkeynes@1
    44
    uint32_t mode;
nkeynes@1
    45
    char unknown3[0x0c];
nkeynes@1
    46
    uint32_t start_lba;
nkeynes@1
    47
    uint32_t total_length;
nkeynes@1
    48
    char unknown4[0x10];
nkeynes@1
    49
    uint32_t sector_size;
nkeynes@1
    50
    char unknown5[0x1D];
nkeynes@1
    51
} __attribute__((packed));
nkeynes@1
    52
nkeynes@1
    53
cdi_t cdi_open( char *filename )
nkeynes@1
    54
{
nkeynes@1
    55
#define BAIL( x, ... ) { fprintf( stderr, x, __VA_ARGS__ ); if( fd != -1 ) close(fd); return NULL; }
nkeynes@1
    56
nkeynes@1
    57
    struct stat st;
nkeynes@1
    58
    int fd = -1, i,j, tmp;
nkeynes@1
    59
    int posn = 0, hdr;
nkeynes@1
    60
    long len;
nkeynes@1
    61
    struct cdi_trailer trail;
nkeynes@1
    62
    struct cdi_handle cdi;
nkeynes@1
    63
    uint16_t tracks;
nkeynes@1
    64
    uint32_t new_fmt;
nkeynes@1
    65
    char tmpc;
nkeynes@1
    66
    char marker[20];
nkeynes@1
    67
nkeynes@1
    68
    fd = open( filename, O_RDONLY );
nkeynes@1
    69
    if( fd == -1 )
nkeynes@1
    70
        BAIL( "Unable to open file: %s (%s)\n", filename, strerror(errno) );
nkeynes@1
    71
    fstat( fd, &st );
nkeynes@1
    72
    if( st.st_size < 8 )
nkeynes@1
    73
        BAIL( "File is too small to be a valid CDI image: %s\n", filename );
nkeynes@1
    74
    len = lseek( fd, -8, SEEK_END );
nkeynes@1
    75
    read( fd, &trail, sizeof(trail) );
nkeynes@1
    76
    if( trail.header_offset > st.st_size ||
nkeynes@1
    77
        trail.header_offset == 0 )
nkeynes@1
    78
        BAIL( "Not a valid CDI image: %s\n", filename );
nkeynes@1
    79
nkeynes@1
    80
    if( trail.cdi_version == CDI_V2 ) trail.cdi_version = 2;
nkeynes@1
    81
    else if( trail.cdi_version == CDI_V3 ) trail.cdi_version = 3;
nkeynes@1
    82
    else BAIL( "Unknown CDI version code %08x in %s\n", trail.cdi_version,
nkeynes@1
    83
               filename );
nkeynes@1
    84
nkeynes@1
    85
    lseek( fd, trail.header_offset, SEEK_SET );
nkeynes@1
    86
    read( fd, &cdi.num_sessions, 2 );
nkeynes@1
    87
    
nkeynes@1
    88
nkeynes@1
    89
    printf( "CDI version: %d\n", trail.cdi_version );
nkeynes@1
    90
    printf( "Sessions: %d\n\n", cdi.num_sessions );
nkeynes@1
    91
    for( i=0; i< cdi.num_sessions; i++ ) {        
nkeynes@1
    92
        read( fd, &tracks, 2 );
nkeynes@1
    93
        printf( "Session %d - %d tracks:\n", i+1, tracks );
nkeynes@1
    94
        for( j=0; j<tracks; j++ ) {
nkeynes@1
    95
            struct cdi_track_data trk;
nkeynes@1
    96
            
nkeynes@1
    97
            read( fd, &new_fmt, 4 );
nkeynes@1
    98
            if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
nkeynes@1
    99
                printf( "Note: CDI image has 3.00.780+ flag set\n" );
nkeynes@1
   100
                lseek( fd, 8, SEEK_CUR );
nkeynes@1
   101
            }
nkeynes@1
   102
            read( fd, marker, 20 );
nkeynes@1
   103
            if( memcmp( marker, track_start_marker, 20) != 0 )
nkeynes@1
   104
                BAIL( "Track start marker not found, error reading cdi\n",NULL );
nkeynes@1
   105
            read( fd, &tmp, 4 );
nkeynes@1
   106
            printf( "Unknown 4 bytes: %08x\n", tmp );
nkeynes@1
   107
            read( fd, &tmpc, 1 );
nkeynes@1
   108
            lseek( fd, (int)tmpc, SEEK_CUR ); /* skip over the filename */
nkeynes@1
   109
            read( fd, &trk, sizeof(trk) );
nkeynes@1
   110
            switch( trk.sector_size ) {
nkeynes@1
   111
                case 0: trk.sector_size = 2048; hdr=0; break;
nkeynes@1
   112
                case 1: trk.sector_size = 2336; hdr=8; break;
nkeynes@1
   113
                case 2:
nkeynes@1
   114
                    trk.sector_size = 2352;
nkeynes@1
   115
                    if( trk.mode == 2 ) hdr=24;
nkeynes@1
   116
                    else hdr=16;
nkeynes@1
   117
                    break;
nkeynes@1
   118
                default: BAIL( "Unknown sector size: %d\n", trk.sector_size );
nkeynes@1
   119
            }
nkeynes@1
   120
            posn += hdr;
nkeynes@1
   121
            len = trk.length*trk.sector_size;
nkeynes@1
   122
            printf( "  Track %d\n", j+1 );
nkeynes@1
   123
            printf( "    Pregap: %08x\n", trk.pregap_length );
nkeynes@1
   124
            printf( "    Length: %08x\n", trk.length );
nkeynes@1
   125
            printf( "    Mode: %d\n", trk.mode );
nkeynes@1
   126
            printf( "    Sector size: %d\n", trk.sector_size );
nkeynes@1
   127
            printf( "    Start LBA: %08x\n", trk.start_lba );
nkeynes@1
   128
            printf( "    Total length: %08x\n", trk.total_length );
nkeynes@1
   129
            printf( "   ---\n" );
nkeynes@1
   130
            printf( "    File position: %08x\n", posn+trk.pregap_length*trk.sector_size );
nkeynes@1
   131
            printf( "    File length: %d\n", len );
nkeynes@1
   132
            posn += trk.total_length*trk.sector_size;
nkeynes@1
   133
            lseek( fd, 12, SEEK_CUR );
nkeynes@1
   134
            if( new_fmt )
nkeynes@1
   135
                lseek( fd, 78, SEEK_CUR );
nkeynes@1
   136
            tmp = lseek( fd, 0, SEEK_CUR );
nkeynes@1
   137
            printf( "(Header offset: %x)\n", tmp - trail.header_offset );
nkeynes@1
   138
        }
nkeynes@1
   139
    }
nkeynes@1
   140
    
nkeynes@1
   141
    return NULL;
nkeynes@1
   142
#undef BAIL(x, ...)
nkeynes@1
   143
}
nkeynes@1
   144
nkeynes@1
   145
int main(int argc, char *argv[] )
nkeynes@1
   146
{
nkeynes@1
   147
    int i;
nkeynes@1
   148
nkeynes@1
   149
    for( i=1; i<argc; i++ ) {
nkeynes@1
   150
        cdi_open(argv[i]);
nkeynes@1
   151
    }
nkeynes@1
   152
    return 0;
nkeynes@1
   153
}
nkeynes@1
   154
    
.