xref: /onnv-gate/usr/src/cmd/cdrw/write_image.c (revision 7024:c74e1591c8fd)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51707Szk194757  * Common Development and Distribution License (the "License").
61707Szk194757  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*7024Sec158148  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <libintl.h>
310Sstevel@tonic-gate #include <unistd.h>
320Sstevel@tonic-gate #include "trackio.h"
330Sstevel@tonic-gate #include "main.h"
340Sstevel@tonic-gate #include "util.h"
350Sstevel@tonic-gate #include "bstream.h"
360Sstevel@tonic-gate #include "misc_scsi.h"
370Sstevel@tonic-gate #include "msgs.h"
380Sstevel@tonic-gate #include "device.h"
390Sstevel@tonic-gate #include "mmc.h"
400Sstevel@tonic-gate #include "transport.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate void
write_image(void)430Sstevel@tonic-gate write_image(void)
440Sstevel@tonic-gate {
450Sstevel@tonic-gate 	bstreamhandle h;
460Sstevel@tonic-gate 	off_t size;
470Sstevel@tonic-gate 	int no_size, ret;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 	get_media_type(target->d_fd);
500Sstevel@tonic-gate 
510Sstevel@tonic-gate 	/* DVD+RW does not have blanking and can be overwritten */
520Sstevel@tonic-gate 	if (device_type != DVD_PLUS_W) {
530Sstevel@tonic-gate 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
540Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
550Sstevel@tonic-gate 	    EXIT_IF_CHECK_FAILED);
560Sstevel@tonic-gate 	} else {
570Sstevel@tonic-gate 		(void) check_device(target, CHECK_DEVICE_NOT_READY |
585337Szk194757 		    EXIT_IF_CHECK_FAILED);
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 
611707Szk194757 	/*
621707Szk194757 	 * Simulation writing can't happen on DVD+RW's
631707Szk194757 	 * or DVD+R's. According to the MMC spec this
641707Szk194757 	 * operation is not supported. So we should
651707Szk194757 	 * bail out if the user tries to do a simulation
661707Szk194757 	 * write.
671707Szk194757 	 */
681707Szk194757 	if (simulation && (device_type == DVD_PLUS_W ||
691707Szk194757 	    device_type == DVD_PLUS)) {
701707Szk194757 		err_msg(gettext("Media does not support simulated writing.\n"));
711707Szk194757 		exit(1);
721707Szk194757 	}
731707Szk194757 
740Sstevel@tonic-gate 	write_init(TRACK_MODE_DATA);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (image_file) {
774274Szk194757 		h = open_iso_read_stream(image_file);
780Sstevel@tonic-gate 	} else {
790Sstevel@tonic-gate 		h = open_stdin_read_stream();
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if (h == NULL) {
830Sstevel@tonic-gate 		err_msg(gettext("Cannot open %s: %s\n"),
840Sstevel@tonic-gate 		    image_file ? image_file : "stdin", get_err_str());
850Sstevel@tonic-gate 		exit(1);
860Sstevel@tonic-gate 	}
870Sstevel@tonic-gate 	no_size = 0;
880Sstevel@tonic-gate 	ret = h->bstr_size(h, &size);
890Sstevel@tonic-gate 	if (ret == 0) {
900Sstevel@tonic-gate 		if ((str_errno == STR_ERR_NO_REG_FILE)) {
910Sstevel@tonic-gate 			no_size = 1;
920Sstevel@tonic-gate 		} else {
930Sstevel@tonic-gate 			err_msg(gettext("Cannot stat input file: %s\n"),
940Sstevel@tonic-gate 			    get_err_str());
950Sstevel@tonic-gate 			exit(1);
960Sstevel@tonic-gate 		}
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 	if ((no_size == 0) && (size == 0)) {
990Sstevel@tonic-gate 		err_msg(gettext("Input size(0) not valid\n"));
1000Sstevel@tonic-gate 		exit(1);
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	if (no_size == 0) {
1035476Szk194757 		off_t cap;
1040Sstevel@tonic-gate 		struct track_info *ti;
1050Sstevel@tonic-gate 		uint_t bsize;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 		ti = (struct track_info *)my_zalloc(sizeof (*ti));
1080Sstevel@tonic-gate 		if (write_mode == TAO_MODE)
1095337Szk194757 			if (!build_track_info(target, -1, ti)) {
1105337Szk194757 				err_msg(
1115337Szk194757 				    gettext("Unable to find out writable "
1125337Szk194757 				    "address\n"));
1135337Szk194757 				exit(1);
1140Sstevel@tonic-gate 			}
115*7024Sec158148 		if (device_type == CD_RW) {
116*7024Sec158148 			if ((cap = get_last_possible_lba(target)) <= 0) {
117*7024Sec158148 				if ((cap = read_format_capacity(target->d_fd,
118*7024Sec158148 				    &bsize)) <= 0) {
119*7024Sec158148 					err_msg(gettext("Unable to determine "
120*7024Sec158148 					    "media capacity.  Defaulting to "
121*7024Sec158148 					    "650 MB (74 minute) disc.\n"));
122*7024Sec158148 					cap = MAX_CD_BLKS;
123*7024Sec158148 				}
1240Sstevel@tonic-gate 			}
1250Sstevel@tonic-gate 		} else {
1260Sstevel@tonic-gate 			/*
127*7024Sec158148 			 * For DVD drives use read_format_capacity to
128*7024Sec158148 			 * find media size, it can be 3.6, 3.9, 4.2,
129*7024Sec158148 			 * 4.7, 9.2
1300Sstevel@tonic-gate 			 */
131*7024Sec158148 			cap = read_format_capacity(target->d_fd,
132*7024Sec158148 			    &bsize);
133*7024Sec158148 			/*
134*7024Sec158148 			 * Sanity check; Default to 4.7 GB if cap unreasonable
135*7024Sec158148 			 */
136*7024Sec158148 			if (cap < MAX_CD_BLKS)
137*7024Sec158148 				cap = MAX_DVD_BLKS;
1380Sstevel@tonic-gate 		}
139*7024Sec158148 
1400Sstevel@tonic-gate 		if (device_type == CD_RW)
1410Sstevel@tonic-gate 			cap = (cap + 1 - ti->ti_start_address) * 2048;
1420Sstevel@tonic-gate 		else
1430Sstevel@tonic-gate 			cap *= 2048 + 1;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		if (size > cap) {
1460Sstevel@tonic-gate 			err_msg(gettext("Size required (%lld bytes) is greater "
1470Sstevel@tonic-gate 			    "than available space (%lld bytes).\n"), size, cap);
1480Sstevel@tonic-gate 			exit(1);
1490Sstevel@tonic-gate 		}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		if (device_type == DVD_MINUS) {
1520Sstevel@tonic-gate 			(void) printf(gettext("Preparing to write DVD\n"));
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 			/* streamed file, we dont know the size to reserve */
1550Sstevel@tonic-gate 			if (no_size == 1) {
1560Sstevel@tonic-gate 				size = cap - 1;
1570Sstevel@tonic-gate 			}
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 			/* DAO requires that we reserve the size to write */
1600Sstevel@tonic-gate 			if (debug)
1610Sstevel@tonic-gate 				(void) printf(
1620Sstevel@tonic-gate 				    "DAO_MODE:reserving track size of = 0x%x\n",
1630Sstevel@tonic-gate 				    (uint32_t)(size/2048));
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 			if (!set_reservation(target->d_fd, size/2048)) {
1660Sstevel@tonic-gate 				(void) printf(gettext(
1670Sstevel@tonic-gate 				    "Setting reservation failed\n"));
1680Sstevel@tonic-gate 				exit(1);
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 		} else if (device_type == DVD_PLUS_W) {
1710Sstevel@tonic-gate 			/*
1720Sstevel@tonic-gate 			 * DVD+RW requires that we format the media before
1730Sstevel@tonic-gate 			 * writing.
1740Sstevel@tonic-gate 			 */
1750Sstevel@tonic-gate 			(void) print_n_flush(gettext("Formatting media..."));
1760Sstevel@tonic-gate 			if (!format_media(target->d_fd)) {
1770Sstevel@tonic-gate 				(void) printf(gettext(
1780Sstevel@tonic-gate 				    "Could not format media\n"));
1790Sstevel@tonic-gate 				exit(1);
1800Sstevel@tonic-gate 			} else {
1810Sstevel@tonic-gate 				int counter;
1820Sstevel@tonic-gate 				uchar_t *di;
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate 				/* poll until format is done */
1850Sstevel@tonic-gate 				di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
1860Sstevel@tonic-gate 				(void) sleep(10);
1870Sstevel@tonic-gate 				for (counter = 0; counter < 200; counter++) {
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 				ret = read_disc_info(target->d_fd, di);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 				if ((SENSE_KEY(rqbuf) == 2) &&
1920Sstevel@tonic-gate 				    (ASC(rqbuf) == 4)) {
1930Sstevel@tonic-gate 					(void) print_n_flush(".");
1940Sstevel@tonic-gate 					(void) sleep(5);
1950Sstevel@tonic-gate 				} else {
1960Sstevel@tonic-gate 					break;
1970Sstevel@tonic-gate 					}
1980Sstevel@tonic-gate 				}
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 			(void) printf(gettext("done\n"));
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 		free(ti);
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	write_next_track(TRACK_MODE_DATA, h);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	h->bstr_close(h);
2110Sstevel@tonic-gate 	write_fini();
2120Sstevel@tonic-gate 	fini_device(target);
2130Sstevel@tonic-gate 	exit(0);
2140Sstevel@tonic-gate }
215