Imported Upstream version 4.0.43
[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 "llong.h"
30
31 #include "open_image.h"
32
33 #include "scsi.h"
34 #include "plain_io.h"
35 #include "scsi_io.h"
36 #ifdef HAVE_SCSI
37 typedef struct ScsiDevice_t {
38         struct Stream_t head;
39
40         int fd;
41         int privileged;
42
43         uint32_t scsi_sector_size;
44         mt_off_t device_size;
45         uint32_t tot_sectors;
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 */
49         const char *postcmd;
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
69 static 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  */
104 static 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
114 static 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
232 static 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
241 static 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
250 static 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 static int scsi_free(Stream_t *Stream)
263 {
264         DeclareThis(ScsiDevice_t);
265
266         if(This->fd > 2) {
267                 int ret = close(This->fd);
268                 postcmd(This->postcmd);
269                 return ret;
270         } else
271                 return 0;
272 }
273         
274 static Class_t ScsiDeviceClass = {
275         0,
276         0,
277         scsi_pread,
278         scsi_pwrite,
279         0,
280         scsi_free,
281         set_geom_noop,
282         scsi_get_data, /* get_data */
283         0, /* pre-allocate */
284         0, /* dos-convert */
285         0 /* discard */
286 };
287
288 Stream_t *OpenScsi(struct device *dev,
289                    const char *name, int mode, char *errmsg,
290                    int mode2, int locked, int lockMode,
291                    mt_off_t *maxSize)
292 {
293         int ret;
294         ScsiDevice_t *This;
295         if (!IS_SCSI(dev))
296                 return NULL;
297
298         This = New(ScsiDevice_t);
299         if (!This){
300                 printOom();
301                 return 0;
302         }
303         memset((void*)This, 0, sizeof(ScsiDevice_t));
304         init_head(&This->head, &ScsiDeviceClass, NULL);
305         This->scsi_sector_size = 512;
306
307         if(dev) {
308                 if(!(mode2 & NO_PRIV))
309                         This->privileged = IS_PRIVILEGED(dev);
310                 mode |= dev->mode;
311         }
312
313         precmd(dev);
314         if(dev)
315                 This->postcmd = dev->postcmd;
316         if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
317                 reclaim_privs();
318
319         /* End of stuff copied from top of plain_io.c before actual open */
320
321         This->fd = scsi_open(name, mode, IS_NOLOCK(dev)?0444:0666,
322                              &This->extra_data);
323
324         if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
325                 drop_privs();
326
327         if (This->fd < 0) {
328                 if(errmsg) {
329 #ifdef HAVE_SNPRINTF
330                         snprintf(errmsg, 199, "Can't open %s: %s",
331                                 name, strerror(errno));
332 #else
333                         sprintf(errmsg, "Can't open %s: %s",
334                                 name, strerror(errno));
335 #endif
336                 }
337                 goto exit_1;
338         }
339
340         if(IS_PRIVILEGED(dev) && !(mode2 & NO_PRIV))
341                 closeExec(This->fd);
342
343         if(LockDevice(This->fd, dev, locked, lockMode, errmsg) < 0)
344                 goto exit_0;
345
346         if(maxSize)
347                 *maxSize = MAX_OFF_T_B(31+log_2(This->scsi_sector_size));
348         if(This->privileged)
349                 reclaim_privs();
350         ret=scsi_init(This);
351         if(This->privileged)
352                 drop_privs();
353         if(ret < 0)
354                 goto exit_0;
355         dev->tot_sectors = This->tot_sectors;
356         return &This->head;
357  exit_0:
358         close(This->fd);
359         postcmd(This->postcmd);
360  exit_1:
361         Free(This);
362         return NULL;
363 }
364 #endif