1995-01-24 12:07:27 +00:00
|
|
|
/*
|
|
|
|
* Written By Julian ELischer
|
|
|
|
* Copyright julian Elischer 1993.
|
|
|
|
* Permission is granted to use or redistribute this file in any way as long
|
1995-05-30 06:12:45 +00:00
|
|
|
* as this notice remains. Julian Elischer does not guarantee that this file
|
|
|
|
* is totally correct for any given task and users of this file must
|
1995-01-24 12:07:27 +00:00
|
|
|
* accept responsibility for any damage that occurs from the application of this
|
|
|
|
* file.
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1995-01-24 12:07:27 +00:00
|
|
|
* (julian@tfs.com julian@dialix.oz.au)
|
|
|
|
*
|
|
|
|
* User SCSI hooks added by Peter Dufault:
|
|
|
|
*
|
|
|
|
* Copyright (c) 1994 HD Associates
|
|
|
|
* (contact: dufault@hda.com)
|
|
|
|
* All rights reserved.
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1995-01-24 12:07:27 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of HD Associates
|
|
|
|
* may not be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
1995-05-30 06:12:45 +00:00
|
|
|
*
|
1995-01-24 12:07:27 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1995-07-30 12:58:47 +00:00
|
|
|
* $Id: scsi.c,v 1.9 1995/07/11 09:21:33 dufault Exp $
|
1995-01-24 12:07:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
1995-04-28 19:24:39 +00:00
|
|
|
#include <unistd.h>
|
1995-01-24 12:07:27 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/scsiio.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <scsi.h>
|
1995-04-28 19:24:39 +00:00
|
|
|
#include <ctype.h>
|
1995-07-11 09:21:33 +00:00
|
|
|
#include <signal.h>
|
1995-01-24 12:07:27 +00:00
|
|
|
|
|
|
|
int fd;
|
|
|
|
int debuglevel;
|
1995-04-28 19:24:39 +00:00
|
|
|
int debugflag;
|
|
|
|
int commandflag;
|
1995-01-24 12:07:27 +00:00
|
|
|
int reprobe;
|
|
|
|
int probe_all;
|
|
|
|
int verbose = 0;
|
|
|
|
int bus = -1; /* all busses */
|
|
|
|
int targ = -1; /* all targs */
|
|
|
|
int lun = 0; /* just lun 0 */
|
1995-04-28 19:24:39 +00:00
|
|
|
int freeze = 0; /* Freeze this many seconds */
|
1995-01-24 12:07:27 +00:00
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
int modeflag;
|
|
|
|
int editflag;
|
|
|
|
int modepage = 0; /* Read this mode page */
|
|
|
|
int pagectl = 0; /* Mode sense page control */
|
1995-05-01 12:35:05 +00:00
|
|
|
int seconds = 2;
|
1995-04-28 19:24:39 +00:00
|
|
|
|
|
|
|
void usage(void)
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
|
|
|
printf(
|
|
|
|
|
|
|
|
"Usage:\n"
|
|
|
|
"\n"
|
|
|
|
" scsi -f device -d debug_level # To set debug level\n"
|
1995-04-28 19:24:39 +00:00
|
|
|
" scsi -f device [-v] -z seconds # To freeze bus\n"
|
|
|
|
" scsi -f device -m page [-P pc] # To read mode pages\n"
|
1995-01-24 12:07:27 +00:00
|
|
|
" scsi -f device -p [-b bus] [-l lun] # To probe all devices\n"
|
|
|
|
" scsi -f device -r [-b bus] [-t targ] [-l lun] # To reprobe a device\n"
|
1995-05-01 12:35:05 +00:00
|
|
|
" scsi -f device [-v] [-s seconds] -c cmd_fmt [arg0 ... argn] # A command...\n"
|
|
|
|
" -o count out_fmt [arg0 ... argn] # EITHER (data out)\n"
|
|
|
|
" -i count in_fmt # OR (data in)\n"
|
1995-01-24 12:07:27 +00:00
|
|
|
"\n"
|
|
|
|
"\"out_fmt\" can be \"-\" to read output data from stdin;\n"
|
|
|
|
"\"in_fmt\" can be \"-\" to write input data to stdout;\n"
|
|
|
|
"\n"
|
|
|
|
"If debugging is not compiled in the kernel, \"-d\" will have no effect\n"
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void procargs(int *argc_p, char ***argv_p)
|
|
|
|
{
|
|
|
|
int argc = *argc_p;
|
|
|
|
char **argv = *argv_p;
|
|
|
|
extern char *optarg;
|
|
|
|
extern int optind;
|
|
|
|
int fflag,
|
|
|
|
ch;
|
|
|
|
|
|
|
|
fflag = 0;
|
1995-04-28 19:24:39 +00:00
|
|
|
commandflag = 0;
|
|
|
|
debugflag = 0;
|
1995-05-01 12:35:05 +00:00
|
|
|
while ((ch = getopt(argc, argv, "ceprvf:d:b:t:l:z:m:P:s:")) != EOF) {
|
1995-01-24 12:07:27 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
probe_all = 1;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
reprobe = 1;
|
|
|
|
break;
|
|
|
|
case 'c':
|
1995-04-28 19:24:39 +00:00
|
|
|
commandflag = 1;
|
1995-01-24 12:07:27 +00:00
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose = 1;
|
|
|
|
break;
|
1995-04-28 19:24:39 +00:00
|
|
|
case 'e':
|
|
|
|
editflag = 1;
|
|
|
|
break;
|
1995-01-24 12:07:27 +00:00
|
|
|
case 'f':
|
|
|
|
if ((fd = scsi_open(optarg, O_RDWR)) < 0) {
|
|
|
|
(void) fprintf(stderr,
|
|
|
|
"%s: unable to open device %s: %s\n",
|
|
|
|
argv[0], optarg, strerror(errno));
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
fflag = 1;
|
|
|
|
break;
|
|
|
|
case 'd':
|
1995-04-28 19:24:39 +00:00
|
|
|
debuglevel = strtol(optarg, 0, 0);
|
|
|
|
debugflag = 1;
|
1995-01-24 12:07:27 +00:00
|
|
|
break;
|
|
|
|
case 'b':
|
1995-04-28 19:24:39 +00:00
|
|
|
bus = strtol(optarg, 0, 0);
|
1995-01-24 12:07:27 +00:00
|
|
|
break;
|
|
|
|
case 't':
|
1995-04-28 19:24:39 +00:00
|
|
|
targ = strtol(optarg, 0, 0);
|
1995-01-24 12:07:27 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
1995-04-28 19:24:39 +00:00
|
|
|
lun = strtol(optarg, 0, 0);
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
freeze = strtol(optarg, 0, 0);
|
|
|
|
break;
|
|
|
|
case 'P':
|
|
|
|
pagectl = strtol(optarg, 0, 0);
|
|
|
|
break;
|
1995-05-01 12:35:05 +00:00
|
|
|
case 's':
|
|
|
|
seconds = strtol(optarg, 0, 0);
|
|
|
|
break;
|
1995-04-28 19:24:39 +00:00
|
|
|
case 'm':
|
|
|
|
modeflag = 1;
|
|
|
|
modepage = strtol(optarg, 0, 0);
|
1995-01-24 12:07:27 +00:00
|
|
|
break;
|
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*argc_p = argc - optind;
|
|
|
|
*argv_p = argv + optind;
|
|
|
|
|
|
|
|
if (!fflag) usage();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get_hook: Structure for evaluating args in a callback.
|
|
|
|
*/
|
|
|
|
struct get_hook
|
|
|
|
{
|
|
|
|
int argc;
|
|
|
|
char **argv;
|
|
|
|
int got;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* iget: Integer argument callback
|
|
|
|
*/
|
|
|
|
int iget(void *hook, char *name)
|
|
|
|
{
|
|
|
|
struct get_hook *h = (struct get_hook *)hook;
|
|
|
|
int arg;
|
|
|
|
|
|
|
|
if (h->got >= h->argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Expecting an integer argument.\n");
|
|
|
|
usage();
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
arg = strtol(h->argv[h->got], 0, 0);
|
1995-01-24 12:07:27 +00:00
|
|
|
h->got++;
|
|
|
|
|
1995-05-01 12:54:32 +00:00
|
|
|
if (verbose && name && *name)
|
1995-04-17 14:35:07 +00:00
|
|
|
printf("%s: %d\n", name, arg);
|
|
|
|
|
1995-01-24 12:07:27 +00:00
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cget: char * argument callback
|
|
|
|
*/
|
|
|
|
char *cget(void *hook, char *name)
|
|
|
|
{
|
|
|
|
struct get_hook *h = (struct get_hook *)hook;
|
|
|
|
char *arg;
|
|
|
|
|
|
|
|
if (h->got >= h->argc)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Expecting a character pointer argument.\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
arg = h->argv[h->got];
|
|
|
|
h->got++;
|
|
|
|
|
1995-04-17 14:35:07 +00:00
|
|
|
if (verbose && name)
|
|
|
|
printf("cget: %s: %s", name, arg);
|
|
|
|
|
1995-01-24 12:07:27 +00:00
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* arg_put: "put argument" callback
|
|
|
|
*/
|
|
|
|
void arg_put(void *hook, int letter, void *arg, int count, char *name)
|
|
|
|
{
|
1995-01-26 23:40:40 +00:00
|
|
|
if (verbose && name && *name)
|
1995-04-28 19:24:39 +00:00
|
|
|
printf("%s: ", name);
|
1995-01-24 12:07:27 +00:00
|
|
|
|
|
|
|
switch(letter)
|
|
|
|
{
|
|
|
|
case 'i':
|
|
|
|
case 'b':
|
|
|
|
printf("%d ", (int)arg);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
case 'z':
|
|
|
|
{
|
|
|
|
char *p = malloc(count + 1);
|
|
|
|
p[count] = 0;
|
|
|
|
strncpy(p, (char *)arg, count);
|
|
|
|
if (letter == 'z')
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = count - 1; i >= 0; i--)
|
|
|
|
if (p[i] == ' ')
|
|
|
|
p[i] = 0;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
printf("%s ", p);
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
printf("Unknown format letter: '%c'\n", letter);
|
|
|
|
}
|
|
|
|
if (verbose)
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
|
1995-04-17 14:35:07 +00:00
|
|
|
int arg_get (void *hook, char *field_name)
|
|
|
|
{
|
|
|
|
printf("get \"%s\".\n", field_name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1995-01-24 12:07:27 +00:00
|
|
|
/* data_phase: SCSI bus data phase: DATA IN, DATA OUT, or no data transfer.
|
|
|
|
*/
|
|
|
|
enum data_phase {none = 0, in, out};
|
|
|
|
|
|
|
|
/* do_cmd: Send a command to a SCSI device
|
|
|
|
*/
|
1995-05-01 12:35:05 +00:00
|
|
|
static void
|
|
|
|
do_cmd(int fd, char *fmt, int argc, char **argv)
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
|
|
|
struct get_hook h;
|
|
|
|
scsireq_t *scsireq = scsireq_new();
|
|
|
|
enum data_phase data_phase;
|
1995-04-28 19:24:39 +00:00
|
|
|
int count;
|
|
|
|
char *data_fmt;
|
1995-01-24 12:07:27 +00:00
|
|
|
|
|
|
|
h.argc = argc;
|
|
|
|
h.argv = argv;
|
|
|
|
h.got = 0;
|
|
|
|
|
|
|
|
scsireq_reset(scsireq);
|
|
|
|
|
|
|
|
scsireq_build_visit(scsireq, 0, 0, 0, fmt, iget, (void *)&h);
|
|
|
|
|
|
|
|
/* Three choices here:
|
|
|
|
* 1. We've used up all the args and have no data phase.
|
|
|
|
* 2. We have input data ("-i")
|
|
|
|
* 3. We have output data ("-o")
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (h.got >= h.argc)
|
1995-04-28 19:24:39 +00:00
|
|
|
{
|
1995-01-24 12:07:27 +00:00
|
|
|
data_phase = none;
|
1995-04-28 19:24:39 +00:00
|
|
|
count = scsireq->datalen = 0;
|
|
|
|
}
|
1995-01-24 12:07:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char *flag = cget(&h, 0);
|
|
|
|
|
|
|
|
if (strcmp(flag, "-o") == 0)
|
1995-04-28 19:24:39 +00:00
|
|
|
{
|
1995-01-24 12:07:27 +00:00
|
|
|
data_phase = out;
|
1995-04-28 19:24:39 +00:00
|
|
|
scsireq->flags = SCCMD_WRITE;
|
|
|
|
}
|
1995-01-24 12:07:27 +00:00
|
|
|
else if (strcmp(flag, "-i") == 0)
|
1995-04-28 19:24:39 +00:00
|
|
|
{
|
1995-01-24 12:07:27 +00:00
|
|
|
data_phase = in;
|
1995-04-28 19:24:39 +00:00
|
|
|
scsireq->flags = SCCMD_READ;
|
|
|
|
}
|
1995-01-24 12:07:27 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr,
|
|
|
|
"Need either \"-i\" or \"-o\" for data phase; not \"%s\".\n", flag);
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
count = scsireq->datalen = iget(&h, 0);
|
1995-01-24 12:07:27 +00:00
|
|
|
if (count)
|
|
|
|
{
|
|
|
|
data_fmt = cget(&h, 0);
|
|
|
|
|
|
|
|
scsireq->databuf = malloc(count);
|
|
|
|
|
|
|
|
if (data_phase == out)
|
|
|
|
{
|
1995-04-17 14:35:07 +00:00
|
|
|
if (strcmp(data_fmt, "-") == 0) /* Read data from stdin */
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
|
|
|
if (read(0, scsireq->databuf, count) != count)
|
|
|
|
{
|
|
|
|
perror("read");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
else
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
1995-04-17 14:35:07 +00:00
|
|
|
bzero(scsireq->databuf, count);
|
|
|
|
scsireq_encode_visit(scsireq, data_fmt, iget, (void *)&h);
|
1995-01-24 12:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-01 12:35:05 +00:00
|
|
|
|
|
|
|
scsireq->timeout = seconds * 1000;
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
if (scsireq_enter(fd, scsireq) == -1)
|
|
|
|
{
|
|
|
|
scsi_debug(stderr, -1, scsireq);
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SCSIREQ_ERROR(scsireq))
|
|
|
|
scsi_debug(stderr, 0, scsireq);
|
1995-01-24 12:07:27 +00:00
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
if (count && data_phase == in)
|
|
|
|
{
|
|
|
|
if (strcmp(data_fmt, "-") == 0) /* stdout */
|
|
|
|
{
|
|
|
|
if (write(1, scsireq->databuf, count) != count)
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
1995-04-28 19:24:39 +00:00
|
|
|
perror("write");
|
1995-01-24 12:07:27 +00:00
|
|
|
exit(errno);
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
scsireq_decode_visit(scsireq, data_fmt, arg_put, 0);
|
|
|
|
putchar('\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-01-24 12:07:27 +00:00
|
|
|
|
1995-05-01 12:35:05 +00:00
|
|
|
static void
|
|
|
|
freeze_ioctl(int fd, int op, void *data)
|
1995-04-28 19:24:39 +00:00
|
|
|
{
|
|
|
|
if (ioctl(fd, SCIOCFREEZE, 0) == -1) {
|
|
|
|
if (errno == ENODEV) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"Your kernel must be configured with option SCSI_FREEZE.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
perror("SCIOCFREEZE");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
}
|
1995-04-17 14:35:07 +00:00
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
/* do_freeze: Freeze the bus for a given number of seconds.
|
|
|
|
*/
|
|
|
|
static void do_freeze(int seconds)
|
|
|
|
{
|
|
|
|
if (seconds == -1) {
|
|
|
|
printf("Hit return to thaw: ");
|
|
|
|
fflush(stdout);
|
|
|
|
sync();
|
|
|
|
|
|
|
|
freeze_ioctl(fd, SCIOCFREEZE, 0);
|
|
|
|
|
|
|
|
(void)getchar();
|
|
|
|
|
|
|
|
freeze_ioctl(fd, SCIOCTHAW, 0);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sync();
|
|
|
|
freeze_ioctl(fd, SCIOCFREEZETHAW, &seconds);
|
|
|
|
if (verbose) {
|
|
|
|
putchar('\007');
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeze_ioctl(fd, SCIOCWAITTHAW, 0);
|
|
|
|
if (verbose) {
|
|
|
|
putchar('\007');
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mode_sense(int fd, u_char *data, int len, int pc, int page)
|
|
|
|
{
|
|
|
|
scsireq_t *scsireq;
|
|
|
|
|
|
|
|
bzero(data, len);
|
|
|
|
|
|
|
|
scsireq = scsireq_new();
|
|
|
|
|
|
|
|
if (scsireq_enter(fd, scsireq_build(scsireq,
|
|
|
|
len, data, SCCMD_READ,
|
|
|
|
"1A 0 v:2 {Page Control} v:6 {Page Code} 0 v:i1 {Allocation Length} 0",
|
|
|
|
pc, page, len)) == -1) /* Mode sense */
|
|
|
|
{
|
|
|
|
scsi_debug(stderr, -1, scsireq);
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SCSIREQ_ERROR(scsireq))
|
|
|
|
{
|
|
|
|
scsi_debug(stderr, 0, scsireq);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(scsireq);
|
|
|
|
}
|
|
|
|
|
1995-05-05 20:42:00 +00:00
|
|
|
void mode_select(int fd, u_char *data, int len, int perm)
|
|
|
|
{
|
|
|
|
scsireq_t *scsireq;
|
|
|
|
|
|
|
|
scsireq = scsireq_new();
|
|
|
|
|
|
|
|
if (scsireq_enter(fd, scsireq_build(scsireq,
|
|
|
|
len, data, SCCMD_WRITE,
|
|
|
|
"15 0:7 v:1 {SP} 0 0 v:i1 {Allocation Length} 0", perm, len)) == -1) /* Mode select */
|
|
|
|
{
|
|
|
|
scsi_debug(stderr, -1, scsireq);
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (SCSIREQ_ERROR(scsireq))
|
|
|
|
{
|
|
|
|
scsi_debug(stderr, 0, scsireq);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(scsireq);
|
|
|
|
}
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
|
|
|
|
#define START_ENTRY '{'
|
|
|
|
#define END_ENTRY '}'
|
|
|
|
|
|
|
|
static void
|
|
|
|
skipwhite(FILE *f)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
|
|
|
skip_again:
|
|
|
|
|
|
|
|
while (isspace(c = getc(f)))
|
|
|
|
;
|
|
|
|
|
|
|
|
if (c == '#') {
|
|
|
|
while ((c = getc(f)) != '\n' && c != EOF)
|
|
|
|
;
|
|
|
|
goto skip_again;
|
|
|
|
}
|
|
|
|
|
|
|
|
ungetc(c, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mode_lookup: Lookup a format description for a given page.
|
|
|
|
*/
|
|
|
|
char *mode_db = "/usr/share/misc/scsi_modes";
|
|
|
|
static char *mode_lookup(int page)
|
|
|
|
{
|
|
|
|
char *new_db;
|
|
|
|
FILE *modes;
|
|
|
|
int match, next, found, c;
|
|
|
|
static char fmt[1024]; /* XXX This should be with strealloc */
|
|
|
|
int page_desc;
|
|
|
|
new_db = getenv("SCSI_MODES");
|
|
|
|
|
|
|
|
if (new_db)
|
|
|
|
mode_db = new_db;
|
|
|
|
|
|
|
|
modes = fopen(mode_db, "r");
|
|
|
|
if (modes == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
next = 0;
|
|
|
|
found = 0;
|
|
|
|
|
|
|
|
while (!found) {
|
|
|
|
|
|
|
|
skipwhite(modes);
|
|
|
|
|
|
|
|
if (fscanf(modes, "%i", &page_desc) != 1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (page_desc == page)
|
|
|
|
found = 1;
|
|
|
|
|
|
|
|
skipwhite(modes);
|
|
|
|
if (getc(modes) != START_ENTRY) {
|
|
|
|
fprintf(stderr, "Expected %c.\n", START_ENTRY);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
match = 1;
|
|
|
|
while (match != 0) {
|
|
|
|
c = getc(modes);
|
|
|
|
if (c == EOF) {
|
|
|
|
fprintf(stderr, "Expected %c.\n", END_ENTRY);
|
|
|
|
}
|
1995-05-30 06:12:45 +00:00
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
if (c == START_ENTRY) {
|
|
|
|
match++;
|
|
|
|
}
|
|
|
|
if (c == END_ENTRY) {
|
|
|
|
match--;
|
|
|
|
if (match == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (found && c != '\n') {
|
|
|
|
if (next >= sizeof(fmt)) {
|
|
|
|
fprintf(stderr, "Stupid program: Buffer overflow.\n");
|
|
|
|
exit(ENOMEM);
|
1995-01-24 12:07:27 +00:00
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
|
|
|
|
fmt[next++] = (u_char)c;
|
1995-01-24 12:07:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
fmt[next] = 0;
|
|
|
|
|
|
|
|
return (found) ? fmt : 0;
|
|
|
|
}
|
|
|
|
|
1995-05-05 20:42:00 +00:00
|
|
|
/* -------- edit: Mode Select Editor ---------
|
|
|
|
*/
|
|
|
|
struct editinfo
|
|
|
|
{
|
|
|
|
int can_edit;
|
|
|
|
int default_value;
|
|
|
|
} editinfo[64]; /* XXX Bogus fixed size */
|
|
|
|
|
|
|
|
static int editind;
|
1995-07-11 09:21:33 +00:00
|
|
|
volatile int edit_opened;
|
1995-05-05 20:42:00 +00:00
|
|
|
static FILE *edit_file;
|
|
|
|
static char edit_name[L_tmpnam];
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
edit_rewind(void)
|
|
|
|
{
|
|
|
|
editind = 0;
|
|
|
|
}
|
|
|
|
|
1995-07-11 09:21:33 +00:00
|
|
|
static void
|
|
|
|
edit_done(void)
|
|
|
|
{
|
|
|
|
int opened;
|
|
|
|
|
|
|
|
sigset_t all, prev;
|
|
|
|
sigfillset(&all);
|
|
|
|
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &all, &prev);
|
|
|
|
|
|
|
|
opened = (int)edit_opened;
|
|
|
|
edit_opened = 0;
|
|
|
|
|
|
|
|
(void)sigprocmask(SIG_SETMASK, &prev, 0);
|
|
|
|
|
|
|
|
if (opened)
|
|
|
|
{
|
|
|
|
if (fclose(edit_file))
|
|
|
|
perror(edit_name);
|
|
|
|
if (unlink(edit_name))
|
|
|
|
perror(edit_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-05-05 20:42:00 +00:00
|
|
|
static void
|
|
|
|
edit_init(void)
|
|
|
|
{
|
|
|
|
edit_rewind();
|
|
|
|
if (tmpnam(edit_name) == 0) {
|
|
|
|
perror("tmpnam failed");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
if ( (edit_file = fopen(edit_name, "w")) == 0) {
|
|
|
|
perror(edit_name);
|
|
|
|
exit(errno);
|
|
|
|
}
|
1995-07-11 09:21:33 +00:00
|
|
|
edit_opened = 1;
|
|
|
|
|
|
|
|
atexit(edit_done);
|
1995-05-05 20:42:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
edit_check(void *hook, int letter, void *arg, int count, char *name)
|
|
|
|
{
|
|
|
|
if (letter != 'i' && letter != 'b') {
|
|
|
|
fprintf(stderr, "Can't edit format %c.\n", letter);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (editind >= sizeof(editinfo) / sizeof(editinfo[0])) {
|
|
|
|
fprintf(stderr, "edit table overflow\n");
|
|
|
|
exit(ENOMEM);
|
|
|
|
}
|
|
|
|
editinfo[editind].can_edit = ((int)arg != 0);
|
|
|
|
editind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
edit_defaults(void *hook, int letter, void *arg, int count, char *name)
|
|
|
|
{
|
|
|
|
if (letter != 'i' && letter != 'b') {
|
|
|
|
fprintf(stderr, "Can't edit format %c.\n", letter);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
editinfo[editind].default_value = ((int)arg);
|
|
|
|
editind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
edit_report(void *hook, int letter, void *arg, int count, char *name)
|
|
|
|
{
|
|
|
|
if (editinfo[editind].can_edit) {
|
|
|
|
if (letter != 'i' && letter != 'b') {
|
|
|
|
fprintf(stderr, "Can't report format %c.\n", letter);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(edit_file, "%s: %d\n", name, (int)arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
editind++;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
edit_get(void *hook, char *name)
|
|
|
|
{
|
|
|
|
struct get_hook *h = (struct get_hook *)hook;
|
|
|
|
int arg = editinfo[editind].default_value;
|
|
|
|
|
|
|
|
if (editinfo[editind].can_edit) {
|
|
|
|
char line[80];
|
|
|
|
if (fgets(line, sizeof(line), edit_file) == 0) {
|
|
|
|
perror("fgets");
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
line[strlen(line) - 1] = 0;
|
|
|
|
|
|
|
|
if (strncmp(name, line, strlen(name)) != 0) {
|
|
|
|
fprintf(stderr, "Expected \"%s\" and read \"%s\"\n",
|
|
|
|
name, line);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
arg = strtoul(line + strlen(name) + 2, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
editind++;
|
|
|
|
return arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
edit_edit(void)
|
|
|
|
{
|
|
|
|
char *system_line;
|
|
|
|
char *editor = getenv("EDITOR");
|
|
|
|
if (!editor)
|
|
|
|
editor = "vi";
|
|
|
|
|
|
|
|
fclose(edit_file);
|
|
|
|
|
|
|
|
system_line = malloc(strlen(editor) + strlen(edit_name) + 6);
|
|
|
|
sprintf(system_line, "%s %s", editor, edit_name);
|
|
|
|
system(system_line);
|
|
|
|
free(system_line);
|
|
|
|
|
|
|
|
if ( (edit_file = fopen(edit_name, "r")) == 0) {
|
|
|
|
perror(edit_name);
|
|
|
|
exit(errno);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
mode_edit(int fd, int page, int edit, int argc, char *argv[])
|
1995-04-28 19:24:39 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u_char data[255];
|
1995-05-05 20:42:00 +00:00
|
|
|
u_char *mode_pars;
|
|
|
|
struct mode_header
|
|
|
|
{
|
|
|
|
u_char mdl; /* Mode data length */
|
|
|
|
u_char medium_type;
|
|
|
|
u_char dev_spec_par;
|
|
|
|
u_char bdl; /* Block descriptor length */
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mode_page_header
|
|
|
|
{
|
|
|
|
u_char page_code;
|
|
|
|
u_char page_length;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mode_header *mh;
|
|
|
|
struct mode_page_header *mph;
|
1995-04-28 19:24:39 +00:00
|
|
|
|
|
|
|
char *fmt = mode_lookup(page);
|
|
|
|
if (!fmt && verbose) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"No mode data base entry in \"%s\" for page %d; binary %s only.\n",
|
|
|
|
mode_db, page, (edit ? "edit" : "display"));
|
|
|
|
}
|
|
|
|
|
1995-05-05 20:42:00 +00:00
|
|
|
if (edit) {
|
|
|
|
if (!fmt) {
|
|
|
|
fprintf(stderr, "Sorry: can't edit without a format.\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pagectl != 0 && pagectl != 3) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"It only makes sense to edit page 0 (current) or page 3 (saved values)\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
verbose = 1;
|
|
|
|
|
|
|
|
mode_sense(fd, data, sizeof(data), 1, page);
|
|
|
|
|
|
|
|
mh = (struct mode_header *)data;
|
|
|
|
mph = (struct mode_page_header *)
|
|
|
|
(((char *)mh) + sizeof(*mh) + mh->bdl);
|
|
|
|
|
|
|
|
mode_pars = (char *)mph + sizeof(*mph);
|
|
|
|
|
|
|
|
edit_init();
|
|
|
|
scsireq_buff_decode_visit(mode_pars, mh->mdl,
|
|
|
|
fmt, edit_check, 0);
|
|
|
|
|
|
|
|
mode_sense(fd, data, sizeof(data), 0, page);
|
|
|
|
|
|
|
|
edit_rewind();
|
|
|
|
scsireq_buff_decode_visit(mode_pars, mh->mdl,
|
|
|
|
fmt, edit_defaults, 0);
|
|
|
|
|
|
|
|
edit_rewind();
|
|
|
|
scsireq_buff_decode_visit(mode_pars, mh->mdl,
|
|
|
|
fmt, edit_report, 0);
|
|
|
|
|
|
|
|
edit_edit();
|
|
|
|
|
|
|
|
edit_rewind();
|
1995-05-30 06:12:45 +00:00
|
|
|
scsireq_buff_encode_visit(mode_pars, mh->mdl,
|
1995-05-05 20:42:00 +00:00
|
|
|
fmt, edit_get, 0);
|
|
|
|
|
|
|
|
/* Eliminate block descriptors:
|
|
|
|
*/
|
|
|
|
bcopy((char *)mph, ((char *)mh) + sizeof(*mh),
|
|
|
|
sizeof(*mph) + mph->page_length);
|
|
|
|
|
|
|
|
mh->bdl = 0;
|
|
|
|
mph = (struct mode_page_header *) (((char *)mh) + sizeof(*mh));
|
|
|
|
mode_pars = ((char *)mph) + 2;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
/* Turn this on to see what you're sending to the
|
|
|
|
* device:
|
|
|
|
*/
|
|
|
|
edit_rewind();
|
|
|
|
scsireq_buff_decode_visit(mode_pars,
|
|
|
|
mh->mdl, fmt, arg_put, 0);
|
|
|
|
#endif
|
|
|
|
|
1995-07-11 09:21:33 +00:00
|
|
|
edit_done();
|
|
|
|
|
1995-05-05 20:42:00 +00:00
|
|
|
/* Make it permanent if pageselect is three.
|
|
|
|
*/
|
|
|
|
|
|
|
|
mph->page_code &= ~0xC0; /* Clear PS and RESERVED */
|
|
|
|
mh->mdl = 0; /* Reserved for mode select */
|
|
|
|
|
|
|
|
mode_select(fd, (char *)mh,
|
|
|
|
sizeof(*mh) + mh->bdl + sizeof(*mph) + mph->page_length,
|
|
|
|
(pagectl == 3));
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
|
|
|
|
mode_sense(fd, data, sizeof(data), pagectl, page);
|
|
|
|
|
|
|
|
/* Skip over the block descriptors.
|
|
|
|
*/
|
1995-05-05 20:42:00 +00:00
|
|
|
mh = (struct mode_header *)data;
|
|
|
|
mph = (struct mode_page_header *)(((char *)mh) + sizeof(*mh) + mh->bdl);
|
|
|
|
mode_pars = (char *)mph + sizeof(*mph);
|
1995-04-28 19:24:39 +00:00
|
|
|
|
|
|
|
if (!fmt) {
|
1995-05-05 20:42:00 +00:00
|
|
|
for (i = 0; i < mh->mdl; i++) {
|
|
|
|
printf("%02x%c",mode_pars[i],
|
1995-04-28 19:24:39 +00:00
|
|
|
(((i + 1) % 8) == 0) ? '\n' : ' ');
|
|
|
|
}
|
|
|
|
putc('\n', stdout);
|
|
|
|
} else {
|
|
|
|
verbose = 1;
|
1995-05-05 20:42:00 +00:00
|
|
|
scsireq_buff_decode_visit(mode_pars,
|
|
|
|
mh->mdl, fmt, arg_put, 0);
|
1995-04-28 19:24:39 +00:00
|
|
|
}
|
1995-01-24 12:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do_probe_all: Loop over all SCSI IDs and see if something is
|
|
|
|
* there. This only does BUS 0 LUN 0.
|
|
|
|
*/
|
1995-04-28 19:24:39 +00:00
|
|
|
void do_probe_all(void)
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
|
|
|
scsireq_t *scsireq;
|
|
|
|
|
|
|
|
char vendor_id[8 + 1], product_id[16 + 1], revision[4 + 1];
|
|
|
|
int id;
|
|
|
|
u_char *inq_buf = malloc(96);
|
|
|
|
struct scsi_addr addr;
|
|
|
|
|
|
|
|
scsireq = scsireq_build(scsireq_new(),
|
|
|
|
96, inq_buf, SCCMD_READ,
|
|
|
|
"12 0 0 0 v 0", 96);
|
|
|
|
|
|
|
|
addr.scbus = (bus == -1) ? 0 : bus;
|
|
|
|
addr.lun = lun;
|
|
|
|
|
|
|
|
if (addr.scbus || addr.lun)
|
|
|
|
{
|
|
|
|
printf("For bus %d lun %d:\n", addr.scbus, addr.lun);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (id = 0; id < 8; id++)
|
|
|
|
{
|
|
|
|
addr.target = id;
|
|
|
|
|
|
|
|
printf("%d: ", id);
|
|
|
|
if (ioctl(fd, SCIOCADDR, &addr) == -1) {
|
|
|
|
if (errno == ENXIO)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
printf("nothing.\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
printf("SCIOCADDR: %s\n", strerror(errno));
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scsireq_enter(fd, scsireq) == -1) {
|
|
|
|
printf("scsireq_enter: %s\n", strerror(errno));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
vendor_id[sizeof(vendor_id) - 1] = 0;
|
|
|
|
product_id[sizeof(product_id) - 1] = 0;
|
|
|
|
revision[sizeof(revision) - 1] = 0;
|
|
|
|
|
|
|
|
scsireq_decode(scsireq, "s8 c8 c16 c4",
|
|
|
|
vendor_id, product_id, revision);
|
|
|
|
|
|
|
|
printf("%s %s %s\n", vendor_id, product_id, revision);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
void main(int argc, char **argv)
|
1995-01-24 12:07:27 +00:00
|
|
|
{
|
|
|
|
struct scsi_addr scaddr;
|
|
|
|
|
|
|
|
procargs(&argc,&argv);
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
/* XXX This has grown to the point that it should be cleaned up.
|
|
|
|
*/
|
|
|
|
if (freeze) {
|
|
|
|
do_freeze(freeze);
|
|
|
|
} else if (probe_all) {
|
1995-01-24 12:07:27 +00:00
|
|
|
do_probe_all();
|
1995-04-28 19:24:39 +00:00
|
|
|
} else if(reprobe) {
|
1995-01-24 12:07:27 +00:00
|
|
|
scaddr.scbus = bus;
|
|
|
|
scaddr.target = targ;
|
1995-05-30 06:12:45 +00:00
|
|
|
scaddr.lun = lun;
|
1995-01-24 12:07:27 +00:00
|
|
|
|
|
|
|
if (ioctl(fd,SCIOCREPROBE,&scaddr) == -1)
|
|
|
|
perror("ioctl");
|
1995-04-28 19:24:39 +00:00
|
|
|
} else if(debugflag) {
|
1995-01-24 12:07:27 +00:00
|
|
|
if (ioctl(fd,SCIOCDEBUG,&debuglevel) == -1)
|
|
|
|
{
|
|
|
|
perror("ioctl [SCIODEBUG]");
|
|
|
|
exit(1);
|
|
|
|
}
|
1995-04-28 19:24:39 +00:00
|
|
|
} else if (commandflag) {
|
|
|
|
int i;
|
1995-01-24 12:07:27 +00:00
|
|
|
char *fmt;
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
if (argc < 1) {
|
1995-01-24 12:07:27 +00:00
|
|
|
fprintf(stderr, "Need the command format string.\n");
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1995-04-28 19:24:39 +00:00
|
|
|
|
1995-01-24 12:07:27 +00:00
|
|
|
fmt = argv[0];
|
|
|
|
|
|
|
|
argc -= 1;
|
|
|
|
argv += 1;
|
|
|
|
|
|
|
|
do_cmd(fd, fmt, argc, argv);
|
1995-04-28 19:24:39 +00:00
|
|
|
} else if (modeflag) {
|
|
|
|
mode_edit(fd, modepage, editflag, argc, argv);
|
1995-01-24 12:07:27 +00:00
|
|
|
}
|
1995-07-30 12:58:47 +00:00
|
|
|
exit(0);
|
1995-01-24 12:07:27 +00:00
|
|
|
}
|