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"
32 #include "open_image.h"
38 typedef struct ScsiDevice_t {
47 uint32_t scsi_sector_size;
50 void *extra_data; /* extra system dependent information for scsi.
51 On some platforms, filled in by scsi_open, and to
52 be supplied to scsi_cmd */
55 /* ZIP or other scsi device on Solaris or SunOS system.
56 Since Sun won't accept a non-Sun label on a scsi disk, we must
57 bypass Sun's disk interface and use low-level SCSI commands to read
58 or write the ZIP drive. We thus replace the file_read and file_write
59 routines with our own scsi_read and scsi_write routines, that use the
60 uscsi ioctl interface. By James Dugal, jpd@usl.edu, 11-96. Tested
61 under Solaris 2.5 and SunOS 4.3.1_u1 using GCC.
63 Note: the mtools.conf entry for a ZIP drive would look like this:
64 (solaris) drive C: file="/dev/rdsk/c0t5d0s2" partition=4 FAT=16 nodelay exclusive scsi=1
65 (sunos) drive C: file="/dev/rsd5c" partition=4 FAT=16 nodelay exclusive scsi=1
67 Note 2: Sol 2.5 wants mtools to be suid-root, to use the ioctl. SunOS is
68 happy if we just have access to the device, so making mtools sgid to a
69 group called, say, "ziprw" which has rw permission on /dev/rsd5c, is fine.
72 static int scsi_init(ScsiDevice_t *This)
75 unsigned char cdb[10],buf[8];
77 memset(cdb, 0, sizeof cdb);
78 memset(buf,0, sizeof(buf));
79 cdb[0]=SCSI_READ_CAPACITY;
80 if (scsi_cmd(fd, (unsigned char *)cdb,
81 sizeof(cdb), SCSI_IO_READ, buf,
82 sizeof(buf), This->extra_data)==0)
85 ((unsigned)buf[0]<<24)|
86 ((unsigned)buf[1]<<16)|
87 ((unsigned)buf[2]<<8)|
89 if(This->tot_sectors < UINT32_MAX)
92 This->scsi_sector_size=
93 ((unsigned)buf[5]<<16)|
94 ((unsigned)buf[6]<<8)|
96 if (This->scsi_sector_size != 512)
97 fprintf(stderr," (scsi_sector_size=%d)\n",This->scsi_sector_size);
105 * Overflow-safe conversion of bytes to sectors
107 static uint32_t bytesToSectors(size_t bytes, uint32_t sector_size) {
108 size_t sectors = bytes / sector_size;
109 if(bytes % sector_size)
111 if(sectors > UINT32_MAX)
114 return (uint32_t) sectors;
117 static ssize_t scsi_io(Stream_t *Stream, char *buf,
118 mt_off_t where, size_t len, scsi_io_mode_t rwcmd)
120 unsigned int firstblock, nsect;
125 unsigned char cdb[10];
126 DeclareThis(ScsiDevice_t);
128 firstblock=truncMtOffTo32u(where/(mt_off_t)This->scsi_sector_size);
129 /* 512,1024,2048,... bytes/sector supported */
130 offset=(smt_off_t) where % This->scsi_sector_size;
131 nsect=bytesToSectors(offset+len, This->scsi_sector_size);
132 #if defined(OS_sun) && defined(OS_i386)
133 if (This->scsi_sector_size>512)
134 firstblock*=This->scsi_sector_size/512; /* work around a uscsi bug */
135 #endif /* sun && i386 */
138 /* avoid buffer overruns. The transfer MUST be smaller or
139 * equal to the requested size! */
140 while (nsect*This->scsi_sector_size>len)
143 fprintf(stderr,"Scsi buffer too small\n");
146 if(rwcmd == SCSI_IO_WRITE && offset) {
147 /* there seems to be no memmove before a write */
148 fprintf(stderr,"Unaligned write\n");
151 /* a better implementation should use bounce buffers.
152 * However, in normal operation no buffer overruns or
153 * unaligned writes should happen anyways, as the logical
154 * sector size is (hopefully!) equal to the physical one
159 max = scsi_max_length();
164 /* set up SCSI READ/WRITE command */
165 memset(cdb, 0, sizeof cdb);
178 if (firstblock > 0x1fffff || nsect > 0xff) {
179 /* I suspect that the ZIP drive also understands Group 1
180 * commands. If that is indeed true, we may chose Group 1
181 * more aggressively in the future */
183 cdb[0] |= SCSI_GROUP1;
184 clen=10; /* SCSI Group 1 cmd */
186 /* this is one of the rare case where explicit coding is
187 * more portable than macros... The meaning of scsi command
188 * bytes is standardised, whereas the preprocessor macros
189 * handling it might be not... */
191 cdb[2] = (unsigned char) (firstblock >> 24) & 0xff;
192 cdb[3] = (unsigned char) (firstblock >> 16) & 0xff;
193 cdb[4] = (unsigned char) (firstblock >> 8) & 0xff;
194 cdb[5] = (unsigned char) firstblock & 0xff;
196 cdb[7] = (unsigned char) (nsect >> 8) & 0xff;
197 cdb[8] = (unsigned char) nsect & 0xff;
200 clen = 6; /* SCSI Group 0 cmd */
201 cdb[1] |= (unsigned char) ((firstblock >> 16) & 0x1f);
202 cdb[2] = (unsigned char) ((firstblock >> 8) & 0xff);
203 cdb[3] = (unsigned char) firstblock & 0xff;
204 cdb[4] = (unsigned char) nsect;
211 r=scsi_cmd(This->fd, (unsigned char *)cdb, clen, rwcmd, buf,
212 nsect*This->scsi_sector_size, This->extra_data);
218 perror(rwcmd == SCSI_IO_READ ? "SCMD_READ" : "SCMD_WRITE");
222 printf("finished %u for %u\n", firstblock, nsect);
226 printf("zip: read or write OK\n");
229 memmove(buf,buf+offset, nsect*This->scsi_sector_size-offset);
230 if (len==256) return 256;
231 else if (len==512) return 512;
232 else return (ssize_t)(nsect*This->scsi_sector_size-offset);
235 static ssize_t scsi_read(Stream_t *Stream, char *buf, mt_off_t where, size_t len)
238 printf("zip: to read %d bytes at %d\n", len, where);
240 return scsi_io(Stream, buf, where, len, SCSI_IO_READ);
243 static ssize_t scsi_write(Stream_t *Stream, char *buf,
244 mt_off_t where, size_t len)
247 Printf("zip: to write %d bytes at %d\n", len, where);
249 return scsi_io(Stream, buf, where, len, SCSI_IO_WRITE);
252 static int scsi_get_data(Stream_t *Stream, time_t *date, mt_off_t *size,
253 int *type, uint32_t *address)
255 DeclareThis(ScsiDevice_t);
257 if(date || type || address)
258 fprintf(stderr, "Get_data call not supported\n");
260 *size = This->device_size;
266 static Class_t ScsiDeviceClass = {
272 scsi_get_data, /* get_data */
273 0, /* pre-allocate */
278 Stream_t *OpenScsi(struct device *dev,
279 const char *name, int mode, char *errmsg,
280 int mode2, int locked, int lockMode,
288 This = New(ScsiDevice_t);
293 memset((void*)This, 0, sizeof(ScsiDevice_t));
294 This->scsi_sector_size = 512;
295 This->Class = &ScsiDeviceClass;
298 if(!(mode2 & NO_PRIV))
299 This->privileged = IS_PRIVILEGED(dev);
304 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
307 /* End of stuff copied from top of plain_io.c before actual open */
309 This->fd = scsi_open(name, mode, IS_NOLOCK(dev)?0444:0666,
312 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
318 snprintf(errmsg, 199, "Can't open %s: %s",
319 name, strerror(errno));
321 sprintf(errmsg, "Can't open %s: %s",
322 name, strerror(errno));
328 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
331 if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
338 *maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
339 This->Class = &ScsiDeviceClass;
347 dev->tot_sectors = This->tot_sectors;
348 return (Stream_t *) This;