Search
lxdream.org :: lxdream/src/gdrom/nrg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/nrg.c
changeset 168:203a72138e16
prev142:2f631c3a3946
next342:850502f0e8de
author nkeynes
date Fri Dec 29 00:21:46 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix comment for the IO bit
view annotate diff log raw
     1 /**
     2  * $Id: nrg.c,v 1.3 2006-06-26 10:30:42 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 static gboolean nrg_image_is_valid( FILE *f );
    27 static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f );
    29 struct gdrom_image_class nrg_image_class = { "Nero", "nrg", 
    30 					     nrg_image_is_valid, nrg_image_open };
    32 #define NERO_V55_ID  0x4e455235 
    33 #define NERO_V50_ID  0x4e45524f 
    35 /* Courtesy of libcdio */
    36 /* 5.0 or earlier */
    37 #define NERO_ID  0x4e45524f  /* Nero pre 5.5.x */
    38 #define CUES_ID  0x43554553  /* Nero pre version 5.5.x-6.x */
    39 #define DAOI_ID  0x44414f49
    40 #define ETNF_ID  0x45544e46
    41 #define SINF_ID  0x53494e46  /* Session information */
    42 #define END_ID  0x454e4421
    43 /* 5.5+ only */
    44 #define NER5_ID  0x4e455235  /* Nero version 5.5.x */
    45 #define CDTX_ID  0x43445458  /* CD TEXT */
    46 #define CUEX_ID  0x43554558  /* Nero version 5.5.x-6.x */
    47 #define DAOX_ID  0x44414f58  /* Nero version 5.5.x-6.x */
    48 #define ETN2_ID  0x45544e32
    49 #define MTYP_ID  0x4d545950  /* Disc Media type? */
    52 union nrg_footer {
    53     struct nrg_footer_v50 {
    54 	uint32_t dummy;
    55 	uint32_t id;
    56 	uint32_t offset;
    57     } v50;
    58     struct nrg_footer_v55 {
    59 	uint32_t id;
    60 	uint64_t offset;
    61     } v55;
    62 };
    64 struct nrg_chunk {
    65     uint32_t id;
    66     uint32_t length;
    67 };
    69 struct nrg_etnf {
    70     uint32_t offset;
    71     uint32_t length;
    72     uint32_t mode;
    73     uint32_t lba;
    74     uint32_t padding;
    75 };
    77 struct nrg_cues {
    78     uint8_t type;
    79     uint8_t track;
    80     uint8_t control;
    81     uint8_t pad;
    82     uint32_t addr;
    83 };
    85 struct nrg_daoi {
    86     uint32_t length;
    87     char mcn[14];
    88     uint8_t disc_mode;
    89     uint8_t unknown[2]; /* always 01 01? */
    90     uint8_t track_count;
    91     struct nrg_daoi_track {
    92 	char unknown[10];
    93 	uint32_t sector_size __attribute__((packed)); /* Always 0? */
    94 	uint8_t mode;
    95 	uint8_t unknown2[3]; /* Always 00 00 01? */
    96 	uint32_t pregap __attribute__((packed));
    97 	uint32_t offset __attribute__((packed));
    98 	uint32_t end __attribute__((packed));
    99     } track[0];
   100 } __attribute__((packed));
   102 /**
   103  * Convert an 8-bit BCD number to normal integer form. 
   104  * Eg, 0x79 => 79
   105  */
   106 uint8_t static bcd_to_uint8( uint8_t bcd )
   107 {
   108     return (bcd & 0x0F) + (((bcd & 0xF0)>>4)*10);
   109 }
   112 /**
   113  * Convert a 32 bit MSF address (BCD coded) to the
   114  * equivalent LBA form. 
   115  * Eg, 0x
   116  */
   117 uint32_t static msf_to_lba( uint32_t msf )
   118 {
   119     msf = ntohl(msf);
   120     int f = bcd_to_uint8(msf);
   121     int s = bcd_to_uint8(msf>>8);
   122     int m = bcd_to_uint8(msf>>16);
   123     return (m * 60 + s) * 75 + f;
   125 }
   127 uint32_t static nrg_track_mode( uint8_t mode )
   128 {
   129     switch( mode ) {
   130     case 0: return GDROM_MODE1;
   131     case 2: return GDROM_MODE2_XA1;
   132     case 3: return GDROM_MODE2;
   133     case 7: return GDROM_CDDA;
   134     default: 
   135 	ERROR( "Unrecognized track mode %d in Nero image", mode );
   136 	return -1;
   137     }
   138 }
   140 static gboolean nrg_image_is_valid( FILE *f )
   141 {
   142     union nrg_footer footer;
   144     fseek( f, -12, SEEK_END );
   145     fread( &footer, sizeof(footer), 1, f );
   146     if( ntohl(footer.v50.id) == NERO_V50_ID ) {
   147 	return TRUE;
   148     } else {
   149 	return FALSE;
   150     }
   151 }
   153 static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f )
   154 {
   155     union nrg_footer footer;
   156     struct nrg_chunk chunk;
   157     struct nrg_daoi *dao;
   158     gdrom_disc_t disc;
   159     gboolean end = FALSE;
   160     int session_id = 0;
   161     int session_track_id = 0;
   162     int track_id = 0;
   163     int cue_track_id = 0, cue_track_count = 0;
   164     int i;
   166     fseek( f, -12, SEEK_END );
   167     fread( &footer, sizeof(footer), 1, f );
   168     if( ntohl(footer.v50.id) == NERO_V50_ID ) {
   169 	INFO( "Loading Nero 5.0 image" );
   170 	fseek( f, ntohl(footer.v50.offset), SEEK_SET );
   171     } else if( ntohl(footer.v55.id) == NERO_V55_ID ) {
   172 	INFO( "Loading Nero 5.5+ image" );
   173 	fseek( f, ntohl(footer.v55.offset), SEEK_SET );
   174     } else {
   175 	/* Not a (recognized) Nero image */
   176 	return NULL;
   177     }
   179     disc = gdrom_image_new(f);
   180     if( disc == NULL ) {
   181 	ERROR("Unable to allocate memory!");
   182 	return NULL;
   183     }
   185     do {
   186 	fread( &chunk, sizeof(chunk), 1, f );
   187 	chunk.length = ntohl(chunk.length);
   188 	char data[chunk.length];
   189 	fread( data, chunk.length, 1, f );
   190 	switch( ntohl(chunk.id) ) {
   191 	case CUES_ID:
   192 	    cue_track_id = track_id;
   193 	    cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
   194 	    track_id += cue_track_count;
   195 	    for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
   196 		struct nrg_cues *cue = (struct nrg_cues *)(data+i);
   197 		int track = 0;
   198 		if( cue->track == 0 )
   199 		    continue; /* Track 0. Leadin? always 0? */
   200 		if( cue->track == 0xAA ) { /* end of disc */
   201 		    disc->track[track_id-1].sector_count =
   202 			msf_to_lba( cue->addr ) - disc->track[track_id-1].lba;
   203 		} else {
   204 		    track = cue_track_id + bcd_to_uint8(cue->track) - 1;
   205 		    if( (cue->control & 0x01) == 0 ) { 
   206 			/* Pre-gap address. */
   207 			if( track != 0 ) {
   208 			    disc->track[track-1].sector_count = 
   209 				msf_to_lba( cue->addr ) - disc->track[track-1].lba;
   210 			}
   211 		    } else { /* Track-start address */
   212 			disc->track[track].lba = msf_to_lba( cue->addr );
   213 			disc->track[track].flags = cue->type;
   214 		    }
   215 		}
   216 	    }
   217 	    break;
   218 	case DAOI_ID:
   219 	    dao = (struct nrg_daoi *)data;
   220 	    memcpy( disc->mcn, dao->mcn, 13 );
   221 	    disc->mcn[13] = '\0';
   222 	    assert( dao->track_count * 30 + 22 == chunk.length );
   223 	    assert( dao->track_count == cue_track_count );
   224 	    for( i=0; i<dao->track_count; i++ ) {
   225 		disc->track[cue_track_id].sector_size = ntohl(dao->track[i].sector_size);
   226 		disc->track[cue_track_id].offset = ntohl(dao->track[i].offset);
   227 		disc->track[cue_track_id].mode = nrg_track_mode( dao->track[i].mode );
   228 		assert( disc->track[cue_track_id].sector_count == 
   229 			(ntohl(dao->track[i].end) - ntohl(dao->track[i].offset))/
   230 			ntohl(dao->track[i].sector_size) );
   231 		cue_track_id++;
   232 	    }
   233 	    break;
   234 	case SINF_ID: 
   235 	    /* Data is a single 32-bit number representing number of tracks in session */
   236 	    i = ntohl( *(uint32_t *)data );
   237 	    while( i-- > 0 )
   238 		disc->track[session_track_id++].session = session_id;
   239 	    session_id++;
   240 	    break;
   241 	case ETNF_ID:
   242 	    for( i=0; i < chunk.length; i+= 0x14 ) {
   243 		struct nrg_etnf *etnf = (struct nrg_etnf *)(data+i);
   244 		disc->track[track_id].offset = ntohl(etnf->offset);
   245 		disc->track[track_id].lba = ntohl(etnf->lba) + (i+1)*GDROM_PREGAP;
   246 		disc->track[track_id].mode = nrg_track_mode( ntohl(etnf->mode) );
   247 		if( disc->track[track_id].mode == -1 ) {
   248 		    disc->close(disc);
   249 		    return NULL;
   250 		}
   251 		if( disc->track[track_id].mode == GDROM_CDDA )
   252 		    disc->track[track_id].flags = 0x01;
   253 		else
   254 		    disc->track[track_id].flags = 0x01 | TRACK_DATA;
   255 		disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
   256 		disc->track[track_id].sector_count = ntohl(etnf->length) / 
   257 		    disc->track[track_id].sector_size;
   258 		track_id++;
   259 	    }
   260 	    break;
   261 	case END_ID:
   262 	    end = TRUE;
   263 	    break;
   264 	}
   265     } while( !end );
   266     disc->track_count = track_id;
   267     return disc;
   268 }
   270 gboolean nrg_read_sectors( struct gdrom_disc *disc, uint32_t lba, uint32_t count, char *buf )
   271 {
   272     return FALSE;
   273 }
.