1 /* Copyright 2021 Alain Knaff.
2 * This file is part of mtools.
4 * Mtools is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Mtools is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
17 * I/O to a SCSI device
26 #include "sysincludes.h"
31 #include "open_image.h"
37 typedef struct ScsiDevice_t {
43 uint32_t scsi_sector_size;
46 void *extra_data; /* extra system dependent information for scsi.
47 On some platforms, filled in by scsi_open, and to
48 be supplied to scsi_cmd */
52 /* ZIP or other scsi device on Solaris or SunOS system.
53 Since Sun won't accept a non-Sun label on a scsi disk, we must
54 bypass Sun's disk interface and use low-level SCSI commands to read
55 or write the ZIP drive. We thus replace the file_read and file_write
56 routines with our own scsi_read and scsi_write routines, that use the
57 uscsi ioctl interface. By James Dugal, jpd@usl.edu, 11-96. Tested
58 under Solaris 2.5 and SunOS 4.3.1_u1 using GCC.
60 Note: the mtools.conf entry for a ZIP drive would look like this:
61 (solaris) drive C: file="/dev/rdsk/c0t5d0s2" partition=4 FAT=16 nodelay exclusive scsi=1
62 (sunos) drive C: file="/dev/rsd5c" partition=4 FAT=16 nodelay exclusive scsi=1
64 Note 2: Sol 2.5 wants mtools to be suid-root, to use the ioctl. SunOS is
65 happy if we just have access to the device, so making mtools sgid to a
66 group called, say, "ziprw" which has rw permission on /dev/rsd5c, is fine.
69 static int scsi_init(ScsiDevice_t *This)
72 unsigned char cdb[10],buf[8];
74 memset(cdb, 0, sizeof cdb);
75 memset(buf,0, sizeof(buf));
76 cdb[0]=SCSI_READ_CAPACITY;
77 if (scsi_cmd(fd, (unsigned char *)cdb,
78 sizeof(cdb), SCSI_IO_READ, buf,
79 sizeof(buf), This->extra_data)==0)
82 ((unsigned)buf[0]<<24)|
83 ((unsigned)buf[1]<<16)|
84 ((unsigned)buf[2]<<8)|
86 if(This->tot_sectors < UINT32_MAX)
89 This->scsi_sector_size=
90 ((unsigned)buf[5]<<16)|
91 ((unsigned)buf[6]<<8)|
93 if (This->scsi_sector_size != 512)
94 fprintf(stderr," (scsi_sector_size=%d)\n",This->scsi_sector_size);
102 * Overflow-safe conversion of bytes to sectors
104 static uint32_t bytesToSectors(size_t bytes, uint32_t sector_size) {
105 size_t sectors = bytes / sector_size;
106 if(bytes % sector_size)
108 if(sectors > UINT32_MAX)
111 return (uint32_t) sectors;
114 static ssize_t scsi_io(Stream_t *Stream, char *buf,
115 mt_off_t where, size_t len, scsi_io_mode_t rwcmd)
117 unsigned int firstblock, nsect;
122 unsigned char cdb[10];
123 DeclareThis(ScsiDevice_t);
125 firstblock=truncMtOffTo32u(where/(mt_off_t)This->scsi_sector_size);
126 /* 512,1024,2048,... bytes/sector supported */
127 offset=(smt_off_t) where % This->scsi_sector_size;
128 nsect=bytesToSectors(offset+len, This->scsi_sector_size);
129 #if defined(OS_sun) && defined(OS_i386)
130 if (This->scsi_sector_size>512)
131 firstblock*=This->scsi_sector_size/512; /* work around a uscsi bug */
132 #endif /* sun && i386 */
135 /* avoid buffer overruns. The transfer MUST be smaller or
136 * equal to the requested size! */
137 while (nsect*This->scsi_sector_size>len)
140 fprintf(stderr,"Scsi buffer too small\n");
143 if(rwcmd == SCSI_IO_WRITE && offset) {
144 /* there seems to be no memmove before a write */
145 fprintf(stderr,"Unaligned write\n");
148 /* a better implementation should use bounce buffers.
149 * However, in normal operation no buffer overruns or
150 * unaligned writes should happen anyways, as the logical
151 * sector size is (hopefully!) equal to the physical one
156 max = scsi_max_length();
161 /* set up SCSI READ/WRITE command */
162 memset(cdb, 0, sizeof cdb);
175 if (firstblock > 0x1fffff || nsect > 0xff) {
176 /* I suspect that the ZIP drive also understands Group 1
177 * commands. If that is indeed true, we may chose Group 1
178 * more aggressively in the future */
180 cdb[0] |= SCSI_GROUP1;
181 clen=10; /* SCSI Group 1 cmd */
183 /* this is one of the rare case where explicit coding is
184 * more portable than macros... The meaning of scsi command
185 * bytes is standardised, whereas the preprocessor macros
186 * handling it might be not... */
188 cdb[2] = (unsigned char) (firstblock >> 24) & 0xff;
189 cdb[3] = (unsigned char) (firstblock >> 16) & 0xff;
190 cdb[4] = (unsigned char) (firstblock >> 8) & 0xff;
191 cdb[5] = (unsigned char) firstblock & 0xff;
193 cdb[7] = (unsigned char) (nsect >> 8) & 0xff;
194 cdb[8] = (unsigned char) nsect & 0xff;
197 clen = 6; /* SCSI Group 0 cmd */
198 cdb[1] |= (unsigned char) ((firstblock >> 16) & 0x1f);
199 cdb[2] = (unsigned char) ((firstblock >> 8) & 0xff);
200 cdb[3] = (unsigned char) firstblock & 0xff;
201 cdb[4] = (unsigned char) nsect;
208 r=scsi_cmd(This->fd, (unsigned char *)cdb, clen, rwcmd, buf,
209 nsect*This->scsi_sector_size, This->extra_data);
215 perror(rwcmd == SCSI_IO_READ ? "SCMD_READ" : "SCMD_WRITE");
219 printf("finished %u for %u\n", firstblock, nsect);
223 printf("zip: read or write OK\n");
226 memmove(buf,buf+offset, nsect*This->scsi_sector_size-offset);
227 if (len==256) return 256;
228 else if (len==512) return 512;
229 else return (ssize_t)(nsect*This->scsi_sector_size-offset);
232 static ssize_t scsi_pread(Stream_t *Stream, char *buf,
233 mt_off_t where, size_t len)
236 printf("zip: to read %d bytes at %d\n", len, where);
238 return scsi_io(Stream, buf, where, len, SCSI_IO_READ);
241 static ssize_t scsi_pwrite(Stream_t *Stream, char *buf,
242 mt_off_t where, size_t len)
245 Printf("zip: to write %d bytes at %d\n", len, where);
247 return scsi_io(Stream, buf, where, len, SCSI_IO_WRITE);
250 static int scsi_get_data(Stream_t *Stream, time_t *date, mt_off_t *size,
251 int *type, uint32_t *address)
253 DeclareThis(ScsiDevice_t);
255 if(date || type || address)
256 fprintf(stderr, "Get_data call not supported\n");
258 *size = This->device_size;
262 static int scsi_free(Stream_t *Stream)
264 DeclareThis(ScsiDevice_t);
267 int ret = close(This->fd);
268 postcmd(This->postcmd);
274 static Class_t ScsiDeviceClass = {
282 scsi_get_data, /* get_data */
283 0, /* pre-allocate */
288 Stream_t *OpenScsi(struct device *dev,
289 const char *name, int mode, char *errmsg,
290 int mode2, int locked, int lockMode,
298 This = New(ScsiDevice_t);
303 memset((void*)This, 0, sizeof(ScsiDevice_t));
304 init_head(&This->head, &ScsiDeviceClass, NULL);
305 This->scsi_sector_size = 512;
308 if(!(mode2 & NO_PRIV))
309 This->privileged = IS_PRIVILEGED(dev);
315 This->postcmd = dev->postcmd;
316 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
319 /* End of stuff copied from top of plain_io.c before actual open */
321 This->fd = scsi_open(name, mode, IS_NOLOCK(dev)?0444:0666,
324 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
330 snprintf(errmsg, 199, "Can't open %s: %s",
331 name, strerror(errno));
333 sprintf(errmsg, "Can't open %s: %s",
334 name, strerror(errno));
340 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
343 if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
347 *maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
355 dev->tot_sectors = This->tot_sectors;
359 postcmd(This->postcmd);