1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sys/types.h>
#include <fcntl.h>
#include <volmgt.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/dkio.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <libintl.h>
#include <limits.h>
#include "transport.h"
#include "mmc.h"
#include "device.h"
#include "util.h"
#include "msgs.h"
#include "misc_scsi.h"
#include "toshiba.h"
#include "main.h"
/*
* Old sun drives have a vendor specific mode page for setting/getting speed.
* Also they use a different method for extracting audio.
* We have the device inquiry strings at this time. This is used to enable
* us to use older sun drives to extract audio.
*/
static int
is_old_sun_drive(cd_device *dev)
{
/*
* If we have a SONY CDU 561, CDU 8012, or TOSHIBA model with XMa we
* need to handle these drives a bit differently.
*/
if (strncmp("SONY", (const char *)&dev->d_inq[8], 4) == 0) {
if (strncmp("CDU 561", (const char *)&dev->d_inq[16], 7) == 0)
return (1);
if (strncmp("CDU-8012", (const char *)&dev->d_inq[16], 8) == 0)
return (1);
}
if ((strncmp("TOSHIBA", (const char *)&dev->d_inq[8], 7) == 0) &&
(strncmp("XM", (const char *)&dev->d_inq[16], 2) == 0)) {
char product_id[17];
/* Changing speed is not allowed for 32X TOSHIBA drives */
if (strncmp("SUN32XCD", (const char *)&dev->d_inq[24], 8) == 0)
dev->d_cap |= DEV_CAP_SETTING_SPEED_NOT_ALLOWED;
(void) strncpy(product_id, (const char *)&dev->d_inq[16], 16);
product_id[16] = 0;
if (strstr(product_id, "SUN") != NULL)
return (1);
}
return (0);
}
/*
* returns a cd_device handle for a node returned by lookup_device()
* also takes the user supplied name and stores it inside the node
*/
cd_device *
get_device(char *user_supplied, char *node)
{
cd_device *dev;
int fd;
uchar_t *cap;
char devnode[PATH_MAX];
int size;
struct dk_minfo mediainfo;
int use_cd_speed = 0;
/*
* we need to resolve any link paths to avoid fake files
* such as /dev/rdsk/../../export/file.
*/
TRACE(traceall_msg("get_device(%s, %s)\n", user_supplied ?
user_supplied : "<nil>", node ? node : "<nil>"));
size = resolvepath(node, devnode, PATH_MAX);
if ((size <= 0) || (size >= (PATH_MAX - 1)))
return (NULL);
/* resolvepath may not return a null terminated string */
devnode[size] = '\0';
/* the device node must be in /devices/ or /vol/dev/rdsk */
if ((strncmp(devnode, "/devices/", 9) != 0) &&
(strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
return (NULL);
/*
* Since we are currently running with the user euid it is
* safe to try to open the file without checking access.
*/
fd = open(devnode, O_RDONLY|O_NDELAY);
if (fd < 0) {
TRACE(traceall_msg("Cannot open %s: %s\n", node,
strerror(errno)));
return (NULL);
}
dev = (cd_device *)my_zalloc(sizeof (cd_device));
dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
(void) strcpy(dev->d_node, devnode);
dev->d_fd = fd;
dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
if (!inquiry(fd, dev->d_inq)) {
TRACE(traceall_msg("Inquiry failed on device %s\n", node));
if (debug) {
(void) printf("USCSI ioctl failed %d\n",
uscsi_error);
}
free(dev->d_inq);
free(dev->d_node);
(void) close(dev->d_fd);
free(dev);
return (NULL);
}
if (debug) {
cap = (uchar_t *)my_zalloc(18);
(void) printf("Checking device type\n");
if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
if (cap[2] & 0x10)
(void) printf("DVD-R read support\n");
if (cap[3] & 0x10)
(void) printf("DVD-R write support\n");
if (cap[5] & 0x4)
(void) printf("R-W supported\n");
if (cap[2] & 0x20)
(void) printf("DVD-RAM read supported\n");
if (cap[3] & 0x20)
(void) printf("DVD-RAM write supported\n");
} else {
(void) printf("Could not read mode page 2A! \n");
}
free(cap);
}
/* Detect if it's a Lite-ON drive with a streaming CD problem */
if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
(strncmp("LTR-48", (const char *)&dev->d_inq[16], 6) == 0)) {
use_cd_speed = 1;
}
/*
* a workaround for the firmware problem in LITE-ON COMBO drives.
* streaming for these drives sets it only to max speed regardless
* of requested speed. cd_speed_ctrl allow speeds less than max
* to be set but not max thus the code below. (x48 is max speed
* for these drives).
*/
if ((strncmp("LITE-ON", (const char *)&dev->d_inq[8], 7) == 0) &&
(strncmp("COMBO SOHC-4836VS",
(const char *)&dev->d_inq[16], 17) == 0))
if (requested_speed < 48)
use_cd_speed = 1;
cap = (uchar_t *)my_zalloc(8);
if (is_old_sun_drive(dev)) {
dev->d_read_audio = toshiba_read_audio;
dev->d_speed_ctrl = toshiba_speed_ctrl;
} else {
/*
* If the CD Read Feature is supported, READ CD will work
* and will return jitter free audio data. Otherwise, look
* at Page Code 2A for this information.
*/
if (ftr_supported(fd, MMC_FTR_CD_READ) == 1) {
dev->d_read_audio = read_audio_through_read_cd;
dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
} else if (get_mode_page(fd, 0x2A, 0, 8, cap)) {
if (cap[5] & 1) {
dev->d_read_audio = read_audio_through_read_cd;
if (cap[5] & 2)
dev->d_cap |= DEV_CAP_ACCURATE_CDDA;
}
}
/*
* If the Real Time Streaming Feature is supported then
* Real-time streaming commands can be used for speed control
* (except when we want to use cd_speed_ctrl explicitly which
* is specified by setting use_cd_speed to 1).
* Otherwise try SET CD SPEED.
*/
if ((ftr_supported(fd, MMC_FTR_RT_STREAM) == 1) &&
!use_cd_speed) {
dev->d_speed_ctrl = rt_streaming_ctrl;
if (debug)
(void) printf("using rt speed ctrl\n");
} else {
dev->d_speed_ctrl = cd_speed_ctrl;
if (debug)
(void) printf("using cd speed ctrl\n");
}
}
if (dev->d_read_audio != NULL)
dev->d_cap |= DEV_CAP_EXTRACT_CDDA;
dev->d_blksize = 0;
/*
* Find the block size of the device so we can translate
* the reads/writes to the device blocksize.
*/
if (ioctl(fd, DKIOCGMEDIAINFO, &mediainfo) < 0) {
/*
* If DKIOCGMEDIAINFO fails we'll try to get
* the blocksize from the device itself.
*/
if (debug)
(void) printf("DKIOCGMEDIAINFO failed\n");
if (read_capacity(fd, cap))
dev->d_blksize = read_scsi32(cap + 4);
} else {
dev->d_blksize = mediainfo.dki_lbsize;
}
if (debug) {
uint_t bsize;
(void) printf("blocksize = %d\n", dev->d_blksize);
(void) printf("read_format_capacity = %d \n",
read_format_capacity(fd, &bsize));
}
/*
* Some devices will return invalid blocksizes. ie. Toshiba
* drives will return 2352 when an audio CD is inserted.
* Older Sun drives will use 512 byte block sizes. All newer
* drives should have 2k blocksizes.
*/
if (((dev->d_blksize != 512) && (dev->d_blksize != 2048))) {
if (is_old_sun_drive(dev)) {
dev->d_blksize = 512;
} else {
dev->d_blksize = 2048;
}
if (debug)
(void) printf(" switching to %d\n", dev->d_blksize);
}
free(cap);
if (user_supplied) {
dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
(void) strcpy(dev->d_name, user_supplied);
}
TRACE(traceall_msg("Got device %s\n", node));
return (dev);
}
void
fini_device(cd_device *dev)
{
free(dev->d_inq);
free(dev->d_node);
(void) close(dev->d_fd);
if (dev->d_name)
free(dev->d_name);
free(dev);
}
static int
vol_name_to_dev_node(char *vname, char *found)
{
struct stat statbuf;
char *p1;
int i;
if (vname == NULL)
return (0);
if (vol_running)
(void) volmgt_check(vname);
p1 = media_findname(vname);
if (p1 == NULL)
return (0);
if (stat(p1, &statbuf) < 0) {
free(p1);
return (0);
}
if (S_ISDIR(statbuf.st_mode)) {
for (i = 0; i < 16; i++) {
(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
if (access(found, F_OK) >= 0)
break;
}
if (i == 16) {
free(p1);
return (0);
}
} else {
(void) strlcpy(found, p1, PATH_MAX);
}
free(p1);
return (1);
}
/*
* Searches for volume manager's equivalent char device for the
* supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
*/
static int
vol_lookup(char *supplied, char *found)
{
char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
int i, ret;
(void) strlcpy(tmpstr, supplied, PATH_MAX);
if ((p = volmgt_symname(tmpstr)) == NULL) {
if (strrchr(tmpstr, 's') == NULL)
return (0);
*((char *)(strrchr(tmpstr, 's') + 1)) = 0;
for (i = 0; i < 16; i++) {
(void) snprintf(tmpstr1, PATH_MAX, "%s%d", tmpstr, i);
if ((p = volmgt_symname(tmpstr1)) != NULL)
break;
}
if (p == NULL)
return (0);
}
ret = vol_name_to_dev_node(p, found);
free(p);
return (ret);
}
/*
* Builds an open()able device path from a user supplied node which can be
* of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
* cdrom[n]
* returns the path found in 'found' and returns 1. Otherwise returns 0.
*/
int
lookup_device(char *supplied, char *found)
{
struct stat statbuf;
int fd;
char tmpstr[PATH_MAX];
/* If everything is fine and proper, no need to analyze */
if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
(void) close(fd);
(void) strlcpy(found, supplied, PATH_MAX);
return (1);
}
if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
return (vol_lookup(supplied, found));
if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
(char *)strrchr(supplied, '/'));
if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
(void) close(fd);
(void) strlcpy(found, supplied, PATH_MAX);
return (1);
}
if ((access(tmpstr, F_OK) == 0) && vol_running)
return (vol_lookup(tmpstr, found));
else
return (0);
}
if ((strncmp(supplied, "cdrom", 5) != 0) &&
(strlen(supplied) < 32)) {
(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
supplied);
if (access(tmpstr, F_OK) < 0) {
(void) strcat(tmpstr, "s2");
}
if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
(void) close(fd);
(void) strlcpy(found, tmpstr, PATH_MAX);
return (1);
}
if ((access(tmpstr, F_OK) == 0) && vol_running)
return (vol_lookup(tmpstr, found));
}
return (vol_name_to_dev_node(supplied, found));
}
/*
* Opens the device node name passed and returns 1 (true) if the
* device is a CD.
*/
static int
is_cd(char *node)
{
int fd;
struct dk_cinfo cinfo;
int ret = 1;
fd = open(node, O_RDONLY|O_NDELAY);
if (fd < 0) {
ret = 0;
} else if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
ret = 0;
} else if (cinfo.dki_ctype != DKC_CDROM) {
ret = 0;
}
if (fd >= 0) {
(void) close(fd);
}
return (ret);
}
static void
print_header(void)
{
/* l10n_NOTE : Column spacing should be kept same */
(void) printf(gettext(" Node Connected Device"));
/* l10n_NOTE : Column spacing should be kept same */
(void) printf(gettext(" Device type\n"));
(void) printf(
"----------------------+--------------------------------");
(void) printf("+-----------------\n");
}
/*
* returns the number of writers or CD/DVD-roms found and the path of
* the first device found depending on the mode argument.
* possible mode values are:
* SCAN_ALL_CDS Scan all CD/DVD devices. Return first CD-RW found.
* SCAN_WRITERS Scan all CD-RW devices. Return first one found.
* SCAN_LISTDEVS List all devices found.
*/
int
scan_for_cd_device(int mode, cd_device **found)
{
DIR *dir;
struct dirent *dirent;
char sdev[PATH_MAX], dev[PATH_MAX];
cd_device *t_dev;
int writers_found = 0;
int header_printed = 0;
int is_writer;
int retry = 0;
int total_devices_found;
int total_devices_found_last_time = 0;
int defer = 0;
#define MAX_RETRIES_FOR_SCANNING 5
TRACE(traceall_msg("scan_for_cd_devices (mode=%d) called\n", mode));
if (mode) {
if (vol_running)
defer = 1;
(void) printf(gettext("Looking for CD devices...\n"));
}
try_again:
dir = opendir("/dev/rdsk");
if (dir == NULL)
return (0);
writers_found = 0;
total_devices_found = 0;
while ((dirent = readdir(dir)) != NULL) {
if (dirent->d_name[0] == '.')
continue;
(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
dirent->d_name);
if (strcmp("s2", (char *)strrchr(sdev, 's')) != 0)
continue;
if (!lookup_device(sdev, dev))
continue;
if (!is_cd(dev))
continue;
if ((t_dev = get_device(NULL, dev)) == NULL) {
continue;
}
total_devices_found++;
is_writer = !(check_device(t_dev, CHECK_DEVICE_NOT_WRITABLE));
if (is_writer) {
writers_found++;
if ((writers_found == 1) && (mode != SCAN_LISTDEVS)) {
*found = t_dev;
}
} else if ((mode == SCAN_ALL_CDS) && (writers_found == 0) &&
(total_devices_found == 1) && found) {
/* We found a CD-ROM or DVD-ROM */
*found = t_dev;
}
if ((mode == SCAN_LISTDEVS) && (!defer)) {
char *sn;
sn = volmgt_symname(sdev);
if (!header_printed) {
print_header();
header_printed = 1;
}
/* show vendor, model, firmware rev and device type */
(void) printf(" %-21.21s| %.8s %.16s %.4s | %s%s\n",
sn ? sn : sdev, &t_dev->d_inq[8],
&t_dev->d_inq[16], &t_dev->d_inq[32],
gettext("CD Reader"),
is_writer ? gettext("/Writer") : "");
if (sn)
free(sn);
}
if ((found != NULL) && ((*found) != t_dev))
fini_device(t_dev);
}
(void) closedir(dir);
/*
* If volume manager is running we'll do a retry in case the
* user has just inserted media, This should allow vold
* enough time to create the device links. If we cannot
* find any devices, or we find any new devices we'll retry
* again up to MAX_RETRIES_FOR_SCANNING
*/
retry++;
if ((retry <= MAX_RETRIES_FOR_SCANNING) && vol_running) {
if (defer || ((mode != SCAN_LISTDEVS) &&
(writers_found == 0))) {
if ((total_devices_found == 0) ||
(total_devices_found !=
total_devices_found_last_time)) {
/* before we rescan remove the device found */
if (total_devices_found != 0 && t_dev) {
fini_device(t_dev);
}
total_devices_found_last_time =
total_devices_found;
(void) sleep(2);
goto try_again;
} else {
/* Do the printing this time */
defer = 0;
goto try_again;
}
}
}
if ((mode & SCAN_WRITERS) || writers_found)
return (writers_found);
else
return (total_devices_found);
}
/*
* Check device for various conditions/capabilities
* If EXIT_IF_CHECK_FAILED set in cond then it will also exit after
* printing a message.
*/
int
check_device(cd_device *dev, int cond)
{
uchar_t *disc_info, disc_status = 0, erasable = 0;
uchar_t page_code[4];
char *errmsg = NULL;
if ((errmsg == NULL) && (cond & CHECK_TYPE_NOT_CDROM) &&
((dev->d_inq[0] & 0x1f) != 5)) {
errmsg =
gettext("Specified device does not appear to be a CDROM");
}
if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_READY) &&
!test_unit_ready(dev->d_fd)) {
errmsg = gettext("Device not ready");
}
/* Look at the capabilities page for this information */
if ((errmsg == NULL) && (cond & CHECK_DEVICE_NOT_WRITABLE)) {
if (!get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) ||
((page_code[3] & 1) == 0)) {
errmsg = gettext("Target device is not a CD writer");
}
}
if ((errmsg == NULL) && (cond & CHECK_NO_MEDIA)) {
if (!test_unit_ready(dev->d_fd) && (uscsi_status == 2) &&
((RQBUFLEN - rqresid) >= 14) &&
((SENSE_KEY(rqbuf) & 0x0f) == 2) && (ASC(rqbuf) == 0x3A) &&
((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1) ||
(ASCQ(rqbuf) == 2))) {
/* medium not present */
errmsg = gettext("No media in device");
}
}
/* Issue READ DISC INFORMATION mmc command */
if ((errmsg == NULL) && ((cond & CHECK_MEDIA_IS_NOT_BLANK) ||
(cond & CHECK_MEDIA_IS_NOT_WRITABLE) ||
(cond & CHECK_MEDIA_IS_NOT_ERASABLE))) {
disc_info = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
if (!read_disc_info(dev->d_fd, disc_info)) {
errmsg = gettext("Cannot obtain disc information");
} else {
disc_status = disc_info[2] & 0x03;
erasable = disc_info[2] & 0x10;
}
free(disc_info);
if (errmsg == NULL) {
if (!erasable && (cond & CHECK_MEDIA_IS_NOT_ERASABLE))
errmsg = gettext(
"Media in the device is not erasable");
else if ((disc_status != 0) &&
(cond & CHECK_MEDIA_IS_NOT_BLANK))
errmsg = gettext(
"Media in the device is not blank");
else if ((disc_status == 2) &&
(cond & CHECK_MEDIA_IS_NOT_WRITABLE) &&
((device_type != DVD_PLUS_W) &&
(device_type != DVD_PLUS)))
errmsg = gettext(
"Media in the device is not writable");
}
}
if (errmsg) {
if (cond & EXIT_IF_CHECK_FAILED) {
err_msg("%s.\n", errmsg);
exit(1);
}
return (1);
}
return (0);
}
/*
* Generic routine for writing whatever the next track is and taking
* care of the progress bar. Mode tells the track type (audio or data).
* Data from track is taken from the byte stream h
*/
void
write_next_track(int mode, bstreamhandle h)
{
struct track_info *ti;
struct trackio_error *te;
off_t size;
ti = (struct track_info *)my_zalloc(sizeof (*ti));
if ((build_track_info(target, -1, ti) == 0) ||
((ti->ti_flags & TI_NWA_VALID) == 0)) {
if ((device_type == DVD_PLUS) || (device_type ==
DVD_PLUS_W)) {
ti->ti_flags |= TI_NWA_VALID;
} else {
err_msg(gettext(
"Cannot get writable address for the media.\n"));
exit(1);
}
}
if (ti->ti_nwa != ti->ti_start_address) {
err_msg(gettext(
"Media state is not suitable for this write mode.\n"));
exit(1);
}
if (mode == TRACK_MODE_DATA) {
if (!(ti->ti_track_mode & 4)) {
/* Write track depends upon this bit */
ti->ti_track_mode |= TRACK_MODE_DATA;
}
}
size = 0;
h->bstr_size(h, &size);
h->bstr_rewind(h);
te = (struct trackio_error *)my_zalloc(sizeof (*te));
print_n_flush(gettext("Writing track %d..."), (int)ti->ti_track_no);
init_progress();
if (!write_track(target, ti, h, progress, size, te)) {
if (te->err_type == TRACKIO_ERR_USER_ABORT) {
(void) str_print(gettext("Aborted.\n"), progress_pos);
} else {
if (device_type != DVD_PLUS_W) {
/* l10n_NOTE : 'failed' as in Writing Track...failed */
(void) str_print(gettext("failed.\n"),
progress_pos);
}
}
}
/* l10n_NOTE : 'done' as in "Writing track 1...done" */
(void) str_print(gettext("done.\n"), progress_pos);
free(ti);
free(te);
}
void
list(void)
{
if (scan_for_cd_device(SCAN_LISTDEVS, NULL) == 0) {
if (vol_running) {
err_msg(gettext(
"No CD writers found or no media in the drive.\n"));
} else {
if (cur_uid != 0) {
err_msg(gettext(
"Volume manager is not running.\n"));
err_msg(gettext(
"Please start volume manager or run cdrw as root to access all devices.\n"));
} else {
err_msg(gettext("No CD writers found.\n"));
}
}
}
exit(0);
}
void
get_media_type(int fd)
{
uchar_t *cap = (uchar_t *)my_zalloc(MMC_FTR_HDR_LEN);
if (get_configuration(fd, MMC_FTR_PRFL_LIST,
MMC_FTR_HDR_LEN, cap)) {
if (debug)
(void) print_profile_list(fd);
switch (read_scsi16(&cap[6])) {
case 0x8: /* CD-ROM */
if (debug)
(void) printf("CD-ROM found\n");
/*
* To avoid regression issues, treat as
* A cdrw, we will check the writable
* mode page to see if the media is
* actually writable.
*/
device_type = CD_RW;
break;
case 0x9: /* CD-R */
if (debug)
(void) printf("CD-R found\n");
device_type = CD_RW;
break;
case 0x10: /* DVD-ROM */
/*
* Have seen drives return DVD+RW media
* DVD-ROM, so try treating it as a DVD+RW
* profile. checking for writable media
* is done through mode page 5.
*/
if (debug)
(void) printf("DVD-ROM found\n");
device_type = DVD_PLUS_W;
break;
case 0xA: /* CD-RW */
if (debug)
(void) printf("CD-RW found\n");
device_type = CD_RW;
break;
case 0x11: /* DVD-R */
if (debug)
(void) printf("DVD-R found\n");
device_type = DVD_MINUS;
break;
case 0x12: /* DVD-RAM */
if (debug)
(void) printf("DVD-RAM found\n");
/* treat as CD-RW, may be a legacy drive */
device_type = CD_RW;
break;
case 0x13: /* DVD-RW restricted overwrite */
case 0x14: /* DVD-RW sequencial */
if (debug)
(void) printf("DVD-RW found\n");
device_type = DVD_MINUS;
break;
case 0x1A: /* DVD+RW */
if (debug)
(void) printf("DVD+RW found\n");
device_type = DVD_PLUS_W;
break;
case 0x1B: /* DVD+R */
if (debug)
(void) printf("DVD+R found\n");
device_type = DVD_PLUS;
break;
default:
if (debug)
(void) printf(
"unknown drive found\n type = 0x%x",
cap[7]);
/*
* Treat as CD_RW to avoid regression, may
* be a legacy drive.
*/
device_type = CD_RW;
}
}
free(cap);
}
/* Translate a transfer rate (eg, KB/s) into a Speed (eg, "2X") */
uint_t
cdrw_bandwidth_to_x(uint_t rate)
{
switch (device_type) {
case DVD_PLUS_W:
case DVD_MINUS:
case DVD_PLUS:
return (DVD_RATE_TO_X(rate));
default:
case CD_RW:
return (CD_RATE_TO_X(rate));
}
}
/* Translate a Speed (eg, "2X") into a transfer rate (eg, KB/s) */
uint_t
cdrw_x_to_bandwidth(uint_t x)
{
switch (device_type) {
case DVD_PLUS_W:
case DVD_MINUS:
case DVD_PLUS:
return (DVD_X_TO_RATE(x));
default:
case CD_RW:
return (CD_X_TO_RATE(x));
}
}
|