xref: /onnv-gate/usr/src/cmd/cdrw/blank.c (revision 808:a214ec7576f6)
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
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*808Srameshc  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <stdlib.h>
310Sstevel@tonic-gate #include <string.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate #include <unistd.h>
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "msgs.h"
360Sstevel@tonic-gate #include "mmc.h"
370Sstevel@tonic-gate #include "util.h"
380Sstevel@tonic-gate #include "transport.h"
390Sstevel@tonic-gate #include "main.h"
400Sstevel@tonic-gate #include "misc_scsi.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate /*
430Sstevel@tonic-gate  * This is called recursively once, if an ALL blank succeeds but the
440Sstevel@tonic-gate  * media is not blank we call blank() again to perform a fast blank.
450Sstevel@tonic-gate  * This is a workaround for some drives such as older Toshiba DVD-RW
460Sstevel@tonic-gate  * which has this problem with ALL blanking.
470Sstevel@tonic-gate  */
480Sstevel@tonic-gate void
blank(void)490Sstevel@tonic-gate blank(void)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate 	int type, invalid;
520Sstevel@tonic-gate 	int count, ret;
530Sstevel@tonic-gate 	uchar_t *di, *buf;
540Sstevel@tonic-gate 	int immediate, err;
550Sstevel@tonic-gate 	int silent_pass = 0;
56*808Srameshc 	/*
57*808Srameshc 	 * silent_pass is set to 1 whenever we do not want to print
58*808Srameshc 	 * information messages. This is the case where blank() function
59*808Srameshc 	 * is called within the blank() function or the blank() function
60*808Srameshc 	 * is called from other functions within cdrw to blank the media
61*808Srameshc 	 * as part of other operations (clearing ghost TOC, closing the media
62*808Srameshc 	 * after a write operation, etc). In all those cases we need not print
63*808Srameshc 	 * or duplicate information messages. We should also return from the
64*808Srameshc 	 * blank() function to the calling function in those cases.
65*808Srameshc 	 */
66*808Srameshc 	int ignore_error = 0;
67*808Srameshc 	/*
68*808Srameshc 	 * ignore_error is set to 1 whenever we do not want to report any
69*808Srameshc 	 * error messages to the user and make things transparent to the
70*808Srameshc 	 * user (For eg: Clearing ghost TOC during write simulation).
71*808Srameshc 	 */
72*808Srameshc 
730Sstevel@tonic-gate 	invalid = 0;
740Sstevel@tonic-gate 	err = 0;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	(void) check_device(target, CHECK_TYPE_NOT_CDROM | CHECK_NO_MEDIA |
770Sstevel@tonic-gate 	    EXIT_IF_CHECK_FAILED);
780Sstevel@tonic-gate 	(void) check_device(target, CHECK_DEVICE_NOT_READY |
790Sstevel@tonic-gate 	    CHECK_DEVICE_NOT_WRITABLE | EXIT_IF_CHECK_FAILED);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (blanking_type == NULL) {
820Sstevel@tonic-gate 		invalid = 1;
830Sstevel@tonic-gate 	}
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	get_media_type(target->d_fd);
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	if (strcmp(blanking_type, "all") == 0) {
880Sstevel@tonic-gate 		/* erase the whole disk */
890Sstevel@tonic-gate 		type = ALL;
900Sstevel@tonic-gate 	} else if (strcmp(blanking_type, "session") == 0) {
910Sstevel@tonic-gate 		/* only erase the last session */
920Sstevel@tonic-gate 		type = SESSION;
930Sstevel@tonic-gate 	} else if (strcmp(blanking_type, "fast") == 0) {
940Sstevel@tonic-gate 		/* quick blank the TOC on the media */
950Sstevel@tonic-gate 		type = FAST;
960Sstevel@tonic-gate 	} else if (strcmp(blanking_type, "leadout") == 0) {
970Sstevel@tonic-gate 		/* erase the track tail to unclose the media */
980Sstevel@tonic-gate 		type = LEADOUT;
990Sstevel@tonic-gate 		silent_pass = 1;
1000Sstevel@tonic-gate 	} else if (strcmp(blanking_type, "clear") == 0) {
1010Sstevel@tonic-gate 		/*
1020Sstevel@tonic-gate 		 * used for drives where "all" blanking fails,
1030Sstevel@tonic-gate 		 * if it fails we follow up with a quick erase of TOC.
1040Sstevel@tonic-gate 		 * This is only called from within this function to do
1050Sstevel@tonic-gate 		 * a second blanking pass.
1060Sstevel@tonic-gate 		 */
1070Sstevel@tonic-gate 		type = CLEAR;
1080Sstevel@tonic-gate 		silent_pass = 1;
109*808Srameshc 	} else if (strcmp(blanking_type, "clear_ghost") == 0) {
110*808Srameshc 		/*
111*808Srameshc 		 * used for drives in simulation mode to blank ghost
112*808Srameshc 		 * TOC after simulation write is complete.
113*808Srameshc 		 */
114*808Srameshc 		type = CLEAR;
115*808Srameshc 		silent_pass = 1;
116*808Srameshc 		ignore_error = 1;
1170Sstevel@tonic-gate 	} else {
1180Sstevel@tonic-gate 		/* invalid blank type was passed on the command line */
1190Sstevel@tonic-gate 		invalid = 1;
1200Sstevel@tonic-gate 	}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (invalid) {
1230Sstevel@tonic-gate 		err_msg(gettext("Invalid blanking type specified\n"));
1240Sstevel@tonic-gate 		exit(1);
1250Sstevel@tonic-gate 	}
126*808Srameshc 
127*808Srameshc 	/*
128*808Srameshc 	 * many DVD+RW drives do not allow blanking the media, it is also
129*808Srameshc 	 * not included in the spec, we would just reformat the media prior
130*808Srameshc 	 * to writing. This is not the equivelent to blanking as the media
131*808Srameshc 	 * contains a TOC when formatted.
132*808Srameshc 	 */
133*808Srameshc 	if (device_type == DVD_PLUS_W) {
134*808Srameshc 		if (ignore_error)
135*808Srameshc 			return;
136*808Srameshc 		err_msg(gettext("Blanking cannot be done on DVD+RW media\n"));
137*808Srameshc 		exit(1);
138*808Srameshc 	}
139*808Srameshc 
1400Sstevel@tonic-gate 	if ((target->d_inq[2] & 7) != 0) {
1410Sstevel@tonic-gate 		/* SCSI device */
1420Sstevel@tonic-gate 		immediate = 0;
1430Sstevel@tonic-gate 	} else {
1440Sstevel@tonic-gate 		/* non-SCSI (e.g ATAPI) device */
1450Sstevel@tonic-gate 		immediate = 1;
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/* we are doing a second pass. We don't want to re-print messsages */
1490Sstevel@tonic-gate 	if (!silent_pass)
1500Sstevel@tonic-gate 		print_n_flush(gettext("Initializing device..."));
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* Make sure that test write is off */
1530Sstevel@tonic-gate 	buf = (uchar_t *)my_zalloc(64);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/* get mode page for test writing if it fails we cannot turn it off */
1560Sstevel@tonic-gate 	if (!get_mode_page(target->d_fd, 5, 0, 64, buf)) {
157*808Srameshc 		if (ignore_error)
158*808Srameshc 			return;
1590Sstevel@tonic-gate 		err_msg(gettext("Device not supported\n"));
1600Sstevel@tonic-gate 		exit(1);
1610Sstevel@tonic-gate 	}
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	buf[2] &= 0xef;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 	/* turn laser on */
1660Sstevel@tonic-gate 	if (!set_mode_page(target->d_fd, buf)) {
167*808Srameshc 		if (ignore_error)
168*808Srameshc 			return;
1690Sstevel@tonic-gate 		err_msg(gettext("Unable to configure device\n"));
1700Sstevel@tonic-gate 		exit(1);
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	free(buf);
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	/* we are doing a second pass. We don't want to re-print messsages */
1750Sstevel@tonic-gate 	if (!silent_pass) {
1760Sstevel@tonic-gate 		/* l10n_NOTE : 'done' as in "Initializing device...done"  */
1770Sstevel@tonic-gate 		(void) printf(gettext("done.\n"));
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 		print_n_flush(gettext(
1800Sstevel@tonic-gate 		    "Blanking the media (Can take several minutes)..."));
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	if (!blank_disc(target->d_fd, type, immediate)) {
183*808Srameshc 		if (ignore_error)
184*808Srameshc 			return;
1850Sstevel@tonic-gate 		err_msg(gettext("Blank command failed\n"));
1860Sstevel@tonic-gate 		if (debug)
1870Sstevel@tonic-gate 			(void) printf("%x %x %x %x\n", uscsi_status,
1880Sstevel@tonic-gate 			    SENSE_KEY(rqbuf), ASC(rqbuf), ASCQ(rqbuf));
1890Sstevel@tonic-gate 		goto blank_failed;
1900Sstevel@tonic-gate 	}
1910Sstevel@tonic-gate 	/* Allow the blanking to start */
1920Sstevel@tonic-gate 	(void) sleep(10);
1930Sstevel@tonic-gate 
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 * set ATAPI devices to immediately return from the command and poll
1960Sstevel@tonic-gate 	 * so that we don't hog the channel.
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (immediate) {
2000Sstevel@tonic-gate 		di = (uchar_t *)my_zalloc(DISC_INFO_BLOCK_SIZE);
2010Sstevel@tonic-gate 		/* Blanking should not take more than 75 minutes */
2020Sstevel@tonic-gate 		for (count = 0; count < (16*60); count++) {
2030Sstevel@tonic-gate 			ret = read_disc_info(target->d_fd, di);
2040Sstevel@tonic-gate 			if (ret != 0)
2050Sstevel@tonic-gate 				break;
2060Sstevel@tonic-gate 			if (uscsi_status != 2)
2070Sstevel@tonic-gate 				err = 1;
2080Sstevel@tonic-gate 			/* not ready but not becoming ready */
2090Sstevel@tonic-gate 			if (SENSE_KEY(rqbuf) == 2) {
2100Sstevel@tonic-gate 				if (ASC(rqbuf) != 4)
2110Sstevel@tonic-gate 					err = 1;
2120Sstevel@tonic-gate 			/* illegal mode for this track */
2130Sstevel@tonic-gate 			} else if (SENSE_KEY(rqbuf) == 5) {
2140Sstevel@tonic-gate 				if (ASC(rqbuf) != 0x64)
2150Sstevel@tonic-gate 					err = 1;
2160Sstevel@tonic-gate 			} else {
2170Sstevel@tonic-gate 				err = 1;
2180Sstevel@tonic-gate 			}
2190Sstevel@tonic-gate 			if (err == 1) {
220*808Srameshc 				if (ignore_error)
221*808Srameshc 					break;
2220Sstevel@tonic-gate 				err_msg(gettext("Blanking operation failed\n"));
2230Sstevel@tonic-gate 				if (debug) {
2240Sstevel@tonic-gate 					(void) printf("%x %x %x %x\n",
2250Sstevel@tonic-gate 					    uscsi_status, SENSE_KEY(rqbuf),
2260Sstevel@tonic-gate 					    ASC(rqbuf), ASCQ(rqbuf));
2270Sstevel@tonic-gate 				}
2280Sstevel@tonic-gate 				free(di);
2290Sstevel@tonic-gate 				goto blank_failed;
2300Sstevel@tonic-gate 			}
2310Sstevel@tonic-gate 			(void) sleep(5);
2320Sstevel@tonic-gate 		}
2330Sstevel@tonic-gate 		free(di);
2340Sstevel@tonic-gate 		if (count == (16*60)) {
235*808Srameshc 			if (!silent_pass) {
236*808Srameshc 				(void) printf(gettext(
237*808Srameshc 				    "Blank command timed out.\n"));
238*808Srameshc 			}
2390Sstevel@tonic-gate 			goto blank_failed;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 	/* we are doing a second pass. We don't want to re-print messsages */
2430Sstevel@tonic-gate 	if (!silent_pass) {
2440Sstevel@tonic-gate 		/* l10n_NOTE : 'done' as in "Erasing track 1...done"  */
2450Sstevel@tonic-gate 		(void) printf(gettext("done.\n"));
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	/*
2490Sstevel@tonic-gate 	 * some cruft left from all blanking, this has been seen on some
2500Sstevel@tonic-gate 	 * newer drives including Toshiba SD-6112 DVD-RW and Sony 510A.
2510Sstevel@tonic-gate 	 * we will do a second pass with a recursive call to blank the
2520Sstevel@tonic-gate 	 * lead-in.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	if (type == ALL) {
255*808Srameshc 		if (check_device(target,  CHECK_MEDIA_IS_NOT_BLANK)) {
2560Sstevel@tonic-gate 			blanking_type = "clear";
2570Sstevel@tonic-gate 			blank();
258*808Srameshc 			if (silent_pass)
259*808Srameshc 				return;
2600Sstevel@tonic-gate 			exit(0);
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	/*
2650Sstevel@tonic-gate 	 * We erased part of the leadout for the media to unclose
2660Sstevel@tonic-gate 	 * the disk, we still need to generate an appendable leadout
2670Sstevel@tonic-gate 	 * so that the next track can be written. so do not eject or exit.
2680Sstevel@tonic-gate 	 */
2690Sstevel@tonic-gate 	if (silent_pass)
2700Sstevel@tonic-gate 		return;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	if (vol_running)
2730Sstevel@tonic-gate 		(void) eject_media(target);
2740Sstevel@tonic-gate 	exit(0);
2750Sstevel@tonic-gate blank_failed:
2760Sstevel@tonic-gate 	if ((type != ALL) && !silent_pass) {
2770Sstevel@tonic-gate 		(void) printf("Try using blanking type 'all'\n");
2780Sstevel@tonic-gate 	}
279*808Srameshc 	if (silent_pass)
280*808Srameshc 		return;
2810Sstevel@tonic-gate 	if (vol_running)
2820Sstevel@tonic-gate 		(void) eject_media(target);
2830Sstevel@tonic-gate 	exit(1);
2840Sstevel@tonic-gate }
285