Deleted Added
full compact
modeedit.c (56384) modeedit.c (64382)
1/*
2 * Written By Julian ELischer
3 * Copyright julian Elischer 1993.
4 * Permission is granted to use or redistribute this file in any way as long
5 * as this notice remains. Julian Elischer does not guarantee that this file
6 * is totally correct for any given task and users of this file must
7 * accept responsibility for any damage that occurs from the application of this
8 * file.
9 *
10 * (julian@tfs.com julian@dialix.oz.au)
11 *
12 * User SCSI hooks added by Peter Dufault:
13 *
14 * Copyright (c) 1994 HD Associates
15 * (contact: dufault@hda.com)
1/*-
2 * Copyright (c) 2000 Kelly Yancey <kbyanc@posi.net>
3 * Derived from work done by Julian Elischer <julian@tfs.com,
4 * julian@dialix.oz.au>, 1993, and Peter Dufault <dufault@hda.com>, 1994.
16 * All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 * 3. The name of HD Associates
27 * may not be used to endorse or promote products derived from this software
28 * without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY HD ASSOCIATES ``AS IS'' AND
31 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL HD ASSOCIATES BE LIABLE
34 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
36 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
38 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
39 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * SUCH DAMAGE.
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 */
27 */
42/*
43 * Taken from the original scsi(8) program.
44 * from: scsi.c,v 1.17 1998/01/12 07:57:57 charnier Exp $";
45 */
28
46#ifndef lint
47static const char rcsid[] =
29#ifndef lint
30static const char rcsid[] =
48 "$FreeBSD: head/sbin/camcontrol/modeedit.c 56384 2000-01-21 23:19:30Z mjacob $";
31 "$FreeBSD: head/sbin/camcontrol/modeedit.c 64382 2000-08-08 06:24:17Z kbyanc $";
49#endif /* not lint */
50
32#endif /* not lint */
33
34#include <sys/queue.h>
35#include <sys/types.h>
36
37#include <assert.h>
51#include <ctype.h>
52#include <err.h>
53#include <errno.h>
38#include <ctype.h>
39#include <err.h>
40#include <errno.h>
54#include <string.h>
55#include <stdlib.h>
41#include <stdlib.h>
42#include <string.h>
56#include <stdio.h>
43#include <stdio.h>
57#include <sys/file.h>
58#include <signal.h>
44#include <sysexits.h>
59#include <unistd.h>
60
45#include <unistd.h>
46
47#include <cam/scsi/scsi_all.h>
61#include <cam/cam.h>
62#include <cam/cam_ccb.h>
63#include <camlib.h>
64#include "camcontrol.h"
65
66int verbose = 0;
67
48#include <cam/cam.h>
49#include <cam/cam_ccb.h>
50#include <camlib.h>
51#include "camcontrol.h"
52
53int verbose = 0;
54
68/* iget: Integer argument callback
69 */
70int
71iget(void *hook, char *name)
55#define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes"
56#define DEFAULT_EDITOR "vi"
57#define MAX_FORMAT_SPEC 4096 /* Max CDB format specifier. */
58#define MAX_PAGENUM_LEN 10 /* Max characters in page num. */
59#define MAX_PAGENAME_LEN 64 /* Max characters in page name. */
60#define PAGEDEF_START '{' /* Page definition delimiter. */
61#define PAGEDEF_END '}' /* Page definition delimiter. */
62#define PAGENAME_START '"' /* Page name delimiter. */
63#define PAGENAME_END '"' /* Page name delimiter. */
64#define PAGEENTRY_END ';' /* Page entry terminator (optional). */
65#define MAX_COMMAND_SIZE 255 /* Mode/Log sense data buffer size. */
66
67
68/* Macros for working with mode pages. */
69#define MODE_PAGE_HEADER(mh) \
70 (struct scsi_mode_page_header *)find_mode_page_6(mh)
71
72#define MODE_PAGE_DATA(mph) \
73 (u_int8_t *)(mph) + sizeof(struct scsi_mode_page_header)
74
75
76struct editentry {
77 STAILQ_ENTRY(editentry) link;
78 char *name;
79 char type;
80 int editable;
81 int size;
82 union {
83 int ivalue;
84 char *svalue;
85 } value;
86};
87STAILQ_HEAD(, editentry) editlist; /* List of page entries. */
88int editlist_changed = 0; /* Whether any entries were changed. */
89
90struct pagename {
91 SLIST_ENTRY(pagename) link;
92 int pagenum;
93 char *name;
94};
95SLIST_HEAD(, pagename) namelist; /* Page number to name mappings. */
96
97static char format[MAX_FORMAT_SPEC]; /* Buffer for scsi cdb format def. */
98
99static FILE *edit_file = NULL; /* File handle for edit file. */
100static char edit_path[] = "/tmp/camXXXXXX";
101
102
103/* Function prototypes. */
104static void editentry_create(void *hook, int letter, void *arg,
105 int count, char *name);
106static void editentry_update(void *hook, int letter, void *arg,
107 int count, char *name);
108static int editentry_save(void *hook, char *name);
109static struct editentry *editentry_lookup(char *name);
110static int editentry_set(char *name, char *newvalue,
111 int editonly);
112static void editlist_populate(struct cam_device *device,
113 int modepage, int page_control,
114 int dbd, int retries, int timeout);
115static void editlist_save(struct cam_device *device, int modepage,
116 int page_control, int dbd, int retries,
117 int timeout);
118static void nameentry_create(int pagenum, char *name);
119static struct pagename *nameentry_lookup(int pagenum);
120static int load_format(char *pagedb_path, int page);
121static int modepage_write(FILE *file, int editonly);
122static int modepage_read(FILE *file);
123static void modepage_edit(void);
124static void modepage_dump(struct cam_device *device, int page,
125 int page_control, int dbd, int retries,
126 int timeout);
127static void cleanup_editfile(void);
128void mode_edit(struct cam_device *device, int page,
129 int page_control, int dbd, int edit,
130 int binary, int retry_count, int timeout);
131void mode_list(struct cam_device *device, int page_control,
132 int dbd, int retry_count, int timeout);
133
134
135#define returnerr(code) do { \
136 errno = code; \
137 return (-1); \
138} while (0)
139
140
141#define RTRIM(string) do { \
142 register int _length; \
143 while (isspace(string[_length = strlen(string) - 1])) \
144 string[_length] = '\0'; \
145} while (0)
146
147
148static void
149editentry_create(void *hook, int letter, void *arg, int count, char *name)
72{
150{
73 struct get_hook *h = (struct get_hook *)hook;
74 int arg;
151 struct editentry *newentry; /* Buffer to hold new entry. */
75
152
76 if (h->got >= h->argc)
77 {
78 fprintf(stderr, "Expecting an integer argument.\n");
79 usage(0);
80 exit(1);
81 }
82 arg = strtol(h->argv[h->got], 0, 0);
83 h->got++;
153 /* Allocate memory for the new entry and a copy of the entry name. */
154 if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
155 (newentry->name = strdup(name)) == NULL)
156 err(EX_OSERR, NULL);
84
157
85 if (verbose && name && *name)
86 printf("%s: %d\n", name, arg);
158 /* Trim any trailing whitespace for the entry name. */
159 RTRIM(newentry->name);
87
160
88 return arg;
161 newentry->editable = (arg != NULL);
162 newentry->type = letter;
163 newentry->size = count; /* Placeholder; not accurate. */
164 newentry->value.svalue = NULL;
165
166 STAILQ_INSERT_TAIL(&editlist, newentry, link);
89}
90
167}
168
91/* cget: char * argument callback
92 */
93char *
94cget(void *hook, char *name)
169static void
170editentry_update(void *hook, int letter, void *arg, int count, char *name)
95{
171{
96 struct get_hook *h = (struct get_hook *)hook;
97 char *arg;
172 struct editentry *dest; /* Buffer to hold entry to update. */
98
173
99 if (h->got >= h->argc)
100 {
101 fprintf(stderr, "Expecting a character pointer argument.\n");
102 usage(0);
103 exit(1);
174 dest = editentry_lookup(name);
175 assert(dest != NULL);
176
177 dest->type = letter;
178 dest->size = count; /* We get the real size now. */
179
180 switch (dest->type) {
181 case 'i': /* Byte-sized integral type. */
182 case 'b': /* Bit-sized integral types. */
183 case 't':
184 dest->value.ivalue = (intptr_t)arg;
185 break;
186
187 case 'c': /* Character array. */
188 case 'z': /* Null-padded string. */
189 editentry_set(name, (char *)arg, 0);
190 break;
191 default:
192 /* NOTREACHED */
104 }
193 }
105 arg = h->argv[h->got];
106 h->got++;
194}
107
195
108 if (verbose && name)
109 printf("cget: %s: %s", name, arg);
196static int
197editentry_save(void *hook, char *name)
198{
199 struct editentry *src; /* Entry value to save. */
110
200
111 return arg;
201 src = editentry_lookup(name);
202 assert(src != NULL);
203
204 switch (src->type) {
205 case 'i': /* Byte-sized integral type. */
206 case 'b': /* Bit-sized integral types. */
207 case 't':
208 return (src->value.ivalue);
209 /* NOTREACHED */
210
211 case 'c': /* Character array. */
212 case 'z': /* Null-padded string. */
213 return ((intptr_t)src->value.svalue);
214 /* NOTREACHED */
215
216 default:
217 /* NOTREACHED */
218 }
219
220 return (0); /* This should never happen. */
112}
113
221}
222
114/* arg_put: "put argument" callback
115 */
116void
117arg_put(void *hook, int letter, void *arg, int count, char *name)
223static struct editentry *
224editentry_lookup(char *name)
118{
225{
119 if (verbose && name && *name)
120 printf("%s: ", name);
226 struct editentry *scan;
121
227
122 switch(letter)
123 {
124 case 'i':
125 case 'b':
126 printf("%d ", (intptr_t)arg);
127 break;
228 assert(name != NULL);
128
229
129 case 'c':
130 case 'z':
131 {
132 char *p;
230 STAILQ_FOREACH(scan, &editlist, link) {
231 if (strcasecmp(scan->name, name) == 0)
232 return (scan);
233 }
133
234
134 p = malloc(count + 1);
235 /* Not found during list traversal. */
236 return (NULL);
237}
135
238
136 bzero(p, count +1);
137 strncpy(p, (char *)arg, count);
138 if (letter == 'z')
139 {
140 int i;
141 for (i = count - 1; i >= 0; i--)
142 if (p[i] == ' ')
143 p[i] = 0;
144 else
145 break;
146 }
147 printf("%s ", p);
239static int
240editentry_set(char *name, char *newvalue, int editonly)
241{
242 struct editentry *dest; /* Modepage entry to update. */
243 char *cval; /* Pointer to new string value. */
244 char *convertend; /* End-of-conversion pointer. */
245 int ival; /* New integral value. */
246 int resolution; /* Resolution in bits for integer conversion. */
148
247
149 free(p);
248/*
249 * Macro to determine the maximum value of the given size for the current
250 * resolution.
251 * XXX Lovely x86's optimize out the case of shifting by 32 and gcc doesn't
252 * currently workaround it (even for int64's), so we have to kludge it.
253 */
254#define RESOLUTION_MAX(size) ((resolution * (size) == 32)? \
255 0xffffffff: 1 << (resolution * (size)) - 1)
256
257 assert(newvalue != NULL);
258 if (*newvalue == '\0')
259 return (0); /* Nothing to do. */
260
261 if ((dest = editentry_lookup(name)) == NULL)
262 returnerr(ENOENT);
263 if (!dest->editable && editonly)
264 returnerr(EPERM);
265
266 switch (dest->type) {
267 case 'i': /* Byte-sized integral type. */
268 case 'b': /* Bit-sized integral types. */
269 case 't':
270 /* Convert the value string to an integer. */
271 resolution = (dest->type == 'i')? 8: 1;
272 ival = (int)strtol(newvalue, &convertend, 0);
273 if (*convertend != '\0')
274 returnerr(EINVAL);
275 if (ival > RESOLUTION_MAX(dest->size) || ival < 0) {
276 int newival = (ival < 0)? 0: RESOLUTION_MAX(dest->size);
277 warnx("value %d is out of range for entry %s; clipping "
278 "to %d", ival, name, newival);
279 ival = newival;
150 }
280 }
281 if (dest->value.ivalue != ival)
282 editlist_changed = 1;
283 dest->value.ivalue = ival;
284 break;
151
285
286 case 'c': /* Character array. */
287 case 'z': /* Null-padded string. */
288 if ((cval = malloc(dest->size + 1)) == NULL)
289 err(EX_OSERR, NULL);
290 bzero(cval, dest->size + 1);
291 strncpy(cval, newvalue, dest->size);
292 if (dest->type == 'z') {
293 /* Convert trailing spaces to nulls. */
294 char *convertend;
295
296 for (convertend = cval + dest->size;
297 convertend >= cval; convertend--) {
298 if (*convertend == ' ')
299 *convertend = '\0';
300 else if (*convertend != '\0')
301 break;
302 }
303 }
304 if (strncmp(dest->value.svalue, cval, dest->size) == 0) {
305 /* Nothing changed, free the newly allocated string. */
306 free(cval);
307 break;
308 }
309 if (dest->value.svalue != NULL) {
310 /* Free the current string buffer. */
311 free(dest->value.svalue);
312 dest->value.svalue = NULL;
313 }
314 dest->value.svalue = cval;
315 editlist_changed = 1;
152 break;
153
316 break;
317
154 default:
155 printf("Unknown format letter: '%c'\n", letter);
318 default:
319 /* NOTREACHED */
156 }
320 }
157 if (verbose)
158 putchar('\n');
321
322 return (0);
323#undef RESOLUTION_MAX
159}
160
324}
325
161#define START_ENTRY '{'
162#define END_ENTRY '}'
163
164static void
326static void
165skipwhite(FILE *f)
166{
167 int c;
327nameentry_create(int pagenum, char *name) {
328 struct pagename *newentry;
168
329
169skip_again:
330 if (pagenum < 0 || name == NULL || name[0] == '\0')
331 return;
170
332
171 while (isspace(c = getc(f)))
172 ;
333 /* Allocate memory for the new entry and a copy of the entry name. */
334 if ((newentry = malloc(sizeof(struct pagename))) == NULL ||
335 (newentry->name = strdup(name)) == NULL)
336 err(EX_OSERR, NULL);
173
337
174 if (c == '#') {
175 while ((c = getc(f)) != '\n' && c != EOF)
176 ;
177 goto skip_again;
338 /* Trim any trailing whitespace for the page name. */
339 RTRIM(newentry->name);
340
341 newentry->pagenum = pagenum;
342 SLIST_INSERT_HEAD(&namelist, newentry, link);
343}
344
345static struct pagename *
346nameentry_lookup(int pagenum) {
347 struct pagename *scan;
348
349 SLIST_FOREACH(scan, &namelist, link) {
350 if (pagenum == scan->pagenum)
351 return (scan);
178 }
179
352 }
353
180 ungetc(c, f);
354 /* Not found during list traversal. */
355 return (NULL);
181}
182
356}
357
183/* mode_lookup: Lookup a format description for a given page.
184 */
185char *mode_db = "/usr/share/misc/scsi_modes";
186static char *
187mode_lookup(int page)
358static int
359load_format(char *pagedb_path, int page)
188{
360{
189 char *new_db;
190 FILE *modes;
191 int match, next, found, c;
192 static char fmt[4096]; /* XXX This should be with strealloc */
193 int page_desc;
194 new_db = getenv("SCSI_MODES");
361 FILE *pagedb;
362 char str_pagenum[MAX_PAGENUM_LEN];
363 char str_pagename[MAX_PAGENAME_LEN];
364 int pagenum;
365 int depth; /* Quoting depth. */
366 int found;
367 int lineno;
368 enum { LOCATE, PAGENAME, PAGEDEF } state;
369 char c;
195
370
196 if (new_db)
197 mode_db = new_db;
371#define SETSTATE_LOCATE do { \
372 str_pagenum[0] = '\0'; \
373 str_pagename[0] = '\0'; \
374 pagenum = -1; \
375 state = LOCATE; \
376} while (0)
198
377
199 modes = fopen(mode_db, "r");
200 if (modes == 0)
201 return 0;
378#define SETSTATE_PAGENAME do { \
379 str_pagename[0] = '\0'; \
380 state = PAGENAME; \
381} while (0)
202
382
203 next = 0;
204 found = 0;
383#define SETSTATE_PAGEDEF do { \
384 format[0] = '\0'; \
385 state = PAGEDEF; \
386} while (0)
205
387
206 while (!found) {
388#define UPDATE_LINENO do { \
389 if (c == '\n') \
390 lineno++; \
391} while (0)
207
392
208 skipwhite(modes);
393#define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer))
209
394
210 if (fscanf(modes, "%i", &page_desc) != 1)
211 break;
395 if ((pagedb = fopen(pagedb_path, "r")) == NULL)
396 returnerr(ENOENT);
212
397
213 if (page_desc == page)
214 found = 1;
398 SLIST_INIT(&namelist);
215
399
216 skipwhite(modes);
217 if (getc(modes) != START_ENTRY)
218 errx(1, "expected %c", START_ENTRY);
400 depth = 0;
401 lineno = 0;
402 found = 0;
403 SETSTATE_LOCATE;
404 while ((c = fgetc(pagedb)) != EOF) {
219
405
220 match = 1;
221 while (match != 0) {
222 c = getc(modes);
223 if (c == EOF) {
224 warnx("expected %c", END_ENTRY);
406 /* Keep a line count to make error messages more useful. */
407 UPDATE_LINENO;
408
409 /* Skip over comments anywhere in the mode database. */
410 if (c == '#') {
411 do {
412 c = fgetc(pagedb);
413 } while (c != '\n' && c != EOF);
414 UPDATE_LINENO;
415 continue;
416 }
417
418 /* Strip out newline characters. */
419 if (c == '\n')
420 continue;
421
422 /* Keep track of the nesting depth for braces. */
423 if (c == PAGEDEF_START)
424 depth++;
425 else if (c == PAGEDEF_END) {
426 depth--;
427 if (depth < 0) {
428 errx(EX_OSFILE, "%s:%d: %s", pagedb_path,
429 lineno, "mismatched bracket");
225 }
430 }
431 }
226
432
227 if (c == START_ENTRY) {
228 match++;
433 switch (state) {
434 case LOCATE:
435 /*
436 * Locate the page the user is interested in, skipping
437 * all others.
438 */
439 if (isspace(c)) {
440 /* Ignore all whitespace between pages. */
441 break;
442 } else if (depth == 0 && c == PAGEENTRY_END) {
443 /*
444 * A page entry terminator will reset page
445 * scanning (useful for assigning names to
446 * modes without providing a mode definition).
447 */
448 /* Record the name of this page. */
449 pagenum = strtol(str_pagenum, NULL, 0);
450 nameentry_create(pagenum, str_pagename);
451 SETSTATE_LOCATE;
452 } else if (depth == 0 && c == PAGENAME_START) {
453 SETSTATE_PAGENAME;
454 } else if (c == PAGEDEF_START) {
455 pagenum = strtol(str_pagenum, NULL, 0);
456 if (depth == 1) {
457 /* Record the name of this page. */
458 nameentry_create(pagenum, str_pagename);
459 /*
460 * Only record the format if this is
461 * the page we are interested in.
462 */
463 if (page == pagenum && !found)
464 SETSTATE_PAGEDEF;
465 }
466 } else if (c == PAGEDEF_END) {
467 /* Reset the processor state. */
468 SETSTATE_LOCATE;
469 } else if (depth == 0 && ! BUFFERFULL(str_pagenum)) {
470 strncat(str_pagenum, &c, 1);
471 } else if (depth == 0) {
472 errx(EX_OSFILE, "%s:%d: %s %d %s", pagedb_path,
473 lineno, "page identifier exceeds",
474 sizeof(str_pagenum) - 1, "characters");
229 }
475 }
230 if (c == END_ENTRY) {
231 match--;
232 if (match == 0)
233 break;
476 break;
477
478 case PAGENAME:
479 if (c == PAGENAME_END) {
480 /*
481 * Return to LOCATE state without resetting the
482 * page number buffer.
483 */
484 state = LOCATE;
485 } else if (! BUFFERFULL(str_pagename)) {
486 strncat(str_pagename, &c, 1);
487 } else {
488 errx(EX_OSFILE, "%s:%d: %s %d %s", pagedb_path,
489 lineno, "page name exceeds",
490 sizeof(str_pagenum) - 1, "characters");
234 }
491 }
235 if (found && c != '\n') {
236 if (next >= sizeof(fmt))
237 errx(1, "buffer overflow");
492 break;
238
493
239 fmt[next++] = (u_char)c;
494 case PAGEDEF:
495 /*
496 * Transfer the page definition into a format buffer
497 * suitable for use with CDB encoding/decoding routines.
498 */
499 if (depth == 0) {
500 found = 1;
501 SETSTATE_LOCATE;
502 } else if (! BUFFERFULL(format)) {
503 strncat(format, &c, 1);
504 } else {
505 errx(EX_OSFILE, "%s:%d: %s %d %s", pagedb_path,
506 lineno, "page definition exceeds",
507 sizeof(format) - 1, "characters");
240 }
508 }
509 break;
510
511 default:
512 /* NOTREACHED */
241 }
513 }
514
515 /* Repeat processing loop with next character. */
242 }
516 }
243 fmt[next] = 0;
244
517
245 return (found) ? fmt : 0;
246}
518 if (ferror(pagedb))
519 err(EX_OSFILE, "%s", pagedb_path);
247
520
248/* -------- edit: Mode Select Editor ---------
249 */
250struct editinfo
251{
252 int can_edit;
253 int default_value;
254} editinfo[64]; /* XXX Bogus fixed size */
521 /* Close the SCSI page database. */
522 fclose(pagedb);
255
523
256static int editind;
257volatile int edit_opened;
258static FILE *edit_file;
259static char edit_name[L_tmpnam];
524 if (!found) /* Never found a matching page. */
525 returnerr(ESRCH);
260
526
261static inline void
262edit_rewind(void)
263{
264 editind = 0;
527 return (0);
265}
266
267static void
528}
529
530static void
268edit_done(void)
531editlist_populate(struct cam_device *device, int modepage, int page_control,
532 int dbd, int retries, int timeout)
269{
533{
270 int opened;
534 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
535 u_int8_t *mode_pars; /* Pointer to modepage params. */
536 struct scsi_mode_header_6 *mh; /* Location of mode header. */
537 struct scsi_mode_page_header *mph;
271
538
272 sigset_t all, prev;
273 sigfillset(&all);
539 STAILQ_INIT(&editlist);
274
540
275 (void)sigprocmask(SIG_SETMASK, &all, &prev);
541 /* Fetch changeable values; use to build initial editlist. */
542 mode_sense(device, modepage, 1, dbd, retries, timeout, data,
543 sizeof(data));
276
544
277 opened = (int)edit_opened;
278 edit_opened = 0;
545 mh = (struct scsi_mode_header_6 *)data;
546 mph = MODE_PAGE_HEADER(mh);
547 mode_pars = MODE_PAGE_DATA(mph);
279
548
280 (void)sigprocmask(SIG_SETMASK, &prev, 0);
549 /* Decode the value data, creating edit_entries for each value. */
550 buff_decode_visit(mode_pars, mh->data_length, format,
551 editentry_create, 0);
281
552
282 if (opened)
283 {
284 if (fclose(edit_file))
285 warn("%s", edit_name);
286 if (unlink(edit_name))
287 warn("%s", edit_name);
288 }
553 /* Fetch the current/saved values; use to set editentry values. */
554 mode_sense(device, modepage, page_control, dbd, retries, timeout, data,
555 sizeof(data));
556 buff_decode_visit(mode_pars, mh->data_length, format,
557 editentry_update, 0);
289}
290
291static void
558}
559
560static void
292edit_init(void)
561editlist_save(struct cam_device *device, int modepage, int page_control,
562 int dbd, int retries, int timeout)
293{
563{
294 int fd;
564 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
565 u_int8_t *mode_pars; /* Pointer to modepage params. */
566 struct scsi_mode_header_6 *mh; /* Location of mode header. */
567 struct scsi_mode_page_header *mph;
295
568
296 edit_rewind();
297 strlcpy(edit_name, "/tmp/camXXXXXX", sizeof(edit_name));
298 if ((fd = mkstemp(edit_name)) == -1)
299 errx(1, "mkstemp failed");
300 if ((edit_file = fdopen(fd, "w")) == 0)
301 err(1, "%s", edit_name);
302 edit_opened = 1;
569 /* Make sure that something changed before continuing. */
570 if (! editlist_changed)
571 return;
303
572
304 atexit(edit_done);
305}
573 /*
574 * Preload the CDB buffer with the current mode page data.
575 * XXX If buff_encode_visit would return the number of bytes encoded
576 * we *should* use that to build a header from scratch. As it is
577 * now, we need mode_sense to find out the page length.
578 */
579 mode_sense(device, modepage, page_control, dbd, retries, timeout, data,
580 sizeof(data));
306
581
307static void
308edit_check(void *hook, int letter, void *arg, int count, char *name)
309{
310 if (letter != 'i' && letter != 'b')
311 errx(1, "can't edit format %c", letter);
582 /* Initial headers & offsets. */
583 mh = (struct scsi_mode_header_6 *)data;
584 mph = MODE_PAGE_HEADER(mh);
585 mode_pars = MODE_PAGE_DATA(mph);
312
586
313 if (editind >= sizeof(editinfo) / sizeof(editinfo[0]))
314 errx(1, "edit table overflow");
587 /* Encode the value data to be passed back to the device. */
588 buff_encode_visit(mode_pars, mh->data_length, format,
589 editentry_save, 0);
315
590
316 editinfo[editind].can_edit = (arg != NULL);
317 editind++;
318}
591 /* Eliminate block descriptors. */
592 bcopy(mph, ((u_int8_t *)mh) + sizeof(*mh),
593 sizeof(*mph) + mph->page_length);
319
594
320static void
321edit_defaults(void *hook, int letter, void *arg, int count, char *name)
322{
323 if (letter != 'i' && letter != 'b')
324 errx(1, "can't edit format %c", letter);
595 /* Recalculate headers & offsets. */
596 mh->blk_desc_len = 0; /* No block descriptors. */
597 mh->dev_spec = 0; /* Clear device-specific parameters. */
598 mph = MODE_PAGE_HEADER(mh);
599 mode_pars = MODE_PAGE_DATA(mph);
325
600
326 editinfo[editind].default_value = (intptr_t)arg; /* truncated */
327 editind++;
601 mph->page_code &= SMS_PAGE_CODE;/* Isolate just the page code. */
602 mh->data_length = 0; /* Reserved for MODE SELECT command. */
603
604 /*
605 * Write the changes back to the device. If the user editted control
606 * page 3 (saved values) then request the changes be permanently
607 * recorded.
608 */
609 mode_select(device, (page_control == SMS_PAGE_CTRL_SAVED), retries,
610 timeout, (u_int8_t *)mh, sizeof(*mh) + mh->blk_desc_len +
611 sizeof(*mph) + mph->page_length);
328}
329
612}
613
330static void
331edit_report(void *hook, int letter, void *arg, int count, char *name)
614static int
615modepage_write(FILE *file, int editonly)
332{
616{
333 if (editinfo[editind].can_edit) {
334 if (letter != 'i' && letter != 'b')
335 errx(1, "can't report format %c", letter);
617 struct editentry *scan;
618 int written = 0;
336
619
337 fprintf(edit_file, "%s: %d\n", name, (intptr_t)arg);
620 STAILQ_FOREACH(scan, &editlist, link) {
621 if (scan->editable || !editonly) {
622 written++;
623 if (scan->type == 'c' || scan->type == 'z') {
624 fprintf(file, "%s: %s\n", scan->name,
625 scan->value.svalue);
626 } else {
627 fprintf(file, "%s: %d\n", scan->name,
628 scan->value.ivalue);
629 }
630 }
338 }
631 }
339
340 editind++;
632 return (written);
341}
342
343static int
633}
634
635static int
344edit_get(void *hook, char *name)
636modepage_read(FILE *file)
345{
637{
346 int arg = editinfo[editind].default_value;
638 char *buffer; /* Pointer to dynamic line buffer. */
639 char *line; /* Pointer to static fgetln buffer. */
640 char *name; /* Name portion of the line buffer. */
641 char *value; /* Value portion of line buffer. */
642 int length; /* Length of static fgetln buffer. */
347
643
348 if (editinfo[editind].can_edit) {
349 char line[80];
350 if (fgets(line, sizeof(line), edit_file) == 0)
351 err(1, "fgets");
644#define ABORT_READ(message, param) do { \
645 warnx(message, param); \
646 free(buffer); \
647 returnerr(EAGAIN); \
648} while (0)
352
649
353 line[strlen(line) - 1] = 0;
650 while ((line = fgetln(file, &length)) != NULL) {
651 /* Trim trailing whitespace (including optional newline). */
652 while (length > 0 && isspace(line[length - 1]))
653 length--;
354
654
355 if (strncmp(name, line, strlen(name)) != 0)
356 errx(1, "expected \"%s\" and read \"%s\"", name, line);
655 /* Allocate a buffer to hold the line + terminating null. */
656 if ((buffer = malloc(length + 1)) == NULL)
657 err(EX_OSERR, NULL);
658 memcpy(buffer, line, length);
659 buffer[length] = '\0';
357
660
358 arg = strtoul(line + strlen(name) + 2, 0, 0);
661 /* Strip out comments. */
662 if ((value = strchr(buffer, '#')) != NULL)
663 *value = '\0';
664
665 /* The name is first in the buffer. Trim whitespace.*/
666 name = buffer;
667 RTRIM(name);
668 while (isspace(*name))
669 name++;
670
671 /* Skip empty lines. */
672 if (strlen(name) == 0)
673 continue;
674
675 /* The name ends at the colon; the value starts there. */
676 if ((value = strrchr(buffer, ':')) == NULL)
677 ABORT_READ("no value associated with %s", name);
678 *value = '\0'; /* Null-terminate name. */
679 value++; /* Value starts afterwards. */
680
681 /* Trim leading and trailing whitespace. */
682 RTRIM(value);
683 while (isspace(*value))
684 value++;
685
686 /* Make sure there is a value left. */
687 if (strlen(value) == 0)
688 ABORT_READ("no value associated with %s", name);
689
690 /* Update our in-memory copy of the modepage entry value. */
691 if (editentry_set(name, value, 1) != 0) {
692 if (errno == ENOENT) {
693 /* No entry by the name. */
694 ABORT_READ("no such modepage entry \"%s\"",
695 name);
696 } else if (errno == EINVAL) {
697 /* Invalid value. */
698 ABORT_READ("Invalid value for entry \"%s\"",
699 name);
700 } else if (errno == ERANGE) {
701 /* Value out of range for entry type. */
702 ABORT_READ("value out of range for %s", name);
703 } else if (errno == EPERM) {
704 /* Entry is not editable; not fatal. */
705 warnx("modepage entry \"%s\" is read-only; "
706 "skipping.", name);
707 }
708 }
709
710 free(buffer);
359 }
711 }
712 return (ferror(file)? -1: 0);
360
713
361 editind++;
362 return arg;
714#undef ABORT_READ
363}
364
365static void
715}
716
717static void
366edit_edit(void)
718modepage_edit(void)
367{
719{
368 char *system_line;
369 char *editor = getenv("EDITOR");
370 if (!editor)
371 editor = "vi";
720 char *editor;
721 char *commandline;
722 int fd;
723 int written;
372
724
373 fclose(edit_file);
725 if (!isatty(fileno(stdin))) {
726 /* Not a tty, read changes from stdin. */
727 modepage_read(stdin);
728 return;
729 }
374
730
375 system_line = malloc(strlen(editor) + strlen(edit_name) + 6);
376 sprintf(system_line, "%s %s", editor, edit_name);
377 system(system_line);
378 free(system_line);
731 /* Lookup editor to invoke. */
732 if ((editor = getenv("EDITOR")) == NULL)
733 editor = DEFAULT_EDITOR;
379
734
380 if ((edit_file = fopen(edit_name, "r")) == 0)
381 err(1, "%s", edit_name);
382}
735 /* Create temp file for editor to modify. */
736 if ((fd = mkstemp(edit_path)) == -1)
737 errx(EX_CANTCREAT, "mkstemp failed");
383
738
384void
385mode_edit(struct cam_device *device, int page, int page_control, int dbd,
386 int edit, int retry_count, int timeout)
387{
388 int i;
389 u_char data[255];
390 u_char *mode_pars;
391 struct mode_header
392 {
393 u_char mdl; /* Mode data length */
394 u_char medium_type;
395 u_char dev_spec_par;
396 u_char bdl; /* Block descriptor length */
397 };
739 atexit(cleanup_editfile);
398
740
399 struct mode_page_header
400 {
401 u_char page_code;
402 u_char page_length;
403 };
741 if ((edit_file = fdopen(fd, "w")) == NULL)
742 err(EX_NOINPUT, "%s", edit_path);
404
743
405 struct mode_header *mh;
406 struct mode_page_header *mph;
744 written = modepage_write(edit_file, 1);
407
745
408 char *fmt = mode_lookup(page);
409 if (!fmt && verbose) {
410 fprintf(stderr,
411 "No mode data base entry in \"%s\" for page %d; "
412 " binary %s only.\n",
413 mode_db, page, (edit ? "edit" : "display"));
746 fclose(edit_file);
747 edit_file = NULL;
748
749 if (written == 0) {
750 warnx("no editable entries");
751 cleanup_editfile();
752 return;
414 }
415
753 }
754
416 if (edit) {
417 if (!fmt)
418 errx(1, "can't edit without a format");
755 /*
756 * Allocate memory to hold the command line (the 2 extra characters
757 * are to hold the argument separator (a space), and the terminating
758 * null character.
759 */
760 commandline = malloc(strlen(editor) + strlen(edit_path) + 2);
761 if (commandline == NULL)
762 err(EX_OSERR, NULL);
763 sprintf(commandline, "%s %s", editor, edit_path);
419
764
420 if (page_control != 0 && page_control != 3)
421 errx(1, "it only makes sense to edit page 0 "
422 "(current) or page 3 (saved values)");
765 /* Invoke the editor on the temp file. */
766 if (system(commandline) == -1)
767 err(EX_UNAVAILABLE, "could not invoke %s", editor);
768 free(commandline);
423
769
424 verbose = 1;
770 if ((edit_file = fopen(edit_path, "r")) == NULL)
771 err(EX_NOINPUT, "%s", edit_path);
425
772
426 mode_sense(device, page, 1, dbd, retry_count, timeout,
427 data, sizeof(data));
773 /* Read any changes made to the temp file. */
774 modepage_read(edit_file);
428
775
429 mh = (struct mode_header *)data;
430 mph = (struct mode_page_header *)
431 (((char *)mh) + sizeof(*mh) + mh->bdl);
776 cleanup_editfile();
777}
432
778
433 mode_pars = (char *)mph + sizeof(*mph);
779static void
780modepage_dump(struct cam_device *device, int page, int page_control, int dbd,
781 int retries, int timeout)
782{
783 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
784 u_int8_t *mode_pars; /* Pointer to modepage params. */
785 struct scsi_mode_header_6 *mh; /* Location of mode header. */
786 struct scsi_mode_page_header *mph;
787 int index; /* Index for scanning mode params. */
434
788
435 edit_init();
436 buff_decode_visit(mode_pars, mh->mdl, fmt, edit_check, 0);
789 mode_sense(device, page, page_control, dbd, retries, timeout, data,
790 sizeof(data));
437
791
438 mode_sense(device, page, 0, dbd, retry_count, timeout,
439 data, sizeof(data));
792 mh = (struct scsi_mode_header_6 *)data;
793 mph = MODE_PAGE_HEADER(mh);
794 mode_pars = MODE_PAGE_DATA(mph);
440
795
441 edit_rewind();
442 buff_decode_visit(mode_pars, mh->mdl, fmt, edit_defaults, 0);
796 /* Print the raw mode page data with newlines each 8 bytes. */
797 for (index = 0; index < mph->page_length; index++) {
798 printf("%02x%c",mode_pars[index],
799 (((index + 1) % 8) == 0) ? '\n' : ' ');
800 }
801 putchar('\n');
802}
443
803
444 edit_rewind();
445 buff_decode_visit(mode_pars, mh->mdl, fmt, edit_report, 0);
804static void
805cleanup_editfile(void)
806{
807 if (edit_file == NULL)
808 return;
809 if (fclose(edit_file) != 0 || unlink(edit_path) != 0)
810 warn("%s", edit_path);
811 edit_file = NULL;
812}
446
813
447 edit_edit();
814void
815mode_edit(struct cam_device *device, int page, int page_control, int dbd,
816 int edit, int binary, int retry_count, int timeout)
817{
818 char *pagedb_path; /* Path to modepage database. */
448
819
449 edit_rewind();
450 buff_encode_visit(mode_pars, mh->mdl, fmt, edit_get, 0);
820 if (edit && binary)
821 errx(EX_USAGE, "cannot edit in binary mode.");
451
822
452 /* Eliminate block descriptors:
453 */
454 bcopy((char *)mph, ((char *)mh) + sizeof(*mh),
455 sizeof(*mph) + mph->page_length);
823 if (! binary) {
824 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
825 pagedb_path = DEFAULT_SCSI_MODE_DB;
456
826
457 mh->bdl = mh->dev_spec_par = 0;
458 mph = (struct mode_page_header *) (((char *)mh) + sizeof(*mh));
459 mode_pars = ((char *)mph) + 2;
827 if (load_format(pagedb_path, page) != 0 && (edit || verbose)) {
828 if (errno == ENOENT) {
829 /* Modepage database file not found. */
830 warn("cannot open modepage database \"%s\"",
831 pagedb_path);
832 } else if (errno == ESRCH) {
833 /* Modepage entry not found in database. */
834 warnx("modepage %d not found in database"
835 "\"%s\"", page, pagedb_path);
836 }
837 /* We can recover in display mode, otherwise we exit. */
838 if (!edit) {
839 warnx("reverting to binary display only");
840 binary = 1;
841 } else
842 exit(EX_OSFILE);
843 }
460
844
461#if 0
462 /* Turn this on to see what you're sending to the
463 * device:
464 */
465 edit_rewind();
466 buff_decode_visit(mode_pars, mh->mdl, fmt, arg_put, 0);
467#endif
845 editlist_populate(device, page, page_control, dbd, retry_count,
846 timeout);
847 }
468
848
469 edit_done();
849 if (edit) {
850 if (page_control != SMS_PAGE_CTRL_CURRENT &&
851 page_control != SMS_PAGE_CTRL_SAVED)
852 errx(EX_USAGE, "it only makes sense to edit page 0 "
853 "(current) or page 3 (saved values)");
854 modepage_edit();
855 editlist_save(device, page, page_control, dbd, retry_count,
856 timeout);
857 } else if (binary || STAILQ_EMPTY(&editlist)) {
858 /* Display without formatting information. */
859 modepage_dump(device, page, page_control, dbd, retry_count,
860 timeout);
861 } else {
862 /* Display with format. */
863 modepage_write(stdout, 0);
864 }
865}
470
866
471 /* Make it permanent if pageselect is three.
472 */
867void
868mode_list(struct cam_device *device, int page_control, int dbd,
869 int retry_count, int timeout)
870{
871 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
872 u_int8_t *mode_pars; /* Pointer to modepage params. */
873 struct scsi_mode_header_6 *mh; /* Location of mode header. */
874 struct scsi_mode_page_header *mph;
875 struct pagename *nameentry;
876 char *pagedb_path;
877 int len;
473
878
474 mph->page_code &= ~0xC0; /* Clear PS and RESERVED */
475 mh->mdl = 0; /* Reserved for mode select */
879 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
880 pagedb_path = DEFAULT_SCSI_MODE_DB;
476
881
477 mode_select(device, (page_control == 3), retry_count,
478 timeout, (u_int8_t *)mh, sizeof(*mh) + mh->bdl +
479 sizeof(*mph) + mph->page_length);
480
481 return;
882 if (load_format(pagedb_path, 0) != 0 && verbose && errno == ENOENT) {
883 /* Modepage database file not found. */
884 warn("cannot open modepage database \"%s\"", pagedb_path);
482 }
483
885 }
886
484 mode_sense(device, page, page_control, dbd, retry_count, timeout,
485 data, sizeof(data));
887 /* Build the list of all mode pages by querying the "all pages" page. */
888 mode_sense(device, SMS_ALL_PAGES_PAGE, page_control, dbd, retry_count,
889 timeout, data, sizeof(data));
486
890
487 /* Skip over the block descriptors.
488 */
489 mh = (struct mode_header *)data;
490 mph = (struct mode_page_header *)(((char *)mh) + sizeof(*mh) + mh->bdl);
491 mode_pars = (char *)mph + sizeof(*mph);
891 mh = (struct scsi_mode_header_6 *)data;
892 len = mh->blk_desc_len; /* Skip block descriptors. */
893 /* Iterate through the pages in the reply. */
894 while (len < mh->data_length) {
895 /* Locate the next mode page header. */
896 mph = (struct scsi_mode_page_header *)
897 ((intptr_t)mh + sizeof(*mh) + len);
898 mode_pars = MODE_PAGE_DATA(mph);
492
899
493 if (!fmt) {
494 for (i = 0; i < mh->mdl; i++) {
495 printf("%02x%c",mode_pars[i],
496 (((i + 1) % 8) == 0) ? '\n' : ' ');
497 }
498 putc('\n', stdout);
499 } else {
500 verbose = 1;
501 buff_decode_visit(mode_pars, mh->mdl, fmt, arg_put, NULL);
900 mph->page_code &= SMS_PAGE_CODE;
901 nameentry = nameentry_lookup(mph->page_code);
902
903 if (nameentry == NULL || nameentry->name == NULL)
904 printf("0x%02x\n", mph->page_code);
905 else
906 printf("0x%02x\t%s\n", mph->page_code,
907 nameentry->name);
908 len += mph->page_length + sizeof(*mph);
502 }
503}
909 }
910}