2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Kelly Yancey <kbyanc@posi.net>
5 * Derived from work done by Julian Elischer <julian@tfs.com,
6 * julian@dialix.oz.au>, 1993, and Peter Dufault <dufault@hda.com>, 1994.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer,
14 * without modification, immediately at the beginning of the file.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/queue.h>
35 #include <sys/types.h>
47 #include <cam/scsi/scsi_all.h>
49 #include <cam/cam_ccb.h>
51 #include "camcontrol.h"
53 #define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes"
54 #define DEFAULT_EDITOR "vi"
55 #define MAX_FORMAT_SPEC 4096 /* Max CDB format specifier. */
56 #define MAX_PAGENUM_LEN 10 /* Max characters in page num. */
57 #define MAX_PAGENAME_LEN 64 /* Max characters in page name. */
58 #define PAGEDEF_START '{' /* Page definition delimiter. */
59 #define PAGEDEF_END '}' /* Page definition delimiter. */
60 #define PAGENAME_START '"' /* Page name delimiter. */
61 #define PAGENAME_END '"' /* Page name delimiter. */
62 #define PAGEENTRY_END ';' /* Page entry terminator (optional). */
63 #define MAX_DATA_SIZE 4096 /* Mode/Log sense data buffer size. */
64 #define PAGE_CTRL_SHIFT 6 /* Bit offset to page control field. */
67 STAILQ_ENTRY(editentry) link;
77 static STAILQ_HEAD(, editentry) editlist; /* List of page entries. */
78 static int editlist_changed = 0; /* Whether any entries were changed. */
81 SLIST_ENTRY(pagename) link;
86 static SLIST_HEAD(, pagename) namelist; /* Page number to name mappings. */
88 static char format[MAX_FORMAT_SPEC]; /* Buffer for scsi cdb format def. */
90 static FILE *edit_file = NULL; /* File handle for edit file. */
91 static char edit_path[] = "/tmp/camXXXXXX";
94 /* Function prototypes. */
95 static void editentry_create(void *hook, int letter, void *arg,
96 int count, char *name);
97 static void editentry_update(void *hook, int letter, void *arg,
98 int count, char *name);
99 static int editentry_save(void *hook, char *name);
100 static struct editentry *editentry_lookup(char *name);
101 static int editentry_set(char *name, char *newvalue,
103 static void editlist_populate(struct cam_device *device,
104 int cdb_len, int dbd, int pc, int page, int subpage,
105 int task_attr, int retries, int timeout);
106 static void editlist_save(struct cam_device *device, int cdb_len,
107 int dbd, int pc, int page, int subpage,
108 int task_attr, int retries, int timeout);
109 static void nameentry_create(int page, int subpage, char *name);
110 static struct pagename *nameentry_lookup(int page, int subpage);
111 static int load_format(const char *pagedb_path, int lpage,
113 static int modepage_write(FILE *file, int editonly);
114 static int modepage_read(FILE *file);
115 static void modepage_edit(void);
116 static void modepage_dump(struct cam_device *device, int cdb_len,
117 int dbd, int pc, int page, int subpage,
118 int task_attr, int retries, int timeout);
119 static void cleanup_editfile(void);
122 #define returnerr(code) do { \
128 #define RTRIM(string) do { \
130 while (isspace(string[_length = strlen(string) - 1])) \
131 string[_length] = '\0'; \
136 editentry_create(void *hook __unused, int letter, void *arg, int count,
139 struct editentry *newentry; /* Buffer to hold new entry. */
141 /* Allocate memory for the new entry and a copy of the entry name. */
142 if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
143 (newentry->name = strdup(name)) == NULL)
146 /* Trim any trailing whitespace for the entry name. */
147 RTRIM(newentry->name);
149 newentry->editable = (arg != NULL);
150 newentry->type = letter;
151 newentry->size = count; /* Placeholder; not accurate. */
152 newentry->value.svalue = NULL;
154 STAILQ_INSERT_TAIL(&editlist, newentry, link);
158 editentry_update(void *hook __unused, int letter, void *arg, int count,
161 struct editentry *dest; /* Buffer to hold entry to update. */
163 dest = editentry_lookup(name);
164 assert(dest != NULL);
167 dest->size = count; /* We get the real size now. */
169 switch (dest->type) {
170 case 'i': /* Byte-sized integral type. */
171 case 'b': /* Bit-sized integral types. */
173 dest->value.ivalue = (intptr_t)arg;
176 case 'c': /* Character array. */
177 case 'z': /* Null-padded string. */
178 editentry_set(name, (char *)arg, 0);
186 editentry_save(void *hook __unused, char *name)
188 struct editentry *src; /* Entry value to save. */
190 src = editentry_lookup(name);
193 * This happens if field does not fit into read page size.
194 * It also means that this field won't be written, so the
195 * returned value does not really matter.
201 case 'i': /* Byte-sized integral type. */
202 case 'b': /* Bit-sized integral types. */
204 return (src->value.ivalue);
207 case 'c': /* Character array. */
208 case 'z': /* Null-padded string. */
209 return ((intptr_t)src->value.svalue);
216 return (0); /* This should never happen. */
219 static struct editentry *
220 editentry_lookup(char *name)
222 struct editentry *scan;
224 assert(name != NULL);
226 STAILQ_FOREACH(scan, &editlist, link) {
227 if (strcasecmp(scan->name, name) == 0)
231 /* Not found during list traversal. */
236 editentry_set(char *name, char *newvalue, int editonly)
238 struct editentry *dest; /* Modepage entry to update. */
239 char *cval; /* Pointer to new string value. */
240 char *convertend; /* End-of-conversion pointer. */
241 int ival; /* New integral value. */
242 int resolution; /* Resolution in bits for integer conversion. */
245 * Macro to determine the maximum value of the given size for the current
247 * XXX Lovely x86's optimize out the case of shifting by 32 and gcc doesn't
248 * currently workaround it (even for int64's), so we have to kludge it.
250 #define RESOLUTION_MAX(size) ((resolution * (size) == 32)? \
251 INT_MAX: (1 << (resolution * (size))) - 1)
253 assert(newvalue != NULL);
254 if (*newvalue == '\0')
255 return (0); /* Nothing to do. */
257 if ((dest = editentry_lookup(name)) == NULL)
259 if (!dest->editable && editonly)
262 switch (dest->type) {
263 case 'i': /* Byte-sized integral type. */
264 case 'b': /* Bit-sized integral types. */
266 /* Convert the value string to an integer. */
267 resolution = (dest->type == 'i')? 8: 1;
268 ival = (int)strtol(newvalue, &convertend, 0);
269 if (*convertend != '\0')
271 if (ival > RESOLUTION_MAX(dest->size) || ival < 0) {
272 int newival = (ival < 0)? 0: RESOLUTION_MAX(dest->size);
273 warnx("value %d is out of range for entry %s; clipping "
274 "to %d", ival, name, newival);
277 if (dest->value.ivalue != ival)
278 editlist_changed = 1;
279 dest->value.ivalue = ival;
282 case 'c': /* Character array. */
283 case 'z': /* Null-padded string. */
284 if ((cval = malloc(dest->size + 1)) == NULL)
286 bzero(cval, dest->size + 1);
287 strncpy(cval, newvalue, dest->size);
288 if (dest->type == 'z') {
289 /* Convert trailing spaces to nulls. */
292 for (convertend2 = cval + dest->size;
293 convertend2 >= cval; convertend2--) {
294 if (*convertend2 == ' ')
296 else if (*convertend2 != '\0')
300 if (strncmp(dest->value.svalue, cval, dest->size) == 0) {
301 /* Nothing changed, free the newly allocated string. */
305 if (dest->value.svalue != NULL) {
306 /* Free the current string buffer. */
307 free(dest->value.svalue);
308 dest->value.svalue = NULL;
310 dest->value.svalue = cval;
311 editlist_changed = 1;
319 #undef RESOLUTION_MAX
323 nameentry_create(int page, int subpage, char *name) {
324 struct pagename *newentry;
326 if (page < 0 || subpage < 0 || name == NULL || name[0] == '\0')
329 /* Allocate memory for the new entry and a copy of the entry name. */
330 if ((newentry = malloc(sizeof(struct pagename))) == NULL ||
331 (newentry->name = strdup(name)) == NULL)
334 /* Trim any trailing whitespace for the page name. */
335 RTRIM(newentry->name);
337 newentry->page = page;
338 newentry->subpage = subpage;
339 SLIST_INSERT_HEAD(&namelist, newentry, link);
342 static struct pagename *
343 nameentry_lookup(int page, int subpage) {
344 struct pagename *scan;
346 SLIST_FOREACH(scan, &namelist, link) {
347 if (page == scan->page && subpage == scan->subpage)
351 /* Not found during list traversal. */
356 load_format(const char *pagedb_path, int lpage, int lsubpage)
359 char str_page[MAX_PAGENUM_LEN];
361 char str_pagename[MAX_PAGENAME_LEN];
364 int depth; /* Quoting depth. */
367 enum { LOCATE, PAGENAME, PAGEDEF } state;
371 #define SETSTATE_LOCATE do { \
372 str_page[0] = '\0'; \
373 str_pagename[0] = '\0'; \
379 #define SETSTATE_PAGENAME do { \
380 str_pagename[0] = '\0'; \
384 #define SETSTATE_PAGEDEF do { \
389 #define UPDATE_LINENO do { \
394 #define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer))
396 if ((pagedb = fopen(pagedb_path, "r")) == NULL)
399 SLIST_INIT(&namelist);
406 while ((ch = fgetc(pagedb)) != EOF) {
408 /* Keep a line count to make error messages more useful. */
411 /* Skip over comments anywhere in the mode database. */
415 } while (ch != '\n' && ch != EOF);
421 /* Strip out newline characters. */
425 /* Keep track of the nesting depth for braces. */
426 if (c == PAGEDEF_START)
428 else if (c == PAGEDEF_END) {
431 errx(EX_OSFILE, "%s:%d: %s", pagedb_path,
432 lineno, "mismatched bracket");
439 * Locate the page the user is interested in, skipping
443 /* Ignore all whitespace between pages. */
445 } else if (depth == 0 && c == PAGEENTRY_END) {
447 * A page entry terminator will reset page
448 * scanning (useful for assigning names to
449 * modes without providing a mode definition).
451 /* Record the name of this page. */
452 str_subpage = str_page;
453 strsep(&str_subpage, ",");
454 page = strtol(str_page, NULL, 0);
456 subpage = strtol(str_subpage, NULL, 0);
459 nameentry_create(page, subpage, str_pagename);
461 } else if (depth == 0 && c == PAGENAME_START) {
463 } else if (c == PAGEDEF_START) {
464 str_subpage = str_page;
465 strsep(&str_subpage, ",");
466 page = strtol(str_page, NULL, 0);
468 subpage = strtol(str_subpage, NULL, 0);
472 /* Record the name of this page. */
473 nameentry_create(page, subpage,
476 * Only record the format if this is
477 * the page we are interested in.
480 lsubpage == subpage && !found)
483 } else if (c == PAGEDEF_END) {
484 /* Reset the processor state. */
486 } else if (depth == 0 && ! BUFFERFULL(str_page)) {
487 strncat(str_page, &c, 1);
488 } else if (depth == 0) {
489 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
490 lineno, "page identifier exceeds",
491 sizeof(str_page) - 1, "characters");
496 if (c == PAGENAME_END) {
498 * Return to LOCATE state without resetting the
499 * page number buffer.
502 } else if (! BUFFERFULL(str_pagename)) {
503 strncat(str_pagename, &c, 1);
505 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
506 lineno, "page name exceeds",
507 sizeof(str_page) - 1, "characters");
513 * Transfer the page definition into a format buffer
514 * suitable for use with CDB encoding/decoding routines.
519 } else if (! BUFFERFULL(format)) {
520 strncat(format, &c, 1);
522 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
523 lineno, "page definition exceeds",
524 sizeof(format) - 1, "characters");
532 /* Repeat processing loop with next character. */
536 err(EX_OSFILE, "%s", pagedb_path);
538 /* Close the SCSI page database. */
541 if (!found) /* Never found a matching page. */
548 editlist_populate(struct cam_device *device, int cdb_len, int dbd, int pc,
549 int page, int subpage, int task_attr, int retries, int timeout)
551 u_int8_t data[MAX_DATA_SIZE];/* Buffer to hold sense data. */
552 u_int8_t *mode_pars; /* Pointer to modepage params. */
553 struct scsi_mode_page_header *mph;
554 struct scsi_mode_page_header_sp *mphsp;
557 STAILQ_INIT(&editlist);
559 /* Fetch changeable values; use to build initial editlist. */
560 mode_sense(device, &cdb_len, dbd, 1, page, subpage, task_attr, retries,
561 timeout, data, sizeof(data));
564 struct scsi_mode_header_6 *mh =
565 (struct scsi_mode_header_6 *)data;
566 mph = find_mode_page_6(mh);
568 struct scsi_mode_header_10 *mh =
569 (struct scsi_mode_header_10 *)data;
570 mph = find_mode_page_10(mh);
572 if ((mph->page_code & SMPH_SPF) == 0) {
573 mode_pars = (uint8_t *)(mph + 1);
574 len = mph->page_length;
576 mphsp = (struct scsi_mode_page_header_sp *)mph;
577 mode_pars = (uint8_t *)(mphsp + 1);
578 len = scsi_2btoul(mphsp->page_length);
580 len = MIN(len, sizeof(data) - (mode_pars - data));
582 /* Decode the value data, creating edit_entries for each value. */
583 buff_decode_visit(mode_pars, len, format, editentry_create, 0);
585 /* Fetch the current/saved values; use to set editentry values. */
586 mode_sense(device, &cdb_len, dbd, pc, page, subpage, task_attr,
587 retries, timeout, data, sizeof(data));
588 buff_decode_visit(mode_pars, len, format, editentry_update, 0);
592 editlist_save(struct cam_device *device, int cdb_len, int dbd, int pc,
593 int page, int subpage, int task_attr, int retries, int timeout)
595 u_int8_t data[MAX_DATA_SIZE];/* Buffer to hold sense data. */
596 u_int8_t *mode_pars; /* Pointer to modepage params. */
597 struct scsi_mode_page_header *mph;
598 struct scsi_mode_page_header_sp *mphsp;
599 size_t len, hlen, mphlen;
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, &cdb_len, dbd, pc, page, subpage, task_attr,
607 retries, timeout, data, sizeof(data));
609 /* Initial headers & offsets. */
611 * Tape drives include write protect (WP), Buffered Mode and Speed
612 * settings in the device-specific parameter. Clearing this
613 * parameter on a mode select can have the effect of turning off
614 * write protect or buffered mode, or changing the speed setting of
617 * Disks report DPO/FUA support via the device specific parameter
618 * for MODE SENSE, but the bit is reserved for MODE SELECT. So we
619 * clear this for disks (and other non-tape devices) to avoid
620 * potential errors from the target device.
623 struct scsi_mode_header_6 *mh =
624 (struct scsi_mode_header_6 *)data;
626 /* Eliminate block descriptors. */
627 if (mh->blk_desc_len > 0) {
628 bcopy(find_mode_page_6(mh), mh + 1,
629 mh->data_length + 1 - hlen -
631 mh->blk_desc_len = 0;
633 mh->data_length = 0; /* Reserved for MODE SELECT command. */
634 if (device->pd_type != T_SEQUENTIAL)
635 mh->dev_spec = 0; /* See comment above */
636 mph = find_mode_page_6(mh);
638 struct scsi_mode_header_10 *mh =
639 (struct scsi_mode_header_10 *)data;
641 /* Eliminate block descriptors. */
642 if (scsi_2btoul(mh->blk_desc_len) > 0) {
643 bcopy(find_mode_page_10(mh), mh + 1,
644 scsi_2btoul(mh->data_length) + 1 - hlen -
645 scsi_2btoul(mh->blk_desc_len));
646 scsi_ulto2b(0, mh->blk_desc_len);
648 scsi_ulto2b(0, mh->data_length); /* Reserved for MODE SELECT. */
649 if (device->pd_type != T_SEQUENTIAL)
650 mh->dev_spec = 0; /* See comment above */
651 mph = find_mode_page_10(mh);
653 if ((mph->page_code & SMPH_SPF) == 0) {
654 mphlen = sizeof(*mph);
655 mode_pars = (uint8_t *)(mph + 1);
656 len = mph->page_length;
658 mphsp = (struct scsi_mode_page_header_sp *)mph;
659 mphlen = sizeof(*mphsp);
660 mode_pars = (uint8_t *)(mphsp + 1);
661 len = scsi_2btoul(mphsp->page_length);
663 len = MIN(len, sizeof(data) - (mode_pars - data));
665 /* Encode the value data to be passed back to the device. */
666 buff_encode_visit(mode_pars, len, format, editentry_save, 0);
668 mph->page_code &= ~SMPH_PS; /* Reserved for MODE SELECT command. */
671 * Write the changes back to the device. If the user editted control
672 * page 3 (saved values) then request the changes be permanently
675 mode_select(device, cdb_len, (pc << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED),
676 task_attr, retries, timeout, data, hlen + mphlen + len);
680 modepage_write(FILE *file, int editonly)
682 struct editentry *scan;
685 STAILQ_FOREACH(scan, &editlist, link) {
686 if (scan->editable || !editonly) {
688 if (scan->type == 'c' || scan->type == 'z') {
689 fprintf(file, "%s: %s\n", scan->name,
692 fprintf(file, "%s: %d\n", scan->name,
701 modepage_read(FILE *file)
703 char *buffer; /* Pointer to dynamic line buffer. */
704 char *line; /* Pointer to static fgetln buffer. */
705 char *name; /* Name portion of the line buffer. */
706 char *value; /* Value portion of line buffer. */
707 size_t length; /* Length of static fgetln buffer. */
709 #define ABORT_READ(message, param) do { \
710 warnx(message, param); \
715 while ((line = fgetln(file, &length)) != NULL) {
716 /* Trim trailing whitespace (including optional newline). */
717 while (length > 0 && isspace(line[length - 1]))
720 /* Allocate a buffer to hold the line + terminating null. */
721 if ((buffer = malloc(length + 1)) == NULL)
723 memcpy(buffer, line, length);
724 buffer[length] = '\0';
726 /* Strip out comments. */
727 if ((value = strchr(buffer, '#')) != NULL)
730 /* The name is first in the buffer. Trim whitespace.*/
733 while (isspace(*name))
736 /* Skip empty lines. */
737 if (strlen(name) == 0)
740 /* The name ends at the colon; the value starts there. */
741 if ((value = strrchr(buffer, ':')) == NULL)
742 ABORT_READ("no value associated with %s", name);
743 *value = '\0'; /* Null-terminate name. */
744 value++; /* Value starts afterwards. */
746 /* Trim leading and trailing whitespace. */
748 while (isspace(*value))
751 /* Make sure there is a value left. */
752 if (strlen(value) == 0)
753 ABORT_READ("no value associated with %s", name);
755 /* Update our in-memory copy of the modepage entry value. */
756 if (editentry_set(name, value, 1) != 0) {
757 if (errno == ENOENT) {
758 /* No entry by the name. */
759 ABORT_READ("no such modepage entry \"%s\"",
761 } else if (errno == EINVAL) {
763 ABORT_READ("Invalid value for entry \"%s\"",
765 } else if (errno == ERANGE) {
766 /* Value out of range for entry type. */
767 ABORT_READ("value out of range for %s", name);
768 } else if (errno == EPERM) {
769 /* Entry is not editable; not fatal. */
770 warnx("modepage entry \"%s\" is read-only; "
777 return (ferror(file)? -1: 0);
790 if (!isatty(fileno(stdin))) {
791 /* Not a tty, read changes from stdin. */
792 modepage_read(stdin);
796 /* Lookup editor to invoke. */
797 if ((editor = getenv("EDITOR")) == NULL)
798 editor = DEFAULT_EDITOR;
800 /* Create temp file for editor to modify. */
801 if ((fd = mkstemp(edit_path)) == -1)
802 errx(EX_CANTCREAT, "mkstemp failed");
804 atexit(cleanup_editfile);
806 if ((edit_file = fdopen(fd, "w")) == NULL)
807 err(EX_NOINPUT, "%s", edit_path);
809 written = modepage_write(edit_file, 1);
815 warnx("no editable entries");
821 * Allocate memory to hold the command line (the 2 extra characters
822 * are to hold the argument separator (a space), and the terminating
825 commandline = malloc(strlen(editor) + strlen(edit_path) + 2);
826 if (commandline == NULL)
828 sprintf(commandline, "%s %s", editor, edit_path);
830 /* Invoke the editor on the temp file. */
831 if (system(commandline) == -1)
832 err(EX_UNAVAILABLE, "could not invoke %s", editor);
835 if ((edit_file = fopen(edit_path, "r")) == NULL)
836 err(EX_NOINPUT, "%s", edit_path);
838 /* Read any changes made to the temp file. */
839 modepage_read(edit_file);
845 modepage_dump(struct cam_device *device, int cdb_len, int dbd, int pc,
846 int page, int subpage, int task_attr, int retries, int timeout)
848 u_int8_t data[MAX_DATA_SIZE];/* Buffer to hold sense data. */
849 u_int8_t *mode_pars; /* Pointer to modepage params. */
850 struct scsi_mode_page_header *mph;
851 struct scsi_mode_page_header_sp *mphsp;
854 mode_sense(device, &cdb_len, dbd, pc, page, subpage, task_attr,
855 retries, timeout, data, sizeof(data));
858 struct scsi_mode_header_6 *mh =
859 (struct scsi_mode_header_6 *)data;
860 mph = find_mode_page_6(mh);
862 struct scsi_mode_header_10 *mh =
863 (struct scsi_mode_header_10 *)data;
864 mph = find_mode_page_10(mh);
866 if ((mph->page_code & SMPH_SPF) == 0) {
867 mode_pars = (uint8_t *)(mph + 1);
868 len = mph->page_length;
870 mphsp = (struct scsi_mode_page_header_sp *)mph;
871 mode_pars = (uint8_t *)(mphsp + 1);
872 len = scsi_2btoul(mphsp->page_length);
874 len = MIN(len, sizeof(data) - (mode_pars - data));
876 /* Print the raw mode page data with newlines each 8 bytes. */
877 for (indx = 0; indx < len; indx++) {
878 printf("%02x%c",mode_pars[indx],
879 (((indx + 1) % 8) == 0) ? '\n' : ' ');
885 cleanup_editfile(void)
887 if (edit_file == NULL)
889 if (fclose(edit_file) != 0 || unlink(edit_path) != 0)
890 warn("%s", edit_path);
895 mode_edit(struct cam_device *device, int cdb_len, int dbd, int pc, int page,
896 int subpage, int edit, int binary, int task_attr, int retry_count,
899 const char *pagedb_path; /* Path to modepage database. */
902 errx(EX_USAGE, "cannot edit in binary mode.");
905 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
906 pagedb_path = DEFAULT_SCSI_MODE_DB;
908 if (load_format(pagedb_path, page, subpage) != 0 &&
910 if (errno == ENOENT) {
911 /* Modepage database file not found. */
912 warn("cannot open modepage database \"%s\"",
914 } else if (errno == ESRCH) {
915 /* Modepage entry not found in database. */
916 warnx("modepage 0x%02x,0x%02x not found in "
917 "database \"%s\"", page, subpage,
920 /* We can recover in display mode, otherwise we exit. */
922 warnx("reverting to binary display only");
928 editlist_populate(device, cdb_len, dbd, pc, page, subpage,
929 task_attr, retry_count, timeout);
933 if (pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT &&
934 pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED)
935 errx(EX_USAGE, "it only makes sense to edit page 0 "
936 "(current) or page 3 (saved values)");
938 editlist_save(device, cdb_len, dbd, pc, page, subpage,
939 task_attr, retry_count, timeout);
940 } else if (binary || STAILQ_EMPTY(&editlist)) {
941 /* Display without formatting information. */
942 modepage_dump(device, cdb_len, dbd, pc, page, subpage,
943 task_attr, retry_count, timeout);
945 /* Display with format. */
946 modepage_write(stdout, 0);
951 mode_list(struct cam_device *device, int cdb_len, int dbd, int pc, int subpages,
952 int task_attr, int retry_count, int timeout)
954 u_int8_t data[MAX_DATA_SIZE];/* Buffer to hold sense data. */
955 struct scsi_mode_page_header *mph;
956 struct scsi_mode_page_header_sp *mphsp;
957 struct pagename *nameentry;
958 const char *pagedb_path;
959 int len, off, page, subpage;
961 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
962 pagedb_path = DEFAULT_SCSI_MODE_DB;
964 if (load_format(pagedb_path, 0, 0) != 0 && verbose && errno == ENOENT) {
965 /* Modepage database file not found. */
966 warn("cannot open modepage database \"%s\"", pagedb_path);
969 /* Build the list of all mode pages by querying the "all pages" page. */
970 mode_sense(device, &cdb_len, dbd, pc, SMS_ALL_PAGES_PAGE,
971 subpages ? SMS_SUBPAGE_ALL : 0,
972 task_attr, retry_count, timeout, data, sizeof(data));
974 /* Skip block descriptors. */
976 struct scsi_mode_header_6 *mh =
977 (struct scsi_mode_header_6 *)data;
978 len = mh->data_length;
979 off = sizeof(*mh) + mh->blk_desc_len;
981 struct scsi_mode_header_10 *mh =
982 (struct scsi_mode_header_10 *)data;
983 len = scsi_2btoul(mh->data_length);
984 off = sizeof(*mh) + scsi_2btoul(mh->blk_desc_len);
986 /* Iterate through the pages in the reply. */
988 /* Locate the next mode page header. */
989 mph = (struct scsi_mode_page_header *)(data + off);
991 if ((mph->page_code & SMPH_SPF) == 0) {
992 page = mph->page_code & SMS_PAGE_CODE;
994 off += sizeof(*mph) + mph->page_length;
996 mphsp = (struct scsi_mode_page_header_sp *)mph;
997 page = mphsp->page_code & SMS_PAGE_CODE;
998 subpage = mphsp->subpage;
999 off += sizeof(*mphsp) + scsi_2btoul(mphsp->page_length);
1002 nameentry = nameentry_lookup(page, subpage);
1004 printf("0x%02x\t%s\n", page,
1005 nameentry ? nameentry->name : "");
1007 printf("0x%02x,0x%02x\t%s\n", page, subpage,
1008 nameentry ? nameentry->name : "");