aboutsummaryrefslogtreecommitdiff
path: root/src/sg_dd.c
diff options
context:
space:
mode:
authorDouglas Gilbert <dgilbert@interlog.com>2014-09-18 05:20:09 +0000
committerDouglas Gilbert <dgilbert@interlog.com>2014-09-18 05:20:09 +0000
commita93e55ea7300ce369fb36750f6526bdcedeea3e1 (patch)
tree30c31796fe0461da93930254d8bb65f5c0b8b7d2 /src/sg_dd.c
parent992e601d27bdd94162f35a1f885e5f541186b1b9 (diff)
downloadsg3_utils-a93e55ea7300ce369fb36750f6526bdcedeea3e1.tar.gz
sg_vpd: more TPC VPD page decoding; sg_ses: handle element descriptor names that count a trailing NULL
git-svn-id: https://svn.bingwo.ca/repos/sg3_utils/trunk@607 6180dd3e-e324-4e3e-922d-17de1ae2f315
Diffstat (limited to 'src/sg_dd.c')
-rw-r--r--src/sg_dd.c55
1 files changed, 14 insertions, 41 deletions
diff --git a/src/sg_dd.c b/src/sg_dd.c
index 13e18471..8497a200 100644
--- a/src/sg_dd.c
+++ b/src/sg_dd.c
@@ -57,8 +57,9 @@
#include "sg_cmds_basic.h"
#include "sg_cmds_extra.h"
#include "sg_io_linux.h"
+#include "sg_unaligned.h"
-static const char * version_str = "5.80 20140516";
+static const char * version_str = "5.81 20140917";
#define ME "sg_dd: "
@@ -416,7 +417,7 @@ usage()
static int
scsi_read_capacity(int sg_fd, int64_t * num_sect, int * sect_sz)
{
- int k, res;
+ int res;
unsigned int ui;
unsigned char rcBuff[RCAP16_REPLY_LEN];
int verb;
@@ -434,20 +435,14 @@ scsi_read_capacity(int sg_fd, int64_t * num_sect, int * sect_sz)
verb);
if (0 != res)
return res;
- for (k = 0, ls = 0; k < 8; ++k) {
- ls <<= 8;
- ls |= rcBuff[k];
- }
+ ls = (int64_t)sg_get_unaligned_be64(rcBuff);
*num_sect = ls + 1;
- *sect_sz = (rcBuff[8] << 24) | (rcBuff[9] << 16) |
- (rcBuff[10] << 8) | rcBuff[11];
+ *sect_sz = (int)sg_get_unaligned_be32(rcBuff + 8);
} else {
- ui = ((rcBuff[0] << 24) | (rcBuff[1] << 16) | (rcBuff[2] << 8) |
- rcBuff[3]);
+ ui = sg_get_unaligned_be32(rcBuff);
/* take care not to sign extend values > 0x7fffffff */
*num_sect = (int64_t)ui + 1;
- *sect_sz = (rcBuff[4] << 24) | (rcBuff[5] << 16) |
- (rcBuff[6] << 8) | rcBuff[7];
+ *sect_sz = (int)sg_get_unaligned_be32(rcBuff + 4);
}
if (verbose)
fprintf(stderr, " number of blocks=%" PRId64 " [0x%" PRIx64
@@ -522,9 +517,7 @@ sg_build_scsi_cdb(unsigned char * cdbp, int cdb_sz, unsigned int blocks,
sz_ind = 0;
cdbp[0] = (unsigned char)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
- cdbp[1] = (unsigned char)((start_block >> 16) & 0x1f);
- cdbp[2] = (unsigned char)((start_block >> 8) & 0xff);
- cdbp[3] = (unsigned char)(start_block & 0xff);
+ sg_put_unaligned_be24(start_block, cdbp + 1);
cdbp[4] = (256 == blocks) ? 0 : (unsigned char)blocks;
if (blocks > 256) {
fprintf(stderr, ME "for 6 byte commands, maximum number of "
@@ -546,12 +539,8 @@ sg_build_scsi_cdb(unsigned char * cdbp, int cdb_sz, unsigned int blocks,
sz_ind = 1;
cdbp[0] = (unsigned char)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
- cdbp[2] = (unsigned char)((start_block >> 24) & 0xff);
- cdbp[3] = (unsigned char)((start_block >> 16) & 0xff);
- cdbp[4] = (unsigned char)((start_block >> 8) & 0xff);
- cdbp[5] = (unsigned char)(start_block & 0xff);
- cdbp[7] = (unsigned char)((blocks >> 8) & 0xff);
- cdbp[8] = (unsigned char)(blocks & 0xff);
+ sg_put_unaligned_be32(start_block, cdbp + 2);
+ sg_put_unaligned_be16(blocks, cdbp + 7);
if (blocks & (~0xffff)) {
fprintf(stderr, ME "for 10 byte commands, maximum number of "
"blocks is %d\n", 0xffff);
@@ -562,31 +551,15 @@ sg_build_scsi_cdb(unsigned char * cdbp, int cdb_sz, unsigned int blocks,
sz_ind = 2;
cdbp[0] = (unsigned char)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
- cdbp[2] = (unsigned char)((start_block >> 24) & 0xff);
- cdbp[3] = (unsigned char)((start_block >> 16) & 0xff);
- cdbp[4] = (unsigned char)((start_block >> 8) & 0xff);
- cdbp[5] = (unsigned char)(start_block & 0xff);
- cdbp[6] = (unsigned char)((blocks >> 24) & 0xff);
- cdbp[7] = (unsigned char)((blocks >> 16) & 0xff);
- cdbp[8] = (unsigned char)((blocks >> 8) & 0xff);
- cdbp[9] = (unsigned char)(blocks & 0xff);
+ sg_put_unaligned_be32(start_block, cdbp + 2);
+ sg_put_unaligned_be32(blocks, cdbp + 6);
break;
case 16:
sz_ind = 3;
cdbp[0] = (unsigned char)(write_true ? wr_opcode[sz_ind] :
rd_opcode[sz_ind]);
- cdbp[2] = (unsigned char)((start_block >> 56) & 0xff);
- cdbp[3] = (unsigned char)((start_block >> 48) & 0xff);
- cdbp[4] = (unsigned char)((start_block >> 40) & 0xff);
- cdbp[5] = (unsigned char)((start_block >> 32) & 0xff);
- cdbp[6] = (unsigned char)((start_block >> 24) & 0xff);
- cdbp[7] = (unsigned char)((start_block >> 16) & 0xff);
- cdbp[8] = (unsigned char)((start_block >> 8) & 0xff);
- cdbp[9] = (unsigned char)(start_block & 0xff);
- cdbp[10] = (unsigned char)((blocks >> 24) & 0xff);
- cdbp[11] = (unsigned char)((blocks >> 16) & 0xff);
- cdbp[12] = (unsigned char)((blocks >> 8) & 0xff);
- cdbp[13] = (unsigned char)(blocks & 0xff);
+ sg_put_unaligned_be64(start_block, cdbp + 2);
+ sg_put_unaligned_be32(blocks, cdbp + 10);
break;
default:
fprintf(stderr, ME "expected cdb size of 6, 10, 12, or 16 but got"