Search
lxdream.org :: lxdream/src/gdrom/nrg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/nrg.c
changeset 138:afabd7e6d26d
next142:2f631c3a3946
author nkeynes
date Sun Apr 30 01:51:08 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Add Nero file format handler and general gdrom disc hooks
view annotate diff log raw
     1 /**
     2  * $Id: nrg.c,v 1.1 2006-04-30 01:51:08 nkeynes Exp $
     3  *
     4  * Nero (NRG) CD file format. File information stolen shamelessly from
     5  * libcdio.
     6  *
     7  * Copyright (c) 2005 Nathan Keynes.
     8  *
     9  * This program is free software; you can redistribute it and/or modify
    10  * it under the terms of the GNU General Public License as published by
    11  * the Free Software Foundation; either version 2 of the License, or
    12  * (at your option) any later version.
    13  *
    14  * This program is distributed in the hope that it will be useful,
    15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17  * GNU General Public License for more details.
    18  */
    20 #include <assert.h>
    21 #include <stdio.h>
    22 #include <errno.h>
    23 #include "gdrom/gdrom.h"
    24 #include "dream.h"
    26 #define NERO_V55_ID  0x4e455235 
    27 #define NERO_V50_ID  0x4e45524f 
    29 /* Courtesy of libcdio */
    30 /* 5.0 or earlier */
    31 #define NERO_ID  0x4e45524f  /* Nero pre 5.5.x */
    32 #define CUES_ID  0x43554553  /* Nero pre version 5.5.x-6.x */
    33 #define DAOI_ID  0x44414f49
    34 #define ETNF_ID  0x45544e46
    35 #define SINF_ID  0x53494e46  /* Session information */
    36 #define END_ID  0x454e4421
    37 /* 5.5+ only */
    38 #define NER5_ID  0x4e455235  /* Nero version 5.5.x */
    39 #define CDTX_ID  0x43445458  /* CD TEXT */
    40 #define CUEX_ID  0x43554558  /* Nero version 5.5.x-6.x */
    41 #define DAOX_ID  0x44414f58  /* Nero version 5.5.x-6.x */
    42 #define ETN2_ID  0x45544e32
    43 #define MTYP_ID  0x4d545950  /* Disc Media type? */
    46 union nrg_footer {
    47     struct nrg_footer_v50 {
    48 	uint32_t dummy;
    49 	uint32_t id;
    50 	uint32_t offset;
    51     } v50;
    52     struct nrg_footer_v55 {
    53 	uint32_t id;
    54 	uint64_t offset;
    55     } v55;
    56 };
    58 struct nrg_chunk {
    59     uint32_t id;
    60     uint32_t length;
    61 };
    63 struct nrg_etnf {
    64     uint32_t offset;
    65     uint32_t length;
    66     uint32_t mode;
    67     uint32_t lba;
    68     uint32_t padding;
    69 };
    71 struct nrg_cues {
    72     uint8_t type;
    73     uint8_t track;
    74     uint8_t control;
    75     uint8_t pad;
    76     uint32_t addr;
    77 };
    79 struct nrg_daoi {
    80     uint32_t length;
    81     char mcn[14];
    82     uint8_t disc_mode;
    83     uint8_t unknown[2]; /* always 01 01? */
    84     uint8_t track_count;
    85     struct nrg_daoi_track {
    86 	char unknown[10];
    87 	uint32_t sector_size __attribute__((packed)); /* Always 0? */
    88 	uint8_t mode;
    89 	uint8_t unknown2[3]; /* Always 00 00 01? */
    90 	uint32_t pregap __attribute__((packed));
    91 	uint32_t offset __attribute__((packed));
    92 	uint32_t end __attribute__((packed));
    93     } track[0];
    94 } __attribute__((packed));
    96 /**
    97  * Convert an 8-bit BCD number to normal integer form. 
    98  * Eg, 0x79 => 79
    99  */
   100 uint8_t static bcd_to_uint8( uint8_t bcd )
   101 {
   102     return (bcd & 0x0F) + (((bcd & 0xF0)>>4)*10);
   103 }
   106 /**
   107  * Convert a 32 bit MSF address (BCD coded) to the
   108  * equivalent LBA form. 
   109  * Eg, 0x
   110  */
   111 uint32_t static msf_to_lba( uint32_t msf )
   112 {
   113     msf = ntohl(msf);
   114     int f = bcd_to_uint8(msf);
   115     int s = bcd_to_uint8(msf>>8);
   116     int m = bcd_to_uint8(msf>>16);
   117     return (m * 60 + s) * 75 + f;
   119 }
   121 uint32_t static nrg_track_mode( uint8_t mode )
   122 {
   123     switch( mode ) {
   124     case 0: return GDROM_MODE1;
   125     case 2: return GDROM_MODE2_XA1;
   126     case 3: return GDROM_MODE2;
   127     case 7: return GDROM_CDDA;
   128     default: 
   129 	ERROR( "Unrecognized track mode %d in Nero image", mode );
   130 	return -1;
   131     }
   132 }
   134 gdrom_disc_t nrg_image_open( const gchar *filename )
   135 {
   136     FILE *f = fopen( filename, "ro" );
   137     union nrg_footer footer;
   138     struct nrg_chunk chunk;
   139     struct nrg_daoi *dao;
   140     gdrom_disc_t disc;
   141     gboolean end = FALSE;
   142     int session_id = 0;
   143     int session_track_id = 0;
   144     int track_id = 0;
   145     int cue_track_id = 0, cue_track_count = 0;
   146     int i;
   148     if( f == NULL ) {
   149 	ERROR( "Unable to open file '%s': %s", filename, strerror(errno) );
   150 	return NULL;
   151     }
   153     fseek( f, -12, SEEK_END );
   154     fread( &footer, sizeof(footer), 1, f );
   155     if( ntohl(footer.v50.id) == NERO_V50_ID ) {
   156 	INFO( "Loading Nero 5.0 image" );
   157 	fseek( f, ntohl(footer.v50.offset), SEEK_SET );
   158     } else if( ntohl(footer.v55.id) == NERO_V55_ID ) {
   159 	INFO( "Loading Nero 5.5+ image" );
   160 	fseek( f, ntohl(footer.v55.offset), SEEK_SET );
   161     } else {
   162 	ERROR("Unable to understand file '%s' as a Nero image", filename );
   163 	return NULL;
   164     }
   166     disc = gdrom_image_new(f);
   167     if( disc == NULL ) {
   168 	ERROR("Unable to allocate memory!");
   169 	fclose(f);
   170 	return NULL;
   171     }
   173     do {
   174 	fread( &chunk, sizeof(chunk), 1, f );
   175 	chunk.length = ntohl(chunk.length);
   176 	char data[chunk.length];
   177 	fread( data, chunk.length, 1, f );
   178 	switch( ntohl(chunk.id) ) {
   179 	case CUES_ID:
   180 	    cue_track_id = track_id;
   181 	    cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
   182 	    track_id += cue_track_count;
   183 	    for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
   184 		struct nrg_cues *cue = (struct nrg_cues *)(data+i);
   185 		int track = 0;
   186 		if( cue->track == 0 )
   187 		    continue; /* Track 0. Leadin? always 0? */
   188 		if( cue->track == 0xAA ) { /* end of disc */
   189 		    disc->track[track_id-1].sector_count =
   190 			msf_to_lba( cue->addr ) - disc->track[track_id-1].lba;
   191 		} else {
   192 		    track = cue_track_id + bcd_to_uint8(cue->track) - 1;
   193 		    if( (cue->control & 0x01) == 0 ) { 
   194 			/* Pre-gap address. */
   195 			if( track != 0 ) {
   196 			    disc->track[track-1].sector_count = 
   197 				msf_to_lba( cue->addr ) - disc->track[track-1].lba;
   198 			}
   199 		    } else { /* Track-start address */
   200 			disc->track[track].lba = msf_to_lba( cue->addr );
   202 		    }
   203 		}
   204 	    }
   205 	    break;
   206 	case DAOI_ID:
   207 	    dao = (struct nrg_daoi *)data;
   208 	    memcpy( disc->mcn, dao->mcn, 13 );
   209 	    disc->mcn[13] = '\0';
   210 	    assert( dao->track_count * 30 + 22 == chunk.length );
   211 	    assert( dao->track_count == cue_track_count );
   212 	    for( i=0; i<dao->track_count; i++ ) {
   213 		disc->track[cue_track_id].sector_size = ntohl(dao->track[i].sector_size);
   214 		disc->track[cue_track_id].offset = ntohl(dao->track[i].offset);
   215 		disc->track[cue_track_id].mode = nrg_track_mode( dao->track[i].mode );
   216 		assert( disc->track[cue_track_id].sector_count == 
   217 			(ntohl(dao->track[i].end) - ntohl(dao->track[i].offset))/
   218 			ntohl(dao->track[i].sector_size) );
   219 		cue_track_id++;
   220 	    }
   221 	    break;
   222 	case SINF_ID: 
   223 	    /* Data is a single 32-bit number representing number of tracks in session */
   224 	    i = ntohl( *(uint32_t *)data );
   225 	    while( i-- > 0 )
   226 		disc->track[session_track_id++].session = session_id;
   227 	    session_id++;
   228 	    break;
   229 	case ETNF_ID:
   230 	    for( i=0; i < chunk.length; i+= 0x14 ) {
   231 		struct nrg_etnf *etnf = (struct nrg_etnf *)(data+i);
   232 		disc->track[track_id].offset = ntohl(etnf->offset);
   233 		disc->track[track_id].lba = ntohl(etnf->lba) + (i+1)*GDROM_PREGAP;
   234 		disc->track[track_id].mode = nrg_track_mode( ntohl(etnf->mode) );
   235 		if( disc->track[track_id].mode == -1 ) {
   236 		    disc->close(disc);
   237 		    return NULL;
   238 		}
   239 		disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
   240 		disc->track[track_id].sector_count = ntohl(etnf->length) / 
   241 		    disc->track[track_id].sector_size;
   242 		track_id++;
   243 	    }
   244 	    break;
   245 	case END_ID:
   246 	    end = TRUE;
   247 	    break;
   248 	}
   249     } while( !end );
   250     disc->track_count = track_id;
   251     return disc;
   252 }
   254 gboolean nrg_read_sectors( struct gdrom_disc *disc, uint32_t lba, uint32_t count, char *buf )
   255 {
   256     return FALSE;
   257 }
.