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>
48 #include <cam/scsi/scsi_all.h>
50 #include <cam/cam_ccb.h>
52 #include "camcontrol.h"
54 #define DEFAULT_SCSI_MODE_DB "/usr/share/misc/scsi_modes"
55 #define DEFAULT_EDITOR "vi"
56 #define MAX_FORMAT_SPEC 4096 /* Max CDB format specifier. */
57 #define MAX_PAGENUM_LEN 10 /* Max characters in page num. */
58 #define MAX_PAGENAME_LEN 64 /* Max characters in page name. */
59 #define PAGEDEF_START '{' /* Page definition delimiter. */
60 #define PAGEDEF_END '}' /* Page definition delimiter. */
61 #define PAGENAME_START '"' /* Page name delimiter. */
62 #define PAGENAME_END '"' /* Page name delimiter. */
63 #define PAGEENTRY_END ';' /* Page entry terminator (optional). */
64 #define MAX_DATA_SIZE 4096 /* Mode/Log sense data buffer size. */
65 #define PAGE_CTRL_SHIFT 6 /* Bit offset to page control field. */
68 STAILQ_ENTRY(editentry) link;
78 static STAILQ_HEAD(, editentry) editlist; /* List of page entries. */
79 static int editlist_changed = 0; /* Whether any entries were changed. */
82 SLIST_ENTRY(pagename) link;
87 static SLIST_HEAD(, pagename) namelist; /* Page number to name mappings. */
89 static char format[MAX_FORMAT_SPEC]; /* Buffer for scsi cdb format def. */
91 static FILE *edit_file = NULL; /* File handle for edit file. */
92 static char edit_path[] = "/tmp/camXXXXXX";
95 /* Function prototypes. */
96 static void editentry_create(void *hook, int letter, void *arg,
97 int count, char *name);
98 static void editentry_update(void *hook, int letter, void *arg,
99 int count, char *name);
100 static void editentry_create_desc(void *hook, int letter, void *arg,
101 int count, char *name);
102 static int editentry_save(void *hook, char *name);
103 static struct editentry *editentry_lookup(char *name);
104 static int editentry_set(char *name, char *newvalue,
106 static void editlist_populate(struct cam_device *device,
107 int cdb_len, int dbd, int pc, int page, int subpage,
108 int task_attr, int retries, int timeout);
109 static void editlist_populate_desc(struct cam_device *device,
110 int cdb_len, int llbaa, int pc, int page,
111 int subpage, int task_attr, int retries,
113 static void editlist_save(struct cam_device *device, int cdb_len,
114 int dbd, int pc, int page, int subpage,
115 int task_attr, int retries, int timeout);
116 static void editlist_save_desc(struct cam_device *device, int cdb_len,
117 int llbaa, int pc, int page, int subpage,
118 int task_attr, int retries, int timeout);
119 static void nameentry_create(int page, int subpage, char *name);
120 static struct pagename *nameentry_lookup(int page, int subpage);
121 static int load_format(const char *pagedb_path, int lpage,
123 static int modepage_write(FILE *file, int editonly);
124 static int modepage_read(FILE *file);
125 static void modepage_edit(void);
126 static void modepage_dump(struct cam_device *device, int cdb_len,
127 int dbd, int pc, int page, int subpage,
128 int task_attr, int retries, int timeout);
129 static void modepage_dump_desc(struct cam_device *device,
130 int cdb_len, int llbaa, int pc, int page,
131 int subpage, int task_attr, int retries,
133 static void cleanup_editfile(void);
136 #define returnerr(code) do { \
142 #define RTRIM(string) do { \
144 while (isspace(string[_length = strlen(string) - 1])) \
145 string[_length] = '\0'; \
150 editentry_create(void *hook __unused, int letter, void *arg, int count,
153 struct editentry *newentry; /* Buffer to hold new entry. */
155 /* Allocate memory for the new entry and a copy of the entry name. */
156 if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
157 (newentry->name = strdup(name)) == NULL)
160 /* Trim any trailing whitespace for the entry name. */
161 RTRIM(newentry->name);
163 newentry->editable = (arg != NULL);
164 newentry->type = letter;
165 newentry->size = count; /* Placeholder; not accurate. */
166 newentry->value.svalue = NULL;
168 STAILQ_INSERT_TAIL(&editlist, newentry, link);
172 editentry_update(void *hook __unused, int letter, void *arg, int count,
175 struct editentry *dest; /* Buffer to hold entry to update. */
177 dest = editentry_lookup(name);
178 assert(dest != NULL);
181 dest->size = count; /* We get the real size now. */
183 switch (dest->type) {
184 case 'i': /* Byte-sized integral type. */
185 case 'b': /* Bit-sized integral types. */
187 dest->value.ivalue = (intptr_t)arg;
190 case 'c': /* Character array. */
191 case 'z': /* Null-padded string. */
192 editentry_set(name, (char *)arg, 0);
200 editentry_create_desc(void *hook __unused, int letter, void *arg, int count,
203 struct editentry *newentry; /* Buffer to hold new entry. */
205 /* Allocate memory for the new entry and a copy of the entry name. */
206 if ((newentry = malloc(sizeof(struct editentry))) == NULL ||
207 (newentry->name = strdup(name)) == NULL)
210 /* Trim any trailing whitespace for the entry name. */
211 RTRIM(newentry->name);
213 newentry->editable = 1;
214 newentry->type = letter;
215 newentry->size = count;
216 newentry->value.svalue = NULL;
218 STAILQ_INSERT_TAIL(&editlist, newentry, link);
221 case 'i': /* Byte-sized integral type. */
222 case 'b': /* Bit-sized integral types. */
224 newentry->value.ivalue = (intptr_t)arg;
227 case 'c': /* Character array. */
228 case 'z': /* Null-padded string. */
229 editentry_set(name, (char *)arg, 0);
237 editentry_save(void *hook __unused, char *name)
239 struct editentry *src; /* Entry value to save. */
241 src = editentry_lookup(name);
244 * This happens if field does not fit into read page size.
245 * It also means that this field won't be written, so the
246 * returned value does not really matter.
252 case 'i': /* Byte-sized integral type. */
253 case 'b': /* Bit-sized integral types. */
255 return (src->value.ivalue);
258 case 'c': /* Character array. */
259 case 'z': /* Null-padded string. */
260 return ((intptr_t)src->value.svalue);
267 return (0); /* This should never happen. */
270 static struct editentry *
271 editentry_lookup(char *name)
273 struct editentry *scan;
275 assert(name != NULL);
277 STAILQ_FOREACH(scan, &editlist, link) {
278 if (strcasecmp(scan->name, name) == 0)
282 /* Not found during list traversal. */
287 editentry_set(char *name, char *newvalue, int editonly)
289 struct editentry *dest; /* Modepage entry to update. */
290 char *cval; /* Pointer to new string value. */
291 char *convertend; /* End-of-conversion pointer. */
292 long long ival, newival; /* New integral value. */
293 int resolution; /* Resolution in bits for integer conversion. */
296 * Macro to determine the maximum value of the given size for the current
299 #define RESOLUTION_MAX(size) ((1LL << (resolution * (size))) - 1)
301 assert(newvalue != NULL);
302 if (*newvalue == '\0')
303 return (0); /* Nothing to do. */
305 if ((dest = editentry_lookup(name)) == NULL)
307 if (!dest->editable && editonly)
310 switch (dest->type) {
311 case 'i': /* Byte-sized integral type. */
312 case 'b': /* Bit-sized integral types. */
314 /* Convert the value string to an integer. */
315 resolution = (dest->type == 'i')? 8: 1;
316 ival = strtoll(newvalue, &convertend, 0);
317 if (*convertend != '\0')
319 if (ival > RESOLUTION_MAX(dest->size) || ival < 0) {
320 newival = (ival < 0) ? 0 : RESOLUTION_MAX(dest->size);
321 warnx("value %lld is out of range for entry %s; "
322 "clipping to %lld", ival, name, newival);
325 if (dest->value.ivalue != ival)
326 editlist_changed = 1;
327 dest->value.ivalue = ival;
330 case 'c': /* Character array. */
331 case 'z': /* Null-padded string. */
332 if ((cval = malloc(dest->size + 1)) == NULL)
334 bzero(cval, dest->size + 1);
335 strncpy(cval, newvalue, dest->size);
336 if (dest->type == 'z') {
337 /* Convert trailing spaces to nulls. */
340 for (convertend2 = cval + dest->size;
341 convertend2 >= cval; convertend2--) {
342 if (*convertend2 == ' ')
344 else if (*convertend2 != '\0')
348 if (strncmp(dest->value.svalue, cval, dest->size) == 0) {
349 /* Nothing changed, free the newly allocated string. */
353 if (dest->value.svalue != NULL) {
354 /* Free the current string buffer. */
355 free(dest->value.svalue);
356 dest->value.svalue = NULL;
358 dest->value.svalue = cval;
359 editlist_changed = 1;
367 #undef RESOLUTION_MAX
371 nameentry_create(int page, int subpage, char *name) {
372 struct pagename *newentry;
374 if (page < 0 || subpage < 0 || name == NULL || name[0] == '\0')
377 /* Allocate memory for the new entry and a copy of the entry name. */
378 if ((newentry = malloc(sizeof(struct pagename))) == NULL ||
379 (newentry->name = strdup(name)) == NULL)
382 /* Trim any trailing whitespace for the page name. */
383 RTRIM(newentry->name);
385 newentry->page = page;
386 newentry->subpage = subpage;
387 SLIST_INSERT_HEAD(&namelist, newentry, link);
390 static struct pagename *
391 nameentry_lookup(int page, int subpage) {
392 struct pagename *scan;
394 SLIST_FOREACH(scan, &namelist, link) {
395 if (page == scan->page && subpage == scan->subpage)
399 /* Not found during list traversal. */
404 load_format(const char *pagedb_path, int lpage, int lsubpage)
407 char str_page[MAX_PAGENUM_LEN];
409 char str_pagename[MAX_PAGENAME_LEN];
412 int depth; /* Quoting depth. */
415 enum { LOCATE, PAGENAME, PAGEDEF } state;
419 #define SETSTATE_LOCATE do { \
420 str_page[0] = '\0'; \
421 str_pagename[0] = '\0'; \
427 #define SETSTATE_PAGENAME do { \
428 str_pagename[0] = '\0'; \
432 #define SETSTATE_PAGEDEF do { \
437 #define UPDATE_LINENO do { \
442 #define BUFFERFULL(buffer) (strlen(buffer) + 1 >= sizeof(buffer))
444 if ((pagedb = fopen(pagedb_path, "r")) == NULL)
447 SLIST_INIT(&namelist);
454 while ((ch = fgetc(pagedb)) != EOF) {
456 /* Keep a line count to make error messages more useful. */
459 /* Skip over comments anywhere in the mode database. */
463 } while (ch != '\n' && ch != EOF);
469 /* Strip out newline characters. */
473 /* Keep track of the nesting depth for braces. */
474 if (c == PAGEDEF_START)
476 else if (c == PAGEDEF_END) {
479 errx(EX_OSFILE, "%s:%d: %s", pagedb_path,
480 lineno, "mismatched bracket");
487 * Locate the page the user is interested in, skipping
491 /* Ignore all whitespace between pages. */
493 } else if (depth == 0 && c == PAGEENTRY_END) {
495 * A page entry terminator will reset page
496 * scanning (useful for assigning names to
497 * modes without providing a mode definition).
499 /* Record the name of this page. */
500 str_subpage = str_page;
501 strsep(&str_subpage, ",");
502 page = strtol(str_page, NULL, 0);
504 subpage = strtol(str_subpage, NULL, 0);
507 nameentry_create(page, subpage, str_pagename);
509 } else if (depth == 0 && c == PAGENAME_START) {
511 } else if (c == PAGEDEF_START) {
512 str_subpage = str_page;
513 strsep(&str_subpage, ",");
514 page = strtol(str_page, NULL, 0);
516 subpage = strtol(str_subpage, NULL, 0);
520 /* Record the name of this page. */
521 nameentry_create(page, subpage,
524 * Only record the format if this is
525 * the page we are interested in.
528 lsubpage == subpage && !found)
531 } else if (c == PAGEDEF_END) {
532 /* Reset the processor state. */
534 } else if (depth == 0 && ! BUFFERFULL(str_page)) {
535 strncat(str_page, &c, 1);
536 } else if (depth == 0) {
537 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
538 lineno, "page identifier exceeds",
539 sizeof(str_page) - 1, "characters");
544 if (c == PAGENAME_END) {
546 * Return to LOCATE state without resetting the
547 * page number buffer.
550 } else if (! BUFFERFULL(str_pagename)) {
551 strncat(str_pagename, &c, 1);
553 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
554 lineno, "page name exceeds",
555 sizeof(str_page) - 1, "characters");
561 * Transfer the page definition into a format buffer
562 * suitable for use with CDB encoding/decoding routines.
567 } else if (! BUFFERFULL(format)) {
568 strncat(format, &c, 1);
570 errx(EX_OSFILE, "%s:%d: %s %zd %s", pagedb_path,
571 lineno, "page definition exceeds",
572 sizeof(format) - 1, "characters");
580 /* Repeat processing loop with next character. */
584 err(EX_OSFILE, "%s", pagedb_path);
586 /* Close the SCSI page database. */
589 if (!found) /* Never found a matching page. */
596 editlist_populate(struct cam_device *device, int cdb_len, int dbd, int pc,
597 int page, int subpage, int task_attr, int retries, int timeout)
599 u_int8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
600 u_int8_t *mode_pars; /* Pointer to modepage params. */
601 struct scsi_mode_page_header *mph;
602 struct scsi_mode_page_header_sp *mphsp;
605 STAILQ_INIT(&editlist);
607 /* Fetch changeable values; use to build initial editlist. */
608 mode_sense(device, &cdb_len, dbd, 0, 1, page, subpage, task_attr,
609 retries, timeout, data, sizeof(data));
612 struct scsi_mode_header_6 *mh =
613 (struct scsi_mode_header_6 *)data;
614 mph = find_mode_page_6(mh);
616 struct scsi_mode_header_10 *mh =
617 (struct scsi_mode_header_10 *)data;
618 mph = find_mode_page_10(mh);
620 if ((mph->page_code & SMPH_SPF) == 0) {
621 mode_pars = (uint8_t *)(mph + 1);
622 len = mph->page_length;
624 mphsp = (struct scsi_mode_page_header_sp *)mph;
625 mode_pars = (uint8_t *)(mphsp + 1);
626 len = scsi_2btoul(mphsp->page_length);
628 len = MIN(len, sizeof(data) - (mode_pars - data));
630 /* Decode the value data, creating edit_entries for each value. */
631 buff_decode_visit(mode_pars, len, format, editentry_create, 0);
633 /* Fetch the current/saved values; use to set editentry values. */
634 mode_sense(device, &cdb_len, dbd, 0, pc, page, subpage, task_attr,
635 retries, timeout, data, sizeof(data));
636 buff_decode_visit(mode_pars, len, format, editentry_update, 0);
640 editlist_populate_desc(struct cam_device *device, int cdb_len, int llbaa, int pc,
641 int page, int subpage, int task_attr, int retries, int timeout)
643 uint8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
644 uint8_t *desc; /* Pointer to block descriptor. */
648 u_int longlba, dlen, i;
650 STAILQ_INIT(&editlist);
652 /* Fetch the current/saved values. */
653 mode_sense(device, &cdb_len, 0, llbaa, pc, page, subpage, task_attr,
654 retries, timeout, data, sizeof(data));
657 struct scsi_mode_header_6 *mh =
658 (struct scsi_mode_header_6 *)data;
659 desc = (uint8_t *)(mh + 1);
660 len = mh->blk_desc_len;
663 struct scsi_mode_header_10 *mh =
664 (struct scsi_mode_header_10 *)data;
665 desc = (uint8_t *)(mh + 1);
666 len = scsi_2btoul(mh->blk_desc_len);
667 longlba = (mh->flags & SMH_LONGLBA) != 0;
669 dlen = longlba ? 16 : 8;
670 len = MIN(len, sizeof(data) - (desc - data));
672 sbuf_new(&sb, format, sizeof(format), SBUF_FIXEDLEN);
674 for (i = 0; i * dlen < len; i++) {
676 snprintf(num, sizeof(num), " %d", i + 1);
678 sbuf_printf(&sb, "{Number of Logical Blocks%s High} i4\n", num);
679 sbuf_printf(&sb, "{Number of Logical Blocks%s} i4\n", num);
680 sbuf_cat(&sb, "{Reserved} *i4\n");
681 sbuf_printf(&sb, "{Logical Block Length%s} i4\n", num);
682 } else if (device->pd_type == T_DIRECT) {
683 sbuf_printf(&sb, "{Number of Logical Blocks%s} i4\n", num);
684 sbuf_cat(&sb, "{Reserved} *i1\n");
685 sbuf_printf(&sb, "{Logical Block Length%s} i3\n", num);
687 sbuf_printf(&sb, "{Density Code%s} i1\n", num);
688 sbuf_printf(&sb, "{Number of Logical Blocks%s} i3\n", num);
689 sbuf_cat(&sb, "{Reserved} *i1\n");
690 sbuf_printf(&sb, "{Logical Block Length%s} i3\n", num);
696 /* Decode the value data, creating edit_entries for each value. */
697 buff_decode_visit(desc, len, format, editentry_create_desc, 0);
701 editlist_save(struct cam_device *device, int cdb_len, int dbd, int pc,
702 int page, int subpage, int task_attr, int retries, int timeout)
704 u_int8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
705 u_int8_t *mode_pars; /* Pointer to modepage params. */
706 struct scsi_mode_page_header *mph;
707 struct scsi_mode_page_header_sp *mphsp;
708 size_t len, hlen, mphlen;
710 /* Make sure that something changed before continuing. */
711 if (! editlist_changed)
714 /* Preload the CDB buffer with the current mode page data. */
715 mode_sense(device, &cdb_len, dbd, 0, pc, page, subpage, task_attr,
716 retries, timeout, data, sizeof(data));
718 /* Initial headers & offsets. */
720 * Tape drives include write protect (WP), Buffered Mode and Speed
721 * settings in the device-specific parameter. Clearing this
722 * parameter on a mode select can have the effect of turning off
723 * write protect or buffered mode, or changing the speed setting of
726 * Disks report DPO/FUA support via the device specific parameter
727 * for MODE SENSE, but the bit is reserved for MODE SELECT. So we
728 * clear this for disks (and other non-tape devices) to avoid
729 * potential errors from the target device.
732 struct scsi_mode_header_6 *mh =
733 (struct scsi_mode_header_6 *)data;
735 /* Eliminate block descriptors. */
736 if (mh->blk_desc_len > 0) {
737 bcopy(find_mode_page_6(mh), mh + 1,
738 mh->data_length + 1 - hlen -
740 mh->blk_desc_len = 0;
742 mh->data_length = 0; /* Reserved for MODE SELECT command. */
743 if (device->pd_type != T_SEQUENTIAL)
744 mh->dev_spec = 0; /* See comment above */
745 mph = find_mode_page_6(mh);
747 struct scsi_mode_header_10 *mh =
748 (struct scsi_mode_header_10 *)data;
750 /* Eliminate block descriptors. */
751 if (scsi_2btoul(mh->blk_desc_len) > 0) {
752 bcopy(find_mode_page_10(mh), mh + 1,
753 scsi_2btoul(mh->data_length) + 1 - hlen -
754 scsi_2btoul(mh->blk_desc_len));
755 scsi_ulto2b(0, mh->blk_desc_len);
757 scsi_ulto2b(0, mh->data_length); /* Reserved for MODE SELECT. */
758 if (device->pd_type != T_SEQUENTIAL)
759 mh->dev_spec = 0; /* See comment above */
760 mph = find_mode_page_10(mh);
762 if ((mph->page_code & SMPH_SPF) == 0) {
763 mphlen = sizeof(*mph);
764 mode_pars = (uint8_t *)(mph + 1);
765 len = mph->page_length;
767 mphsp = (struct scsi_mode_page_header_sp *)mph;
768 mphlen = sizeof(*mphsp);
769 mode_pars = (uint8_t *)(mphsp + 1);
770 len = scsi_2btoul(mphsp->page_length);
772 len = MIN(len, sizeof(data) - (mode_pars - data));
774 /* Encode the value data to be passed back to the device. */
775 buff_encode_visit(mode_pars, len, format, editentry_save, 0);
777 mph->page_code &= ~SMPH_PS; /* Reserved for MODE SELECT command. */
780 * Write the changes back to the device. If the user editted control
781 * page 3 (saved values) then request the changes be permanently
784 mode_select(device, cdb_len, (pc << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED),
785 task_attr, retries, timeout, data, hlen + mphlen + len);
789 editlist_save_desc(struct cam_device *device, int cdb_len, int llbaa, int pc,
790 int page, int subpage, int task_attr, int retries, int timeout)
792 uint8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
793 uint8_t *desc; /* Pointer to block descriptor. */
796 /* Make sure that something changed before continuing. */
797 if (! editlist_changed)
800 /* Preload the CDB buffer with the current mode page data. */
801 mode_sense(device, &cdb_len, 0, llbaa, pc, page, subpage, task_attr,
802 retries, timeout, data, sizeof(data));
804 /* Initial headers & offsets. */
806 struct scsi_mode_header_6 *mh =
807 (struct scsi_mode_header_6 *)data;
809 desc = (uint8_t *)(mh + 1);
810 len = mh->blk_desc_len;
811 mh->data_length = 0; /* Reserved for MODE SELECT command. */
812 if (device->pd_type != T_SEQUENTIAL)
813 mh->dev_spec = 0; /* See comment above */
815 struct scsi_mode_header_10 *mh =
816 (struct scsi_mode_header_10 *)data;
818 desc = (uint8_t *)(mh + 1);
819 len = scsi_2btoul(mh->blk_desc_len);
820 scsi_ulto2b(0, mh->data_length); /* Reserved for MODE SELECT. */
821 if (device->pd_type != T_SEQUENTIAL)
822 mh->dev_spec = 0; /* See comment above */
824 len = MIN(len, sizeof(data) - (desc - data));
826 /* Encode the value data to be passed back to the device. */
827 buff_encode_visit(desc, len, format, editentry_save, 0);
830 * Write the changes back to the device. If the user editted control
831 * page 3 (saved values) then request the changes be permanently
834 mode_select(device, cdb_len, (pc << PAGE_CTRL_SHIFT == SMS_PAGE_CTRL_SAVED),
835 task_attr, retries, timeout, data, hlen + len);
839 modepage_write(FILE *file, int editonly)
841 struct editentry *scan;
844 STAILQ_FOREACH(scan, &editlist, link) {
845 if (scan->editable || !editonly) {
847 if (scan->type == 'c' || scan->type == 'z') {
848 fprintf(file, "%s: %s\n", scan->name,
851 fprintf(file, "%s: %u\n", scan->name,
860 modepage_read(FILE *file)
862 char *buffer; /* Pointer to dynamic line buffer. */
863 char *line; /* Pointer to static fgetln buffer. */
864 char *name; /* Name portion of the line buffer. */
865 char *value; /* Value portion of line buffer. */
866 size_t length; /* Length of static fgetln buffer. */
868 #define ABORT_READ(message, param) do { \
869 warnx(message, param); \
874 while ((line = fgetln(file, &length)) != NULL) {
875 /* Trim trailing whitespace (including optional newline). */
876 while (length > 0 && isspace(line[length - 1]))
879 /* Allocate a buffer to hold the line + terminating null. */
880 if ((buffer = malloc(length + 1)) == NULL)
882 memcpy(buffer, line, length);
883 buffer[length] = '\0';
885 /* Strip out comments. */
886 if ((value = strchr(buffer, '#')) != NULL)
889 /* The name is first in the buffer. Trim whitespace.*/
892 while (isspace(*name))
895 /* Skip empty lines. */
896 if (strlen(name) == 0)
899 /* The name ends at the colon; the value starts there. */
900 if ((value = strrchr(buffer, ':')) == NULL)
901 ABORT_READ("no value associated with %s", name);
902 *value = '\0'; /* Null-terminate name. */
903 value++; /* Value starts afterwards. */
905 /* Trim leading and trailing whitespace. */
907 while (isspace(*value))
910 /* Make sure there is a value left. */
911 if (strlen(value) == 0)
912 ABORT_READ("no value associated with %s", name);
914 /* Update our in-memory copy of the modepage entry value. */
915 if (editentry_set(name, value, 1) != 0) {
916 if (errno == ENOENT) {
917 /* No entry by the name. */
918 ABORT_READ("no such modepage entry \"%s\"",
920 } else if (errno == EINVAL) {
922 ABORT_READ("Invalid value for entry \"%s\"",
924 } else if (errno == ERANGE) {
925 /* Value out of range for entry type. */
926 ABORT_READ("value out of range for %s", name);
927 } else if (errno == EPERM) {
928 /* Entry is not editable; not fatal. */
929 warnx("modepage entry \"%s\" is read-only; "
936 return (ferror(file)? -1: 0);
949 if (!isatty(fileno(stdin))) {
950 /* Not a tty, read changes from stdin. */
951 modepage_read(stdin);
955 /* Lookup editor to invoke. */
956 if ((editor = getenv("EDITOR")) == NULL)
957 editor = DEFAULT_EDITOR;
959 /* Create temp file for editor to modify. */
960 if ((fd = mkstemp(edit_path)) == -1)
961 errx(EX_CANTCREAT, "mkstemp failed");
963 atexit(cleanup_editfile);
965 if ((edit_file = fdopen(fd, "w")) == NULL)
966 err(EX_NOINPUT, "%s", edit_path);
968 written = modepage_write(edit_file, 1);
974 warnx("no editable entries");
980 * Allocate memory to hold the command line (the 2 extra characters
981 * are to hold the argument separator (a space), and the terminating
984 commandline = malloc(strlen(editor) + strlen(edit_path) + 2);
985 if (commandline == NULL)
987 sprintf(commandline, "%s %s", editor, edit_path);
989 /* Invoke the editor on the temp file. */
990 if (system(commandline) == -1)
991 err(EX_UNAVAILABLE, "could not invoke %s", editor);
994 if ((edit_file = fopen(edit_path, "r")) == NULL)
995 err(EX_NOINPUT, "%s", edit_path);
997 /* Read any changes made to the temp file. */
998 modepage_read(edit_file);
1004 modepage_dump(struct cam_device *device, int cdb_len, int dbd, int pc,
1005 int page, int subpage, int task_attr, int retries, int timeout)
1007 u_int8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
1008 u_int8_t *mode_pars; /* Pointer to modepage params. */
1009 struct scsi_mode_page_header *mph;
1010 struct scsi_mode_page_header_sp *mphsp;
1013 mode_sense(device, &cdb_len, dbd, 0, pc, page, subpage, task_attr,
1014 retries, timeout, data, sizeof(data));
1017 struct scsi_mode_header_6 *mh =
1018 (struct scsi_mode_header_6 *)data;
1019 mph = find_mode_page_6(mh);
1021 struct scsi_mode_header_10 *mh =
1022 (struct scsi_mode_header_10 *)data;
1023 mph = find_mode_page_10(mh);
1025 if ((mph->page_code & SMPH_SPF) == 0) {
1026 mode_pars = (uint8_t *)(mph + 1);
1027 len = mph->page_length;
1029 mphsp = (struct scsi_mode_page_header_sp *)mph;
1030 mode_pars = (uint8_t *)(mphsp + 1);
1031 len = scsi_2btoul(mphsp->page_length);
1033 len = MIN(len, sizeof(data) - (mode_pars - data));
1035 /* Print the raw mode page data with newlines each 8 bytes. */
1036 for (indx = 0; indx < len; indx++) {
1037 printf("%02x%c",mode_pars[indx],
1038 (((indx + 1) % 8) == 0) ? '\n' : ' ');
1043 modepage_dump_desc(struct cam_device *device, int cdb_len, int llbaa, int pc,
1044 int page, int subpage, int task_attr, int retries, int timeout)
1046 uint8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
1047 uint8_t *desc; /* Pointer to block descriptor. */
1050 mode_sense(device, &cdb_len, 0, llbaa, pc, page, subpage, task_attr,
1051 retries, timeout, data, sizeof(data));
1054 struct scsi_mode_header_6 *mh =
1055 (struct scsi_mode_header_6 *)data;
1056 desc = (uint8_t *)(mh + 1);
1057 len = mh->blk_desc_len;
1059 struct scsi_mode_header_10 *mh =
1060 (struct scsi_mode_header_10 *)data;
1061 desc = (uint8_t *)(mh + 1);
1062 len = scsi_2btoul(mh->blk_desc_len);
1064 len = MIN(len, sizeof(data) - (desc - data));
1066 /* Print the raw mode page data with newlines each 8 bytes. */
1067 for (indx = 0; indx < len; indx++) {
1068 printf("%02x%c", desc[indx],
1069 (((indx + 1) % 8) == 0) ? '\n' : ' ');
1075 cleanup_editfile(void)
1077 if (edit_file == NULL)
1079 if (fclose(edit_file) != 0 || unlink(edit_path) != 0)
1080 warn("%s", edit_path);
1085 mode_edit(struct cam_device *device, int cdb_len, int desc, int dbd, int llbaa,
1086 int pc, int page, int subpage, int edit, int binary, int task_attr,
1087 int retry_count, int timeout)
1089 const char *pagedb_path; /* Path to modepage database. */
1093 errx(EX_USAGE, "cannot edit in binary mode.");
1095 editlist_populate_desc(device, cdb_len, llbaa, pc, page,
1096 subpage, task_attr, retry_count, timeout);
1098 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
1099 pagedb_path = DEFAULT_SCSI_MODE_DB;
1101 if (load_format(pagedb_path, page, subpage) != 0 &&
1102 (edit || verbose)) {
1103 if (errno == ENOENT) {
1104 /* Modepage database file not found. */
1105 warn("cannot open modepage database \"%s\"",
1107 } else if (errno == ESRCH) {
1108 /* Modepage entry not found in database. */
1109 warnx("modepage 0x%02x,0x%02x not found in "
1110 "database \"%s\"", page, subpage,
1113 /* We can recover in display mode, otherwise we exit. */
1115 warnx("reverting to binary display only");
1121 editlist_populate(device, cdb_len, dbd, pc, page, subpage,
1122 task_attr, retry_count, timeout);
1126 if (pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_CURRENT &&
1127 pc << PAGE_CTRL_SHIFT != SMS_PAGE_CTRL_SAVED)
1128 errx(EX_USAGE, "it only makes sense to edit page 0 "
1129 "(current) or page 3 (saved values)");
1132 editlist_save_desc(device, cdb_len, llbaa, pc, page,
1133 subpage, task_attr, retry_count, timeout);
1135 editlist_save(device, cdb_len, dbd, pc, page, subpage,
1136 task_attr, retry_count, timeout);
1138 } else if (binary || STAILQ_EMPTY(&editlist)) {
1139 /* Display without formatting information. */
1141 modepage_dump_desc(device, cdb_len, llbaa, pc, page,
1142 subpage, task_attr, retry_count, timeout);
1144 modepage_dump(device, cdb_len, dbd, pc, page, subpage,
1145 task_attr, retry_count, timeout);
1148 /* Display with format. */
1149 modepage_write(stdout, 0);
1154 mode_list(struct cam_device *device, int cdb_len, int dbd, int pc, int subpages,
1155 int task_attr, int retry_count, int timeout)
1157 u_int8_t data[MAX_DATA_SIZE]; /* Buffer to hold mode parameters. */
1158 struct scsi_mode_page_header *mph;
1159 struct scsi_mode_page_header_sp *mphsp;
1160 struct pagename *nameentry;
1161 const char *pagedb_path;
1162 int len, off, page, subpage;
1164 if ((pagedb_path = getenv("SCSI_MODES")) == NULL)
1165 pagedb_path = DEFAULT_SCSI_MODE_DB;
1167 if (load_format(pagedb_path, 0, 0) != 0 && verbose && errno == ENOENT) {
1168 /* Modepage database file not found. */
1169 warn("cannot open modepage database \"%s\"", pagedb_path);
1172 /* Build the list of all mode pages by querying the "all pages" page. */
1173 mode_sense(device, &cdb_len, dbd, 0, pc, SMS_ALL_PAGES_PAGE,
1174 subpages ? SMS_SUBPAGE_ALL : 0,
1175 task_attr, retry_count, timeout, data, sizeof(data));
1177 /* Skip block descriptors. */
1179 struct scsi_mode_header_6 *mh =
1180 (struct scsi_mode_header_6 *)data;
1181 len = mh->data_length;
1182 off = sizeof(*mh) + mh->blk_desc_len;
1184 struct scsi_mode_header_10 *mh =
1185 (struct scsi_mode_header_10 *)data;
1186 len = scsi_2btoul(mh->data_length);
1187 off = sizeof(*mh) + scsi_2btoul(mh->blk_desc_len);
1189 /* Iterate through the pages in the reply. */
1191 /* Locate the next mode page header. */
1192 mph = (struct scsi_mode_page_header *)(data + off);
1194 if ((mph->page_code & SMPH_SPF) == 0) {
1195 page = mph->page_code & SMS_PAGE_CODE;
1197 off += sizeof(*mph) + mph->page_length;
1199 mphsp = (struct scsi_mode_page_header_sp *)mph;
1200 page = mphsp->page_code & SMS_PAGE_CODE;
1201 subpage = mphsp->subpage;
1202 off += sizeof(*mphsp) + scsi_2btoul(mphsp->page_length);
1205 nameentry = nameentry_lookup(page, subpage);
1207 printf("0x%02x\t%s\n", page,
1208 nameentry ? nameentry->name : "");
1210 printf("0x%02x,0x%02x\t%s\n", page, subpage,
1211 nameentry ? nameentry->name : "");