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 Mon Jun 26 10:30:42 2006 +0000 (14 years ago)
permissions -rw-r--r--
last change Implement multi-format CD image support
Fix CDI support up to (more or less) work
file annotate diff log raw
nkeynes@138
     1
/**
nkeynes@168
     2
 * $Id: nrg.c,v 1.3 2006-06-26 10:30:42 nkeynes Exp $
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@138
    23
#include "gdrom/gdrom.h"
nkeynes@138
    24
#include "dream.h"
nkeynes@138
    25
nkeynes@168
    26
static gboolean nrg_image_is_valid( FILE *f );
nkeynes@168
    27
static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f );
nkeynes@168
    28
nkeynes@168
    29
struct gdrom_image_class nrg_image_class = { "Nero", "nrg", 
nkeynes@168
    30
					     nrg_image_is_valid, nrg_image_open };
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@138
    54
	uint32_t dummy;
nkeynes@138
    55
	uint32_t id;
nkeynes@138
    56
	uint32_t offset;
nkeynes@138
    57
    } v50;
nkeynes@138
    58
    struct nrg_footer_v55 {
nkeynes@138
    59
	uint32_t id;
nkeynes@138
    60
	uint64_t offset;
nkeynes@138
    61
    } 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@138
    77
struct nrg_cues {
nkeynes@138
    78
    uint8_t type;
nkeynes@138
    79
    uint8_t track;
nkeynes@138
    80
    uint8_t control;
nkeynes@138
    81
    uint8_t pad;
nkeynes@138
    82
    uint32_t addr;
nkeynes@138
    83
};
nkeynes@138
    84
nkeynes@138
    85
struct nrg_daoi {
nkeynes@138
    86
    uint32_t length;
nkeynes@138
    87
    char mcn[14];
nkeynes@138
    88
    uint8_t disc_mode;
nkeynes@138
    89
    uint8_t unknown[2]; /* always 01 01? */
nkeynes@138
    90
    uint8_t track_count;
nkeynes@138
    91
    struct nrg_daoi_track {
nkeynes@138
    92
	char unknown[10];
nkeynes@138
    93
	uint32_t sector_size __attribute__((packed)); /* Always 0? */
nkeynes@138
    94
	uint8_t mode;
nkeynes@138
    95
	uint8_t unknown2[3]; /* Always 00 00 01? */
nkeynes@138
    96
	uint32_t pregap __attribute__((packed));
nkeynes@138
    97
	uint32_t offset __attribute__((packed));
nkeynes@138
    98
	uint32_t end __attribute__((packed));
nkeynes@138
    99
    } track[0];
nkeynes@138
   100
} __attribute__((packed));
nkeynes@138
   101
nkeynes@138
   102
/**
nkeynes@138
   103
 * Convert an 8-bit BCD number to normal integer form. 
nkeynes@138
   104
 * Eg, 0x79 => 79
nkeynes@138
   105
 */
nkeynes@138
   106
uint8_t static bcd_to_uint8( uint8_t bcd )
nkeynes@138
   107
{
nkeynes@138
   108
    return (bcd & 0x0F) + (((bcd & 0xF0)>>4)*10);
nkeynes@138
   109
}
nkeynes@138
   110
nkeynes@138
   111
nkeynes@138
   112
/**
nkeynes@138
   113
 * Convert a 32 bit MSF address (BCD coded) to the
nkeynes@138
   114
 * equivalent LBA form. 
nkeynes@138
   115
 * Eg, 0x
nkeynes@138
   116
 */
nkeynes@138
   117
uint32_t static msf_to_lba( uint32_t msf )
nkeynes@138
   118
{
nkeynes@138
   119
    msf = ntohl(msf);
nkeynes@138
   120
    int f = bcd_to_uint8(msf);
nkeynes@138
   121
    int s = bcd_to_uint8(msf>>8);
nkeynes@138
   122
    int m = bcd_to_uint8(msf>>16);
nkeynes@138
   123
    return (m * 60 + s) * 75 + f;
nkeynes@138
   124
nkeynes@138
   125
}
nkeynes@138
   126
nkeynes@138
   127
uint32_t static nrg_track_mode( uint8_t mode )
nkeynes@138
   128
{
nkeynes@138
   129
    switch( mode ) {
nkeynes@138
   130
    case 0: return GDROM_MODE1;
nkeynes@138
   131
    case 2: return GDROM_MODE2_XA1;
nkeynes@138
   132
    case 3: return GDROM_MODE2;
nkeynes@138
   133
    case 7: return GDROM_CDDA;
nkeynes@138
   134
    default: 
nkeynes@138
   135
	ERROR( "Unrecognized track mode %d in Nero image", mode );
nkeynes@138
   136
	return -1;
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@168
   146
    if( ntohl(footer.v50.id) == NERO_V50_ID ) {
nkeynes@168
   147
	return TRUE;
nkeynes@168
   148
    } else {
nkeynes@168
   149
	return FALSE;
nkeynes@168
   150
    }
nkeynes@168
   151
}
nkeynes@168
   152
nkeynes@168
   153
static gdrom_disc_t nrg_image_open( const gchar *filename, FILE *f )
nkeynes@168
   154
{
nkeynes@138
   155
    union nrg_footer footer;
nkeynes@138
   156
    struct nrg_chunk chunk;
nkeynes@138
   157
    struct nrg_daoi *dao;
nkeynes@138
   158
    gdrom_disc_t disc;
nkeynes@138
   159
    gboolean end = FALSE;
nkeynes@138
   160
    int session_id = 0;
nkeynes@138
   161
    int session_track_id = 0;
nkeynes@138
   162
    int track_id = 0;
nkeynes@138
   163
    int cue_track_id = 0, cue_track_count = 0;
nkeynes@138
   164
    int i;
nkeynes@138
   165
nkeynes@138
   166
    fseek( f, -12, SEEK_END );
nkeynes@138
   167
    fread( &footer, sizeof(footer), 1, f );
nkeynes@138
   168
    if( ntohl(footer.v50.id) == NERO_V50_ID ) {
nkeynes@138
   169
	INFO( "Loading Nero 5.0 image" );
nkeynes@138
   170
	fseek( f, ntohl(footer.v50.offset), SEEK_SET );
nkeynes@138
   171
    } else if( ntohl(footer.v55.id) == NERO_V55_ID ) {
nkeynes@138
   172
	INFO( "Loading Nero 5.5+ image" );
nkeynes@138
   173
	fseek( f, ntohl(footer.v55.offset), SEEK_SET );
nkeynes@138
   174
    } else {
nkeynes@168
   175
	/* Not a (recognized) Nero image */
nkeynes@138
   176
	return NULL;
nkeynes@138
   177
    }
nkeynes@138
   178
    
nkeynes@138
   179
    disc = gdrom_image_new(f);
nkeynes@138
   180
    if( disc == NULL ) {
nkeynes@138
   181
	ERROR("Unable to allocate memory!");
nkeynes@138
   182
	return NULL;
nkeynes@138
   183
    }
nkeynes@138
   184
nkeynes@138
   185
    do {
nkeynes@138
   186
	fread( &chunk, sizeof(chunk), 1, f );
nkeynes@138
   187
	chunk.length = ntohl(chunk.length);
nkeynes@138
   188
	char data[chunk.length];
nkeynes@138
   189
	fread( data, chunk.length, 1, f );
nkeynes@138
   190
	switch( ntohl(chunk.id) ) {
nkeynes@138
   191
	case CUES_ID:
nkeynes@138
   192
	    cue_track_id = track_id;
nkeynes@138
   193
	    cue_track_count = ((chunk.length / sizeof(struct nrg_cues)) >> 1) - 1;
nkeynes@138
   194
	    track_id += cue_track_count;
nkeynes@138
   195
	    for( i=0; i<chunk.length; i+= sizeof(struct nrg_cues) ) {
nkeynes@138
   196
		struct nrg_cues *cue = (struct nrg_cues *)(data+i);
nkeynes@138
   197
		int track = 0;
nkeynes@138
   198
		if( cue->track == 0 )
nkeynes@138
   199
		    continue; /* Track 0. Leadin? always 0? */
nkeynes@138
   200
		if( cue->track == 0xAA ) { /* end of disc */
nkeynes@138
   201
		    disc->track[track_id-1].sector_count =
nkeynes@138
   202
			msf_to_lba( cue->addr ) - disc->track[track_id-1].lba;
nkeynes@138
   203
		} else {
nkeynes@138
   204
		    track = cue_track_id + bcd_to_uint8(cue->track) - 1;
nkeynes@138
   205
		    if( (cue->control & 0x01) == 0 ) { 
nkeynes@138
   206
			/* Pre-gap address. */
nkeynes@138
   207
			if( track != 0 ) {
nkeynes@138
   208
			    disc->track[track-1].sector_count = 
nkeynes@138
   209
				msf_to_lba( cue->addr ) - disc->track[track-1].lba;
nkeynes@138
   210
			}
nkeynes@138
   211
		    } else { /* Track-start address */
nkeynes@138
   212
			disc->track[track].lba = msf_to_lba( cue->addr );
nkeynes@142
   213
			disc->track[track].flags = cue->type;
nkeynes@138
   214
		    }
nkeynes@138
   215
		}
nkeynes@138
   216
	    }
nkeynes@138
   217
	    break;
nkeynes@138
   218
	case DAOI_ID:
nkeynes@138
   219
	    dao = (struct nrg_daoi *)data;
nkeynes@138
   220
	    memcpy( disc->mcn, dao->mcn, 13 );
nkeynes@138
   221
	    disc->mcn[13] = '\0';
nkeynes@138
   222
	    assert( dao->track_count * 30 + 22 == chunk.length );
nkeynes@138
   223
	    assert( dao->track_count == cue_track_count );
nkeynes@138
   224
	    for( i=0; i<dao->track_count; i++ ) {
nkeynes@138
   225
		disc->track[cue_track_id].sector_size = ntohl(dao->track[i].sector_size);
nkeynes@138
   226
		disc->track[cue_track_id].offset = ntohl(dao->track[i].offset);
nkeynes@138
   227
		disc->track[cue_track_id].mode = nrg_track_mode( dao->track[i].mode );
nkeynes@138
   228
		assert( disc->track[cue_track_id].sector_count == 
nkeynes@138
   229
			(ntohl(dao->track[i].end) - ntohl(dao->track[i].offset))/
nkeynes@138
   230
			ntohl(dao->track[i].sector_size) );
nkeynes@138
   231
		cue_track_id++;
nkeynes@138
   232
	    }
nkeynes@138
   233
	    break;
nkeynes@138
   234
	case SINF_ID: 
nkeynes@138
   235
	    /* Data is a single 32-bit number representing number of tracks in session */
nkeynes@138
   236
	    i = ntohl( *(uint32_t *)data );
nkeynes@138
   237
	    while( i-- > 0 )
nkeynes@138
   238
		disc->track[session_track_id++].session = session_id;
nkeynes@138
   239
	    session_id++;
nkeynes@138
   240
	    break;
nkeynes@138
   241
	case ETNF_ID:
nkeynes@138
   242
	    for( i=0; i < chunk.length; i+= 0x14 ) {
nkeynes@138
   243
		struct nrg_etnf *etnf = (struct nrg_etnf *)(data+i);
nkeynes@138
   244
		disc->track[track_id].offset = ntohl(etnf->offset);
nkeynes@138
   245
		disc->track[track_id].lba = ntohl(etnf->lba) + (i+1)*GDROM_PREGAP;
nkeynes@138
   246
		disc->track[track_id].mode = nrg_track_mode( ntohl(etnf->mode) );
nkeynes@138
   247
		if( disc->track[track_id].mode == -1 ) {
nkeynes@138
   248
		    disc->close(disc);
nkeynes@138
   249
		    return NULL;
nkeynes@138
   250
		}
nkeynes@142
   251
		if( disc->track[track_id].mode == GDROM_CDDA )
nkeynes@142
   252
		    disc->track[track_id].flags = 0x01;
nkeynes@142
   253
		else
nkeynes@142
   254
		    disc->track[track_id].flags = 0x01 | TRACK_DATA;
nkeynes@138
   255
		disc->track[track_id].sector_size = GDROM_SECTOR_SIZE(disc->track[track_id].mode);
nkeynes@138
   256
		disc->track[track_id].sector_count = ntohl(etnf->length) / 
nkeynes@138
   257
		    disc->track[track_id].sector_size;
nkeynes@138
   258
		track_id++;
nkeynes@138
   259
	    }
nkeynes@138
   260
	    break;
nkeynes@138
   261
	case END_ID:
nkeynes@138
   262
	    end = TRUE;
nkeynes@138
   263
	    break;
nkeynes@138
   264
	}
nkeynes@138
   265
    } while( !end );
nkeynes@138
   266
    disc->track_count = track_id;
nkeynes@138
   267
    return disc;
nkeynes@138
   268
}
nkeynes@138
   269
nkeynes@138
   270
gboolean nrg_read_sectors( struct gdrom_disc *disc, uint32_t lba, uint32_t count, char *buf )
nkeynes@138
   271
{
nkeynes@138
   272
    return FALSE;
nkeynes@138
   273
}
nkeynes@138
   274
.