Search
lxdream.org :: lxdream/src/gdrom/nrg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/nrg.c
changeset 1023:264e2fd90be8
prev832:40e5bb525c4e
next1030:864417a57662
author nkeynes
date Mon Jun 08 04:12:21 2009 +0000 (13 years ago)
permissions -rw-r--r--
last change General cleanup of the GD-rom subsystem
- merge gdrom_image_t and gdrom_disc_t
- Abstract MMC devices using a lower-level scsi transport
- OSX: only look at the whole disc device, and ignore partitions
file annotate diff log raw
nkeynes@138
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@138
     3
 *
nkeynes@138
     4
 * Nero (NRG) CD file format. File information stolen shamelessly from
nkeynes@138
     5
 * libcdio.
nkeynes@138
     6
 *
nkeynes@138
     7
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@138
     8
 *
nkeynes@138
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@138
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@138
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@138
    12
 * (at your option) any later version.
nkeynes@138
    13
 *
nkeynes@138
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@138
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@138
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@138
    17
 * GNU General Public License for more details.
nkeynes@138
    18
 */
nkeynes@138
    19
nkeynes@138
    20
#include <assert.h>
nkeynes@138
    21
#include <stdio.h>
nkeynes@138
    22
#include <errno.h>
nkeynes@514
    23
#include <glib/gtypes.h>
nkeynes@678
    24
#include "gdrom/gddriver.h"
nkeynes@138
    25
#include "dream.h"
nkeynes@138
    26
nkeynes@168
    27
static gboolean nrg_image_is_valid( FILE *f );
nkeynes@168
    28
static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f );
nkeynes@168
    29
nkeynes@168
    30
struct gdrom_image_class nrg_image_class = { "Nero", "nrg", 
nkeynes@736
    31
        nrg_image_is_valid, nrg_image_open };
nkeynes@168
    32
nkeynes@138
    33
#define NERO_V55_ID  0x4e455235 
nkeynes@138
    34
#define NERO_V50_ID  0x4e45524f 
nkeynes@138
    35
nkeynes@138
    36
/* Courtesy of libcdio */
nkeynes@138
    37
/* 5.0 or earlier */
nkeynes@138
    38
#define NERO_ID  0x4e45524f  /* Nero pre 5.5.x */
nkeynes@138
    39
#define CUES_ID  0x43554553  /* Nero pre version 5.5.x-6.x */
nkeynes@138
    40
#define DAOI_ID  0x44414f49
nkeynes@138
    41
#define ETNF_ID  0x45544e46
nkeynes@138
    42
#define SINF_ID  0x53494e46  /* Session information */
nkeynes@138
    43
#define END_ID  0x454e4421
nkeynes@138
    44
/* 5.5+ only */
nkeynes@138
    45
#define NER5_ID  0x4e455235  /* Nero version 5.5.x */
nkeynes@138
    46
#define CDTX_ID  0x43445458  /* CD TEXT */
nkeynes@138
    47
#define CUEX_ID  0x43554558  /* Nero version 5.5.x-6.x */
nkeynes@138
    48
#define DAOX_ID  0x44414f58  /* Nero version 5.5.x-6.x */
nkeynes@138
    49
#define ETN2_ID  0x45544e32
nkeynes@138
    50
#define MTYP_ID  0x4d545950  /* Disc Media type? */
nkeynes@138
    51
nkeynes@138
    52
nkeynes@138
    53
union nrg_footer {
nkeynes@138
    54
    struct nrg_footer_v50 {
nkeynes@736
    55
        uint32_t dummy;
nkeynes@736
    56
        uint32_t id;
nkeynes@736
    57
        uint32_t offset;
nkeynes@138
    58
    } v50;
nkeynes@138
    59
    struct nrg_footer_v55 {
nkeynes@736
    60
        uint32_t id;
nkeynes@736
    61
        uint64_t offset;
nkeynes@138
    62
    } v55;
nkeynes@138
    63
};
nkeynes@138
    64
nkeynes@138
    65
struct nrg_chunk {
nkeynes@138
    66
    uint32_t id;
nkeynes@138
    67
    uint32_t length;
nkeynes@138
    68
};
nkeynes@138
    69
nkeynes@138
    70
struct nrg_etnf {
nkeynes@138
    71
    uint32_t offset;
nkeynes@138
    72
    uint32_t length;
nkeynes@138
    73
    uint32_t mode;
nkeynes@138
    74
    uint32_t lba;
nkeynes@138
    75
    uint32_t padding;
nkeynes@138
    76
};
nkeynes@138
    77
nkeynes@514
    78
struct nrg_etn2 {
nkeynes@514
    79
    uint64_t offset;
nkeynes@514
    80
    uint64_t length;
nkeynes@514
    81
    uint32_t mode;
nkeynes@514
    82
    uint32_t lba;
nkeynes@514
    83
    uint64_t padding;
nkeynes@514
    84
};
nkeynes@514
    85
nkeynes@138
    86
struct nrg_cues {
nkeynes@138
    87
    uint8_t type;
nkeynes@138
    88
    uint8_t track;
nkeynes@138
    89
    uint8_t control;
nkeynes@138
    90
    uint8_t pad;
nkeynes@138
    91
    uint32_t addr;
nkeynes@138
    92
};
nkeynes@138
    93
nkeynes@138
    94
struct nrg_daoi {
nkeynes@138
    95
    uint32_t length;
nkeynes@138
    96
    char mcn[14];
nkeynes@138
    97
    uint8_t disc_mode;
nkeynes@138
    98
    uint8_t unknown[2]; /* always 01 01? */
nkeynes@138
    99
    uint8_t track_count;
nkeynes@138
   100
    struct nrg_daoi_track {
nkeynes@736
   101
        char unknown[10];
nkeynes@736
   102
        uint32_t sector_size __attribute__((packed)); /* Always 0? */
nkeynes@736
   103
        uint8_t mode;
nkeynes@736
   104
        uint8_t unknown2[3]; /* Always 00 00 01? */
nkeynes@736
   105
        uint32_t pregap __attribute__((packed));
nkeynes@736
   106
        uint32_t offset __attribute__((packed));
nkeynes@736
   107
        uint32_t end __attribute__((packed));
nkeynes@138
   108
    } track[0];
nkeynes@138
   109
} __attribute__((packed));
nkeynes@138
   110
nkeynes@514
   111
struct nrg_daox {
nkeynes@514
   112
    uint32_t length;
nkeynes@514
   113
    char mcn[14];
nkeynes@514
   114
    uint8_t disc_mode;
nkeynes@514
   115
    uint8_t unknown[2]; /* always 01 01? */
nkeynes@514
   116
    uint8_t track_count;
nkeynes@514
   117
    struct nrg_daox_track {
nkeynes@736
   118
        char unknown[10];
nkeynes@736
   119
        uint32_t sector_size __attribute__((packed)); /* Always 0? */
nkeynes@736
   120
        uint8_t mode;
nkeynes@736
   121
        uint8_t unknown2[3]; /* Always 00 00 01? */
nkeynes@736
   122
        uint64_t pregap __attribute__((packed));
nkeynes@736
   123
        uint64_t offset __attribute__((packed));
nkeynes@736
   124
        uint64_t end __attribute__((packed));
nkeynes@514
   125
    } track[0];
nkeynes@514
   126
} __attribute__((packed));
nkeynes@514
   127
nkeynes@138
   128
/**
nkeynes@138
   129
 * Convert an 8-bit BCD number to normal integer form. 
nkeynes@138
   130
 * Eg, 0x79 => 79
nkeynes@138
   131
 */
nkeynes@138
   132
uint8_t static bcd_to_uint8( uint8_t bcd )
nkeynes@138
   133
{
nkeynes@138
   134
    return (bcd & 0x0F) + (((bcd & 0xF0)>>4)*10);
nkeynes@138
   135
}
nkeynes@138
   136
nkeynes@138
   137
nkeynes@138
   138
/**
nkeynes@138
   139
 * Convert a 32 bit MSF address (BCD coded) to the
nkeynes@138
   140
 * equivalent LBA form. 
nkeynes@138
   141
 * Eg, 0x
nkeynes@138
   142
 */
nkeynes@138
   143
uint32_t static msf_to_lba( uint32_t msf )
nkeynes@138
   144
{
nkeynes@514
   145
    msf = GUINT32_FROM_BE(msf);
nkeynes@138
   146
    int f = bcd_to_uint8(msf);
nkeynes@138
   147
    int s = bcd_to_uint8(msf>>8);
nkeynes@138
   148
    int m = bcd_to_uint8(msf>>16);
nkeynes@138
   149
    return (m * 60 + s) * 75 + f;
nkeynes@138
   150
nkeynes@138
   151
}
nkeynes@138
   152
nkeynes@138
   153
uint32_t static nrg_track_mode( uint8_t mode )
nkeynes@138
   154
{
nkeynes@138
   155
    switch( mode ) {
nkeynes@138
   156
    case 0: return GDROM_MODE1;
nkeynes@644
   157
    case 2: return GDROM_MODE2_FORM1;
nkeynes@644
   158
    case 3: return GDROM_SEMIRAW_MODE2;
nkeynes@138
   159
    case 7: return GDROM_CDDA;
nkeynes@138
   160
    default: 
nkeynes@736
   161
        ERROR( "Unrecognized track mode %d in Nero image", mode );
nkeynes@736
   162
        return -1;
nkeynes@138
   163
    }
nkeynes@138
   164
}
nkeynes@138
   165
nkeynes@168
   166
static gboolean nrg_image_is_valid( FILE *f )
nkeynes@138
   167
{
nkeynes@168
   168
    union nrg_footer footer;
nkeynes@168
   169
nkeynes@168
   170
    fseek( f, -12, SEEK_END );
nkeynes@168
   171
    fread( &footer, sizeof(footer), 1, f );
nkeynes@514
   172
    if( GUINT32_FROM_BE(footer.v50.id) == NERO_V50_ID ||
nkeynes@736
   173
            GUINT32_FROM_BE(footer.v55.id) == NERO_V55_ID ) {
nkeynes@736
   174
        return TRUE;
nkeynes@168
   175
    } else {
nkeynes@736
   176
        return FALSE;
nkeynes@168
   177
    }
nkeynes@168
   178
}
nkeynes@168
   179
nkeynes@168
   180
static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f )
nkeynes@168
   181
{
nkeynes@138
   182
    union nrg_footer footer;
nkeynes@138
   183
    struct nrg_chunk chunk;
nkeynes@138
   184
    struct nrg_daoi *dao;
nkeynes@514
   185
    struct nrg_daox *daox;
nkeynes@514
   186
    struct nrg_etnf *etnf;
nkeynes@514
   187
    struct nrg_etn2 *etn2;
nkeynes@138
   188
    gdrom_disc_t disc;
nkeynes@138
   189
    gboolean end = FALSE;
nkeynes@514
   190
    uint32_t chunk_id;
nkeynes@138
   191
    int session_id = 0;
nkeynes@138
   192
    int session_track_id = 0;
nkeynes@138
   193
    int track_id = 0;
nkeynes@138
   194
    int cue_track_id = 0, cue_track_count = 0;
nkeynes@514
   195
    int i, count;
nkeynes@138
   196
nkeynes@138
   197
    fseek( f, -12, SEEK_END );
nkeynes@138
   198
    fread( &footer, sizeof(footer), 1, f );
nkeynes@514
   199
    if( GUINT32_FROM_BE(footer.v50.id) == NERO_V50_ID ) {
nkeynes@736
   200
        INFO( "Loading Nero 5.0 image" );
nkeynes@736
   201
        fseek( f, GUINT32_FROM_BE(footer.v50.offset), SEEK_SET );
nkeynes@514
   202
    } else if( GUINT32_FROM_BE(footer.v55.id) == NERO_V55_ID ) {
nkeynes@736
   203
        INFO( "Loading Nero 5.5+ image" );
nkeynes@736
   204
        fseek( f, (uint32_t)GUINT64_FROM_BE(footer.v55.offset), SEEK_SET );
nkeynes@138
   205
    } else {
nkeynes@736
   206
        /* Not a (recognized) Nero image */
nkeynes@736
   207
        return NULL;
nkeynes@138
   208
    }
nkeynes@736
   209
nkeynes@464
   210
    disc = gdrom_image_new(filename, f);
nkeynes@138
   211
    if( disc == NULL ) {
nkeynes@736
   212
        ERROR("Unable to allocate memory!");
nkeynes@736
   213
        return NULL;
nkeynes@138
   214
    }
nkeynes@138
   215
nkeynes@138
   216
    do {
nkeynes@736
   217
        fread( &chunk, sizeof(chunk), 1, f );
nkeynes@736
   218
        chunk.length = GUINT32_FROM_BE(chunk.length);
nkeynes@736
   219
        char data[chunk.length];
nkeynes@736
   220
        fread( data, chunk.length, 1, f );
nkeynes@736
   221
        chunk_id = GUINT32_FROM_BE(chunk.id);
nkeynes@736
   222
        switch( chunk_id ) {
nkeynes@736
   223
        case CUES_ID:
nkeynes@736
   224
        case CUEX_ID:
nkeynes@736
   225
            cue_track_id = track_id;
nkeynes@736
   226
            cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
nkeynes@736
   227
            track_id += cue_track_count;
nkeynes@736
   228
            for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
nkeynes@736
   229
                struct nrg_cues *cue = (struct nrg_cues *)(data+i);
nkeynes@736
   230
                int track = 0;
nkeynes@736
   231
                uint32_t lba;
nkeynes@736
   232
                if( chunk_id == CUEX_ID ) {
nkeynes@736
   233
                    lba = GUINT32_FROM_BE( cue->addr ) + GDROM_PREGAP;
nkeynes@736
   234
                } else {
nkeynes@736
   235
                    lba = msf_to_lba( cue->addr );
nkeynes@736
   236
                }
nkeynes@736
   237
                if( cue->track == 0 )
nkeynes@736
   238
                    continue; /* Track 0. Leadin? always 0? */
nkeynes@736
   239
                if( cue->track == 0xAA ) { /* end of disc */
nkeynes@1023
   240
                    disc->track[track_id-1].sector_count =
nkeynes@1023
   241
                        lba - disc->track[track_id-1].lba;
nkeynes@736
   242
                } else {
nkeynes@832
   243
                    track = bcd_to_uint8(cue->track) - 1;
nkeynes@736
   244
                    if( (cue->control & 0x01) == 0 ) { 
nkeynes@736
   245
                        /* Pre-gap address. */
nkeynes@736
   246
                        if( track != 0 ) {
nkeynes@1023
   247
                            disc->track[track-1].sector_count = 
nkeynes@1023
   248
                                lba - disc->track[track-1].lba;
nkeynes@736
   249
                        }
nkeynes@736
   250
                    } else { /* Track-start address */
nkeynes@1023
   251
                        disc->track[track].lba = lba;
nkeynes@1023
   252
                        disc->track[track].flags = cue->type;
nkeynes@736
   253
                    }
nkeynes@736
   254
                }
nkeynes@736
   255
            }
nkeynes@736
   256
            break;
nkeynes@736
   257
        case DAOI_ID:
nkeynes@736
   258
            dao = (struct nrg_daoi *)data;
nkeynes@1023
   259
            memcpy( disc->mcn, dao->mcn, 13 );
nkeynes@1023
   260
            disc->mcn[13] = '\0';
nkeynes@832
   261
            assert( (dao->track_count - cue_track_id) * 30 + 22 == chunk.length );
nkeynes@832
   262
            assert( dao->track_count == track_id );
nkeynes@832
   263
            for( i=0; i<(dao->track_count-cue_track_id); i++ ) {
nkeynes@1023
   264
                disc->track[cue_track_id].sector_size = GUINT32_FROM_BE(dao->track[i].sector_size);
nkeynes@1023
   265
                disc->track[cue_track_id].offset = GUINT32_FROM_BE(dao->track[i].offset);
nkeynes@1023
   266
                disc->track[cue_track_id].mode = nrg_track_mode( dao->track[i].mode );
nkeynes@1023
   267
                disc->track[cue_track_id].sector_count =
nkeynes@736
   268
                    (GUINT32_FROM_BE(dao->track[i].end) - GUINT32_FROM_BE(dao->track[i].offset))/
nkeynes@736
   269
                    GUINT32_FROM_BE(dao->track[i].sector_size);
nkeynes@736
   270
                cue_track_id++;
nkeynes@736
   271
            }
nkeynes@736
   272
            break;
nkeynes@736
   273
        case DAOX_ID:
nkeynes@736
   274
            daox = (struct nrg_daox *)data;
nkeynes@1023
   275
            memcpy( disc->mcn, daox->mcn, 13 );
nkeynes@1023
   276
            disc->mcn[13] = '\0';
nkeynes@832
   277
            assert( (daox->track_count - cue_track_id) * 42 + 22 == chunk.length );
nkeynes@832
   278
            assert( daox->track_count == track_id );
nkeynes@832
   279
            for( i=0; i<(daox->track_count-cue_track_id); i++ ) {
nkeynes@1023
   280
                disc->track[cue_track_id].sector_size = GUINT32_FROM_BE(daox->track[i].sector_size);
nkeynes@1023
   281
                disc->track[cue_track_id].offset = GUINT64_FROM_BE(daox->track[i].offset);
nkeynes@1023
   282
                disc->track[cue_track_id].mode = nrg_track_mode( daox->track[i].mode );
nkeynes@1023
   283
                disc->track[cue_track_id].sector_count =
nkeynes@736
   284
                    (GUINT64_FROM_BE(daox->track[i].end) - GUINT64_FROM_BE(daox->track[i].offset))/
nkeynes@736
   285
                    GUINT32_FROM_BE(daox->track[i].sector_size);
nkeynes@736
   286
                cue_track_id++;
nkeynes@736
   287
            }
nkeynes@736
   288
            break;
nkeynes@514
   289
nkeynes@736
   290
        case SINF_ID: 
nkeynes@736
   291
            /* Data is a single 32-bit number representing number of tracks in session */
nkeynes@736
   292
            i = GUINT32_FROM_BE( *(uint32_t *)data );
nkeynes@736
   293
            while( i-- > 0 )
nkeynes@1023
   294
                disc->track[session_track_id++].session = session_id;
nkeynes@736
   295
            session_id++;
nkeynes@736
   296
            break;
nkeynes@736
   297
        case ETNF_ID:
nkeynes@736
   298
            etnf = (struct nrg_etnf *)data;
nkeynes@736
   299
            count = chunk.length / sizeof(struct nrg_etnf);
nkeynes@736
   300
            for( i=0; i < count; i++, etnf++ ) {
nkeynes@1023
   301
                disc->track[track_id].offset = GUINT32_FROM_BE(etnf->offset);
nkeynes@1023
   302
                disc->track[track_id].lba = GUINT32_FROM_BE(etnf->lba) + (i+1)*GDROM_PREGAP;
nkeynes@1023
   303
                disc->track[track_id].mode = nrg_track_mode( GUINT32_FROM_BE(etnf->mode) );
nkeynes@1023
   304
                if( disc->track[track_id].mode == -1 ) {
nkeynes@1023
   305
                    disc->destroy(disc,FALSE);
nkeynes@736
   306
                    return NULL;
nkeynes@736
   307
                }
nkeynes@1023
   308
                if( disc->track[track_id].mode == GDROM_CDDA )
nkeynes@1023
   309
                    disc->track[track_id].flags = 0x01;
nkeynes@736
   310
                else
nkeynes@1023
   311
                    disc->track[track_id].flags = 0x01 | TRACK_DATA;
nkeynes@1023
   312
                disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
nkeynes@1023
   313
                disc->track[track_id].sector_count = GUINT32_FROM_BE(etnf->length) / 
nkeynes@1023
   314
                disc->track[track_id].sector_size;
nkeynes@736
   315
                track_id++;
nkeynes@736
   316
            }
nkeynes@736
   317
            break;
nkeynes@736
   318
        case ETN2_ID:
nkeynes@736
   319
            etn2 = (struct nrg_etn2 *)data;
nkeynes@736
   320
            count = chunk.length / sizeof(struct nrg_etn2);
nkeynes@736
   321
            for( i=0; i < count; i++, etn2++ ) {
nkeynes@1023
   322
                disc->track[track_id].offset = (uint32_t)GUINT64_FROM_BE(etn2->offset);
nkeynes@1023
   323
                disc->track[track_id].lba = GUINT32_FROM_BE(etn2->lba) + (i+1)*GDROM_PREGAP;
nkeynes@1023
   324
                disc->track[track_id].mode = nrg_track_mode( GUINT32_FROM_BE(etn2->mode) );
nkeynes@1023
   325
                if( disc->track[track_id].mode == -1 ) {
nkeynes@1023
   326
                    disc->destroy(disc,FALSE);
nkeynes@736
   327
                    return NULL;
nkeynes@736
   328
                }
nkeynes@1023
   329
                if( disc->track[track_id].mode == GDROM_CDDA )
nkeynes@1023
   330
                    disc->track[track_id].flags = 0x01;
nkeynes@736
   331
                else
nkeynes@1023
   332
                    disc->track[track_id].flags = 0x01 | TRACK_DATA;
nkeynes@1023
   333
                disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
nkeynes@1023
   334
                disc->track[track_id].sector_count = (uint32_t)(GUINT64_FROM_BE(etn2->length) / 
nkeynes@1023
   335
                        disc->track[track_id].sector_size);
nkeynes@736
   336
                track_id++;
nkeynes@736
   337
            }
nkeynes@736
   338
            break;
nkeynes@736
   339
nkeynes@736
   340
        case END_ID:
nkeynes@736
   341
            end = TRUE;
nkeynes@736
   342
            break;
nkeynes@736
   343
        }
nkeynes@138
   344
    } while( !end );
nkeynes@1023
   345
    disc->track_count = track_id;
nkeynes@138
   346
    return disc;
nkeynes@138
   347
}
nkeynes@138
   348
nkeynes@138
   349
nkeynes@342
   350
.