xref: /titanic_41/usr/src/cmd/cdrw/copycd.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <sys/types.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <unistd.h>
35 #include <libintl.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((void *)(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 
140 	get_media_type(target->d_fd);
141 
142 	if (use_media_stated_capacity) {
143 		nblks_avail = get_last_possible_lba(target);
144 
145 		if (nblks_avail <= 0) {
146 
147 			/* most newer drives use READ FORMAT CAPACITY */
148 			nblks_avail = read_format_capacity(target->d_fd,
149 			    &bsize);
150 
151 			/* if both methods fail no choice but to bail out */
152 			if (nblks_avail <= 0) {
153 
154 				err_msg(gettext(
155 				    "Cannot find out media capacity.\n"));
156 				exit(1);
157 			}
158 		}
159 	} else {
160 		if (device_type == CD_RW) {
161 			nblks_avail = MAX_CD_BLKS;
162 		} else {
163 			/*
164 			 * For DVD drives use read_format_capacity as default
165 			 * retrieve the media size, it can be 3.6, 3.9, 4.2,
166 			 * 4.7, or 9.2 GB
167 			 */
168 			nblks_avail =
169 			    read_format_capacity(target->d_fd, &bsize);
170 
171 			/* sanity check. if not reasonable default to 4.7 GB */
172 			if (nblks_avail < MAX_CD_BLKS) {
173 				nblks_avail = MAX_DVD_BLKS;
174 			}
175 		}
176 	}
177 
178 	if ((total_nblks + end_tno*300) > nblks_avail) {
179 		err_msg(gettext("Not enough space on the media.\n"));
180 		if (debug) {
181 			(void) printf("Need %u  only found %u \n",
182 			    (total_nblks + end_tno*300),
183 			    (uint32_t)nblks_avail);
184 		}
185 
186 		exit(1);
187 	}
188 }
189 
190 /*
191  * This copies both audio and data CDs. It first reads the TOC of the source CD
192  * and creates a temp file with the CD image. After this is completed it creates
193  * the target CD using TAO mode.
194  */
195 void
196 copy_cd(void)
197 {
198 	cd_device *src;
199 	char *p;
200 	uchar_t *toc, end_tno;
201 	int blksize, i;
202 	int audio_cd, data_cd;
203 	uint32_t total_nblks;
204 	int ret;
205 	struct t_data *tlist;
206 
207 	print_n_flush(gettext("Analyzing source CD..."));
208 	(void) check_device(target,
209 	    CHECK_DEVICE_NOT_WRITABLE|EXIT_IF_CHECK_FAILED);
210 
211 	/* if source drive is specified on the command line */
212 
213 	if (copy_src) {
214 		p = my_zalloc(PATH_MAX);
215 		if (lookup_device(copy_src, p) == 0) {
216 			err_msg(gettext("Cannot find device %s"), copy_src);
217 			err_msg(gettext(" or no media in the drive\n"));
218 			exit(1);
219 		}
220 		src = get_device(copy_src, p);
221 		if (src == NULL) {
222 			err_msg(gettext("Unable to open %s\n"), copy_src);
223 			exit(1);
224 		}
225 		free(p);
226 	} else {
227 		/* source is same as target drive */
228 		src = target;
229 	}
230 
231 	(void) check_device(src, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
232 	    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
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_PACKET_MODE) ||
270 		    (ti->ti_flags & TI_BLANK_TRACK) ||
271 		    (ti->ti_flags & TI_DAMAGED_TRACK) ||
272 		    (data_cd && audio_cd) || (ti->ti_data_mode == 2)) {
273 
274 		    err_msg(gettext("CD format is not supported\n"));
275 			exit(1);
276 		}
277 		if ((ti->ti_flags & TI_NWA_VALID) &&
278 		    (ti->ti_nwa != 0xffffffff)) {
279 			err_msg(gettext("Cannot copy incomplete discs\n"));
280 			exit(1);
281 		}
282 	}
283 	/* l10n_NOTE : 'done' as in "Analyzing source CD...done"  */
284 	(void) printf(gettext("done.\n"));
285 
286 	if (data_cd) {
287 		blksize = 2048;
288 	} else {
289 		/* audio cd */
290 		blksize = 2352;
291 	}
292 
293 	/* In case of audio CDs, build_track_info() returns 2352 sized nblks */
294 	if (src->d_blksize == 512 && data_cd) {
295 		total_nblks /= 4;
296 	}
297 	(void) printf(gettext("\nCopying %d %s track%s : %ld kbytes\n\n"),
298 	    end_tno, (audio_cd == 1) ? gettext("audio") : gettext("data"),
299 	    (end_tno > 1) ? "s" : "", (long)((total_nblks*blksize)/1024));
300 
301 	if (!check_avail_temp_space(total_nblks*blksize)) {
302 		err_msg(gettext("Not enough space in temporary directory\n"));
303 		err_msg(
304 		gettext("Use -m to specify alternate 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 
350 	while ((target == NULL) ||
351 	    check_device(target, CHECK_NO_MEDIA|CHECK_MEDIA_IS_NOT_BLANK)) {
352 		if (target != NULL) {
353 			(void) eject_media(target);
354 		}
355 
356 		(void) printf("\n");
357 		print_n_flush(
358 		gettext("Insert a blank media in the drive and press Enter."));
359 		(void) fflush(stdin);
360 		if (target) {
361 			fini_device(target);
362 			target = NULL;
363 		}
364 		(void) getchar();
365 		(void) sleep(4);
366 		(void) setup_target(SCAN_WRITERS);
367 	}
368 	(void) printf("\n");
369 	(void) setreuid(ruid, 0);
370 
371 	if ((device_type != DVD_PLUS) && (device_type != DVD_PLUS_W)) {
372 		ensure_media_space(total_nblks, end_tno);
373 		write_init(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA);
374 	}
375 
376 	/* for each track */
377 	for (i = 0; i < end_tno; i++) {
378 		/*
379 		 * DVD's dont contain tracks and need to be written in DAO
380 		 * mode.
381 		 */
382 		if (device_type != CD_RW) {
383 			if (end_tno > 1) {
384 				err_msg(gettext(
385 				    "Media state is not suitable for this"
386 				    " write mode.\n"));
387 			}
388 			write_mode = DAO_MODE;
389 
390 			/*
391 			 * DVD-R(W) and DVD+R needs to have space reserved
392 			 * prior to writing.
393 			 */
394 			if ((device_type == DVD_MINUS) ||
395 			    (device_type == DVD_PLUS)) {
396 				if (!set_reservation(target->d_fd,
397 				    total_nblks + 1)) {
398 					(void) printf(gettext(
399 					    "Setting reservation failed\n"));
400 					exit(1);
401 				}
402 			}
403 		}
404 
405 		write_next_track(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA,
406 		    tlist[i].h);
407 
408 		/*
409 		 * Running in simulation mode and writing several tracks is
410 		 * useless so bail after the first track is done.
411 		 */
412 
413 		if (simulation && (end_tno != 1)) {
414 			(void) printf(gettext(
415 			"Simulation mode : skipping remaining tracks\n"));
416 			break;
417 		}
418 	}
419 
420 	write_fini();
421 	/* close the temp file handles */
422 	for (i = 0; i < end_tno; i++)
423 		(tlist[i].h)->bstr_close(tlist[i].h);
424 	free(tlist);
425 	fini_device(target);
426 	exit(0);
427 }
428