Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 840:c6a778c228a6
prev837:4eae2ddccf9c
next1023:264e2fd90be8
author nkeynes
date Tue Sep 02 03:34:23 2008 +0000 (11 years ago)
permissions -rw-r--r--
last change Add omitted ctype.h include
view annotate diff log raw
     1 /**
     2  * $Id$
     3  *
     4  * GD-Rom image-file common functions. 
     5  *
     6  * Copyright (c) 2005 Nathan Keynes.
     7  *
     8  * This program is free software; you can redistribute it and/or modify
     9  * it under the terms of the GNU General Public License as published by
    10  * the Free Software Foundation; either version 2 of the License, or
    11  * (at your option) any later version.
    12  *
    13  * This program is distributed in the hope that it will be useful,
    14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    16  * GNU General Public License for more details.
    17  */
    19 #include <assert.h>
    20 #include <stdlib.h>
    21 #include <string.h>
    22 #include <ctype.h>
    23 #include <sys/types.h>
    24 #include <netinet/in.h>
    26 #include "gdrom/gddriver.h"
    27 #include "gdrom/packet.h"
    28 #include "ecc.h"
    29 #include "bootstrap.h"
    31 static void gdrom_image_destroy( gdrom_disc_t disc );
    32 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode, 
    33                                               unsigned char *buf, uint32_t *readlength );
    34 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
    35 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
    36 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
    37 static int gdrom_image_drive_status( gdrom_disc_t disc );
    39 static uint8_t gdrom_default_sync[12] = { 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0 };
    41 #define SECTOR_HEADER_SIZE 16
    42 #define SECTOR_SUBHEADER_SIZE 8
    44 /* Data offset (from start of raw sector) by sector mode */
    45 static int gdrom_data_offset[] = { 16, 16, 16, 24, 24, 0, 8, 0, 0 };
    49 struct cdrom_sector_header {
    50     uint8_t sync[12];
    51     uint8_t msf[3];
    52     uint8_t mode;
    53     uint8_t subhead[8]; // Mode-2 XA sectors only
    54 };
    56 /**
    57  * Initialize a gdrom_disc structure with the gdrom_image_* methods
    58  */
    59 void gdrom_image_init( gdrom_disc_t disc )
    60 {
    61     memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
    62     disc->read_sector = gdrom_image_read_sector;
    63     disc->read_toc = gdrom_image_read_toc;
    64     disc->read_session = gdrom_image_read_session;
    65     disc->read_position = gdrom_image_read_position;
    66     disc->drive_status = gdrom_image_drive_status;
    67     disc->play_audio = NULL; /* not supported yet */
    68     disc->run_time_slice = NULL; /* not needed */
    69     disc->close = gdrom_image_destroy;
    70 }
    72 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
    73 {
    74     gdrom_image_t image = (gdrom_image_t)g_malloc0(sizeof(struct gdrom_image));
    75     if( image == NULL ) {
    76         return NULL;
    77     }
    78     image->disc_type = IDE_DISC_CDROMXA;
    79     image->file = f;
    80     gdrom_disc_t disc = (gdrom_disc_t)image;
    81     gdrom_image_init(disc);
    82     if( filename == NULL ) {
    83         disc->name = NULL;
    84     } else {
    85         disc->name = g_strdup(filename);
    86     }
    88     return disc;
    89 }
    91 static void gdrom_image_destroy( gdrom_disc_t disc )
    92 {
    93     int i;
    94     FILE *lastfile = NULL;
    95     gdrom_image_t img = (gdrom_image_t)disc;
    96     if( img->file != NULL ) {
    97         fclose(img->file);
    98         img->file = NULL;
    99     }
   100     for( i=0; i<img->track_count; i++ ) {
   101         if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
   102             lastfile = img->track[i].file;
   103             fclose(lastfile);
   104             img->track[i].file = NULL;
   105         }
   106     }
   107     if( disc->name != NULL ) {
   108         g_free( (gpointer)disc->name );
   109         disc->name = NULL;
   110     }
   111     free( disc );
   112 }
   114 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
   115 {
   116     int i;
   117     FILE *lastfile = NULL;
   118     gdrom_image_t img = (gdrom_image_t)disc;
   119     if( img->file != NULL ) {
   120         img->file = NULL;
   121     }
   122     for( i=0; i<img->track_count; i++ ) {
   123         if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
   124             lastfile = img->track[i].file;
   125             fclose(lastfile);
   126             img->track[i].file = NULL;
   127         }
   128     }
   129     if( disc->name != NULL ) {
   130         g_free( (gpointer)disc->name );
   131         disc->name = NULL;
   132     }
   133     free( disc );
   134 }
   136 int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
   137 {
   138     int i;
   139     for( i=0; i<image->track_count; i++ ) {
   140         if( image->track[i].lba <= lba && 
   141                 lba < (image->track[i].lba + image->track[i].sector_count) ) {
   142             return i+1;
   143         }
   144     }
   145     return -1;
   146 }
   148 /**
   149  * Read a block from an image file, handling negative file offsets
   150  * with 0-fill.
   151  */
   152 static gboolean gdrom_read_block( unsigned char *buf, int file_offset, int length, FILE *f )
   153 {
   154     if( file_offset < 0 ) {
   155         int size = -file_offset;
   156         if( size >= length ) {
   157             memset( buf, 0, length );
   158             return TRUE;
   159         } else {
   160             memset( buf, 0, size );
   161             file_offset = 0;
   162             length -= size;
   163         }
   164     }
   165     fseek( f, file_offset, SEEK_SET );
   166     return fread( buf, length, 1, f ) == 1;
   167 }
   169 static void gdrom_build_sector_header( unsigned char *buf, uint32_t lba, 
   170                                        gdrom_track_mode_t sector_mode )
   171 {
   172     memcpy( buf, gdrom_default_sync, 12 );
   173     cd_build_address( buf, sector_mode, lba );
   174 }
   176 /**
   177  * Return TRUE if the given read mode + track modes are compatible,
   178  * otherwise FALSE.
   179  * @param track_mode one of the GDROM_MODE* constants
   180  * @param read_mode the READ_CD_MODE from the read request
   181  */
   182 static gboolean gdrom_is_compatible_read_mode( int track_mode, int read_mode )
   183 {
   184     switch( read_mode ) {
   185     case READ_CD_MODE_ANY:
   186         return TRUE;
   187     case READ_CD_MODE_CDDA:
   188         return track_mode == GDROM_CDDA;
   189     case READ_CD_MODE_1:
   190         return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
   191     case READ_CD_MODE_2_FORM_1:
   192         return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
   193     case READ_CD_MODE_2_FORM_2:
   194         return track_mode == GDROM_MODE2_FORM2;
   195     case READ_CD_MODE_2:
   196         return track_mode == GDROM_MODE2_FORMLESS;
   197     default:
   198         return FALSE;
   199     }
   200 }
   202 /**
   203  * Determine the start position in a raw sector, and the amount of data to read
   204  * in bytes, for a given combination of sector mode and read mode.
   205  */ 
   206 static void gdrom_get_read_bounds( int sector_mode, int read_mode, int *start, int *size )
   207 {
   208     if( READ_CD_RAW(read_mode) ) {
   209         // whole sector
   210         *start = 0;
   211         *size = 2352;
   212     } else {
   213         *size = 0;
   214         if( READ_CD_DATA(read_mode) ) {
   215             *start = gdrom_data_offset[sector_mode];
   216             *size = gdrom_sector_size[sector_mode];
   217         }
   219         if( READ_CD_SUBHEAD(read_mode) && 
   220                 (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   221             *start = SECTOR_HEADER_SIZE;
   222             *size += SECTOR_SUBHEADER_SIZE;
   223         }
   225         if( READ_CD_HEADER(read_mode) ) {
   226             *size += SECTOR_HEADER_SIZE;
   227             *start = 0;
   228         }
   230     }
   231 }
   233 void gdrom_extract_raw_data_sector( char *sector_data, int channels, unsigned char *buf, uint32_t *length )
   234 {
   235     int sector_mode;
   236     int start, size;
   237     struct cdrom_sector_header *secthead = (struct cdrom_sector_header *)sector_data;
   238     if( secthead->mode == 1 ) {
   239         sector_mode = GDROM_MODE1;
   240     } else {
   241         sector_mode = ((secthead->subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   242     }
   243     gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   245     memcpy( buf, sector_data+start, size );
   246     *length = size;
   247 }
   249 /**
   250  * Read a single sector from a disc image. If you thought this would be simple, 
   251  * I have just one thing to say to you: Bwahahahahahahahah.
   252  *
   253  * Once we've decided that there's a real sector at the requested lba, there's 
   254  * really two things we need to care about:
   255  *   1. Is the sector mode compatible with the requested read mode
   256  *   2. Which parts of the sector do we need to return? 
   257  *      (header/subhead/data/raw sector)
   258  *
   259  * Also note that the disc image may supply us with just the data (most common 
   260  * case), or may have the full raw sector. In the former case we may need to 
   261  * generate the missing data on the fly, for which we use libedc to compute the
   262  * data correction codes.
   263  */
   264 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
   265                                               int mode, unsigned char *buf, uint32_t *length )
   266 {
   267     gdrom_image_t image = (gdrom_image_t)disc;
   268     struct cdrom_sector_header secthead;
   269     int file_offset, read_len, track_no;
   271     FILE *f;
   273     track_no = gdrom_image_get_track_by_lba( image, lba );
   274     if( track_no == -1 ) {
   275         return PKT_ERR_BADREAD;
   276     }
   277     struct gdrom_track *track = &image->track[track_no-1];
   278     file_offset = track->offset + track->sector_size * (lba - track->lba);
   279     read_len = track->sector_size;
   280     if( track->file != NULL ) {
   281         f = track->file;
   282     } else {
   283         f = image->file;
   284     }
   286     /* First figure out what the real sector mode is for raw/semiraw sectors */
   287     int sector_mode;
   288     switch( track->mode ) {
   289     case GDROM_RAW_NONXA:
   290         gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   291         sector_mode = (secthead.mode == 1) ? GDROM_MODE1 : GDROM_MODE2_FORMLESS;
   292         break;
   293     case GDROM_RAW_XA:
   294         gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   295         if( secthead.mode == 1 ) {
   296             sector_mode = GDROM_MODE1;
   297         } else {
   298             sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   299         }
   300         break;
   301     case GDROM_SEMIRAW_MODE2:
   302         gdrom_read_block( secthead.subhead, file_offset, 8, f );
   303         sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   304         break;
   305     default:
   306         /* In the other cases, the track mode completely defines the sector mode */
   307         sector_mode = track->mode;
   308         break;
   309     }
   311     if( !gdrom_is_compatible_read_mode(sector_mode, READ_CD_MODE(mode)) ) {
   312         return PKT_ERR_BADREADMODE;
   313     }
   315     /* Ok, we've got a valid sector, check what parts of the sector we need to
   316      * return - header | subhead | data | everything
   317      */
   318     int channels = READ_CD_CHANNELS(mode);
   320     if( channels == 0 ) {
   321         // legal, if weird
   322         *length = 0;
   323         return PKT_ERR_OK;
   324     } else if( channels == 0xA0 && 
   325             (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2 )) {
   326         // caller requested a non-contiguous region
   327         return PKT_ERR_BADFIELD;
   328     } else if( READ_CD_RAW(channels) ) {
   329         channels = 0xF0; // implies everything
   330     }
   332     read_len = 0;
   333     int start, size;
   334     switch( track->mode ) {
   335     case GDROM_CDDA:
   336         // audio is nice and simple (assume perfect reads for now)
   337         *length = 2352;
   338         gdrom_read_block( buf, file_offset, track->sector_size, f );
   339         return PKT_ERR_OK;
   340     case GDROM_RAW_XA:
   341     case GDROM_RAW_NONXA:
   342         gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   343         gdrom_read_block( buf, file_offset+start, size, f );
   344         read_len = size;
   345         break;
   346     case GDROM_SEMIRAW_MODE2:
   347         gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   348         if( READ_CD_HEADER(channels) ) {
   349             gdrom_build_sector_header( buf, lba, sector_mode );
   350             read_len += SECTOR_HEADER_SIZE;
   351             size -= SECTOR_HEADER_SIZE;
   352         } else {
   353             start -= SECTOR_HEADER_SIZE;
   354         }
   355         gdrom_read_block( buf + read_len, file_offset+start, size, f );
   356         read_len += size;
   357         break;
   358     default: // Data track w/ data only in file
   359         if( READ_CD_RAW(channels) ) {
   360             gdrom_read_block( buf + gdrom_data_offset[track->mode], file_offset, 
   361                     track->sector_size, f );
   362             do_encode_L2( buf, sector_mode, lba );
   363             read_len = 2352;
   364         } else {
   365             if( READ_CD_HEADER(channels) ) {
   366                 gdrom_build_sector_header( buf, lba, sector_mode );
   367                 read_len += SECTOR_HEADER_SIZE;
   368             }
   369             if( READ_CD_SUBHEAD(channels) && 
   370                     (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   371                 if( sector_mode == GDROM_MODE2_FORM1 ) {
   372                     *((uint32_t *)(buf+read_len)) = 0;
   373                     *((uint32_t *)(buf+read_len+4)) = 0;
   374                 } else {
   375                     *((uint32_t *)(buf+read_len)) = 0x00200000;
   376                     *((uint32_t *)(buf+read_len+4)) = 0x00200000;
   377                 }
   378                 read_len += 8;
   379             }
   380             if( READ_CD_DATA(channels) ) {
   381                 gdrom_read_block( buf+read_len, file_offset, track->sector_size, f );
   382                 read_len += track->sector_size;
   383             }
   384         }
   385     }
   386     *length = read_len;
   387     return PKT_ERR_OK;
   388 }
   390 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   391 {
   392     gdrom_image_t image = (gdrom_image_t)disc;
   393     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   394     int i;
   396     for( i=0; i<image->track_count; i++ ) {
   397         toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   398     }
   399     toc->first = 0x0100 | image->track[0].flags;
   400     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   401     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   402     image->track[i-1].flags;
   403     for( ;i<99; i++ )
   404         toc->track[i] = 0xFFFFFFFF;
   405     return PKT_ERR_OK;
   406 }
   408 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   409 {
   410     gdrom_image_t image = (gdrom_image_t)disc;
   411     struct gdrom_track *last_track = &image->track[image->track_count-1];
   412     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   413     int i;
   414     buf[0] = 0x01; /* Disc status? */
   415     buf[1] = 0;
   417     if( session == 0 ) {
   418         buf[2] = last_track->session+1; /* last session */
   419         buf[3] = (end_of_disc >> 16) & 0xFF;
   420         buf[4] = (end_of_disc >> 8) & 0xFF;
   421         buf[5] = end_of_disc & 0xFF;
   422         return PKT_ERR_OK;
   423     } else {
   424         session--;
   425         for( i=0; i<image->track_count; i++ ) {
   426             if( image->track[i].session == session ) {
   427                 buf[2] = i+1; /* first track of session */
   428                 buf[3] = (image->track[i].lba >> 16) & 0xFF;
   429                 buf[4] = (image->track[i].lba >> 8) & 0xFF;
   430                 buf[5] = image->track[i].lba & 0xFF;
   431                 return PKT_ERR_OK;
   432             }
   433         }
   434         return PKT_ERR_BADFIELD; /* No such session */
   435     }
   436 }
   438 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   439 {
   440     gdrom_image_t image = (gdrom_image_t)disc;
   441     int track_no = gdrom_image_get_track_by_lba( image, lba );
   442     if( track_no == -1 ) {
   443         track_no = 1;
   444         lba = 150;
   445     }
   446     struct gdrom_track *track = &image->track[track_no-1];
   447     uint32_t offset = lba - track->lba;
   448     buf[4] = track->flags;
   449     buf[5] = track_no;
   450     buf[6] = 0x01; /* ?? */
   451     buf[7] = (offset >> 16) & 0xFF;
   452     buf[8] = (offset >> 8) & 0xFF;
   453     buf[9] = offset & 0xFF;
   454     buf[10] = 0;
   455     buf[11] = (lba >> 16) & 0xFF;
   456     buf[12] = (lba >> 8) & 0xFF;
   457     buf[13] = lba & 0xFF;
   458     return PKT_ERR_OK;
   459 }
   461 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   462 {
   463     gdrom_image_t image = (gdrom_image_t)disc;
   464     if( image->disc_type == IDE_DISC_NONE ) {
   465         return IDE_DISC_NONE;
   466     } else {
   467         return image->disc_type | IDE_DISC_READY;
   468     }
   469 }
   471 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name )
   472 {
   473     struct gdrom_device *dev = g_malloc0( sizeof(struct gdrom_device) );
   474     dev->name = g_strdup(name);
   475     dev->device_name = g_strdup(dev_name);
   476     return dev;
   477 }
   479 void gdrom_device_destroy( gdrom_device_t dev )
   480 {
   481     if( dev->name != NULL ) {
   482         g_free( dev->name );
   483         dev->name = NULL;
   484     }
   485     if( dev->device_name != NULL ) {
   486         g_free( dev->device_name );
   487         dev->device_name = NULL;
   488     }
   489     g_free( dev );
   490 }
   492 /**
   493  * Check the disc for a useable DC bootstrap, and update the disc
   494  * with the title accordingly.
   495  * @return TRUE if we found a bootstrap, otherwise FALSE.
   496  */
   497 gboolean gdrom_image_read_info( gdrom_disc_t d ) {
   498     gdrom_image_t disc = (gdrom_image_t)d;
   499     if( disc->track_count > 0 ) {
   500         /* Find the first data track of the last session */
   501         int last_session = disc->track[disc->track_count-1].session;
   502         int i, boot_track = -1;
   503         for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   504             if( disc->track[i].flags & TRACK_DATA ) {
   505                 boot_track = i;
   506             }
   507         }
   508         if( boot_track != -1 ) {
   509             unsigned char boot_sector[MAX_SECTOR_SIZE];
   510             uint32_t length = sizeof(boot_sector);
   511             if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   512                     boot_sector, &length ) == PKT_ERR_OK ) {
   513                 if( memcmp( boot_sector, "SEGA SEGAKATANA SEGA ENTERPRISES", 32) == 0 ) {
   514                     /* Got magic */
   515                     memcpy( d->title, boot_sector+128, 128 );
   516                     for( i=127; i>=0; i-- ) {
   517                         if( !isspace(d->title[i]) ) 
   518                             break;
   519                     }
   520                     d->title[i+1] = '\0';
   521                 }
   522                 bootstrap_dump(boot_sector, FALSE);
   523                 return TRUE;
   524             }
   525         }
   526     }
   527     return FALSE;
   528 }
.