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