static int prin_work(int sg_fd, const struct opts_t * op) { int k, j, num, add_len, add_desc_len; int res = 0; unsigned int pr_gen; uint8_t * bp; uint8_t * pr_buff = NULL; uint8_t * free_pr_buff = NULL; pr_buff = sg_memalign(op->alloc_len, 0 /* page aligned */, &free_pr_buff, false); if (NULL == pr_buff) { pr2serr("%s: unable to allocate %d bytes on heap\n", __func__, op->alloc_len); return sg_convert_errno(ENOMEM); } res = sg_ll_persistent_reserve_in(sg_fd, op->prin_sa, pr_buff, op->alloc_len, true, op->verbose); if (res) { char b[64]; char bb[80]; if (op->prin_sa < num_prin_sa_strs) snprintf(b, sizeof(b), "%s", prin_sa_strs[op->prin_sa]); else snprintf(b, sizeof(b), "service action=0x%x", op->prin_sa); if (SG_LIB_CAT_INVALID_OP == res) pr2serr("PR in (%s): command not supported\n", b); else if (SG_LIB_CAT_ILLEGAL_REQ == res) pr2serr("PR in (%s): bad field in cdb or parameter list (perhaps " "unsupported service action)\n", b); else { sg_get_category_sense_str(res, sizeof(bb), bb, op->verbose); pr2serr("PR in (%s): %s\n", b, bb); } goto fini; } if (PRIN_RCAP_SA == op->prin_sa) { if (8 != pr_buff[1]) { pr2serr("Unexpected response for PRIN Report Capabilities\n"); if (op->hex) hex2stdout(pr_buff, pr_buff[1], 1); res = SG_LIB_CAT_MALFORMED; goto fini; } if (op->hex) hex2stdout(pr_buff, 8, 1); else { printf("Report capabilities response:\n"); printf(" Replace Lost Reservation Capable(RLR_C): %d\n", !!(pr_buff[2] & 0x80)); /* added spc4r26 */ printf(" Compatible Reservation Handling(CRH): %d\n", !!(pr_buff[2] & 0x10)); printf(" Specify Initiator Ports Capable(SIP_C): %d\n", !!(pr_buff[2] & 0x8)); printf(" All Target Ports Capable(ATP_C): %d\n", !!(pr_buff[2] & 0x4)); printf(" Persist Through Power Loss Capable(PTPL_C): %d\n", !!(pr_buff[2] & 0x1)); printf(" Type Mask Valid(TMV): %d\n", !!(pr_buff[3] & 0x80)); printf(" Allow Commands: %d\n", (pr_buff[3] >> 4) & 0x7); printf(" Persist Through Power Loss Active(PTPL_A): %d\n", !!(pr_buff[3] & 0x1)); if (pr_buff[3] & 0x80) { printf(" Support indicated in Type mask:\n"); printf(" %s: %d\n", pr_type_strs[7], !!(pr_buff[4] & 0x80)); /* WR_EX_AR */ printf(" %s: %d\n", pr_type_strs[6], !!(pr_buff[4] & 0x40)); /* EX_AC_RO */ printf(" %s: %d\n", pr_type_strs[5], !!(pr_buff[4] & 0x20)); /* WR_EX_RO */ printf(" %s: %d\n", pr_type_strs[3], !!(pr_buff[4] & 0x8)); /* EX_AC */ printf(" %s: %d\n", pr_type_strs[1], !!(pr_buff[4] & 0x2)); /* WR_EX */ printf(" %s: %d\n", pr_type_strs[8], !!(pr_buff[5] & 0x1)); /* EX_AC_AR */ } } } else {
int main(int argc, char * argv[]) { bool do_long = false; bool o_readonly = false; bool do_raw = false; bool verbose_given = false; bool version_given = false; int res, c, len, k; int sg_fd = -1; int do_help = 0; int do_hex = 0; int rb_id = 0; int rb_len = 4; int rb_mode = 0; int rb_mode_sp = 0; int resid = 0; int verbose = 0; int ret = 0; int64_t ll; uint64_t rb_offset = 0; const char * device_name = NULL; uint8_t * resp; const struct mode_s * mp; while (1) { int option_index = 0; c = getopt_long(argc, argv, "hHi:l:Lm:o:rRS:vV", long_options, &option_index); if (c == -1) break; switch (c) { case 'h': case '?': ++do_help; break; case 'H': ++do_hex; break; case 'i': rb_id = sg_get_num(optarg); if ((rb_id < 0) || (rb_id > 255)) { pr2serr("argument to '--id' should be in the range 0 to " "255\n"); return SG_LIB_SYNTAX_ERROR; } break; case 'l': rb_len = sg_get_num(optarg); if (rb_len < 0) { pr2serr("bad argument to '--length'\n"); return SG_LIB_SYNTAX_ERROR; } if (rb_len > 0xffffff) { pr2serr("argument to '--length' must be <= 0xffffff\n"); return SG_LIB_SYNTAX_ERROR; } break; case 'L': do_long = true; break; case 'm': if (isdigit(*optarg)) { rb_mode = sg_get_num(optarg); if ((rb_mode < 0) || (rb_mode > 31)) { pr2serr("argument to '--mode' should be in the range 0 " "to 31\n"); return SG_LIB_SYNTAX_ERROR; } } else { len = strlen(optarg); for (mp = modes; mp->mode_string; ++mp) { if (0 == strncmp(mp->mode_string, optarg, len)) { rb_mode = mp->mode; break; } } if (NULL == mp->mode_string) { print_modes(); return SG_LIB_SYNTAX_ERROR; } } break; case 'o': ll = sg_get_llnum(optarg); if (ll < 0) { pr2serr("bad argument to '--offset'\n"); return SG_LIB_SYNTAX_ERROR; } rb_offset = ll; break; case 'r': do_raw = true; break; case 'R': o_readonly = true; break; case 'S': rb_mode_sp = sg_get_num(optarg); if ((rb_mode_sp < 0) || (rb_mode_sp > 7)) { pr2serr("expected argument to '--specific' to be 0 to 7\n"); return SG_LIB_SYNTAX_ERROR; } break; case 'v': verbose_given = true; ++verbose; break; case 'V': version_given = true; break; default: pr2serr("unrecognised option code 0x%x ??\n", c); usage(); return SG_LIB_SYNTAX_ERROR; } } if (do_help) { if (do_help > 1) { usage(); pr2serr("\n"); print_modes(); } else usage(); return 0; } if (optind < argc) { if (NULL == device_name) { device_name = argv[optind]; ++optind; } if (optind < argc) { for (; optind < argc; ++optind) pr2serr("Unexpected extra argument: %s\n", argv[optind]); usage(); return SG_LIB_SYNTAX_ERROR; } } #ifdef DEBUG pr2serr("In DEBUG mode, "); if (verbose_given && version_given) { pr2serr("but override: '-vV' given, zero verbose and continue\n"); verbose_given = false; version_given = false; verbose = 0; } else if (! verbose_given) { pr2serr("set '-vv'\n"); verbose = 2; } else pr2serr("keep verbose=%d\n", verbose); #else if (verbose_given && version_given) pr2serr("Not in DEBUG mode, so '-vV' has no special action\n"); #endif if (version_given) { pr2serr("version: %s\n", version_str); return 0; } if (NULL == device_name) { pr2serr("Missing device name!\n\n"); usage(); return SG_LIB_SYNTAX_ERROR; } len = rb_len ? rb_len : 8; resp = (uint8_t *)malloc(len); if (NULL == resp) { pr2serr("unable to allocate %d bytes on the heap\n", len); return SG_LIB_CAT_OTHER; } memset(resp, 0, len); if (do_raw) { if (sg_set_binary_mode(STDOUT_FILENO) < 0) { perror("sg_set_binary_mode"); ret = SG_LIB_FILE_ERROR; goto fini; } } #ifdef SG_LIB_WIN32 #ifdef SG_LIB_WIN32_DIRECT if (verbose > 4) pr2serr("Initial win32 SPT interface state: %s\n", scsi_pt_win32_spt_state() ? "direct" : "indirect"); scsi_pt_win32_direct(SG_LIB_WIN32_DIRECT /* SPT pt interface */); #endif #endif sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { if (verbose) pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); goto fini; } if (do_long) res = sg_ll_read_buffer_16(sg_fd, rb_mode, rb_mode_sp, rb_id, rb_offset, resp, rb_len, &resid, true, verbose); else if (rb_offset > 0xffffff) { pr2serr("--offset value is too large for READ BUFFER(10), try " "--16\n"); ret = SG_LIB_SYNTAX_ERROR; goto fini; } else res = sg_ll_read_buffer_10(sg_fd, rb_mode, rb_mode_sp, rb_id, (uint32_t)rb_offset, resp, rb_len, &resid, true, verbose); if (0 != res) { char b[80]; ret = res; if (res > 0) { sg_get_category_sense_str(res, sizeof(b), b, verbose); pr2serr("Read buffer(%d) failed: %s\n", (do_long ? 16 : 10), b); } goto fini; } if (resid > 0) rb_len -= resid; /* got back less than requested */ if (rb_len > 0) { if (do_raw) dStrRaw(resp, rb_len); else if (do_hex || (rb_len < 4)) hex2stdout((const uint8_t *)resp, rb_len, ((do_hex > 1) ? 0 : 1)); else { switch (rb_mode) { case MODE_DESCRIPTOR: k = sg_get_unaligned_be24(resp + 1); printf("OFFSET BOUNDARY: %d, Buffer offset alignment: " "%d-byte\n", resp[0], (1 << resp[0])); printf("BUFFER CAPACITY: %d (0x%x)\n", k, k); break; case MODE_ECHO_BDESC: k = sg_get_unaligned_be16(resp + 2) & 0x1fff; printf("EBOS:%d\n", resp[0] & 1 ? 1 : 0); printf("Echo buffer capacity: %d (0x%x)\n", k, k); break; default: hex2stdout((const uint8_t *)resp, rb_len, (verbose > 1 ? 0 : 1)); break; } } } fini: if (resp) free(resp); if (sg_fd >= 0) { res = sg_cmds_close_device(sg_fd); if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) ret = sg_convert_errno(-res); } } if (0 == verbose) { if (! sg_if_can2stderr("sg_read_buffer failed: ", ret)) pr2serr("Some error occurred, try again with '-v' " "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }
static int do_identify_dev(int sg_fd, bool do_packet, int cdb_len, bool ck_cond, bool extend, bool do_ident, int do_hex, bool do_raw, int verbose) { bool t_type = false;/* false -> 512 byte blocks, true -> device's LB size */ bool t_dir = true; /* false -> to device, true -> from device */ bool byte_block = true; /* false -> bytes, true -> 512 byte blocks (if t_type=false) */ bool got_ard = false; /* got ATA result descriptor */ bool got_fixsense = false; /* got ATA result in fixed format sense */ bool ok; int j, res, ret, sb_sz; /* Following for ATA READ/WRITE MULTIPLE (EXT) cmds, normally 0 */ int multiple_count = 0; int protocol = 4; /* PIO data-in */ int t_length = 2; /* 0 -> no data transferred, 2 -> sector count */ int resid = 0; uint64_t ull; struct sg_scsi_sense_hdr ssh; uint8_t inBuff[ID_RESPONSE_LEN]; uint8_t sense_buffer[64]; uint8_t ata_return_desc[16]; uint8_t apt_cdb[SAT_ATA_PASS_THROUGH16_LEN] = {SAT_ATA_PASS_THROUGH16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint8_t apt12_cdb[SAT_ATA_PASS_THROUGH12_LEN] = {SAT_ATA_PASS_THROUGH12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; uint8_t apt32_cdb[SAT_ATA_PASS_THROUGH32_LEN]; const unsigned short * usp; sb_sz = sizeof(sense_buffer); memset(sense_buffer, 0, sb_sz); memset(apt32_cdb, 0, sizeof(apt32_cdb)); memset(ata_return_desc, 0, sizeof(ata_return_desc)); ok = false; switch (cdb_len) { case SAT_ATA_PASS_THROUGH32_LEN: /* SAT-4 revision 5 or later */ /* Prepare SCSI ATA PASS-THROUGH COMMAND(32) command */ sg_put_unaligned_be16(1, apt32_cdb + 22); /* count=1 */ apt32_cdb[25] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE : ATA_IDENTIFY_DEVICE); apt32_cdb[10] = (multiple_count << 5) | (protocol << 1); if (extend) apt32_cdb[10] |= 0x1; apt32_cdb[11] = t_length; if (ck_cond) apt32_cdb[11] |= 0x20; if (t_type) apt32_cdb[11] |= 0x10; if (t_dir) apt32_cdb[11] |= 0x8; if (byte_block) apt32_cdb[11] |= 0x4; /* following call takes care of all bytes below offset 10 in cdb */ res = sg_ll_ata_pt(sg_fd, apt32_cdb, cdb_len, DEF_TIMEOUT, inBuff, NULL /* doutp */, ID_RESPONSE_LEN, sense_buffer, sb_sz, ata_return_desc, sizeof(ata_return_desc), &resid, verbose); break; case SAT_ATA_PASS_THROUGH16_LEN: /* Prepare SCSI ATA PASS-THROUGH COMMAND(16) command */ apt_cdb[6] = 1; /* sector count */ apt_cdb[14] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE : ATA_IDENTIFY_DEVICE); apt_cdb[1] = (multiple_count << 5) | (protocol << 1); if (extend) apt_cdb[1] |= 0x1; apt_cdb[2] = t_length; if (ck_cond) apt_cdb[2] |= 0x20; if (t_type) apt_cdb[2] |= 0x10; if (t_dir) apt_cdb[2] |= 0x8; if (byte_block) apt_cdb[2] |= 0x4; res = sg_ll_ata_pt(sg_fd, apt_cdb, cdb_len, DEF_TIMEOUT, inBuff, NULL /* doutp */, ID_RESPONSE_LEN, sense_buffer, sb_sz, ata_return_desc, sizeof(ata_return_desc), &resid, verbose); break; case SAT_ATA_PASS_THROUGH12_LEN: /* Prepare SCSI ATA PASS-THROUGH COMMAND(12) command */ apt12_cdb[4] = 1; /* sector count */ apt12_cdb[9] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE : ATA_IDENTIFY_DEVICE); apt12_cdb[1] = (multiple_count << 5) | (protocol << 1); apt12_cdb[2] = t_length; if (ck_cond) apt12_cdb[2] |= 0x20; if (t_type) apt12_cdb[2] |= 0x10; if (t_dir) apt12_cdb[2] |= 0x8; if (byte_block) apt12_cdb[2] |= 0x4; res = sg_ll_ata_pt(sg_fd, apt12_cdb, cdb_len, DEF_TIMEOUT, inBuff, NULL /* doutp */, ID_RESPONSE_LEN, sense_buffer, sb_sz, ata_return_desc, sizeof(ata_return_desc), &resid, verbose); break; default: pr2serr("%s: bad cdb_len=%d\n", __func__, cdb_len); return -1; } if (0 == res) { ok = true; if (verbose > 2) pr2serr("command completed with SCSI GOOD status\n"); } else if ((res > 0) && (res & SAM_STAT_CHECK_CONDITION)) { if (verbose > 1) { pr2serr("ATA pass-through:\n"); sg_print_sense(NULL, sense_buffer, sb_sz, ((verbose > 2) ? 1 : 0)); } if (sg_scsi_normalize_sense(sense_buffer, sb_sz, &ssh)) { switch (ssh.sense_key) { case SPC_SK_ILLEGAL_REQUEST: if ((0x20 == ssh.asc) && (0x0 == ssh.ascq)) { ret = SG_LIB_CAT_INVALID_OP; if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d) not supported\n", cdb_len); } else { ret = SG_LIB_CAT_ILLEGAL_REQ; if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d), bad field in cdb\n", cdb_len); } return ret; case SPC_SK_NO_SENSE: case SPC_SK_RECOVERED_ERROR: if ((0x0 == ssh.asc) && (ASCQ_ATA_PT_INFO_AVAILABLE == ssh.ascq)) { if (0x72 == ssh.response_code) { if (SAT_ATA_RETURN_DESC != ata_return_desc[0]) { if (verbose) pr2serr("did not find ATA Return (sense) " "Descriptor\n"); return SG_LIB_CAT_RECOVERED; } got_ard = true; break; } else if (0x70 == ssh.response_code) { got_fixsense = true; break; } else { if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d), unexpected " "response_code=0x%x\n", ssh.response_code, cdb_len); return SG_LIB_CAT_RECOVERED; } } else if (SPC_SK_RECOVERED_ERROR == ssh.sense_key) return SG_LIB_CAT_RECOVERED; else { if ((0x0 == ssh.asc) && (0x0 == ssh.ascq)) break; return SG_LIB_CAT_SENSE; } case SPC_SK_UNIT_ATTENTION: if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d), Unit Attention detected\n", cdb_len); return SG_LIB_CAT_UNIT_ATTENTION; case SPC_SK_NOT_READY: if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d), device not ready\n", cdb_len); return SG_LIB_CAT_NOT_READY; case SPC_SK_MEDIUM_ERROR: case SPC_SK_HARDWARE_ERROR: if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d), medium or hardware " "error\n", cdb_len); return SG_LIB_CAT_MEDIUM_HARD; case SPC_SK_ABORTED_COMMAND: if (0x10 == ssh.asc) { pr2serr("Aborted command: protection information\n"); return SG_LIB_CAT_PROTECTION; } else { pr2serr("Aborted command: try again with%s '-p' option\n", (do_packet ? "out" : "")); return SG_LIB_CAT_ABORTED_COMMAND; } case SPC_SK_DATA_PROTECT: pr2serr("ATA PASS-THROUGH (%d): data protect, read only " "media?\n", cdb_len); return SG_LIB_CAT_DATA_PROTECT; default: if (verbose < 2) pr2serr("ATA PASS-THROUGH (%d), some sense data, use " "'-v' for more information\n", cdb_len); return SG_LIB_CAT_SENSE; } } else { pr2serr("CHECK CONDITION without response code ??\n"); return SG_LIB_CAT_SENSE; } if (0x72 != (sense_buffer[0] & 0x7f)) { pr2serr("expected descriptor sense format, response code=0x%x\n", sense_buffer[0]); return SG_LIB_CAT_MALFORMED; } } else if (res > 0) { if (SAM_STAT_RESERVATION_CONFLICT == res) { pr2serr("SCSI status: RESERVATION CONFLICT\n"); return SG_LIB_CAT_RES_CONFLICT; } else { pr2serr("Unexpected SCSI status=0x%x\n", res); return SG_LIB_CAT_MALFORMED; } } else { pr2serr("ATA pass-through (%d) failed\n", cdb_len); if (verbose < 2) pr2serr(" try adding '-v' for more information\n"); return -1; } if ((SAT_ATA_RETURN_DESC == ata_return_desc[0]) && (! got_ard)) pr2serr("Seem to have got ATA Result Descriptor but it was not " "indicated\n"); if (got_ard) { if (ata_return_desc[3] & 0x4) { pr2serr("error indication in returned FIS: aborted command\n"); pr2serr(" try again with%s '-p' option\n", (do_packet ? "out" : "")); return SG_LIB_CAT_ABORTED_COMMAND; } ok = true; } if (got_fixsense) { if (0x4 & sense_buffer[3]) { /* Error is MSB of Info field */ pr2serr("error indication in returned FIS: aborted command\n"); pr2serr(" try again with%s '-p' option\n", (do_packet ? "out" : "")); return SG_LIB_CAT_ABORTED_COMMAND; } ok = true; } if (ok) { /* output result if it is available */ if (do_raw) dStrRaw(inBuff, 512); else if (0 == do_hex) { if (do_ident) { usp = (const unsigned short *)inBuff; ull = 0; for (j = 0; j < 4; ++j) { if (j > 0) ull <<= 16; ull |= usp[108 + j]; } printf("0x%016" PRIx64 "\n", ull); } else { printf("Response for IDENTIFY %sDEVICE ATA command:\n", (do_packet ? "PACKET " : "")); dWordHex((const unsigned short *)inBuff, 256, 0, sg_is_big_endian()); } } else if (1 == do_hex) hex2stdout(inBuff, 512, 0); else if (2 == do_hex) dWordHex((const unsigned short *)inBuff, 256, 0, sg_is_big_endian()); else if (3 == do_hex) /* '-HHH' suitable for "hdparm --Istdin" */ dWordHex((const unsigned short *)inBuff, 256, -2, sg_is_big_endian()); else /* '-HHHH' hex bytes only */ hex2stdout(inBuff, 512, -1); } return 0; }
int main(int argc, char * argv[]) { bool correct = false; bool do_16 = false; bool pblock = false; bool readonly = false; bool got_stdout; bool verbose_given = false; bool version_given = false; int outfd, res, c; int sg_fd = -1; int ret = 0; int xfer_len = 520; int verbose = 0; uint64_t llba = 0; int64_t ll; uint8_t * readLongBuff = NULL; uint8_t * rawp = NULL; uint8_t * free_rawp = NULL; const char * device_name = NULL; char out_fname[256]; char ebuff[EBUFF_SZ]; memset(out_fname, 0, sizeof out_fname); while (1) { int option_index = 0; c = getopt_long(argc, argv, "chl:o:prSvVx:", long_options, &option_index); if (c == -1) break; switch (c) { case 'c': correct = true; break; case 'h': case '?': usage(); return 0; case 'l': ll = sg_get_llnum(optarg); if (-1 == ll) { pr2serr("bad argument to '--lba'\n"); return SG_LIB_SYNTAX_ERROR; } llba = (uint64_t)ll; break; case 'o': strncpy(out_fname, optarg, sizeof(out_fname) - 1); break; case 'p': pblock = true; break; case 'r': readonly = true; break; case 'S': do_16 = true; break; case 'v': verbose_given = true; ++verbose; break; case 'V': version_given = true; break; case 'x': xfer_len = sg_get_num(optarg); if (-1 == xfer_len) { pr2serr("bad argument to '--xfer_len'\n"); return SG_LIB_SYNTAX_ERROR; } break; default: pr2serr("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) pr2serr("Unexpected extra argument: %s\n", argv[optind]); usage(); return SG_LIB_SYNTAX_ERROR; } } #ifdef DEBUG pr2serr("In DEBUG mode, "); if (verbose_given && version_given) { pr2serr("but override: '-vV' given, zero verbose and continue\n"); verbose_given = false; version_given = false; verbose = 0; } else if (! verbose_given) { pr2serr("set '-vv'\n"); verbose = 2; } else pr2serr("keep verbose=%d\n", verbose); #else if (verbose_given && version_given) pr2serr("Not in DEBUG mode, so '-vV' has no special action\n"); #endif if (version_given) { pr2serr(ME "version: %s\n", version_str); return 0; } if (NULL == device_name) { pr2serr("Missing device name!\n\n"); usage(); return SG_LIB_SYNTAX_ERROR; } if (xfer_len >= MAX_XFER_LEN){ pr2serr("xfer_len (%d) is out of range ( < %d)\n", xfer_len, MAX_XFER_LEN); usage(); return SG_LIB_SYNTAX_ERROR; } sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { if (verbose) pr2serr(ME "open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); goto err_out; } if (NULL == (rawp = (uint8_t *)sg_memalign(MAX_XFER_LEN, 0, &free_rawp, false))) { if (verbose) pr2serr(ME "out of memory\n"); ret = sg_convert_errno(ENOMEM); goto err_out; } readLongBuff = (uint8_t *)rawp; memset(rawp, 0x0, MAX_XFER_LEN); pr2serr(ME "issue read long (%s) to device %s\n xfer_len=%d (0x%x), " "lba=%" PRIu64 " (0x%" PRIx64 "), correct=%d\n", (do_16 ? "16" : "10"), device_name, xfer_len, xfer_len, llba, llba, (int)correct); if ((ret = process_read_long(sg_fd, do_16, pblock, correct, llba, readLongBuff, xfer_len, verbose))) goto err_out; if ('\0' == out_fname[0]) hex2stdout((const uint8_t *)rawp, xfer_len, 0); else { got_stdout = (0 == strcmp(out_fname, "-")); if (got_stdout) outfd = STDOUT_FILENO; else { if ((outfd = open(out_fname, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { snprintf(ebuff, EBUFF_SZ, ME "could not open %s for writing", out_fname); perror(ebuff); goto err_out; } } if (sg_set_binary_mode(outfd) < 0) { perror("sg_set_binary_mode"); goto err_out; } res = write(outfd, readLongBuff, xfer_len); if (res < 0) { snprintf(ebuff, EBUFF_SZ, ME "couldn't write to %s", out_fname); perror(ebuff); goto err_out; } if (! got_stdout) close(outfd); } err_out: if (free_rawp) free(free_rawp); if (sg_fd >= 0) { res = sg_cmds_close_device(sg_fd); if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) ret = sg_convert_errno(-res); } } if (0 == verbose) { if (! sg_if_can2stderr("sg_read_long failed: ", ret)) pr2serr("Some error occurred, try again with '-v' " "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }
int main(int argc, char * argv[]) { bool do_one_segment = false; bool o_readonly = false; bool do_raw = false; bool verbose_given = false; bool version_given = false; int k, res, c, rlen; int sg_fd = -1; int do_hex = 0; int maxlen = DEF_REFER_BUFF_LEN; int verbose = 0; int desc = 0; int ret = 0; int64_t ll; uint64_t lba = 0; const char * device_name = NULL; const uint8_t * bp; uint8_t * referralBuffp = referralBuff; uint8_t * free_referralBuffp = NULL; while (1) { int option_index = 0; c = getopt_long(argc, argv, "hHl:m:rRsvV", long_options, &option_index); if (c == -1) break; switch (c) { case 'h': case '?': usage(); return 0; case 'H': ++do_hex; break; case 'l': ll = sg_get_llnum(optarg); if (-1 == ll) { pr2serr("bad argument to '--lba'\n"); return SG_LIB_SYNTAX_ERROR; } lba = (uint64_t)ll; break; case 'm': maxlen = sg_get_num(optarg); if ((maxlen < 0) || (maxlen > MAX_REFER_BUFF_LEN)) { pr2serr("argument to '--maxlen' should be %d or less\n", MAX_REFER_BUFF_LEN); return SG_LIB_SYNTAX_ERROR; } break; case 's': do_one_segment = true; break; case 'r': do_raw = true; break; case 'R': o_readonly = true; break; case 'v': verbose_given = true; ++verbose; break; case 'V': version_given = true; break; default: pr2serr("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) pr2serr("Unexpected extra argument: %s\n", argv[optind]); usage(); return SG_LIB_SYNTAX_ERROR; } } #ifdef DEBUG pr2serr("In DEBUG mode, "); if (verbose_given && version_given) { pr2serr("but override: '-vV' given, zero verbose and continue\n"); verbose_given = false; version_given = false; verbose = 0; } else if (! verbose_given) { pr2serr("set '-vv'\n"); verbose = 2; } else pr2serr("keep verbose=%d\n", verbose); #else if (verbose_given && version_given) pr2serr("Not in DEBUG mode, so '-vV' has no special action\n"); #endif if (version_given) { pr2serr("version: %s\n", version_str); return 0; } if (NULL == device_name) { pr2serr("No DEVICE argument given\n\n"); usage(); return SG_LIB_SYNTAX_ERROR; } if (maxlen > DEF_REFER_BUFF_LEN) { referralBuffp = (uint8_t *)sg_memalign(maxlen, 0, &free_referralBuffp, verbose > 3); if (NULL == referralBuffp) { pr2serr("unable to allocate %d bytes on heap\n", maxlen); return sg_convert_errno(ENOMEM); } } if (do_raw) { if (sg_set_binary_mode(STDOUT_FILENO) < 0) { perror("sg_set_binary_mode"); ret = SG_LIB_FILE_ERROR; goto free_buff; } } sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose); if (sg_fd < 0) { if (verbose) pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); goto free_buff; } res = sg_ll_report_referrals(sg_fd, lba, do_one_segment, referralBuffp, maxlen, true, verbose); ret = res; if (0 == res) { if (maxlen >= 4) /* * This is strictly speaking incorrect. However, the * spec reserved bytes 0 and 1, so some implementations * might want to use them to increase the number of * possible user segments. * And maybe someone takes a pity and updates the spec ... */ rlen = sg_get_unaligned_be32(referralBuffp + 0) + 4; else rlen = maxlen; k = (rlen > maxlen) ? maxlen : rlen; if (do_raw) { dStrRaw(referralBuffp, k); goto the_end; } if (do_hex) { hex2stdout(referralBuffp, k, 1); goto the_end; } if (maxlen < 4) { if (verbose) pr2serr("Exiting because allocation length (maxlen) less " "than 4\n"); goto the_end; } if ((verbose > 1) || (verbose && (rlen > maxlen))) { pr2serr("response length %d bytes\n", rlen); if (rlen > maxlen) pr2serr(" ... which is greater than maxlen (allocation " "length %d), truncation\n", maxlen); } if (rlen > maxlen) rlen = maxlen; bp = referralBuffp + 4; k = 0; printf("Report referrals:\n"); while (k < rlen - 4) { printf(" descriptor %d:\n", desc); res = decode_referral_desc(bp + k, rlen - 4 - k); if (res < 0) { pr2serr("bad user data segment referral descriptor\n"); break; } k += res; desc++; } } else { char b[80]; sg_get_category_sense_str(res, sizeof(b), b, verbose); pr2serr("Report Referrals command failed: %s\n", b); } the_end: res = sg_cmds_close_device(sg_fd); if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) ret = sg_convert_errno(-res); } free_buff: if (free_referralBuffp) free(free_referralBuffp); if (0 == verbose) { if (! sg_if_can2stderr("sg_referrals failed: ", ret)) pr2serr("Some error occurred, try again with '-v' " "or '-vv' for more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }
int main(int argc, char * argv[]) { bool do_raw = false; bool readonly = false; bool verbose_given = false; bool version_given = false; int sg_fd, k, m, res, c; int do_hex = 0; int verbose = 0; int ret = 0; uint32_t max_block_size; uint16_t min_block_size; const char * device_name = NULL; while (1) { int option_index = 0; c = getopt_long(argc, argv, "hHrRvV", long_options, &option_index); if (c == -1) break; switch (c) { case 'h': case '?': usage(); return 0; case 'H': ++do_hex; break; case 'r': do_raw = true; break; case 'R': readonly = true; break; case 'v': verbose_given = true; ++verbose; break; case 'V': version_given = true; break; default: pr2serr("invalid option -%c ??\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) pr2serr("Unexpected extra argument: %s\n", argv[optind]); usage(); return SG_LIB_SYNTAX_ERROR; } } #ifdef DEBUG pr2serr("In DEBUG mode, "); if (verbose_given && version_given) { pr2serr("but override: '-vV' given, zero verbose and continue\n"); verbose_given = false; version_given = false; verbose = 0; } else if (! verbose_given) { pr2serr("set '-vv'\n"); verbose = 2; } else pr2serr("keep verbose=%d\n", verbose); #else if (verbose_given && version_given) pr2serr("Not in DEBUG mode, so '-vV' has no special action\n"); #endif if (version_given) { pr2serr("version: %s\n", version_str); return 0; } if (NULL == device_name) { pr2serr("missing device name!\n"); usage(); return SG_LIB_SYNTAX_ERROR; } sg_fd = sg_cmds_open_device(device_name, readonly, verbose); if (sg_fd < 0) { if (verbose) pr2serr("open error: %s: %s\n", device_name, safe_strerror(-sg_fd)); ret = sg_convert_errno(-sg_fd); goto the_end2; } memset(readBlkLmtBuff, 0x0, 6); res = sg_ll_read_block_limits(sg_fd, readBlkLmtBuff, 6, true, verbose); ret = res; if (0 == res) { if (do_hex) { hex2stdout(readBlkLmtBuff, sizeof(readBlkLmtBuff), 1); goto the_end; } else if (do_raw) { dStrRaw((const char *)readBlkLmtBuff, sizeof(readBlkLmtBuff)); goto the_end; } max_block_size = sg_get_unaligned_be32(readBlkLmtBuff + 0); min_block_size = sg_get_unaligned_be16(readBlkLmtBuff + 4); k = min_block_size / 1024; pr2serr("Read Block Limits results:\n"); pr2serr("\tMinimum block size: %u byte(s)", (unsigned int)min_block_size); if (k != 0) pr2serr(", %d KB", k); pr2serr("\n"); k = max_block_size / 1024; m = max_block_size / 1048576; pr2serr("\tMaximum block size: %u byte(s)", (unsigned int)max_block_size); if (k != 0) pr2serr(", %d KB", k); if (m != 0) pr2serr(", %d MB", m); pr2serr("\n"); } else { char b[80]; sg_get_category_sense_str(res, sizeof(b), b, verbose); pr2serr("Read block limits: %s\n", b); if (0 == verbose) pr2serr(" try '-v' option for more information\n"); } the_end: res = sg_cmds_close_device(sg_fd); if (res < 0) { pr2serr("close error: %s\n", safe_strerror(-res)); if (0 == ret) ret = sg_convert_errno(-res); } the_end2: if (0 == verbose) { if (! sg_if_can2stderr("sg_read_block_limits failed: ", ret)) pr2serr("Some error occurred, try again with '-v' or '-vv' for " "more information\n"); } return (ret >= 0) ? ret : SG_LIB_CAT_OTHER; }