xref: /onnv-gate/usr/src/cmd/cdrw/write_image.c (revision 1707)
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
5*1707Szk194757  * Common Development and Distribution License (the "License").
6*1707Szk194757  * 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*1707Szk194757  * Copyright 2006 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
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 |
580Sstevel@tonic-gate 		EXIT_IF_CHECK_FAILED);
590Sstevel@tonic-gate 	}
600Sstevel@tonic-gate 
61*1707Szk194757 	/*
62*1707Szk194757 	 * Simulation writing can't happen on DVD+RW's
63*1707Szk194757 	 * or DVD+R's. According to the MMC spec this
64*1707Szk194757 	 * operation is not supported. So we should
65*1707Szk194757 	 * bail out if the user tries to do a simulation
66*1707Szk194757 	 * write.
67*1707Szk194757 	 */
68*1707Szk194757 	if (simulation && (device_type == DVD_PLUS_W ||
69*1707Szk194757 	    device_type == DVD_PLUS)) {
70*1707Szk194757 		err_msg(gettext("Media does not support simulated writing.\n"));
71*1707Szk194757 		exit(1);
72*1707Szk194757 	}
73*1707Szk194757 
740Sstevel@tonic-gate 	write_init(TRACK_MODE_DATA);
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (image_file) {
770Sstevel@tonic-gate 		h = open_file_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) {
1030Sstevel@tonic-gate 		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)
1090Sstevel@tonic-gate 		    if (!build_track_info(target, -1, ti)) {
1100Sstevel@tonic-gate 			err_msg(
1110Sstevel@tonic-gate 			    gettext("Unable to find out writable address\n"));
1120Sstevel@tonic-gate 			exit(1);
1130Sstevel@tonic-gate 			}
1140Sstevel@tonic-gate 		if (use_media_stated_capacity) {
1150Sstevel@tonic-gate 			cap = get_last_possible_lba(target);
1160Sstevel@tonic-gate 			if (cap <= 0) {
1170Sstevel@tonic-gate 				cap = read_format_capacity(target->d_fd,
1180Sstevel@tonic-gate 				    &bsize);
1190Sstevel@tonic-gate 			}
1200Sstevel@tonic-gate 		} else {
1210Sstevel@tonic-gate 			/*
1220Sstevel@tonic-gate 			 * For DVD drives use read_format_capacity to retrieve
1230Sstevel@tonic-gate 			 * the media size, it could be 3.6, 3.9, 4.2, 4.7, 9.2
1240Sstevel@tonic-gate 			 */
1250Sstevel@tonic-gate 			if (device_type == CD_RW) {
1260Sstevel@tonic-gate 				cap = MAX_CD_BLKS;
1270Sstevel@tonic-gate 			} else {
1280Sstevel@tonic-gate 				/*
1290Sstevel@tonic-gate 				 * For DVD drives use read_format_capacity to
1300Sstevel@tonic-gate 				 * find media size, it can be 3.6, 3.9, 4.2,
1310Sstevel@tonic-gate 				 * 4.7, 9.2
1320Sstevel@tonic-gate 				 */
1330Sstevel@tonic-gate 				cap = read_format_capacity(target->d_fd,
1340Sstevel@tonic-gate 				    &bsize);
1350Sstevel@tonic-gate 				/* sanity if not reasonable default to 4.7 GB */
1360Sstevel@tonic-gate 				if (cap < MAX_CD_BLKS)
1370Sstevel@tonic-gate 					cap = MAX_DVD_BLKS;
1380Sstevel@tonic-gate 			}
1390Sstevel@tonic-gate 		}
1400Sstevel@tonic-gate 		if (cap == 0) {
1410Sstevel@tonic-gate 			err_msg(gettext("Unable to find out media capacity\n"));
1420Sstevel@tonic-gate 			exit(1);
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 		if (device_type == CD_RW)
1450Sstevel@tonic-gate 			cap = (cap + 1 - ti->ti_start_address) * 2048;
1460Sstevel@tonic-gate 		else
1470Sstevel@tonic-gate 			cap *= 2048 + 1;
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 		if (size > cap) {
1500Sstevel@tonic-gate 			err_msg(gettext("Size required (%lld bytes) is greater "
1510Sstevel@tonic-gate 			    "than available space (%lld bytes).\n"), size, cap);
1520Sstevel@tonic-gate 			exit(1);
1530Sstevel@tonic-gate 		}
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 		if (device_type == DVD_MINUS) {
1560Sstevel@tonic-gate 			(void) printf(gettext("Preparing to write DVD\n"));
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 			/* streamed file, we dont know the size to reserve */
1590Sstevel@tonic-gate 			if (no_size == 1) {
1600Sstevel@tonic-gate 				size = cap - 1;
1610Sstevel@tonic-gate 			}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 			/* DAO requires that we reserve the size to write */
1640Sstevel@tonic-gate 			if (debug)
1650Sstevel@tonic-gate 				(void) printf(
1660Sstevel@tonic-gate 				    "DAO_MODE:reserving track size of = 0x%x\n",
1670Sstevel@tonic-gate 				    (uint32_t)(size/2048));
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 			if (!set_reservation(target->d_fd, size/2048)) {
1700Sstevel@tonic-gate 				(void) printf(gettext(
1710Sstevel@tonic-gate 				    "Setting reservation failed\n"));
1720Sstevel@tonic-gate 				exit(1);
1730Sstevel@tonic-gate 			}
1740Sstevel@tonic-gate 		} else if (device_type == DVD_PLUS_W) {
1750Sstevel@tonic-gate 			/*
1760Sstevel@tonic-gate 			 * DVD+RW requires that we format the media before
1770Sstevel@tonic-gate 			 * writing.
1780Sstevel@tonic-gate 			 */
1790Sstevel@tonic-gate 			(void) print_n_flush(gettext("Formatting media..."));
1800Sstevel@tonic-gate 			if (!format_media(target->d_fd)) {
1810Sstevel@tonic-gate 				(void) printf(gettext(
1820Sstevel@tonic-gate 				    "Could not format media\n"));
1830Sstevel@tonic-gate 				exit(1);
1840Sstevel@tonic-gate 			} else {
1850Sstevel@tonic-gate 				int counter;
1860Sstevel@tonic-gate 				uchar_t *di;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 				/* poll until format is done */
1890Sstevel@tonic-gate 				di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
1900Sstevel@tonic-gate 				(void) sleep(10);
1910Sstevel@tonic-gate 				for (counter = 0; counter < 200; counter++) {
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 				ret = read_disc_info(target->d_fd, di);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 				if ((SENSE_KEY(rqbuf) == 2) &&
1960Sstevel@tonic-gate 				    (ASC(rqbuf) == 4)) {
1970Sstevel@tonic-gate 					(void) print_n_flush(".");
1980Sstevel@tonic-gate 					(void) sleep(5);
1990Sstevel@tonic-gate 				} else {
2000Sstevel@tonic-gate 					break;
2010Sstevel@tonic-gate 					}
2020Sstevel@tonic-gate 				}
2030Sstevel@tonic-gate 			}
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 			(void) printf(gettext("done\n"));
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		free(ti);
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	write_next_track(TRACK_MODE_DATA, h);
2130Sstevel@tonic-gate 
2140Sstevel@tonic-gate 	h->bstr_close(h);
2150Sstevel@tonic-gate 	write_fini();
2160Sstevel@tonic-gate 	fini_device(target);
2170Sstevel@tonic-gate 	exit(0);
2180Sstevel@tonic-gate }
219