1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12 
13 /* @(#)scsi.c	1.20 05/05/01 Copyright 1997 J. Schilling */
14 /*
15  *	Copyright (c) 1997 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31 
32 #ifdef	USE_SCG
33 #include <mconfig.h>
34 
35 #include <stdio.h>
36 #include <standard.h>
37 #include <stdxlib.h>
38 #include <unixstd.h>
39 #include <schily.h>
40 
41 #include "genisoimage.h"
42 #include <usal/scsireg.h>
43 #include <usal/scsitransp.h>
44 
45 #include "wodim.h"
46 #include "../wodim/defaults.h"
47 
48 /*
49  * NOTICE:	You should not make BUF_SIZE more than
50  *		the buffer size of the CD-Recorder.
51  *
52  * Do not set BUF_SIZE to be more than 126 KBytes
53  * if you are running cdrecord on a sun4c machine.
54  *
55  * WARNING:	Philips CDD 521 dies if BUF_SIZE is to big.
56  */
57 #define	BUF_SIZE	(62*1024)	/* Must be a multiple of 2048	   */
58 
59 static	SCSI	*usalp;
60 static	long	bufsize;		/* The size of the transfer buffer */
61 
62 int	readsecs(int startsecno, void *buffer, int sectorcount);
63 int	scsidev_open(char *path);
64 int	scsidev_close(void);
65 
66 int
readsecs(int startsecno,void * buffer,int sectorcount)67 readsecs(int startsecno, void *buffer, int sectorcount)
68 {
69 	int	f;
70 	int	secsize;	/* The drive's SCSI sector size		*/
71 	long	amount;		/* The number of bytes to be transfered	*/
72 	long	secno;		/* The sector number to read from	*/
73 	long	secnum;		/* The number of sectors to read	*/
74 	char	*bp;
75 	long	amt;
76 
77 	if (in_image == NULL) {
78 		/*
79 		 * We are using the standard CD-ROM sectorsize of 2048 bytes
80 		 * while the drive may be switched to 512 bytes per sector.
81 		 *
82 		 * XXX We assume that secsize is no more than SECTOR_SIZE
83 		 * XXX and that SECTOR_SIZE / secsize is not a fraction.
84 		 */
85 		secsize = usalp->cap->c_bsize;
86 		amount = sectorcount * SECTOR_SIZE;
87 		secno = startsecno * (SECTOR_SIZE / secsize);
88 		bp = buffer;
89 
90 		while (amount > 0) {
91 			amt = amount;
92 			if (amount > bufsize)
93 				amt = bufsize;
94 			secnum = amt / secsize;
95 
96 			if (read_scsi(usalp, bp, secno, secnum) < 0 ||
97 						usal_getresid(usalp) != 0) {
98 #ifdef	OLD
99 				return (-1);
100 #else
101 				comerr("Read error on old image\n");
102 #endif
103 			}
104 
105 			amount	-= secnum * secsize;
106 			bp	+= secnum * secsize;
107 			secno	+= secnum;
108 		}
109 		return (SECTOR_SIZE * sectorcount);
110 	}
111 
112 	f = fileno(in_image);
113 
114 	if (lseek(f, (off_t)startsecno * SECTOR_SIZE, SEEK_SET) == (off_t)-1) {
115 #ifdef	USE_LIBSCHILY
116 		comerr("Seek error on old image\n");
117 #else
118 		fprintf(stderr, "Seek error on old image\n");
119 		exit(10);
120 #endif
121 	}
122 	if ((amt = read(f, buffer, (sectorcount * SECTOR_SIZE)))
123 			!= (sectorcount * SECTOR_SIZE)) {
124 #ifdef	USE_LIBSCHILY
125 		if (amt < 0)
126 			comerr("Read error on old image\n");
127 		comerrno(EX_BAD, "Short read on old image\n"); /* < secnt aber > 0 */
128 #else
129 		if (amt < 0)
130 			fprintf(stderr, "Read error on old image\n");
131 		else
132 			fprintf(stderr, "Short read on old image\n");
133 
134 		exit(10);
135 #endif
136 	}
137 	return (sectorcount * SECTOR_SIZE);
138 }
139 
140 int
scsidev_open(char * path)141 scsidev_open(char *path)
142 {
143 	char	errstr[80];
144 	char	*buf;	/* ignored, bit OS/2 ASPI layer needs memory which */
145 			/* has been allocated by scsi_getbuf()		   */
146 
147 	/*
148 	 * Call usal_remote() to force loading the remote SCSI transport library
149 	 * code that is located in librusal instead of the dummy remote routines
150 	 * that are located inside libusal.
151 	 */
152 	usal_remote();
153 
154 	cdr_defaults(&path, NULL, NULL, NULL);
155 			/* path, debug, verboseopen */
156 	usalp = usal_open(path, errstr, sizeof (errstr), 0, 0);
157 	if (usalp == 0) {
158 		errmsg("%s%sCannot open SCSI driver.\n", errstr, errstr[0]?". ":"");
159 		return (-1);
160 	}
161 
162 	bufsize = usal_bufsize(usalp, BUF_SIZE);
163 	if ((buf = usal_getbuf(usalp, bufsize)) == NULL) {
164 		errmsg("Cannot get SCSI I/O buffer.\n");
165 		usal_close(usalp);
166 		return (-1);
167 	}
168 
169 	bufsize = (bufsize / SECTOR_SIZE) * SECTOR_SIZE;
170 
171 	allow_atapi(usalp, TRUE);
172 
173 	if (!wait_unit_ready(usalp, 60)) { /* Eat Unit att / Wait for drive */
174 		usalp->silent--;
175 		return (-1);
176 	}
177 
178 	usalp->silent++;
179 	read_capacity(usalp);	/* Set Capacity/Sectorsize for I/O */
180 	usalp->silent--;
181 
182 	return (1);
183 }
184 
185 int
scsidev_close()186 scsidev_close()
187 {
188 	if (in_image == NULL) {
189 		return (usal_close(usalp));
190 	} else {
191 		return (fclose(in_image));
192 	}
193 }
194 
195 #endif	/* USE_SCG */
196