Imported Upstream version 4.0.35
[platform/upstream/mtools.git] / scsi_io.c
1 /*  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
38 typedef struct ScsiDevice_t {
39         Class_t *Class;
40         int refs;
41         Stream_t *Next;
42         Stream_t *Buffer;
43
44         int fd;
45         int privileged;
46
47         uint32_t scsi_sector_size;
48         mt_off_t device_size;
49         uint32_t tot_sectors;
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 */
53 } ScsiDevice_t;
54
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.
62
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
66
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.
70  */
71
72 static int scsi_init(ScsiDevice_t *This)
73 {
74    int fd = This->fd;
75    unsigned char cdb[10],buf[8];
76
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)
83    {
84            This->tot_sectors=
85                    ((unsigned)buf[0]<<24)|
86                    ((unsigned)buf[1]<<16)|
87                    ((unsigned)buf[2]<<8)|
88                    (unsigned)buf[3];
89            if(This->tot_sectors < UINT32_MAX)
90                    This->tot_sectors++;
91
92            This->scsi_sector_size=
93                    ((unsigned)buf[5]<<16)|
94                    ((unsigned)buf[6]<<8)|
95                    (unsigned)buf[7];
96            if (This->scsi_sector_size != 512)
97                    fprintf(stderr,"  (scsi_sector_size=%d)\n",This->scsi_sector_size);
98            return 0;
99    } else
100            return -1;
101 }
102
103
104 /**
105  * Overflow-safe conversion of bytes to sectors
106  */
107 static uint32_t bytesToSectors(size_t bytes, uint32_t sector_size) {
108         size_t sectors = bytes / sector_size;
109         if(bytes % sector_size)
110                 sectors++;
111         if(sectors > UINT32_MAX)
112                 return UINT32_MAX;
113         else
114                 return (uint32_t) sectors;
115 }
116
117 static ssize_t scsi_io(Stream_t *Stream, char *buf,
118                        mt_off_t where, size_t len, scsi_io_mode_t rwcmd)
119 {
120         unsigned int firstblock, nsect;
121         uint8_t clen;
122         int r;
123         unsigned int max;
124         uint32_t offset;
125         unsigned char cdb[10];
126         DeclareThis(ScsiDevice_t);
127
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 */
136
137         if (len>512) {
138                 /* avoid buffer overruns. The transfer MUST be smaller or
139                 * equal to the requested size! */
140                 while (nsect*This->scsi_sector_size>len)
141                         --nsect;
142                 if(!nsect) {
143                         fprintf(stderr,"Scsi buffer too small\n");
144                         exit(1);
145                 }
146                 if(rwcmd == SCSI_IO_WRITE && offset) {
147                         /* there seems to be no memmove before a write */
148                         fprintf(stderr,"Unaligned write\n");
149                         exit(1);
150                 }
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
155                  */
156         }
157
158
159         max = scsi_max_length();
160
161         if (nsect > max)
162                 nsect=max;
163
164         /* set up SCSI READ/WRITE command */
165         memset(cdb, 0, sizeof cdb);
166
167         switch(rwcmd) {
168                 case SCSI_IO_READ:
169                         cdb[0] = SCSI_READ;
170                         break;
171                 case SCSI_IO_WRITE:
172                         cdb[0] = SCSI_WRITE;
173                         break;
174         }
175
176         cdb[1] = 0;
177
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 */
182
183                 cdb[0] |= SCSI_GROUP1;
184                 clen=10; /* SCSI Group 1 cmd */
185
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... */
190
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;
195                 cdb[6] = 0;
196                 cdb[7] = (unsigned char) (nsect >> 8) & 0xff;
197                 cdb[8] = (unsigned char) nsect & 0xff;
198                 cdb[9] = 0;
199         } else {
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;
205                 cdb[5] = 0;
206         }
207
208         if(This->privileged)
209                 reclaim_privs();
210
211         r=scsi_cmd(This->fd, (unsigned char *)cdb, clen, rwcmd, buf,
212                    nsect*This->scsi_sector_size, This->extra_data);
213
214         if(This->privileged)
215                 drop_privs();
216
217         if(r) {
218                 perror(rwcmd == SCSI_IO_READ ? "SCMD_READ" : "SCMD_WRITE");
219                 return -1;
220         }
221 #ifdef JPD
222         printf("finished %u for %u\n", firstblock, nsect);
223 #endif
224
225 #ifdef JPD
226         printf("zip: read or write OK\n");
227 #endif
228         if (offset>0)
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);
233 }
234
235 static ssize_t scsi_read(Stream_t *Stream, char *buf, mt_off_t where, size_t len)
236 {
237 #ifdef JPD
238         printf("zip: to read %d bytes at %d\n", len, where);
239 #endif
240         return scsi_io(Stream, buf, where, len, SCSI_IO_READ);
241 }
242
243 static ssize_t scsi_write(Stream_t *Stream, char *buf,
244                           mt_off_t where, size_t len)
245 {
246 #ifdef JPD
247         Printf("zip: to write %d bytes at %d\n", len, where);
248 #endif
249         return scsi_io(Stream, buf, where, len, SCSI_IO_WRITE);
250 }
251
252 static int scsi_get_data(Stream_t *Stream, time_t *date, mt_off_t *size,
253                          int *type, uint32_t *address)
254 {
255         DeclareThis(ScsiDevice_t);
256
257         if(date || type || address)
258                 fprintf(stderr, "Get_data call not supported\n");
259         if(size)
260                 *size = This->device_size;
261         return 0;
262 }
263
264
265
266 static Class_t ScsiDeviceClass = {
267         scsi_read,
268         scsi_write,
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
278 Stream_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         This->scsi_sector_size = 512;
295         This->Class = &ScsiDeviceClass;
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         This->refs = 1;
334         This->Next = 0;
335         This->Buffer = 0;
336
337         if(maxSize)
338                 *maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
339         This->Class = &ScsiDeviceClass;
340         if(This->privileged)
341                 reclaim_privs();
342         ret=scsi_init(This);
343         if(This->privileged)
344                 drop_privs();
345         if(ret < 0)
346                 goto exit_0;
347         dev->tot_sectors = This->tot_sectors;
348         return (Stream_t *) This;
349  exit_0:
350         close(This->fd);
351  exit_1:
352         Free(This);
353         return NULL;
354 }