Search
lxdream.org :: lxdream/src/gdrom/gdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdi.c
changeset 836:d314bf370949
prev759:f16975739abc
next1023:264e2fd90be8
author nkeynes
date Thu Aug 28 00:08:12 2008 +0000 (12 years ago)
permissions -rw-r--r--
last change Change so all tracks after sector 45000 belong to session 1, even if they don't have a valid lba
file annotate diff log raw
nkeynes@492
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@492
     3
 *
nkeynes@492
     4
 * NullDC GDI image format
nkeynes@492
     5
 *
nkeynes@492
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@492
     7
 *
nkeynes@492
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@492
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@492
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@492
    11
 * (at your option) any later version.
nkeynes@492
    12
 *
nkeynes@492
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@492
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@492
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@492
    16
 * GNU General Public License for more details.
nkeynes@492
    17
 */
nkeynes@492
    18
nkeynes@492
    19
#include <stdlib.h>
nkeynes@492
    20
#include <stdio.h>
nkeynes@492
    21
#include <stdint.h>
nkeynes@759
    22
#include <string.h>
nkeynes@492
    23
#include <fcntl.h>
nkeynes@492
    24
#include <errno.h>
nkeynes@492
    25
#include <sys/stat.h>
nkeynes@492
    26
#include <glib/gutils.h>
nkeynes@678
    27
#include "gdrom/gddriver.h"
nkeynes@492
    28
nkeynes@492
    29
nkeynes@492
    30
static gboolean gdi_image_is_valid( FILE *f );
nkeynes@492
    31
static gdrom_disc_t gdi_image_open( const gchar *filename, FILE *f );
nkeynes@492
    32
nkeynes@492
    33
struct gdrom_image_class gdi_image_class = { "NullDC GD-Rom Image", "gdi", 
nkeynes@736
    34
        gdi_image_is_valid, gdi_image_open };
nkeynes@492
    35
nkeynes@492
    36
static gboolean gdi_image_is_valid( FILE *f )
nkeynes@492
    37
{
nkeynes@492
    38
    char line[512];
nkeynes@492
    39
    uint32_t track_count;
nkeynes@736
    40
nkeynes@492
    41
    fseek(f, 0, SEEK_SET);
nkeynes@492
    42
    if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@736
    43
        return FALSE;
nkeynes@492
    44
    }
nkeynes@492
    45
    track_count = strtoul(line, NULL, 0);
nkeynes@492
    46
    if( track_count == 0 || track_count > 99 ) {
nkeynes@736
    47
        return FALSE;
nkeynes@492
    48
    }
nkeynes@492
    49
    return TRUE;
nkeynes@492
    50
}
nkeynes@492
    51
nkeynes@492
    52
static gdrom_disc_t gdi_image_open( const gchar *filename, FILE *f )
nkeynes@492
    53
{
nkeynes@492
    54
    int i;
nkeynes@492
    55
    uint32_t track_count;
nkeynes@492
    56
    gdrom_disc_t disc;
nkeynes@492
    57
    gdrom_image_t image;
nkeynes@492
    58
    struct stat st;
nkeynes@492
    59
    char line[512];
nkeynes@836
    60
    int session = 0;
nkeynes@492
    61
    gchar *dirname;
nkeynes@492
    62
nkeynes@492
    63
    fseek(f, 0, SEEK_SET);
nkeynes@736
    64
nkeynes@492
    65
    if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@736
    66
        return FALSE;
nkeynes@492
    67
    }
nkeynes@492
    68
    track_count = strtoul(line, NULL, 0);
nkeynes@492
    69
    if( track_count == 0 || track_count > 99 ) {
nkeynes@736
    70
        return NULL;
nkeynes@492
    71
    }
nkeynes@492
    72
nkeynes@492
    73
    disc = gdrom_image_new(filename, f);
nkeynes@492
    74
    if( disc == NULL ) {
nkeynes@736
    75
        ERROR("Unable to allocate memory!");
nkeynes@736
    76
        return NULL;
nkeynes@492
    77
    }
nkeynes@492
    78
    dirname = g_path_get_dirname(filename);
nkeynes@492
    79
    image = (gdrom_image_t)disc;
nkeynes@492
    80
    image->disc_type = IDE_DISC_GDROM;
nkeynes@492
    81
    image->track_count = track_count;
nkeynes@492
    82
    for( i=0; i<track_count; i++ ) {
nkeynes@736
    83
        int track_no, start_lba, flags, size, offset;
nkeynes@736
    84
        char filename[256];
nkeynes@492
    85
nkeynes@736
    86
        if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@736
    87
            gdrom_image_destroy_no_close(disc);
nkeynes@736
    88
            return NULL;
nkeynes@736
    89
        }
nkeynes@736
    90
        sscanf( line, "%d %d %d %d %s %d", &track_no, &start_lba, &flags, &size,
nkeynes@736
    91
                filename, &offset );
nkeynes@736
    92
        if( start_lba >= 45000 ) {
nkeynes@836
    93
            session = 1;
nkeynes@736
    94
        }
nkeynes@836
    95
        image->track[i].session = session;
nkeynes@736
    96
        image->track[i].lba = start_lba + 150; // 2-second offset
nkeynes@736
    97
        image->track[i].flags = (flags & 0x0F)<<4;
nkeynes@736
    98
        image->track[i].sector_size = size;
nkeynes@736
    99
        if( strcasecmp( filename, "none" ) == 0 ) {
nkeynes@736
   100
            image->track[i].file = NULL;
nkeynes@736
   101
            image->track[i].sector_count = 0;
nkeynes@736
   102
            image->track[i].mode = GDROM_MODE1;
nkeynes@736
   103
        } else {
nkeynes@736
   104
            gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, filename );
nkeynes@736
   105
            image->track[i].file = fopen( pathname, "ro" );
nkeynes@736
   106
            g_free(pathname);
nkeynes@736
   107
            if( image->track[i].file == NULL ) {
nkeynes@736
   108
                gdrom_image_destroy_no_close(disc);
nkeynes@736
   109
                g_free(dirname);
nkeynes@736
   110
                return NULL;
nkeynes@736
   111
            }
nkeynes@736
   112
            fstat( fileno(image->track[i].file), &st );
nkeynes@736
   113
            image->track[i].sector_count = st.st_size / size;
nkeynes@736
   114
            if( image->track[i].flags & TRACK_DATA ) {
nkeynes@736
   115
                /* Data track */
nkeynes@736
   116
                switch(size) {
nkeynes@736
   117
                case 2048: image->track[i].mode = GDROM_MODE1; break;
nkeynes@736
   118
                case 2336: image->track[i].mode = GDROM_SEMIRAW_MODE2; break;
nkeynes@736
   119
                case 2352: image->track[i].mode = GDROM_RAW_XA; break;
nkeynes@736
   120
                default:
nkeynes@736
   121
                    gdrom_image_destroy_no_close(disc);
nkeynes@736
   122
                    g_free(dirname);
nkeynes@736
   123
                    return NULL;
nkeynes@736
   124
                }
nkeynes@736
   125
            } else {
nkeynes@736
   126
                /* Audio track */
nkeynes@736
   127
                image->track[i].mode = GDROM_CDDA;
nkeynes@736
   128
                if( size != 2352 ) {
nkeynes@736
   129
                    gdrom_image_destroy_no_close(disc);
nkeynes@736
   130
                    g_free(dirname);
nkeynes@736
   131
                    return NULL;
nkeynes@736
   132
                }
nkeynes@736
   133
            }
nkeynes@736
   134
        }
nkeynes@736
   135
        image->track[i].offset = offset;
nkeynes@492
   136
    }
nkeynes@492
   137
    g_free(dirname);
nkeynes@492
   138
    return disc;
nkeynes@492
   139
}
.