Search
lxdream.org :: lxdream/src/gdrom/gdrom.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdrom.c
changeset 142:2f631c3a3946
prev138:afabd7e6d26d
next143:9446fb6df0c5
author nkeynes
date Tue May 02 14:09:11 2006 +0000 (15 years ago)
permissions -rw-r--r--
last change Add packet.h
Implement read toc, request sense, test ready commands.
Fix failure to clear error status on new command
file annotate diff log raw
nkeynes@138
     1
/**
nkeynes@142
     2
 * $Id: gdrom.c,v 1.2 2006-05-02 14:09:11 nkeynes Exp $
nkeynes@138
     3
 *
nkeynes@138
     4
 * GD-Rom  access functions.
nkeynes@138
     5
 *
nkeynes@138
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@138
     7
 *
nkeynes@138
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@138
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@138
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@138
    11
 * (at your option) any later version.
nkeynes@138
    12
 *
nkeynes@138
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@138
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@138
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@138
    16
 * GNU General Public License for more details.
nkeynes@138
    17
 */
nkeynes@138
    18
nkeynes@138
    19
#include <stdio.h>
nkeynes@138
    20
nkeynes@138
    21
#include "gdrom/ide.h"
nkeynes@138
    22
#include "gdrom/gdrom.h"
nkeynes@138
    23
#include "dream.h"
nkeynes@138
    24
nkeynes@138
    25
static void gdrom_image_destroy( gdrom_disc_t );
nkeynes@138
    26
static uint32_t gdrom_image_read_sectors( gdrom_disc_t, uint32_t, uint32_t, char * );
nkeynes@138
    27
nkeynes@138
    28
nkeynes@138
    29
gdrom_disc_t gdrom_disc = NULL;
nkeynes@138
    30
nkeynes@138
    31
char *gdrom_mode_names[] = { "Mode1", "Mode2", "XA 1", "XA2", "Audio", "GD-Rom" };
nkeynes@138
    32
uint32_t gdrom_sector_size[] = { 2048, 2336, 2048, 2324, 2352, 2336 };
nkeynes@138
    33
nkeynes@138
    34
gdrom_disc_t gdrom_image_open( const gchar *filename )
nkeynes@138
    35
{
nkeynes@138
    36
    return nrg_image_open( filename );
nkeynes@138
    37
}
nkeynes@138
    38
nkeynes@138
    39
nkeynes@138
    40
gdrom_disc_t gdrom_image_new( FILE *file )
nkeynes@138
    41
{
nkeynes@138
    42
    struct gdrom_disc *disc = (struct gdrom_disc *)calloc(1, sizeof(struct gdrom_disc));
nkeynes@138
    43
    if( disc == NULL )
nkeynes@138
    44
	return NULL;
nkeynes@138
    45
    disc->read_sectors = gdrom_image_read_sectors;
nkeynes@138
    46
    disc->close = gdrom_image_destroy;
nkeynes@138
    47
    disc->disc_type = IDE_DISC_CDROM;
nkeynes@138
    48
    disc->file = file;
nkeynes@138
    49
    return disc;
nkeynes@138
    50
}
nkeynes@138
    51
nkeynes@138
    52
static void gdrom_image_destroy( gdrom_disc_t disc )
nkeynes@138
    53
{
nkeynes@138
    54
    if( disc->file != NULL ) {
nkeynes@138
    55
	fclose(disc->file);
nkeynes@138
    56
	disc->file = NULL;
nkeynes@138
    57
    }
nkeynes@138
    58
    free( disc );
nkeynes@138
    59
}
nkeynes@138
    60
nkeynes@138
    61
static uint32_t gdrom_image_read_sectors( gdrom_disc_t disc, uint32_t sector,
nkeynes@138
    62
				   uint32_t sector_count, char *buf )
nkeynes@138
    63
{
nkeynes@138
    64
    int i, track = -1, track_offset, read_len;
nkeynes@138
    65
nkeynes@138
    66
    for( i=0; i<disc->track_count; i++ ) {
nkeynes@138
    67
	if( disc->track[i].lba <= sector && 
nkeynes@142
    68
	    (sector + sector_count) <= (disc->track[i].lba + disc->track[i].sector_count) ) {
nkeynes@138
    69
	    track = i;
nkeynes@138
    70
	    break;
nkeynes@138
    71
	}
nkeynes@138
    72
    }
nkeynes@138
    73
    if( track == -1 )
nkeynes@138
    74
	return 0;
nkeynes@138
    75
    
nkeynes@138
    76
    track_offset = disc->track[track].sector_size * (sector - disc->track[track].lba);
nkeynes@138
    77
    read_len = disc->track[track].sector_size * sector_count;
nkeynes@138
    78
    fseek( disc->file, disc->track[track].offset + track_offset, SEEK_SET );
nkeynes@138
    79
    fread( buf, disc->track[track].sector_size, sector_count, disc->file );
nkeynes@138
    80
    return read_len;
nkeynes@138
    81
}
nkeynes@138
    82
nkeynes@138
    83
uint32_t gdrom_read_sectors( uint32_t sector, uint32_t sector_count,
nkeynes@138
    84
			     char *buf )
nkeynes@138
    85
{
nkeynes@138
    86
    if( gdrom_disc == NULL )
nkeynes@138
    87
	return 0; /* No media */
nkeynes@138
    88
    return gdrom_disc->read_sectors( gdrom_disc, sector, sector_count, buf );
nkeynes@138
    89
}
nkeynes@138
    90
nkeynes@138
    91
nkeynes@138
    92
void gdrom_dump_disc( gdrom_disc_t disc ) {
nkeynes@138
    93
    int i;
nkeynes@138
    94
    INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
nkeynes@138
    95
	  disc->track[disc->track_count-1].session + 1 );
nkeynes@138
    96
    for( i=0; i<disc->track_count; i++ ) {
nkeynes@138
    97
	INFO( "Sess %d Trk %d: Start %06X Length %06X, %s",
nkeynes@138
    98
	      disc->track[i].session+1, i+1, disc->track[i].lba,
nkeynes@138
    99
	      disc->track[i].sector_count, gdrom_mode_names[disc->track[i].mode] );
nkeynes@138
   100
    }
nkeynes@138
   101
}
nkeynes@138
   102
nkeynes@138
   103
gboolean gdrom_get_toc( char *buf ) 
nkeynes@138
   104
{
nkeynes@142
   105
    struct gdrom_toc *toc = (struct gdrom_toc *)buf;
nkeynes@142
   106
    int i;
nkeynes@142
   107
nkeynes@138
   108
    if( gdrom_disc == NULL )
nkeynes@138
   109
	return FALSE;
nkeynes@142
   110
nkeynes@142
   111
    for( i=0; i<gdrom_disc->track_count; i++ ) {
nkeynes@142
   112
	toc->track[i] = htonl( gdrom_disc->track[i].lba ) | gdrom_disc->track[i].flags;
nkeynes@142
   113
    }
nkeynes@142
   114
    toc->first = 0x0100 | gdrom_disc->track[0].flags;
nkeynes@142
   115
    toc->last = (gdrom_disc->track_count<<8) | gdrom_disc->track[i-1].flags;
nkeynes@142
   116
    toc->leadout = htonl(gdrom_disc->track[i-1].lba + gdrom_disc->track[i-1].sector_count) |
nkeynes@142
   117
	gdrom_disc->track[i-1].flags;
nkeynes@142
   118
    for( ;i<99; i++ )
nkeynes@142
   119
	toc->track[i] = 0xFFFFFFFF;
nkeynes@138
   120
    return TRUE;
nkeynes@138
   121
}
nkeynes@138
   122
nkeynes@138
   123
void gdrom_mount_disc( gdrom_disc_t disc ) 
nkeynes@138
   124
{
nkeynes@138
   125
    gdrom_unmount_disc();
nkeynes@138
   126
    gdrom_disc = disc;
nkeynes@138
   127
    idereg.disc = disc->disc_type | IDE_DISC_READY;
nkeynes@138
   128
    gdrom_dump_disc( disc );
nkeynes@138
   129
}
nkeynes@138
   130
nkeynes@138
   131
gdrom_disc_t gdrom_mount_image( const gchar *filename )
nkeynes@138
   132
{
nkeynes@138
   133
    gdrom_disc_t disc = gdrom_image_open(filename);
nkeynes@138
   134
    if( disc != NULL )
nkeynes@138
   135
	gdrom_mount_disc( disc );
nkeynes@138
   136
    return disc;
nkeynes@138
   137
}
nkeynes@138
   138
nkeynes@138
   139
void gdrom_unmount_disc( ) 
nkeynes@138
   140
{
nkeynes@138
   141
    if( gdrom_disc != NULL ) {
nkeynes@138
   142
	gdrom_disc->close(gdrom_disc);
nkeynes@138
   143
    }
nkeynes@138
   144
    gdrom_disc = NULL;
nkeynes@138
   145
    idereg.disc = IDE_DISC_NONE;
nkeynes@138
   146
}
nkeynes@138
   147
nkeynes@138
   148
gboolean gdrom_is_mounted( void ) 
nkeynes@138
   149
{
nkeynes@138
   150
    return gdrom_disc != NULL;
nkeynes@138
   151
}
.