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 retries, int timeout);
110 static void editlist_save(struct cam_device *device, int dbd,
111 int pc, int page, int subpage,
112 int retries, int timeout);
113 static void nameentry_create(int page, int subpage, char *name);
114 static struct pagename *nameentry_lookup(int page, int subpage);
115 static int load_format(const char *pagedb_path, int lpage,
117 static int modepage_write(FILE *file, int editonly);
118 static int modepage_read(FILE *file);
119 static void modepage_edit(void);
120 static void modepage_dump(struct cam_device *device, int dbd,
121 int pc, int page, int subpage, int retries,
123 static void cleanup_editfile(void);
126 #define returnerr(code) do { \
132 #define RTRIM(string) do { \
134 while (isspace(string[_length = strlen(string) - 1])) \
135 string[_length] = '\0'; \
140 editentry_create(void *hook __unused, int letter, void *arg, int count,
143 struct editentry *newentry; /* Buffer to hold new entry. */
145 /* Allocate memory for the new entry and a copy of the entry name. */
146 if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
147 (newentry->name = strdup(name)) == NULL)
150 /* Trim any trailing whitespace for the entry name. */
151 RTRIM(newentry->name);
153 newentry->editable = (arg != NULL);
154 newentry->type = letter;
155 newentry->size = count; /* Placeholder; not accurate. */
156 newentry->value.svalue = NULL;
158 STAILQ_INSERT_TAIL(&editlist, newentry, link);
162 editentry_update(void *hook __unused, int letter, void *arg, int count,
165 struct editentry *dest; /* Buffer to hold entry to update. */
167 dest = editentry_lookup(name);
168 assert(dest != NULL);
171 dest->size = count; /* We get the real size now. */
173 switch (dest->type) {
174 case 'i': /* Byte-sized integral type. */
175 case 'b': /* Bit-sized integral types. */
177 dest->value.ivalue = (intptr_t)arg;
180 case 'c': /* Character array. */
181 case 'z': /* Null-padded string. */
182 editentry_set(name, (char *)arg, 0);
190 editentry_save(void *hook __unused, char *name)
192 struct editentry *src; /* Entry value to save. */
194 src = editentry_lookup(name);
197 * This happens if field does not fit into read page size.
198 * It also means that this field won't be written, so the
199 * returned value does not really matter.
205 case 'i': /* Byte-sized integral type. */
206 case 'b': /* Bit-sized integral types. */
208 return (src->value.ivalue);
211 case 'c': /* Character array. */
212 case 'z': /* Null-padded string. */
213 return ((intptr_t)src->value.svalue);
220 return (0); /* This should never happen. */
223 static struct editentry *
224 editentry_lookup(char *name)
226 struct editentry *scan;
228 assert(name != NULL);
230 STAILQ_FOREACH(scan, &editlist, link) {
231 if (strcasecmp(scan->name, name) == 0)
235 /* Not found during list traversal. */
240 editentry_set(char *name, char *newvalue, int editonly)
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. */
249 * Macro to determine the maximum value of the given size for the current
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.
254 #define RESOLUTION_MAX(size) ((resolution * (size) == 32)? \
255 INT_MAX: (1 << (resolution * (size))) - 1)
257 assert(newvalue != NULL);
258 if (*newvalue == '\0')
259 return (0); /* Nothing to do. */
261 if ((dest = editentry_lookup(name)) == NULL)
263 if (!dest->editable && editonly)
266 switch (dest->type) {
267 case 'i': /* Byte-sized integral type. */
268 case 'b': /* Bit-sized integral types. */
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')
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);
281 if (dest->value.ivalue != ival)
282 editlist_changed = 1;
283 dest->value.ivalue = ival;
286 case 'c': /* Character array. */
287 case 'z': /* Null-padded string. */
288 if ((cval = malloc(dest->size + 1)) == NULL)
290 bzero(cval, dest->size + 1);
291 strncpy(cval, newvalue, dest->size);
292 if (dest->type == 'z') {
293 /* Convert trailing spaces to nulls. */
296 for (convertend2 = cval + dest->size;
297 convertend2 >= cval; convertend2--) {
298 if (*convertend2 == ' ')
300 else if (*convertend2 != '\0')
304 if (strncmp(dest->value.svalue, cval, dest->size) == 0) {
305 /* Nothing changed, free the newly allocated string. */
309 if (dest->value.svalue != NULL) {
310 /* Free the current string buffer. */
311 free(dest->value.svalue);
312 dest->value.svalue = NULL;
314 dest->value.svalue = cval;
315 editlist_changed = 1;
323 #undef RESOLUTION_MAX
327 nameentry_create(int page, int subpage, char *name) {
328 struct pagename *newentry;
330 if (page < 0 || subpage < 0 || name == NULL || name[0] == '\0')
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)
338 /* Trim any trailing whitespace for the page name. */
339 RTRIM(newentry->name);
341 newentry->page = page;
342 newentry->subpage = subpage;
343 SLIST_INSERT_HEAD(&namelist, newentry, link);
346 static struct pagename *
347 nameentry_lookup(int page, int subpage) {
348 struct pagename *scan;
350 SLIST_FOREACH(scan, &namelist, link) {
351 if (page == scan->page && subpage == scan->subpage)
355 /* Not found during list traversal. */
360 load_format(const char *pagedb_path, int lpage, int lsubpage)
363 char str_page[MAX_PAGENUM_LEN];
365 char str_pagename[MAX_PAGENAME_LEN];
368 int depth; /* Quoting depth. */
371 enum { LOCATE, PAGENAME, PAGEDEF } state;
375 #define SETSTATE_LOCATE do { \
376 str_page[0] = '\0'; \
377 str_pagename[0] = '\0'; \
383 #define SETSTATE_PAGENAME do { \
384 str_pagename[0] = '\0'; \
388 #define SETSTATE_PAGEDEF do { \
393 #define UPDATE_LINENO do { \
398 #define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer))
400 if ((pagedb = fopen(pagedb_path, "r")) == NULL)
403 SLIST_INIT(&namelist);
410 while ((ch = fgetc(pagedb)) != EOF) {
412 /* Keep a line count to make error messages more useful. */
415 /* Skip over comments anywhere in the mode database. */
419 } while (ch != '\n' && ch != EOF);
425 /* Strip out newline characters. */
429 /* Keep track of the nesting depth for braces. */
430 if (c == PAGEDEF_START)
432 else if (c == PAGEDEF_END) {
435 errx(EX_OSFILE, "%s:%d: %s", pagedb_path,
436 lineno, "mismatched bracket");
443 * Locate the page the user is interested in, skipping
447 /* Ignore all whitespace between pages. */
449 } else if (depth == 0 && c == PAGEENTRY_END) {
451 * A page entry terminator will reset page
452 * scanning (useful for assigning names to
453 * modes without providing a mode definition).
455 /* Record the name of this page. */
456 str_subpage = str_page;
457 strsep(&str_subpage, ",");
458 page = strtol(str_page, NULL, 0);
460 subpage = strtol(str_subpage, NULL, 0);
463 nameentry_create(page, subpage, str_pagename);
465 } else if (depth == 0 && c == PAGENAME_START) {
467 } else if (c == PAGEDEF_START) {
468 str_subpage = str_page;
469 strsep(&str_subpage, ",");
470 page = strtol(str_page, NULL, 0);
472 subpage = strtol(str_subpage, NULL, 0);
476 /* Record the name of this page. */
477 nameentry_create(page, subpage,
480 * Only record the format if this is
481 * the page we are interested in.
484 lsubpage == subpage && !found)
487 } else if (c == PAGEDEF_END) {
488 /* Reset the processor state. */
490 } else if (depth == 0 && ! BUFFERFULL(str_page)) {
491 strncat(str_page, &c, 1);
492 } else if (depth == 0) {
493 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
494 lineno, "page identifier exceeds",
495 sizeof(str_page) - 1, "characters");
500 if (c == PAGENAME_END) {
502 * Return to LOCATE state without resetting the
503 * page number buffer.
506 } else if (! BUFFERFULL(str_pagename)) {
507 strncat(str_pagename, &c, 1);
509 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
510 lineno, "page name exceeds",
511 sizeof(str_page) - 1, "characters");
517 * Transfer the page definition into a format buffer
518 * suitable for use with CDB encoding/decoding routines.
523 } else if (! BUFFERFULL(format)) {
524 strncat(format, &c, 1);
526 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
527 lineno, "page definition exceeds",
528 sizeof(format) - 1, "characters");
536 /* Repeat processing loop with next character. */
540 err(EX_OSFILE, "%s", pagedb_path);
542 /* Close the SCSI page database. */
545 if (!found) /* Never found a matching page. */
552 editlist_populate(struct cam_device *device, int dbd, int pc, int page,
553 int subpage, int retries, int timeout)
555 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
556 u_int8_t *mode_pars; /* Pointer to modepage params. */
557 struct scsi_mode_header_6 *mh; /* Location of mode header. */
558 struct scsi_mode_page_header *mph;
559 struct scsi_mode_page_header_sp *mphsp;
562 STAILQ_INIT(&editlist);
564 /* Fetch changeable values; use to build initial editlist. */
565 mode_sense(device, dbd, 1, page, subpage, retries, timeout, data,
568 mh = (struct scsi_mode_header_6 *)data;
569 mph = MODE_PAGE_HEADER(mh);
570 if ((mph->page_code & SMPH_SPF) == 0) {
571 mode_pars = (uint8_t *)(mph + 1);
572 len = mph->page_length;
574 mphsp = (struct scsi_mode_page_header_sp *)mph;
575 mode_pars = (uint8_t *)(mphsp + 1);
576 len = scsi_2btoul(mphsp->page_length);
578 len = MIN(len, sizeof(data) - (mode_pars - data));
580 /* Decode the value data, creating edit_entries for each value. */
581 buff_decode_visit(mode_pars, len, format, editentry_create, 0);
583 /* Fetch the current/saved values; use to set editentry values. */
584 mode_sense(device, dbd, pc, page, subpage, retries, timeout,
586 buff_decode_visit(mode_pars, len, format, editentry_update, 0);
590 editlist_save(struct cam_device *device, int dbd, int pc, int page,
591 int subpage, int retries, int timeout)
593 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
594 u_int8_t *mode_pars; /* Pointer to modepage params. */
595 struct scsi_mode_header_6 *mh; /* Location of mode header. */
596 struct scsi_mode_page_header *mph;
597 struct scsi_mode_page_header_sp *mphsp;
600 /* Make sure that something changed before continuing. */
601 if (! editlist_changed)
604 /* Preload the CDB buffer with the current mode page data. */
605 mode_sense(device, dbd, pc, page, subpage, retries, timeout,
608 /* Initial headers & offsets. */
609 mh = (struct scsi_mode_header_6 *)data;
610 mph = MODE_PAGE_HEADER(mh);
611 if ((mph->page_code & SMPH_SPF) == 0) {
613 mode_pars = (uint8_t *)(mph + 1);
614 len = mph->page_length;
616 mphsp = (struct scsi_mode_page_header_sp *)mph;
617 hlen = sizeof(*mphsp);
618 mode_pars = (uint8_t *)(mphsp + 1);
619 len = scsi_2btoul(mphsp->page_length);
621 len = MIN(len, sizeof(data) - (mode_pars - data));
623 /* Encode the value data to be passed back to the device. */
624 buff_encode_visit(mode_pars, len, format, editentry_save, 0);
626 /* Eliminate block descriptors. */
627 bcopy(mph, mh + 1, hlen + len);
629 /* Recalculate headers & offsets. */
630 mh->data_length = 0; /* Reserved for MODE SELECT command. */
631 mh->dev_spec = 0; /* Clear device-specific parameters. */
632 mh->blk_desc_len = 0; /* No block descriptors. */
633 mph = MODE_PAGE_HEADER(mh);
634 mph->page_code &= ~SMPH_PS; /* Reserved for MODE SELECT command. */
637 * Write the changes back to the device. If the user editted control
638 * page 3 (saved values) then request the changes be permanently
641 mode_select(device, (pc << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED),
642 retries, timeout, (u_int8_t *)mh, sizeof(*mh) + hlen + len);
646 modepage_write(FILE *file, int editonly)
648 struct editentry *scan;
651 STAILQ_FOREACH(scan, &editlist, link) {
652 if (scan->editable || !editonly) {
654 if (scan->type == 'c' || scan->type == 'z') {
655 fprintf(file, "%s: %s\n", scan->name,
658 fprintf(file, "%s: %d\n", scan->name,
667 modepage_read(FILE *file)
669 char *buffer; /* Pointer to dynamic line buffer. */
670 char *line; /* Pointer to static fgetln buffer. */
671 char *name; /* Name portion of the line buffer. */
672 char *value; /* Value portion of line buffer. */
673 size_t length; /* Length of static fgetln buffer. */
675 #define ABORT_READ(message, param) do { \
676 warnx(message, param); \
681 while ((line = fgetln(file, &length)) != NULL) {
682 /* Trim trailing whitespace (including optional newline). */
683 while (length > 0 && isspace(line[length - 1]))
686 /* Allocate a buffer to hold the line + terminating null. */
687 if ((buffer = malloc(length + 1)) == NULL)
689 memcpy(buffer, line, length);
690 buffer[length] = '\0';
692 /* Strip out comments. */
693 if ((value = strchr(buffer, '#')) != NULL)
696 /* The name is first in the buffer. Trim whitespace.*/
699 while (isspace(*name))
702 /* Skip empty lines. */
703 if (strlen(name) == 0)
706 /* The name ends at the colon; the value starts there. */
707 if ((value = strrchr(buffer, ':')) == NULL)
708 ABORT_READ("no value associated with %s", name);
709 *value = '\0'; /* Null-terminate name. */
710 value++; /* Value starts afterwards. */
712 /* Trim leading and trailing whitespace. */
714 while (isspace(*value))
717 /* Make sure there is a value left. */
718 if (strlen(value) == 0)
719 ABORT_READ("no value associated with %s", name);
721 /* Update our in-memory copy of the modepage entry value. */
722 if (editentry_set(name, value, 1) != 0) {
723 if (errno == ENOENT) {
724 /* No entry by the name. */
725 ABORT_READ("no such modepage entry \"%s\"",
727 } else if (errno == EINVAL) {
729 ABORT_READ("Invalid value for entry \"%s\"",
731 } else if (errno == ERANGE) {
732 /* Value out of range for entry type. */
733 ABORT_READ("value out of range for %s", name);
734 } else if (errno == EPERM) {
735 /* Entry is not editable; not fatal. */
736 warnx("modepage entry \"%s\" is read-only; "
743 return (ferror(file)? -1: 0);
756 if (!isatty(fileno(stdin))) {
757 /* Not a tty, read changes from stdin. */
758 modepage_read(stdin);
762 /* Lookup editor to invoke. */
763 if ((editor = getenv("EDITOR")) == NULL)
764 editor = DEFAULT_EDITOR;
766 /* Create temp file for editor to modify. */
767 if ((fd = mkstemp(edit_path)) == -1)
768 errx(EX_CANTCREAT, "mkstemp failed");
770 atexit(cleanup_editfile);
772 if ((edit_file = fdopen(fd, "w")) == NULL)
773 err(EX_NOINPUT, "%s", edit_path);
775 written = modepage_write(edit_file, 1);
781 warnx("no editable entries");
787 * Allocate memory to hold the command line (the 2 extra characters
788 * are to hold the argument separator (a space), and the terminating
791 commandline = malloc(strlen(editor) + strlen(edit_path) + 2);
792 if (commandline == NULL)
794 sprintf(commandline, "%s %s", editor, edit_path);
796 /* Invoke the editor on the temp file. */
797 if (system(commandline) == -1)
798 err(EX_UNAVAILABLE, "could not invoke %s", editor);
801 if ((edit_file = fopen(edit_path, "r")) == NULL)
802 err(EX_NOINPUT, "%s", edit_path);
804 /* Read any changes made to the temp file. */
805 modepage_read(edit_file);
811 modepage_dump(struct cam_device *device, int dbd, int pc, int page, int subpage,
812 int retries, int timeout)
814 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
815 u_int8_t *mode_pars; /* Pointer to modepage params. */
816 struct scsi_mode_header_6 *mh; /* Location of mode header. */
817 struct scsi_mode_page_header *mph;
818 struct scsi_mode_page_header_sp *mphsp;
821 mode_sense(device, dbd, pc, page, subpage, retries, timeout,
824 mh = (struct scsi_mode_header_6 *)data;
825 mph = MODE_PAGE_HEADER(mh);
826 if ((mph->page_code & SMPH_SPF) == 0) {
827 mode_pars = (uint8_t *)(mph + 1);
828 len = mph->page_length;
830 mphsp = (struct scsi_mode_page_header_sp *)mph;
831 mode_pars = (uint8_t *)(mphsp + 1);
832 len = scsi_2btoul(mphsp->page_length);
834 len = MIN(len, sizeof(data) - (mode_pars - data));
836 /* Print the raw mode page data with newlines each 8 bytes. */
837 for (indx = 0; indx < len; indx++) {
838 printf("%02x%c",mode_pars[indx],
839 (((indx + 1) % 8) == 0) ? '\n' : ' ');
845 cleanup_editfile(void)
847 if (edit_file == NULL)
849 if (fclose(edit_file) != 0 || unlink(edit_path) != 0)
850 warn("%s", edit_path);
855 mode_edit(struct cam_device *device, int dbd, int pc, int page, int subpage,
856 int edit, int binary, int retry_count, int timeout)
858 const char *pagedb_path; /* Path to modepage database. */
861 errx(EX_USAGE, "cannot edit in binary mode.");
864 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
865 pagedb_path = DEFAULT_SCSI_MODE_DB;
867 if (load_format(pagedb_path, page, subpage) != 0 &&
869 if (errno == ENOENT) {
870 /* Modepage database file not found. */
871 warn("cannot open modepage database \"%s\"",
873 } else if (errno == ESRCH) {
874 /* Modepage entry not found in database. */
875 warnx("modepage 0x%02x,0x%02x not found in "
876 "database \"%s\"", page, subpage,
879 /* We can recover in display mode, otherwise we exit. */
881 warnx("reverting to binary display only");
887 editlist_populate(device, dbd, pc, page, subpage, retry_count,
892 if (pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT &&
893 pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED)
894 errx(EX_USAGE, "it only makes sense to edit page 0 "
895 "(current) or page 3 (saved values)");
897 editlist_save(device, dbd, pc, page, subpage, retry_count, timeout);
898 } else if (binary || STAILQ_EMPTY(&editlist)) {
899 /* Display without formatting information. */
900 modepage_dump(device, dbd, pc, page, subpage, retry_count, timeout);
902 /* Display with format. */
903 modepage_write(stdout, 0);
908 mode_list(struct cam_device *device, int dbd, int pc, int subpages,
909 int retry_count, int timeout)
911 u_int8_t data[MAX_COMMAND_SIZE];/* Buffer to hold sense data. */
912 struct scsi_mode_header_6 *mh; /* Location of mode header. */
913 struct scsi_mode_page_header *mph;
914 struct scsi_mode_page_header_sp *mphsp;
915 struct pagename *nameentry;
916 const char *pagedb_path;
917 int len, page, subpage;
919 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
920 pagedb_path = DEFAULT_SCSI_MODE_DB;
922 if (load_format(pagedb_path, 0, 0) != 0 && verbose && errno == ENOENT) {
923 /* Modepage database file not found. */
924 warn("cannot open modepage database \"%s\"", pagedb_path);
927 /* Build the list of all mode pages by querying the "all pages" page. */
928 mode_sense(device, dbd, pc, SMS_ALL_PAGES_PAGE,
929 subpages ? SMS_SUBPAGE_ALL : 0,
930 retry_count, timeout, data, sizeof(data));
932 mh = (struct scsi_mode_header_6 *)data;
933 len = sizeof(*mh) + mh->blk_desc_len; /* Skip block descriptors. */
934 /* Iterate through the pages in the reply. */
935 while (len < mh->data_length) {
936 /* Locate the next mode page header. */
937 mph = (struct scsi_mode_page_header *)((intptr_t)mh + len);
939 if ((mph->page_code & SMPH_SPF) == 0) {
940 page = mph->page_code & SMS_PAGE_CODE;
942 len += sizeof(*mph) + mph->page_length;
944 mphsp = (struct scsi_mode_page_header_sp *)mph;
945 page = mphsp->page_code & SMS_PAGE_CODE;
946 subpage = mphsp->subpage;
947 len += sizeof(*mphsp) + scsi_2btoul(mphsp->page_length);
950 nameentry = nameentry_lookup(page, subpage);
952 printf("0x%02x\t%s\n", page,
953 nameentry ? nameentry->name : "");
955 printf("0x%02x,0x%02x\t%s\n", page, subpage,
956 nameentry ? nameentry->name : "");