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