Search
lxdream.org :: lxdream/src/gdrom/gdimage.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gdrom/gdimage.c
changeset 782:1af8b5ce627c
prev759:f16975739abc
next796:a2dc83592467
author nkeynes
date Tue Jul 29 06:35:28 2008 +0000 (14 years ago)
permissions -rw-r--r--
last change Workaround OS X's inability to handle CD reads of unknown size (ie where the read
request failed to specify an expected sector type) - read raw sectors and parse it
out by hand.
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 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 void gdrom_extract_raw_data_sector( char *sector_data, int channels, unsigned char *buf, uint32_t *length )
   232 {
   233     int sector_mode;
   234     int start, size;
   235     struct cdrom_sector_header *secthead = (struct cdrom_sector_header *)sector_data;
   236     if( secthead->mode == 1 ) {
   237         sector_mode = GDROM_MODE1;
   238     } else {
   239         sector_mode = ((secthead->subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   240     }
   241     gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   243     memcpy( buf, sector_data+start, size );
   244     *length = size;
   245 }
   247 /**
   248  * Read a single sector from a disc image. If you thought this would be simple, 
   249  * I have just one thing to say to you: Bwahahahahahahahah.
   250  *
   251  * Once we've decided that there's a real sector at the requested lba, there's 
   252  * really two things we need to care about:
   253  *   1. Is the sector mode compatible with the requested read mode
   254  *   2. Which parts of the sector do we need to return? 
   255  *      (header/subhead/data/raw sector)
   256  *
   257  * Also note that the disc image may supply us with just the data (most common 
   258  * case), or may have the full raw sector. In the former case we may need to 
   259  * generate the missing data on the fly, for which we use libedc to compute the
   260  * data correction codes.
   261  */
   262 static gdrom_error_t gdrom_image_read_sector( gdrom_disc_t disc, uint32_t lba,
   263                                               int mode, unsigned char *buf, uint32_t *length )
   264 {
   265     gdrom_image_t image = (gdrom_image_t)disc;
   266     struct cdrom_sector_header secthead;
   267     int file_offset, read_len, track_no;
   269     FILE *f;
   271     track_no = gdrom_image_get_track_by_lba( image, lba );
   272     if( track_no == -1 ) {
   273         return PKT_ERR_BADREAD;
   274     }
   275     struct gdrom_track *track = &image->track[track_no-1];
   276     file_offset = track->offset + track->sector_size * (lba - track->lba);
   277     read_len = track->sector_size;
   278     if( track->file != NULL ) {
   279         f = track->file;
   280     } else {
   281         f = image->file;
   282     }
   284     /* First figure out what the real sector mode is for raw/semiraw sectors */
   285     int sector_mode;
   286     switch( track->mode ) {
   287     case GDROM_RAW_NONXA:
   288         gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   289         sector_mode = (secthead.mode == 1) ? GDROM_MODE1 : GDROM_MODE2_FORMLESS;
   290         break;
   291     case GDROM_RAW_XA:
   292         gdrom_read_block( (unsigned char *)(&secthead), file_offset, sizeof(secthead), f );
   293         if( secthead.mode == 1 ) {
   294             sector_mode = GDROM_MODE1;
   295         } else {
   296             sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   297         }
   298         break;
   299     case GDROM_SEMIRAW_MODE2:
   300         gdrom_read_block( secthead.subhead, file_offset, 8, f );
   301         sector_mode = ((secthead.subhead[2] & 0x20) == 0 ) ? GDROM_MODE2_FORM1 : GDROM_MODE2_FORM2;
   302         break;
   303     default:
   304         /* In the other cases, the track mode completely defines the sector mode */
   305         sector_mode = track->mode;
   306         break;
   307     }
   309     if( !gdrom_is_compatible_read_mode(sector_mode, READ_CD_MODE(mode)) ) {
   310         return PKT_ERR_BADREADMODE;
   311     }
   313     /* Ok, we've got a valid sector, check what parts of the sector we need to
   314      * return - header | subhead | data | everything
   315      */
   316     int channels = READ_CD_CHANNELS(mode);
   318     if( channels == 0 ) {
   319         // legal, if weird
   320         *length = 0;
   321         return PKT_ERR_OK;
   322     } else if( channels == 0xA0 && 
   323             (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2 )) {
   324         // caller requested a non-contiguous region
   325         return PKT_ERR_BADFIELD;
   326     } else if( READ_CD_RAW(channels) ) {
   327         channels = 0xF0; // implies everything
   328     }
   330     read_len = 0;
   331     int start, size;
   332     switch( track->mode ) {
   333     case GDROM_CDDA:
   334         // audio is nice and simple (assume perfect reads for now)
   335         *length = 2352;
   336         gdrom_read_block( buf, file_offset, track->sector_size, f );
   337         return PKT_ERR_OK;
   338     case GDROM_RAW_XA:
   339     case GDROM_RAW_NONXA:
   340         gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   341         gdrom_read_block( buf, file_offset+start, size, f );
   342         read_len = size;
   343         break;
   344     case GDROM_SEMIRAW_MODE2:
   345         gdrom_get_read_bounds( sector_mode, channels, &start, &size );
   346         if( READ_CD_HEADER(channels) ) {
   347             gdrom_build_sector_header( buf, lba, sector_mode );
   348             read_len += SECTOR_HEADER_SIZE;
   349             size -= SECTOR_HEADER_SIZE;
   350         } else {
   351             start -= SECTOR_HEADER_SIZE;
   352         }
   353         gdrom_read_block( buf + read_len, file_offset+start, size, f );
   354         read_len += size;
   355         break;
   356     default: // Data track w/ data only in file
   357         if( READ_CD_RAW(channels) ) {
   358             gdrom_read_block( buf + gdrom_data_offset[track->mode], file_offset, 
   359                     track->sector_size, f );
   360             do_encode_L2( buf, sector_mode, lba );
   361             read_len = 2352;
   362         } else {
   363             if( READ_CD_HEADER(channels) ) {
   364                 gdrom_build_sector_header( buf, lba, sector_mode );
   365                 read_len += SECTOR_HEADER_SIZE;
   366             }
   367             if( READ_CD_SUBHEAD(channels) && 
   368                     (sector_mode == GDROM_MODE2_FORM1 || sector_mode == GDROM_MODE2_FORM2) ) {
   369                 if( sector_mode == GDROM_MODE2_FORM1 ) {
   370                     *((uint32_t *)(buf+read_len)) = 0;
   371                     *((uint32_t *)(buf+read_len+4)) = 0;
   372                 } else {
   373                     *((uint32_t *)(buf+read_len)) = 0x00200000;
   374                     *((uint32_t *)(buf+read_len+4)) = 0x00200000;
   375                 }
   376                 read_len += 8;
   377             }
   378             if( READ_CD_DATA(channels) ) {
   379                 gdrom_read_block( buf+read_len, file_offset, track->sector_size, f );
   380                 read_len += track->sector_size;
   381             }
   382         }
   383     }
   384     *length = read_len;
   385     return PKT_ERR_OK;
   386 }
   388 static gdrom_error_t gdrom_image_read_toc( gdrom_disc_t disc, unsigned char *buf ) 
   389 {
   390     gdrom_image_t image = (gdrom_image_t)disc;
   391     struct gdrom_toc *toc = (struct gdrom_toc *)buf;
   392     int i;
   394     for( i=0; i<image->track_count; i++ ) {
   395         toc->track[i] = htonl( image->track[i].lba ) | image->track[i].flags;
   396     }
   397     toc->first = 0x0100 | image->track[0].flags;
   398     toc->last = (image->track_count<<8) | image->track[i-1].flags;
   399     toc->leadout = htonl(image->track[i-1].lba + image->track[i-1].sector_count) |
   400     image->track[i-1].flags;
   401     for( ;i<99; i++ )
   402         toc->track[i] = 0xFFFFFFFF;
   403     return PKT_ERR_OK;
   404 }
   406 static gdrom_error_t gdrom_image_read_session( gdrom_disc_t disc, int session, unsigned char *buf )
   407 {
   408     gdrom_image_t image = (gdrom_image_t)disc;
   409     struct gdrom_track *last_track = &image->track[image->track_count-1];
   410     unsigned int end_of_disc = last_track->lba + last_track->sector_count;
   411     int i;
   412     buf[0] = 0x01; /* Disc status? */
   413     buf[1] = 0;
   415     if( session == 0 ) {
   416         buf[2] = last_track->session+1; /* last session */
   417         buf[3] = (end_of_disc >> 16) & 0xFF;
   418         buf[4] = (end_of_disc >> 8) & 0xFF;
   419         buf[5] = end_of_disc & 0xFF;
   420         return PKT_ERR_OK;
   421     } else {
   422         session--;
   423         for( i=0; i<image->track_count; i++ ) {
   424             if( image->track[i].session == session ) {
   425                 buf[2] = i+1; /* first track of session */
   426                 buf[3] = (image->track[i].lba >> 16) & 0xFF;
   427                 buf[4] = (image->track[i].lba >> 8) & 0xFF;
   428                 buf[5] = image->track[i].lba & 0xFF;
   429                 return PKT_ERR_OK;
   430             }
   431         }
   432         return PKT_ERR_BADFIELD; /* No such session */
   433     }
   434 }
   436 static gdrom_error_t gdrom_image_read_position( gdrom_disc_t disc, uint32_t lba, unsigned char *buf )
   437 {
   438     gdrom_image_t image = (gdrom_image_t)disc;
   439     int track_no = gdrom_image_get_track_by_lba( image, lba );
   440     if( track_no == -1 ) {
   441         track_no = 1;
   442         lba = 150;
   443     }
   444     struct gdrom_track *track = &image->track[track_no-1];
   445     uint32_t offset = lba - track->lba;
   446     buf[4] = track->flags;
   447     buf[5] = track_no;
   448     buf[6] = 0x01; /* ?? */
   449     buf[7] = (offset >> 16) & 0xFF;
   450     buf[8] = (offset >> 8) & 0xFF;
   451     buf[9] = offset & 0xFF;
   452     buf[10] = 0;
   453     buf[11] = (lba >> 16) & 0xFF;
   454     buf[12] = (lba >> 8) & 0xFF;
   455     buf[13] = lba & 0xFF;
   456     return PKT_ERR_OK;
   457 }
   459 static int gdrom_image_drive_status( gdrom_disc_t disc ) 
   460 {
   461     gdrom_image_t image = (gdrom_image_t)disc;
   462     if( image->disc_type == IDE_DISC_NONE ) {
   463         return IDE_DISC_NONE;
   464     } else {
   465         return image->disc_type | IDE_DISC_READY;
   466     }
   467 }
   469 void gdrom_image_dump_info( gdrom_disc_t d ) {
   470     gdrom_image_t disc = (gdrom_image_t)d;
   471     int i;
   472     int last_session = disc->track[disc->track_count-1].session;
   473     gboolean is_bootable = FALSE;
   475     INFO( "Disc ID: %s, %d tracks in %d sessions", disc->mcn, disc->track_count, 
   476           disc->track[disc->track_count-1].session + 1 );
   477     if( last_session > 0 ) {
   478         /* Boot track is the first data track of the last session, provided that it 
   479          * cannot be a single-session disc.
   480          */
   481         int boot_track = -1;
   482         for( i=disc->track_count-1; i>=0 && disc->track[i].session == last_session; i-- ) {
   483             if( disc->track[i].flags & TRACK_DATA ) {
   484                 boot_track = i;
   485             }
   486         }
   487         if( boot_track != -1 ) {
   488             unsigned char boot_sector[MAX_SECTOR_SIZE];
   489             uint32_t length = sizeof(boot_sector);
   490             if( d->read_sector( d, disc->track[boot_track].lba, 0x28,
   491                     boot_sector, &length ) == PKT_ERR_OK ) {
   492                 bootstrap_dump(boot_sector, FALSE);
   493                 is_bootable = TRUE;
   494             }
   495         }
   496     }
   497     if( !is_bootable ) {
   498         WARN( "Disc does not appear to be bootable" );
   499     }
   500 }
   502 gdrom_device_t gdrom_device_new( const gchar *name, const gchar *dev_name )
   503 {
   504     struct gdrom_device *dev = g_malloc0( sizeof(struct gdrom_device) );
   505     dev->name = g_strdup(name);
   506     dev->device_name = g_strdup(dev_name);
   507     return dev;
   508 }
   510 void gdrom_device_destroy( gdrom_device_t dev )
   511 {
   512     if( dev->name != NULL ) {
   513         g_free( dev->name );
   514         dev->name = NULL;
   515     }
   516     if( dev->device_name != NULL ) {
   517         g_free( dev->device_name );
   518         dev->device_name = NULL;
   519     }
   520     g_free( dev );
   521 }
.