#include <sys/queue.h>
#include <sys/types.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sysexits.h>
#include <unistd.h>
#include <bus/cam/scsi/scsi_all.h>
#include <bus/cam/cam.h>
#include <bus/cam/cam_ccb.h>
#include <camlib.h>
#include "camcontrol.h"
#define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes"
#define DEFAULT_EDITOR "vi"
#define MAX_FORMAT_SPEC 4096
#define MAX_PAGENUM_LEN 10
#define MAX_PAGENAME_LEN 64
#define PAGEDEF_START '{'
#define PAGEDEF_END '}'
#define PAGENAME_START '"'
#define PAGENAME_END '"'
#define PAGEENTRY_END ';'
#define MAX_COMMAND_SIZE 255
#define PAGE_CTRL_SHIFT 6
#define MODE_PAGE_HEADER(mh) \
(struct scsi_mode_page_header *)find_mode_page_6(mh)
#define MODE_PAGE_DATA(mph) \
(u_int8_t *)(mph) + sizeof(struct scsi_mode_page_header)
struct editentry {
STAILQ_ENTRY(editentry) link;
char *name;
char type;
int editable;
int size;
union {
int ivalue;
char *svalue;
} value;
};
STAILQ_HEAD(, editentry) editlist;
int editlist_changed = 0;
struct pagename {
SLIST_ENTRY(pagename) link;
int pagenum;
char *name;
};
SLIST_HEAD(, pagename) namelist;
static char format[MAX_FORMAT_SPEC];
static FILE *edit_file = NULL;
static char edit_path[] = "/tmp/camXXXXXX";
static void editentry_create(void *, int, void *, int, char *);
static void editentry_update(void *, int, void *, int, char *);
static int editentry_save(void *, char *);
static struct editentry *editentry_lookup(char *);
static int editentry_set(char *, char *, int);
static void editlist_populate(struct cam_device *, int, int, int,
int, int);
static void editlist_save(struct cam_device *, int, int, int, int,
int);
static void nameentry_create(int, char *);
static struct pagename *nameentry_lookup(int);
static int load_format(const char *, int);
static int modepage_write(FILE *, int);
static int modepage_read(FILE *);
static void modepage_edit(void);
static void modepage_dump(struct cam_device *, int, int, int, int,
int);
static void cleanup_editfile(void);
#define returnerr(code) do { \
errno = code; \
return (-1); \
} while (0)
#define RTRIM(string) do { \
int _length; \
while (isspace(string[_length = strlen(string) - 1])) \
string[_length] = '\0'; \
} while (0)
static void
editentry_create(void *hook __unused, int letter, void *arg, int count,
char *name)
{
struct editentry *newentry;
if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
(newentry->name = strdup(name)) == NULL)
err(EX_OSERR, NULL);
RTRIM(newentry->name);
newentry->editable = (arg != NULL);
newentry->type = letter;
newentry->size = count;
newentry->value.svalue = NULL;
STAILQ_INSERT_TAIL(&editlist, newentry, link);
}
static void
editentry_update(void *hook __unused, int letter, void *arg, int count,
char *name)
{
struct editentry *dest;
dest = editentry_lookup(name);
assert(dest != NULL);
dest->type = letter;
dest->size = count;
switch (dest->type) {
case 'i':
case 'b':
case 't':
dest->value.ivalue = (intptr_t)arg;
break;
case 'c':
case 'z':
editentry_set(name, (char *)arg, 0);
break;
default:
;
}
}
static int
editentry_save(void *hook __unused, char *name)
{
struct editentry *src;
src = editentry_lookup(name);
assert(src != NULL);
switch (src->type) {
case 'i':
case 'b':
case 't':
return (src->value.ivalue);
case 'c':
case 'z':
return ((intptr_t)src->value.svalue);
default:
;
}
return (0);
}
static struct editentry *
editentry_lookup(char *name)
{
struct editentry *scan;
assert(name != NULL);
STAILQ_FOREACH(scan, &editlist, link) {
if (strcasecmp(scan->name, name) == 0)
return (scan);
}
return (NULL);
}
static int
editentry_set(char *name, char *newvalue, int editonly)
{
struct editentry *dest;
char *cval;
char *convertend;
int ival;
int resolution;
int resolution_max;
assert(newvalue != NULL);
if (*newvalue == '\0')
return (0);
if ((dest = editentry_lookup(name)) == NULL)
returnerr(ENOENT);
if (!dest->editable && editonly)
returnerr(EPERM);
switch (dest->type) {
case 'i':
case 'b':
case 't':
resolution = (dest->type == 'i')? 8: 1;
ival = (int)strtol(newvalue, &convertend, 0);
if (*convertend != '\0')
returnerr(EINVAL);
if (resolution * dest->size == 32)
resolution_max = 0xffffffff;
else
resolution_max = (1 << (resolution * dest->size)) - 1;
if (ival > resolution_max || ival < 0) {
int newival = (ival < 0) ? 0 : resolution_max;
warnx("value %d is out of range for entry %s; clipping "
"to %d", ival, name, newival);
ival = newival;
}
if (dest->value.ivalue != ival)
editlist_changed = 1;
dest->value.ivalue = ival;
break;
case 'c':
case 'z':
if ((cval = malloc(dest->size + 1)) == NULL)
err(EX_OSERR, NULL);
bzero(cval, dest->size + 1);
strncpy(cval, newvalue, dest->size);
if (dest->type == 'z') {
char *conv_end;
for (conv_end = cval + dest->size;
conv_end >= cval; conv_end--) {
if (*conv_end == ' ')
*conv_end = '\0';
else if (*conv_end != '\0')
break;
}
}
if (strncmp(dest->value.svalue, cval, dest->size) == 0) {
free(cval);
break;
}
if (dest->value.svalue != NULL) {
free(dest->value.svalue);
dest->value.svalue = NULL;
}
dest->value.svalue = cval;
editlist_changed = 1;
break;
default:
;
}
return (0);
}
static void
nameentry_create(int pagenum, char *name) {
struct pagename *newentry;
if (pagenum < 0 || name == NULL || name[0] == '\0')
return;
if ((newentry = malloc(sizeof(struct pagename))) == NULL ||
(newentry->name = strdup(name)) == NULL)
err(EX_OSERR, NULL);
RTRIM(newentry->name);
newentry->pagenum = pagenum;
SLIST_INSERT_HEAD(&namelist, newentry, link);
}
static struct pagename *
nameentry_lookup(int pagenum) {
struct pagename *scan;
SLIST_FOREACH(scan, &namelist, link) {
if (pagenum == scan->pagenum)
return (scan);
}
return (NULL);
}
static int
load_format(const char *pagedb_path, int page)
{
FILE *pagedb;
char str_pagenum[MAX_PAGENUM_LEN];
char str_pagename[MAX_PAGENAME_LEN];
int pagenum;
int depth;
int found;
int lineno;
enum { LOCATE, PAGENAME, PAGEDEF } state;
int cc;
char c;
#define SETSTATE_LOCATE do { \
str_pagenum[0] = '\0'; \
str_pagename[0] = '\0'; \
pagenum = -1; \
state = LOCATE; \
} while (0)
#define SETSTATE_PAGENAME do { \
str_pagename[0] = '\0'; \
state = PAGENAME; \
} while (0)
#define SETSTATE_PAGEDEF do { \
format[0] = '\0'; \
state = PAGEDEF; \
} while (0)
#define UPDATE_LINENO do { \
if (c == '\n') \
lineno++; \
} while (0)
#define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer))
if ((pagedb = fopen(pagedb_path, "r")) == NULL)
returnerr(ENOENT);
SLIST_INIT(&namelist);
depth = 0;
lineno = 0;
found = 0;
SETSTATE_LOCATE;
while ((cc = fgetc(pagedb)) != EOF) {
c = (char)cc;
UPDATE_LINENO;
if (c == '#') {
do {
cc = fgetc(pagedb);
} while (cc != '\n' && cc != EOF);
c = (char)cc;
UPDATE_LINENO;
continue;
}
if (c == '\n')
continue;
if (c == PAGEDEF_START)
depth++;
else if (c == PAGEDEF_END) {
depth--;
if (depth < 0) {
errx(EX_OSFILE, "%s:%d: %s", pagedb_path,
lineno, "mismatched bracket");
}
}
switch (state) {
case LOCATE:
if (isspace(c)) {
break;
} else if (depth == 0 && c == PAGEENTRY_END) {
pagenum = strtol(str_pagenum, NULL, 0);
nameentry_create(pagenum, str_pagename);
SETSTATE_LOCATE;
} else if (depth == 0 && c == PAGENAME_START) {
SETSTATE_PAGENAME;
} else if (c == PAGEDEF_START) {
pagenum = strtol(str_pagenum, NULL, 0);
if (depth == 1) {
nameentry_create(pagenum, str_pagename);
if (page == pagenum && !found)
SETSTATE_PAGEDEF;
}
} else if (c == PAGEDEF_END) {
SETSTATE_LOCATE;
} else if (depth == 0 && ! BUFFERFULL(str_pagenum)) {
strncat(str_pagenum, &c, 1);
} else if (depth == 0) {
errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
lineno, "page identifier exceeds",
sizeof(str_pagenum) - 1, "characters");
}
break;
case PAGENAME:
if (c == PAGENAME_END) {
state = LOCATE;
} else if (! BUFFERFULL(str_pagename)) {
strncat(str_pagename, &c, 1);
} else {
errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
lineno, "page name exceeds",
sizeof(str_pagenum) - 1, "characters");
}
break;
case PAGEDEF:
if (depth == 0) {
found = 1;
SETSTATE_LOCATE;
} else if (! BUFFERFULL(format)) {
strncat(format, &c, 1);
} else {
errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
lineno, "page definition exceeds",
sizeof(format) - 1, "characters");
}
break;
default:
;
}
}
if (ferror(pagedb))
err(EX_OSFILE, "%s", pagedb_path);
fclose(pagedb);
if (!found)
returnerr(ESRCH);
return (0);
}
static void
editlist_populate(struct cam_device *device, int modepage, int page_control,
int dbd, int retries, int timeout)
{
u_int8_t data[MAX_COMMAND_SIZE];
u_int8_t *mode_pars;
struct scsi_mode_header_6 *mh;
struct scsi_mode_page_header *mph;
STAILQ_INIT(&editlist);
mode_sense(device, modepage, 1, dbd, retries, timeout, data,
sizeof(data));
mh = (struct scsi_mode_header_6 *)data;
mph = MODE_PAGE_HEADER(mh);
mode_pars = MODE_PAGE_DATA(mph);
buff_decode_visit(mode_pars, mh->data_length, format,
editentry_create, 0);
mode_sense(device, modepage, page_control, dbd, retries, timeout, data,
sizeof(data));
buff_decode_visit(mode_pars, mh->data_length, format,
editentry_update, 0);
}
static void
editlist_save(struct cam_device *device, int modepage, int page_control,
int dbd, int retries, int timeout)
{
u_int8_t data[MAX_COMMAND_SIZE];
u_int8_t *mode_pars;
struct scsi_mode_header_6 *mh;
struct scsi_mode_page_header *mph;
if (! editlist_changed)
return;
mode_sense(device, modepage, page_control, dbd, retries, timeout, data,
sizeof(data));
mh = (struct scsi_mode_header_6 *)data;
mph = MODE_PAGE_HEADER(mh);
mode_pars = MODE_PAGE_DATA(mph);
buff_encode_visit(mode_pars, mh->data_length, format,
editentry_save, 0);
bcopy(mph, ((u_int8_t *)mh) + sizeof(*mh),
sizeof(*mph) + mph->page_length);
mh->blk_desc_len = 0;
mh->dev_spec = 0;
mph = MODE_PAGE_HEADER(mh);
mode_pars = MODE_PAGE_DATA(mph);
mph->page_code &= SMS_PAGE_CODE;
mh->data_length = 0;
mode_select(device,
(page_control << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED),
retries, timeout, (u_int8_t *)mh,
sizeof(*mh) + mh->blk_desc_len + sizeof(*mph) + mph->page_length);
}
static int
modepage_write(FILE *file, int editonly)
{
struct editentry *scan;
int written = 0;
STAILQ_FOREACH(scan, &editlist, link) {
if (scan->editable || !editonly) {
written++;
if (scan->type == 'c' || scan->type == 'z') {
fprintf(file, "%s: %s\n", scan->name,
scan->value.svalue);
} else {
fprintf(file, "%s: %d\n", scan->name,
scan->value.ivalue);
}
}
}
return (written);
}
static int
modepage_read(FILE *file)
{
char *buffer;
char *line;
char *name;
char *value;
size_t length;
#define ABORT_READ(message, param) do { \
warnx(message, param); \
free(buffer); \
returnerr(EAGAIN); \
} while (0)
while ((line = fgetln(file, &length)) != NULL) {
while (length > 0 && isspace(line[length - 1]))
length--;
if ((buffer = malloc(length + 1)) == NULL)
err(EX_OSERR, NULL);
memcpy(buffer, line, length);
buffer[length] = '\0';
if ((value = strchr(buffer, '#')) != NULL)
*value = '\0';
name = buffer;
RTRIM(name);
while (isspace(*name))
name++;
if (strlen(name) == 0)
continue;
if ((value = strrchr(buffer, ':')) == NULL)
ABORT_READ("no value associated with %s", name);
*value = '\0';
value++;
RTRIM(value);
while (isspace(*value))
value++;
if (strlen(value) == 0)
ABORT_READ("no value associated with %s", name);
if (editentry_set(name, value, 1) != 0) {
if (errno == ENOENT) {
ABORT_READ("no such modepage entry \"%s\"",
name);
} else if (errno == EINVAL) {
ABORT_READ("Invalid value for entry \"%s\"",
name);
} else if (errno == ERANGE) {
ABORT_READ("value out of range for %s", name);
} else if (errno == EPERM) {
warnx("modepage entry \"%s\" is read-only; "
"skipping.", name);
}
}
free(buffer);
}
return (ferror(file)? -1: 0);
#undef ABORT_READ
}
static void
modepage_edit(void)
{
const char *editor;
char *commandline;
int fd;
int written;
if (!isatty(fileno(stdin))) {
modepage_read(stdin);
return;
}
if ((editor = getenv("EDITOR")) == NULL)
editor = DEFAULT_EDITOR;
if ((fd = mkstemp(edit_path)) == -1)
errx(EX_CANTCREAT, "mkstemp failed");
atexit(cleanup_editfile);
if ((edit_file = fdopen(fd, "w")) == NULL)
err(EX_NOINPUT, "%s", edit_path);
written = modepage_write(edit_file, 1);
fclose(edit_file);
edit_file = NULL;
if (written == 0) {
warnx("no editable entries");
cleanup_editfile();
return;
}
commandline = malloc(strlen(editor) + strlen(edit_path) + 2);
if (commandline == NULL)
err(EX_OSERR, NULL);
sprintf(commandline, "%s %s", editor, edit_path);
if (system(commandline) == -1)
err(EX_UNAVAILABLE, "could not invoke %s", editor);
free(commandline);
if ((edit_file = fopen(edit_path, "r")) == NULL)
err(EX_NOINPUT, "%s", edit_path);
modepage_read(edit_file);
cleanup_editfile();
}
static void
modepage_dump(struct cam_device *device, int page, int page_control, int dbd,
int retries, int timeout)
{
u_int8_t data[MAX_COMMAND_SIZE];
u_int8_t *mode_pars;
struct scsi_mode_header_6 *mh;
struct scsi_mode_page_header *mph;
int mode_idx;
mode_sense(device, page, page_control, dbd, retries, timeout, data,
sizeof(data));
mh = (struct scsi_mode_header_6 *)data;
mph = MODE_PAGE_HEADER(mh);
mode_pars = MODE_PAGE_DATA(mph);
for (mode_idx = 0; mode_idx < mph->page_length; mode_idx++) {
printf("%02x%c", mode_pars[mode_idx],
(((mode_idx + 1) % 8) == 0) ? '\n' : ' ');
}
putchar('\n');
}
static void
cleanup_editfile(void)
{
if (edit_file == NULL)
return;
if (fclose(edit_file) != 0 || unlink(edit_path) != 0)
warn("%s", edit_path);
edit_file = NULL;
}
void
mode_edit(struct cam_device *device, int page, int page_control, int dbd,
int edit, int binary, int retry_count, int timeout)
{
const char *pagedb_path;
if (edit && binary)
errx(EX_USAGE, "cannot edit in binary mode.");
if (! binary) {
if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
pagedb_path = DEFAULT_SCSI_MODE_DB;
if (load_format(pagedb_path, page) != 0 && (edit || verbose)) {
if (errno == ENOENT) {
warn("cannot open modepage database \"%s\"",
pagedb_path);
} else if (errno == ESRCH) {
warnx("modepage %d not found in database"
"\"%s\"", page, pagedb_path);
}
if (!edit) {
warnx("reverting to binary display only");
binary = 1;
} else
exit(EX_OSFILE);
}
editlist_populate(device, page, page_control, dbd, retry_count,
timeout);
}
if (edit) {
if (page_control << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT &&
page_control << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED)
errx(EX_USAGE, "it only makes sense to edit page 0 "
"(current) or page 3 (saved values)");
modepage_edit();
editlist_save(device, page, page_control, dbd, retry_count,
timeout);
} else if (binary || STAILQ_EMPTY(&editlist)) {
modepage_dump(device, page, page_control, dbd, retry_count,
timeout);
} else {
modepage_write(stdout, 0);
}
}
void
mode_list(struct cam_device *device, int page_control, int dbd,
int retry_count, int timeout)
{
u_int8_t data[MAX_COMMAND_SIZE];
struct scsi_mode_header_6 *mh;
struct scsi_mode_page_header *mph;
struct pagename *nameentry;
const char *pagedb_path;
int len;
if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
pagedb_path = DEFAULT_SCSI_MODE_DB;
if (load_format(pagedb_path, 0) != 0 && verbose && errno == ENOENT) {
warn("cannot open modepage database \"%s\"", pagedb_path);
}
mode_sense(device, SMS_ALL_PAGES_PAGE, page_control, dbd, retry_count,
timeout, data, sizeof(data));
mh = (struct scsi_mode_header_6 *)data;
len = mh->blk_desc_len;
while (len < mh->data_length) {
mph = (struct scsi_mode_page_header *)
((intptr_t)mh + sizeof(*mh) + len);
mph->page_code &= SMS_PAGE_CODE;
nameentry = nameentry_lookup(mph->page_code);
if (nameentry == NULL || nameentry->name == NULL)
printf("0x%02x\n", mph->page_code);
else
printf("0x%02x\t%s\n", mph->page_code,
nameentry->name);
len += mph->page_length + sizeof(*mph);
}
}