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 Sun Dec 25 08:24:11 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change Finish adding header blocks to all files
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@31
     2
 * $Id: cdi.c,v 1.2 2005-12-25 08:24:11 nkeynes Exp $
nkeynes@31
     3
 *
nkeynes@31
     4
 * CDI CD-image file support
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@1
    18
nkeynes@1
    19
#include <stdlib.h>
nkeynes@1
    20
#include <stdio.h>
nkeynes@1
    21
#include <stdint.h>
nkeynes@1
    22
#include <fcntl.h>
nkeynes@1
    23
#include <errno.h>
nkeynes@1
    24
#include <sys/stat.h>
nkeynes@1
    25
#include "cdi.h"
nkeynes@1
    26
nkeynes@1
    27
#define CDI_V2 0x80000004
nkeynes@1
    28
#define CDI_V3 0x80000005
nkeynes@1
    29
nkeynes@1
    30
static char track_start_marker[20] = { 0,0,1,0,0,0,255,255,255,255,
nkeynes@1
    31
                                       0,0,1,0,0,0,255,255,255,255 };
nkeynes@1
    32
nkeynes@1
    33
struct cdi_trailer {
nkeynes@1
    34
    uint32_t cdi_version;
nkeynes@1
    35
    uint32_t header_offset;
nkeynes@1
    36
};
nkeynes@1
    37
nkeynes@1
    38
struct cdi_track {
nkeynes@1
    39
    int file_posn;
nkeynes@1
    40
    int length; /* bytes */
nkeynes@1
    41
    int pregap; /* sectors */
nkeynes@1
    42
    int mode;
nkeynes@1
    43
    int sector_size;
nkeynes@1
    44
    int session;
nkeynes@1
    45
    struct cdi_track *next;
nkeynes@1
    46
};
nkeynes@1
    47
nkeynes@1
    48
struct cdi_handle {
nkeynes@1
    49
    int fd;
nkeynes@1
    50
    uint16_t num_sessions;
nkeynes@1
    51
    struct cdi_track *tracks;
nkeynes@1
    52
};
nkeynes@1
    53
nkeynes@1
    54
nkeynes@1
    55
nkeynes@1
    56
struct cdi_track_data {
nkeynes@1
    57
    char unknown[0x19];
nkeynes@1
    58
    uint32_t pregap_length;
nkeynes@1
    59
    uint32_t length;
nkeynes@1
    60
    char unknown2[6];
nkeynes@1
    61
    uint32_t mode;
nkeynes@1
    62
    char unknown3[0x0c];
nkeynes@1
    63
    uint32_t start_lba;
nkeynes@1
    64
    uint32_t total_length;
nkeynes@1
    65
    char unknown4[0x10];
nkeynes@1
    66
    uint32_t sector_size;
nkeynes@1
    67
    char unknown5[0x1D];
nkeynes@1
    68
} __attribute__((packed));
nkeynes@1
    69
nkeynes@1
    70
cdi_t cdi_open( char *filename )
nkeynes@1
    71
{
nkeynes@1
    72
#define BAIL( x, ... ) { fprintf( stderr, x, __VA_ARGS__ ); if( fd != -1 ) close(fd); return NULL; }
nkeynes@1
    73
nkeynes@1
    74
    struct stat st;
nkeynes@1
    75
    int fd = -1, i,j, tmp;
nkeynes@1
    76
    int posn = 0, hdr;
nkeynes@1
    77
    long len;
nkeynes@1
    78
    struct cdi_trailer trail;
nkeynes@1
    79
    struct cdi_handle cdi;
nkeynes@1
    80
    uint16_t tracks;
nkeynes@1
    81
    uint32_t new_fmt;
nkeynes@1
    82
    char tmpc;
nkeynes@1
    83
    char marker[20];
nkeynes@1
    84
nkeynes@1
    85
    fd = open( filename, O_RDONLY );
nkeynes@1
    86
    if( fd == -1 )
nkeynes@1
    87
        BAIL( "Unable to open file: %s (%s)\n", filename, strerror(errno) );
nkeynes@1
    88
    fstat( fd, &st );
nkeynes@1
    89
    if( st.st_size < 8 )
nkeynes@1
    90
        BAIL( "File is too small to be a valid CDI image: %s\n", filename );
nkeynes@1
    91
    len = lseek( fd, -8, SEEK_END );
nkeynes@1
    92
    read( fd, &trail, sizeof(trail) );
nkeynes@1
    93
    if( trail.header_offset > st.st_size ||
nkeynes@1
    94
        trail.header_offset == 0 )
nkeynes@1
    95
        BAIL( "Not a valid CDI image: %s\n", filename );
nkeynes@1
    96
nkeynes@1
    97
    if( trail.cdi_version == CDI_V2 ) trail.cdi_version = 2;
nkeynes@1
    98
    else if( trail.cdi_version == CDI_V3 ) trail.cdi_version = 3;
nkeynes@1
    99
    else BAIL( "Unknown CDI version code %08x in %s\n", trail.cdi_version,
nkeynes@1
   100
               filename );
nkeynes@1
   101
nkeynes@1
   102
    lseek( fd, trail.header_offset, SEEK_SET );
nkeynes@1
   103
    read( fd, &cdi.num_sessions, 2 );
nkeynes@1
   104
    
nkeynes@1
   105
nkeynes@1
   106
    printf( "CDI version: %d\n", trail.cdi_version );
nkeynes@1
   107
    printf( "Sessions: %d\n\n", cdi.num_sessions );
nkeynes@1
   108
    for( i=0; i< cdi.num_sessions; i++ ) {        
nkeynes@1
   109
        read( fd, &tracks, 2 );
nkeynes@1
   110
        printf( "Session %d - %d tracks:\n", i+1, tracks );
nkeynes@1
   111
        for( j=0; j<tracks; j++ ) {
nkeynes@1
   112
            struct cdi_track_data trk;
nkeynes@1
   113
            
nkeynes@1
   114
            read( fd, &new_fmt, 4 );
nkeynes@1
   115
            if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
nkeynes@1
   116
                printf( "Note: CDI image has 3.00.780+ flag set\n" );
nkeynes@1
   117
                lseek( fd, 8, SEEK_CUR );
nkeynes@1
   118
            }
nkeynes@1
   119
            read( fd, marker, 20 );
nkeynes@1
   120
            if( memcmp( marker, track_start_marker, 20) != 0 )
nkeynes@1
   121
                BAIL( "Track start marker not found, error reading cdi\n",NULL );
nkeynes@1
   122
            read( fd, &tmp, 4 );
nkeynes@1
   123
            printf( "Unknown 4 bytes: %08x\n", tmp );
nkeynes@1
   124
            read( fd, &tmpc, 1 );
nkeynes@1
   125
            lseek( fd, (int)tmpc, SEEK_CUR ); /* skip over the filename */
nkeynes@1
   126
            read( fd, &trk, sizeof(trk) );
nkeynes@1
   127
            switch( trk.sector_size ) {
nkeynes@1
   128
                case 0: trk.sector_size = 2048; hdr=0; break;
nkeynes@1
   129
                case 1: trk.sector_size = 2336; hdr=8; break;
nkeynes@1
   130
                case 2:
nkeynes@1
   131
                    trk.sector_size = 2352;
nkeynes@1
   132
                    if( trk.mode == 2 ) hdr=24;
nkeynes@1
   133
                    else hdr=16;
nkeynes@1
   134
                    break;
nkeynes@1
   135
                default: BAIL( "Unknown sector size: %d\n", trk.sector_size );
nkeynes@1
   136
            }
nkeynes@1
   137
            posn += hdr;
nkeynes@1
   138
            len = trk.length*trk.sector_size;
nkeynes@1
   139
            printf( "  Track %d\n", j+1 );
nkeynes@1
   140
            printf( "    Pregap: %08x\n", trk.pregap_length );
nkeynes@1
   141
            printf( "    Length: %08x\n", trk.length );
nkeynes@1
   142
            printf( "    Mode: %d\n", trk.mode );
nkeynes@1
   143
            printf( "    Sector size: %d\n", trk.sector_size );
nkeynes@1
   144
            printf( "    Start LBA: %08x\n", trk.start_lba );
nkeynes@1
   145
            printf( "    Total length: %08x\n", trk.total_length );
nkeynes@1
   146
            printf( "   ---\n" );
nkeynes@1
   147
            printf( "    File position: %08x\n", posn+trk.pregap_length*trk.sector_size );
nkeynes@1
   148
            printf( "    File length: %d\n", len );
nkeynes@1
   149
            posn += trk.total_length*trk.sector_size;
nkeynes@1
   150
            lseek( fd, 12, SEEK_CUR );
nkeynes@1
   151
            if( new_fmt )
nkeynes@1
   152
                lseek( fd, 78, SEEK_CUR );
nkeynes@1
   153
            tmp = lseek( fd, 0, SEEK_CUR );
nkeynes@1
   154
            printf( "(Header offset: %x)\n", tmp - trail.header_offset );
nkeynes@1
   155
        }
nkeynes@1
   156
    }
nkeynes@1
   157
    
nkeynes@1
   158
    return NULL;
nkeynes@1
   159
#undef BAIL(x, ...)
nkeynes@1
   160
}
nkeynes@1
   161
nkeynes@1
   162
int main(int argc, char *argv[] )
nkeynes@1
   163
{
nkeynes@1
   164
    int i;
nkeynes@1
   165
nkeynes@1
   166
    for( i=1; i<argc; i++ ) {
nkeynes@1
   167
        cdi_open(argv[i]);
nkeynes@1
   168
    }
nkeynes@1
   169
    return 0;
nkeynes@1
   170
}
nkeynes@1
   171
    
.