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