xref: /titanic_52/usr/src/cmd/cdrw/copycd.c (revision f498645a3eecf2ddd304b4ea9c7f1b4c155ff79e)
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 2006 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 	off_t nblks_avail;
138 	uint_t bsize;
139 	uint_t leadin_size = 0;
140 
141 	get_media_type(target->d_fd);
142 
143 	if (use_media_stated_capacity) {
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 no choice but to bail out */
153 			if (nblks_avail <= 0) {
154 
155 				err_msg(gettext(
156 				    "Cannot find out media capacity.\n"));
157 				exit(1);
158 			}
159 		leadin_size = end_tno*300;
160 		}
161 	} else {
162 		if (device_type == CD_RW) {
163 			nblks_avail = MAX_CD_BLKS;
164 		} else {
165 			/*
166 			 * For DVD drives use read_format_capacity as default
167 			 * retrieve the media size, it can be 3.6, 3.9, 4.2,
168 			 * 4.7, or 9.2 GB
169 			 */
170 			nblks_avail =
171 			    read_format_capacity(target->d_fd, &bsize);
172 
173 			/* sanity check. if not reasonable default to 4.7 GB */
174 			if (nblks_avail < MAX_CD_BLKS) {
175 				nblks_avail = MAX_DVD_BLKS;
176 			}
177 		}
178 	}
179 
180 	if ((total_nblks + leadin_size) > nblks_avail) {
181 		err_msg(gettext("Not enough space on the media.\n"));
182 		if (debug) {
183 			(void) printf("Need %u  only found %u \n",
184 			    (total_nblks + leadin_size),
185 			    (uint32_t)nblks_avail);
186 		}
187 
188 		exit(1);
189 	}
190 }
191 
192 /*
193  * This copies both audio and data CDs. It first reads the TOC of the source CD
194  * and creates a temp file with the CD image. After this is completed it creates
195  * the target CD using TAO mode.
196  */
197 void
198 copy_cd(void)
199 {
200 	cd_device *src;
201 	char *p;
202 	uchar_t *toc, end_tno;
203 	int blksize, i;
204 	int audio_cd, data_cd;
205 	uint32_t total_nblks;
206 	int ret;
207 	struct t_data *tlist;
208 
209 	print_n_flush(gettext("Analyzing source CD..."));
210 	(void) check_device(target,
211 	    CHECK_DEVICE_NOT_WRITABLE|EXIT_IF_CHECK_FAILED);
212 
213 	/* if source drive is specified on the command line */
214 
215 	if (copy_src) {
216 		p = my_zalloc(PATH_MAX);
217 		if (lookup_device(copy_src, p) == 0) {
218 			err_msg(gettext("Cannot find device %s"), copy_src);
219 			err_msg(gettext(" or no media in the drive\n"));
220 			exit(1);
221 		}
222 		src = get_device(copy_src, p);
223 		if (src == NULL) {
224 			err_msg(gettext("Unable to open %s\n"), copy_src);
225 			exit(1);
226 		}
227 		free(p);
228 	} else {
229 		/* source is same as target drive */
230 		src = target;
231 	}
232 
233 	(void) check_device(src, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
234 	    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
235 
236 	toc = (uchar_t *)my_zalloc(4);
237 	if (!read_toc(src->d_fd, 0, 0, 4, toc)) {
238 		err_msg(gettext("Cannot read table of contents\n"));
239 		exit(1);
240 	}
241 	end_tno = toc[3];
242 	free(toc);
243 	tlist = (struct t_data *)my_zalloc(end_tno * sizeof (struct t_data));
244 
245 	audio_cd = data_cd = 0;
246 	total_nblks = 0;
247 
248 	/* build track information so we can copy it over */
249 	for (i = 1; i <= end_tno; i++) {
250 		struct track_info *ti;
251 
252 		ti = &tlist[i - 1].ti;
253 		if (!build_track_info(src, i, ti)) {
254 			err_msg(gettext(
255 			    "Cannot get information for track %d\n"), i);
256 			exit(1);
257 		}
258 		total_nblks += ti->ti_track_size;
259 		if (ti->ti_track_mode & 4)
260 			data_cd = 1;
261 		else
262 			audio_cd = 1;
263 
264 		/* Now some sanity checks on the track information */
265 		if ((ti->ti_flags & TI_SESSION_NO_VALID) &&
266 		    (ti->ti_session_no != 1)) {
267 			err_msg(
268 			gettext("Copying multisession CD is not supported\n"));
269 			exit(1);
270 		}
271 		if ((ti->ti_flags & TI_BLANK_TRACK) ||
272 		    (ti->ti_flags & TI_DAMAGED_TRACK) ||
273 		    (data_cd && audio_cd) || (ti->ti_data_mode == 2)) {
274 
275 		    err_msg(gettext("CD format is not supported\n"));
276 			exit(1);
277 		}
278 		if ((ti->ti_flags & TI_NWA_VALID) &&
279 		    (ti->ti_nwa != 0xffffffff)) {
280 			err_msg(gettext("Cannot copy incomplete discs\n"));
281 			exit(1);
282 		}
283 	}
284 	/* l10n_NOTE : 'done' as in "Analyzing source CD...done"  */
285 	(void) printf(gettext("done.\n"));
286 
287 	if (data_cd) {
288 		blksize = 2048;
289 	} else {
290 		/* audio cd */
291 		blksize = 2352;
292 	}
293 
294 	/* In case of audio CDs, build_track_info() returns 2352 sized nblks */
295 	if (src->d_blksize == 512 && data_cd) {
296 		total_nblks /= 4;
297 	}
298 	(void) printf(gettext("\nCopying %d %s track%s : %ld kbytes\n\n"),
299 	    end_tno, (audio_cd == 1) ? gettext("audio") : gettext("data"),
300 	    (end_tno > 1) ? "s" : "", (long)((total_nblks*blksize)/1024));
301 
302 	if ((ret = check_avail_temp_space(total_nblks*blksize)) != 0) {
303 		err_msg(gettext("Cannot use temporary directory : %s\n"),
304 		    strerror(ret));
305 		err_msg(gettext("Use -m to specify alternate"
306 		    " temporary directory\n"));
307 		exit(1);
308 	}
309 
310 	/*
311 	 * If we can check available space on the target media at this
312 	 * Stage, then it is always better. We cannot check DVD+R(W)
313 	 * as this media may be formatted and not blank.
314 	 */
315 	if (target && (src != target) && (device_type != DVD_PLUS) &&
316 	    (device_type != DVD_PLUS_W) && (!check_device(target,
317 	    CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK))) {
318 		ensure_media_space(total_nblks, end_tno);
319 	}
320 
321 	/* for each track */
322 	for (i = 1; i <= end_tno; i++) {
323 		tlist[i - 1].h = open_temp_file_stream();
324 		if (tlist[i - 1].h == NULL) {
325 			err_msg(gettext("Cannot create temporary file : %s\n"),
326 			    get_err_str());
327 			exit(1);
328 		}
329 
330 		if (audio_cd)
331 			ret = read_audio_track(src, &tlist[i - 1].ti,
332 			    tlist[i - 1].h);
333 		else
334 			ret = read_data_track(src, &tlist[i - 1].ti,
335 			    tlist[i - 1].h);
336 		if (ret == 0) {
337 			err_msg(gettext("Error reading track %d : %s\n"), i,
338 			    get_err_str());
339 			if (debug)
340 				(void) printf("%x %x %x %x\n", uscsi_status,
341 				    SENSE_KEY(rqbuf), ASC(rqbuf), ASCQ(rqbuf));
342 			exit(1);
343 		}
344 	}
345 
346 	/*
347 	 * We've finished copying the CD. If source and destination are the same
348 	 * or they where not specified then eject the disk and wait for a new
349 	 * disk to be inserted.
350 	 */
351 
352 	while ((target == NULL) ||
353 	    check_device(target, CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK)) {
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 	}
370 	(void) printf("\n");
371 	(void) setreuid(ruid, 0);
372 
373 	if ((device_type != DVD_PLUS) && (device_type != DVD_PLUS_W)) {
374 		ensure_media_space(total_nblks, end_tno);
375 		write_init(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA);
376 	}
377 
378 	/*
379 	 * Simulation writing can't happen on DVD+RW's
380 	 * or DVD+R's. According to the MMC spec this
381 	 * operation is not supported. So we should
382 	 * bail out if the user tries to do a simulation
383 	 * write.
384 	 */
385 	if (simulation && (device_type == DVD_PLUS_W ||
386 	    device_type == DVD_PLUS)) {
387 		err_msg(gettext("Media does not support simulated writing.\n"));
388 		exit(1);
389 	}
390 
391 	if (device_type == DVD_PLUS_W) {
392 		/*
393 		 * DVD+RW requires that we format the media before
394 		 * writing.
395 		 */
396 		(void) print_n_flush(gettext("Formatting media..."));
397 		if (!format_media(target->d_fd)) {
398 			(void) printf(gettext(
399 			    "Could not format media\n"));
400 			exit(1);
401 		} else {
402 			int counter;
403 			uchar_t *di;
404 
405 			/* poll until format is done */
406 			di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
407 			(void) sleep(10);
408 			for (counter = 0; counter < 200; counter++) {
409 				ret = read_disc_info(target->d_fd, di);
410 				if ((SENSE_KEY(rqbuf) == 2) &&
411 				    (ASC(rqbuf) == 4)) {
412 					(void) print_n_flush(".");
413 					(void) sleep(5);
414 				} else {
415 					break;
416 				}
417 			}
418 		}
419 	}
420 
421 	/* for each track */
422 	for (i = 0; i < end_tno; i++) {
423 		/*
424 		 * DVD's dont contain tracks and need to be written in DAO
425 		 * mode.
426 		 */
427 		if (device_type != CD_RW) {
428 			if (end_tno > 1) {
429 				err_msg(gettext(
430 				    "Media state is not suitable for this"
431 				    " write mode.\n"));
432 			}
433 			write_mode = DAO_MODE;
434 
435 			/*
436 			 * DVD-R(W) and DVD+R needs to have space reserved
437 			 * prior to writing.
438 			 */
439 			if ((device_type == DVD_MINUS) ||
440 			    (device_type == DVD_PLUS)) {
441 				if (!set_reservation(target->d_fd,
442 				    total_nblks + 1)) {
443 					(void) printf(gettext(
444 					    "Setting reservation failed\n"));
445 					exit(1);
446 				}
447 			}
448 		}
449 
450 		write_next_track(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA,
451 		    tlist[i].h);
452 
453 		/*
454 		 * Running in simulation mode and writing several tracks is
455 		 * useless so bail after the first track is done.
456 		 */
457 
458 		if (simulation && (end_tno != 1)) {
459 			(void) printf(gettext(
460 			"Simulation mode : skipping remaining tracks\n"));
461 			break;
462 		}
463 	}
464 
465 	write_fini();
466 	/* close the temp file handles */
467 	for (i = 0; i < end_tno; i++)
468 		(tlist[i].h)->bstr_close(tlist[i].h);
469 	free(tlist);
470 	fini_device(target);
471 	exit(0);
472 }
473