xref: /illumos-gate/usr/src/cmd/cdrw/write_image.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51b01eaa0Szk  * Common Development and Distribution License (the "License").
61b01eaa0Szk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4df0f9a3Sec  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <stdlib.h>
287c478bd9Sstevel@tonic-gate #include <libintl.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include "trackio.h"
317c478bd9Sstevel@tonic-gate #include "main.h"
327c478bd9Sstevel@tonic-gate #include "util.h"
337c478bd9Sstevel@tonic-gate #include "bstream.h"
347c478bd9Sstevel@tonic-gate #include "misc_scsi.h"
357c478bd9Sstevel@tonic-gate #include "msgs.h"
367c478bd9Sstevel@tonic-gate #include "device.h"
377c478bd9Sstevel@tonic-gate #include "mmc.h"
387c478bd9Sstevel@tonic-gate #include "transport.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate void
write_image(void)417c478bd9Sstevel@tonic-gate write_image(void)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	bstreamhandle h;
447c478bd9Sstevel@tonic-gate 	off_t size;
457c478bd9Sstevel@tonic-gate 	int no_size, ret;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	get_media_type(target->d_fd);
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate 	/* DVD+RW does not have blanking and can be overwritten */
507c478bd9Sstevel@tonic-gate 	if (device_type != DVD_PLUS_W) {
517c478bd9Sstevel@tonic-gate 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
527c478bd9Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE | CHECK_MEDIA_IS_NOT_WRITABLE |
537c478bd9Sstevel@tonic-gate 	    EXIT_IF_CHECK_FAILED);
547c478bd9Sstevel@tonic-gate 	} else {
557c478bd9Sstevel@tonic-gate 		(void) check_device(target, CHECK_DEVICE_NOT_READY |
564d218355Szk 		    EXIT_IF_CHECK_FAILED);
577c478bd9Sstevel@tonic-gate 	}
587c478bd9Sstevel@tonic-gate 
591b01eaa0Szk 	/*
601b01eaa0Szk 	 * Simulation writing can't happen on DVD+RW's
611b01eaa0Szk 	 * or DVD+R's. According to the MMC spec this
621b01eaa0Szk 	 * operation is not supported. So we should
631b01eaa0Szk 	 * bail out if the user tries to do a simulation
641b01eaa0Szk 	 * write.
651b01eaa0Szk 	 */
661b01eaa0Szk 	if (simulation && (device_type == DVD_PLUS_W ||
671b01eaa0Szk 	    device_type == DVD_PLUS)) {
681b01eaa0Szk 		err_msg(gettext("Media does not support simulated writing.\n"));
691b01eaa0Szk 		exit(1);
701b01eaa0Szk 	}
711b01eaa0Szk 
727c478bd9Sstevel@tonic-gate 	write_init(TRACK_MODE_DATA);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if (image_file) {
7529c5e6ceSzk 		h = open_iso_read_stream(image_file);
767c478bd9Sstevel@tonic-gate 	} else {
777c478bd9Sstevel@tonic-gate 		h = open_stdin_read_stream();
787c478bd9Sstevel@tonic-gate 	}
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	if (h == NULL) {
817c478bd9Sstevel@tonic-gate 		err_msg(gettext("Cannot open %s: %s\n"),
827c478bd9Sstevel@tonic-gate 		    image_file ? image_file : "stdin", get_err_str());
837c478bd9Sstevel@tonic-gate 		exit(1);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	no_size = 0;
867c478bd9Sstevel@tonic-gate 	ret = h->bstr_size(h, &size);
877c478bd9Sstevel@tonic-gate 	if (ret == 0) {
887c478bd9Sstevel@tonic-gate 		if ((str_errno == STR_ERR_NO_REG_FILE)) {
897c478bd9Sstevel@tonic-gate 			no_size = 1;
907c478bd9Sstevel@tonic-gate 		} else {
917c478bd9Sstevel@tonic-gate 			err_msg(gettext("Cannot stat input file: %s\n"),
927c478bd9Sstevel@tonic-gate 			    get_err_str());
937c478bd9Sstevel@tonic-gate 			exit(1);
947c478bd9Sstevel@tonic-gate 		}
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 	if ((no_size == 0) && (size == 0)) {
977c478bd9Sstevel@tonic-gate 		err_msg(gettext("Input size(0) not valid\n"));
987c478bd9Sstevel@tonic-gate 		exit(1);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 	if (no_size == 0) {
10173e1749cSzk 		off_t cap;
1027c478bd9Sstevel@tonic-gate 		struct track_info *ti;
1037c478bd9Sstevel@tonic-gate 		uint_t bsize;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		ti = (struct track_info *)my_zalloc(sizeof (*ti));
1067c478bd9Sstevel@tonic-gate 		if (write_mode == TAO_MODE)
1074d218355Szk 			if (!build_track_info(target, -1, ti)) {
1084d218355Szk 				err_msg(
1094d218355Szk 				    gettext("Unable to find out writable "
1104d218355Szk 				    "address\n"));
1114d218355Szk 				exit(1);
1127c478bd9Sstevel@tonic-gate 			}
113*4df0f9a3Sec 		if (device_type == CD_RW) {
114*4df0f9a3Sec 			if ((cap = get_last_possible_lba(target)) <= 0) {
115*4df0f9a3Sec 				if ((cap = read_format_capacity(target->d_fd,
116*4df0f9a3Sec 				    &bsize)) <= 0) {
117*4df0f9a3Sec 					err_msg(gettext("Unable to determine "
118*4df0f9a3Sec 					    "media capacity.  Defaulting to "
119*4df0f9a3Sec 					    "650 MB (74 minute) disc.\n"));
120*4df0f9a3Sec 					cap = MAX_CD_BLKS;
121*4df0f9a3Sec 				}
1227c478bd9Sstevel@tonic-gate 			}
1237c478bd9Sstevel@tonic-gate 		} else {
1247c478bd9Sstevel@tonic-gate 			/*
125*4df0f9a3Sec 			 * For DVD drives use read_format_capacity to
126*4df0f9a3Sec 			 * find media size, it can be 3.6, 3.9, 4.2,
127*4df0f9a3Sec 			 * 4.7, 9.2
1287c478bd9Sstevel@tonic-gate 			 */
129*4df0f9a3Sec 			cap = read_format_capacity(target->d_fd,
130*4df0f9a3Sec 			    &bsize);
131*4df0f9a3Sec 			/*
132*4df0f9a3Sec 			 * Sanity check; Default to 4.7 GB if cap unreasonable
133*4df0f9a3Sec 			 */
134*4df0f9a3Sec 			if (cap < MAX_CD_BLKS)
135*4df0f9a3Sec 				cap = MAX_DVD_BLKS;
1367c478bd9Sstevel@tonic-gate 		}
137*4df0f9a3Sec 
1387c478bd9Sstevel@tonic-gate 		if (device_type == CD_RW)
1397c478bd9Sstevel@tonic-gate 			cap = (cap + 1 - ti->ti_start_address) * 2048;
1407c478bd9Sstevel@tonic-gate 		else
1417c478bd9Sstevel@tonic-gate 			cap *= 2048 + 1;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		if (size > cap) {
1447c478bd9Sstevel@tonic-gate 			err_msg(gettext("Size required (%lld bytes) is greater "
1457c478bd9Sstevel@tonic-gate 			    "than available space (%lld bytes).\n"), size, cap);
1467c478bd9Sstevel@tonic-gate 			exit(1);
1477c478bd9Sstevel@tonic-gate 		}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		if (device_type == DVD_MINUS) {
1507c478bd9Sstevel@tonic-gate 			(void) printf(gettext("Preparing to write DVD\n"));
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 			/* streamed file, we dont know the size to reserve */
1537c478bd9Sstevel@tonic-gate 			if (no_size == 1) {
1547c478bd9Sstevel@tonic-gate 				size = cap - 1;
1557c478bd9Sstevel@tonic-gate 			}
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 			/* DAO requires that we reserve the size to write */
1587c478bd9Sstevel@tonic-gate 			if (debug)
1597c478bd9Sstevel@tonic-gate 				(void) printf(
1607c478bd9Sstevel@tonic-gate 				    "DAO_MODE:reserving track size of = 0x%x\n",
1617c478bd9Sstevel@tonic-gate 				    (uint32_t)(size/2048));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 			if (!set_reservation(target->d_fd, size/2048)) {
1647c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
1657c478bd9Sstevel@tonic-gate 				    "Setting reservation failed\n"));
1667c478bd9Sstevel@tonic-gate 				exit(1);
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 		} else if (device_type == DVD_PLUS_W) {
1697c478bd9Sstevel@tonic-gate 			/*
1707c478bd9Sstevel@tonic-gate 			 * DVD+RW requires that we format the media before
1717c478bd9Sstevel@tonic-gate 			 * writing.
1727c478bd9Sstevel@tonic-gate 			 */
1737c478bd9Sstevel@tonic-gate 			(void) print_n_flush(gettext("Formatting media..."));
1747c478bd9Sstevel@tonic-gate 			if (!format_media(target->d_fd)) {
1757c478bd9Sstevel@tonic-gate 				(void) printf(gettext(
1767c478bd9Sstevel@tonic-gate 				    "Could not format media\n"));
1777c478bd9Sstevel@tonic-gate 				exit(1);
1787c478bd9Sstevel@tonic-gate 			} else {
1797c478bd9Sstevel@tonic-gate 				int counter;
1807c478bd9Sstevel@tonic-gate 				uchar_t *di;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 				/* poll until format is done */
1837c478bd9Sstevel@tonic-gate 				di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
1847c478bd9Sstevel@tonic-gate 				(void) sleep(10);
1857c478bd9Sstevel@tonic-gate 				for (counter = 0; counter < 200; counter++) {
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 				ret = read_disc_info(target->d_fd, di);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 				if ((SENSE_KEY(rqbuf) == 2) &&
1907c478bd9Sstevel@tonic-gate 				    (ASC(rqbuf) == 4)) {
1917c478bd9Sstevel@tonic-gate 					(void) print_n_flush(".");
1927c478bd9Sstevel@tonic-gate 					(void) sleep(5);
1937c478bd9Sstevel@tonic-gate 				} else {
1947c478bd9Sstevel@tonic-gate 					break;
1957c478bd9Sstevel@tonic-gate 					}
1967c478bd9Sstevel@tonic-gate 				}
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 			(void) printf(gettext("done\n"));
2007c478bd9Sstevel@tonic-gate 		}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		free(ti);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	write_next_track(TRACK_MODE_DATA, h);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	h->bstr_close(h);
2097c478bd9Sstevel@tonic-gate 	write_fini();
2107c478bd9Sstevel@tonic-gate 	fini_device(target);
2117c478bd9Sstevel@tonic-gate 	exit(0);
2127c478bd9Sstevel@tonic-gate }
213