blob: 48abdcb7228bf69c33c0f4ac222286cafa89674c [file] [log] [blame]
Yi Kong39bbd962022-01-09 19:41:38 +08001/* Copyright 2021 Alain Knaff.
2 * This file is part of mtools.
3 *
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.
8 *
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.
13 *
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/>.
16 *
17 * I/O to a SCSI device
18 *
19 * written by:
20 *
21 * Alain L. Knaff
22 * alain@knaff.lu
23 *
24 */
25
26#include "sysincludes.h"
27#include "stream.h"
28#include "mtools.h"
29#include "msdos.h"
30#include "llong.h"
31
32#include "open_image.h"
33
34#include "scsi.h"
35#include "plain_io.h"
36#include "scsi_io.h"
37
38typedef struct ScsiDevice_t {
39 struct Stream_t head;
40
41 int fd;
42 int privileged;
43
44 uint32_t scsi_sector_size;
45 mt_off_t device_size;
46 uint32_t tot_sectors;
47 void *extra_data; /* extra system dependent information for scsi.
48 On some platforms, filled in by scsi_open, and to
49 be supplied to scsi_cmd */
50} ScsiDevice_t;
51
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.
59
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
63
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.
67 */
68
69static int scsi_init(ScsiDevice_t *This)
70{
71 int fd = This->fd;
72 unsigned char cdb[10],buf[8];
73
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)
80 {
81 This->tot_sectors=
82 ((unsigned)buf[0]<<24)|
83 ((unsigned)buf[1]<<16)|
84 ((unsigned)buf[2]<<8)|
85 (unsigned)buf[3];
86 if(This->tot_sectors < UINT32_MAX)
87 This->tot_sectors++;
88
89 This->scsi_sector_size=
90 ((unsigned)buf[5]<<16)|
91 ((unsigned)buf[6]<<8)|
92 (unsigned)buf[7];
93 if (This->scsi_sector_size != 512)
94 fprintf(stderr," (scsi_sector_size=%d)\n",This->scsi_sector_size);
95 return 0;
96 } else
97 return -1;
98}
99
100
101/**
102 * Overflow-safe conversion of bytes to sectors
103 */
104static uint32_t bytesToSectors(size_t bytes, uint32_t sector_size) {
105 size_t sectors = bytes / sector_size;
106 if(bytes % sector_size)
107 sectors++;
108 if(sectors > UINT32_MAX)
109 return UINT32_MAX;
110 else
111 return (uint32_t) sectors;
112}
113
114static ssize_t scsi_io(Stream_t *Stream, char *buf,
115 mt_off_t where, size_t len, scsi_io_mode_t rwcmd)
116{
117 unsigned int firstblock, nsect;
118 uint8_t clen;
119 int r;
120 unsigned int max;
121 uint32_t offset;
122 unsigned char cdb[10];
123 DeclareThis(ScsiDevice_t);
124
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 */
133
134 if (len>512) {
135 /* avoid buffer overruns. The transfer MUST be smaller or
136 * equal to the requested size! */
137 while (nsect*This->scsi_sector_size>len)
138 --nsect;
139 if(!nsect) {
140 fprintf(stderr,"Scsi buffer too small\n");
141 exit(1);
142 }
143 if(rwcmd == SCSI_IO_WRITE && offset) {
144 /* there seems to be no memmove before a write */
145 fprintf(stderr,"Unaligned write\n");
146 exit(1);
147 }
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
152 */
153 }
154
155
156 max = scsi_max_length();
157
158 if (nsect > max)
159 nsect=max;
160
161 /* set up SCSI READ/WRITE command */
162 memset(cdb, 0, sizeof cdb);
163
164 switch(rwcmd) {
165 case SCSI_IO_READ:
166 cdb[0] = SCSI_READ;
167 break;
168 case SCSI_IO_WRITE:
169 cdb[0] = SCSI_WRITE;
170 break;
171 }
172
173 cdb[1] = 0;
174
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 */
179
180 cdb[0] |= SCSI_GROUP1;
181 clen=10; /* SCSI Group 1 cmd */
182
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... */
187
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;
192 cdb[6] = 0;
193 cdb[7] = (unsigned char) (nsect >> 8) & 0xff;
194 cdb[8] = (unsigned char) nsect & 0xff;
195 cdb[9] = 0;
196 } else {
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;
202 cdb[5] = 0;
203 }
204
205 if(This->privileged)
206 reclaim_privs();
207
208 r=scsi_cmd(This->fd, (unsigned char *)cdb, clen, rwcmd, buf,
209 nsect*This->scsi_sector_size, This->extra_data);
210
211 if(This->privileged)
212 drop_privs();
213
214 if(r) {
215 perror(rwcmd == SCSI_IO_READ ? "SCMD_READ" : "SCMD_WRITE");
216 return -1;
217 }
218#ifdef JPD
219 printf("finished %u for %u\n", firstblock, nsect);
220#endif
221
222#ifdef JPD
223 printf("zip: read or write OK\n");
224#endif
225 if (offset>0)
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);
230}
231
232static ssize_t scsi_pread(Stream_t *Stream, char *buf,
233 mt_off_t where, size_t len)
234{
235#ifdef JPD
236 printf("zip: to read %d bytes at %d\n", len, where);
237#endif
238 return scsi_io(Stream, buf, where, len, SCSI_IO_READ);
239}
240
241static ssize_t scsi_pwrite(Stream_t *Stream, char *buf,
242 mt_off_t where, size_t len)
243{
244#ifdef JPD
245 Printf("zip: to write %d bytes at %d\n", len, where);
246#endif
247 return scsi_io(Stream, buf, where, len, SCSI_IO_WRITE);
248}
249
250static int scsi_get_data(Stream_t *Stream, time_t *date, mt_off_t *size,
251 int *type, uint32_t *address)
252{
253 DeclareThis(ScsiDevice_t);
254
255 if(date || type || address)
256 fprintf(stderr, "Get_data call not supported\n");
257 if(size)
258 *size = This->device_size;
259 return 0;
260}
261
262
263
264static Class_t ScsiDeviceClass = {
265 0,
266 0,
267 scsi_pread,
268 scsi_pwrite,
269 0,
270 0,
271 set_geom_noop,
272 scsi_get_data, /* get_data */
273 0, /* pre-allocate */
274 0, /* dos-convert */
275 0 /* discard */
276};
277
278Stream_t *OpenScsi(struct device *dev,
279 const char *name, int mode, char *errmsg,
280 int mode2, int locked, int lockMode,
281 mt_off_t *maxSize)
282{
283 int ret;
284 ScsiDevice_t *This;
285 if (!IS_SCSI(dev))
286 return NULL;
287
288 This = New(ScsiDevice_t);
289 if (!This){
290 printOom();
291 return 0;
292 }
293 memset((void*)This, 0, sizeof(ScsiDevice_t));
294 init_head(&This->head, &ScsiDeviceClass, NULL);
295 This->scsi_sector_size = 512;
296
297 if(dev) {
298 if(!(mode2 & NO_PRIV))
299 This->privileged = IS_PRIVILEGED(dev);
300 mode |= dev->mode;
301 }
302
303 precmd(dev);
304 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
305 reclaim_privs();
306
307 /* End of stuff copied from top of plain_io.c before actual open */
308
309 This->fd = scsi_open(name, mode, IS_NOLOCK(dev)?0444:0666,
310 &This->extra_data);
311
312 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
313 drop_privs();
314
315 if (This->fd < 0) {
316 if(errmsg) {
317#ifdef HAVE_SNPRINTF
318 snprintf(errmsg, 199, "Can't open %s: %s",
319 name, strerror(errno));
320#else
321 sprintf(errmsg, "Can't open %s: %s",
322 name, strerror(errno));
323#endif
324 }
325 goto exit_1;
326 }
327
328 if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
329 closeExec(This->fd);
330
331 if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
332 goto exit_0;
333
334 if(maxSize)
335 *maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
336 if(This->privileged)
337 reclaim_privs();
338 ret=scsi_init(This);
339 if(This->privileged)
340 drop_privs();
341 if(ret < 0)
342 goto exit_0;
343 dev->tot_sectors = This->tot_sectors;
344 return &This->head;
345 exit_0:
346 close(This->fd);
347 exit_1:
348 Free(This);
349 return NULL;
350}