Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 759:f16975739abc
prev736:a02d1475ccfd
next782:1af8b5ce627c
author nkeynes
date Mon Jul 21 01:01:39 2008 +0000 (13 years ago)
permissions -rw-r--r--
last change Fix batch of -Wall warnings
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 <netinet/in.h>
    24 #include "gdrom/gddriver.h"
    25 #include "gdrom/packet.h"
    26 #include "ecc.h"
    27 #include "bootstrap.h"
    29 static void gdrom_image_destroy( gdrom_disc_t disc );
    30 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba, int mode, 
    31                                               unsigned char *buf, uint32_t *readlength );
    32 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf );
    33 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf );
    34 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf );
    35 static int gdrom_image_drive_status( gdrom_disc_t disc );
    37 static uint8_t gdrom_default_sync[12] = { 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0 };
    39 #define SECTOR_HEADER_SIZE 16
    40 #define SECTOR_SUBHEADER_SIZE 8
    42 /* Data offset (from start of raw sector) by sector mode */
    43 static int gdrom_data_offset[] = { 16, 16, 16, 24, 24, 0, 8, 0, 0 };
    47 struct cdrom_sector_header {
    48     uint8_t sync[12];
    49     uint8_t msf[3];
    50     uint8_t mode;
    51     uint8_t subhead[8]; // Mode-2 XA sectors only
    52 };
    54 /**
    55  * Initialize a gdrom_disc structure with the gdrom_image_* methods
    56  */
    57 void gdrom_image_init( gdrom_disc_t disc )
    58 {
    59     memset( disc, 0, sizeof(struct gdrom_disc) ); /* safety */
    60     disc->read_sector = gdrom_image_read_sector;
    61     disc->read_toc = gdrom_image_read_toc;
    62     disc->read_session = gdrom_image_read_session;
    63     disc->read_position = gdrom_image_read_position;
    64     disc->drive_status = gdrom_image_drive_status;
    65     disc->play_audio = NULL; /* not supported yet */
    66     disc->run_time_slice = NULL; /* not needed */
    67     disc->close = gdrom_image_destroy;
    68 }
    70 gdrom_disc_t gdrom_image_new( const gchar *filename, FILE *f )
    71 {
    72     gdrom_image_t image = (gdrom_image_t)g_malloc0(sizeof(struct gdrom_image));
    73     if( image == NULL ) {
    74         return NULL;
    75     }
    76     image->disc_type = IDE_DISC_CDROMXA;
    77     image->file = f;
    78     gdrom_disc_t disc = (gdrom_disc_t)image;
    79     gdrom_image_init(disc);
    80     if( filename == NULL ) {
    81         disc->name = NULL;
    82     } else {
    83         disc->name = g_strdup(filename);
    84     }
    86     return disc;
    87 }
    89 static void gdrom_image_destroy( gdrom_disc_t disc )
    90 {
    91     int i;
    92     FILE *lastfile = NULL;
    93     gdrom_image_t img = (gdrom_image_t)disc;
    94     if( img->file != NULL ) {
    95         fclose(img->file);
    96         img->file = NULL;
    97     }
    98     for( i=0; i<img->track_count; i++ ) {
    99         if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
   100             lastfile = img->track[i].file;
   101             fclose(lastfile);
   102             img->track[i].file = NULL;
   103         }
   104     }
   105     if( disc->name != NULL ) {
   106         g_free( (gpointer)disc->name );
   107         disc->name = NULL;
   108     }
   109     free( disc );
   110 }
   112 void gdrom_image_destroy_no_close( gdrom_disc_t disc )
   113 {
   114     int i;
   115     FILE *lastfile = NULL;
   116     gdrom_image_t img = (gdrom_image_t)disc;
   117     if( img->file != NULL ) {
   118         img->file = NULL;
   119     }
   120     for( i=0; i<img->track_count; i++ ) {
   121         if( img->track[i].file != NULL && img->track[i].file != lastfile ) {
   122             lastfile = img->track[i].file;
   123             fclose(lastfile);
   124             img->track[i].file = NULL;
   125         }
   126     }
   127     if( disc->name != NULL ) {
   128         g_free( (gpointer)disc->name );
   129         disc->name = NULL;
   130     }
   131     free( disc );
   132 }
   134 static int gdrom_image_get_track_by_lba( gdrom_image_t image, uint32_t lba )
   135 {
   136     int i;
   137     for( i=0; i<image->track_count; i++ ) {
   138         if( image->track[i].lba <= lba && 
   139                 lba < (image->track[i].lba + image->track[i].sector_count) ) {
   140             return i+1;
   141         }
   142     }
   143     return -1;
   144 }
   146 /**
   147  * Read a block from an image file, handling negative file offsets
   148  * with 0-fill.
   149  */
   150 static gboolean gdrom_read_block( unsigned char *buf, int file_offset, int length, FILE *f )
   151 {
   152     if( file_offset < 0 ) {
   153         int size = -file_offset;
   154         if( size >= length ) {
   155             memset( buf, 0, length );
   156             return TRUE;
   157         } else {
   158             memset( buf, 0, size );
   159             file_offset = 0;
   160             length -= size;
   161         }
   162     }
   163     fseek( f, file_offset, SEEK_SET );
   164     return fread( buf, length, 1, f ) == 1;
   165 }
   167 static void gdrom_build_sector_header( unsigned char *buf, uint32_t lba, 
   168                                        gdrom_track_mode_t sector_mode )
   169 {
   170     memcpy( buf, gdrom_default_sync, 12 );
   171     cd_build_address( buf, sector_mode, lba );
   172 }
   174 /**
   175  * Return TRUE if the given read mode + track modes are compatible,
   176  * otherwise FALSE.
   177  * @param track_mode one of the GDROM_MODE* constants
   178  * @param read_mode the READ_CD_MODE from the read request
   179  */
   180 static gboolean gdrom_is_compatible_read_mode( int track_mode, int read_mode )
   181 {
   182     switch( read_mode ) {
   183     case READ_CD_MODE_ANY:
   184         return TRUE;
   185     case READ_CD_MODE_CDDA:
   186         return track_mode == GDROM_CDDA;
   187     case READ_CD_MODE_1:
   188         return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
   189     case READ_CD_MODE_2_FORM_1:
   190         return track_mode == GDROM_MODE1 || track_mode == GDROM_MODE2_FORM1;
   191     case READ_CD_MODE_2_FORM_2:
   192         return track_mode == GDROM_MODE2_FORM2;
   193     case READ_CD_MODE_2:
   194         return track_mode == GDROM_MODE2_FORMLESS;
   195     default:
   196         return FALSE;
   197     }
   198 }
   200 /**
   201  * Determine the start position in a raw sector, and the amount of data to read
   202  * in bytes, for a given combination of sector mode and read mode.
   203  */ 
   204 static void gdrom_get_read_bounds( int sector_mode, int read_mode, int *start, int *size )
   205 {
   206     if( READ_CD_RAW(read_mode) ) {
   207         // whole sector
   208         *start = 0;
   209         *size = 2352;
   210     } else {
   211         *size = 0;
   212         if( READ_CD_DATA(read_mode) ) {
   213             *start = gdrom_data_offset[sector_mode];
   214             *size = gdrom_sector_size[sector_mode];
   215         }
   217         if( READ_CD_SUBHEAD(read_mode) && 
   218                 (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   219             *start = SECTOR_HEADER_SIZE;
   220             *size += SECTOR_SUBHEADER_SIZE;
   221         }
   223         if( READ_CD_HEADER(read_mode) ) {
   224             *size += SECTOR_HEADER_SIZE;
   225             *start = 0;
   226         }
   228     }
   229 }
   231 /**
   232  * Read a single sector from a disc image. If you thought this would be simple, 
   233  * I have just one thing to say to you: Bwahahahahahahahah.
   234  *
   235  * Once we've decided that there's a real sector at the requested lba, there's 
   236  * really two things we need to care about:
   237  *   1. Is the sector mode compatible with the requested read mode
   238  *   2. Which parts of the sector do we need to return? 
   239  *      (header/subhead/data/raw sector)
   240  *
   241  * Also note that the disc image may supply us with just the data (most common 
   242  * case), or may have the full raw sector. In the former case we may need to 
   243  * generate the missing data on the fly, for which we use libedc to compute the
   244  * data correction codes.
   245  */
   246 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
   247                                               int mode, unsigned char *buf, uint32_t *length )
   248 {
   249     gdrom_image_t image = (gdrom_image_t)disc;
   250     struct cdrom_sector_header secthead;
   251     int file_offset, read_len, track_no;
   253     FILE *f;
   255     track_no = gdrom_image_get_track_by_lba( image, lba );
   256     if( track_no == -1 ) {
   257         return PKT_ERR_BADREAD;
   258     }
   259     struct gdrom_track *track = &image->track[track_no-1];
   260     file_offset = track->offset + track->sector_size * (lba - track->lba);
   261     read_len = track->sector_size;
   262     if( track->file != NULL ) {
   263         f = track->file;
   264     } else {
   265         f = image->file;
   266     }
   268     /* First figure out what the real sector mode is for raw/semiraw sectors */
   269     int sector_mode;
   270     switch( track->mode ) {
   271     case GDROM_RAW_NONXA:
   272         gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   273         sector_mode = (secthead.mode == 1) ? GDROM_MODE1 : GDROM_MODE2_FORMLESS;
   274         break;
   275     case GDROM_RAW_XA:
   276         gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   277         if( secthead.mode == 1 ) {
   278             sector_mode = GDROM_MODE1;
   279         } else {
   280             sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   281         }
   282         break;
   283     case GDROM_SEMIRAW_MODE2:
   284         gdrom_read_block( secthead.subhead, file_offset, 8, f );
   285         sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   286         break;
   287     default:
   288         /* In the other cases, the track mode completely defines the sector mode */
   289         sector_mode = track->mode;
   290         break;
   291     }
   293     if( !gdrom_is_compatible_read_mode(sector_mode, READ_CD_MODE(mode)) ) {
   294         return PKT_ERR_BADREADMODE;
   295     }
   297     /* Ok, we've got a valid sector, check what parts of the sector we need to
   298      * return - header | subhead | data | everything
   299      */
   300     int channels = READ_CD_CHANNELS(mode);
   302     if( channels == 0 ) {
   303         // legal, if weird
   304         *length = 0;
   305         return PKT_ERR_OK;
   306     } else if( channels == 0xA0 && 
   307             (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2 )) {
   308         // caller requested a non-contiguous region
   309         return PKT_ERR_BADFIELD;
   310     } else if( READ_CD_RAW(channels) ) {
   311         channels = 0xF0; // implies everything
   312     }
   314     read_len = 0;
   315     int start, size;
   316     switch( track->mode ) {
   317     case GDROM_CDDA:
   318         // audio is nice and simple (assume perfect reads for now)
   319         *length = 2352;
   320         gdrom_read_block( buf, file_offset, track->sector_size, f );
   321         return PKT_ERR_OK;
   322     case GDROM_RAW_XA:
   323     case GDROM_RAW_NONXA:
   324         gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   325         gdrom_read_block( buf, file_offset+start, size, f );
   326         read_len = size;
   327         break;
   328     case GDROM_SEMIRAW_MODE2:
   329         gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   330         if( READ_CD_HEADER(channels) ) {
   331             gdrom_build_sector_header( buf, lba, sector_mode );
   332             read_len += SECTOR_HEADER_SIZE;
   333             size -= SECTOR_HEADER_SIZE;
   334         } else {
   335             start -= SECTOR_HEADER_SIZE;
   336         }
   337         gdrom_read_block( buf + read_len, file_offset+start, size, f );
   338         read_len += size;
   339         break;
   340     default: // Data track w/ data only in file
   341         if( READ_CD_RAW(channels) ) {
   342             gdrom_read_block( buf + gdrom_data_offset[track->mode], file_offset, 
   343                     track->sector_size, f );
   344             do_encode_L2( buf, sector_mode, lba );
   345             read_len = 2352;
   346         } else {
   347             if( READ_CD_HEADER(channels) ) {
   348                 gdrom_build_sector_header( buf, lba, sector_mode );
   349                 read_len += SECTOR_HEADER_SIZE;
   350             }
   351             if( READ_CD_SUBHEAD(channels) && 
   352                     (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   353                 if( sector_mode == GDROM_MODE2_FORM1 ) {
   354                     *((uint32_t *)(buf+read_len)) = 0;
   355                     *((uint32_t *)(buf+read_len+4)) = 0;
   356                 } else {
   357                     *((uint32_t *)(buf+read_len)) = 0x00200000;
   358                     *((uint32_t *)(buf+read_len+4)) = 0x00200000;
   359                 }
   360                 read_len += 8;
   361             }
   362             if( READ_CD_DATA(channels) ) {
   363                 gdrom_read_block( buf+read_len, file_offset, track->sector_size, f );
   364                 read_len += track->sector_size;
   365             }
   366         }
   367     }
   368     *length = read_len;
   369     return PKT_ERR_OK;
   370 }
   372 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   373 {
   374     gdrom_image_t image = (gdrom_image_t)disc;
   375     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   376     int i;
   378     for( i=0; i<image->track_count; i++ ) {
   379         toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   380     }
   381     toc->first = 0x0100 | image->track[0].flags;
   382     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   383     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   384     image->track[i-1].flags;
   385     for( ;i<99; i++ )
   386         toc->track[i] = 0xFFFFFFFF;
   387     return PKT_ERR_OK;
   388 }
   390 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   391 {
   392     gdrom_image_t image = (gdrom_image_t)disc;
   393     struct gdrom_track *last_track = &image->track[image->track_count-1];
   394     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   395     int i;
   396     buf[0] = 0x01; /* Disc status? */
   397     buf[1] = 0;
   399     if( session == 0 ) {
   400         buf[2] = last_track->session+1; /* last session */
   401         buf[3] = (end_of_disc >> 16) & 0xFF;
   402         buf[4] = (end_of_disc >> 8) & 0xFF;
   403         buf[5] = end_of_disc & 0xFF;
   404         return PKT_ERR_OK;
   405     } else {
   406         session--;
   407         for( i=0; i<image->track_count; i++ ) {
   408             if( image->track[i].session == session ) {
   409                 buf[2] = i+1; /* first track of session */
   410                 buf[3] = (image->track[i].lba >> 16) & 0xFF;
   411                 buf[4] = (image->track[i].lba >> 8) & 0xFF;
   412                 buf[5] = image->track[i].lba & 0xFF;
   413                 return PKT_ERR_OK;
   414             }
   415         }
   416         return PKT_ERR_BADFIELD; /* No such session */
   417     }
   418 }
   420 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   421 {
   422     gdrom_image_t image = (gdrom_image_t)disc;
   423     int track_no = gdrom_image_get_track_by_lba( image, lba );
   424     if( track_no == -1 ) {
   425         track_no = 1;
   426         lba = 150;
   427     }
   428     struct gdrom_track *track = &image->track[track_no-1];
   429     uint32_t offset = lba - track->lba;
   430     buf[4] = track->flags;
   431     buf[5] = track_no;
   432     buf[6] = 0x01; /* ?? */
   433     buf[7] = (offset >> 16) & 0xFF;
   434     buf[8] = (offset >> 8) & 0xFF;
   435     buf[9] = offset & 0xFF;
   436     buf[10] = 0;
   437     buf[11] = (lba >> 16) & 0xFF;
   438     buf[12] = (lba >> 8) & 0xFF;
   439     buf[13] = lba & 0xFF;
   440     return PKT_ERR_OK;
   441 }
   443 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   444 {
   445     gdrom_image_t image = (gdrom_image_t)disc;
   446     if( image->disc_type == IDE_DISC_NONE ) {
   447         return IDE_DISC_NONE;
   448     } else {
   449         return image->disc_type | IDE_DISC_READY;
   450     }
   451 }
   453 void gdrom_image_dump_info( gdrom_disc_t d ) {
   454     gdrom_image_t disc = (gdrom_image_t)d;
   455     int i;
   456     int last_session = disc->track[disc->track_count-1].session;
   457     gboolean is_bootable = FALSE;
   459     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   460           disc->track[disc->track_count-1].session + 1 );
   461     if( last_session > 0 ) {
   462         /* Boot track is the first data track of the last session, provided that it 
   463          * cannot be a single-session disc.
   464          */
   465         int boot_track = -1;
   466         for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   467             if( disc->track[i].flags & TRACK_DATA ) {
   468                 boot_track = i;
   469             }
   470         }
   471         if( boot_track != -1 ) {
   472             unsigned char boot_sector[MAX_SECTOR_SIZE];
   473             uint32_t length = sizeof(boot_sector);
   474             if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   475                     boot_sector, &length ) == PKT_ERR_OK ) {
   476                 bootstrap_dump(boot_sector, FALSE);
   477                 is_bootable = TRUE;
   478             }
   479         }
   480     }
   481     if( !is_bootable ) {
   482         WARN( "Disc does not appear to be bootable" );
   483     }
   484 }
   486 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name )
   487 {
   488     struct gdrom_device *dev = g_malloc0( sizeof(struct gdrom_device) );
   489     dev->name = g_strdup(name);
   490     dev->device_name = g_strdup(dev_name);
   491     return dev;
   492 }
   494 void gdrom_device_destroy( gdrom_device_t dev )
   495 {
   496     if( dev->name != NULL ) {
   497         g_free( dev->name );
   498         dev->name = NULL;
   499     }
   500     if( dev->device_name != NULL ) {
   501         g_free( dev->device_name );
   502         dev->device_name = NULL;
   503     }
   504     g_free( dev );
   505 }
.