xref: /titanic_44/usr/src/cmd/cdrw/copycd.c (revision 60b08185ce63023f22fd6b2ed0db8c0d119b2023)
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 2005 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 #include <string.h>
37 
38 #include "main.h"
39 #include "util.h"
40 #include "misc_scsi.h"
41 #include "mmc.h"
42 #include "bstream.h"
43 #include "device.h"
44 #include "msgs.h"
45 #include "transport.h"
46 
47 struct t_data {
48 	bstreamhandle h;
49 	struct track_info ti;
50 };
51 
52 int read_audio_track(cd_device *dev, struct track_info *ti, bstreamhandle h);
53 
54 #define	READ_BURST	24	/* < 64K in all cases */
55 
56 /*
57  * This reads the data off of a cd while updating the progress indicator.
58  * We want to do this in smaller chunks since some CD drives have
59  * problems with larger reads.
60  */
61 static int
62 read_data_track(cd_device *dev, struct track_info *ti, bstreamhandle h)
63 {
64 	int blksize;
65 	uint32_t blks_read, cblk, read_chunk, read_size;
66 	uchar_t *buf;
67 	int ret, sav;
68 	int link_blks_count;
69 
70 	buf = NULL;
71 	ret = 0;
72 
73 	/*
74 	 * the last link_blks_count blocks may not exist or be completely
75 	 * filled. We need to record the amount to avoid bailing out if
76 	 * they cannot be read.
77 	 */
78 
79 	if (dev->d_blksize == 512) {
80 		blksize = 512;
81 		link_blks_count = 8;
82 	} else {
83 		blksize = 2048;
84 		link_blks_count = 2;
85 	}
86 
87 	buf = (uchar_t *)my_zalloc(READ_BURST * blksize);
88 
89 	print_n_flush(gettext("Reading track %d..."), ti->ti_track_no);
90 
91 	if (verbose)
92 		print_n_flush("Track size is %u...", ti->ti_track_size);
93 
94 	init_progress();
95 	cblk = ti->ti_start_address;
96 	blks_read = 0;
97 	while (blks_read < ti->ti_track_size) {
98 		/* Last few are special */
99 		read_chunk = ti->ti_track_size - blks_read - link_blks_count;
100 		read_chunk = (read_chunk > READ_BURST) ? READ_BURST :
101 		    read_chunk;
102 		if (read_chunk == 0) {
103 			/* Time for last link blocks */
104 			read_chunk = link_blks_count;
105 		}
106 		read_size = read_chunk * blksize;
107 		if (read10(dev->d_fd, cblk, read_chunk, buf, read_size)) {
108 			if (h->bstr_write(h, buf, read_size) != read_size) {
109 				goto read_data_track_failed;
110 			}
111 		} else {
112 			if (blks_read !=
113 			    (ti->ti_track_size - link_blks_count)) {
114 				goto read_data_track_failed;
115 			} else {
116 			/* Read can fail for last link sectors */
117 				errno = 0;
118 			}
119 		}
120 		blks_read += read_chunk;
121 		cblk += read_chunk;
122 		(void) progress((ti->ti_track_size), blks_read);
123 	}
124 	/* l10n_NOTE : 'done' as in "Reading track 1...done"  */
125 	(void) str_print(gettext("done.\n"), progress_pos);
126 	ret = 1;
127 read_data_track_failed:
128 	sav = errno;
129 
130 	free(buf);
131 	errno = sav;
132 	return (ret);
133 }
134 
135 static void
136 ensure_media_space(uint32_t total_nblks, uchar_t end_tno)
137 {
138 	off_t nblks_avail;
139 	uint_t bsize;
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 		}
160 	} else {
161 		if (device_type == CD_RW) {
162 			nblks_avail = MAX_CD_BLKS;
163 		} else {
164 			/*
165 			 * For DVD drives use read_format_capacity as default
166 			 * retrieve the media size, it can be 3.6, 3.9, 4.2,
167 			 * 4.7, or 9.2 GB
168 			 */
169 			nblks_avail =
170 			    read_format_capacity(target->d_fd, &bsize);
171 
172 			/* sanity check. if not reasonable default to 4.7 GB */
173 			if (nblks_avail < MAX_CD_BLKS) {
174 				nblks_avail = MAX_DVD_BLKS;
175 			}
176 		}
177 	}
178 
179 	if ((total_nblks + end_tno*300) > nblks_avail) {
180 		err_msg(gettext("Not enough space on the media.\n"));
181 		if (debug) {
182 			(void) printf("Need %u  only found %u \n",
183 			    (total_nblks + end_tno*300),
184 			    (uint32_t)nblks_avail);
185 		}
186 
187 		exit(1);
188 	}
189 }
190 
191 /*
192  * This copies both audio and data CDs. It first reads the TOC of the source CD
193  * and creates a temp file with the CD image. After this is completed it creates
194  * the target CD using TAO mode.
195  */
196 void
197 copy_cd(void)
198 {
199 	cd_device *src;
200 	char *p;
201 	uchar_t *toc, end_tno;
202 	int blksize, i;
203 	int audio_cd, data_cd;
204 	uint32_t total_nblks;
205 	int ret;
206 	struct t_data *tlist;
207 
208 	print_n_flush(gettext("Analyzing source CD..."));
209 	(void) check_device(target,
210 	    CHECK_DEVICE_NOT_WRITABLE|EXIT_IF_CHECK_FAILED);
211 
212 	/* if source drive is specified on the command line */
213 
214 	if (copy_src) {
215 		p = my_zalloc(PATH_MAX);
216 		if (lookup_device(copy_src, p) == 0) {
217 			err_msg(gettext("Cannot find device %s"), copy_src);
218 			err_msg(gettext(" or no media in the drive\n"));
219 			exit(1);
220 		}
221 		src = get_device(copy_src, p);
222 		if (src == NULL) {
223 			err_msg(gettext("Unable to open %s\n"), copy_src);
224 			exit(1);
225 		}
226 		free(p);
227 	} else {
228 		/* source is same as target drive */
229 		src = target;
230 	}
231 
232 	(void) check_device(src, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
233 	    CHECK_DEVICE_NOT_READY | EXIT_IF_CHECK_FAILED);
234 
235 	toc = (uchar_t *)my_zalloc(4);
236 	if (!read_toc(src->d_fd, 0, 0, 4, toc)) {
237 		err_msg(gettext("Cannot read table of contents\n"));
238 		exit(1);
239 	}
240 	end_tno = toc[3];
241 	free(toc);
242 	tlist = (struct t_data *)my_zalloc(end_tno * sizeof (struct t_data));
243 
244 	audio_cd = data_cd = 0;
245 	total_nblks = 0;
246 
247 	/* build track information so we can copy it over */
248 	for (i = 1; i <= end_tno; i++) {
249 		struct track_info *ti;
250 
251 		ti = &tlist[i - 1].ti;
252 		if (!build_track_info(src, i, ti)) {
253 			err_msg(gettext(
254 			    "Cannot get information for track %d\n"), i);
255 			exit(1);
256 		}
257 		total_nblks += ti->ti_track_size;
258 		if (ti->ti_track_mode & 4)
259 			data_cd = 1;
260 		else
261 			audio_cd = 1;
262 
263 		/* Now some sanity checks on the track information */
264 		if ((ti->ti_flags & TI_SESSION_NO_VALID) &&
265 		    (ti->ti_session_no != 1)) {
266 			err_msg(
267 			gettext("Copying multisession CD is not supported\n"));
268 			exit(1);
269 		}
270 		if ((ti->ti_flags & TI_PACKET_MODE) ||
271 		    (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 	/* for each track */
379 	for (i = 0; i < end_tno; i++) {
380 		/*
381 		 * DVD's dont contain tracks and need to be written in DAO
382 		 * mode.
383 		 */
384 		if (device_type != CD_RW) {
385 			if (end_tno > 1) {
386 				err_msg(gettext(
387 				    "Media state is not suitable for this"
388 				    " write mode.\n"));
389 			}
390 			write_mode = DAO_MODE;
391 
392 			/*
393 			 * DVD-R(W) and DVD+R needs to have space reserved
394 			 * prior to writing.
395 			 */
396 			if ((device_type == DVD_MINUS) ||
397 			    (device_type == DVD_PLUS)) {
398 				if (!set_reservation(target->d_fd,
399 				    total_nblks + 1)) {
400 					(void) printf(gettext(
401 					    "Setting reservation failed\n"));
402 					exit(1);
403 				}
404 			}
405 		}
406 
407 		write_next_track(audio_cd ? TRACK_MODE_AUDIO : TRACK_MODE_DATA,
408 		    tlist[i].h);
409 
410 		/*
411 		 * Running in simulation mode and writing several tracks is
412 		 * useless so bail after the first track is done.
413 		 */
414 
415 		if (simulation && (end_tno != 1)) {
416 			(void) printf(gettext(
417 			"Simulation mode : skipping remaining tracks\n"));
418 			break;
419 		}
420 	}
421 
422 	write_fini();
423 	/* close the temp file handles */
424 	for (i = 0; i < end_tno; i++)
425 		(tlist[i].h)->bstr_close(tlist[i].h);
426 	free(tlist);
427 	fini_device(target);
428 	exit(0);
429 }
430