4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
30 #include <sys/types.h>
39 #include "misc_scsi.h"
44 #include "transport.h"
51 int read_audio_track(cd_device
*dev
, struct track_info
*ti
, bstreamhandle h
);
53 #define READ_BURST 24 /* < 64K in all cases */
56 * This reads the data off of a cd while updating the progress indicator.
57 * We want to do this in smaller chunks since some CD drives have
58 * problems with larger reads.
61 read_data_track(cd_device
*dev
, struct track_info
*ti
, bstreamhandle h
)
64 uint32_t blks_read
, cblk
, read_chunk
, read_size
;
73 * the last link_blks_count blocks may not exist or be completely
74 * filled. We need to record the amount to avoid bailing out if
75 * they cannot be read.
78 if (dev
->d_blksize
== 512) {
86 buf
= (uchar_t
*)my_zalloc(READ_BURST
* blksize
);
88 print_n_flush(gettext("Reading track %d..."), ti
->ti_track_no
);
91 print_n_flush("Track size is %u...", ti
->ti_track_size
);
94 cblk
= ti
->ti_start_address
;
96 while (blks_read
< ti
->ti_track_size
) {
97 /* Last few are special */
98 read_chunk
= ti
->ti_track_size
- blks_read
- link_blks_count
;
99 read_chunk
= (read_chunk
> READ_BURST
) ? READ_BURST
:
101 if (read_chunk
== 0) {
102 /* Time for last link blocks */
103 read_chunk
= link_blks_count
;
105 read_size
= read_chunk
* blksize
;
106 if (read10(dev
->d_fd
, cblk
, read_chunk
, buf
, read_size
)) {
107 if (h
->bstr_write(h
, buf
, read_size
) != read_size
) {
108 goto read_data_track_failed
;
112 (ti
->ti_track_size
- link_blks_count
)) {
113 goto read_data_track_failed
;
115 /* Read can fail for last link sectors */
119 blks_read
+= read_chunk
;
121 (void) progress((ti
->ti_track_size
), blks_read
);
123 /* l10n_NOTE : 'done' as in "Reading track 1...done" */
124 (void) str_print(gettext("done.\n"), progress_pos
);
126 read_data_track_failed
:
135 ensure_media_space(uint32_t total_nblks
, uchar_t end_tno
)
137 uint32_t nblks_avail
;
139 uint_t leadin_size
= 0;
141 get_media_type(target
->d_fd
);
143 if (device_type
== CD_RW
) {
144 nblks_avail
= get_last_possible_lba(target
);
146 if (nblks_avail
== 0) {
148 /* most newer drives use READ FORMAT CAPACITY */
149 nblks_avail
= read_format_capacity(target
->d_fd
,
152 /* if both methods fail, fall back on defaults */
153 if (nblks_avail
== 0) {
154 err_msg(gettext("Unable to determine media "
155 "capacity. Defaulting to 650 MB (74 minute)"
157 nblks_avail
= MAX_CD_BLKS
;
159 leadin_size
= end_tno
*300;
163 * For DVD drives use read_format_capacity as default
164 * retrieve the media size, it can be 3.6, 3.9, 4.2,
167 nblks_avail
= read_format_capacity(target
->d_fd
, &bsize
);
169 /* sanity check. if not reasonable default to 4.7 GB */
170 if (nblks_avail
< MAX_CD_BLKS
) {
171 nblks_avail
= MAX_DVD_BLKS
;
175 if ((total_nblks
+ leadin_size
) > nblks_avail
) {
176 err_msg(gettext("Not enough space on the media.\n"));
178 (void) printf("Need %u only found %u \n",
179 (total_nblks
+ leadin_size
),
180 (uint32_t)nblks_avail
);
188 * This copies both audio and data CDs. It first reads the TOC of the source CD
189 * and creates a temp file with the CD image. After this is completed it creates
190 * the target CD using TAO mode.
197 uchar_t
*toc
, end_tno
;
199 int audio_cd
, data_cd
;
200 uint32_t total_nblks
;
202 struct t_data
*tlist
;
204 print_n_flush(gettext("Analyzing source CD..."));
205 (void) check_device(target
,
206 CHECK_DEVICE_NOT_WRITABLE
|EXIT_IF_CHECK_FAILED
);
208 /* if source drive is specified on the command line */
211 p
= my_zalloc(PATH_MAX
);
212 if (lookup_device(copy_src
, p
) == 0) {
213 err_msg(gettext("Cannot find device %s"), copy_src
);
214 err_msg(gettext(" or no media in the drive\n"));
217 src
= get_device(copy_src
, p
);
219 err_msg(gettext("Unable to open %s\n"), copy_src
);
224 /* source is same as target drive */
228 (void) check_device(src
, CHECK_TYPE_NOT_CDROM
| CHECK_NO_MEDIA
|
229 CHECK_DEVICE_NOT_READY
| EXIT_IF_CHECK_FAILED
);
231 /* What type of media are we working with? */
232 get_media_type(src
->d_fd
);
234 toc
= (uchar_t
*)my_zalloc(4);
235 if (!read_toc(src
->d_fd
, 0, 0, 4, toc
)) {
236 err_msg(gettext("Cannot read table of contents\n"));
241 tlist
= (struct t_data
*)my_zalloc(end_tno
* sizeof (struct t_data
));
243 audio_cd
= data_cd
= 0;
246 /* build track information so we can copy it over */
247 for (i
= 1; i
<= end_tno
; i
++) {
248 struct track_info
*ti
;
250 ti
= &tlist
[i
- 1].ti
;
251 if (!build_track_info(src
, i
, ti
)) {
253 "Cannot get information for track %d\n"), i
);
256 total_nblks
+= ti
->ti_track_size
;
257 if (ti
->ti_track_mode
& 4)
262 /* Now some sanity checks on the track information */
263 if ((ti
->ti_flags
& TI_SESSION_NO_VALID
) &&
264 (ti
->ti_session_no
!= 1)) {
266 gettext("Copying multisession CD is not supported\n"));
269 if ((ti
->ti_flags
& TI_BLANK_TRACK
) ||
270 (ti
->ti_flags
& TI_DAMAGED_TRACK
) ||
271 (data_cd
&& audio_cd
) || (ti
->ti_data_mode
== 2)) {
273 err_msg(gettext("CD format is not supported\n"));
276 if ((ti
->ti_flags
& TI_NWA_VALID
) &&
277 (ti
->ti_nwa
!= 0xffffffff)) {
278 err_msg(gettext("Cannot copy incomplete discs\n"));
282 /* l10n_NOTE : 'done' as in "Analyzing source CD...done" */
283 (void) printf(gettext("done.\n"));
292 /* In case of audio CDs, build_track_info() returns 2352 sized nblks */
293 if (src
->d_blksize
== 512 && data_cd
) {
296 (void) printf(gettext("\nCopying %d %s track%s : %ld kbytes\n\n"),
297 end_tno
, (audio_cd
== 1) ? gettext("audio") : gettext("data"),
298 (end_tno
> 1) ? "s" : "", (long)((total_nblks
*blksize
)/1024));
300 if ((ret
= check_avail_temp_space(total_nblks
*blksize
)) != 0) {
301 err_msg(gettext("Cannot use temporary directory : %s\n"),
303 err_msg(gettext("Use -m to specify alternate"
304 " temporary directory\n"));
309 * If we can check available space on the target media at this
310 * Stage, then it is always better. We cannot check DVD+R(W)
311 * as this media may be formatted and not blank.
313 if (target
&& (src
!= target
) && (device_type
!= DVD_PLUS
) &&
314 (device_type
!= DVD_PLUS_W
) && (!check_device(target
,
315 CHECK_NO_MEDIA
|CHECK_MEDIA_IS_NOT_BLANK
))) {
316 ensure_media_space(total_nblks
, end_tno
);
320 for (i
= 1; i
<= end_tno
; i
++) {
321 tlist
[i
- 1].h
= open_temp_file_stream();
322 if (tlist
[i
- 1].h
== NULL
) {
323 err_msg(gettext("Cannot create temporary file : %s\n"),
329 ret
= read_audio_track(src
, &tlist
[i
- 1].ti
,
332 ret
= read_data_track(src
, &tlist
[i
- 1].ti
,
335 err_msg(gettext("Error reading track %d : %s\n"), i
,
338 (void) printf("%x %x %x %x\n", uscsi_status
,
339 SENSE_KEY(rqbuf
), ASC(rqbuf
), ASCQ(rqbuf
));
345 * We've finished copying the CD. If source and destination are the same
346 * or they where not specified then eject the disk and wait for a new
347 * disk to be inserted.
349 * Since, DVD+RWs are not blanked just reformated, allow the insertion
350 * of a DVD+RW to be the only condition necessary to complete copying.
354 if (target
!= NULL
) {
355 (void) eject_media(target
);
360 gettext("Insert a blank media in the drive and press Enter."));
361 (void) fflush(stdin
);
368 (void) setup_target(SCAN_WRITERS
);
370 get_media_type(target
->d_fd
);
371 } while ((target
== NULL
) ||
372 ((device_type
== DVD_PLUS_W
)? check_device(target
, CHECK_NO_MEDIA
):
373 check_device(target
, CHECK_NO_MEDIA
|CHECK_MEDIA_IS_NOT_BLANK
)));
376 (void) setreuid(ruid
, 0);
378 if ((device_type
!= DVD_PLUS
) && (device_type
!= DVD_PLUS_W
)) {
379 ensure_media_space(total_nblks
, end_tno
);
380 write_init(audio_cd
? TRACK_MODE_AUDIO
: TRACK_MODE_DATA
);
384 * Simulation writing can't happen on DVD+RW's
385 * or DVD+R's. According to the MMC spec this
386 * operation is not supported. So we should
387 * bail out if the user tries to do a simulation
390 if (simulation
&& (device_type
== DVD_PLUS_W
||
391 device_type
== DVD_PLUS
)) {
392 err_msg(gettext("Media does not support simulated writing.\n"));
396 if (device_type
== DVD_PLUS_W
) {
398 * DVD+RW requires that we format the media before
401 (void) print_n_flush(gettext("Formatting media..."));
402 if (!format_media(target
->d_fd
)) {
403 (void) printf(gettext(
404 "Could not format media\n"));
410 /* poll until format is done */
411 di
= (uchar_t
*)my_zalloc(DISC_INFO_BLOCK_SIZE
);
413 for (counter
= 0; counter
< 200; counter
++) {
414 ret
= read_disc_info(target
->d_fd
, di
);
415 if ((SENSE_KEY(rqbuf
) == 2) &&
417 (void) print_n_flush(".");
427 for (i
= 0; i
< end_tno
; i
++) {
429 * DVD's dont contain tracks and need to be written in DAO
432 if (device_type
!= CD_RW
) {
435 "Media state is not suitable for this"
438 write_mode
= DAO_MODE
;
441 * DVD-R(W) and DVD+R needs to have space reserved
444 if ((device_type
== DVD_MINUS
) ||
445 (device_type
== DVD_PLUS
)) {
446 if (!set_reservation(target
->d_fd
,
448 (void) printf(gettext(
449 "Setting reservation failed\n"));
455 write_next_track(audio_cd
? TRACK_MODE_AUDIO
: TRACK_MODE_DATA
,
459 * Running in simulation mode and writing several tracks is
460 * useless so bail after the first track is done.
463 if (simulation
&& (end_tno
!= 1)) {
464 (void) printf(gettext(
465 "Simulation mode : skipping remaining tracks\n"));
471 /* close the temp file handles */
472 for (i
= 0; i
< end_tno
; i
++)
473 (tlist
[i
].h
)->bstr_close(tlist
[i
].h
);