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.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/queue.h>
33 #include <sys/types.h>
45 #include <cam/scsi/scsi_all.h>
47 #include <cam/cam_ccb.h>
49 #include "camcontrol.h"
51 #define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes"
52 #define DEFAULT_EDITOR "vi"
53 #define MAX_FORMAT_SPEC 4096 /* Max CDB format specifier. */
54 #define MAX_PAGENUM_LEN 10 /* Max characters in page num. */
55 #define MAX_PAGENAME_LEN 64 /* Max characters in page name. */
56 #define PAGEDEF_START '{' /* Page definition delimiter. */
57 #define PAGEDEF_END '}' /* Page definition delimiter. */
58 #define PAGENAME_START '"' /* Page name delimiter. */
59 #define PAGENAME_END '"' /* Page name delimiter. */
60 #define PAGEENTRY_END ';' /* Page entry terminator (optional). */
61 #define MAX_COMMAND_SIZE 255 /* Mode/Log sense data buffer size. */
62 #define PAGE_CTRL_SHIFT 6 /* Bit offset to page control field. */
65 /* Macros for working with mode pages. */
66 #define MODE_PAGE_HEADER(mh) \
67 (struct scsi_mode_page_header *)find_mode_page_6(mh)
71 STAILQ_ENTRY(editentry) link;
81 static STAILQ_HEAD(, editentry) editlist; /* List of page entries. */
82 static int editlist_changed = 0; /* Whether any entries were changed. */
85 SLIST_ENTRY(pagename) link;
90 static SLIST_HEAD(, pagename) namelist; /* Page number to name mappings. */
92 static char format[MAX_FORMAT_SPEC]; /* Buffer for scsi cdb format def. */
94 static FILE *edit_file = NULL; /* File handle for edit file. */
95 static char edit_path[] = "/tmp/camXXXXXX";
98 /* Function prototypes. */
99 static void editentry_create(void *hook, int letter, void *arg,
100 int count, char *name);
101 static void editentry_update(void *hook, int letter, void *arg,
102 int count, char *name);
103 static int editentry_save(void *hook, char *name);
104 static struct editentry *editentry_lookup(char *name);
105 static int editentry_set(char *name, char *newvalue,
107 static void editlist_populate(struct cam_device *device, int dbd,
108 int pc, int page, int subpage,
109 int task_attr, int retries,
111 static void editlist_save(struct cam_device *device, int dbd,
112 int pc, int page, int subpage,
113 int task_attr, int retries, int timeout);
114 static void nameentry_create(int page, int subpage, char *name);
115 static struct pagename *nameentry_lookup(int page, int subpage);
116 static int load_format(const char *pagedb_path, int lpage,
118 static int modepage_write(FILE *file, int editonly);
119 static int modepage_read(FILE *file);
120 static void modepage_edit(void);
121 static void modepage_dump(struct cam_device *device, int dbd,
122 int pc, int page, int subpage, int task_attr,
123 int retries, int timeout);
124 static void cleanup_editfile(void);
127 #define returnerr(code) do { \
133 #define RTRIM(string) do { \
135 while (isspace(string[_length = strlen(string) - 1])) \
136 string[_length] = '\0'; \
141 editentry_create(void *hook __unused, int letter, void *arg, int count,
144 struct editentry *newentry; /* Buffer to hold new entry. */
146 /* Allocate memory for the new entry and a copy of the entry name. */
147 if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
148 (newentry->name = strdup(name)) == NULL)
151 /* Trim any trailing whitespace for the entry name. */
152 RTRIM(newentry->name);
154 newentry->editable = (arg != NULL);
155 newentry->type = letter;
156 newentry->size = count; /* Placeholder; not accurate. */
157 newentry->value.svalue = NULL;
159 STAILQ_INSERT_TAIL(&editlist, newentry, link);
163 editentry_update(void *hook __unused, int letter, void *arg, int count,
166 struct editentry *dest; /* Buffer to hold entry to update. */
168 dest = editentry_lookup(name);
169 assert(dest != NULL);
172 dest->size = count; /* We get the real size now. */
174 switch (dest->type) {
175 case 'i': /* Byte-sized integral type. */
176 case 'b': /* Bit-sized integral types. */
178 dest->value.ivalue = (intptr_t)arg;
181 case 'c': /* Character array. */
182 case 'z': /* Null-padded string. */
183 editentry_set(name, (char *)arg, 0);
191 editentry_save(void *hook __unused, char *name)
193 struct editentry *src; /* Entry value to save. */
195 src = editentry_lookup(name);
198 * This happens if field does not fit into read page size.
199 * It also means that this field won't be written, so the
200 * returned value does not really matter.
206 case 'i': /* Byte-sized integral type. */
207 case 'b': /* Bit-sized integral types. */
209 return (src->value.ivalue);
212 case 'c': /* Character array. */
213 case 'z': /* Null-padded string. */
214 return ((intptr_t)src->value.svalue);
221 return (0); /* This should never happen. */
224 static struct editentry *
225 editentry_lookup(char *name)
227 struct editentry *scan;
229 assert(name != NULL);
231 STAILQ_FOREACH(scan, &editlist, link) {
232 if (strcasecmp(scan->name, name) == 0)
236 /* Not found during list traversal. */
241 editentry_set(char *name, char *newvalue, int editonly)
243 struct editentry *dest; /* Modepage entry to update. */
244 char *cval; /* Pointer to new string value. */
245 char *convertend; /* End-of-conversion pointer. */
246 int ival; /* New integral value. */
247 int resolution; /* Resolution in bits for integer conversion. */
250 * Macro to determine the maximum value of the given size for the current
252 * XXX Lovely x86's optimize out the case of shifting by 32 and gcc doesn't
253 * currently workaround it (even for int64's), so we have to kludge it.
255 #define RESOLUTION_MAX(size) ((resolution * (size) == 32)? \
256 INT_MAX: (1 << (resolution * (size))) - 1)
258 assert(newvalue != NULL);
259 if (*newvalue == '\0')
260 return (0); /* Nothing to do. */
262 if ((dest = editentry_lookup(name)) == NULL)
264 if (!dest->editable && editonly)
267 switch (dest->type) {
268 case 'i': /* Byte-sized integral type. */
269 case 'b': /* Bit-sized integral types. */
271 /* Convert the value string to an integer. */
272 resolution = (dest->type == 'i')? 8: 1;
273 ival = (int)strtol(newvalue, &convertend, 0);
274 if (*convertend != '\0')
276 if (ival > RESOLUTION_MAX(dest->size) || ival < 0) {
277 int newival = (ival < 0)? 0: RESOLUTION_MAX(dest->size);
278 warnx("value %d is out of range for entry %s; clipping "
279 "to %d", ival, name, newival);
282 if (dest->value.ivalue != ival)
283 editlist_changed = 1;
284 dest->value.ivalue = ival;
287 case 'c': /* Character array. */
288 case 'z': /* Null-padded string. */
289 if ((cval = malloc(dest->size + 1)) == NULL)
291 bzero(cval, dest->size + 1);
292 strncpy(cval, newvalue, dest->size);
293 if (dest->type == 'z') {
294 /* Convert trailing spaces to nulls. */
297 for (convertend2 = cval + dest->size;
298 convertend2 >= cval; convertend2--) {
299 if (*convertend2 == ' ')
301 else if (*convertend2 != '\0')
305 if (strncmp(dest->value.svalue, cval, dest->size) == 0) {
306 /* Nothing changed, free the newly allocated string. */
310 if (dest->value.svalue != NULL) {
311 /* Free the current string buffer. */
312 free(dest->value.svalue);
313 dest->value.svalue = NULL;
315 dest->value.svalue = cval;
316 editlist_changed = 1;
324 #undef RESOLUTION_MAX
328 nameentry_create(int page, int subpage, char *name) {
329 struct pagename *newentry;
331 if (page < 0 || subpage < 0 || name == NULL || name[0] == '\0')
334 /* Allocate memory for the new entry and a copy of the entry name. */
335 if ((newentry = malloc(sizeof(struct pagename))) == NULL ||
336 (newentry->name = strdup(name)) == NULL)
339 /* Trim any trailing whitespace for the page name. */
340 RTRIM(newentry->name);
342 newentry->page = page;
343 newentry->subpage = subpage;
344 SLIST_INSERT_HEAD(&namelist, newentry, link);
347 static struct pagename *
348 nameentry_lookup(int page, int subpage) {
349 struct pagename *scan;
351 SLIST_FOREACH(scan, &namelist, link) {
352 if (page == scan->page && subpage == scan->subpage)
356 /* Not found during list traversal. */
361 load_format(const char *pagedb_path, int lpage, int lsubpage)
364 char str_page[MAX_PAGENUM_LEN];
366 char str_pagename[MAX_PAGENAME_LEN];
369 int depth; /* Quoting depth. */
372 enum { LOCATE, PAGENAME, PAGEDEF } state;
376 #define SETSTATE_LOCATE do { \
377 str_page[0] = '\0'; \
378 str_pagename[0] = '\0'; \
384 #define SETSTATE_PAGENAME do { \
385 str_pagename[0] = '\0'; \
389 #define SETSTATE_PAGEDEF do { \
394 #define UPDATE_LINENO do { \
399 #define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer))
401 if ((pagedb = fopen(pagedb_path, "r")) == NULL)
404 SLIST_INIT(&namelist);
411 while ((ch = fgetc(pagedb)) != EOF) {
413 /* Keep a line count to make error messages more useful. */
416 /* Skip over comments anywhere in the mode database. */
420 } while (ch != '\n' && ch != EOF);
426 /* Strip out newline characters. */
430 /* Keep track of the nesting depth for braces. */
431 if (c == PAGEDEF_START)
433 else if (c == PAGEDEF_END) {
436 errx(EX_OSFILE, "%s:%d: %s", pagedb_path,
437 lineno, "mismatched bracket");
444 * Locate the page the user is interested in, skipping
448 /* Ignore all whitespace between pages. */
450 } else if (depth == 0 && c == PAGEENTRY_END) {
452 * A page entry terminator will reset page
453 * scanning (useful for assigning names to
454 * modes without providing a mode definition).
456 /* Record the name of this page. */
457 str_subpage = str_page;
458 strsep(&str_subpage, ",");
459 page = strtol(str_page, NULL, 0);
461 subpage = strtol(str_subpage, NULL, 0);
464 nameentry_create(page, subpage, str_pagename);
466 } else if (depth == 0 && c == PAGENAME_START) {
468 } else if (c == PAGEDEF_START) {
469 str_subpage = str_page;
470 strsep(&str_subpage, ",");
471 page = strtol(str_page, NULL, 0);
473 subpage = strtol(str_subpage, NULL, 0);
477 /* Record the name of this page. */
478 nameentry_create(page, subpage,
481 * Only record the format if this is
482 * the page we are interested in.
485 lsubpage == subpage && !found)
488 } else if (c == PAGEDEF_END) {
489 /* Reset the processor state. */
491 } else if (depth == 0 && ! BUFFERFULL(str_page)) {
492 strncat(str_page, &c, 1);
493 } else if (depth == 0) {
494 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
495 lineno, "page identifier exceeds",
496 sizeof(str_page) - 1, "characters");
501 if (c == PAGENAME_END) {
503 * Return to LOCATE state without resetting the
504 * page number buffer.
507 } else if (! BUFFERFULL(str_pagename)) {
508 strncat(str_pagename, &c, 1);
510 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
511 lineno, "page name exceeds",
512 sizeof(str_page) - 1, "characters");
518 * Transfer the page definition into a format buffer
519 * suitable for use with CDB encoding/decoding routines.
524 } else if (! BUFFERFULL(format)) {
525 strncat(format, &c, 1);
527 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
528 lineno, "page definition exceeds",
529 sizeof(format) - 1, "characters");
537 /* Repeat processing loop with next character. */
541 err(EX_OSFILE, "%s", pagedb_path);
543 /* Close the SCSI page database. */
546 if (!found) /* Never found a matching page. */
553 editlist_populate(struct cam_device *device, int dbd, int pc, int page,
554 int subpage, int task_attr, int retries, int timeout)
556 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
557 u_int8_t *mode_pars; /* Pointer to modepage params. */
558 struct scsi_mode_header_6 *mh; /* Location of mode header. */
559 struct scsi_mode_page_header *mph;
560 struct scsi_mode_page_header_sp *mphsp;
563 STAILQ_INIT(&editlist);
565 /* Fetch changeable values; use to build initial editlist. */
566 mode_sense(device, dbd, 1, page, subpage, task_attr, retries, timeout,
569 mh = (struct scsi_mode_header_6 *)data;
570 mph = MODE_PAGE_HEADER(mh);
571 if ((mph->page_code & SMPH_SPF) == 0) {
572 mode_pars = (uint8_t *)(mph + 1);
573 len = mph->page_length;
575 mphsp = (struct scsi_mode_page_header_sp *)mph;
576 mode_pars = (uint8_t *)(mphsp + 1);
577 len = scsi_2btoul(mphsp->page_length);
579 len = MIN(len, sizeof(data) - (mode_pars - data));
581 /* Decode the value data, creating edit_entries for each value. */
582 buff_decode_visit(mode_pars, len, format, editentry_create, 0);
584 /* Fetch the current/saved values; use to set editentry values. */
585 mode_sense(device, dbd, pc, page, subpage, task_attr, retries, timeout,
587 buff_decode_visit(mode_pars, len, format, editentry_update, 0);
591 editlist_save(struct cam_device *device, int dbd, int pc, int page,
592 int subpage, int task_attr, int retries, int timeout)
594 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
595 u_int8_t *mode_pars; /* Pointer to modepage params. */
596 struct scsi_mode_header_6 *mh; /* Location of mode header. */
597 struct scsi_mode_page_header *mph;
598 struct scsi_mode_page_header_sp *mphsp;
601 /* Make sure that something changed before continuing. */
602 if (! editlist_changed)
605 /* Preload the CDB buffer with the current mode page data. */
606 mode_sense(device, dbd, pc, page, subpage, task_attr, retries, timeout,
609 /* Initial headers & offsets. */
610 mh = (struct scsi_mode_header_6 *)data;
611 mph = MODE_PAGE_HEADER(mh);
612 if ((mph->page_code & SMPH_SPF) == 0) {
614 mode_pars = (uint8_t *)(mph + 1);
615 len = mph->page_length;
617 mphsp = (struct scsi_mode_page_header_sp *)mph;
618 hlen = sizeof(*mphsp);
619 mode_pars = (uint8_t *)(mphsp + 1);
620 len = scsi_2btoul(mphsp->page_length);
622 len = MIN(len, sizeof(data) - (mode_pars - data));
624 /* Encode the value data to be passed back to the device. */
625 buff_encode_visit(mode_pars, len, format, editentry_save, 0);
627 /* Eliminate block descriptors. */
628 bcopy(mph, mh + 1, hlen + len);
630 /* Recalculate headers & offsets. */
631 mh->data_length = 0; /* Reserved for MODE SELECT command. */
632 mh->blk_desc_len = 0; /* No block descriptors. */
634 * Tape drives include write protect (WP), Buffered Mode and Speed
635 * settings in the device-specific parameter. Clearing this
636 * parameter on a mode select can have the effect of turning off
637 * write protect or buffered mode, or changing the speed setting of
640 * Disks report DPO/FUA support via the device specific parameter
641 * for MODE SENSE, but the bit is reserved for MODE SELECT. So we
642 * clear this for disks (and other non-tape devices) to avoid
643 * potential errors from the target device.
645 if (device->pd_type != T_SEQUENTIAL)
647 mph = MODE_PAGE_HEADER(mh);
648 mph->page_code &= ~SMPH_PS; /* Reserved for MODE SELECT command. */
651 * Write the changes back to the device. If the user editted control
652 * page 3 (saved values) then request the changes be permanently
655 mode_select(device, (pc << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED),
656 task_attr, retries, timeout, (u_int8_t *)mh,
657 sizeof(*mh) + hlen + len);
661 modepage_write(FILE *file, int editonly)
663 struct editentry *scan;
666 STAILQ_FOREACH(scan, &editlist, link) {
667 if (scan->editable || !editonly) {
669 if (scan->type == 'c' || scan->type == 'z') {
670 fprintf(file, "%s: %s\n", scan->name,
673 fprintf(file, "%s: %d\n", scan->name,
682 modepage_read(FILE *file)
684 char *buffer; /* Pointer to dynamic line buffer. */
685 char *line; /* Pointer to static fgetln buffer. */
686 char *name; /* Name portion of the line buffer. */
687 char *value; /* Value portion of line buffer. */
688 size_t length; /* Length of static fgetln buffer. */
690 #define ABORT_READ(message, param) do { \
691 warnx(message, param); \
696 while ((line = fgetln(file, &length)) != NULL) {
697 /* Trim trailing whitespace (including optional newline). */
698 while (length > 0 && isspace(line[length - 1]))
701 /* Allocate a buffer to hold the line + terminating null. */
702 if ((buffer = malloc(length + 1)) == NULL)
704 memcpy(buffer, line, length);
705 buffer[length] = '\0';
707 /* Strip out comments. */
708 if ((value = strchr(buffer, '#')) != NULL)
711 /* The name is first in the buffer. Trim whitespace.*/
714 while (isspace(*name))
717 /* Skip empty lines. */
718 if (strlen(name) == 0)
721 /* The name ends at the colon; the value starts there. */
722 if ((value = strrchr(buffer, ':')) == NULL)
723 ABORT_READ("no value associated with %s", name);
724 *value = '\0'; /* Null-terminate name. */
725 value++; /* Value starts afterwards. */
727 /* Trim leading and trailing whitespace. */
729 while (isspace(*value))
732 /* Make sure there is a value left. */
733 if (strlen(value) == 0)
734 ABORT_READ("no value associated with %s", name);
736 /* Update our in-memory copy of the modepage entry value. */
737 if (editentry_set(name, value, 1) != 0) {
738 if (errno == ENOENT) {
739 /* No entry by the name. */
740 ABORT_READ("no such modepage entry \"%s\"",
742 } else if (errno == EINVAL) {
744 ABORT_READ("Invalid value for entry \"%s\"",
746 } else if (errno == ERANGE) {
747 /* Value out of range for entry type. */
748 ABORT_READ("value out of range for %s", name);
749 } else if (errno == EPERM) {
750 /* Entry is not editable; not fatal. */
751 warnx("modepage entry \"%s\" is read-only; "
758 return (ferror(file)? -1: 0);
771 if (!isatty(fileno(stdin))) {
772 /* Not a tty, read changes from stdin. */
773 modepage_read(stdin);
777 /* Lookup editor to invoke. */
778 if ((editor = getenv("EDITOR")) == NULL)
779 editor = DEFAULT_EDITOR;
781 /* Create temp file for editor to modify. */
782 if ((fd = mkstemp(edit_path)) == -1)
783 errx(EX_CANTCREAT, "mkstemp failed");
785 atexit(cleanup_editfile);
787 if ((edit_file = fdopen(fd, "w")) == NULL)
788 err(EX_NOINPUT, "%s", edit_path);
790 written = modepage_write(edit_file, 1);
796 warnx("no editable entries");
802 * Allocate memory to hold the command line (the 2 extra characters
803 * are to hold the argument separator (a space), and the terminating
806 commandline = malloc(strlen(editor) + strlen(edit_path) + 2);
807 if (commandline == NULL)
809 sprintf(commandline, "%s %s", editor, edit_path);
811 /* Invoke the editor on the temp file. */
812 if (system(commandline) == -1)
813 err(EX_UNAVAILABLE, "could not invoke %s", editor);
816 if ((edit_file = fopen(edit_path, "r")) == NULL)
817 err(EX_NOINPUT, "%s", edit_path);
819 /* Read any changes made to the temp file. */
820 modepage_read(edit_file);
826 modepage_dump(struct cam_device *device, int dbd, int pc, int page, int subpage,
827 int task_attr, int retries, int timeout)
829 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
830 u_int8_t *mode_pars; /* Pointer to modepage params. */
831 struct scsi_mode_header_6 *mh; /* Location of mode header. */
832 struct scsi_mode_page_header *mph;
833 struct scsi_mode_page_header_sp *mphsp;
836 mode_sense(device, dbd, pc, page, subpage, task_attr, retries, timeout,
839 mh = (struct scsi_mode_header_6 *)data;
840 mph = MODE_PAGE_HEADER(mh);
841 if ((mph->page_code & SMPH_SPF) == 0) {
842 mode_pars = (uint8_t *)(mph + 1);
843 len = mph->page_length;
845 mphsp = (struct scsi_mode_page_header_sp *)mph;
846 mode_pars = (uint8_t *)(mphsp + 1);
847 len = scsi_2btoul(mphsp->page_length);
849 len = MIN(len, sizeof(data) - (mode_pars - data));
851 /* Print the raw mode page data with newlines each 8 bytes. */
852 for (indx = 0; indx < len; indx++) {
853 printf("%02x%c",mode_pars[indx],
854 (((indx + 1) % 8) == 0) ? '\n' : ' ');
860 cleanup_editfile(void)
862 if (edit_file == NULL)
864 if (fclose(edit_file) != 0 || unlink(edit_path) != 0)
865 warn("%s", edit_path);
870 mode_edit(struct cam_device *device, int dbd, int pc, int page, int subpage,
871 int edit, int binary, int task_attr, int retry_count, int timeout)
873 const char *pagedb_path; /* Path to modepage database. */
876 errx(EX_USAGE, "cannot edit in binary mode.");
879 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
880 pagedb_path = DEFAULT_SCSI_MODE_DB;
882 if (load_format(pagedb_path, page, subpage) != 0 &&
884 if (errno == ENOENT) {
885 /* Modepage database file not found. */
886 warn("cannot open modepage database \"%s\"",
888 } else if (errno == ESRCH) {
889 /* Modepage entry not found in database. */
890 warnx("modepage 0x%02x,0x%02x not found in "
891 "database \"%s\"", page, subpage,
894 /* We can recover in display mode, otherwise we exit. */
896 warnx("reverting to binary display only");
902 editlist_populate(device, dbd, pc, page, subpage, task_attr,
903 retry_count, timeout);
907 if (pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT &&
908 pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED)
909 errx(EX_USAGE, "it only makes sense to edit page 0 "
910 "(current) or page 3 (saved values)");
912 editlist_save(device, dbd, pc, page, subpage, task_attr,
913 retry_count, timeout);
914 } else if (binary || STAILQ_EMPTY(&editlist)) {
915 /* Display without formatting information. */
916 modepage_dump(device, dbd, pc, page, subpage, task_attr,
917 retry_count, timeout);
919 /* Display with format. */
920 modepage_write(stdout, 0);
925 mode_list(struct cam_device *device, int dbd, int pc, int subpages,
926 int task_attr, int retry_count, int timeout)
928 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
929 struct scsi_mode_header_6 *mh; /* Location of mode header. */
930 struct scsi_mode_page_header *mph;
931 struct scsi_mode_page_header_sp *mphsp;
932 struct pagename *nameentry;
933 const char *pagedb_path;
934 int len, page, subpage;
936 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
937 pagedb_path = DEFAULT_SCSI_MODE_DB;
939 if (load_format(pagedb_path, 0, 0) != 0 && verbose && errno == ENOENT) {
940 /* Modepage database file not found. */
941 warn("cannot open modepage database \"%s\"", pagedb_path);
944 /* Build the list of all mode pages by querying the "all pages" page. */
945 mode_sense(device, dbd, pc, SMS_ALL_PAGES_PAGE,
946 subpages ? SMS_SUBPAGE_ALL : 0,
947 task_attr, retry_count, timeout, data, sizeof(data));
949 mh = (struct scsi_mode_header_6 *)data;
950 len = sizeof(*mh) + mh->blk_desc_len; /* Skip block descriptors. */
951 /* Iterate through the pages in the reply. */
952 while (len < mh->data_length) {
953 /* Locate the next mode page header. */
954 mph = (struct scsi_mode_page_header *)((intptr_t)mh + len);
956 if ((mph->page_code & SMPH_SPF) == 0) {
957 page = mph->page_code & SMS_PAGE_CODE;
959 len += sizeof(*mph) + mph->page_length;
961 mphsp = (struct scsi_mode_page_header_sp *)mph;
962 page = mphsp->page_code & SMS_PAGE_CODE;
963 subpage = mphsp->subpage;
964 len += sizeof(*mphsp) + scsi_2btoul(mphsp->page_length);
967 nameentry = nameentry_lookup(page, subpage);
969 printf("0x%02x\t%s\n", page,
970 nameentry ? nameentry->name : "");
972 printf("0x%02x,0x%02x\t%s\n", page, subpage,
973 nameentry ? nameentry->name : "");