2018-04-25 23:51:13 +02:00
/*
* 86 Box A hypervisor and IBM PC system emulator that specializes in
* running old operating systems and software designed for IBM
* PC systems and compatibles from 1981 through fairly recent
* system designs based on the PCI bus .
*
* This file is part of the 86 Box distribution .
*
* CD - ROM image support .
*
2018-10-30 13:32:25 +01:00
* Version : @ ( # ) cdrom_image . cc 1.0 .7 2018 / 10 / 28
2018-04-25 23:51:13 +02:00
*
* Author : RichardG867 ,
* Miran Grca , < mgrca8 @ gmail . com >
* bit ,
*
* Copyright 2015 - 2018 Richardg867 .
* Copyright 2015 - 2018 Miran Grca .
* Copyright 2017 , 2018 bit .
*/
2017-09-04 01:52:29 -04:00
# define __USE_LARGEFILE64
# define _LARGEFILE_SOURCE
# define _LARGEFILE64_SOURCE
2018-01-13 22:56:13 +01:00
# include <stdarg.h>
2017-09-25 04:31:20 -04:00
# include <stdio.h>
# include <stdint.h>
# include <string.h>
2017-10-12 14:25:17 -04:00
# include <stdlib.h>
2017-09-25 04:31:20 -04:00
# include <wchar.h>
2017-09-04 01:52:29 -04:00
# include "../config.h"
2017-10-12 14:25:17 -04:00
# include "../plat.h"
2018-10-02 22:54:28 +02:00
# include "../scsi/scsi_device.h"
2017-05-18 14:03:43 -04:00
# include "cdrom_dosbox.h"
# include "cdrom.h"
# include "cdrom_image.h"
2018-10-17 05:29:48 +02:00
# ifdef ENABLE_CDROM_IMAGE_LOG
int cdrom_image_do_log = ENABLE_CDROM_IMAGE_LOG ;
void
cdrom_image_log ( const char * fmt , . . . )
{
va_list ap ;
if ( cdrom_image_do_log ) {
va_start ( ap , fmt ) ;
pclog_ex ( fmt , ap ) ;
va_end ( ap ) ;
}
}
# else
# define cdrom_image_log(fmt, ...)
# endif
2017-05-18 14:03:43 -04:00
/* The addresses sent from the guest are absolute, ie. a LBA of 0 corresponds to a MSF of 00:00:00. Otherwise, the counter displayed by the guest is wrong:
there is a seeming 2 seconds in which audio plays but counter does not move , while a data track before audio jumps to 2 seconds before the actual start
of the audio while audio still plays . With an absolute conversion , the counter is fine . */
2018-04-25 23:51:13 +02:00
# define MSFtoLBA(m,s,f) ((((m*60)+s)*75)+f)
2018-10-17 05:29:48 +02:00
static void
2018-10-30 13:32:25 +01:00
image_get_tracks ( cdrom_t * dev , int * first , int * last )
2017-05-18 14:03:43 -04:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-04-25 23:51:13 +02:00
TMSF tmsf ;
2017-05-18 14:03:43 -04:00
2018-10-30 13:32:25 +01:00
img - > GetAudioTracks ( * first , * last , tmsf ) ;
2017-05-18 14:03:43 -04:00
}
2018-04-25 23:51:13 +02:00
static void
2018-10-30 13:32:25 +01:00
image_get_track_info ( cdrom_t * dev , uint32_t track , int end , track_info_t * ti )
2017-05-18 14:03:43 -04:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-10-30 13:32:25 +01:00
TMSF tmsf ;
2018-10-17 05:29:48 +02:00
2018-10-30 13:32:25 +01:00
if ( end )
img - > GetAudioTrackEndInfo ( track , ti - > number , tmsf , ti - > attr ) ;
else
img - > GetAudioTrackInfo ( track , ti - > number , tmsf , ti - > attr ) ;
2018-03-17 20:32:20 +01:00
2018-10-30 13:32:25 +01:00
ti - > m = tmsf . min ;
ti - > s = tmsf . sec ;
ti - > f = tmsf . fr ;
2017-05-18 14:03:43 -04:00
}
2018-04-25 23:51:13 +02:00
static void
2018-10-30 13:32:25 +01:00
image_get_subchannel ( cdrom_t * dev , uint32_t lba , subchannel_t * subc )
2018-04-25 23:51:13 +02:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-10-30 13:32:25 +01:00
TMSF rel_pos , abs_pos ;
2018-10-17 05:29:48 +02:00
2018-10-30 13:32:25 +01:00
img - > GetAudioSub ( lba , subc - > attr , subc - > track , subc - > index ,
rel_pos , abs_pos ) ;
2018-10-17 05:29:48 +02:00
2018-10-30 13:32:25 +01:00
subc - > abs_m = abs_pos . min ;
subc - > abs_s = abs_pos . sec ;
subc - > abs_f = abs_pos . fr ;
2018-04-25 23:51:13 +02:00
2018-10-30 13:32:25 +01:00
subc - > rel_m = rel_pos . min ;
subc - > rel_s = rel_pos . sec ;
subc - > rel_f = rel_pos . fr ;
2017-05-18 14:03:43 -04:00
}
2018-04-25 23:51:13 +02:00
static int
2018-10-17 05:29:48 +02:00
image_get_last_block ( cdrom_t * dev )
2017-05-18 14:03:43 -04:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-04-25 23:51:13 +02:00
int first_track , last_track ;
int number , c ;
unsigned char attr ;
TMSF tmsf ;
2018-10-17 05:29:48 +02:00
uint32_t lb = 0 ;
uint32_t address ;
2017-05-18 14:03:43 -04:00
2018-10-30 13:32:25 +01:00
if ( ! img )
return 0 ;
2017-05-18 14:03:43 -04:00
2018-10-17 05:29:48 +02:00
img - > GetAudioTracks ( first_track , last_track , tmsf ) ;
2017-05-18 14:03:43 -04:00
2018-04-25 23:51:13 +02:00
for ( c = 0 ; c < = last_track ; c + + ) {
2018-10-17 05:29:48 +02:00
img - > GetAudioTrackInfo ( c + 1 , number , tmsf , attr ) ;
2018-04-25 23:51:13 +02:00
address = MSFtoLBA ( tmsf . min , tmsf . sec , tmsf . fr ) - 150 ; /* Do the - 150 here as well. */
if ( address > lb )
lb = address ;
}
2018-10-17 05:29:48 +02:00
2018-04-25 23:51:13 +02:00
return lb ;
2017-05-18 14:03:43 -04:00
}
2018-04-25 23:51:13 +02:00
static int
2018-10-17 05:29:48 +02:00
image_is_track_audio ( cdrom_t * dev , uint32_t pos , int ismsf )
2017-05-18 14:03:43 -04:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
uint8_t attr ;
2018-04-25 23:51:13 +02:00
TMSF tmsf ;
2018-10-17 05:29:48 +02:00
int m , s , f ;
2018-04-25 23:51:13 +02:00
int number ;
2017-05-18 14:03:43 -04:00
2018-10-30 13:32:25 +01:00
if ( ! img | | ( dev - > cd_status = = CD_STATUS_DATA_ONLY ) )
return 0 ;
2017-05-18 14:03:43 -04:00
2018-04-25 23:51:13 +02:00
if ( ismsf ) {
m = ( pos > > 16 ) & 0xff ;
s = ( pos > > 8 ) & 0xff ;
f = pos & 0xff ;
pos = MSFtoLBA ( m , s , f ) - 150 ;
}
2017-05-18 14:03:43 -04:00
2018-04-25 23:51:13 +02:00
/* GetTrack requires LBA. */
2018-10-17 05:29:48 +02:00
img - > GetAudioTrackInfo ( img - > GetTrack ( pos ) , number , tmsf , attr ) ;
2017-05-18 14:03:43 -04:00
2018-04-25 23:51:13 +02:00
return attr = = AUDIO_TRACK ;
}
2017-05-18 14:03:43 -04:00
2018-10-30 13:32:25 +01:00
static int
image_sector_size ( struct cdrom * dev , uint32_t lba )
2018-04-25 23:51:13 +02:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-10-30 13:32:25 +01:00
return img - > GetSectorSize ( lba ) ;
2018-04-25 23:51:13 +02:00
}
2017-05-18 14:03:43 -04:00
2018-04-25 23:51:13 +02:00
static int
2018-10-30 13:32:25 +01:00
image_read_sector ( struct cdrom * dev , int type , uint8_t * b , uint32_t lba )
2018-04-25 23:51:13 +02:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2017-05-18 14:03:43 -04:00
2018-10-30 13:32:25 +01:00
switch ( type ) {
case CD_READ_DATA :
return img - > ReadSector ( b , false , lba ) ;
case CD_READ_AUDIO :
return img - > ReadSector ( b , true , lba ) ;
case CD_READ_RAW :
if ( img - > GetSectorSize ( lba ) = = 2352 )
return img - > ReadSector ( b , true , lba ) ;
2018-02-15 23:14:44 +01:00
else
2018-10-30 13:32:25 +01:00
return img - > ReadSectorSub ( b , lba ) ;
default :
cdrom_image_log ( " CD-ROM %i: Unknown CD read type \n " , dev - > id ) ;
return 0 ;
2018-04-25 23:51:13 +02:00
}
2017-05-18 14:03:43 -04:00
}
2018-04-25 23:51:13 +02:00
static int
2018-10-30 13:32:25 +01:00
image_track_type ( cdrom_t * dev , uint32_t lba )
2017-05-18 14:03:43 -04:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-04-25 23:51:13 +02:00
2018-10-30 13:32:25 +01:00
if ( img ) {
if ( image_is_track_audio ( dev , lba , 0 ) )
return CD_TRACK_AUDIO ;
else {
if ( img - > IsMode2 ( lba ) )
return CD_TRACK_MODE2 | img - > GetMode2Form ( lba ) ;
2018-04-25 23:51:13 +02:00
}
}
2018-10-30 13:32:25 +01:00
return 0 ;
2017-05-18 14:03:43 -04:00
}
2018-10-17 05:29:48 +02:00
static void
image_exit ( cdrom_t * dev )
2017-05-18 14:03:43 -04:00
{
2018-10-17 05:29:48 +02:00
CDROM_Interface_Image * img = ( CDROM_Interface_Image * ) dev - > image ;
2018-03-17 20:32:20 +01:00
2018-10-17 05:29:48 +02:00
cdrom_image_log ( " CDROM: image_exit(%ls) \n " , dev - > image_path ) ;
2018-10-30 13:32:25 +01:00
dev - > cd_status = CD_STATUS_EMPTY ;
2017-05-18 14:03:43 -04:00
2018-10-17 05:29:48 +02:00
if ( img ) {
delete img ;
dev - > image = NULL ;
2018-04-25 23:51:13 +02:00
}
2017-05-18 14:03:43 -04:00
2018-10-17 05:29:48 +02:00
dev - > ops = NULL ;
2017-05-18 14:03:43 -04:00
}
2018-04-25 23:51:13 +02:00
2018-10-17 05:29:48 +02:00
static const cdrom_ops_t cdrom_image_ops = {
2018-10-30 13:32:25 +01:00
image_get_tracks ,
image_get_track_info ,
image_get_subchannel ,
image_sector_size ,
image_read_sector ,
image_track_type ,
2018-04-25 23:51:13 +02:00
image_exit
2017-05-18 14:03:43 -04:00
} ;
2018-10-17 05:29:48 +02:00
2018-10-30 13:32:25 +01:00
static int
image_open_abort ( cdrom_t * dev )
{
cdrom_image_close ( dev ) ;
dev - > ops = NULL ;
dev - > host_drive = 0 ;
return 1 ;
}
2018-10-17 05:29:48 +02:00
int
cdrom_image_open ( cdrom_t * dev , const wchar_t * fn )
{
char temp [ 1024 ] ;
CDROM_Interface_Image * img ;
wcscpy ( dev - > image_path , fn ) ;
/* Create new instance of the CDROM_Image class. */
img = new CDROM_Interface_Image ( ) ;
2018-10-30 13:32:25 +01:00
/* This guarantees that if ops is not NULL, then
neither is the image pointer . */
if ( ! img )
return image_open_abort ( dev ) ;
2018-10-17 05:29:48 +02:00
dev - > image = img ;
/* Convert filename and open the image. */
memset ( temp , ' \0 ' , sizeof ( temp ) ) ;
wcstombs ( temp , fn , sizeof ( temp ) ) ;
2018-10-30 13:32:25 +01:00
if ( ! img - > SetDevice ( temp , false ) )
return image_open_abort ( dev ) ;
2018-10-17 05:29:48 +02:00
/* All good, reset state. */
2018-10-30 13:32:25 +01:00
if ( ! wcscasecmp ( plat_get_extension ( ( wchar_t * ) fn ) , L " ISO " ) )
dev - > cd_status = CD_STATUS_DATA_ONLY ;
else
dev - > cd_status = CD_STATUS_STOPPED ;
2018-10-17 05:29:48 +02:00
dev - > seek_pos = 0 ;
dev - > cd_buflen = 0 ;
dev - > cdrom_capacity = image_get_last_block ( dev ) + 1 ;
/* Attach this handler to the drive. */
dev - > ops = & cdrom_image_ops ;
return 0 ;
}
void
cdrom_image_close ( cdrom_t * dev )
{
2018-10-30 13:32:25 +01:00
cdrom_image_log ( " CDROM: image_close(%ls) \n " , dev - > image_path ) ;
2018-10-17 05:29:48 +02:00
if ( dev - > ops - > exit )
dev - > ops - > exit ( dev ) ;
}