1 /*
2 * Copyright (c) 2006-2022 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 <string.h>
17 #include <getopt.h>
18 #define __STDC_FORMAT_MACROS 1
19 #include <inttypes.h>
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "sg_lib.h"
26 #include "sg_cmds_basic.h"
27 #include "sg_cmds_extra.h"
28 #include "sg_pr2serr.h"
29 #include "sg_unaligned.h"
30
31 /* This program uses a ATA PASS-THROUGH SCSI command to package an
32 * ATA IDENTIFY (PACKAGE) DEVICE command. It is based on the SCSI to
33 * ATA Translation (SAT) drafts and standards. See https://www.t10.org
34 * for drafts. SAT is a standard: SAT ANSI INCITS 431-2007 (draft prior
35 * to that is sat-r09.pdf). SAT-2 is also a standard: SAT-2 ANSI INCITS
36 * 465-2010 and the draft prior to that is sat2r09.pdf . The SAT-3 is
37 * now a standard: SAT-3 ANSI INCITS 517-2015. The most current draft of
38 * SAT-4 is revision 5c (sat4r05c.pdf).
39 */
40
41 #define SAT_ATA_PASS_THROUGH32_LEN 32
42 #define SAT_ATA_PASS_THROUGH16 0x85
43 #define SAT_ATA_PASS_THROUGH16_LEN 16
44 #define SAT_ATA_PASS_THROUGH12 0xa1 /* clashes with MMC BLANK command */
45 #define SAT_ATA_PASS_THROUGH12_LEN 12
46 #define SAT_ATA_RETURN_DESC 9 /* ATA Return (sense) Descriptor */
47 #define ASCQ_ATA_PT_INFO_AVAILABLE 0x1d
48
49 #define ATA_IDENTIFY_DEVICE 0xec
50 #define ATA_IDENTIFY_PACKET_DEVICE 0xa1
51 #define ID_RESPONSE_LEN 512
52
53 #define DEF_TIMEOUT 20
54
55 #define EBUFF_SZ 256
56
57 static const char * version_str = "1.19 20220425";
58
59 static struct option long_options[] = {
60 {"ck-cond", no_argument, 0, 'c'},
61 {"ck_cond", no_argument, 0, 'c'},
62 {"extend", no_argument, 0, 'e'},
63 {"help", no_argument, 0, 'h'},
64 {"hex", no_argument, 0, 'H'},
65 {"len", required_argument, 0, 'l'},
66 {"ident", no_argument, 0, 'i'},
67 {"packet", no_argument, 0, 'p'},
68 {"raw", no_argument, 0, 'r'},
69 {"readonly", no_argument, 0, 'R'},
70 {"verbose", no_argument, 0, 'v'},
71 {"version", no_argument, 0, 'V'},
72 {0, 0, 0, 0},
73 };
74
75
76 static void
usage()77 usage()
78 {
79 pr2serr("Usage: sg_sat_identify [--ck_cond] [--extend] [--help] [--hex] "
80 "[--ident]\n"
81 " [--len=CLEN] [--packet] [--raw] "
82 "[--readonly]\n"
83 " [--verbose] [--version] DEVICE\n"
84 " where:\n"
85 " --ck_cond|-c sets ck_cond bit in cdb (def: 0)\n"
86 " --extend|-e sets extend bit in cdb (def: 0)\n"
87 " --help|-h print out usage message then exit\n"
88 " --hex|-H output response in hex\n"
89 " --ident|-i output WWN prefixed by 0x, if not "
90 "available output\n"
91 " 0x0000000000000000\n"
92 " --len=CLEN| -l CLEN CLEN is cdb length: 12, 16 or 32 "
93 "bytes\n"
94 " (default: 16)\n"
95 " --packet|-p do IDENTIFY PACKET DEVICE (def: IDENTIFY "
96 "DEVICE)\n"
97 " command\n"
98 " --raw|-r output response in binary to stdout\n"
99 " --readonly|-R open DEVICE read-only (def: read-write)\n"
100 " --verbose|-v increase verbosity\n"
101 " --version|-V print version string and exit\n\n"
102 "Performs a ATA IDENTIFY (PACKET) DEVICE command via a SAT "
103 "layer using\na SCSI ATA PASS-THROUGH(12), (16) or (32) command. "
104 "Only SAT layers\ncompliant with SAT-4 revision 5 or later will "
105 "support the SCSI ATA\nPASS-THROUGH(32) command.\n");
106 }
107
108 static void
dStrRaw(const uint8_t * str,int len)109 dStrRaw(const uint8_t * str, int len)
110 {
111 int k;
112
113 for (k = 0; k < len; ++k)
114 printf("%c", str[k]);
115 }
116
117 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)118 do_identify_dev(int sg_fd, bool do_packet, int cdb_len, bool ck_cond,
119 bool extend, bool do_ident, int do_hex, bool do_raw,
120 int verbose)
121 {
122 const bool t_type = false;/* false -> 512 byte blocks,
123 true -> device's LB size */
124 bool t_dir = true; /* false -> to device, true -> from device */
125 bool byte_block = true; /* false -> bytes, true -> 512 byte blocks (if
126 t_type=false) */
127 bool got_ard = false; /* got ATA result descriptor */
128 bool got_fixsense = false; /* got ATA result in fixed format sense */
129 bool ok;
130 int j, res, ret, sb_sz;
131 /* Following for ATA READ/WRITE MULTIPLE (EXT) cmds, normally 0 */
132 int multiple_count = 0;
133 int protocol = 4; /* PIO data-in */
134 int t_length = 2; /* 0 -> no data transferred, 2 -> sector count */
135 int resid = 0;
136 uint64_t ull;
137 struct sg_scsi_sense_hdr ssh;
138 uint8_t inBuff[ID_RESPONSE_LEN];
139 uint8_t sense_buffer[64] SG_C_CPP_ZERO_INIT;
140 uint8_t ata_return_desc[16] SG_C_CPP_ZERO_INIT;
141 uint8_t apt_cdb[SAT_ATA_PASS_THROUGH16_LEN] =
142 {SAT_ATA_PASS_THROUGH16, 0, 0, 0, 0, 0, 0, 0,
143 0, 0, 0, 0, 0, 0, 0, 0};
144 uint8_t apt12_cdb[SAT_ATA_PASS_THROUGH12_LEN] =
145 {SAT_ATA_PASS_THROUGH12, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0};
147 uint8_t apt32_cdb[SAT_ATA_PASS_THROUGH32_LEN] SG_C_CPP_ZERO_INIT;
148 const unsigned short * usp;
149
150 sb_sz = sizeof(sense_buffer);
151 ok = false;
152 switch (cdb_len) {
153 case SAT_ATA_PASS_THROUGH32_LEN: /* SAT-4 revision 5 or later */
154 /* Prepare SCSI ATA PASS-THROUGH COMMAND(32) command */
155 sg_put_unaligned_be16(1, apt32_cdb + 22); /* count=1 */
156 apt32_cdb[25] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE :
157 ATA_IDENTIFY_DEVICE);
158 apt32_cdb[10] = (multiple_count << 5) | (protocol << 1);
159 if (extend)
160 apt32_cdb[10] |= 0x1;
161 apt32_cdb[11] = t_length;
162 if (ck_cond)
163 apt32_cdb[11] |= 0x20;
164 if (t_type)
165 apt32_cdb[11] |= 0x10;
166 if (t_dir)
167 apt32_cdb[11] |= 0x8;
168 if (byte_block)
169 apt32_cdb[11] |= 0x4;
170 /* following call takes care of all bytes below offset 10 in cdb */
171 res = sg_ll_ata_pt(sg_fd, apt32_cdb, cdb_len, DEF_TIMEOUT, inBuff,
172 NULL /* doutp */, ID_RESPONSE_LEN, sense_buffer,
173 sb_sz, ata_return_desc,
174 sizeof(ata_return_desc), &resid, verbose);
175 break;
176 case SAT_ATA_PASS_THROUGH16_LEN:
177 /* Prepare SCSI ATA PASS-THROUGH COMMAND(16) command */
178 apt_cdb[6] = 1; /* sector count */
179 apt_cdb[14] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE :
180 ATA_IDENTIFY_DEVICE);
181 apt_cdb[1] = (multiple_count << 5) | (protocol << 1);
182 if (extend)
183 apt_cdb[1] |= 0x1;
184 apt_cdb[2] = t_length;
185 if (ck_cond)
186 apt_cdb[2] |= 0x20;
187 if (t_type)
188 apt_cdb[2] |= 0x10;
189 if (t_dir)
190 apt_cdb[2] |= 0x8;
191 if (byte_block)
192 apt_cdb[2] |= 0x4;
193 res = sg_ll_ata_pt(sg_fd, apt_cdb, cdb_len, DEF_TIMEOUT, inBuff,
194 NULL /* doutp */, ID_RESPONSE_LEN, sense_buffer,
195 sb_sz, ata_return_desc,
196 sizeof(ata_return_desc), &resid, verbose);
197 break;
198 case SAT_ATA_PASS_THROUGH12_LEN:
199 /* Prepare SCSI ATA PASS-THROUGH COMMAND(12) command */
200 apt12_cdb[4] = 1; /* sector count */
201 apt12_cdb[9] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE :
202 ATA_IDENTIFY_DEVICE);
203 apt12_cdb[1] = (multiple_count << 5) | (protocol << 1);
204 apt12_cdb[2] = t_length;
205 if (ck_cond)
206 apt12_cdb[2] |= 0x20;
207 if (t_type)
208 apt12_cdb[2] |= 0x10;
209 if (t_dir)
210 apt12_cdb[2] |= 0x8;
211 if (byte_block)
212 apt12_cdb[2] |= 0x4;
213 res = sg_ll_ata_pt(sg_fd, apt12_cdb, cdb_len, DEF_TIMEOUT, inBuff,
214 NULL /* doutp */, ID_RESPONSE_LEN, sense_buffer,
215 sb_sz, ata_return_desc,
216 sizeof(ata_return_desc), &resid, verbose);
217 break;
218 default:
219 pr2serr("%s: bad cdb_len=%d\n", __func__, cdb_len);
220 return -1;
221 }
222 if (0 == res) {
223 ok = true;
224 if (verbose > 2)
225 pr2serr("command completed with SCSI GOOD status\n");
226 } else if ((res > 0) && (res & SAM_STAT_CHECK_CONDITION)) {
227 if (verbose > 1) {
228 pr2serr("ATA pass-through:\n");
229 sg_print_sense(NULL, sense_buffer, sb_sz,
230 ((verbose > 2) ? 1 : 0));
231 }
232 if (sg_scsi_normalize_sense(sense_buffer, sb_sz, &ssh)) {
233 switch (ssh.sense_key) {
234 case SPC_SK_ILLEGAL_REQUEST:
235 if ((0x20 == ssh.asc) && (0x0 == ssh.ascq)) {
236 ret = SG_LIB_CAT_INVALID_OP;
237 if (verbose < 2)
238 pr2serr("ATA PASS-THROUGH (%d) not supported\n",
239 cdb_len);
240 } else {
241 ret = SG_LIB_CAT_ILLEGAL_REQ;
242 if (verbose < 2)
243 pr2serr("ATA PASS-THROUGH (%d), bad field in cdb\n",
244 cdb_len);
245 }
246 return ret;
247 case SPC_SK_NO_SENSE:
248 case SPC_SK_RECOVERED_ERROR:
249 if ((0x0 == ssh.asc) &&
250 (ASCQ_ATA_PT_INFO_AVAILABLE == ssh.ascq)) {
251 if (0x72 == ssh.response_code) {
252 if (SAT_ATA_RETURN_DESC != ata_return_desc[0]) {
253 if (verbose)
254 pr2serr("did not find ATA Return (sense) "
255 "Descriptor\n");
256 return SG_LIB_CAT_RECOVERED;
257 }
258 got_ard = true;
259 break;
260 } else if (0x70 == ssh.response_code) {
261 got_fixsense = true;
262 break;
263 } else {
264 if (verbose < 2)
265 pr2serr("ATA PASS-THROUGH (%d), unexpected "
266 "response_code=0x%x\n", ssh.response_code,
267 cdb_len);
268 return SG_LIB_CAT_RECOVERED;
269 }
270 } else if (SPC_SK_RECOVERED_ERROR == ssh.sense_key)
271 return SG_LIB_CAT_RECOVERED;
272 else {
273 if ((0x0 == ssh.asc) && (0x0 == ssh.ascq))
274 break;
275 return SG_LIB_CAT_SENSE;
276 }
277 case SPC_SK_UNIT_ATTENTION:
278 if (verbose < 2)
279 pr2serr("ATA PASS-THROUGH (%d), Unit Attention detected\n",
280 cdb_len);
281 return SG_LIB_CAT_UNIT_ATTENTION;
282 case SPC_SK_NOT_READY:
283 if (verbose < 2)
284 pr2serr("ATA PASS-THROUGH (%d), device not ready\n",
285 cdb_len);
286 return SG_LIB_CAT_NOT_READY;
287 case SPC_SK_MEDIUM_ERROR:
288 case SPC_SK_HARDWARE_ERROR:
289 if (verbose < 2)
290 pr2serr("ATA PASS-THROUGH (%d), medium or hardware "
291 "error\n", cdb_len);
292 return SG_LIB_CAT_MEDIUM_HARD;
293 case SPC_SK_ABORTED_COMMAND:
294 if (0x10 == ssh.asc) {
295 pr2serr("Aborted command: protection information\n");
296 return SG_LIB_CAT_PROTECTION;
297 } else {
298 pr2serr("Aborted command: try again with%s '-p' option\n",
299 (do_packet ? "out" : ""));
300 return SG_LIB_CAT_ABORTED_COMMAND;
301 }
302 case SPC_SK_DATA_PROTECT:
303 pr2serr("ATA PASS-THROUGH (%d): data protect, read only "
304 "media?\n", cdb_len);
305 return SG_LIB_CAT_DATA_PROTECT;
306 default:
307 if (verbose < 2)
308 pr2serr("ATA PASS-THROUGH (%d), some sense data, use "
309 "'-v' for more information\n", cdb_len);
310 return SG_LIB_CAT_SENSE;
311 }
312 } else {
313 pr2serr("CHECK CONDITION without response code ??\n");
314 return SG_LIB_CAT_SENSE;
315 }
316 if (0x72 != (sense_buffer[0] & 0x7f)) {
317 pr2serr("expected descriptor sense format, response code=0x%x\n",
318 sense_buffer[0]);
319 return SG_LIB_CAT_MALFORMED;
320 }
321 } else if (res > 0) {
322 if (SAM_STAT_RESERVATION_CONFLICT == res) {
323 pr2serr("SCSI status: RESERVATION CONFLICT\n");
324 return SG_LIB_CAT_RES_CONFLICT;
325 } else {
326 pr2serr("Unexpected SCSI status=0x%x\n", res);
327 return SG_LIB_CAT_MALFORMED;
328 }
329 } else {
330 pr2serr("ATA pass-through (%d) failed\n", cdb_len);
331 if (verbose < 2)
332 pr2serr(" try adding '-v' for more information\n");
333 return -1;
334 }
335
336 if ((SAT_ATA_RETURN_DESC == ata_return_desc[0]) && (! got_ard))
337 pr2serr("Seem to have got ATA Result Descriptor but it was not "
338 "indicated\n");
339 if (got_ard) {
340 if (ata_return_desc[3] & 0x4) {
341 pr2serr("error indication in returned FIS: aborted command\n");
342 pr2serr(" try again with%s '-p' option\n",
343 (do_packet ? "out" : ""));
344 return SG_LIB_CAT_ABORTED_COMMAND;
345 }
346 ok = true;
347 }
348 if (got_fixsense) {
349 if (0x4 & sense_buffer[3]) { /* Error is MSB of Info field */
350 pr2serr("error indication in returned FIS: aborted command\n");
351 pr2serr(" try again with%s '-p' option\n",
352 (do_packet ? "out" : ""));
353 return SG_LIB_CAT_ABORTED_COMMAND;
354 }
355 ok = true;
356 }
357
358 if (ok) { /* output result if it is available */
359 if (do_raw)
360 dStrRaw(inBuff, 512);
361 else if (0 == do_hex) {
362 if (do_ident) {
363 usp = (const unsigned short *)inBuff;
364 ull = 0;
365 for (j = 0; j < 4; ++j) {
366 if (j > 0)
367 ull <<= 16;
368 ull |= usp[108 + j];
369 }
370 printf("0x%016" PRIx64 "\n", ull);
371 } else {
372 printf("Response for IDENTIFY %sDEVICE ATA command:\n",
373 (do_packet ? "PACKET " : ""));
374 dWordHex((const unsigned short *)inBuff, 256, 0,
375 sg_is_big_endian());
376 }
377 } else if (1 == do_hex)
378 hex2stdout(inBuff, 512, 0);
379 else if (2 == do_hex)
380 dWordHex((const unsigned short *)inBuff, 256, 0,
381 sg_is_big_endian());
382 else if (3 == do_hex) /* '-HHH' suitable for "hdparm --Istdin" */
383 dWordHex((const unsigned short *)inBuff, 256, -2,
384 sg_is_big_endian());
385 else /* '-HHHH' hex bytes only */
386 hex2stdout(inBuff, 512, -1);
387 }
388 return 0;
389 }
390
391 int
main(int argc,char * argv[])392 main(int argc, char * argv[])
393 {
394 bool do_packet = false;
395 bool do_ident = false;
396 bool do_raw = false;
397 bool o_readonly = false;
398 bool ck_cond = false; /* set to true to read register(s) back */
399 bool extend = false; /* set to true to send 48 bit LBA with command */
400 bool verbose_given = false;
401 bool version_given = false;
402 int c, res;
403 int sg_fd = -1;
404 int cdb_len = SAT_ATA_PASS_THROUGH16_LEN;
405 int do_hex = 0;
406 int verbose = 0;
407 int ret = 0;
408 const char * device_name = NULL;
409
410 while (1) {
411 int option_index = 0;
412
413 c = getopt_long(argc, argv, "cehHil:prRvV", long_options,
414 &option_index);
415 if (c == -1)
416 break;
417
418 switch (c) {
419 case 'c':
420 ck_cond = true;
421 break;
422 case 'e':
423 extend = true;
424 break;
425 case 'h':
426 case '?':
427 usage();
428 return 0;
429 case 'H':
430 ++do_hex;
431 break;
432 case 'i':
433 do_ident = true;
434 break;
435 case 'l':
436 cdb_len = sg_get_num(optarg);
437 switch (cdb_len) {
438 case 12:
439 case 16:
440 case 32:
441 break;
442 default:
443 pr2serr("argument to '--len' should be 12, 16 or 32\n");
444 return SG_LIB_SYNTAX_ERROR;
445 }
446 break;
447 case 'p':
448 do_packet = true;
449 break;
450 case 'r':
451 do_raw = true;
452 break;
453 case 'R':
454 o_readonly = true;
455 break;
456 case 'v':
457 verbose_given = true;
458 ++verbose;
459 break;
460 case 'V':
461 version_given = true;
462 break;
463 default:
464 pr2serr("unrecognised option code 0x%x ??\n", c);
465 usage();
466 return SG_LIB_SYNTAX_ERROR;
467 }
468 }
469 if (optind < argc) {
470 if (NULL == device_name) {
471 device_name = argv[optind];
472 ++optind;
473 }
474 if (optind < argc) {
475 for (; optind < argc; ++optind)
476 pr2serr("Unexpected extra argument: %s\n", argv[optind]);
477 usage();
478 return SG_LIB_SYNTAX_ERROR;
479 }
480 }
481 #ifdef DEBUG
482 pr2serr("In DEBUG mode, ");
483 if (verbose_given && version_given) {
484 pr2serr("but override: '-vV' given, zero verbose and continue\n");
485 verbose_given = false;
486 version_given = false;
487 verbose = 0;
488 } else if (! verbose_given) {
489 pr2serr("set '-vv'\n");
490 verbose = 2;
491 } else
492 pr2serr("keep verbose=%d\n", verbose);
493 #else
494 if (verbose_given && version_given)
495 pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
496 #endif
497 if (version_given) {
498 pr2serr("version: %s\n", version_str);
499 return 0;
500 }
501
502 if (NULL == device_name) {
503 pr2serr("Missing device name!\n\n");
504 usage();
505 return 1;
506 }
507 if (do_raw) {
508 if (sg_set_binary_mode(STDOUT_FILENO) < 0) {
509 perror("sg_set_binary_mode");
510 return SG_LIB_FILE_ERROR;
511 }
512 }
513
514 if ((sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose)) < 0) {
515 if (verbose)
516 pr2serr("error opening file: %s: %s\n", device_name,
517 safe_strerror(-sg_fd));
518 ret = sg_convert_errno(-sg_fd);
519 goto fini;
520 }
521
522 ret = do_identify_dev(sg_fd, do_packet, cdb_len, ck_cond, extend,
523 do_ident, do_hex, do_raw, verbose);
524
525 fini:
526 if (sg_fd >= 0) {
527 res = sg_cmds_close_device(sg_fd);
528 if (res < 0) {
529 pr2serr("close error: %s\n", safe_strerror(-res));
530 if (0 == ret)
531 ret = sg_convert_errno(-res);
532 }
533 }
534 if (0 == verbose) {
535 if (! sg_if_can2stderr("sg_sat_identify failed: ", ret))
536 pr2serr("Some error occurred, try again with '-v' "
537 "or '-vv' for more information\n");
538 }
539 return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
540 }
541