Search
lxdream.org :: lxdream/src/drivers/cdrom/cd_nrg.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/cd_nrg.c
changeset 1298:d0eb2307b847
prev1178:e55ec927d55d
author nkeynes
date Wed Feb 04 08:38:23 2015 +1000 (5 years ago)
permissions -rw-r--r--
last change Fix assorted compile warnings reported by Clang
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@1097
    23
#include "drivers/cdrom/cdimpl.h"
nkeynes@138
    24
#include "dream.h"
nkeynes@138
    25
nkeynes@168
    26
static gboolean nrg_image_is_valid( FILE *f );
nkeynes@1097
    27
static gboolean nrg_image_read_toc( cdrom_disc_t disc, ERROR *err );
nkeynes@168
    28
nkeynes@1097
    29
struct cdrom_disc_factory nrg_disc_factory = { "Nero", "nrg",
nkeynes@1097
    30
        nrg_image_is_valid, NULL, nrg_image_read_toc };
nkeynes@168
    31
nkeynes@138
    32
#define NERO_V55_ID  0x4e455235 
nkeynes@138
    33
#define NERO_V50_ID  0x4e45524f 
nkeynes@138
    34
nkeynes@138
    35
/* Courtesy of libcdio */
nkeynes@138
    36
/* 5.0 or earlier */
nkeynes@138
    37
#define NERO_ID  0x4e45524f  /* Nero pre 5.5.x */
nkeynes@138
    38
#define CUES_ID  0x43554553  /* Nero pre version 5.5.x-6.x */
nkeynes@138
    39
#define DAOI_ID  0x44414f49
nkeynes@138
    40
#define ETNF_ID  0x45544e46
nkeynes@138
    41
#define SINF_ID  0x53494e46  /* Session information */
nkeynes@138
    42
#define END_ID  0x454e4421
nkeynes@138
    43
/* 5.5+ only */
nkeynes@138
    44
#define NER5_ID  0x4e455235  /* Nero version 5.5.x */
nkeynes@138
    45
#define CDTX_ID  0x43445458  /* CD TEXT */
nkeynes@138
    46
#define CUEX_ID  0x43554558  /* Nero version 5.5.x-6.x */
nkeynes@138
    47
#define DAOX_ID  0x44414f58  /* Nero version 5.5.x-6.x */
nkeynes@138
    48
#define ETN2_ID  0x45544e32
nkeynes@138
    49
#define MTYP_ID  0x4d545950  /* Disc Media type? */
nkeynes@138
    50
nkeynes@138
    51
nkeynes@138
    52
union nrg_footer {
nkeynes@138
    53
    struct nrg_footer_v50 {
nkeynes@736
    54
        uint32_t dummy;
nkeynes@736
    55
        uint32_t id;
nkeynes@736
    56
        uint32_t offset;
nkeynes@138
    57
    } v50;
nkeynes@138
    58
    struct nrg_footer_v55 {
nkeynes@736
    59
        uint32_t id;
nkeynes@736
    60
        uint64_t offset;
nkeynes@1178
    61
    } __attribute__((packed)) v55;
nkeynes@138
    62
};
nkeynes@138
    63
nkeynes@138
    64
struct nrg_chunk {
nkeynes@138
    65
    uint32_t id;
nkeynes@138
    66
    uint32_t length;
nkeynes@138
    67
};
nkeynes@138
    68
nkeynes@138
    69
struct nrg_etnf {
nkeynes@138
    70
    uint32_t offset;
nkeynes@138
    71
    uint32_t length;
nkeynes@138
    72
    uint32_t mode;
nkeynes@138
    73
    uint32_t lba;
nkeynes@138
    74
    uint32_t padding;
nkeynes@138
    75
};
nkeynes@138
    76
nkeynes@514
    77
struct nrg_etn2 {
nkeynes@514
    78
    uint64_t offset;
nkeynes@514
    79
    uint64_t length;
nkeynes@514
    80
    uint32_t mode;
nkeynes@514
    81
    uint32_t lba;
nkeynes@514
    82
    uint64_t padding;
nkeynes@514
    83
};
nkeynes@514
    84
nkeynes@138
    85
struct nrg_cues {
nkeynes@138
    86
    uint8_t type;
nkeynes@138
    87
    uint8_t track;
nkeynes@138
    88
    uint8_t control;
nkeynes@138
    89
    uint8_t pad;
nkeynes@138
    90
    uint32_t addr;
nkeynes@138
    91
};
nkeynes@138
    92
nkeynes@138
    93
struct nrg_daoi {
nkeynes@138
    94
    uint32_t length;
nkeynes@138
    95
    char mcn[14];
nkeynes@138
    96
    uint8_t disc_mode;
nkeynes@138
    97
    uint8_t unknown[2]; /* always 01 01? */
nkeynes@138
    98
    uint8_t track_count;
nkeynes@138
    99
    struct nrg_daoi_track {
nkeynes@736
   100
        char unknown[10];
nkeynes@736
   101
        uint32_t sector_size __attribute__((packed)); /* Always 0? */
nkeynes@736
   102
        uint8_t mode;
nkeynes@736
   103
        uint8_t unknown2[3]; /* Always 00 00 01? */
nkeynes@736
   104
        uint32_t pregap __attribute__((packed));
nkeynes@736
   105
        uint32_t offset __attribute__((packed));
nkeynes@736
   106
        uint32_t end __attribute__((packed));
nkeynes@138
   107
    } track[0];
nkeynes@138
   108
} __attribute__((packed));
nkeynes@138
   109
nkeynes@514
   110
struct nrg_daox {
nkeynes@514
   111
    uint32_t length;
nkeynes@514
   112
    char mcn[14];
nkeynes@514
   113
    uint8_t disc_mode;
nkeynes@514
   114
    uint8_t unknown[2]; /* always 01 01? */
nkeynes@514
   115
    uint8_t track_count;
nkeynes@514
   116
    struct nrg_daox_track {
nkeynes@736
   117
        char unknown[10];
nkeynes@736
   118
        uint32_t sector_size __attribute__((packed)); /* Always 0? */
nkeynes@736
   119
        uint8_t mode;
nkeynes@736
   120
        uint8_t unknown2[3]; /* Always 00 00 01? */
nkeynes@736
   121
        uint64_t pregap __attribute__((packed));
nkeynes@736
   122
        uint64_t offset __attribute__((packed));
nkeynes@736
   123
        uint64_t end __attribute__((packed));
nkeynes@514
   124
    } track[0];
nkeynes@514
   125
} __attribute__((packed));
nkeynes@514
   126
nkeynes@138
   127
nkeynes@1097
   128
sector_mode_t static nrg_track_mode( uint8_t mode )
nkeynes@138
   129
{
nkeynes@138
   130
    switch( mode ) {
nkeynes@1097
   131
    case 0: return SECTOR_MODE1;
nkeynes@1097
   132
    case 2: return SECTOR_MODE2_FORM1;
nkeynes@1097
   133
    case 3: return SECTOR_SEMIRAW_MODE2;
nkeynes@1097
   134
    case 7: return SECTOR_CDDA;
nkeynes@1178
   135
    case 16: return SECTOR_CDDA_SUBCHANNEL;
nkeynes@1298
   136
    default: return SECTOR_UNKNOWN;
nkeynes@138
   137
    }
nkeynes@138
   138
}
nkeynes@138
   139
nkeynes@168
   140
static gboolean nrg_image_is_valid( FILE *f )
nkeynes@138
   141
{
nkeynes@168
   142
    union nrg_footer footer;
nkeynes@168
   143
nkeynes@168
   144
    fseek( f, -12, SEEK_END );
nkeynes@168
   145
    fread( &footer, sizeof(footer), 1, f );
nkeynes@514
   146
    if( GUINT32_FROM_BE(footer.v50.id) == NERO_V50_ID ||
nkeynes@736
   147
            GUINT32_FROM_BE(footer.v55.id) == NERO_V55_ID ) {
nkeynes@736
   148
        return TRUE;
nkeynes@168
   149
    } else {
nkeynes@736
   150
        return FALSE;
nkeynes@168
   151
    }
nkeynes@168
   152
}
nkeynes@168
   153
nkeynes@1109
   154
#define RETURN_PARSE_ERROR( ... ) do { SET_ERROR(err, LX_ERR_FILE_INVALID, __VA_ARGS__); return FALSE; } while(0)
nkeynes@1097
   155
nkeynes@1097
   156
static gboolean nrg_image_read_toc( cdrom_disc_t disc, ERROR *err )
nkeynes@168
   157
{
nkeynes@138
   158
    union nrg_footer footer;
nkeynes@138
   159
    struct nrg_chunk chunk;
nkeynes@138
   160
    struct nrg_daoi *dao;
nkeynes@514
   161
    struct nrg_daox *daox;
nkeynes@514
   162
    struct nrg_etnf *etnf;
nkeynes@514
   163
    struct nrg_etn2 *etn2;
nkeynes@138
   164
    gboolean end = FALSE;
nkeynes@514
   165
    uint32_t chunk_id;
nkeynes@1097
   166
    int session_id = 1;
nkeynes@138
   167
    int session_track_id = 0;
nkeynes@138
   168
    int track_id = 0;
nkeynes@138
   169
    int cue_track_id = 0, cue_track_count = 0;
nkeynes@514
   170
    int i, count;
nkeynes@138
   171
nkeynes@1097
   172
    FILE *f = cdrom_disc_get_base_file(disc);
nkeynes@1097
   173
nkeynes@138
   174
    fseek( f, -12, SEEK_END );
nkeynes@138
   175
    fread( &footer, sizeof(footer), 1, f );
nkeynes@1178
   176
    uint32_t start = 0;
nkeynes@514
   177
    if( GUINT32_FROM_BE(footer.v50.id) == NERO_V50_ID ) {
nkeynes@1178
   178
        start = GUINT32_FROM_BE(footer.v50.offset);
nkeynes@514
   179
    } else if( GUINT32_FROM_BE(footer.v55.id) == NERO_V55_ID ) {
nkeynes@1178
   180
        start = (uint32_t)GUINT64_FROM_BE(footer.v55.offset);
nkeynes@138
   181
    } else {
nkeynes@1097
   182
        /* Not a (recognized) Nero image (should never happen) */
nkeynes@1097
   183
        RETURN_PARSE_ERROR("File is not an NRG image" );
nkeynes@138
   184
    }
nkeynes@1178
   185
    if( fseek( f, start, SEEK_SET) != 0 ) {
nkeynes@1178
   186
        RETURN_PARSE_ERROR("File is not a valid NRG image" );
nkeynes@1178
   187
    }
nkeynes@138
   188
nkeynes@138
   189
    do {
nkeynes@736
   190
        fread( &chunk, sizeof(chunk), 1, f );
nkeynes@736
   191
        chunk.length = GUINT32_FROM_BE(chunk.length);
nkeynes@736
   192
        char data[chunk.length];
nkeynes@736
   193
        fread( data, chunk.length, 1, f );
nkeynes@736
   194
        chunk_id = GUINT32_FROM_BE(chunk.id);
nkeynes@736
   195
        switch( chunk_id ) {
nkeynes@736
   196
        case CUES_ID:
nkeynes@736
   197
        case CUEX_ID:
nkeynes@736
   198
            cue_track_id = track_id;
nkeynes@736
   199
            cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
nkeynes@736
   200
            track_id += cue_track_count;
nkeynes@736
   201
            for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
nkeynes@736
   202
                struct nrg_cues *cue = (struct nrg_cues *)(data+i);
nkeynes@736
   203
                int track = 0;
nkeynes@736
   204
                uint32_t lba;
nkeynes@736
   205
                if( chunk_id == CUEX_ID ) {
nkeynes@1097
   206
                    lba = GUINT32_FROM_BE( cue->addr );
nkeynes@736
   207
                } else {
nkeynes@1097
   208
                    lba = BCD_MSFTOLBA( cue->addr );
nkeynes@736
   209
                }
nkeynes@736
   210
                if( cue->track == 0 )
nkeynes@736
   211
                    continue; /* Track 0. Leadin? always 0? */
nkeynes@736
   212
                if( cue->track == 0xAA ) { /* end of disc */
nkeynes@1097
   213
                    disc->leadout = lba;
nkeynes@736
   214
                } else {
nkeynes@1097
   215
                    track = BCDTOU8(cue->track) - 1;
nkeynes@1097
   216
                    if( (cue->control & 0x01) != 0 ) {
nkeynes@1097
   217
                        /* Track-start address */
nkeynes@1023
   218
                        disc->track[track].lba = lba;
nkeynes@1023
   219
                        disc->track[track].flags = cue->type;
nkeynes@736
   220
                    }
nkeynes@736
   221
                }
nkeynes@736
   222
            }
nkeynes@736
   223
            break;
nkeynes@736
   224
        case DAOI_ID:
nkeynes@736
   225
            dao = (struct nrg_daoi *)data;
nkeynes@1097
   226
            count = dao->track_count - cue_track_id;
nkeynes@1023
   227
            memcpy( disc->mcn, dao->mcn, 13 );
nkeynes@1023
   228
            disc->mcn[13] = '\0';
nkeynes@1097
   229
            if( dao->track_count != track_id ||
nkeynes@1097
   230
                count * 30 + 22 != chunk.length ) {
nkeynes@1097
   231
                RETURN_PARSE_ERROR( "Invalid NRG image file (bad DAOI block)" );
nkeynes@1097
   232
            }
nkeynes@1097
   233
            for( i=0; i<count; i++ ) {
nkeynes@1097
   234
                uint32_t offset = GUINT32_FROM_BE(dao->track[i].offset);
nkeynes@1097
   235
                sector_mode_t mode = nrg_track_mode( dao->track[i].mode );
nkeynes@1298
   236
                if( mode == SECTOR_UNKNOWN ) {
nkeynes@1097
   237
                    RETURN_PARSE_ERROR("Unknown track mode in NRG image file (%d)", dao->track[i].mode);
nkeynes@1097
   238
                }
nkeynes@1097
   239
                if( CDROM_SECTOR_SIZE(mode) != GUINT32_FROM_BE(dao->track[i].sector_size) ) {
nkeynes@1097
   240
                    /* Sector size mismatch */
nkeynes@1097
   241
                    RETURN_PARSE_ERROR("Invalid NRG image file (Bad sector size in DAOI block)");
nkeynes@1097
   242
                }
nkeynes@1097
   243
                cdrom_count_t sector_count =
nkeynes@736
   244
                    (GUINT32_FROM_BE(dao->track[i].end) - GUINT32_FROM_BE(dao->track[i].offset))/
nkeynes@1097
   245
                    CDROM_SECTOR_SIZE(mode);
nkeynes@1097
   246
                disc->track[cue_track_id].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
nkeynes@736
   247
                cue_track_id++;
nkeynes@736
   248
            }
nkeynes@736
   249
            break;
nkeynes@736
   250
        case DAOX_ID:
nkeynes@736
   251
            daox = (struct nrg_daox *)data;
nkeynes@1097
   252
            count = daox->track_count - cue_track_id;
nkeynes@1023
   253
            memcpy( disc->mcn, daox->mcn, 13 );
nkeynes@1023
   254
            disc->mcn[13] = '\0';
nkeynes@1097
   255
            if( daox->track_count != track_id ||
nkeynes@1097
   256
                count * 42 + 22 != chunk.length ) {
nkeynes@1097
   257
                RETURN_PARSE_ERROR( "Invalid NRG image file (bad DAOX block)" );
nkeynes@1097
   258
            }
nkeynes@1097
   259
            for( i=0; i<count; i++ ) {
nkeynes@1097
   260
                uint32_t offset = (uint32_t)GUINT64_FROM_BE(daox->track[i].offset);
nkeynes@1097
   261
                sector_mode_t mode = nrg_track_mode( daox->track[i].mode );
nkeynes@1298
   262
                if( mode == SECTOR_UNKNOWN ) {
nkeynes@1097
   263
                    RETURN_PARSE_ERROR("Unknown track mode in NRG image file (%d)", daox->track[i].mode);
nkeynes@1097
   264
                }
nkeynes@1097
   265
                if( CDROM_SECTOR_SIZE(mode) != GUINT32_FROM_BE(daox->track[i].sector_size) ) {
nkeynes@1097
   266
                    /* Sector size mismatch */
nkeynes@1097
   267
                    RETURN_PARSE_ERROR("Invalid NRG image file (Bad sector size in DAOX block)");
nkeynes@1097
   268
                }
nkeynes@1097
   269
                cdrom_count_t sector_count = (cdrom_count_t)
nkeynes@1097
   270
                    ((GUINT64_FROM_BE(daox->track[i].end) - GUINT64_FROM_BE(daox->track[i].offset))/
nkeynes@1097
   271
                    CDROM_SECTOR_SIZE(mode));
nkeynes@1097
   272
                disc->track[cue_track_id].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
nkeynes@736
   273
                cue_track_id++;
nkeynes@736
   274
            }
nkeynes@736
   275
            break;
nkeynes@514
   276
nkeynes@736
   277
        case SINF_ID: 
nkeynes@736
   278
            /* Data is a single 32-bit number representing number of tracks in session */
nkeynes@736
   279
            i = GUINT32_FROM_BE( *(uint32_t *)data );
nkeynes@736
   280
            while( i-- > 0 )
nkeynes@1097
   281
                disc->track[session_track_id++].sessionno = session_id;
nkeynes@736
   282
            session_id++;
nkeynes@736
   283
            break;
nkeynes@736
   284
        case ETNF_ID:
nkeynes@736
   285
            etnf = (struct nrg_etnf *)data;
nkeynes@736
   286
            count = chunk.length / sizeof(struct nrg_etnf);
nkeynes@736
   287
            for( i=0; i < count; i++, etnf++ ) {
nkeynes@1097
   288
                uint32_t offset = GUINT32_FROM_BE(etnf->offset);
nkeynes@1097
   289
                sector_mode_t mode = nrg_track_mode( GUINT32_FROM_BE(etnf->mode) );
nkeynes@1298
   290
                if( mode == SECTOR_UNKNOWN ) {
nkeynes@1097
   291
                    RETURN_PARSE_ERROR("Unknown track mode in NRG image file (%d)", etnf->mode);
nkeynes@736
   292
                }
nkeynes@1097
   293
                cdrom_count_t sector_count = GUINT32_FROM_BE(etnf->length) /
nkeynes@1097
   294
                        CDROM_SECTOR_SIZE(mode);
nkeynes@1097
   295
nkeynes@1097
   296
                disc->track[track_id].lba = GUINT32_FROM_BE(etnf->lba) + i*CDROM_PREGAP;
nkeynes@1097
   297
                if( mode == SECTOR_CDDA )
nkeynes@1023
   298
                    disc->track[track_id].flags = 0x01;
nkeynes@736
   299
                else
nkeynes@1097
   300
                    disc->track[track_id].flags = 0x01 | TRACK_FLAG_DATA;
nkeynes@1097
   301
                disc->track[track_id].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
nkeynes@736
   302
                track_id++;
nkeynes@736
   303
            }
nkeynes@736
   304
            break;
nkeynes@736
   305
        case ETN2_ID:
nkeynes@736
   306
            etn2 = (struct nrg_etn2 *)data;
nkeynes@736
   307
            count = chunk.length / sizeof(struct nrg_etn2);
nkeynes@736
   308
            for( i=0; i < count; i++, etn2++ ) {
nkeynes@1097
   309
                uint32_t offset = (uint32_t)GUINT64_FROM_BE(etn2->offset);
nkeynes@1097
   310
                sector_mode_t mode = nrg_track_mode( GUINT32_FROM_BE(etn2->mode) );
nkeynes@1298
   311
                if( mode == SECTOR_UNKNOWN ) {
nkeynes@1097
   312
                    RETURN_PARSE_ERROR("Unknown track mode in NRG image file (%d)", etn2->mode);
nkeynes@736
   313
                }
nkeynes@1097
   314
                cdrom_count_t sector_count = (uint32_t)(GUINT64_FROM_BE(etn2->length) /
nkeynes@1097
   315
                        CDROM_SECTOR_SIZE(mode));
nkeynes@1097
   316
nkeynes@1097
   317
                disc->track[track_id].lba = GUINT32_FROM_BE(etn2->lba) + i*CDROM_PREGAP;
nkeynes@1097
   318
                if( mode == SECTOR_CDDA )
nkeynes@1023
   319
                    disc->track[track_id].flags = 0x01;
nkeynes@736
   320
                else
nkeynes@1097
   321
                    disc->track[track_id].flags = 0x01 | TRACK_FLAG_DATA;
nkeynes@1097
   322
                disc->track[track_id].source = file_sector_source_new_source( disc->base_source, mode, offset, sector_count );
nkeynes@736
   323
                track_id++;
nkeynes@736
   324
            }
nkeynes@736
   325
            break;
nkeynes@736
   326
nkeynes@736
   327
        case END_ID:
nkeynes@736
   328
            end = TRUE;
nkeynes@736
   329
            break;
nkeynes@736
   330
        }
nkeynes@138
   331
    } while( !end );
nkeynes@1097
   332
nkeynes@1023
   333
    disc->track_count = track_id;
nkeynes@1097
   334
    disc->session_count = session_id-1;
nkeynes@1097
   335
    return TRUE;
nkeynes@138
   336
}
.