94
fprintf(stderr, "Usage: "
95
"sg_write_same [--10] [--16] [--32] [--anchor] [--grpnum=GN] "
97
" [--in=IF] [--lba=LBA] [--lbdata] "
98
"[--num=NUM] [--pbdata]\n"
99
" [--timeout=TO] [--unmap] [--verbose] "
101
" [--wrprotect=WRP] [xferlen=LEN] DEVICE\n"
103
" --10|-R do WRITE SAME(10) (even if '--unmap' "
105
" --16|-S do WRITE SAME(16) (def: 10 unless "
107
" or LBA+NUM needs more than 32 bits)\n"
108
" --32|-T do WRITE SAME(32) (def: 10 or 16)\n"
109
" --anchor|-a set anchor field in cdb\n"
110
" --grpnum=GN|-g GN GN is group number field (def: 0)\n"
111
" --help|-h print out usage message\n"
112
" --in=IF|-i IF IF is file to fetch one block of data "
114
" bytes or whole file). Block written to "
116
" --lba=LBA|-l LBA LBA is the logical block address to "
118
" --lbdata|-L set LBDATA bit\n"
119
" --num=NUM|-n NUM NUM is number of logical blocks to write "
121
" [Beware NUM==0 means rest of device]\n"
122
" --pbdata|-P set PBDATA bit\n"
123
" --timeout=TO|-t TO command timeout (unit: seconds) (def: "
125
" --unmap|-U set UNMAP bit\n"
126
" --verbose|-v increase verbosity\n"
127
" --version|-V print version string then exit\n"
128
" --wrprotect=WPR|-w WPR WPR is the WRPROTECT field value "
130
" --xferlen=LEN|-x LEN LEN is number of bytes from IF to "
132
" DEVICE (def: IF file length)\n\n"
133
"Performs a SCSI WRITE SAME (10, 16 or 32) command\n"
96
fprintf(stderr, "Usage: "
97
"sg_write_same [--10] [--16] [--32] [--anchor] [--grpnum=GN] "
99
" [--in=IF] [--lba=LBA] [--lbdata] "
100
"[--ndob] [--num=NUM]\n"
101
" [--pbdata] [--timeout=TO] [--unmap] "
103
" [--version] [--wrprotect=WRP] "
107
" --10|-R do WRITE SAME(10) (even if '--unmap' "
109
" --16|-S do WRITE SAME(16) (def: 10 unless "
111
" or LBA+NUM needs more than 32 bits)\n"
112
" --32|-T do WRITE SAME(32) (def: 10 or 16)\n"
113
" --anchor|-a set anchor field in cdb\n"
114
" --grpnum=GN|-g GN GN is group number field (def: 0)\n"
115
" --help|-h print out usage message\n"
116
" --in=IF|-i IF IF is file to fetch one block of data "
118
" bytes or whole file). Block written to "
120
" --lba=LBA|-l LBA LBA is the logical block address to "
122
" --lbdata|-L set LBDATA bit\n"
123
" --ndob|-N set 'no data-out buffer' bit\n"
124
" --num=NUM|-n NUM NUM is number of logical blocks to "
126
" [Beware NUM==0 means rest of device]\n"
127
" --pbdata|-P set PBDATA bit\n"
128
" --timeout=TO|-t TO command timeout (unit: seconds) (def: "
130
" --unmap|-U set UNMAP bit\n"
131
" --verbose|-v increase verbosity\n"
132
" --version|-V print version string then exit\n"
133
" --wrprotect=WPR|-w WPR WPR is the WRPROTECT field value "
135
" --xferlen=LEN|-x LEN LEN is number of bytes from IF to "
137
" DEVICE (def: IF file length)\n\n"
138
"Performs a SCSI WRITE SAME (10, 16 or 32) command\n"
478
502
return SG_LIB_FILE_ERROR;
482
if (0 == opts.xfer_len) {
483
res = sg_ll_readcap_16(sg_fd, 0 /* pmi */, 0 /* llba */, resp_buff,
484
RCAP16_RESP_LEN, 0, (vb ? (vb - 1): 0));
487
dStrHex((const char *)resp_buff, RCAP16_RESP_LEN, 1);
488
block_size = ((resp_buff[8] << 24) |
489
(resp_buff[9] << 16) |
490
(resp_buff[10] << 8) |
492
prot_en = !!(resp_buff[12] & 0x1);
493
opts.xfer_len = block_size;
494
if (prot_en && (opts.wrprotect > 0))
496
} else if ((SG_LIB_CAT_INVALID_OP == res) ||
497
(SG_LIB_CAT_ILLEGAL_REQ == res)) {
499
fprintf(stderr, "Read capacity(16) not supported, try Read "
501
res = sg_ll_readcap_10(sg_fd, 0 /* pmi */, 0 /* lba */, resp_buff,
502
RCAP10_RESP_LEN, 0, (vb ? (vb - 1): 0));
507
if (0 == opts.xfer_len) {
508
res = sg_ll_readcap_16(sg_fd, 0 /* pmi */, 0 /* llba */, resp_buff,
509
RCAP16_RESP_LEN, 0, (vb ? (vb - 1): 0));
505
dStrHex((const char *)resp_buff, RCAP10_RESP_LEN, 1);
506
block_size = ((resp_buff[4] << 24) |
507
(resp_buff[5] << 16) |
508
(resp_buff[6] << 8) |
512
dStrHex((const char *)resp_buff, RCAP16_RESP_LEN, 1);
513
block_size = ((resp_buff[8] << 24) |
514
(resp_buff[9] << 16) |
515
(resp_buff[10] << 8) |
517
prot_en = !!(resp_buff[12] & 0x1);
510
518
opts.xfer_len = block_size;
519
if (prot_en && (opts.wrprotect > 0))
521
} else if ((SG_LIB_CAT_INVALID_OP == res) ||
522
(SG_LIB_CAT_ILLEGAL_REQ == res)) {
524
fprintf(stderr, "Read capacity(16) not supported, try "
525
"Read capacity(10)\n");
526
res = sg_ll_readcap_10(sg_fd, 0 /* pmi */, 0 /* lba */,
527
resp_buff, RCAP10_RESP_LEN, 0,
531
dStrHex((const char *)resp_buff, RCAP10_RESP_LEN, 1);
532
block_size = ((resp_buff[4] << 24) |
533
(resp_buff[5] << 16) |
534
(resp_buff[6] << 8) |
536
opts.xfer_len = block_size;
539
fprintf(stderr, "Read capacity(16) failed. Unable to "
540
"calculate block size\n");
542
fprintf(stderr, "Read capacity(10) failed. Unable to "
543
"calculate block size\n");
545
if (opts.xfer_len < 1) {
546
fprintf(stderr, "unable to deduce block size, please give "
547
"'--xferlen=' argument\n");
548
ret = SG_LIB_SYNTAX_ERROR;
551
if (opts.xfer_len > MAX_XFER_LEN) {
552
fprintf(stderr, "'--xferlen=%d is out of range ( want <= %d)\n",
553
opts.xfer_len, MAX_XFER_LEN);
554
ret = SG_LIB_SYNTAX_ERROR;
557
wBuff = (unsigned char*)calloc(opts.xfer_len, 1);
559
fprintf(stderr, "unable to allocate %d bytes of memory with "
560
"calloc()\n", opts.xfer_len);
561
ret = SG_LIB_SYNTAX_ERROR;
564
if (opts.ifilename[0]) {
567
if (sg_set_binary_mode(STDIN_FILENO) < 0)
568
perror("sg_set_binary_mode");
570
if ((infd = open(opts.ifilename, O_RDONLY)) < 0) {
571
snprintf(ebuff, EBUFF_SZ, ME "could not open %s for "
572
"reading", opts.ifilename);
574
ret = SG_LIB_FILE_ERROR;
576
} else if (sg_set_binary_mode(infd) < 0)
577
perror("sg_set_binary_mode");
513
fprintf(stderr, "Read capacity(16) failed. Unable to calculate "
516
fprintf(stderr, "Read capacity(10) failed. Unable to calculate "
519
if (opts.xfer_len < 1) {
520
fprintf(stderr, "unable to deduce block size, please give "
521
"'--xferlen=' argument\n");
522
ret = SG_LIB_SYNTAX_ERROR;
525
if (opts.xfer_len > MAX_XFER_LEN) {
526
fprintf(stderr, "'--xferlen=%d is out of range ( want <= %d)\n",
527
opts.xfer_len, MAX_XFER_LEN);
528
ret = SG_LIB_SYNTAX_ERROR;
531
wBuff = (unsigned char*)calloc(opts.xfer_len, 1);
533
fprintf(stderr, "unable to allocate %d bytes of memory with "
534
"calloc()\n", opts.xfer_len);
535
ret = SG_LIB_SYNTAX_ERROR;
538
if (opts.ifilename[0]) {
541
if (sg_set_binary_mode(STDIN_FILENO) < 0)
542
perror("sg_set_binary_mode");
544
if ((infd = open(opts.ifilename, O_RDONLY)) < 0) {
545
snprintf(ebuff, EBUFF_SZ,
546
ME "could not open %s for reading", opts.ifilename);
579
res = read(infd, wBuff, opts.xfer_len);
581
snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s",
548
586
ret = SG_LIB_FILE_ERROR;
550
} else if (sg_set_binary_mode(infd) < 0)
551
perror("sg_set_binary_mode");
553
res = read(infd, wBuff, opts.xfer_len);
555
snprintf(ebuff, EBUFF_SZ, ME "couldn't read from %s",
589
if (res < opts.xfer_len) {
590
fprintf(stderr, "tried to read %d bytes from %s, got %d "
591
"bytes\n", opts.xfer_len, opts.ifilename, res);
592
fprintf(stderr, " so pad with 0x0 bytes and continue\n");
560
ret = SG_LIB_FILE_ERROR;
563
if (res < opts.xfer_len) {
564
fprintf(stderr, "tried to read %d bytes from %s, got %d "
565
"bytes\n", opts.xfer_len, opts.ifilename, res);
566
fprintf(stderr, " so pad with 0x0 bytes and continue\n");
572
fprintf(stderr, "Default data-out buffer set to %d zeros\n",
574
if (prot_en && (opts.wrprotect > 0)) {
575
/* default for protection is 0xff, rest get 0x0 */
576
memset(wBuff + opts.xfer_len - 8, 0xff, 8);
578
fprintf(stderr, " ... apart from last 8 bytes which are set "
598
fprintf(stderr, "Default data-out buffer set to %d zeros\n",
600
if (prot_en && (opts.wrprotect > 0)) {
601
/* default for protection is 0xff, rest get 0x0 */
602
memset(wBuff + opts.xfer_len - 8, 0xff, 8);
604
fprintf(stderr, " ... apart from last 8 bytes which are "