dvd_region.c 6.79 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
 * Set / inspect region settings on a DVD drive. This is most interesting
 * on a RPC Phase-2 drive, of course.
 *
 * Usage: dvd_region [ -d device ] [ [ -s ] [ -r region ] ]
 *
 * Based on code from Jens Axboe <axboe@suse.de>.
 *
 * FIXME:  This code does _not_ work yet.
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
17
#include <getopt.h>
18

19
#include <dvdcss/dvdcss.h>
20
21
22
23
24
25
26

#include "config.h"
#include "common.h"
#include "ioctl.h"

#define DEFAULT_DEVICE "/dev/dvd"

27
28
29
30
31
32
33
/*****************************************************************************
 * ioctl_SendRPC: set RPC status for the drive
 *****************************************************************************/
static int ioctl_SendRPC( int i_fd, int i_pdrc )
{
    int i_ret;

34
35
36
37
    /* Shut up warnings about unused parameters. */
    (void)i_fd;
    (void)i_pdrc;

38
#if defined( HAVE_LINUX_DVD_STRUCT ) && defined( DVD_HOST_SEND_RPC_STATE )
39
    dvd_authinfo auth_info = { 0 };
40
41
42
43
44
45
46
47
48
49
50

    auth_info.type = DVD_HOST_SEND_RPC_STATE;
    auth_info.hrpcs.pdrc = i_pdrc;

    i_ret = ioctl( i_fd, DVD_AUTH, &auth_info );

#elif defined( HAVE_LINUX_DVD_STRUCT )
    /* FIXME: OpenBSD doesn't know this */
    i_ret = -1;

#elif defined( HAVE_BSD_DVD_STRUCT )
51
    struct dvd_authinfo auth_info = { 0 };
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

    auth_info.format = DVD_SEND_RPC;
    auth_info.region = i_pdrc;

    i_ret = ioctl( i_fd, DVDIOCSENDKEY, &auth_info );

#elif defined( __BEOS__ )
    INIT_RDC( GPCMD_SEND_KEY, 8 );

    rdc.command[ 10 ] = DVD_SEND_RPC;

    p_buffer[ 1 ] = 6;
    p_buffer[ 4 ] = i_pdrc;

    i_ret = ioctl( i_fd, B_RAW_DEVICE_COMMAND, &rdc, sizeof(rdc) );

#elif defined( HPUX_SCTL_IO )
    INIT_SCTL_IO( GPCMD_SEND_KEY, 8 );

    sctl_io.cdb[ 10 ] = DVD_SEND_RPC;

    p_buffer[ 1 ] = 6;
    p_buffer[ 4 ] = i_pdrc;

    i_ret = ioctl( i_fd, SIOC_IO, &sctl_io );

#elif defined( SOLARIS_USCSI )
    INIT_USCSI( GPCMD_SEND_KEY, 8 );

    rs_cdb.cdb_opaque[ 10 ] = DVD_SEND_RPC;

    p_buffer[ 1 ] = 6;
    p_buffer[ 4 ] = i_pdrc;

    i_ret = SolarisSendUSCSI( i_fd, &sc );

    if( i_ret < 0 || sc.uscsi_status )
    {
        i_ret = -1;
    }

#elif defined( DARWIN_DVD_IOCTL )
    INIT_DVDIOCTL( dk_dvd_send_key_t, DVDRegionPlaybackControlInfo,
                   kDVDKeyFormatSetRegion );

    dvd.keyClass = kDVDKeyClassCSS_CPPM_CPRM;
    dvdbs.driveRegion = i_pdrc;

    i_ret = ioctl( i_fd, DKIOCDVDSENDKEY, &dvd );

#elif defined( WIN32 )
    if( WIN2K ) /* NT/2k/XP */
    {
        INIT_SPTD( GPCMD_SEND_KEY, 8 );

        sptd.Cdb[ 10 ] = DVD_SEND_RPC;

        p_buffer[ 1 ] = 6;
        p_buffer[ 4 ] = i_pdrc;

        i_ret = SEND_SPTD( i_fd, &sptd, &tmp );
    }
    else
    {
        INIT_SSC( GPCMD_SEND_KEY, 8 );

        ssc.CDBByte[ 10 ] = DVD_SEND_RPC;

        p_buffer[ 1 ] = 6;
        p_buffer[ 4 ] = i_pdrc;

        i_ret = WinSendSSC( i_fd, &ssc );
    }

#elif defined( __QNXNTO__ )

    INIT_CPT( GPCMD_SEND_KEY, 8 );

    p_cpt->cam_cdb[ 10 ] = DVD_SEND_RPC;

    p_buffer[ 1 ] = 6;
    p_buffer[ 4 ] = i_pdrc;

    i_ret = devctl(i_fd, DCMD_CAM_PASS_THRU, p_cpt, structSize, NULL);

#elif defined( __OS2__ )
    INIT_SSC( GPCMD_SEND_KEY, 8 );

    sdc.command[ 10 ] = DVD_SEND_RPC;

    p_buffer[ 1 ] = 6;
    p_buffer[ 4 ] = i_pdrc;

    i_ret = DosDevIOCtl( i_fd, IOCTL_CDROMDISK, CDROMDISK_EXECMD,
                         &sdc, sizeof(sdc), &ulParamLen,
                         p_buffer, sizeof(p_buffer), &ulDataLen );

#else
#   error "DVD ioctls are unavailable on this system"

#endif
    return i_ret;
}

static int set_region(int fd, int region)
157
158
159
160
161
162
163
164
{
  int ret, region_mask;

  if(region > 8 || region <= 0) {
    printf("Invalid region( %d)\n", region);
    return 1;
  }
  printf("Setting drive region can only be done a finite " \
165
         "number of times, press Ctrl-C now to cancel!\n");
166
167
168
169
170
171
172
173
  getchar();

  region_mask = 0xff & ~(1 << (region - 1));
  printf("Setting region to %d( %x)\n", region, region_mask);
  if( (ret = ioctl_SendRPC(fd, region_mask)) < 0) {
    perror("dvd_region");
    return ret;
  }
174

175
176
177
  return 0;
}

178
static int print_region(int fd)
179
180
181
182
{
  int type, region_mask, rpc_scheme;
  int region = 1;
  int ret;
Diego Biurrun's avatar
Diego Biurrun committed
183

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  printf("Drive region info:\n");

  if( (ret = ioctl_ReportRPC(fd, &type, &region_mask, &rpc_scheme)) < 0) {
    perror("dvd_region");
    return ret;
  }

  printf("Type: ");
  switch( type ) {
  case 0:
    printf("No drive region setting\n");
    break;
  case 1:
    printf("Drive region is set\n");
    break;
  case 2:
    printf("Drive region is set, with additional " \
Diego Biurrun's avatar
Diego Biurrun committed
201
           "restrictions required to make a change\n");
202
203
204
    break;
  case 3:
    printf("Drive region has been set permanently, but " \
Diego Biurrun's avatar
Diego Biurrun committed
205
           "may be reset by the vendor if necessary\n");
206
207
208
209
210
211
212
213
214
215
216
217
    break;
  default:
    printf("Invalid( %x)\n", type);
    break;
  }

  // printf("%d vendor resets available\n", ai->lrpcs.vra);
  // printf("%d user controlled changes available\n", ai->lrpcs.ucca);
  printf("Region: ");
  if( region_mask)
    while(region_mask) {
      if( !(region_mask & 1) )
Diego Biurrun's avatar
Diego Biurrun committed
218
        printf("%d playable\n", region);
219
220
221
222
      region++;
      region_mask >>= 1;
    }
  else
223
    printf("non-RPC( all)\n");
224
225
226
227
228

  printf("RPC Scheme: ");
  switch( rpc_scheme ) {
  case 0:
    printf("The Logical Unit does not enforce Region " \
Diego Biurrun's avatar
Diego Biurrun committed
229
           "Playback Controls( RPC)\n");
230
231
232
    break;
  case 1:
    printf("The Logical Unit _shall_ adhere to the "
Diego Biurrun's avatar
Diego Biurrun committed
233
234
           "specification and all requirements of the " \
           "CSS license agreement concerning RPC\n");
235
236
237
238
239
240
241
242
243
244
    break;
  default:
    printf("Reserved( %x)\n", rpc_scheme);
  }

  return 0;
}

static void usage(void)
{
Diego Biurrun's avatar
Diego Biurrun committed
245
246
247
  fprintf( stderr,
           "Usage: dvd_region [ -d device ] [ [ -s ] [ -r region ] ]\n" );
}
248
249
250

int main(int argc, char *argv[])
{
251
  char device_name[FILENAME_MAX], c, set, region = 0;
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
  int fd, ret;

  strcpy(device_name, DEFAULT_DEVICE);
  set = 0;
  while( (c = getopt(argc, argv, "d:sr:h?")) != EOF ) {
    switch( c ) {
    case 'd':
      strncpy(device_name, optarg, FILENAME_MAX - 1);
      break;
    case 's':
      set = 1;
      break;
    case 'r':
      region = strtoul(optarg, NULL, 10);
      printf("region %d\n", region);
      break;
    case 'h':
    case '?':
    default:
      usage();
      return -1;
      break;
    }
  }

  if( optind != argc ) {
    fprintf(stderr, "Unknown argument\n");
    usage();
    return -1;
  }

283
  /* TODO: use dvdcss_open instead of open */
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304

  if( (fd = open(device_name, O_RDONLY | O_NONBLOCK)) < 0 ) {
    perror("open");
    usage();
    return 1;
  }

  {
    int copyright;
    ret = ioctl_ReadCopyright( fd, 0, &copyright );
    printf( "ret %d, copyright %d\n", ret, copyright );
  }

  if( (ret = print_region(fd)) < 0 )
    return ret;

  if( set ) {
    if( !region ) {
      fprintf( stderr, "you must specify the region!\n" );
      exit(0);
    }
Diego Biurrun's avatar
Diego Biurrun committed
305

306
307
308
309
310
311
    if( (ret = set_region(fd, region)) < 0 )
      return ret;
  }

  exit( 0 );
}