Commit 57801bcb authored by Sam Hocevar's avatar Sam Hocevar

  * additional enhancements to the OS X DVD ioctl module
  * big cleaning of the BeOS DVD ioctl code, got rid of all Linuxisms
parent 0fee43a4
......@@ -3,7 +3,7 @@
*****************************************************************************
* Copyright (C) 1998-2000 Apple Computer, Inc. All rights reserved.
* Copyright (C) 2001 VideoLAN
* $Id: DVDioctl.cpp,v 1.1 2001/04/02 23:30:41 sam Exp $
* $Id: DVDioctl.cpp,v 1.2 2001/04/04 02:49:18 sam Exp $
*
* Authors: Samuel Hocevar <sam@zoy.org>
*
......@@ -29,6 +29,9 @@
* it is still in use
*****************************************************************************/
//XXX: uncomment to activate the key exchange ioctls - may hang the machine
//#define ACTIVATE_DANGEROUS_IOCTL 1
/*****************************************************************************
* Preamble
*****************************************************************************/
......@@ -49,8 +52,9 @@ extern "C"
#include <IOKit/IOLib.h>
#include <IOKit/IOService.h>
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IOMedia.h>
#include <IOKit/storage/IODVDMedia.h>
#include <IOKit/storage/IODVDBlockStorageDriver.h>
#include "DVDioctl.h"
......@@ -85,16 +89,16 @@ typedef struct buf buf_t;
* Local prototypes
*****************************************************************************/
static int DVDClose ( dev_t, int, int, struct proc * );
static int DVDIoctl ( dev_t, u_long, caddr_t, int, struct proc * );
static int DVDBlockIoctl ( dev_t, u_long, caddr_t, int, struct proc * );
static int DVDOpen ( dev_t, int, int, struct proc * );
static int DVDSize ( dev_t );
static void DVDStrategy ( buf_t * );
static int DVDReadWrite( dkr_t, dkrtype_t );
static int DVDReadWrite ( dkr_t, dkrtype_t );
static void DVDReadWriteCompletion( void *, void *, IOReturn, UInt64 );
static struct bdevsw device_functions =
{
DVDOpen, DVDClose, DVDStrategy, DVDIoctl, eno_dump, DVDSize, D_DISK
DVDOpen, DVDClose, DVDStrategy, DVDBlockIoctl, eno_dump, DVDSize, D_DISK
};
/*****************************************************************************
......@@ -106,6 +110,7 @@ static bool b_inuse;
static int i_major;
static void *p_node;
static IODVDMedia *p_dvd;
static IODVDBlockStorageDriver *p_drive;
/*****************************************************************************
* DKR_GET_DEV: borrowed from IOMediaBSDClient.cpp
......@@ -249,6 +254,7 @@ bool DVDioctl::init( OSDictionary *p_dict = 0 )
p_node = NULL;
p_dvd = NULL;
p_drive = NULL;
i_major = -1;
b_inuse = false;
......@@ -341,20 +347,6 @@ void DVDioctl::free( void )
super::free( );
}
#if 0
IOReturn DVDioctl::report( IODVDMedia *DVD, IOMemoryDescriptor *buffer, const DVDKeyClass keyClass, const UInt32 lba, const UInt8 agid, const DVDKeyFormat keyFormat )
{
IOLog( "DVD ioctl: reportkey\n" );
return DVD->getProvider()->reportKey( buffer, keyClass, lba, agid, keyFormat );
}
IOReturn DVDioctl::send( IODVDMedia *DVD, IOMemoryDescriptor *buffer, const DVDKeyClass keyClass, const UInt32 lba, const DVDKeyFormat keyFormat )
{
IOLog( "DVD ioctl: sendkey\n" );
return DVD->getProvider()->sendKey( buffer, keyClass, lba, keyFormat );
}
#endif
/* following functions are local */
/*****************************************************************************
......@@ -363,7 +355,6 @@ IOReturn DVDioctl::send( IODVDMedia *DVD, IOMemoryDescriptor *buffer, const DVDK
static int DVDOpen( dev_t dev, int flags, int devtype, struct proc * )
{
IOStorageAccess level;
int i_err;
/* Check that the device hasn't already been opened */
if( b_inuse )
......@@ -408,19 +399,18 @@ static int DVDOpen( dev_t dev, int flags, int devtype, struct proc * )
level = (flags & FWRITE) ? kIOStorageAccessReaderWriter
: kIOStorageAccessReader;
if( p_dvd->open( p_this, 0, level) )
{
log( LOG_INFO, "DVD ioctl: IODVDMedia->open()\n" );
i_err = 0;
}
else
if( ! p_dvd->open( p_this, 0, level) )
{
log( LOG_INFO, "DVD ioctl: IODVDMedia object busy\n" );
b_inuse = false;
i_err = EBUSY;
return EBUSY;
}
return i_err;
p_drive = p_dvd->getProvider();
log( LOG_INFO, "DVD ioctl: IODVDMedia->open()\n" );
return 0;
}
/*****************************************************************************
......@@ -430,6 +420,9 @@ static int DVDClose( dev_t dev, int flags, int devtype, struct proc * )
{
/* Release the device */
p_dvd->close( p_this );
p_dvd = NULL;
p_drive = NULL;
b_inuse = false;
log( LOG_INFO, "DVD ioctl: IODVDMedia->close()\n" );
......@@ -455,27 +448,59 @@ static void DVDStrategy( buf_t * bp )
}
/*****************************************************************************
* DVDIoctl: issue an ioctl on the device
* DVDBlockIoctl: issue an ioctl on the block device
*****************************************************************************/
static int DVDIoctl( dev_t dev, u_long cmd, caddr_t addr, int flags,
static int DVDBlockIoctl( dev_t dev, u_long cmd, caddr_t addr, int flags,
struct proc *p )
{
dvdioctl_data_t * p_data = (dvdioctl_data_t *)addr;
switch( cmd )
{
case IODVD_READ_STRUCTURE:
//log( LOG_INFO, "DVD ioctl: IODVD_READ_STRUCTURE\n" );
log( LOG_INFO, "DVD ioctl: IODVD_READ_STRUCTURE\n" );
return 0;
case IODVD_SEND_KEY:
//log( LOG_INFO, "DVD ioctl: IODVD_SEND_KEY\n" );
return 0;
log( LOG_INFO, "DVD ioctl: send key to `%s', "
"buf %d, format %d, class %d, agid %d\n",
p_drive->getDeviceTypeName(),
(int)p_data->p_buffer, p_data->i_keyformat,
p_data->i_keyclass, p_data->i_agid );
#ifdef ACTIVATE_DANGEROUS_IOCTL
return p_drive->sendKey( (IOMemoryDescriptor *)p_data->p_buffer,
(DVDKeyClass)p_data->i_keyclass,
p_data->i_agid,
(DVDKeyFormat)p_data->i_keyformat );
#else
return -1;
#endif
case IODVD_REPORT_KEY:
//log( LOG_INFO, "DVD ioctl: IODVD_REPORT_KEY\n" );
return 0;
log( LOG_INFO, "DVD ioctl: report key from `%s', "
p_drive->getDeviceTypeName(),
"buf %d, class %d, lba %d, agid %d, format %d\n",
(int)p_data->p_buffer, p_data->i_keyclass, p_data->i_lba,
p_data->i_agid, p_data->i_keyformat );
#ifdef ACTIVATE_DANGEROUS_IOCTL
return p_drive->reportKey( (IOMemoryDescriptor *)p_data->p_buffer,
(DVDKeyClass)p_data->i_keyclass,
p_data->i_lba, p_data->i_agid,
(DVDKeyFormat)p_data->i_keyformat );
#else
return -1;
#endif
default:
//log( LOG_INFO, "DVD ioctl: unknown ioctl\n" );
log( LOG_INFO, "DVD ioctl: unknown ioctl\n" );
return EINVAL;
}
}
......@@ -591,7 +616,7 @@ static void DVDReadWriteCompletion( void * target,
if ( status != kIOReturnSuccess )
{
IOLog( "%s: %s.\n", /*p_this->name*/ "DVD ioctl",
IOLog( "DVD ioctl: %s (is the disc authenticated ?)\n",
p_this->stringFromReturn(status) );
}
......
......@@ -2,7 +2,7 @@
* DVDioctl.h: Linux-like DVD driver for Darwin and MacOS X
*****************************************************************************
* Copyright (C) 2001 VideoLAN
* $Id: DVDioctl.h,v 1.1 2001/04/02 23:30:41 sam Exp $
* $Id: DVDioctl.h,v 1.2 2001/04/04 02:49:18 sam Exp $
*
* Authors: Samuel Hocevar <sam@zoy.org>
*
......@@ -21,8 +21,49 @@
* under the License.
*****************************************************************************/
struct sum { int a, b, r; };
#define IODVD_READ_STRUCTURE _IOWR('B', 1, struct sum)
#define IODVD_SEND_KEY _IOWR('B', 2, struct sum)
#define IODVD_REPORT_KEY _IOWR('B', 3, struct sum)
#if defined(KERNEL)
/* Everything has already been defined */
#else
enum DVDKeyFormat
{
kCSSAGID = 0x00,
kChallengeKey = 0x01,
kKey1 = 0x02,
kKey2 = 0x03,
kTitleKey = 0x04,
kASF = 0x05,
kSetRegion = 0x06,
kRPCState = 0x08,
kCSS2AGID = 0x10,
kCPRMAGID = 0x11,
kInvalidateAGID = 0x3f
};
enum DVDKeyClass
{
kCSS_CSS2_CPRM = 0x00,
kRSSA = 0x01
};
#endif
typedef struct dvdioctl_data
{
void *p_buffer;
#if defined(KERNEL)
UInt32 i_lba;
UInt8 i_agid;
#else
u32 i_lba;
u8 i_agid;
#endif
int i_keyclass;
int i_keyformat;
} dvdioctl_data_t;
#define IODVD_READ_STRUCTURE _IOWR('B', 1, dvdioctl_data_t)
#define IODVD_SEND_KEY _IOWR('B', 2, dvdioctl_data_t)
#define IODVD_REPORT_KEY _IOWR('B', 3, dvdioctl_data_t)
......@@ -2,7 +2,7 @@
* dvd_css.c: Functions for DVD authentification and unscrambling
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: dvd_css.c,v 1.19 2001/04/02 23:30:41 sam Exp $
* $Id: dvd_css.c,v 1.20 2001/04/04 02:49:18 sam Exp $
*
* Author: Stphane Borel <stef@via.ecp.fr>
*
......@@ -71,7 +71,7 @@ int CSSTest( int i_fd )
{
int i_ret, i_copyright;
i_ret = dvd_ReadCopyright( i_fd, 0 /* i_layer */, &i_copyright );
i_ret = ioctl_ReadCopyright( i_fd, 0 /* i_layer */, &i_copyright );
if( i_ret < 0 )
{
......@@ -105,10 +105,11 @@ int CSSInit( css_t * p_css )
return -1;
case 1:
intf_WarnMsg( 3, "css info: already authenticated" );
return 0;
case 0:
intf_WarnMsg( 3, "css info: authenticating" );
intf_WarnMsg( 3, "css info: need to authenticate" );
}
/* Init sequence, request AGID */
......@@ -116,7 +117,7 @@ int CSSInit( css_t * p_css )
{
intf_WarnMsg( 3, "css info: requesting AGID %d", i );
i_ret = dvd_LUSendAgid( p_css );
i_ret = ioctl_LUSendAgid( p_css );
if( i_ret != -1 )
{
......@@ -127,7 +128,7 @@ int CSSInit( css_t * p_css )
intf_ErrMsg( "css error: AGID N/A, invalidating" );
p_css->i_agid = 0;
dvd_InvalidateAgid( p_css );
ioctl_InvalidateAgid( p_css );
}
/* Unable to authenticate without AGID */
......@@ -149,14 +150,14 @@ int CSSInit( css_t * p_css )
}
/* Send challenge to LU */
if( dvd_HostSendChallenge( p_css, p_buffer ) < 0 )
if( ioctl_HostSendChallenge( p_css, p_buffer ) < 0 )
{
intf_ErrMsg( "css error: failed sending challenge to LU" );
return -1;
}
/* Get key1 from LU */
if( dvd_LUSendKey1( p_css, p_buffer ) < 0)
if( ioctl_LUSendKey1( p_css, p_buffer ) < 0)
{
intf_ErrMsg( "css error: failed getting key1 from LU" );
return -1;
......@@ -189,7 +190,7 @@ int CSSInit( css_t * p_css )
}
/* Get challenge from LU */
if( dvd_LUSendChallenge( p_css, p_buffer ) < 0 )
if( ioctl_LUSendChallenge( p_css, p_buffer ) < 0 )
{
intf_ErrMsg( "css error: failed getting challenge from LU" );
return -1;
......@@ -211,7 +212,7 @@ int CSSInit( css_t * p_css )
}
/* Send key2 to LU */
if( dvd_HostSendKey2( p_css, p_buffer ) < 0 )
if( ioctl_HostSendKey2( p_css, p_buffer ) < 0 )
{
intf_ErrMsg( "css error: failed sending key2 to LU" );
return -1;
......@@ -238,14 +239,15 @@ int CSSInit( css_t * p_css )
return -1;
case 1:
intf_WarnMsg( 3, "css info: already authenticated" );
return 0;
case 0:
intf_WarnMsg( 3, "css info: getting disc key" );
intf_WarnMsg( 3, "css info: need to get disc key" );
}
/* Get encrypted disc key */
if( dvd_ReadKey( p_css, p_buffer ) < 0 )
if( ioctl_ReadKey( p_css, p_buffer ) < 0 )
{
intf_ErrMsg( "css error: could not read Disc Key" );
return -1;
......@@ -262,21 +264,22 @@ int CSSInit( css_t * p_css )
switch( CSSGetASF( p_css ) )
{
case -1:
case 0:
return -1;
case 1:
intf_WarnMsg( 3, "css info: successfully authenticated" );
return 0;
}
return 0;
case 0:
intf_WarnMsg( 3, "css info: no way to authenticate" );
}
#else /* HAVE_CSS */
intf_ErrMsg( "css error: CSS decryption is disabled in this module" );
#endif /* HAVE_CSS */
return -1;
#endif /* HAVE_CSS */
}
/*****************************************************************************
......@@ -475,8 +478,10 @@ int CSSGetKey( css_t * p_css )
p_title_key[i_highest].pi_key, KEY_SIZE );
return 0;
#else /* HAVE_CSS */
return 1;
#endif /* HAVE_CSS */
}
......@@ -522,15 +527,16 @@ int CSSDescrambleSector( dvd_key_t pi_key, u8* pi_sec )
}
return 0;
#else /* HAVE_CSS */
return 1;
#endif /* HAVE_CSS */
}
#ifdef HAVE_CSS
/*
* Following functions are local
*/
/* Following functions are local */
/*****************************************************************************
* CSSGetASF : Get Authentification success flag
......@@ -546,7 +552,7 @@ static int CSSGetASF( css_t *p_css )
for( p_css->i_agid = 0 ; p_css->i_agid < 4 ; p_css->i_agid++ )
{
if( dvd_LUSendASF( p_css, &i_asf ) == 0 )
if( ioctl_LUSendASF( p_css, &i_asf ) == 0 )
{
intf_WarnMsg( 3, "css info: %sauthenticated", i_asf ? "":"not " );
......@@ -898,5 +904,6 @@ static int CSSCracker( int i_start,
return i_exit;
}
#endif /* HAVE_CSS */
......@@ -2,7 +2,7 @@
* dvd_ifo.h: Structures for ifo parsing
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: dvd_ifo.h,v 1.9 2001/04/01 07:31:38 stef Exp $
* $Id: dvd_ifo.h,v 1.10 2001/04/04 02:49:18 sam Exp $
*
* Author: Stphane Borel <stef@via.ecp.fr>
*
......
......@@ -2,7 +2,7 @@
* dvd_ioctl.c: DVD ioctl replacement function
*****************************************************************************
* Copyright (C) 1999-2001 VideoLAN
* $Id: dvd_ioctl.c,v 1.4 2001/04/02 23:30:41 sam Exp $
* $Id: dvd_ioctl.c,v 1.5 2001/04/04 02:49:18 sam Exp $
*
* Authors: Markus Kuespert <ltlBeBoy@beosmail.com>
* Samuel Hocevar <sam@zoy.org>
......@@ -29,6 +29,7 @@
#include <sys/types.h>
#include <netinet/in.h>
#ifdef HAVE_SYS_DVDIO_H
# include <sys/ioctl.h>
# include <sys/dvdio.h>
......@@ -44,12 +45,16 @@
#endif
#ifdef SYS_DARWIN1_3
# include <sys/ioctl.h>
# include <DVDioctl/DVDioctl.h>
#endif
#include "common.h"
#include "intf_msg.h"
#ifdef SYS_DARWIN1_3
# include "DVDioctl/DVDioctl.h"
#endif
#include "dvd_css.h"
#include "dvd_ioctl.h"
......@@ -57,129 +62,117 @@
* Local prototypes - BeOS specific
*****************************************************************************/
#if defined( SYS_BEOS )
static void InitCommand ( struct cdrom_generic_command *p_cgc,
void *buf, int i_len, int i_type );
static int SendCommand ( int i_fd, struct cdrom_generic_command *p_cgc );
static void BeInitRDC ( raw_device_command *, void *, int );
#endif
/*****************************************************************************
* dvd_ReadKey:
*****************************************************************************
*
* ioctl_ReadCopyright: check whether the disc is encrypted or not
*****************************************************************************/
int dvd_ReadKey( css_t *p_css, u8 *p_key )
int ioctl_ReadCopyright( int i_fd, int i_layer, int *pi_copyright )
{
#if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
int i_ret;
dvd_struct dvd;
dvd.type = DVD_STRUCT_DISCKEY;
dvd.disckey.agid = p_css->i_agid;
memset( dvd.disckey.value, 0, 2048 );
#if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
dvd_struct dvd;
i_ret = ioctl( p_css->i_fd, DVD_READ_STRUCT, &dvd );
dvd.type = DVD_STRUCT_COPYRIGHT;
dvd.copyright.layer_num = i_layer;
if( i_ret < 0 )
{
return i_ret;
}
i_ret = ioctl( i_fd, DVD_READ_STRUCT, &dvd );
memcpy( p_key, dvd.disckey.value, 2048 );
return i_ret;
*pi_copyright = dvd.copyright.cpst;
#elif defined( SYS_BEOS )
int i_ret, size;
u8 p_buf[ 2048 + 4];
struct cdrom_generic_command cgc;
raw_device_command rdc;
u8 p_buffer[ 8 ];
size = 2048 + 4;
BeInitRDC( &rdc, p_buffer, 8 );
InitCommand( &cgc, p_buf, size, CGC_DATA_READ );
rdc.flags = B_RAW_DEVICE_DATA_IN;
rdc.command[ 0 ] = GPCMD_READ_DVD_STRUCTURE;
rdc.command[ 6 ] = i_layer;
rdc.command[ 7 ] = DVD_STRUCT_COPYRIGHT;
cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
cgc.cmd[7] = DVD_STRUCT_DISCKEY;
cgc.cmd[8] = size >> 8;
cgc.cmd[9] = size & 0xff;
cgc.cmd[10] = p_css->i_agid << 6;
*pi_copyright = p_buffer[ 4 ];
i_ret = SendCommand( p_css->i_fd, &cgc );
#elif defined( SYS_DARWIN1_3 )
intf_ErrMsg( "css error: DVD ioctls not fully functional yet" );
if( i_ret < 0 )
{
return i_ret;
}
intf_ErrMsg( "css error: assuming disc is unencrypted" );
*pi_copyright = 0;
memcpy( p_key, p_buf + 4, 2048 );
return i_ret;
i_ret = 0;
#else
return -1;
/* DVD ioctls unavailable - do as if the ioctl failed */
i_ret = -1;
#endif
return i_ret;
}
/*****************************************************************************
* dvd_ReadCopyright:
*****************************************************************************
*
* ioctl_ReadKey: get the disc key
*****************************************************************************/
int dvd_ReadCopyright( int i_fd, int i_layer, int *pi_copyright )
int ioctl_ReadKey( css_t *p_css, u8 *p_key )
{
#if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
int i_ret;
#if defined( HAVE_SYS_DVDIO_H ) || defined( LINUX_DVD )
dvd_struct dvd;
dvd.type = DVD_STRUCT_COPYRIGHT;
dvd.copyright.layer_num = i_layer;
dvd.type = DVD_STRUCT_DISCKEY;
dvd.disckey.agid = p_css->i_agid;
i_ret = ioctl( i_fd, DVD_READ_STRUCT, &dvd );
memset( dvd.disckey.value, 0, 2048 );
*pi_copyright = dvd.copyright.cpst;
i_ret = ioctl( p_css->i_fd, DVD_READ_STRUCT, &dvd );
if( i_ret < 0 )
{
return i_ret;
}
#elif defined( SYS_BEOS )
int i_ret;
u8 p_buf[8];
struct cdrom_generic_command cgc;
memcpy( p_key, dvd.disckey.value, 2048 );
InitCommand( &cgc, p_buf, sizeof(p_buf), CGC_DATA_READ );
#elif defined( SYS_BEOS )
raw_device_command rdc;
u8 p_buffer[ 2048 + 4 ];
cgc.cmd[0] = GPCMD_READ_DVD_STRUCTURE;
BeInitRDC( &rdc, p_buffer, 2048 + 4 );
cgc.cmd[6] = i_layer;
cgc.cmd[7] = DVD_STRUCT_COPYRIGHT;
cgc.cmd[8] = cgc.buflen >> 8;
cgc.cmd[9] = cgc.buflen & 0xff;
rdc.flags = B_RAW_DEVICE_DATA_IN;
rdc.command[ 0 ] = GPCMD_READ_DVD_STRUCTURE;
rdc.command[ 7 ] = DVD_STRUCT_DISCKEY;
rdc.command[ 10 ] = p_css->i_agid << 6;
i_ret = SendCommand( i_fd, &cgc );
i_ret = ioctl( p_css->i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );
*pi_copyright = p_buf[4];
if( i_ret < 0 )
{