static int examine_pages(int sg_fd, int inq_pdt, int inq_byte6, const struct opts_t * optsp) { int k, res, header, mresp_len, len; unsigned char rbuf[256]; const char * cp; mresp_len = (optsp->do_raw || optsp->do_hex) ? sizeof(rbuf) : 4; for (header = 0, k = 0; k < PG_CODE_MAX; ++k) { if (optsp->do_six) { res = sg_ll_mode_sense6(sg_fd, 0, 0, k, 0, rbuf, mresp_len, 1, optsp->do_verbose); if (SG_LIB_CAT_INVALID_OP == res) { fprintf(stderr, ">>>>>> try again without the '-6' " "switch for a 10 byte MODE SENSE command\n"); return res; } else if (SG_LIB_CAT_NOT_READY == res) { fprintf(stderr, "MODE SENSE (6) failed, device not ready\n"); return res; } } else { res = sg_ll_mode_sense10(sg_fd, 0, 0, 0, k, 0, rbuf, mresp_len, 1, optsp->do_verbose); if (SG_LIB_CAT_INVALID_OP == res) { fprintf(stderr, ">>>>>> try again with a '-6' " "switch for a 6 byte MODE SENSE command\n"); return res; } else if (SG_LIB_CAT_NOT_READY == res) { fprintf(stderr, "MODE SENSE (10) failed, device not ready\n"); return res; } } if (0 == res) { len = optsp->do_six ? (rbuf[0] + 1) : ((rbuf[0] << 8) + rbuf[1] + 2); if (len > mresp_len) len = mresp_len; if (optsp->do_raw) { dStrRaw((const char *)rbuf, len); continue; } if (0 == header) { printf("Discovered mode pages:\n"); header = 1; } cp = find_page_code_desc(k, 0, inq_pdt, inq_byte6, -1); if (cp) printf(" %s\n", cp); else printf(" [0x%x]\n", k); if (optsp->do_hex) dStrHex((const char *)rbuf, len, 1); } } return res; }
/* Get expected extended self-test time from mode page 0xa (for '-e') */ static int do_modes_0a(int sg_fd, void * resp, int mx_resp_len, int noisy, int mode6, int verbose) { int res; if (mode6) res = sg_ll_mode_sense6(sg_fd, 1 /* dbd */, 0 /* pc */, 0xa /* page */, 0, resp, mx_resp_len, noisy, verbose); else res = sg_ll_mode_sense10(sg_fd, 0 /* llbaa */, 1 /* dbd */, 0, 0xa, 0, resp, mx_resp_len, noisy, verbose); if (res) { char b[80]; sg_get_category_sense_str(res, sizeof(b), b, verbose); fprintf(stderr, "Mode sense (%s): %s\n", (mode6 ? "6" : "10"), b); } return res; }
/* Fetches current, changeable, default and/or saveable modes pages as * indicated by pcontrol_arr for given pg_code and sub_pg_code. If * mode6==0 then use MODE SENSE (10) else use MODE SENSE (6). If * flexible set and mode data length seems wrong then try and * fix (compensating hack for bad device or driver). pcontrol_arr * should have 4 elements for output of current, changeable, default * and saved values respectively. Each element should be NULL or * at least mx_mpage_len bytes long. * Return of 0 -> overall success, various SG_LIB_CAT_* positive values or * -1 -> other errors. * If success_mask pointer is not NULL then first zeros it. Then set bits * 0, 1, 2 and/or 3 if the current, changeable, default and saved values * respectively have been fetched. If error on current page * then stops and returns that error; otherwise continues if an error is * detected but returns the first error encountered. */ int sg_get_mode_page_controls(int sg_fd, int mode6, int pg_code, int sub_pg_code, int dbd, int flexible, int mx_mpage_len, int * success_mask, void * pcontrol_arr[], int * reported_len, int verbose) { int k, n, res, offset, calc_len, xfer_len, resp_mode6; unsigned char buff[MODE_RESP_ARB_LEN]; char ebuff[EBUFF_SZ]; int first_err = 0; if (success_mask) *success_mask = 0; if (reported_len) *reported_len = 0; if (mx_mpage_len < 4) return 0; memset(ebuff, 0, sizeof(ebuff)); /* first try to find length of current page response */ memset(buff, 0, MODE10_RESP_HDR_LEN); if (mode6) /* want first 8 bytes just in case */ res = sg_ll_mode_sense6(sg_fd, dbd, 0 /* pc */, pg_code, sub_pg_code, buff, MODE10_RESP_HDR_LEN, 1, verbose); else res = sg_ll_mode_sense10(sg_fd, 0 /* llbaa */, dbd, 0 /* pc */, pg_code, sub_pg_code, buff, MODE10_RESP_HDR_LEN, 1, verbose); if (0 != res) return res; n = buff[0]; if (reported_len) *reported_len = mode6 ? (n + 1) : (sg_get_unaligned_be16(buff) + 2); resp_mode6 = mode6; if (flexible) { if (mode6 && (n < 3)) { resp_mode6 = 0; if (verbose) pr2ws(">>> msense(6) but resp[0]=%d so try msense(10) " "response processing\n", n); } if ((0 == mode6) && (n > 5)) { if ((n > 11) && (0 == (n % 2)) && (0 == buff[4]) && (0 == buff[5]) && (0 == buff[6])) { buff[1] = n; buff[0] = 0; if (verbose) pr2ws(">>> msense(10) but resp[0]=%d and not msense(6) " "response so fix length\n", n); } else resp_mode6 = 1; } } if (verbose && (resp_mode6 != mode6)) pr2ws(">>> msense(%d) but resp[0]=%d so switch response " "processing\n", (mode6 ? 6 : 10), buff[0]); calc_len = resp_mode6 ? (buff[0] + 1) : (sg_get_unaligned_be16(buff) + 2); if (calc_len > MODE_RESP_ARB_LEN) calc_len = MODE_RESP_ARB_LEN; offset = sg_mode_page_offset(buff, calc_len, resp_mode6, ebuff, EBUFF_SZ); if (offset < 0) { if (('\0' != ebuff[0]) && (verbose > 0)) pr2ws("%s: %s\n", __func__, ebuff); return SG_LIB_CAT_MALFORMED; } xfer_len = calc_len - offset; if (xfer_len > mx_mpage_len) xfer_len = mx_mpage_len; for (k = 0; k < 4; ++k) { if (NULL == pcontrol_arr[k]) continue; memset(pcontrol_arr[k], 0, mx_mpage_len); if (mode6) res = sg_ll_mode_sense6(sg_fd, dbd, k /* pc */, pg_code, sub_pg_code, buff, calc_len, 1, verbose); else res = sg_ll_mode_sense10(sg_fd, 0 /* llbaa */, dbd, k /* pc */, pg_code, sub_pg_code, buff, calc_len, 1, verbose); if (0 != res) { if (0 == first_err) first_err = res; if (0 == k) break; /* if problem on current page, it won't improve */ else continue; } if (xfer_len > 0) memcpy(pcontrol_arr[k], buff + offset, xfer_len); if (success_mask) *success_mask |= (1 << k); } return first_err; }
int main(int argc, char * argv[]) { int sg_fd, res, c, num, alloc_len, off, pdt; int k, md_len, hdr_len, bd_len, mask_in_len; unsigned u, uu; int dbd = 0; int got_contents = 0; int force = 0; int got_mask = 0; int mode_6 = 0; int pg_code = -1; int sub_pg_code = 0; int save = 0; int verbose = 0; int read_in_len = 0; const char * device_name = NULL; unsigned char read_in[MX_ALLOC_LEN]; unsigned char mask_in[MX_ALLOC_LEN]; unsigned char ref_md[MX_ALLOC_LEN]; char ebuff[EBUFF_SZ]; struct sg_simple_inquiry_resp inq_data; int ret = 0; while (1) { int option_index = 0; c = getopt_long(argc, argv, "c:dfhl:m:p:svV", long_options, &option_index); if (c == -1) break; switch (c) { case 'c': memset(read_in, 0, sizeof(read_in)); if (0 != build_mode_page(optarg, read_in, &read_in_len, sizeof(read_in))) { fprintf(stderr, "bad argument to '--contents'\n"); return SG_LIB_SYNTAX_ERROR; } got_contents = 1; break; case 'd': dbd = 1; break; case 'f': force = 1; break; case 'h': case '?': usage(); return 0; case 'l': num = sscanf(optarg, "%d", &res); if ((1 == num) && ((6 == res) || (10 == res))) mode_6 = (6 == res) ? 1 : 0; else { fprintf(stderr, "length (of cdb) must be 6 or 10\n"); return SG_LIB_SYNTAX_ERROR; } break; case 'm': memset(mask_in, 0xff, sizeof(mask_in)); if (0 != build_mask(optarg, mask_in, &mask_in_len, sizeof(mask_in))) { fprintf(stderr, "bad argument to '--mask'\n"); return SG_LIB_SYNTAX_ERROR; } got_mask = 1; break; case 'p': if (NULL == strchr(optarg, ',')) { num = sscanf(optarg, "%x", &u); if ((1 != num) || (u > 62)) { fprintf(stderr, "Bad page code value after '--page' " "switch\n"); return SG_LIB_SYNTAX_ERROR; } pg_code = u; } else if (2 == sscanf(optarg, "%x,%x", &u, &uu)) { if (uu > 254) { fprintf(stderr, "Bad sub page code value after '--page'" " switch\n"); return SG_LIB_SYNTAX_ERROR; } pg_code = u; sub_pg_code = uu; } else { fprintf(stderr, "Bad page code, subpage code sequence after " "'--page' switch\n"); return SG_LIB_SYNTAX_ERROR; } break; case 's': save = 1; break; case 'v': ++verbose; break; case 'V': fprintf(stderr, ME "version: %s\n", version_str); return 0; default: fprintf(stderr, "unrecognised option code 0x%x ??\n", c); usage(); return SG_LIB_SYNTAX_ERROR; } } if (optind < argc) { if (NULL == device_name) { device_name = argv[optind]; ++optind; } if (optind < argc) { for (; optind < argc; ++optind) fprintf(stderr, "Unexpected extra argument: %s\n", argv[optind]); usage(); return SG_LIB_SYNTAX_ERROR; } } if (NULL == device_name) { fprintf(stderr, "missing device name!\n"); usage(); return SG_LIB_SYNTAX_ERROR; } if (pg_code < 0) { fprintf(stderr, "need page code (see '--page=')\n"); usage(); return SG_LIB_SYNTAX_ERROR; } if (got_mask && force) { fprintf(stderr, "cannot use both '--force' and '--mask'\n"); usage(); return SG_LIB_SYNTAX_ERROR; } sg_fd = sg_cmds_open_device(device_name, 0 /* rw */, verbose); if (sg_fd < 0) { fprintf(stderr, ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); return SG_LIB_FILE_ERROR; } if (0 == sg_simple_inquiry(sg_fd, &inq_data, 0, verbose)) pdt = inq_data.peripheral_type; else pdt = 0x1f; /* do MODE SENSE to fetch current values */ memset(ref_md, 0, MX_ALLOC_LEN); alloc_len = mode_6 ? SHORT_ALLOC_LEN : MX_ALLOC_LEN; if (mode_6) res = sg_ll_mode_sense6(sg_fd, dbd, 0 /*current */, pg_code, sub_pg_code, ref_md, alloc_len, 1, verbose); else res = sg_ll_mode_sense10(sg_fd, 0 /* llbaa */, dbd, 0 /* current */, pg_code, sub_pg_code, ref_md, alloc_len, 1, verbose); ret = res; if (SG_LIB_CAT_INVALID_OP == res) { fprintf(stderr, "MODE SENSE (%d) not supported, try '--len=%d'\n", (mode_6 ? 6 : 10), (mode_6 ? 10 : 6)); goto err_out; } else if (SG_LIB_CAT_NOT_READY == res) { fprintf(stderr, "MODE SENSE (%d) failed, device not ready\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_UNIT_ATTENTION == res) { fprintf(stderr, "MODE SENSE (%d) failed, unit attention\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_ABORTED_COMMAND == res) { fprintf(stderr, "MODE SENSE (%d) failed, aborted command\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_ILLEGAL_REQ == res) { fprintf(stderr, "bad field in MODE SENSE (%d) command\n", (mode_6 ? 6 : 10)); goto err_out; } else if (0 != res) { fprintf(stderr, "MODE SENSE (%d) failed\n", (mode_6 ? 6 : 10)); goto err_out; } off = sg_mode_page_offset(ref_md, alloc_len, mode_6, ebuff, EBUFF_SZ); if (off < 0) { fprintf(stderr, "MODE SENSE (%d): %s\n", (mode_6 ? 6 : 10), ebuff); goto err_out; } if (mode_6) { hdr_len = 4; md_len = ref_md[0] + 1; bd_len = ref_md[3]; } else { hdr_len = 8; md_len = (ref_md[0] << 8) + ref_md[1] + 2; bd_len = (ref_md[6] << 8) + ref_md[7]; } if (got_contents) { if (read_in_len < 2) { fprintf(stderr, "contents length=%d too short\n", read_in_len); goto err_out; } ref_md[0] = 0; /* mode data length reserved for mode select */ if (! mode_6) ref_md[1] = 0; /* mode data length reserved for mode select */ if (0 == pdt) /* for disks mask out DPOFUA bit */ ref_md[mode_6 ? 2 : 3] &= 0xef; if (md_len > alloc_len) { fprintf(stderr, "mode data length=%d exceeds allocation " "length=%d\n", md_len, alloc_len); goto err_out; } if (got_mask) { for (k = 0; k < (md_len - off); ++k) { if ((0x0 == mask_in[k]) || (k > read_in_len)) read_in[k] = ref_md[off + k]; else if (mask_in[k] < 0xff) { c = (ref_md[off + k] & (0xff & ~mask_in[k])); read_in[k] = (c | (read_in[k] & mask_in[k])); } } read_in_len = md_len - off; } if (! force) { if ((! (ref_md[off] & 0x80)) && save) { fprintf(stderr, "PS bit in existing mode page indicates that " "it is not saveable\n but '--save' option given\n"); goto err_out; } read_in[0] &= 0x7f; /* mask out PS bit, reserved in mode select */ if ((md_len - off) != read_in_len) { fprintf(stderr, "contents length=%d but reference mode page " "length=%d\n", read_in_len, md_len - off); goto err_out; } if (pg_code != (read_in[0] & 0x3f)) { fprintf(stderr, "contents page_code=0x%x but reference " "page_code=0x%x\n", (read_in[0] & 0x3f), pg_code); goto err_out; } if ((read_in[0] & 0x40) != (ref_md[off] & 0x40)) { fprintf(stderr, "contents flags subpage but reference page" "does not (or vice versa)\n"); goto err_out; } if ((read_in[0] & 0x40) && (read_in[1] != sub_pg_code)) { fprintf(stderr, "contents subpage_code=0x%x but reference " "sub_page_code=0x%x\n", read_in[1], sub_pg_code); goto err_out; } } else md_len = off + read_in_len; /* force length */ memcpy(ref_md + off, read_in, read_in_len); if (mode_6) res = sg_ll_mode_select6(sg_fd, 1, save, ref_md, md_len, 1, verbose); else res = sg_ll_mode_select10(sg_fd, 1, save, ref_md, md_len, 1, verbose); ret = res; if (SG_LIB_CAT_INVALID_OP == res) { fprintf(stderr, "MODE SELECT (%d) not supported\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_NOT_READY == res) { fprintf(stderr, "MODE SELECT (%d) failed, device not ready\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_UNIT_ATTENTION == res) { fprintf(stderr, "MODE SELECT (%d) failed, unit attention\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_ABORTED_COMMAND == res) { fprintf(stderr, "MODE SELECT (%d) failed, aborted command\n", (mode_6 ? 6 : 10)); goto err_out; } else if (SG_LIB_CAT_ILLEGAL_REQ == res) { fprintf(stderr, "bad field in MODE SELECT (%d) command\n", (mode_6 ? 6 : 10)); goto err_out; } else if (0 != res) { fprintf(stderr, "MODE SELECT (%d) failed\n", (mode_6 ? 6 : 10)); goto err_out; } } else { printf(">>> No contents given, so show current mode page data:\n"); printf(" header:\n"); dStrHex((const char *)ref_md, hdr_len, -1); if (bd_len) { printf(" block descriptor(s):\n"); dStrHex((const char *)(ref_md + hdr_len), bd_len, -1); } else printf(" << no block descriptors >>\n"); printf(" mode page:\n"); dStrHex((const char *)(ref_md + off), md_len - off, -1); } err_out: res = sg_cmds_close_device(sg_fd); if (res < 0) { fprintf(stderr, "close error: %s\n", safe_strerror(-res)); if (0 == ret) return SG_LIB_FILE_ERROR; } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }
/* Fetches current, changeable, default and/or saveable modes pages as * indicated by pcontrol_arr for given pg_code and sub_pg_code. If * mode6==false then use MODE SENSE (10) else use MODE SENSE (6). If * flexible set and mode data length seems wrong then try and * fix (compensating hack for bad device or driver). pcontrol_arr * should have 4 elements for output of current, changeable, default * and saved values respectively. Each element should be NULL or * at least mx_mpage_len bytes long. * Return of 0 -> overall success, various SG_LIB_CAT_* positive values or * -1 -> other errors. * If success_mask pointer is not NULL then first zeros it. Then set bits * 0, 1, 2 and/or 3 if the current, changeable, default and saved values * respectively have been fetched. If error on current page * then stops and returns that error; otherwise continues if an error is * detected but returns the first error encountered. */ int sg_get_mode_page_controls(int sg_fd, bool mode6, int pg_code, int sub_pg_code, bool dbd, bool flexible, int mx_mpage_len, int * success_mask, void * pcontrol_arr[], int * reported_lenp, int verbose) { bool resp_mode6; int k, n, res, offset, calc_len, xfer_len; int resid = 0; const int msense10_hlen = MODE10_RESP_HDR_LEN; uint8_t buff[MODE_RESP_ARB_LEN]; char ebuff[EBUFF_SZ]; int first_err = 0; if (success_mask) *success_mask = 0; if (reported_lenp) *reported_lenp = 0; if (mx_mpage_len < 4) return 0; memset(ebuff, 0, sizeof(ebuff)); /* first try to find length of current page response */ memset(buff, 0, msense10_hlen); if (mode6) /* want first 8 bytes just in case */ res = sg_ll_mode_sense6(sg_fd, dbd, 0 /* pc */, pg_code, sub_pg_code, buff, msense10_hlen, true, verbose); else /* MODE SENSE(10) obviously */ res = sg_ll_mode_sense10_v2(sg_fd, false /* llbaa */, dbd, 0 /* pc */, pg_code, sub_pg_code, buff, msense10_hlen, 0, &resid, true, verbose); if (0 != res) return res; n = buff[0]; if (reported_lenp) { int m; m = sg_msense_calc_length(buff, msense10_hlen, mode6, NULL) - resid; if (m < 0) /* Grrr, this should not happen */ m = 0; *reported_lenp = m; } resp_mode6 = mode6; if (flexible) { if (mode6 && (n < 3)) { resp_mode6 = false; if (verbose) pr2ws(">>> msense(6) but resp[0]=%d so try msense(10) " "response processing\n", n); } if ((! mode6) && (n > 5)) { if ((n > 11) && (0 == (n % 2)) && (0 == buff[4]) && (0 == buff[5]) && (0 == buff[6])) { buff[1] = n; buff[0] = 0; if (verbose) pr2ws(">>> msense(10) but resp[0]=%d and not msense(6) " "response so fix length\n", n); } else resp_mode6 = true; } } if (verbose && (resp_mode6 != mode6)) pr2ws(">>> msense(%d) but resp[0]=%d so switch response " "processing\n", (mode6 ? 6 : 10), buff[0]); calc_len = sg_msense_calc_length(buff, msense10_hlen, resp_mode6, NULL); if (calc_len > MODE_RESP_ARB_LEN) calc_len = MODE_RESP_ARB_LEN; offset = sg_mode_page_offset(buff, calc_len, resp_mode6, ebuff, EBUFF_SZ); if (offset < 0) { if (('\0' != ebuff[0]) && (verbose > 0)) pr2ws("%s: %s\n", __func__, ebuff); return SG_LIB_CAT_MALFORMED; } xfer_len = calc_len - offset; if (xfer_len > mx_mpage_len) xfer_len = mx_mpage_len; for (k = 0; k < 4; ++k) { if (NULL == pcontrol_arr[k]) continue; memset(pcontrol_arr[k], 0, mx_mpage_len); resid = 0; if (mode6) res = sg_ll_mode_sense6(sg_fd, dbd, k /* pc */, pg_code, sub_pg_code, buff, calc_len, true, verbose); else res = sg_ll_mode_sense10_v2(sg_fd, false /* llbaa */, dbd, k /* pc */, pg_code, sub_pg_code, buff, calc_len, 0, &resid, true, verbose); if (res || resid) { if (0 == first_err) { if (res) first_err = res; else { first_err = -49; /* unexpected resid != 0 */ if (verbose) pr2ws("%s: unexpected resid=%d, page=0x%x, " "pcontrol=%d\n", __func__, resid, pg_code, k); } } if (0 == k) break; /* if problem on current page, it won't improve */ else continue; } if (xfer_len > 0) memcpy(pcontrol_arr[k], buff + offset, xfer_len); if (success_mask) *success_mask |= (1 << k); } return first_err; }
int main(int argc, char * argv[]) { int sg_fd, k, num, len, res, md_len, bd_len, longlba, page_num, spf; char ebuff[EBUFF_SZ]; const char * descp; unsigned char * rsp_buff = NULL; unsigned char def_rsp_buff[DEF_ALLOC_LEN]; unsigned char * malloc_rsp_buff = NULL; int rsp_buff_size = DEF_ALLOC_LEN; int ret = 0; int density_code_off, t_proto, inq_pdt, inq_byte6, resp_mode6; int num_ua_pages; unsigned char * ucp; unsigned char uc; struct sg_simple_inquiry_resp inq_out; char pdt_name[64]; struct opts_t opts; memset(&opts, 0, sizeof(opts)); opts.pg_code = -1; res = process_cl(&opts, argc, argv); if (res) return SG_LIB_SYNTAX_ERROR; if (opts.do_help) { usage_for(&opts); return 0; } if (opts.do_version) { fprintf(stderr, "Version string: %s\n", version_str); return 0; } if (NULL == opts.device_name) { if (opts.do_list) { if ((opts.pg_code < 0) || (opts.pg_code > PG_CODE_MAX)) { printf(" Assume peripheral device type: disk\n"); list_page_codes(0, 0, -1); } else { printf(" peripheral device type: %s\n", sg_get_pdt_str(opts.pg_code, sizeof(pdt_name), pdt_name)); if (opts.subpg_code_set) list_page_codes(opts.pg_code, 0, opts.subpg_code); else list_page_codes(opts.pg_code, 0, -1); } return 0; } fprintf(stderr, "No DEVICE argument given\n"); usage_for(&opts); return SG_LIB_SYNTAX_ERROR; } if (opts.do_examine && (opts.pg_code >= 0)) { fprintf(stderr, "can't give '-e' and a page number\n"); return SG_LIB_SYNTAX_ERROR; } if ((opts.do_six) && (opts.do_llbaa)) { fprintf(stderr, "LLBAA not defined for MODE SENSE 6, try " "without '-L'\n"); return SG_LIB_SYNTAX_ERROR; } if (opts.maxlen > 0) { if (opts.do_six && (opts.maxlen > 255)) { fprintf(stderr, "For Mode Sense (6) maxlen cannot exceed " "255\n"); return SG_LIB_SYNTAX_ERROR; } if (opts.maxlen > DEF_ALLOC_LEN) { malloc_rsp_buff = (unsigned char *)malloc(opts.maxlen); if (NULL == malloc_rsp_buff) { fprintf(stderr, "Unable to malloc maxlen=%d bytes\n", opts.maxlen); return SG_LIB_SYNTAX_ERROR; } rsp_buff = malloc_rsp_buff; } else rsp_buff = def_rsp_buff; rsp_buff_size = opts.maxlen; } else { /* maxlen == 0 */ rsp_buff_size = opts.do_six ? DEF_6_ALLOC_LEN : DEF_ALLOC_LEN; rsp_buff = def_rsp_buff; } /* If no pages or list selected than treat as 'a' */ if (! ((opts.pg_code >= 0) || opts.do_all || opts.do_list || opts.do_examine)) opts.do_all = 1; if (opts.do_raw) { if (sg_set_binary_mode(STDOUT_FILENO) < 0) { perror("sg_set_binary_mode"); return SG_LIB_FILE_ERROR; } } if ((sg_fd = sg_cmds_open_device(opts.device_name, 1 /* ro */, opts.do_verbose)) < 0) { fprintf(stderr, "error opening file: %s: %s\n", opts.device_name, safe_strerror(-sg_fd)); if (malloc_rsp_buff) free(malloc_rsp_buff); return SG_LIB_FILE_ERROR; } if (sg_simple_inquiry(sg_fd, &inq_out, 1, opts.do_verbose)) { fprintf(stderr, "%s doesn't respond to a SCSI INQUIRY\n", opts.device_name); ret = SG_LIB_CAT_OTHER; goto finish; } inq_pdt = inq_out.peripheral_type; inq_byte6 = inq_out.byte_6; if (0 == opts.do_raw) printf(" %.8s %.16s %.4s peripheral_type: %s [0x%x]\n", inq_out.vendor, inq_out.product, inq_out.revision, sg_get_pdt_str(inq_pdt, sizeof(pdt_name), pdt_name), inq_pdt); if (opts.do_list) { if (opts.subpg_code_set) list_page_codes(inq_pdt, inq_byte6, opts.subpg_code); else list_page_codes(inq_pdt, inq_byte6, -1); goto finish; } if (opts.do_examine) { ret = examine_pages(sg_fd, inq_pdt, inq_byte6, &opts); goto finish; } if (PG_CODE_ALL == opts.pg_code) { if (0 == opts.do_all) ++opts.do_all; } else if (opts.do_all) opts.pg_code = PG_CODE_ALL; if (opts.do_all > 1) opts.subpg_code = SPG_CODE_ALL; if (opts.do_raw > 1) { if (opts.do_all) { if (opts.opt_new) fprintf(stderr, "'-R' requires a specific (sub)page, not " "all\n"); else fprintf(stderr, "'-r' requires a specific (sub)page, not " "all\n"); usage_for(&opts); ret = SG_LIB_SYNTAX_ERROR; goto finish; } } memset(rsp_buff, 0, sizeof(rsp_buff)); if (opts.do_six) { res = sg_ll_mode_sense6(sg_fd, opts.do_dbd, opts.page_control, opts.pg_code, opts.subpg_code, rsp_buff, rsp_buff_size, 1, opts.do_verbose); if (SG_LIB_CAT_INVALID_OP == res) fprintf(stderr, ">>>>>> try again without the '-6' " "switch for a 10 byte MODE SENSE command\n"); } else { res = sg_ll_mode_sense10(sg_fd, opts.do_llbaa, opts.do_dbd, opts.page_control, opts.pg_code, opts.subpg_code, rsp_buff, rsp_buff_size, 1, opts.do_verbose); if (SG_LIB_CAT_INVALID_OP == res) fprintf(stderr, ">>>>>> try again with a '-6' " "switch for a 6 byte MODE SENSE command\n"); } if (SG_LIB_CAT_ILLEGAL_REQ == res) { if (opts.subpg_code > 0) fprintf(stderr, "invalid field in cdb (perhaps subpages " "not supported)\n"); else if (opts.page_control > 0) fprintf(stderr, "invalid field in cdb (perhaps " "page control (PC) not supported)\n"); else fprintf(stderr, "invalid field in cdb (perhaps " "page 0x%x not supported)\n", opts.pg_code); } else if (SG_LIB_CAT_NOT_READY == res) fprintf(stderr, "device not ready\n"); else if (SG_LIB_CAT_UNIT_ATTENTION == res) fprintf(stderr, "unit attention\n"); else if (SG_LIB_CAT_ABORTED_COMMAND == res) fprintf(stderr, "aborted command\n"); ret = res; if (0 == res) { int medium_type, specific, headerlen; ret = 0; resp_mode6 = opts.do_six; if (opts.do_flexible) { num = rsp_buff[0]; if (opts.do_six && (num < 3)) resp_mode6 = 0; if ((0 == opts.do_six) && (num > 5)) { if ((num > 11) && (0 == (num % 2)) && (0 == rsp_buff[4]) && (0 == rsp_buff[5]) && (0 == rsp_buff[6])) { rsp_buff[1] = num; rsp_buff[0] = 0; fprintf(stderr, ">>> msense(10) but resp[0]=%d and " "not msense(6) response so fix length\n", num); } else resp_mode6 = 1; } } if ((! opts.do_raw) && (1 != opts.do_hex)) { if (resp_mode6 == opts.do_six) printf("Mode parameter header from MODE SENSE(%s):\n", (opts.do_six ? "6" : "10")); else printf(" >>> Mode parameter header from MODE SENSE(%s),\n" " decoded as %s byte response:\n", (opts.do_six ? "6" : "10"), (resp_mode6 ? "6" : "10")); } if (resp_mode6) { headerlen = 4; md_len = rsp_buff[0] + 1; bd_len = rsp_buff[3]; medium_type = rsp_buff[1]; specific = rsp_buff[2]; longlba = 0; } else { headerlen = 8; md_len = (rsp_buff[0] << 8) + rsp_buff[1] + 2; bd_len = (rsp_buff[6] << 8) + rsp_buff[7]; medium_type = rsp_buff[2]; specific = rsp_buff[3]; longlba = rsp_buff[4] & 1; } if ((bd_len + headerlen) > md_len) { fprintf(stderr, "Invalid block descriptor length=%d, ignore\n", bd_len); bd_len = 0; } if (opts.do_raw) { if (1 == opts.do_raw) dStrRaw((const char *)rsp_buff, md_len); else { ucp = rsp_buff + bd_len + headerlen; md_len -= bd_len + headerlen; spf = ((ucp[0] & 0x40) ? 1 : 0); len = (spf ? ((ucp[2] << 8) + ucp[3] + 4) : (ucp[1] + 2)); len = (len < md_len) ? len : md_len; for (k = 0; k < len; ++k) printf("%02x\n", ucp[k]); } goto finish; } if (1 == opts.do_hex) { dStrHex((const char *)rsp_buff, md_len, 1); goto finish; } else if (opts.do_hex > 1) dStrHex((const char *)rsp_buff, headerlen, 1); if (0 == inq_pdt) printf(" Mode data length=%d, medium type=0x%.2x, WP=%d," " DpoFua=%d, longlba=%d\n", md_len, medium_type, !!(specific & 0x80), !!(specific & 0x10), longlba); else printf(" Mode data length=%d, medium type=0x%.2x, specific" " param=0x%.2x, longlba=%d\n", md_len, medium_type, specific, longlba); if (md_len > rsp_buff_size) { printf("Only fetched %d bytes of response, truncate output\n", rsp_buff_size); md_len = rsp_buff_size; if (bd_len + headerlen > rsp_buff_size) bd_len = rsp_buff_size - headerlen; } if (! opts.do_dbout) { printf(" Block descriptor length=%d\n", bd_len); if (bd_len > 0) { len = 8; density_code_off = 0; num = bd_len; if (longlba) { printf("> longlba direct access device block " "descriptors:\n"); len = 16; density_code_off = 8; } else if (0 == inq_pdt) { printf("> Direct access device block descriptors:\n"); density_code_off = 4; } else printf("> General mode parameter block descriptors:\n"); ucp = rsp_buff + headerlen; while (num > 0) { printf(" Density code=0x%x\n", *(ucp + density_code_off)); dStrHex((const char *)ucp, len, 1); ucp += len; num -= len; } printf("\n"); } } ucp = rsp_buff + bd_len + headerlen; /* start of mode page(s) */ md_len -= bd_len + headerlen; /* length of mode page(s) */ num_ua_pages = 0; for (k = 0; md_len > 0; ++k) { /* got mode page(s) */ if ((k > 0) && (! opts.do_all) && (SPG_CODE_ALL != opts.subpg_code)) { fprintf(stderr, "Unexpectedly received extra mode page " "responses, ignore\n"); break; } uc = *ucp; spf = ((uc & 0x40) ? 1 : 0); len = (spf ? ((ucp[2] << 8) + ucp[3] + 4) : (ucp[1] + 2)); page_num = ucp[0] & PG_CODE_MASK; if (0x0 == page_num) { ++num_ua_pages; if((num_ua_pages > 3) && (md_len > 0xa00)) { fprintf(stderr, ">>> Seen 3 unit attention pages " "(only one should be at end)\n and mpage " "length=%d, looks malformed, try '-f' option\n", md_len); break; } } if (opts.do_hex) { if (spf) printf(">> page_code=0x%x, subpage_code=0x%x, page_cont" "rol=%d\n", page_num, ucp[1], opts.page_control); else printf(">> page_code=0x%x, page_control=%d\n", page_num, opts.page_control); } else { descp = NULL; if ((0x18 == page_num) || (0x19 == page_num)) { t_proto = (spf ? ucp[5] : ucp[2]) & 0xf; descp = find_page_code_desc(page_num, (spf ? ucp[1] : 0), inq_pdt, inq_byte6, t_proto); } else descp = find_page_code_desc(page_num, (spf ? ucp[1] : 0), inq_pdt, inq_byte6, -1); if (NULL == descp) { if (spf) snprintf(ebuff, EBUFF_SZ, "0x%x, subpage_code: 0x%x", page_num, ucp[1]); else snprintf(ebuff, EBUFF_SZ, "0x%x", page_num); } if (descp) printf(">> %s, page_control: %s\n", descp, pg_control_str_arr[opts.page_control]); else printf(">> page_code: %s, page_control: %s\n", ebuff, pg_control_str_arr[opts.page_control]); } num = (len > md_len) ? md_len : len; if ((k > 0) && (num > 256)) { num = 256; fprintf(stderr, ">>> page length (%d) > 256 bytes, unlikely " "trim\n Try '-f' option\n", len); } dStrHex((const char *)ucp, num , 1); ucp += len; md_len -= len; } } finish: sg_cmds_close_device(sg_fd); if (malloc_rsp_buff) free(malloc_rsp_buff); return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }
int main(int argc, char * argv[]) { unsigned char rsp_buff[MX_ALLOC_LEN]; char **argptr; char * file_name = 0; int res, fd, k, lun = -1; int fail_all = 0; int fail_path = 0; int ret = 0; if (argc < 2) { usage (); return SG_LIB_SYNTAX_ERROR; } for (k = 1; k < argc; ++k) { argptr = argv + k; if (!strcmp (*argptr, "-v")) ++do_verbose; else if (!strncmp(*argptr, "-f=",3)) { ++fail_path; lun = strtoul(*argptr + 3, NULL, 0); } else if (!strcmp(*argptr, "-a")) { ++fail_all; } else if (!strcmp(*argptr, "-V")) { fprintf(stderr, "sg_rdac version: %s\n", version_str); return 0; } else if (*argv[k] == '-') { fprintf(stderr, "Unrecognized switch: %s\n", argv[k]); file_name = 0; break; } else if (0 == file_name) file_name = argv[k]; else { fprintf(stderr, "too many arguments\n"); file_name = 0; break; } } if (0 == file_name) { usage(); return SG_LIB_SYNTAX_ERROR; } fd = sg_cmds_open_device(file_name, 0 /* rw */, do_verbose); if (fd < 0) { fprintf(stderr, "open error: %s: %s\n", file_name, safe_strerror(-fd)); usage(); return SG_LIB_FILE_ERROR; } if (fail_all) { res = fail_all_paths(fd); } else if (fail_path) { res = fail_this_path(fd, lun); } else { res = sg_ll_mode_sense6(fd, /*DBD*/ 0, /* page control */0, 0x2c, 0, rsp_buff, 252, 1, do_verbose); if (!res) { if (do_verbose) dump_mode_page(rsp_buff, rsp_buff[0]); print_rdac_mode(rsp_buff); } else { if (SG_LIB_CAT_INVALID_OP == res) fprintf(stderr, ">>>>>> try again without " "the '-6' switch for a 10 byte MODE " "SENSE command\n"); else if (SG_LIB_CAT_ILLEGAL_REQ == res) fprintf(stderr, "mode sense: invalid field " "in cdb (perhaps subpages or page " "control (PC) not supported)\n"); else { char b[80]; sg_get_category_sense_str(res, sizeof(b), b, do_verbose); fprintf(stderr, "mode sense failed: %s\n", b); } } } ret = res; res = sg_cmds_close_device(fd); if (res < 0) { fprintf(stderr, "close error: %s\n", safe_strerror(-res)); if (0 == ret) return SG_LIB_FILE_ERROR; } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }