1 /*
2 * Copyright (c) 2006-2022 Luben Tuikov and Douglas Gilbert.
3 * All rights reserved.
4 * Use of this source code is governed by a BSD-style
5 * license that can be found in the BSD_LICENSE file.
6 *
7 * SPDX-License-Identifier: BSD-2-Clause
8 */
9
10 #include <unistd.h>
11 #include <fcntl.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <getopt.h>
20 #define __STDC_FORMAT_MACROS 1
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include "sg_lib.h"
30 #include "sg_lib_data.h"
31 #include "sg_cmds_basic.h"
32 #include "sg_cmds_extra.h"
33 #include "sg_pt.h"
34 #include "sg_unaligned.h"
35 #include "sg_pr2serr.h"
36
37 /*
38 * This utility issues the SCSI READ BUFFER(10 or 16) command to the given
39 * device.
40 */
41
42 static const char * version_str = "1.35 20220217"; /* spc6r06 */
43
44 #ifndef SG_READ_BUFFER_10_CMD
45 #define SG_READ_BUFFER_10_CMD 0x3c
46 #define SG_READ_BUFFER_10_CMDLEN 10
47 #endif
48 #ifndef SG_READ_BUFFER_16_CMD
49 #define SG_READ_BUFFER_16_CMD 0x9b
50 #define SG_READ_BUFFER_16_CMDLEN 16
51 #endif
52
53 #define MODE_HEADER_DATA 0
54 #define MODE_VENDOR 1
55 #define MODE_DATA 2
56 #define MODE_DESCRIPTOR 3
57 #define MODE_ECHO_BUFFER 0x0A
58 #define MODE_ECHO_BDESC 0x0B
59 #define MODE_READ_MICROCODE_ST 0x0F
60 #define MODE_EN_EX_ECHO 0x1A
61 #define MODE_ERR_HISTORY 0x1C
62
63 #define MAX_DEF_INHEX_LEN 8192
64 #define SENSE_BUFF_LEN 64 /* Arbitrary, could be larger */
65 #define DEF_PT_TIMEOUT 60 /* 60 seconds */
66 #define DEF_RESPONSE_LEN 4 /* increased to 64 for MODE_ERR_HISTORY */
67
68
69 static struct option long_options[] = {
70 {"16", no_argument, 0, 'L'},
71 {"eh_code", required_argument, 0, 'e'},
72 {"eh-code", required_argument, 0, 'e'},
73 {"help", no_argument, 0, 'h'},
74 {"hex", no_argument, 0, 'H'},
75 {"id", required_argument, 0, 'i'},
76 {"inhex", required_argument, 0, 'I'},
77 {"length", required_argument, 0, 'l'},
78 {"long", no_argument, 0, 'L'},
79 {"mode", required_argument, 0, 'm'},
80 {"no_output", no_argument, 0, 'N'},
81 {"no-output", no_argument, 0, 'N'},
82 {"offset", required_argument, 0, 'o'},
83 {"raw", no_argument, 0, 'r'},
84 {"readonly", no_argument, 0, 'R'},
85 {"specific", required_argument, 0, 'S'},
86 {"verbose", no_argument, 0, 'v'},
87 {"version", no_argument, 0, 'V'},
88 {0, 0, 0, 0}, /* sentinel */
89 };
90
91 struct opts_t {
92 bool do_long;
93 bool o_readonly;
94 bool do_raw;
95 bool eh_code_given;
96 bool no_output;
97 bool rb_id_given;
98 bool rb_len_given;
99 bool rb_mode_given;
100 bool verbose_given;
101 bool version_given;
102 int sg_fd;
103 int do_help;
104 int do_hex;
105 int eh_code;
106 int rb_id;
107 int rb_len;
108 int rb_mode;
109 int rb_mode_sp;
110 int verbose;
111 uint64_t rb_offset;
112 const char * device_name;
113 const char * inhex_name;
114 };
115
116
117 static void
usage()118 usage()
119 {
120 pr2serr("Usage: sg_read_buffer [--16] [--eh_code=EHC] [--help] [--hex] "
121 "[--id=ID]\n"
122 " [--inhex=FN] [--length=LEN] [--long] "
123 "[--mode=MO]\n"
124 " [--no_output] [--offset=OFF] [--raw] "
125 "[--readonly]\n"
126 " [--specific=MS] [--verbose] [--version] "
127 "DEVICE\n"
128 " where:\n"
129 " --16|-L issue READ BUFFER(16) (def: 10)\n"
130 " --eh_code=EHC|-e EHC same as '-m eh -i EHC' where "
131 "EHC is the\n"
132 " error history code\n"
133 " --help|-h print out usage message\n"
134 " --hex|-H print output in hex\n"
135 " --id=ID|-i ID buffer identifier (0 (default) to 255)\n"
136 " --inhex=FN|-I FN filename FN contains hex data to "
137 "decode\n"
138 " rather than DEVICE. If --raw given "
139 "then binary\n"
140 " --length=LEN|-l LEN length in bytes to read (def: 4, "
141 "64 for eh)\n"
142 " --long|-L issue READ BUFFER(16) (def: 10)\n"
143 " --mode=MO|-m MO read buffer mode, MO is number or "
144 "acronym (def: 0)\n"
145 " --no_output|-N perform the command then exit\n"
146 " --offset=OFF|-o OFF buffer offset (unit: bytes, def: 0)\n"
147 " --raw|-r output response in binary to stdout\n"
148 " --readonly|-R open DEVICE read-only (def: read-write)\n"
149 " --specific=MS|-S MS mode specific value; 3 bit field (0 "
150 "to 7)\n"
151 " --verbose|-v increase verbosity\n"
152 " --version|-V print version string and exit\n\n"
153 "Performs a SCSI READ BUFFER (10 or 16) command. Use '-m xxx' to "
154 "list\navailable modes. Some responses are decoded, others are "
155 "output in hex.\n"
156 );
157 }
158
159
160 static struct mode_s {
161 const char *mode_string;
162 int mode;
163 const char *comment;
164 } modes[] = {
165 { "hd", MODE_HEADER_DATA, "combined header and data"},
166 { "vendor", MODE_VENDOR, "vendor specific"},
167 { "data", MODE_DATA, "data"},
168 { "desc", MODE_DESCRIPTOR, "descriptor"},
169 { "echo", MODE_ECHO_BUFFER, "read data from echo buffer "
170 "(spc-2)"},
171 { "echo_desc", MODE_ECHO_BDESC, "echo buffer descriptor (spc-2)"},
172 { "rd_microc_st", MODE_READ_MICROCODE_ST, "read microcode status "
173 "(spc-5)"},
174 { "en_ex", MODE_EN_EX_ECHO,
175 "enable expander communications protocol and echo buffer (spc-3)"},
176 { "err_hist|eh", MODE_ERR_HISTORY, "error history (spc-4)"},
177 { NULL, 999, NULL}, /* end sentinel */
178 };
179
180
181 static void
print_modes(void)182 print_modes(void)
183 {
184 const struct mode_s *mp;
185
186 pr2serr("The modes parameter argument can be numeric (hex or decimal)\n"
187 "or symbolic:\n");
188 for (mp = modes; mp->mode_string; ++mp) {
189 pr2serr(" %2d (0x%02x) %-16s%s\n", mp->mode, mp->mode,
190 mp->mode_string, mp->comment);
191 }
192 }
193
194 /* Invokes a SCSI READ BUFFER(10) command (spc5r02). Return of 0 -> success,
195 * various SG_LIB_CAT_* positive values or -1 -> other errors */
196 static int
sg_ll_read_buffer_10(void * resp,int * residp,bool noisy,const struct opts_t * op)197 sg_ll_read_buffer_10(void * resp, int * residp, bool noisy,
198 const struct opts_t * op)
199 {
200 int ret, res, sense_cat;
201 uint8_t rb10_cb[SG_READ_BUFFER_10_CMDLEN] =
202 {SG_READ_BUFFER_10_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0};
203 uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
204 struct sg_pt_base * ptvp;
205
206 rb10_cb[1] = (uint8_t)(op->rb_mode & 0x1f);
207 if (op->rb_mode_sp)
208 rb10_cb[1] |= (uint8_t)((op->rb_mode_sp & 0x7) << 5);
209 rb10_cb[2] = (uint8_t)op->rb_id;
210 sg_put_unaligned_be24(op->rb_offset, rb10_cb + 3);
211 sg_put_unaligned_be24(op->rb_len, rb10_cb + 6);
212 if (op->verbose) {
213 char b[128];
214
215 pr2serr(" Read buffer(10) cdb: %s\n",
216 sg_get_command_str(rb10_cb, SG_READ_BUFFER_10_CMDLEN, false,
217 sizeof(b), b));
218 }
219
220 ptvp = construct_scsi_pt_obj();
221 if (NULL == ptvp) {
222 pr2serr("Read buffer(10): out of memory\n");
223 return -1;
224 }
225 set_scsi_pt_cdb(ptvp, rb10_cb, sizeof(rb10_cb));
226 set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
227 set_scsi_pt_data_in(ptvp, (uint8_t *)resp, op->rb_len);
228 res = do_scsi_pt(ptvp, op->sg_fd, DEF_PT_TIMEOUT, op->verbose);
229 ret = sg_cmds_process_resp(ptvp, "Read buffer(10)", res, noisy,
230 op->verbose, &sense_cat);
231 if (-1 == ret) {
232 if (get_scsi_pt_transport_err(ptvp))
233 ret = SG_LIB_TRANSPORT_ERROR;
234 else
235 ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
236 } else if (-2 == ret) {
237 switch (sense_cat) {
238 case SG_LIB_CAT_RECOVERED:
239 case SG_LIB_CAT_NO_SENSE:
240 ret = 0;
241 break;
242 default:
243 ret = sense_cat;
244 break;
245 }
246 } else {
247 if ((op->verbose > 2) && (ret > 0)) {
248 pr2serr(" Read buffer(10): response%s\n",
249 (ret > 256 ? ", first 256 bytes" : ""));
250 hex2stderr((const uint8_t *)resp, (ret > 256 ? 256 : ret), -1);
251 }
252 ret = 0;
253 }
254 if (residp)
255 *residp = get_scsi_pt_resid(ptvp);
256 destruct_scsi_pt_obj(ptvp);
257 return ret;
258 }
259
260 /* Invokes a SCSI READ BUFFER(16) command (spc5r02). Return of 0 -> success,
261 * various SG_LIB_CAT_* positive values or -1 -> other errors */
262 static int
sg_ll_read_buffer_16(void * resp,int * residp,bool noisy,const struct opts_t * op)263 sg_ll_read_buffer_16(void * resp, int * residp, bool noisy,
264 const struct opts_t * op)
265 {
266 int ret, res, sense_cat;
267 uint8_t rb16_cb[SG_READ_BUFFER_16_CMDLEN] =
268 {SG_READ_BUFFER_16_CMD, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
269 0, 0, 0, 0};
270 uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
271 struct sg_pt_base * ptvp;
272
273 rb16_cb[1] = (uint8_t)(op->rb_mode & 0x1f);
274 if (op->rb_mode_sp)
275 rb16_cb[1] |= (uint8_t)((op->rb_mode_sp & 0x7) << 5);
276 sg_put_unaligned_be64(op->rb_offset, rb16_cb + 2);
277 sg_put_unaligned_be32(op->rb_len, rb16_cb + 10);
278 rb16_cb[14] = (uint8_t)op->rb_id;
279 if (op->verbose) {
280 char b[128];
281
282 pr2serr(" Read buffer(16) cdb: %s\n",
283 sg_get_command_str(rb16_cb, SG_READ_BUFFER_16_CMDLEN, false,
284 sizeof(b), b));
285 }
286
287 ptvp = construct_scsi_pt_obj();
288 if (NULL == ptvp) {
289 pr2serr("%s: out of memory\n", __func__);
290 return -1;
291 }
292 set_scsi_pt_cdb(ptvp, rb16_cb, sizeof(rb16_cb));
293 set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
294 set_scsi_pt_data_in(ptvp, (uint8_t *)resp, op->rb_len);
295 res = do_scsi_pt(ptvp, op->sg_fd, DEF_PT_TIMEOUT, op->verbose);
296 ret = sg_cmds_process_resp(ptvp, "Read buffer(16)", res, noisy,
297 op->verbose, &sense_cat);
298 if (-1 == ret) {
299 if (get_scsi_pt_transport_err(ptvp))
300 ret = SG_LIB_TRANSPORT_ERROR;
301 else
302 ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
303 } else if (-2 == ret) {
304 switch (sense_cat) {
305 case SG_LIB_CAT_RECOVERED:
306 case SG_LIB_CAT_NO_SENSE:
307 ret = 0;
308 break;
309 default:
310 ret = sense_cat;
311 break;
312 }
313 } else {
314 if ((op->verbose > 2) && (ret > 0)) {
315 pr2serr(" Read buffer(16): response%s\n",
316 (ret > 256 ? ", first 256 bytes" : ""));
317 hex2stderr((const uint8_t *)resp, (ret > 256 ? 256 : ret), -1);
318 }
319 ret = 0;
320 }
321 if (residp)
322 *residp = get_scsi_pt_resid(ptvp);
323 destruct_scsi_pt_obj(ptvp);
324 return ret;
325 }
326
327 /* Microcode status: active, redundant and download */
328 static const char * act_micro_st_arr[] = {
329 "Microcode status not reported",
330 "Activated microcode is valid",
331 "Activated microcode is not valid",
332 "Activated microcode is not a full microcode image",
333 };
334
335 static const char * red_micro_st_arr[] = {
336 "Redundant microcode status is not reported",
337 "At least one redundant microcode copy is valid",
338 "No redundant microcode copy is valid",
339 "Redundant microcode is not a full microcode image",
340 };
341
342 /* Major overlap between this SPC-4 table and SES-4r2 table 63 */
343 struct sg_lib_simple_value_name_t down_micro_st_arr[] = {
344 {0x0, "No download microcode operation in progress"},
345 {0x1, "Download in progress, awaiting more"}, /* SES */
346 {0x2, "Download complete, updating storage"}, /* SES */
347 {0x3, "Updating storage with deferred microcode"}, /* SES */
348 {0x10, "Complete, no error, starting now"}, /* SES */
349 {0x11, "Complete, no error, start after hard reset or power "
350 "cycle"}, /* SES */
351 {0x12, "Complete, no error, start after power cycle"}, /* SES */
352 {0x13, "Complete, no error, start after activate_mc, hard reset or "
353 "power cycle"}, /* SES */
354 {0x21, "Download in progress, awaiting more"}, /* SPC-6 */
355 {0x22, "Download complete, updating storage"}, /* SPC-6 */
356 {0x23, "Updating storage with deferred microcode"}, /* SPC-6 */
357 {0x30, "Deferred microcode download complete, no reports"}, /* SPC-6 */
358 {0x31, "Deferred download ok, await hard reset or power cycle"},
359 {0x32, "Deferred download ok, await power cycle"}, /* SPC-6 */
360 {0x33, "Deferred download ok, await any event"}, /* SPC-6 */
361 {0x34, "Deferred download ok, await Write buffer command"}, /* SPC-6 */
362 {0x35, "Deferred download ok, await any event, WB only this LU"},
363 {0x80, "Error, discarded, see additional status"}, /* SES */
364 {0x81, "Error, discarded, image error"}, /* SES */
365 {0x82, "Timeout, discarded"}, /* SES */
366 {0x83, "Internal error, need new microcode before reset"}, /* SES */
367 {0x84, "Internal error, need new microcode, reset safe"}, /* SES */
368 {0x85, "Unexpected activate_mc received"}, /* SES */
369 {0x90, "Error, discarded, see additional status"}, /* SPC-6 */
370 {0x91, "Error, discarded, image error"}, /* SPC-6 */
371 {0x92, "Timeout, discarded"}, /* SPC-6 */
372 {0x93, "Internal error, need new microcode before reset"}, /* SPC-6 */
373 {0x94, "Internal error, need new microcode, reset safe"}, /* SPC-6 */
374 {0x95, "Unexpected activate_mc received, mcrocode discard"}, /* SPC-6 */
375 {0x1000, NULL}, /* End sentinel */
376 };
377
378 static void
decode_microcode_status(const uint8_t * resp,const struct opts_t * op)379 decode_microcode_status(const uint8_t * resp, const struct opts_t * op)
380 {
381 int n;
382 uint32_t u;
383 const char * cp;
384 const struct sg_lib_simple_value_name_t * vnp;
385 char b[32];
386
387 if ((NULL == resp) || (op->rb_len < 1))
388 return;
389 n = resp[0];
390 if (n < (int)SG_ARRAY_SIZE(act_micro_st_arr))
391 cp = act_micro_st_arr[n];
392 else {
393 snprintf(b, sizeof(b), "unknown [0x%x]", n);
394 cp = b;
395 }
396 printf("Activated microcode status: %s\n", cp);
397
398 if (op->rb_len < 2)
399 return;
400 n = resp[1];
401 if (n < (int)SG_ARRAY_SIZE(red_micro_st_arr))
402 cp = red_micro_st_arr[n];
403 else {
404 snprintf(b, sizeof(b), "unknown [0x%x]", n);
405 cp = b;
406 }
407 printf("Redundant microcode status: %s\n", cp);
408
409 if (op->rb_len < 3)
410 return;
411 n = resp[2];
412 for (vnp = down_micro_st_arr, cp = NULL; vnp->name; ++vnp) {
413 if (vnp->value == n) {
414 cp = vnp->name;
415 break;
416 }
417 }
418 if (NULL == cp) {
419 snprintf(b, sizeof(b), "unknown [0x%x]", n);
420 cp = b;
421 }
422 printf("Download microcode status: %s\n", cp);
423
424 if (op->rb_len > 7) {
425 u = sg_get_unaligned_be32(resp + 4);
426 printf("Download microcode maximum size (bytes): %u [0x%x]\n", u, u);
427 }
428 if (op->rb_len > 15) {
429 u = sg_get_unaligned_be32(resp + 12);
430 printf("Download microcode expected buffer offset (bytes): %u "
431 "[0x%x]\n", u, u);
432 }
433 }
434
435 static void
decode_error_history(const uint8_t * resp,const struct opts_t * op)436 decode_error_history(const uint8_t * resp, const struct opts_t * op)
437 {
438 static const char * eh_s = "Error history";
439 int k, num;
440 uint32_t dir_len;
441 const uint8_t * up;
442
443 if (op->rb_id < 0x4) { /* eh directory variants */
444 if (op->rb_len < 8) {
445 pr2serr("%s response buffer too short [%d] to show directory "
446 "header\n", eh_s, op->rb_len);
447 return;
448 }
449 printf("%s directory header:\n", eh_s);
450 printf(" T10 Vendor: %.8s\n", resp + 0);
451 printf(" Version: %u\n", resp[8]);
452 printf(" EHS_retrieved: %u\n", 0x3 & (resp[9] >> 3));
453 printf(" EHS_source: %u\n", 0x3 & (resp[9] >> 1));
454 printf(" CLR_SUP: %u\n", 0x1 & resp[9]);
455 if (op->rb_len < 32) {
456 pr2serr("%s response buffer too short [%d] to show directory "
457 "length\n", eh_s, op->rb_len);
458 return;
459 }
460 dir_len = sg_get_unaligned_be16(resp + 30);
461 printf(" Directory length: %u\n", dir_len);
462 if ((unsigned)op->rb_len < (32 + dir_len)) {
463 pr2serr("%s directory entries truncated, try adding '-l %u' "
464 "option\n", eh_s, 32 + dir_len);
465 }
466 num = (op->rb_len - 32) / 8;
467 for (k = 0, up = resp + 32; k < num; ++k, up += 8) {
468 if (k > 0)
469 printf("\n");
470 printf(" Supported buffer ID: 0x%x\n", up[0]);
471 printf(" Buffer format: 0x%x\n", up[1]);
472 printf(" Buffer source: 0x%x\n", 0xf & up[2]);
473 printf(" Maximum available length: 0x%x\n",
474 sg_get_unaligned_be32(up + 4));
475 }
476 } else if ((op->rb_id >= 0x10) && (op->rb_id <= 0xef))
477 hex2stdout(resp, op->rb_len, (op->verbose > 1 ? 0 : 1));
478 else if (0xfe == op->rb_id)
479 pr2serr("clear %s I_T nexus [0x%x]\n", eh_s, op->rb_id);
480 else if (0xff == op->rb_id)
481 pr2serr("clear %s I_T nexus and release any snapshots [0x%x]\n",
482 eh_s, op->rb_id);
483 else
484 pr2serr("Reserved Buffer ID value [0x%x] for %s\n", op->rb_id, eh_s);
485
486 }
487
488 static void
dStrRaw(const uint8_t * str,int len)489 dStrRaw(const uint8_t * str, int len)
490 {
491 int k;
492
493 for (k = 0; k < len; ++k)
494 printf("%c", str[k]);
495 }
496
497 int
main(int argc,char * argv[])498 main(int argc, char * argv[])
499 {
500 int res, c, len, k;
501 int inhex_len = 0;
502 int resid = 0;
503 int ret = 0;
504 int64_t ll;
505 const char * cp = NULL;
506 uint8_t * resp = NULL;
507 uint8_t * free_resp = NULL;
508 const struct mode_s * mp;
509 struct opts_t opts SG_C_CPP_ZERO_INIT;
510 struct opts_t * op = &opts;
511
512 op->sg_fd = -1;
513 op->rb_len = DEF_RESPONSE_LEN;
514
515 while (1) {
516 int option_index = 0;
517
518 c = getopt_long(argc, argv, "e:hHi:I:l:Lm:No:rRS:vV", long_options,
519 &option_index);
520 if (c == -1)
521 break;
522
523 switch (c) {
524 case 'e':
525 if (op->rb_mode_given && (MODE_ERR_HISTORY != op->rb_mode)) {
526 pr2serr("mode incompatible with --eh_code= option\n");
527 return SG_LIB_CONTRADICT;
528 }
529 op->eh_code = sg_get_num(optarg);
530 if ((op->eh_code < 0) || (op->eh_code > 255)) {
531 pr2serr("argument to '--eh_code=' should be in the range 0 "
532 "to 255\n");
533 return SG_LIB_SYNTAX_ERROR;
534 }
535 op->rb_mode = MODE_ERR_HISTORY;
536 op->eh_code_given = true;
537 break;
538 case 'h':
539 case '?':
540 ++op->do_help;
541 break;
542 case 'H':
543 ++op->do_hex;
544 break;
545 case 'i':
546 op->rb_id = sg_get_num(optarg);
547 if ((op->rb_id < 0) || (op->rb_id > 255)) {
548 pr2serr("argument to '--id=' should be in the range 0 to "
549 "255\n");
550 return SG_LIB_SYNTAX_ERROR;
551 }
552 op->rb_id_given = true;
553 break;
554 case 'I':
555 if (op->inhex_name) {
556 pr2serr("--inhex= option given more than once. Once only "
557 "please\n");
558 return SG_LIB_SYNTAX_ERROR;
559 } else
560 op->inhex_name = optarg;
561 break;
562 case 'l':
563 op->rb_len = sg_get_num(optarg);
564 if (op->rb_len < 0) {
565 pr2serr("bad argument to '--length'\n");
566 return SG_LIB_SYNTAX_ERROR;
567 }
568 if (op->rb_len > 0xffffff) {
569 pr2serr("argument to '--length' must be <= 0xffffff\n");
570 return SG_LIB_SYNTAX_ERROR;
571 }
572 op->rb_len_given = true;
573 break;
574 case 'L':
575 op->do_long = true;
576 break;
577 case 'm':
578 if (NULL == optarg) {
579 pr2serr("bad argument to '--mode'\n");
580 return SG_LIB_SYNTAX_ERROR;
581 } else if (isdigit((uint8_t)*optarg)) {
582 op->rb_mode = sg_get_num(optarg);
583 if ((op->rb_mode < 0) || (op->rb_mode > 31)) {
584 pr2serr("argument to '--mode' should be in the range 0 "
585 "to 31\n");
586 return SG_LIB_SYNTAX_ERROR;
587 }
588 } else {
589 len = strlen(optarg);
590 for (mp = modes; mp->mode_string; ++mp) {
591 cp = strchr(mp->mode_string, '|');
592 if (NULL == cp) {
593 if (0 == strncmp(mp->mode_string, optarg, len)) {
594 op->rb_mode = mp->mode;
595 break;
596 }
597 } else {
598 int f_len = cp - mp->mode_string;
599
600 if ((f_len == len) &&
601 (0 == memcmp(mp->mode_string, optarg, len))) {
602 op->rb_mode = mp->mode;
603 break;
604 }
605 if (0 == strncmp(cp + 1, optarg, len)) {
606 op->rb_mode = mp->mode;
607 break;
608 }
609 }
610 }
611 if (NULL == mp->mode_string) {
612 print_modes();
613 return SG_LIB_SYNTAX_ERROR;
614 }
615 }
616 if (op->eh_code_given && (MODE_ERR_HISTORY != op->rb_mode)) {
617 pr2serr("mode incompatible with --eh_code= option\n");
618 return SG_LIB_CONTRADICT;
619 }
620 op->rb_mode_given = true;
621 break;
622 case 'N':
623 op->no_output = true;
624 break;
625 case 'o':
626 ll = sg_get_llnum(optarg);
627 if (ll < 0) {
628 pr2serr("bad argument to '--offset'\n");
629 return SG_LIB_SYNTAX_ERROR;
630 }
631 op->rb_offset = ll;
632 break;
633 case 'r':
634 op->do_raw = true;
635 break;
636 case 'R':
637 op->o_readonly = true;
638 break;
639 case 'S':
640 op->rb_mode_sp = sg_get_num(optarg);
641 if ((op->rb_mode_sp < 0) || (op->rb_mode_sp > 7)) {
642 pr2serr("expected argument to '--specific' to be 0 to 7\n");
643 return SG_LIB_SYNTAX_ERROR;
644 }
645 break;
646 case 'v':
647 op->verbose_given = true;
648 ++op->verbose;
649 break;
650 case 'V':
651 op->version_given = true;
652 break;
653 default:
654 pr2serr("unrecognised option code 0x%x ??\n", c);
655 usage();
656 return SG_LIB_SYNTAX_ERROR;
657 }
658 }
659 if (op->do_help) {
660 if (op->do_help > 1) {
661 usage();
662 pr2serr("\n");
663 print_modes();
664 } else
665 usage();
666 return 0;
667 }
668 if (optind < argc) {
669 if (NULL == op->device_name) {
670 op->device_name = argv[optind];
671 ++optind;
672 }
673 if (optind < argc) {
674 for (; optind < argc; ++optind)
675 pr2serr("Unexpected extra argument: %s\n", argv[optind]);
676 usage();
677 return SG_LIB_SYNTAX_ERROR;
678 }
679 }
680
681 #ifdef DEBUG
682 pr2serr("In DEBUG mode, ");
683 if (op->verbose_given && op->version_given) {
684 pr2serr("but override: '-vV' given, zero verbose and continue\n");
685 op->verbose_given = false;
686 op->version_given = false;
687 op->verbose = 0;
688 } else if (! op->verbose_given) {
689 pr2serr("set '-vv'\n");
690 op->verbose = 2;
691 } else
692 pr2serr("keep verbose=%d\n", op->verbose);
693 #else
694 if (op->verbose_given && op->version_given)
695 pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
696 #endif
697 if (op->version_given) {
698 pr2serr("version: %s\n", version_str);
699 return 0;
700 }
701 if ((MODE_ERR_HISTORY == op->rb_mode) && (NULL == op->inhex_name)) {
702 if (! op->rb_len_given)
703 op->rb_len = 64;
704 }
705 if (op->eh_code_given) {
706 if (op->rb_id_given && (op->eh_code != op->rb_id)) {
707 pr2serr("Buffer ID incompatible with --eh_code= option\n");
708 return SG_LIB_CONTRADICT;
709 }
710 op->rb_id = op->eh_code;
711 }
712
713 if (op->device_name && op->inhex_name) {
714 pr2serr("Confused: both DEVICE (%s) and --inhex= option given. One "
715 "only please\n", op->device_name);
716 return SG_LIB_SYNTAX_ERROR;
717 } else if (op->inhex_name) {
718 op->rb_len = (op->rb_len > MAX_DEF_INHEX_LEN) ? op->rb_len :
719 MAX_DEF_INHEX_LEN;
720 resp = (uint8_t *)sg_memalign(op->rb_len, 0, &free_resp, false);
721 ret = sg_f2hex_arr(op->inhex_name, op->do_raw, false, resp,
722 &inhex_len, op->rb_len);
723 if (ret)
724 goto fini;
725 if (op->do_raw)
726 op->do_raw = false; /* only used for input in this case */
727 op->rb_len = inhex_len;
728 resid = 0;
729 goto decode_result;
730 } else if (NULL == op->device_name) {
731 pr2serr("Missing device name!\n\n");
732 usage();
733 return SG_LIB_SYNTAX_ERROR;
734 }
735
736 len = op->rb_len ? op->rb_len : 8;
737 resp = (uint8_t *)sg_memalign(len, 0, &free_resp, false);
738 if (NULL == resp) {
739 pr2serr("unable to allocate %d bytes on the heap\n", len);
740 return SG_LIB_CAT_OTHER;
741 }
742
743 if (op->do_raw) {
744 if (sg_set_binary_mode(STDOUT_FILENO) < 0) {
745 perror("sg_set_binary_mode");
746 ret = SG_LIB_FILE_ERROR;
747 goto fini;
748 }
749 }
750
751 #ifdef SG_LIB_WIN32
752 #ifdef SG_LIB_WIN32_DIRECT
753 if (op->verbose > 4)
754 pr2serr("Initial win32 SPT interface state: %s\n",
755 scsi_pt_win32_spt_state() ? "direct" : "indirect");
756 scsi_pt_win32_direct(SG_LIB_WIN32_DIRECT /* SPT pt interface */);
757 #endif
758 #endif
759
760 op->sg_fd = sg_cmds_open_device(op->device_name, op->o_readonly,
761 op->verbose);
762 if (op->sg_fd < 0) {
763 if (op->verbose)
764 pr2serr("open error: %s: %s\n", op->device_name,
765 safe_strerror(-op->sg_fd));
766 ret = sg_convert_errno(-op->sg_fd);
767 goto fini;
768 }
769
770 if (op->do_long)
771 res = sg_ll_read_buffer_16(resp, &resid, true, op);
772 else if (op->rb_offset > 0xffffff) {
773 pr2serr("--offset value is too large for READ BUFFER(10), try "
774 "--16\n");
775 ret = SG_LIB_SYNTAX_ERROR;
776 goto fini;
777 } else
778 res = sg_ll_read_buffer_10(resp, &resid, true, op);
779 if (0 != res) {
780 char b[80];
781
782 ret = res;
783 if (res > 0) {
784 sg_get_category_sense_str(res, sizeof(b), b, op->verbose);
785 pr2serr("Read buffer(%d) failed: %s\n",
786 (op->do_long ? 16 : 10), b);
787 }
788 goto fini;
789 }
790 if (resid > 0)
791 op->rb_len -= resid; /* got back less than requested */
792 if (op->no_output)
793 goto fini;
794 decode_result:
795 if (op->rb_len > 0) {
796 if (op->do_raw)
797 dStrRaw(resp, op->rb_len);
798 else if (op->do_hex || (op->rb_len < 4)) {
799 k = (op->do_hex > 2) ? -1 : (2 - op->do_hex);
800 hex2stdout(resp, op->rb_len, k);
801 } else {
802 switch (op->rb_mode) {
803 case MODE_DESCRIPTOR:
804 k = sg_get_unaligned_be24(resp + 1);
805 printf("OFFSET BOUNDARY: %d, Buffer offset alignment: "
806 "%d-byte\n", resp[0], (1 << resp[0]));
807 printf("BUFFER CAPACITY: %d (0x%x)\n", k, k);
808 break;
809 case MODE_ECHO_BDESC:
810 k = sg_get_unaligned_be16(resp + 2) & 0x1fff;
811 printf("EBOS:%d\n", resp[0] & 1 ? 1 : 0);
812 printf("Echo buffer capacity: %d (0x%x)\n", k, k);
813 break;
814 case MODE_READ_MICROCODE_ST:
815 decode_microcode_status(resp, op);
816 break;
817 case MODE_ERR_HISTORY:
818 decode_error_history(resp, op);
819 break;
820 default:
821 hex2stdout(resp, op->rb_len, (op->verbose > 1 ? 0 : 1));
822 break;
823 }
824 }
825 }
826
827 fini:
828 if (free_resp)
829 free(free_resp);
830 if (op->sg_fd >= 0) {
831 res = sg_cmds_close_device(op->sg_fd);
832 if (res < 0) {
833 pr2serr("close error: %s\n", safe_strerror(-res));
834 if (0 == ret)
835 ret = sg_convert_errno(-res);
836 }
837 }
838 if (0 == op->verbose) {
839 if (! sg_if_can2stderr("sg_read_buffer failed: ", ret))
840 pr2serr("Some error occurred, try again with '-v' "
841 "or '-vv' for more information\n");
842 }
843 return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
844 }
845