xref: /illumos-gate/usr/src/cmd/cdrw/copycd.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51b01eaa0Szk  * Common Development and Distribution License (the "License").
61b01eaa0Szk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4df0f9a3Sec  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <stdio.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <errno.h>
307c478bd9Sstevel@tonic-gate #include <limits.h>
317c478bd9Sstevel@tonic-gate #include <unistd.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
33416baccdSec #include <string.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "main.h"
367c478bd9Sstevel@tonic-gate #include "util.h"
377c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
387c478bd9Sstevel@tonic-gate #include "mmc.h"
397c478bd9Sstevel@tonic-gate #include "bstream.h"
407c478bd9Sstevel@tonic-gate #include "device.h"
417c478bd9Sstevel@tonic-gate #include "msgs.h"
427c478bd9Sstevel@tonic-gate #include "transport.h"
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate struct t_data {
457c478bd9Sstevel@tonic-gate 	bstreamhandle h;
467c478bd9Sstevel@tonic-gate 	struct track_info ti;
477c478bd9Sstevel@tonic-gate };
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate int read_audio_track(cd_device *dev, struct track_info *ti, bstreamhandle h);
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	READ_BURST	24	/* < 64K in all cases */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate /*
547c478bd9Sstevel@tonic-gate  * This reads the data off of a cd while updating the progress indicator.
557c478bd9Sstevel@tonic-gate  * We want to do this in smaller chunks since some CD drives have
567c478bd9Sstevel@tonic-gate  * problems with larger reads.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate static int
read_data_track(cd_device * dev,struct track_info * ti,bstreamhandle h)597c478bd9Sstevel@tonic-gate read_data_track(cd_device *dev, struct track_info *ti, bstreamhandle h)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate 	int blksize;
627c478bd9Sstevel@tonic-gate 	uint32_t blks_read, cblk, read_chunk, read_size;
637c478bd9Sstevel@tonic-gate 	uchar_t *buf;
647c478bd9Sstevel@tonic-gate 	int ret, sav;
657c478bd9Sstevel@tonic-gate 	int link_blks_count;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	buf = NULL;
687c478bd9Sstevel@tonic-gate 	ret = 0;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	/*
717c478bd9Sstevel@tonic-gate 	 * the last link_blks_count blocks may not exist or be completely
727c478bd9Sstevel@tonic-gate 	 * filled. We need to record the amount to avoid bailing out if
737c478bd9Sstevel@tonic-gate 	 * they cannot be read.
747c478bd9Sstevel@tonic-gate 	 */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	if (dev->d_blksize == 512) {
777c478bd9Sstevel@tonic-gate 		blksize = 512;
787c478bd9Sstevel@tonic-gate 		link_blks_count = 8;
797c478bd9Sstevel@tonic-gate 	} else {
807c478bd9Sstevel@tonic-gate 		blksize = 2048;
817c478bd9Sstevel@tonic-gate 		link_blks_count = 2;
827c478bd9Sstevel@tonic-gate 	}
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(READ_BURST * blksize);
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Reading track %d..."), ti->ti_track_no);
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	if (verbose)
897c478bd9Sstevel@tonic-gate 		print_n_flush("Track size is %u...", ti->ti_track_size);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	init_progress();
927c478bd9Sstevel@tonic-gate 	cblk = ti->ti_start_address;
937c478bd9Sstevel@tonic-gate 	blks_read = 0;
947c478bd9Sstevel@tonic-gate 	while (blks_read < ti->ti_track_size) {
957c478bd9Sstevel@tonic-gate 		/* Last few are special */
967c478bd9Sstevel@tonic-gate 		read_chunk = ti->ti_track_size - blks_read - link_blks_count;
977c478bd9Sstevel@tonic-gate 		read_chunk = (read_chunk > READ_BURST) ? READ_BURST :
987c478bd9Sstevel@tonic-gate 		    read_chunk;
997c478bd9Sstevel@tonic-gate 		if (read_chunk == 0) {
1007c478bd9Sstevel@tonic-gate 			/* Time for last link blocks */
1017c478bd9Sstevel@tonic-gate 			read_chunk = link_blks_count;
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 		read_size = read_chunk * blksize;
1047c478bd9Sstevel@tonic-gate 		if (read10(dev->d_fd, cblk, read_chunk, buf, read_size)) {
1057c478bd9Sstevel@tonic-gate 			if (h->bstr_write(h, buf, read_size) != read_size) {
1067c478bd9Sstevel@tonic-gate 				goto read_data_track_failed;
1077c478bd9Sstevel@tonic-gate 			}
1087c478bd9Sstevel@tonic-gate 		} else {
1097c478bd9Sstevel@tonic-gate 			if (blks_read !=
1107c478bd9Sstevel@tonic-gate 			    (ti->ti_track_size - link_blks_count)) {
1117c478bd9Sstevel@tonic-gate 				goto read_data_track_failed;
1127c478bd9Sstevel@tonic-gate 			} else {
1137c478bd9Sstevel@tonic-gate 			/* Read can fail for last link sectors */
1147c478bd9Sstevel@tonic-gate 				errno = 0;
1157c478bd9Sstevel@tonic-gate 			}
1167c478bd9Sstevel@tonic-gate 		}
1177c478bd9Sstevel@tonic-gate 		blks_read += read_chunk;
1187c478bd9Sstevel@tonic-gate 		cblk += read_chunk;
1190c83a891Snakanon 		(void) progress((ti->ti_track_size), blks_read);
1207c478bd9Sstevel@tonic-gate 	}
1217c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Reading track 1...done"  */
1227c478bd9Sstevel@tonic-gate 	(void) str_print(gettext("done.\n"), progress_pos);
1237c478bd9Sstevel@tonic-gate 	ret = 1;
1247c478bd9Sstevel@tonic-gate read_data_track_failed:
1257c478bd9Sstevel@tonic-gate 	sav = errno;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	free(buf);
1287c478bd9Sstevel@tonic-gate 	errno = sav;
1297c478bd9Sstevel@tonic-gate 	return (ret);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void
ensure_media_space(uint32_t total_nblks,uchar_t end_tno)1337c478bd9Sstevel@tonic-gate ensure_media_space(uint32_t total_nblks, uchar_t end_tno)
1347c478bd9Sstevel@tonic-gate {
1354d218355Szk 	uint32_t nblks_avail;
1367c478bd9Sstevel@tonic-gate 	uint_t bsize;
1374077f1edSminht 	uint_t leadin_size = 0;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	get_media_type(target->d_fd);
1407c478bd9Sstevel@tonic-gate 
141*4df0f9a3Sec 	if (device_type == CD_RW) {
1427c478bd9Sstevel@tonic-gate 		nblks_avail = get_last_possible_lba(target);
1437c478bd9Sstevel@tonic-gate 
1444d218355Szk 		if (nblks_avail == 0) {
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 			/* most newer drives use READ FORMAT CAPACITY */
1477c478bd9Sstevel@tonic-gate 			nblks_avail = read_format_capacity(target->d_fd,
1487c478bd9Sstevel@tonic-gate 			    &bsize);
1497c478bd9Sstevel@tonic-gate 
150*4df0f9a3Sec 			/* if both methods fail, fall back on defaults */
1514d218355Szk 			if (nblks_avail == 0) {
152*4df0f9a3Sec 				err_msg(gettext("Unable to determine media "
153*4df0f9a3Sec 				    "capacity. Defaulting to 650 MB (74 minute)"
154*4df0f9a3Sec 				    " disc.\n"));
155*4df0f9a3Sec 				nblks_avail = MAX_CD_BLKS;
1567c478bd9Sstevel@tonic-gate 			}
1574077f1edSminht 		leadin_size = end_tno*300;
1587c478bd9Sstevel@tonic-gate 		}
1597c478bd9Sstevel@tonic-gate 	} else {
160*4df0f9a3Sec 		/*
161*4df0f9a3Sec 		 * For DVD drives use read_format_capacity as default
162*4df0f9a3Sec 		 * retrieve the media size, it can be 3.6, 3.9, 4.2,
163*4df0f9a3Sec 		 * 4.7, or 9.2 GB
164*4df0f9a3Sec 		 */
165*4df0f9a3Sec 		nblks_avail = read_format_capacity(target->d_fd, &bsize);
166*4df0f9a3Sec 
167*4df0f9a3Sec 		/* sanity check. if not reasonable default to 4.7 GB */
168*4df0f9a3Sec 		if (nblks_avail < MAX_CD_BLKS) {
169*4df0f9a3Sec 			nblks_avail = MAX_DVD_BLKS;
1707c478bd9Sstevel@tonic-gate 		}
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1734077f1edSminht 	if ((total_nblks + leadin_size) > nblks_avail) {
1747c478bd9Sstevel@tonic-gate 		err_msg(gettext("Not enough space on the media.\n"));
1757c478bd9Sstevel@tonic-gate 		if (debug) {
1767c478bd9Sstevel@tonic-gate 			(void) printf("Need %u  only found %u \n",
1774077f1edSminht 			    (total_nblks + leadin_size),
1787c478bd9Sstevel@tonic-gate 			    (uint32_t)nblks_avail);
1797c478bd9Sstevel@tonic-gate 		}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 		exit(1);
1827c478bd9Sstevel@tonic-gate 	}
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * This copies both audio and data CDs. It first reads the TOC of the source CD
1877c478bd9Sstevel@tonic-gate  * and creates a temp file with the CD image. After this is completed it creates
1887c478bd9Sstevel@tonic-gate  * the target CD using TAO mode.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate void
copy_cd(void)1917c478bd9Sstevel@tonic-gate copy_cd(void)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	cd_device *src;
1947c478bd9Sstevel@tonic-gate 	char *p;
1957c478bd9Sstevel@tonic-gate 	uchar_t *toc, end_tno;
1967c478bd9Sstevel@tonic-gate 	int blksize, i;
1977c478bd9Sstevel@tonic-gate 	int audio_cd, data_cd;
1987c478bd9Sstevel@tonic-gate 	uint32_t total_nblks;
1997c478bd9Sstevel@tonic-gate 	int ret;
2007c478bd9Sstevel@tonic-gate 	struct t_data *tlist;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	print_n_flush(gettext("Analyzing source CD..."));
2037c478bd9Sstevel@tonic-gate 	(void) check_device(target,
2047c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE|EXIT_IF_CHECK_FAILED);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/* if source drive is specified on the command line */
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	if (copy_src) {
2097c478bd9Sstevel@tonic-gate 		p = my_zalloc(PATH_MAX);
2107c478bd9Sstevel@tonic-gate 		if (lookup_device(copy_src, p) == 0) {
2117c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot find device %s"), copy_src);
2127c478bd9Sstevel@tonic-gate 			err_msg(gettext(" or no media in the drive\n"));
2137c478bd9Sstevel@tonic-gate 			exit(1);
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		src = get_device(copy_src, p);
2167c478bd9Sstevel@tonic-gate 		if (src == NULL) {
2177c478bd9Sstevel@tonic-gate 			err_msg(gettext("Unable to open %s\n"), copy_src);
2187c478bd9Sstevel@tonic-gate 			exit(1);
2197c478bd9Sstevel@tonic-gate 		}
2207c478bd9Sstevel@tonic-gate 		free(p);
2217c478bd9Sstevel@tonic-gate 	} else {
2227c478bd9Sstevel@tonic-gate 		/* source is same as target drive */
2237c478bd9Sstevel@tonic-gate 		src = target;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	(void) check_device(src, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
2277c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
2287c478bd9Sstevel@tonic-gate 
22954cafc85Szk 	/* What type of media are we working with? */
23054cafc85Szk 	get_media_type(src->d_fd);
23154cafc85Szk 
2327c478bd9Sstevel@tonic-gate 	toc = (uchar_t *)my_zalloc(4);
2337c478bd9Sstevel@tonic-gate 	if (!read_toc(src->d_fd, 0, 0, 4, toc)) {
2347c478bd9Sstevel@tonic-gate 		err_msg(gettext("Cannot read table of contents\n"));
2357c478bd9Sstevel@tonic-gate 		exit(1);
2367c478bd9Sstevel@tonic-gate 	}
2377c478bd9Sstevel@tonic-gate 	end_tno = toc[3];
2387c478bd9Sstevel@tonic-gate 	free(toc);
2397c478bd9Sstevel@tonic-gate 	tlist = (struct t_data *)my_zalloc(end_tno * sizeof (struct t_data));
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	audio_cd = data_cd = 0;
2427c478bd9Sstevel@tonic-gate 	total_nblks = 0;
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	/* build track information so we can copy it over */
2457c478bd9Sstevel@tonic-gate 	for (i = 1; i <= end_tno; i++) {
2467c478bd9Sstevel@tonic-gate 		struct track_info *ti;
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate 		ti = &tlist[i - 1].ti;
2497c478bd9Sstevel@tonic-gate 		if (!build_track_info(src, i, ti)) {
2507c478bd9Sstevel@tonic-gate 			err_msg(gettext(
2517c478bd9Sstevel@tonic-gate 			    "Cannot get information for track %d\n"), i);
2527c478bd9Sstevel@tonic-gate 			exit(1);
2537c478bd9Sstevel@tonic-gate 		}
2547c478bd9Sstevel@tonic-gate 		total_nblks += ti->ti_track_size;
2557c478bd9Sstevel@tonic-gate 		if (ti->ti_track_mode & 4)
2567c478bd9Sstevel@tonic-gate 			data_cd = 1;
2577c478bd9Sstevel@tonic-gate 		else
2587c478bd9Sstevel@tonic-gate 			audio_cd = 1;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 		/* Now some sanity checks on the track information */
2617c478bd9Sstevel@tonic-gate 		if ((ti->ti_flags & TI_SESSION_NO_VALID) &&
2627c478bd9Sstevel@tonic-gate 		    (ti->ti_session_no != 1)) {
2637c478bd9Sstevel@tonic-gate 			err_msg(
2647c478bd9Sstevel@tonic-gate 			gettext("Copying multisession CD is not supported\n"));
2657c478bd9Sstevel@tonic-gate 			exit(1);
2667c478bd9Sstevel@tonic-gate 		}
2674077f1edSminht 		if ((ti->ti_flags & TI_BLANK_TRACK) ||
2687c478bd9Sstevel@tonic-gate 		    (ti->ti_flags & TI_DAMAGED_TRACK) ||
2697c478bd9Sstevel@tonic-gate 		    (data_cd && audio_cd) || (ti->ti_data_mode == 2)) {
2707c478bd9Sstevel@tonic-gate 
2714d218355Szk 			err_msg(gettext("CD format is not supported\n"));
2727c478bd9Sstevel@tonic-gate 			exit(1);
2737c478bd9Sstevel@tonic-gate 		}
2747c478bd9Sstevel@tonic-gate 		if ((ti->ti_flags & TI_NWA_VALID) &&
2757c478bd9Sstevel@tonic-gate 		    (ti->ti_nwa != 0xffffffff)) {
2767c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot copy incomplete discs\n"));
2777c478bd9Sstevel@tonic-gate 			exit(1);
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	/* l10n_NOTE : 'done' as in "Analyzing source CD...done"  */
2817c478bd9Sstevel@tonic-gate 	(void) printf(gettext("done.\n"));
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (data_cd) {
2847c478bd9Sstevel@tonic-gate 		blksize = 2048;
2857c478bd9Sstevel@tonic-gate 	} else {
2867c478bd9Sstevel@tonic-gate 		/* audio cd */
2877c478bd9Sstevel@tonic-gate 		blksize = 2352;
2887c478bd9Sstevel@tonic-gate 	}
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	/* In case of audio CDs, build_track_info() returns 2352 sized nblks */
2917c478bd9Sstevel@tonic-gate 	if (src->d_blksize == 512 && data_cd) {
2927c478bd9Sstevel@tonic-gate 		total_nblks /= 4;
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	(void) printf(gettext("\nCopying %d %s track%s : %ld kbytes\n\n"),
2957c478bd9Sstevel@tonic-gate 	    end_tno, (audio_cd == 1) ? gettext("audio") : gettext("data"),
2967c478bd9Sstevel@tonic-gate 	    (end_tno > 1) ? "s" : "", (long)((total_nblks*blksize)/1024));
2977c478bd9Sstevel@tonic-gate 
298416baccdSec 	if ((ret = check_avail_temp_space(total_nblks*blksize)) != 0) {
299416baccdSec 		err_msg(gettext("Cannot use temporary directory : %s\n"),
300416baccdSec 		    strerror(ret));
301416baccdSec 		err_msg(gettext("Use -m to specify alternate"
302416baccdSec 		    " temporary directory\n"));
3037c478bd9Sstevel@tonic-gate 		exit(1);
3047c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate 	/*
3077c478bd9Sstevel@tonic-gate 	 * If we can check available space on the target media at this
3087c478bd9Sstevel@tonic-gate 	 * Stage, then it is always better. We cannot check DVD+R(W)
3097c478bd9Sstevel@tonic-gate 	 * as this media may be formatted and not blank.
3107c478bd9Sstevel@tonic-gate 	 */
3117c478bd9Sstevel@tonic-gate 	if (target && (src != target) && (device_type != DVD_PLUS) &&
3127c478bd9Sstevel@tonic-gate 	    (device_type != DVD_PLUS_W) && (!check_device(target,
3137c478bd9Sstevel@tonic-gate 	    CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK))) {
3147c478bd9Sstevel@tonic-gate 		ensure_media_space(total_nblks, end_tno);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/* for each track */
3187c478bd9Sstevel@tonic-gate 	for (i = 1; i <= end_tno; i++) {
3197c478bd9Sstevel@tonic-gate 		tlist[i - 1].h = open_temp_file_stream();
3207c478bd9Sstevel@tonic-gate 		if (tlist[i - 1].h == NULL) {
3217c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot create temporary file : %s\n"),
3227c478bd9Sstevel@tonic-gate 			    get_err_str());
3237c478bd9Sstevel@tonic-gate 			exit(1);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		if (audio_cd)
3277c478bd9Sstevel@tonic-gate 			ret = read_audio_track(src, &tlist[i - 1].ti,
3287c478bd9Sstevel@tonic-gate 			    tlist[i - 1].h);
3297c478bd9Sstevel@tonic-gate 		else
3307c478bd9Sstevel@tonic-gate 			ret = read_data_track(src, &tlist[i - 1].ti,
3317c478bd9Sstevel@tonic-gate 			    tlist[i - 1].h);
3327c478bd9Sstevel@tonic-gate 		if (ret == 0) {
3337c478bd9Sstevel@tonic-gate 			err_msg(gettext("Error reading track %d : %s\n"), i,
3347c478bd9Sstevel@tonic-gate 			    get_err_str());
3357c478bd9Sstevel@tonic-gate 			if (debug)
3367c478bd9Sstevel@tonic-gate 				(void) printf("%x %x %x %x\n", uscsi_status,
3377c478bd9Sstevel@tonic-gate 				    SENSE_KEY(rqbuf), ASC(rqbuf), ASCQ(rqbuf));
3387c478bd9Sstevel@tonic-gate 			exit(1);
3397c478bd9Sstevel@tonic-gate 		}
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	/*
3437c478bd9Sstevel@tonic-gate 	 * We've finished copying the CD. If source and destination are the same
3447c478bd9Sstevel@tonic-gate 	 * or they where not specified then eject the disk and wait for a new
3457c478bd9Sstevel@tonic-gate 	 * disk to be inserted.
34654cafc85Szk 	 *
34754cafc85Szk 	 * Since, DVD+RWs are not blanked just reformated, allow the insertion
34854cafc85Szk 	 * of a DVD+RW to be the only condition necessary to complete copying.
3497c478bd9Sstevel@tonic-gate 	 */
3507c478bd9Sstevel@tonic-gate 
35154cafc85Szk 	do {
3527c478bd9Sstevel@tonic-gate 		if (target != NULL) {
3537c478bd9Sstevel@tonic-gate 			(void) eject_media(target);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		(void) printf("\n");
3577c478bd9Sstevel@tonic-gate 		print_n_flush(
3587c478bd9Sstevel@tonic-gate 		gettext("Insert a blank media in the drive and press Enter."));
3597c478bd9Sstevel@tonic-gate 		(void) fflush(stdin);
3607c478bd9Sstevel@tonic-gate 		if (target) {
3617c478bd9Sstevel@tonic-gate 			fini_device(target);
3627c478bd9Sstevel@tonic-gate 			target = NULL;
3637c478bd9Sstevel@tonic-gate 		}
3647c478bd9Sstevel@tonic-gate 		(void) getchar();
3657c478bd9Sstevel@tonic-gate 		(void) sleep(4);
3667c478bd9Sstevel@tonic-gate 		(void) setup_target(SCAN_WRITERS);
36754cafc85Szk 		if (target)
36854cafc85Szk 			get_media_type(target->d_fd);
36954cafc85Szk 	} while ((target == NULL) ||
37054cafc85Szk 	    ((device_type == DVD_PLUS_W)? check_device(target, CHECK_NO_MEDIA):
3714d218355Szk 	    check_device(target, CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK)));
37254cafc85Szk 
3737c478bd9Sstevel@tonic-gate 	(void) printf("\n");
3747c478bd9Sstevel@tonic-gate 	(void) setreuid(ruid, 0);
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	if ((device_type != DVD_PLUS) && (device_type != DVD_PLUS_W)) {
3777c478bd9Sstevel@tonic-gate 		ensure_media_space(total_nblks, end_tno);
3787c478bd9Sstevel@tonic-gate 		write_init(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA);
3797c478bd9Sstevel@tonic-gate 	}
3807c478bd9Sstevel@tonic-gate 
3811b01eaa0Szk 	/*
3821b01eaa0Szk 	 * Simulation writing can't happen on DVD+RW's
3831b01eaa0Szk 	 * or DVD+R's. According to the MMC spec this
3841b01eaa0Szk 	 * operation is not supported. So we should
3851b01eaa0Szk 	 * bail out if the user tries to do a simulation
3861b01eaa0Szk 	 * write.
3871b01eaa0Szk 	 */
3881b01eaa0Szk 	if (simulation && (device_type == DVD_PLUS_W ||
3891b01eaa0Szk 	    device_type == DVD_PLUS)) {
3901b01eaa0Szk 		err_msg(gettext("Media does not support simulated writing.\n"));
3911b01eaa0Szk 		exit(1);
3921b01eaa0Szk 	}
3931b01eaa0Szk 
3944077f1edSminht 	if (device_type == DVD_PLUS_W) {
3954077f1edSminht 		/*
3964077f1edSminht 		 * DVD+RW requires that we format the media before
3974077f1edSminht 		 * writing.
3984077f1edSminht 		 */
3994077f1edSminht 		(void) print_n_flush(gettext("Formatting media..."));
4004077f1edSminht 		if (!format_media(target->d_fd)) {
4014077f1edSminht 			(void) printf(gettext(
4024077f1edSminht 			    "Could not format media\n"));
4034077f1edSminht 			exit(1);
4044077f1edSminht 		} else {
4054077f1edSminht 			int counter;
4064077f1edSminht 			uchar_t *di;
4074077f1edSminht 
4084077f1edSminht 			/* poll until format is done */
4094077f1edSminht 			di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
4104077f1edSminht 			(void) sleep(10);
4114077f1edSminht 			for (counter = 0; counter < 200; counter++) {
4124077f1edSminht 				ret = read_disc_info(target->d_fd, di);
4134077f1edSminht 				if ((SENSE_KEY(rqbuf) == 2) &&
4144077f1edSminht 				    (ASC(rqbuf) == 4)) {
4154077f1edSminht 					(void) print_n_flush(".");
4164077f1edSminht 					(void) sleep(5);
4174077f1edSminht 				} else {
4184077f1edSminht 					break;
4194077f1edSminht 				}
4204077f1edSminht 			}
4214077f1edSminht 		}
4224077f1edSminht 	}
4234077f1edSminht 
4247c478bd9Sstevel@tonic-gate 	/* for each track */
4257c478bd9Sstevel@tonic-gate 	for (i = 0; i < end_tno; i++) {
4267c478bd9Sstevel@tonic-gate 		/*
4277c478bd9Sstevel@tonic-gate 		 * DVD's dont contain tracks and need to be written in DAO
4287c478bd9Sstevel@tonic-gate 		 * mode.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if (device_type != CD_RW) {
4317c478bd9Sstevel@tonic-gate 			if (end_tno > 1) {
4327c478bd9Sstevel@tonic-gate 				err_msg(gettext(
4337c478bd9Sstevel@tonic-gate 				    "Media state is not suitable for this"
4347c478bd9Sstevel@tonic-gate 				    " write mode.\n"));
4357c478bd9Sstevel@tonic-gate 			}
4367c478bd9Sstevel@tonic-gate 			write_mode = DAO_MODE;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 			/*
4397c478bd9Sstevel@tonic-gate 			 * DVD-R(W) and DVD+R needs to have space reserved
4407c478bd9Sstevel@tonic-gate 			 * prior to writing.
4417c478bd9Sstevel@tonic-gate 			 */
4427c478bd9Sstevel@tonic-gate 			if ((device_type == DVD_MINUS) ||
4437c478bd9Sstevel@tonic-gate 			    (device_type == DVD_PLUS)) {
4447c478bd9Sstevel@tonic-gate 				if (!set_reservation(target->d_fd,
4457c478bd9Sstevel@tonic-gate 				    total_nblks + 1)) {
4467c478bd9Sstevel@tonic-gate 					(void) printf(gettext(
4477c478bd9Sstevel@tonic-gate 					    "Setting reservation failed\n"));
4487c478bd9Sstevel@tonic-gate 					exit(1);
4497c478bd9Sstevel@tonic-gate 				}
4507c478bd9Sstevel@tonic-gate 			}
4517c478bd9Sstevel@tonic-gate 		}
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		write_next_track(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA,
4547c478bd9Sstevel@tonic-gate 		    tlist[i].h);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		/*
4577c478bd9Sstevel@tonic-gate 		 * Running in simulation mode and writing several tracks is
4587c478bd9Sstevel@tonic-gate 		 * useless so bail after the first track is done.
4597c478bd9Sstevel@tonic-gate 		 */
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if (simulation && (end_tno != 1)) {
4627c478bd9Sstevel@tonic-gate 			(void) printf(gettext(
4637c478bd9Sstevel@tonic-gate 			"Simulation mode : skipping remaining tracks\n"));
4647c478bd9Sstevel@tonic-gate 			break;
4657c478bd9Sstevel@tonic-gate 		}
4667c478bd9Sstevel@tonic-gate 	}
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	write_fini();
4697c478bd9Sstevel@tonic-gate 	/* close the temp file handles */
4707c478bd9Sstevel@tonic-gate 	for (i = 0; i < end_tno; i++)
4717c478bd9Sstevel@tonic-gate 		(tlist[i].h)->bstr_close(tlist[i].h);
4727c478bd9Sstevel@tonic-gate 	free(tlist);
4737c478bd9Sstevel@tonic-gate 	fini_device(target);
4747c478bd9Sstevel@tonic-gate 	exit(0);
4757c478bd9Sstevel@tonic-gate }
476