Search
lxdream.org :: lxdream/src/drivers/cdrom/cd_cdi.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/cd_cdi.c
changeset 1097:d4807997e450
prev1030:864417a57662
next1109:700c5ab26a63
author nkeynes
date Sun Jan 31 18:35:06 2010 +1000 (14 years ago)
permissions -rw-r--r--
last change Refactor CDROM host support
- Completely separate GDROM hardware (in gdrom/gdrom.c) from generic CDROM
support (now in drivers/cdrom)
- Add concept of 'sector sources' that can be mixed and matched to create
cdrom discs (makes support of arbitrary disc types much simpler)
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/cdrom/cd_cdi.c Sun Jan 31 18:35:06 2010 +1000
1.3 @@ -0,0 +1,192 @@
1.4 +/**
1.5 + * $Id$
1.6 + *
1.7 + * CDI CD-image file support
1.8 + *
1.9 + * Copyright (c) 2005 Nathan Keynes.
1.10 + *
1.11 + * This program is free software; you can redistribute it and/or modify
1.12 + * it under the terms of the GNU General Public License as published by
1.13 + * the Free Software Foundation; either version 2 of the License, or
1.14 + * (at your option) any later version.
1.15 + *
1.16 + * This program is distributed in the hope that it will be useful,
1.17 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.18 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.19 + * GNU General Public License for more details.
1.20 + */
1.21 +
1.22 +#include <stdlib.h>
1.23 +#include <stdio.h>
1.24 +#include <stdint.h>
1.25 +#include <string.h>
1.26 +#include <fcntl.h>
1.27 +#include <errno.h>
1.28 +#include <sys/stat.h>
1.29 +#include "drivers/cdrom/cdimpl.h"
1.30 +
1.31 +#define CDI_V2_ID 0x80000004
1.32 +#define CDI_V3_ID 0x80000005
1.33 +#define CDI_V35_ID 0x80000006
1.34 +
1.35 +
1.36 +static gboolean cdi_image_is_valid( FILE *f );
1.37 +static gboolean cdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
1.38 +
1.39 +struct cdrom_disc_factory cdi_disc_factory = { "DiscJuggler", "cdi",
1.40 + cdi_image_is_valid, NULL, cdi_image_read_toc };
1.41 +
1.42 +static const char TRACK_START_MARKER[20] = { 0,0,1,0,0,0,255,255,255,255,
1.43 + 0,0,1,0,0,0,255,255,255,255 };
1.44 +static const char EXT_MARKER[9] = {0,255,255,255,255,255,255,255,255 };
1.45 +
1.46 +struct cdi_trailer {
1.47 + uint32_t cdi_version;
1.48 + uint32_t header_offset;
1.49 +};
1.50 +
1.51 +struct cdi_track_data {
1.52 + uint32_t pregap_length;
1.53 + uint32_t length;
1.54 + char unknown2[6];
1.55 + uint32_t mode;
1.56 + char unknown3[0x0c];
1.57 + uint32_t start_lba;
1.58 + uint32_t total_length;
1.59 + char unknown4[0x10];
1.60 + uint32_t sector_size;
1.61 + char unknown5[0x1D];
1.62 +} __attribute__((packed));
1.63 +
1.64 +gboolean cdi_image_is_valid( FILE *f )
1.65 +{
1.66 + int len;
1.67 + struct cdi_trailer trail;
1.68 +
1.69 + fseek( f, -8, SEEK_END );
1.70 + len = ftell(f)+8;
1.71 + fread( &trail, sizeof(trail), 1, f );
1.72 + if( trail.header_offset >= len ||
1.73 + trail.header_offset == 0 )
1.74 + return FALSE;
1.75 + return trail.cdi_version == CDI_V2_ID || trail.cdi_version == CDI_V3_ID ||
1.76 + trail.cdi_version == CDI_V35_ID;
1.77 +}
1.78 +
1.79 +#define RETURN_PARSE_ERROR( ... ) do { SET_ERROR(err, EINVAL, __VA_ARGS__); return FALSE; } while(0)
1.80 +
1.81 +static gboolean cdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
1.82 +{
1.83 + int i,j;
1.84 + uint16_t session_count;
1.85 + uint16_t track_count;
1.86 + int total_tracks = 0;
1.87 + int posn = 0;
1.88 + long len;
1.89 + struct cdi_trailer trail;
1.90 + char marker[20];
1.91 +
1.92 + FILE *f = cdrom_disc_get_base_file(disc);
1.93 + fseek( f, -8, SEEK_END );
1.94 + len = ftell(f)+8;
1.95 + fread( &trail, sizeof(trail), 1, f );
1.96 + if( trail.header_offset >= len ||
1.97 + trail.header_offset == 0 ) {
1.98 + RETURN_PARSE_ERROR( "Invalid CDI image" );
1.99 + }
1.100 +
1.101 + if( trail.cdi_version != CDI_V2_ID && trail.cdi_version != CDI_V3_ID &&
1.102 + trail.cdi_version != CDI_V35_ID ) {
1.103 + RETURN_PARSE_ERROR( "Invalid CDI image" );
1.104 + }
1.105 +
1.106 + if( trail.cdi_version == CDI_V35_ID ) {
1.107 + fseek( f, -(long)trail.header_offset, SEEK_END );
1.108 + } else {
1.109 + fseek( f, trail.header_offset, SEEK_SET );
1.110 + }
1.111 + fread( &session_count, sizeof(session_count), 1, f );
1.112 +
1.113 + for( i=0; i< session_count; i++ ) {
1.114 + fread( &track_count, sizeof(track_count), 1, f );
1.115 + if( track_count + total_tracks > 99 ) {
1.116 + RETURN_PARSE_ERROR("Invalid number of tracks, bad cdi image" );
1.117 + }
1.118 + for( j=0; j<track_count; j++ ) {
1.119 + struct cdi_track_data trk;
1.120 + uint32_t new_fmt = 0;
1.121 + uint8_t fnamelen = 0;
1.122 + fread( &new_fmt, sizeof(new_fmt), 1, f );
1.123 + if( new_fmt != 0 ) { /* Additional data 3.00.780+ ?? */
1.124 + fseek( f, 8, SEEK_CUR ); /* Skip */
1.125 + }
1.126 + fread( marker, 20, 1, f );
1.127 + if( memcmp( marker, TRACK_START_MARKER, 20) != 0 ) {
1.128 + RETURN_PARSE_ERROR( "Track start marker not found, error reading cdi image" );
1.129 + }
1.130 + fseek( f, 4, SEEK_CUR );
1.131 + fread( &fnamelen, 1, 1, f );
1.132 + fseek( f, (int)fnamelen, SEEK_CUR ); /* skip over the filename */
1.133 + fseek( f, 19, SEEK_CUR );
1.134 + fread( &new_fmt, sizeof(new_fmt), 1, f );
1.135 + if( new_fmt == 0x80000000 ) {
1.136 + fseek( f, 10, SEEK_CUR );
1.137 + } else {
1.138 + fseek( f, 2, SEEK_CUR );
1.139 + }
1.140 + fread( &trk, sizeof(trk), 1, f );
1.141 + disc->track[total_tracks].sessionno = i+1;
1.142 + disc->track[total_tracks].lba = trk.start_lba;
1.143 + cdrom_count_t sector_count = trk.length;
1.144 + sector_mode_t mode;
1.145 + switch( trk.mode ) {
1.146 + case 0:
1.147 + mode = SECTOR_CDDA;
1.148 + disc->track[total_tracks].flags = 0x01;
1.149 + if( trk.sector_size != 2 ) {
1.150 + RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
1.151 + }
1.152 + break;
1.153 + case 1:
1.154 + mode = SECTOR_MODE1;
1.155 + disc->track[total_tracks].flags = 0x41;
1.156 + if( trk.sector_size != 0 ) {
1.157 + RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
1.158 + }
1.159 + break;
1.160 + case 2:
1.161 + disc->track[total_tracks].flags = 0x41;
1.162 + switch( trk.sector_size ) {
1.163 + case 0:
1.164 + mode = SECTOR_MODE2_FORM1;
1.165 + break;
1.166 + case 1:
1.167 + mode = SECTOR_SEMIRAW_MODE2;
1.168 + break;
1.169 + case 2:
1.170 + default:
1.171 + RETURN_PARSE_ERROR( "Invalid combination of mode %d with size %d", trk.mode, trk.sector_size );
1.172 + }
1.173 + break;
1.174 + default:
1.175 + RETURN_PARSE_ERROR( "Unsupported track mode %d", trk.mode );
1.176 + }
1.177 + uint32_t offset = posn +
1.178 + trk.pregap_length * CDROM_SECTOR_SIZE(mode);
1.179 + disc->track[total_tracks].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
1.180 + posn += trk.total_length * CDROM_SECTOR_SIZE(mode);
1.181 + total_tracks++;
1.182 + fread( marker, 1, 9, f );
1.183 + if( memcmp( marker, EXT_MARKER, 9 ) == 0 ) {
1.184 + fseek( f, 79, SEEK_CUR );
1.185 + } else {
1.186 + fseek( f, -9, SEEK_CUR );
1.187 + }
1.188 + }
1.189 + fseek( f, 12, SEEK_CUR );
1.190 + }
1.191 +
1.192 + disc->track_count = total_tracks;
1.193 + disc->session_count = session_count;
1.194 + return TRUE;
1.195 +}
.