Search
lxdream.org :: lxdream/src/gdrom/nrg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/nrg.c
changeset 1075:1a21750d300c
prev1030:864417a57662
author nkeynes
date Fri Jul 31 13:45:32 2009 +1000 (11 years ago)
permissions -rw-r--r--
last change Remove or change the level of a bunch of INFO messages that shouldn't really
be INFO level
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <glib/gtypes.h>
    24 #include "gdrom/gddriver.h"
    25 #include "dream.h"
    27 static gboolean nrg_image_is_valid( FILE *f );
    28 static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f );
    30 struct gdrom_image_class nrg_image_class = { "Nero", "nrg", 
    31         nrg_image_is_valid, nrg_image_open };
    33 #define NERO_V55_ID  0x4e455235 
    34 #define NERO_V50_ID  0x4e45524f 
    36 /* Courtesy of libcdio */
    37 /* 5.0 or earlier */
    38 #define NERO_ID  0x4e45524f  /* Nero pre 5.5.x */
    39 #define CUES_ID  0x43554553  /* Nero pre version 5.5.x-6.x */
    40 #define DAOI_ID  0x44414f49
    41 #define ETNF_ID  0x45544e46
    42 #define SINF_ID  0x53494e46  /* Session information */
    43 #define END_ID  0x454e4421
    44 /* 5.5+ only */
    45 #define NER5_ID  0x4e455235  /* Nero version 5.5.x */
    46 #define CDTX_ID  0x43445458  /* CD TEXT */
    47 #define CUEX_ID  0x43554558  /* Nero version 5.5.x-6.x */
    48 #define DAOX_ID  0x44414f58  /* Nero version 5.5.x-6.x */
    49 #define ETN2_ID  0x45544e32
    50 #define MTYP_ID  0x4d545950  /* Disc Media type? */
    53 union nrg_footer {
    54     struct nrg_footer_v50 {
    55         uint32_t dummy;
    56         uint32_t id;
    57         uint32_t offset;
    58     } v50;
    59     struct nrg_footer_v55 {
    60         uint32_t id;
    61         uint64_t offset;
    62     } v55;
    63 };
    65 struct nrg_chunk {
    66     uint32_t id;
    67     uint32_t length;
    68 };
    70 struct nrg_etnf {
    71     uint32_t offset;
    72     uint32_t length;
    73     uint32_t mode;
    74     uint32_t lba;
    75     uint32_t padding;
    76 };
    78 struct nrg_etn2 {
    79     uint64_t offset;
    80     uint64_t length;
    81     uint32_t mode;
    82     uint32_t lba;
    83     uint64_t padding;
    84 };
    86 struct nrg_cues {
    87     uint8_t type;
    88     uint8_t track;
    89     uint8_t control;
    90     uint8_t pad;
    91     uint32_t addr;
    92 };
    94 struct nrg_daoi {
    95     uint32_t length;
    96     char mcn[14];
    97     uint8_t disc_mode;
    98     uint8_t unknown[2]; /* always 01 01? */
    99     uint8_t track_count;
   100     struct nrg_daoi_track {
   101         char unknown[10];
   102         uint32_t sector_size __attribute__((packed)); /* Always 0? */
   103         uint8_t mode;
   104         uint8_t unknown2[3]; /* Always 00 00 01? */
   105         uint32_t pregap __attribute__((packed));
   106         uint32_t offset __attribute__((packed));
   107         uint32_t end __attribute__((packed));
   108     } track[0];
   109 } __attribute__((packed));
   111 struct nrg_daox {
   112     uint32_t length;
   113     char mcn[14];
   114     uint8_t disc_mode;
   115     uint8_t unknown[2]; /* always 01 01? */
   116     uint8_t track_count;
   117     struct nrg_daox_track {
   118         char unknown[10];
   119         uint32_t sector_size __attribute__((packed)); /* Always 0? */
   120         uint8_t mode;
   121         uint8_t unknown2[3]; /* Always 00 00 01? */
   122         uint64_t pregap __attribute__((packed));
   123         uint64_t offset __attribute__((packed));
   124         uint64_t end __attribute__((packed));
   125     } track[0];
   126 } __attribute__((packed));
   128 /**
   129  * Convert an 8-bit BCD number to normal integer form. 
   130  * Eg, 0x79 => 79
   131  */
   132 uint8_t static bcd_to_uint8( uint8_t bcd )
   133 {
   134     return (bcd & 0x0F) + (((bcd & 0xF0)>>4)*10);
   135 }
   138 /**
   139  * Convert a 32 bit MSF address (BCD coded) to the
   140  * equivalent LBA form. 
   141  * Eg, 0x
   142  */
   143 uint32_t static msf_to_lba( uint32_t msf )
   144 {
   145     msf = GUINT32_FROM_BE(msf);
   146     int f = bcd_to_uint8(msf);
   147     int s = bcd_to_uint8(msf>>8);
   148     int m = bcd_to_uint8(msf>>16);
   149     return (m * 60 + s) * 75 + f;
   151 }
   153 uint32_t static nrg_track_mode( uint8_t mode )
   154 {
   155     switch( mode ) {
   156     case 0: return GDROM_MODE1;
   157     case 2: return GDROM_MODE2_FORM1;
   158     case 3: return GDROM_SEMIRAW_MODE2;
   159     case 7: return GDROM_CDDA;
   160     default: 
   161         ERROR( "Unrecognized track mode %d in Nero image", mode );
   162         return -1;
   163     }
   164 }
   166 static gboolean nrg_image_is_valid( FILE *f )
   167 {
   168     union nrg_footer footer;
   170     fseek( f, -12, SEEK_END );
   171     fread( &footer, sizeof(footer), 1, f );
   172     if( GUINT32_FROM_BE(footer.v50.id) == NERO_V50_ID ||
   173             GUINT32_FROM_BE(footer.v55.id) == NERO_V55_ID ) {
   174         return TRUE;
   175     } else {
   176         return FALSE;
   177     }
   178 }
   180 static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f )
   181 {
   182     union nrg_footer footer;
   183     struct nrg_chunk chunk;
   184     struct nrg_daoi *dao;
   185     struct nrg_daox *daox;
   186     struct nrg_etnf *etnf;
   187     struct nrg_etn2 *etn2;
   188     gdrom_disc_t disc;
   189     gboolean end = FALSE;
   190     uint32_t chunk_id;
   191     int session_id = 0;
   192     int session_track_id = 0;
   193     int track_id = 0;
   194     int cue_track_id = 0, cue_track_count = 0;
   195     int i, count;
   197     fseek( f, -12, SEEK_END );
   198     fread( &footer, sizeof(footer), 1, f );
   199     if( GUINT32_FROM_BE(footer.v50.id) == NERO_V50_ID ) {
   200         fseek( f, GUINT32_FROM_BE(footer.v50.offset), SEEK_SET );
   201     } else if( GUINT32_FROM_BE(footer.v55.id) == NERO_V55_ID ) {
   202         fseek( f, (uint32_t)GUINT64_FROM_BE(footer.v55.offset), SEEK_SET );
   203     } else {
   204         /* Not a (recognized) Nero image */
   205         return NULL;
   206     }
   208     disc = gdrom_image_new(filename, f);
   209     if( disc == NULL ) {
   210         ERROR("Unable to allocate memory!");
   211         return NULL;
   212     }
   214     do {
   215         fread( &chunk, sizeof(chunk), 1, f );
   216         chunk.length = GUINT32_FROM_BE(chunk.length);
   217         char data[chunk.length];
   218         fread( data, chunk.length, 1, f );
   219         chunk_id = GUINT32_FROM_BE(chunk.id);
   220         switch( chunk_id ) {
   221         case CUES_ID:
   222         case CUEX_ID:
   223             cue_track_id = track_id;
   224             cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
   225             track_id += cue_track_count;
   226             for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
   227                 struct nrg_cues *cue = (struct nrg_cues *)(data+i);
   228                 int track = 0;
   229                 uint32_t lba;
   230                 if( chunk_id == CUEX_ID ) {
   231                     lba = GUINT32_FROM_BE( cue->addr ) + GDROM_PREGAP;
   232                 } else {
   233                     lba = msf_to_lba( cue->addr );
   234                 }
   235                 if( cue->track == 0 )
   236                     continue; /* Track 0. Leadin? always 0? */
   237                 if( cue->track == 0xAA ) { /* end of disc */
   238                     disc->track[track_id-1].sector_count =
   239                         lba - disc->track[track_id-1].lba;
   240                 } else {
   241                     track = bcd_to_uint8(cue->track) - 1;
   242                     if( (cue->control & 0x01) == 0 ) { 
   243                         /* Pre-gap address. */
   244                         if( track != 0 ) {
   245                             disc->track[track-1].sector_count = 
   246                                 lba - disc->track[track-1].lba;
   247                         }
   248                     } else { /* Track-start address */
   249                         disc->track[track].lba = lba;
   250                         disc->track[track].flags = cue->type;
   251                     }
   252                 }
   253             }
   254             break;
   255         case DAOI_ID:
   256             dao = (struct nrg_daoi *)data;
   257             memcpy( disc->mcn, dao->mcn, 13 );
   258             disc->mcn[13] = '\0';
   259             assert( (dao->track_count - cue_track_id) * 30 + 22 == chunk.length );
   260             assert( dao->track_count == track_id );
   261             for( i=0; i<(dao->track_count-cue_track_id); i++ ) {
   262                 disc->track[cue_track_id].sector_size = GUINT32_FROM_BE(dao->track[i].sector_size);
   263                 disc->track[cue_track_id].offset = GUINT32_FROM_BE(dao->track[i].offset);
   264                 disc->track[cue_track_id].mode = nrg_track_mode( dao->track[i].mode );
   265                 disc->track[cue_track_id].sector_count =
   266                     (GUINT32_FROM_BE(dao->track[i].end) - GUINT32_FROM_BE(dao->track[i].offset))/
   267                     GUINT32_FROM_BE(dao->track[i].sector_size);
   268                 cue_track_id++;
   269             }
   270             break;
   271         case DAOX_ID:
   272             daox = (struct nrg_daox *)data;
   273             memcpy( disc->mcn, daox->mcn, 13 );
   274             disc->mcn[13] = '\0';
   275             assert( (daox->track_count - cue_track_id) * 42 + 22 == chunk.length );
   276             assert( daox->track_count == track_id );
   277             for( i=0; i<(daox->track_count-cue_track_id); i++ ) {
   278                 disc->track[cue_track_id].sector_size = GUINT32_FROM_BE(daox->track[i].sector_size);
   279                 disc->track[cue_track_id].offset = GUINT64_FROM_BE(daox->track[i].offset);
   280                 disc->track[cue_track_id].mode = nrg_track_mode( daox->track[i].mode );
   281                 disc->track[cue_track_id].sector_count =
   282                     (GUINT64_FROM_BE(daox->track[i].end) - GUINT64_FROM_BE(daox->track[i].offset))/
   283                     GUINT32_FROM_BE(daox->track[i].sector_size);
   284                 cue_track_id++;
   285             }
   286             break;
   288         case SINF_ID: 
   289             /* Data is a single 32-bit number representing number of tracks in session */
   290             i = GUINT32_FROM_BE( *(uint32_t *)data );
   291             while( i-- > 0 )
   292                 disc->track[session_track_id++].session = session_id;
   293             session_id++;
   294             break;
   295         case ETNF_ID:
   296             etnf = (struct nrg_etnf *)data;
   297             count = chunk.length / sizeof(struct nrg_etnf);
   298             for( i=0; i < count; i++, etnf++ ) {
   299                 disc->track[track_id].offset = GUINT32_FROM_BE(etnf->offset);
   300                 disc->track[track_id].lba = GUINT32_FROM_BE(etnf->lba) + (i+1)*GDROM_PREGAP;
   301                 disc->track[track_id].mode = nrg_track_mode( GUINT32_FROM_BE(etnf->mode) );
   302                 if( disc->track[track_id].mode == -1 ) {
   303                     disc->destroy(disc,FALSE);
   304                     return NULL;
   305                 }
   306                 if( disc->track[track_id].mode == GDROM_CDDA )
   307                     disc->track[track_id].flags = 0x01;
   308                 else
   309                     disc->track[track_id].flags = 0x01 | TRACK_DATA;
   310                 disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
   311                 disc->track[track_id].sector_count = GUINT32_FROM_BE(etnf->length) / 
   312                 disc->track[track_id].sector_size;
   313                 track_id++;
   314             }
   315             break;
   316         case ETN2_ID:
   317             etn2 = (struct nrg_etn2 *)data;
   318             count = chunk.length / sizeof(struct nrg_etn2);
   319             for( i=0; i < count; i++, etn2++ ) {
   320                 disc->track[track_id].offset = (uint32_t)GUINT64_FROM_BE(etn2->offset);
   321                 disc->track[track_id].lba = GUINT32_FROM_BE(etn2->lba) + (i+1)*GDROM_PREGAP;
   322                 disc->track[track_id].mode = nrg_track_mode( GUINT32_FROM_BE(etn2->mode) );
   323                 if( disc->track[track_id].mode == -1 ) {
   324                     disc->destroy(disc,FALSE);
   325                     return NULL;
   326                 }
   327                 if( disc->track[track_id].mode == GDROM_CDDA )
   328                     disc->track[track_id].flags = 0x01;
   329                 else
   330                     disc->track[track_id].flags = 0x01 | TRACK_DATA;
   331                 disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
   332                 disc->track[track_id].sector_count = (uint32_t)(GUINT64_FROM_BE(etn2->length) / 
   333                         disc->track[track_id].sector_size);
   334                 track_id++;
   335             }
   336             break;
   338         case END_ID:
   339             end = TRUE;
   340             break;
   341         }
   342     } while( !end );
   343     disc->track_count = track_id;
   344     gdrom_set_disc_type(disc);
   345     return disc;
   346 }
.