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