Search
lxdream.org :: lxdream/src/drivers/cdrom/cd_gdi.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/cd_gdi.c
changeset 1109:700c5ab26a63
prev1097:d4807997e450
next1286:8376a612a79d
author nkeynes
date Thu Jun 10 22:13:16 2010 +1000 (12 years ago)
permissions -rw-r--r--
last change Integrate executable wrapping into the user interface
- command-line now loads wrapped by default, -e <bin> to run binary
- add support for .bin executables
- Add useful (internal) error codes
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@1097
    27
#include <glib/gstrfuncs.h>
nkeynes@1097
    28
#include "drivers/cdrom/cdimpl.h"
nkeynes@492
    29
nkeynes@492
    30
nkeynes@492
    31
static gboolean gdi_image_is_valid( FILE *f );
nkeynes@1097
    32
static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err );
nkeynes@492
    33
nkeynes@1097
    34
struct cdrom_disc_factory gdi_disc_factory = { "NullDC GD-Rom Image", "gdi",
nkeynes@1097
    35
        gdi_image_is_valid, NULL, gdi_image_read_toc };
nkeynes@492
    36
nkeynes@492
    37
static gboolean gdi_image_is_valid( FILE *f )
nkeynes@492
    38
{
nkeynes@492
    39
    char line[512];
nkeynes@492
    40
    uint32_t track_count;
nkeynes@736
    41
nkeynes@492
    42
    fseek(f, 0, SEEK_SET);
nkeynes@492
    43
    if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@736
    44
        return FALSE;
nkeynes@492
    45
    }
nkeynes@492
    46
    track_count = strtoul(line, NULL, 0);
nkeynes@492
    47
    if( track_count == 0 || track_count > 99 ) {
nkeynes@736
    48
        return FALSE;
nkeynes@492
    49
    }
nkeynes@492
    50
    return TRUE;
nkeynes@492
    51
}
nkeynes@492
    52
nkeynes@1097
    53
static gboolean gdi_image_read_toc( cdrom_disc_t disc, ERROR *err )
nkeynes@492
    54
{
nkeynes@492
    55
    int i;
nkeynes@492
    56
    uint32_t track_count;
nkeynes@492
    57
    struct stat st;
nkeynes@492
    58
    char line[512];
nkeynes@1097
    59
    int session = 1;
nkeynes@492
    60
    gchar *dirname;
nkeynes@492
    61
nkeynes@1097
    62
    FILE *f = cdrom_disc_get_base_file(disc);
nkeynes@1097
    63
nkeynes@492
    64
    fseek(f, 0, SEEK_SET);
nkeynes@736
    65
nkeynes@492
    66
    if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@1109
    67
        SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
nkeynes@736
    68
        return FALSE;
nkeynes@492
    69
    }
nkeynes@492
    70
    track_count = strtoul(line, NULL, 0);
nkeynes@492
    71
    if( track_count == 0 || track_count > 99 ) {
nkeynes@1109
    72
        SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image" );
nkeynes@1097
    73
        return FALSE;
nkeynes@492
    74
    }
nkeynes@492
    75
nkeynes@1097
    76
    dirname = g_path_get_dirname(disc->name);
nkeynes@1097
    77
    disc->disc_type = CDROM_DISC_GDROM;
nkeynes@1023
    78
    disc->track_count = track_count;
nkeynes@1097
    79
    disc->session_count = 2;
nkeynes@492
    80
    for( i=0; i<track_count; i++ ) {
nkeynes@736
    81
        int track_no, start_lba, flags, size, offset;
nkeynes@736
    82
        char filename[256];
nkeynes@492
    83
nkeynes@736
    84
        if( fgets( line, sizeof(line), f ) == NULL ) {
nkeynes@1097
    85
            cdrom_disc_unref(disc);
nkeynes@1109
    86
            SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid GDI image - unexpected end of file" );
nkeynes@1097
    87
            return FALSE;
nkeynes@736
    88
        }
nkeynes@736
    89
        sscanf( line, "%d %d %d %d %s %d", &track_no, &start_lba, &flags, &size,
nkeynes@736
    90
                filename, &offset );
nkeynes@736
    91
        if( start_lba >= 45000 ) {
nkeynes@1097
    92
            session = 2;
nkeynes@736
    93
        }
nkeynes@1097
    94
        disc->track[i].sessionno = session;
nkeynes@1097
    95
        disc->track[i].lba = start_lba;
nkeynes@1023
    96
        disc->track[i].flags = (flags & 0x0F)<<4;
nkeynes@1097
    97
nkeynes@1097
    98
        sector_mode_t mode;
nkeynes@1097
    99
        if( disc->track[i].flags & TRACK_FLAG_DATA ) {
nkeynes@1097
   100
            /* Data track */
nkeynes@1097
   101
            switch(size) {
nkeynes@1097
   102
            case 0:    mode = SECTOR_MODE2_FORM1; break; /* Default */
nkeynes@1097
   103
            case 2048: mode = SECTOR_MODE2_FORM1; break;
nkeynes@1097
   104
            case 2324: mode = SECTOR_MODE2_FORM2; break;
nkeynes@1097
   105
            case 2336: mode = SECTOR_SEMIRAW_MODE2; break;
nkeynes@1097
   106
            case 2352: mode = SECTOR_RAW_XA; break;
nkeynes@1097
   107
            default:
nkeynes@1109
   108
                SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' in GDI track %d", size, (i+1) );
nkeynes@1097
   109
                g_free(dirname);
nkeynes@1097
   110
                return FALSE;
nkeynes@1097
   111
            }
nkeynes@1097
   112
        } else {
nkeynes@1097
   113
            /* Audio track */
nkeynes@1097
   114
            mode = SECTOR_CDDA;
nkeynes@1097
   115
            if( size == 0 )
nkeynes@1097
   116
                size = 2352;
nkeynes@1097
   117
            else if( size != 2352 ) {
nkeynes@1109
   118
                SET_ERROR( err, LX_ERR_FILE_INVALID, "Invalid sector size '%d' for audio track %d", size, (i+1) );
nkeynes@1097
   119
                g_free(dirname);
nkeynes@1097
   120
                return FALSE;
nkeynes@1097
   121
            }
nkeynes@1097
   122
        }
nkeynes@736
   123
        if( strcasecmp( filename, "none" ) == 0 ) {
nkeynes@1097
   124
            disc->track[i].source = null_sector_source_new( mode, 0 );
nkeynes@736
   125
        } else {
nkeynes@736
   126
            gchar *pathname = g_strdup_printf( "%s%c%s", dirname, G_DIR_SEPARATOR, filename );
nkeynes@1097
   127
            disc->track[i].source = file_sector_source_new_filename( pathname, mode,
nkeynes@1097
   128
                    offset, FILE_SECTOR_FULL_FILE );
nkeynes@736
   129
            g_free(pathname);
nkeynes@1097
   130
            if( disc->track[i].source == NULL ) {
nkeynes@1109
   131
                /* Note: status is invalid because it's a failure of the GDI file */
nkeynes@1109
   132
                SET_ERROR( err, LX_ERR_FILE_INVALID, "GDI track file '%s' could not be opened (%s)", filename, strerror(errno) );
nkeynes@736
   133
                g_free(dirname);
nkeynes@1097
   134
                return FALSE;
nkeynes@736
   135
            }
nkeynes@736
   136
        }
nkeynes@492
   137
    }
nkeynes@492
   138
    g_free(dirname);
nkeynes@1097
   139
    return TRUE;
nkeynes@492
   140
}
.