xref: /titanic_51/usr/src/cmd/cdrw/copycd.c (revision 613a2f6ba31e891e3d947a356daf5e563d43c1ce)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <limits.h>
33 #include <unistd.h>
34 #include <libintl.h>
35 #include <string.h>
36 
37 #include "main.h"
38 #include "util.h"
39 #include "misc_scsi.h"
40 #include "mmc.h"
41 #include "bstream.h"
42 #include "device.h"
43 #include "msgs.h"
44 #include "transport.h"
45 
46 struct t_data {
47 	bstreamhandle h;
48 	struct track_info ti;
49 };
50 
51 int read_audio_track(cd_device *dev, struct track_info *ti, bstreamhandle h);
52 
53 #define	READ_BURST	24	/* < 64K in all cases */
54 
55 /*
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.
59  */
60 static int
61 read_data_track(cd_device *dev, struct track_info *ti, bstreamhandle h)
62 {
63 	int blksize;
64 	uint32_t blks_read, cblk, read_chunk, read_size;
65 	uchar_t *buf;
66 	int ret, sav;
67 	int link_blks_count;
68 
69 	buf = NULL;
70 	ret = 0;
71 
72 	/*
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.
76 	 */
77 
78 	if (dev->d_blksize == 512) {
79 		blksize = 512;
80 		link_blks_count = 8;
81 	} else {
82 		blksize = 2048;
83 		link_blks_count = 2;
84 	}
85 
86 	buf = (uchar_t *)my_zalloc(READ_BURST * blksize);
87 
88 	print_n_flush(gettext("Reading track %d..."), ti->ti_track_no);
89 
90 	if (verbose)
91 		print_n_flush("Track size is %u...", ti->ti_track_size);
92 
93 	init_progress();
94 	cblk = ti->ti_start_address;
95 	blks_read = 0;
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 :
100 		    read_chunk;
101 		if (read_chunk == 0) {
102 			/* Time for last link blocks */
103 			read_chunk = link_blks_count;
104 		}
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;
109 			}
110 		} else {
111 			if (blks_read !=
112 			    (ti->ti_track_size - link_blks_count)) {
113 				goto read_data_track_failed;
114 			} else {
115 			/* Read can fail for last link sectors */
116 				errno = 0;
117 			}
118 		}
119 		blks_read += read_chunk;
120 		cblk += read_chunk;
121 		(void) progress((ti->ti_track_size), blks_read);
122 	}
123 	/* l10n_NOTE : 'done' as in "Reading track 1...done"  */
124 	(void) str_print(gettext("done.\n"), progress_pos);
125 	ret = 1;
126 read_data_track_failed:
127 	sav = errno;
128 
129 	free(buf);
130 	errno = sav;
131 	return (ret);
132 }
133 
134 static void
135 ensure_media_space(uint32_t total_nblks, uchar_t end_tno)
136 {
137 	uint32_t nblks_avail;
138 	uint_t bsize;
139 	uint_t leadin_size = 0;
140 
141 	get_media_type(target->d_fd);
142 
143 	if (device_type == CD_RW) {
144 		nblks_avail = get_last_possible_lba(target);
145 
146 		if (nblks_avail == 0) {
147 
148 			/* most newer drives use READ FORMAT CAPACITY */
149 			nblks_avail = read_format_capacity(target->d_fd,
150 			    &bsize);
151 
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)"
156 				    " disc.\n"));
157 				nblks_avail = MAX_CD_BLKS;
158 			}
159 		leadin_size = end_tno*300;
160 		}
161 	} else {
162 		/*
163 		 * For DVD drives use read_format_capacity as default
164 		 * retrieve the media size, it can be 3.6, 3.9, 4.2,
165 		 * 4.7, or 9.2 GB
166 		 */
167 		nblks_avail = read_format_capacity(target->d_fd, &bsize);
168 
169 		/* sanity check. if not reasonable default to 4.7 GB */
170 		if (nblks_avail < MAX_CD_BLKS) {
171 			nblks_avail = MAX_DVD_BLKS;
172 		}
173 	}
174 
175 	if ((total_nblks + leadin_size) > nblks_avail) {
176 		err_msg(gettext("Not enough space on the media.\n"));
177 		if (debug) {
178 			(void) printf("Need %u  only found %u \n",
179 			    (total_nblks + leadin_size),
180 			    (uint32_t)nblks_avail);
181 		}
182 
183 		exit(1);
184 	}
185 }
186 
187 /*
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.
191  */
192 void
193 copy_cd(void)
194 {
195 	cd_device *src;
196 	char *p;
197 	uchar_t *toc, end_tno;
198 	int blksize, i;
199 	int audio_cd, data_cd;
200 	uint32_t total_nblks;
201 	int ret;
202 	struct t_data *tlist;
203 
204 	print_n_flush(gettext("Analyzing source CD..."));
205 	(void) check_device(target,
206 	    CHECK_DEVICE_NOT_WRITABLE|EXIT_IF_CHECK_FAILED);
207 
208 	/* if source drive is specified on the command line */
209 
210 	if (copy_src) {
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"));
215 			exit(1);
216 		}
217 		src = get_device(copy_src, p);
218 		if (src == NULL) {
219 			err_msg(gettext("Unable to open %s\n"), copy_src);
220 			exit(1);
221 		}
222 		free(p);
223 	} else {
224 		/* source is same as target drive */
225 		src = target;
226 	}
227 
228 	(void) check_device(src, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
229 	    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
230 
231 	/* What type of media are we working with? */
232 	get_media_type(src->d_fd);
233 
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"));
237 		exit(1);
238 	}
239 	end_tno = toc[3];
240 	free(toc);
241 	tlist = (struct t_data *)my_zalloc(end_tno * sizeof (struct t_data));
242 
243 	audio_cd = data_cd = 0;
244 	total_nblks = 0;
245 
246 	/* build track information so we can copy it over */
247 	for (i = 1; i <= end_tno; i++) {
248 		struct track_info *ti;
249 
250 		ti = &tlist[i - 1].ti;
251 		if (!build_track_info(src, i, ti)) {
252 			err_msg(gettext(
253 			    "Cannot get information for track %d\n"), i);
254 			exit(1);
255 		}
256 		total_nblks += ti->ti_track_size;
257 		if (ti->ti_track_mode & 4)
258 			data_cd = 1;
259 		else
260 			audio_cd = 1;
261 
262 		/* Now some sanity checks on the track information */
263 		if ((ti->ti_flags & TI_SESSION_NO_VALID) &&
264 		    (ti->ti_session_no != 1)) {
265 			err_msg(
266 			gettext("Copying multisession CD is not supported\n"));
267 			exit(1);
268 		}
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)) {
272 
273 			err_msg(gettext("CD format is not supported\n"));
274 			exit(1);
275 		}
276 		if ((ti->ti_flags & TI_NWA_VALID) &&
277 		    (ti->ti_nwa != 0xffffffff)) {
278 			err_msg(gettext("Cannot copy incomplete discs\n"));
279 			exit(1);
280 		}
281 	}
282 	/* l10n_NOTE : 'done' as in "Analyzing source CD...done"  */
283 	(void) printf(gettext("done.\n"));
284 
285 	if (data_cd) {
286 		blksize = 2048;
287 	} else {
288 		/* audio cd */
289 		blksize = 2352;
290 	}
291 
292 	/* In case of audio CDs, build_track_info() returns 2352 sized nblks */
293 	if (src->d_blksize == 512 && data_cd) {
294 		total_nblks /= 4;
295 	}
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));
299 
300 	if ((ret = check_avail_temp_space(total_nblks*blksize)) != 0) {
301 		err_msg(gettext("Cannot use temporary directory : %s\n"),
302 		    strerror(ret));
303 		err_msg(gettext("Use -m to specify alternate"
304 		    " temporary directory\n"));
305 		exit(1);
306 	}
307 
308 	/*
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.
312 	 */
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);
317 	}
318 
319 	/* for each track */
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"),
324 			    get_err_str());
325 			exit(1);
326 		}
327 
328 		if (audio_cd)
329 			ret = read_audio_track(src, &tlist[i - 1].ti,
330 			    tlist[i - 1].h);
331 		else
332 			ret = read_data_track(src, &tlist[i - 1].ti,
333 			    tlist[i - 1].h);
334 		if (ret == 0) {
335 			err_msg(gettext("Error reading track %d : %s\n"), i,
336 			    get_err_str());
337 			if (debug)
338 				(void) printf("%x %x %x %x\n", uscsi_status,
339 				    SENSE_KEY(rqbuf), ASC(rqbuf), ASCQ(rqbuf));
340 			exit(1);
341 		}
342 	}
343 
344 	/*
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.
348 	 *
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.
351 	 */
352 
353 	do {
354 		if (target != NULL) {
355 			(void) eject_media(target);
356 		}
357 
358 		(void) printf("\n");
359 		print_n_flush(
360 		gettext("Insert a blank media in the drive and press Enter."));
361 		(void) fflush(stdin);
362 		if (target) {
363 			fini_device(target);
364 			target = NULL;
365 		}
366 		(void) getchar();
367 		(void) sleep(4);
368 		(void) setup_target(SCAN_WRITERS);
369 		if (target)
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)));
374 
375 	(void) printf("\n");
376 	(void) setreuid(ruid, 0);
377 
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);
381 	}
382 
383 	/*
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
388 	 * write.
389 	 */
390 	if (simulation && (device_type == DVD_PLUS_W ||
391 	    device_type == DVD_PLUS)) {
392 		err_msg(gettext("Media does not support simulated writing.\n"));
393 		exit(1);
394 	}
395 
396 	if (device_type == DVD_PLUS_W) {
397 		/*
398 		 * DVD+RW requires that we format the media before
399 		 * writing.
400 		 */
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"));
405 			exit(1);
406 		} else {
407 			int counter;
408 			uchar_t *di;
409 
410 			/* poll until format is done */
411 			di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
412 			(void) sleep(10);
413 			for (counter = 0; counter < 200; counter++) {
414 				ret = read_disc_info(target->d_fd, di);
415 				if ((SENSE_KEY(rqbuf) == 2) &&
416 				    (ASC(rqbuf) == 4)) {
417 					(void) print_n_flush(".");
418 					(void) sleep(5);
419 				} else {
420 					break;
421 				}
422 			}
423 		}
424 	}
425 
426 	/* for each track */
427 	for (i = 0; i < end_tno; i++) {
428 		/*
429 		 * DVD's dont contain tracks and need to be written in DAO
430 		 * mode.
431 		 */
432 		if (device_type != CD_RW) {
433 			if (end_tno > 1) {
434 				err_msg(gettext(
435 				    "Media state is not suitable for this"
436 				    " write mode.\n"));
437 			}
438 			write_mode = DAO_MODE;
439 
440 			/*
441 			 * DVD-R(W) and DVD+R needs to have space reserved
442 			 * prior to writing.
443 			 */
444 			if ((device_type == DVD_MINUS) ||
445 			    (device_type == DVD_PLUS)) {
446 				if (!set_reservation(target->d_fd,
447 				    total_nblks + 1)) {
448 					(void) printf(gettext(
449 					    "Setting reservation failed\n"));
450 					exit(1);
451 				}
452 			}
453 		}
454 
455 		write_next_track(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA,
456 		    tlist[i].h);
457 
458 		/*
459 		 * Running in simulation mode and writing several tracks is
460 		 * useless so bail after the first track is done.
461 		 */
462 
463 		if (simulation && (end_tno != 1)) {
464 			(void) printf(gettext(
465 			"Simulation mode : skipping remaining tracks\n"));
466 			break;
467 		}
468 	}
469 
470 	write_fini();
471 	/* close the temp file handles */
472 	for (i = 0; i < end_tno; i++)
473 		(tlist[i].h)->bstr_close(tlist[i].h);
474 	free(tlist);
475 	fini_device(target);
476 	exit(0);
477 }
478