3252 Need a proper flock() implementation
[unleashed.git] / usr / src / cmd / cdrw / write_image.c
blobe41aa90612b255e8d2d01dd4fef1ce5999a14a4d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
28 #include <sys/types.h>
29 #include <stdlib.h>
30 #include <libintl.h>
31 #include <unistd.h>
32 #include "trackio.h"
33 #include "main.h"
34 #include "util.h"
35 #include "bstream.h"
36 #include "misc_scsi.h"
37 #include "msgs.h"
38 #include "device.h"
39 #include "mmc.h"
40 #include "transport.h"
42 void
43 write_image(void)
45 bstreamhandle h;
46 off_t size;
47 int no_size, ret;
49 get_media_type(target->d_fd);
51 /* DVD+RW does not have blanking and can be overwritten */
52 if (device_type != DVD_PLUS_W) {
53 (void) check_device(target, CHECK_DEVICE_NOT_READY |
54 CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
55 EXIT_IF_CHECK_FAILED);
56 } else {
57 (void) check_device(target, CHECK_DEVICE_NOT_READY |
58 EXIT_IF_CHECK_FAILED);
62 * Simulation writing can't happen on DVD+RW's
63 * or DVD+R's. According to the MMC spec this
64 * operation is not supported. So we should
65 * bail out if the user tries to do a simulation
66 * write.
68 if (simulation && (device_type == DVD_PLUS_W ||
69 device_type == DVD_PLUS)) {
70 err_msg(gettext("Media does not support simulated writing.\n"));
71 exit(1);
74 write_init(TRACK_MODE_DATA);
76 if (image_file) {
77 h = open_iso_read_stream(image_file);
78 } else {
79 h = open_stdin_read_stream();
82 if (h == NULL) {
83 err_msg(gettext("Cannot open %s: %s\n"),
84 image_file ? image_file : "stdin", get_err_str());
85 exit(1);
87 no_size = 0;
88 ret = h->bstr_size(h, &size);
89 if (ret == 0) {
90 if ((str_errno == STR_ERR_NO_REG_FILE)) {
91 no_size = 1;
92 } else {
93 err_msg(gettext("Cannot stat input file: %s\n"),
94 get_err_str());
95 exit(1);
98 if ((no_size == 0) && (size == 0)) {
99 err_msg(gettext("Input size(0) not valid\n"));
100 exit(1);
102 if (no_size == 0) {
103 off_t cap;
104 struct track_info *ti;
105 uint_t bsize;
107 ti = (struct track_info *)my_zalloc(sizeof (*ti));
108 if (write_mode == TAO_MODE)
109 if (!build_track_info(target, -1, ti)) {
110 err_msg(
111 gettext("Unable to find out writable "
112 "address\n"));
113 exit(1);
115 if (device_type == CD_RW) {
116 if ((cap = get_last_possible_lba(target)) <= 0) {
117 if ((cap = read_format_capacity(target->d_fd,
118 &bsize)) <= 0) {
119 err_msg(gettext("Unable to determine "
120 "media capacity. Defaulting to "
121 "650 MB (74 minute) disc.\n"));
122 cap = MAX_CD_BLKS;
125 } else {
127 * For DVD drives use read_format_capacity to
128 * find media size, it can be 3.6, 3.9, 4.2,
129 * 4.7, 9.2
131 cap = read_format_capacity(target->d_fd,
132 &bsize);
134 * Sanity check; Default to 4.7 GB if cap unreasonable
136 if (cap < MAX_CD_BLKS)
137 cap = MAX_DVD_BLKS;
140 if (device_type == CD_RW)
141 cap = (cap + 1 - ti->ti_start_address) * 2048;
142 else
143 cap *= 2048 + 1;
145 if (size > cap) {
146 err_msg(gettext("Size required (%lld bytes) is greater "
147 "than available space (%lld bytes).\n"), size, cap);
148 exit(1);
151 if (device_type == DVD_MINUS) {
152 (void) printf(gettext("Preparing to write DVD\n"));
154 /* streamed file, we dont know the size to reserve */
155 if (no_size == 1) {
156 size = cap - 1;
159 /* DAO requires that we reserve the size to write */
160 if (debug)
161 (void) printf(
162 "DAO_MODE:reserving track size of = 0x%x\n",
163 (uint32_t)(size/2048));
165 if (!set_reservation(target->d_fd, size/2048)) {
166 (void) printf(gettext(
167 "Setting reservation failed\n"));
168 exit(1);
170 } else if (device_type == DVD_PLUS_W) {
172 * DVD+RW requires that we format the media before
173 * writing.
175 (void) print_n_flush(gettext("Formatting media..."));
176 if (!format_media(target->d_fd)) {
177 (void) printf(gettext(
178 "Could not format media\n"));
179 exit(1);
180 } else {
181 int counter;
182 uchar_t *di;
184 /* poll until format is done */
185 di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
186 (void) sleep(10);
187 for (counter = 0; counter < 200; counter++) {
189 ret = read_disc_info(target->d_fd, di);
191 if ((SENSE_KEY(rqbuf) == 2) &&
192 (ASC(rqbuf) == 4)) {
193 (void) print_n_flush(".");
194 (void) sleep(5);
195 } else {
196 break;
201 (void) printf(gettext("done\n"));
205 free(ti);
208 write_next_track(TRACK_MODE_DATA, h);
210 h->bstr_close(h);
211 write_fini();
212 fini_device(target);
213 exit(0);