1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <libintl.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include "trackio.h"
34*0Sstevel@tonic-gate #include "main.h"
35*0Sstevel@tonic-gate #include "util.h"
36*0Sstevel@tonic-gate #include "bstream.h"
37*0Sstevel@tonic-gate #include "misc_scsi.h"
38*0Sstevel@tonic-gate #include "msgs.h"
39*0Sstevel@tonic-gate #include "device.h"
40*0Sstevel@tonic-gate #include "mmc.h"
41*0Sstevel@tonic-gate #include "transport.h"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate void
44*0Sstevel@tonic-gate write_image(void)
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate 	bstreamhandle h;
47*0Sstevel@tonic-gate 	off_t size;
48*0Sstevel@tonic-gate 	int no_size, ret;
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate 	get_media_type(target->d_fd);
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate 	/* DVD+RW does not have blanking and can be overwritten */
53*0Sstevel@tonic-gate 	if (device_type != DVD_PLUS_W) {
54*0Sstevel@tonic-gate 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
55*0Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
56*0Sstevel@tonic-gate 	    EXIT_IF_CHECK_FAILED);
57*0Sstevel@tonic-gate 	} else {
58*0Sstevel@tonic-gate 		(void) check_device(target, CHECK_DEVICE_NOT_READY |
59*0Sstevel@tonic-gate 		EXIT_IF_CHECK_FAILED);
60*0Sstevel@tonic-gate 	}
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate 	write_init(TRACK_MODE_DATA);
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate 	if (image_file) {
65*0Sstevel@tonic-gate 		h = open_file_read_stream(image_file);
66*0Sstevel@tonic-gate 	} else {
67*0Sstevel@tonic-gate 		h = open_stdin_read_stream();
68*0Sstevel@tonic-gate 	}
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if (h == NULL) {
71*0Sstevel@tonic-gate 		err_msg(gettext("Cannot open %s: %s\n"),
72*0Sstevel@tonic-gate 		    image_file ? image_file : "stdin", get_err_str());
73*0Sstevel@tonic-gate 		exit(1);
74*0Sstevel@tonic-gate 	}
75*0Sstevel@tonic-gate 	no_size = 0;
76*0Sstevel@tonic-gate 	ret = h->bstr_size(h, &size);
77*0Sstevel@tonic-gate 	if (ret == 0) {
78*0Sstevel@tonic-gate 		if ((str_errno == STR_ERR_NO_REG_FILE)) {
79*0Sstevel@tonic-gate 			no_size = 1;
80*0Sstevel@tonic-gate 		} else {
81*0Sstevel@tonic-gate 			err_msg(gettext("Cannot stat input file: %s\n"),
82*0Sstevel@tonic-gate 			    get_err_str());
83*0Sstevel@tonic-gate 			exit(1);
84*0Sstevel@tonic-gate 		}
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 	if ((no_size == 0) && (size == 0)) {
87*0Sstevel@tonic-gate 		err_msg(gettext("Input size(0) not valid\n"));
88*0Sstevel@tonic-gate 		exit(1);
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	if (no_size == 0) {
91*0Sstevel@tonic-gate 		off_t cap;
92*0Sstevel@tonic-gate 		struct track_info *ti;
93*0Sstevel@tonic-gate 		uint_t bsize;
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 		ti = (struct track_info *)my_zalloc(sizeof (*ti));
96*0Sstevel@tonic-gate 		if (write_mode == TAO_MODE)
97*0Sstevel@tonic-gate 		    if (!build_track_info(target, -1, ti)) {
98*0Sstevel@tonic-gate 			err_msg(
99*0Sstevel@tonic-gate 			    gettext("Unable to find out writable address\n"));
100*0Sstevel@tonic-gate 			exit(1);
101*0Sstevel@tonic-gate 			}
102*0Sstevel@tonic-gate 		if (use_media_stated_capacity) {
103*0Sstevel@tonic-gate 			cap = get_last_possible_lba(target);
104*0Sstevel@tonic-gate 			if (cap <= 0) {
105*0Sstevel@tonic-gate 				cap = read_format_capacity(target->d_fd,
106*0Sstevel@tonic-gate 				    &bsize);
107*0Sstevel@tonic-gate 			}
108*0Sstevel@tonic-gate 		} else {
109*0Sstevel@tonic-gate 			/*
110*0Sstevel@tonic-gate 			 * For DVD drives use read_format_capacity to retrieve
111*0Sstevel@tonic-gate 			 * the media size, it could be 3.6, 3.9, 4.2, 4.7, 9.2
112*0Sstevel@tonic-gate 			 */
113*0Sstevel@tonic-gate 			if (device_type == CD_RW) {
114*0Sstevel@tonic-gate 				cap = MAX_CD_BLKS;
115*0Sstevel@tonic-gate 			} else {
116*0Sstevel@tonic-gate 				/*
117*0Sstevel@tonic-gate 				 * For DVD drives use read_format_capacity to
118*0Sstevel@tonic-gate 				 * find media size, it can be 3.6, 3.9, 4.2,
119*0Sstevel@tonic-gate 				 * 4.7, 9.2
120*0Sstevel@tonic-gate 				 */
121*0Sstevel@tonic-gate 				cap = read_format_capacity(target->d_fd,
122*0Sstevel@tonic-gate 				    &bsize);
123*0Sstevel@tonic-gate 				/* sanity if not reasonable default to 4.7 GB */
124*0Sstevel@tonic-gate 				if (cap < MAX_CD_BLKS)
125*0Sstevel@tonic-gate 					cap = MAX_DVD_BLKS;
126*0Sstevel@tonic-gate 			}
127*0Sstevel@tonic-gate 		}
128*0Sstevel@tonic-gate 		if (cap == 0) {
129*0Sstevel@tonic-gate 			err_msg(gettext("Unable to find out media capacity\n"));
130*0Sstevel@tonic-gate 			exit(1);
131*0Sstevel@tonic-gate 		}
132*0Sstevel@tonic-gate 		if (device_type == CD_RW)
133*0Sstevel@tonic-gate 			cap = (cap + 1 - ti->ti_start_address) * 2048;
134*0Sstevel@tonic-gate 		else
135*0Sstevel@tonic-gate 			cap *= 2048 + 1;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		if (size > cap) {
138*0Sstevel@tonic-gate 			err_msg(gettext("Size required (%lld bytes) is greater "
139*0Sstevel@tonic-gate 			    "than available space (%lld bytes).\n"), size, cap);
140*0Sstevel@tonic-gate 			exit(1);
141*0Sstevel@tonic-gate 		}
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 		if (device_type == DVD_MINUS) {
144*0Sstevel@tonic-gate 			(void) printf(gettext("Preparing to write DVD\n"));
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 			/* streamed file, we dont know the size to reserve */
147*0Sstevel@tonic-gate 			if (no_size == 1) {
148*0Sstevel@tonic-gate 				size = cap - 1;
149*0Sstevel@tonic-gate 			}
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 			/* DAO requires that we reserve the size to write */
152*0Sstevel@tonic-gate 			if (debug)
153*0Sstevel@tonic-gate 				(void) printf(
154*0Sstevel@tonic-gate 				    "DAO_MODE:reserving track size of = 0x%x\n",
155*0Sstevel@tonic-gate 				    (uint32_t)(size/2048));
156*0Sstevel@tonic-gate 
157*0Sstevel@tonic-gate 			if (!set_reservation(target->d_fd, size/2048)) {
158*0Sstevel@tonic-gate 				(void) printf(gettext(
159*0Sstevel@tonic-gate 				    "Setting reservation failed\n"));
160*0Sstevel@tonic-gate 				exit(1);
161*0Sstevel@tonic-gate 			}
162*0Sstevel@tonic-gate 		} else if (device_type == DVD_PLUS_W) {
163*0Sstevel@tonic-gate 			/*
164*0Sstevel@tonic-gate 			 * DVD+RW requires that we format the media before
165*0Sstevel@tonic-gate 			 * writing.
166*0Sstevel@tonic-gate 			 */
167*0Sstevel@tonic-gate 			(void) print_n_flush(gettext("Formatting media..."));
168*0Sstevel@tonic-gate 			if (!format_media(target->d_fd)) {
169*0Sstevel@tonic-gate 				(void) printf(gettext(
170*0Sstevel@tonic-gate 				    "Could not format media\n"));
171*0Sstevel@tonic-gate 				exit(1);
172*0Sstevel@tonic-gate 			} else {
173*0Sstevel@tonic-gate 				int counter;
174*0Sstevel@tonic-gate 				uchar_t *di;
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 				/* poll until format is done */
177*0Sstevel@tonic-gate 				di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
178*0Sstevel@tonic-gate 				(void) sleep(10);
179*0Sstevel@tonic-gate 				for (counter = 0; counter < 200; counter++) {
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 				ret = read_disc_info(target->d_fd, di);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 				if ((SENSE_KEY(rqbuf) == 2) &&
184*0Sstevel@tonic-gate 				    (ASC(rqbuf) == 4)) {
185*0Sstevel@tonic-gate 					(void) print_n_flush(".");
186*0Sstevel@tonic-gate 					(void) sleep(5);
187*0Sstevel@tonic-gate 				} else {
188*0Sstevel@tonic-gate 					break;
189*0Sstevel@tonic-gate 					}
190*0Sstevel@tonic-gate 				}
191*0Sstevel@tonic-gate 			}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 			(void) printf(gettext("done\n"));
194*0Sstevel@tonic-gate 		}
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 		free(ti);
198*0Sstevel@tonic-gate 	}
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate 	write_next_track(TRACK_MODE_DATA, h);
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate 	h->bstr_close(h);
203*0Sstevel@tonic-gate 	write_fini();
204*0Sstevel@tonic-gate 	fini_device(target);
205*0Sstevel@tonic-gate 	exit(0);
206*0Sstevel@tonic-gate }
207