1 /*
2 * Copyright (c) 2014-2022 Hannes Reinecke, SUSE Linux GmbH.
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 <errno.h>
18 #include <getopt.h>
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "sg_lib.h"
25 #include "sg_cmds_basic.h"
26 #include "sg_cmds_extra.h"
27 #include "sg_unaligned.h"
28 #include "sg_pr2serr.h"
29
30 /* This program uses a ATA PASS-THROUGH SCSI command. This usage is
31 * defined in the SCSI to ATA Translation (SAT) drafts and standards.
32 * See https://www.t10.org for drafts. SAT is a standard: SAT ANSI INCITS
33 * 431-2007 (draft prior to that is sat-r09.pdf). SAT-2 is also a
34 * standard: SAT-2 ANSI INCITS 465-2010 and the draft prior to that is
35 * sat2r09.pdf . The SAT-3 project has started and the most recent draft
36 * is sat3r01.pdf .
37 */
38
39 /* This program performs a ATA PASS-THROUGH (16) SCSI command in order
40 * to perform an ATA READ LOG EXT or ATA READ LOG DMA EXT command.
41 *
42 * See man page (sg_sat_read_gplog.8) for details.
43 */
44
45 #define SAT_ATA_PASS_THROUGH16 0x85
46 #define SAT_ATA_PASS_THROUGH16_LEN 16
47 #define SAT_ATA_PASS_THROUGH12 0xa1 /* clashes with MMC BLANK command */
48 #define SAT_ATA_PASS_THROUGH12_LEN 12
49 #define SAT_ATA_RETURN_DESC 9 /* ATA Return (sense) Descriptor */
50 #define ASCQ_ATA_PT_INFO_AVAILABLE 0x1d
51
52 #define ATA_READ_LOG_EXT 0x2f
53 #define ATA_READ_LOG_DMA_EXT 0x47
54
55 #define DEF_TIMEOUT 20
56
57 static const char * version_str = "1.23 20220917";
58
59 struct opts_t {
60 bool ck_cond;
61 bool rdonly;
62 int cdb_len;
63 int count;
64 int hex;
65 int la; /* log address */
66 int pn; /* page number within log address */
67 int verbose;
68 const char * device_name;
69 };
70
71 static struct option long_options[] = {
72 {"count", required_argument, 0, 'c'},
73 {"ck_cond", no_argument, 0, 'C'},
74 {"ck-cond", no_argument, 0, 'C'},
75 {"dma", no_argument, 0, 'd'},
76 {"help", no_argument, 0, 'h'},
77 {"hex", no_argument, 0, 'H'},
78 {"len", required_argument, 0, 'l'},
79 {"log", required_argument, 0, 'L'},
80 {"page", required_argument, 0, 'p'},
81 {"readonly", no_argument, 0, 'r'},
82 {"verbose", no_argument, 0, 'v'},
83 {"version", no_argument, 0, 'V'},
84 {0, 0, 0, 0},
85 };
86
87
88 static void
usage()89 usage()
90 {
91 pr2serr("Usage: "
92 "sg_sat_read_gplog [--ck_cond] [--count=CO] [--dma] [--help]\n"
93 " [--hex] [--len=16|12] [--log=LA] "
94 "[--page=PN]\n"
95 " [--readonly] [--verbose] [--version] "
96 "DEVICE\n"
97 " where:\n"
98 " --ck_cond | -C set ck_cond field in pass-through "
99 "(def: 0)\n"
100 " --count=CO | -c CO block count (def: 1)\n"
101 " --dma | -d Use READ LOG DMA EXT (def: READ LOG "
102 "EXT)\n"
103 " --help | -h output this usage message\n"
104 " --hex | -H output response in hex bytes, -HH "
105 "yields hex\n"
106 " words + ASCII (def), -HHH hex words "
107 "only\n"
108 " --len=16|12 | -l 16|12 cdb length: 16 or 12 bytes "
109 "(def: 16)\n"
110 " --log=LA | -L LA Log address to be read (def: 0)\n"
111 " --page=PN|-p PN Log page number within address (def: "
112 "0)\n"
113 " --readonly | -r open DEVICE read-only (def: "
114 "read-write)\n"
115 " --verbose | -v increase verbosity\n"
116 " recommended if DEVICE is ATA disk\n"
117 " --version | -V print version string and exit\n\n"
118 "Sends an ATA READ LOG EXT (or READ LOG DMA EXT) command via a "
119 "SAT pass\nthrough to fetch a General Purpose (GP) log page. Each "
120 "page is accessed\nvia a log address and then a page number "
121 "within that address: LA,PN .\n"
122 "By default the output is the response in hex (16 bit) words.\n"
123 );
124 }
125
126 static int
do_read_gplog(int sg_fd,int ata_cmd,uint8_t * inbuff,const struct opts_t * op)127 do_read_gplog(int sg_fd, int ata_cmd, uint8_t * inbuff,
128 const struct opts_t * op)
129 {
130 const bool extend = true;
131 const bool t_dir = true; /* false -> to device, true -> from device */
132 const bool byte_block = true;/* false -> bytes, true -> 512 byte blocks */
133 const bool t_type = false; /* false -> 512 byte blocks, true -> logical
134 sectors */
135 bool got_ard = false; /* got ATA result descriptor */
136 int res, ret;
137 int protocol;
138 int t_length = 2; /* 0 -> no data transferred, 2 -> sector count */
139 int resid = 0;
140 int sb_sz;
141 struct sg_scsi_sense_hdr ssh;
142 uint8_t sense_buffer[64] SG_C_CPP_ZERO_INIT;
143 uint8_t ata_return_desc[16] SG_C_CPP_ZERO_INIT;
144 uint8_t apt_cdb[SAT_ATA_PASS_THROUGH16_LEN] =
145 {SAT_ATA_PASS_THROUGH16, 0, 0, 0, 0, 0, 0, 0,
146 0, 0, 0, 0, 0, 0, 0, 0};
147 uint8_t apt12_cdb[SAT_ATA_PASS_THROUGH12_LEN] =
148 {SAT_ATA_PASS_THROUGH12, 0, 0, 0, 0, 0, 0, 0,
149 0, 0, 0, 0};
150 char cmd_name[32];
151
152 snprintf(cmd_name, sizeof(cmd_name), "ATA PASS-THROUGH (%d)",
153 op->cdb_len);
154 if (ata_cmd == ATA_READ_LOG_DMA_EXT) {
155 protocol = 6; /* DMA */
156 } else {
157 protocol = 4; /* PIO Data-In */
158 }
159 sb_sz = sizeof(sense_buffer);
160 memset(inbuff, 0, op->count * 512);
161 if (op->verbose > 1)
162 pr2serr("Building ATA READ LOG%s EXT command; la=0x%x, pn=0x%x\n",
163 ((ata_cmd == ATA_READ_LOG_DMA_EXT) ? " DMA" : ""), op->la,
164 op->pn);
165 if (op->cdb_len == 16) {
166 /* Prepare ATA PASS-THROUGH COMMAND (16) command */
167 apt_cdb[14] = ata_cmd;
168 sg_put_unaligned_be16((uint16_t)op->count, apt_cdb + 5);
169 apt_cdb[8] = op->la;
170 sg_put_unaligned_be16((uint16_t)op->pn, apt_cdb + 9);
171 apt_cdb[1] = (protocol << 1) | extend;
172 if (extend)
173 apt_cdb[1] |= 0x1;
174 apt_cdb[2] = t_length;
175 if (op->ck_cond)
176 apt_cdb[2] |= 0x20;
177 if (t_type)
178 apt_cdb[2] |= 0x10;
179 if (t_dir)
180 apt_cdb[2] |= 0x8;
181 if (byte_block)
182 apt_cdb[2] |= 0x4;
183 res = sg_ll_ata_pt(sg_fd, apt_cdb, op->cdb_len, DEF_TIMEOUT, inbuff,
184 NULL, op->count * 512, sense_buffer,
185 sb_sz, ata_return_desc,
186 sizeof(ata_return_desc), &resid, op->verbose);
187 } else {
188 /* Prepare ATA PASS-THROUGH COMMAND (12) command */
189 /* Cannot map upper 8 bits of the pn since no LBA (39:32) field */
190 apt12_cdb[9] = ata_cmd;
191 apt12_cdb[4] = op->count;
192 apt12_cdb[5] = op->la;
193 apt12_cdb[6] = op->pn & 0xff;
194 /* apt12_cdb[7] = (op->pn >> 8) & 0xff; */
195 apt12_cdb[1] = (protocol << 1);
196 apt12_cdb[2] = t_length;
197 if (op->ck_cond)
198 apt12_cdb[2] |= 0x20;
199 if (t_type)
200 apt12_cdb[2] |= 0x10;
201 if (t_dir)
202 apt12_cdb[2] |= 0x8;
203 if (byte_block)
204 apt12_cdb[2] |= 0x4;
205 res = sg_ll_ata_pt(sg_fd, apt12_cdb, op->cdb_len, DEF_TIMEOUT,
206 inbuff, NULL, op->count * 512, sense_buffer,
207 sb_sz, ata_return_desc,
208 sizeof(ata_return_desc), &resid, op->verbose);
209 }
210 if (0 == res) {
211 if (op->verbose > 2)
212 pr2serr("command completed with SCSI GOOD status\n");
213 if ((0 == op->hex) || (2 == op->hex))
214 dWordHex((const unsigned short *)inbuff, op->count * 256, 0,
215 sg_is_big_endian());
216 else if (1 == op->hex)
217 hex2stdout(inbuff, 512, 0);
218 else if (3 == op->hex) /* '-HHH' suitable for "hdparm --Istdin" */
219 dWordHex((const unsigned short *)inbuff, 256, -2,
220 sg_is_big_endian());
221 else /* '-HHHH' hex bytes only */
222 hex2stdout(inbuff, 512, -1);
223 } else if ((res > 0) && (res & SAM_STAT_CHECK_CONDITION)) {
224 if (op->verbose > 1) {
225 pr2serr("ATA pass through:\n");
226 sg_print_sense(NULL, sense_buffer, sb_sz,
227 ((op->verbose > 2) ? 1 : 0));
228 }
229 if (sg_scsi_normalize_sense(sense_buffer, sb_sz, &ssh)) {
230 switch (ssh.sense_key) {
231 case SPC_SK_ILLEGAL_REQUEST:
232 if ((0x20 == ssh.asc) && (0x0 == ssh.ascq)) {
233 ret = SG_LIB_CAT_INVALID_OP;
234 if (op->verbose < 2)
235 pr2serr("%s not supported\n", cmd_name);
236 } else {
237 ret = SG_LIB_CAT_ILLEGAL_REQ;
238 if (op->verbose < 2)
239 pr2serr("%s, bad field in cdb\n", cmd_name);
240 }
241 return ret;
242 case SPC_SK_NO_SENSE:
243 case SPC_SK_RECOVERED_ERROR:
244 if ((0x0 == ssh.asc) &&
245 (ASCQ_ATA_PT_INFO_AVAILABLE == ssh.ascq)) {
246 if (SAT_ATA_RETURN_DESC != ata_return_desc[0]) {
247 if (op->verbose)
248 pr2serr("did not find ATA Return (sense) "
249 "Descriptor\n");
250 return SG_LIB_CAT_RECOVERED;
251 }
252 got_ard = true;
253 break;
254 } else if (SPC_SK_RECOVERED_ERROR == ssh.sense_key)
255 return SG_LIB_CAT_RECOVERED;
256 else {
257 if ((0x0 == ssh.asc) && (0x0 == ssh.ascq))
258 break;
259 return SG_LIB_CAT_SENSE;
260 }
261 case SPC_SK_UNIT_ATTENTION:
262 if (op->verbose < 2)
263 pr2serr("%s, Unit Attention detected\n", cmd_name);
264 return SG_LIB_CAT_UNIT_ATTENTION;
265 case SPC_SK_NOT_READY:
266 if (op->verbose < 2)
267 pr2serr("%s, device not ready\n", cmd_name);
268 return SG_LIB_CAT_NOT_READY;
269 case SPC_SK_MEDIUM_ERROR:
270 case SPC_SK_HARDWARE_ERROR:
271 if (op->verbose < 2)
272 pr2serr("%s, medium or hardware error\n", cmd_name);
273 return SG_LIB_CAT_MEDIUM_HARD;
274 case SPC_SK_ABORTED_COMMAND:
275 if (0x10 == ssh.asc) {
276 pr2serr("Aborted command: protection information\n");
277 return SG_LIB_CAT_PROTECTION;
278 } else {
279 pr2serr("Aborted command\n");
280 return SG_LIB_CAT_ABORTED_COMMAND;
281 }
282 case SPC_SK_DATA_PROTECT:
283 pr2serr("%s: data protect, read only media?\n", cmd_name);
284 return SG_LIB_CAT_DATA_PROTECT;
285 default:
286 if (op->verbose < 2)
287 pr2serr("%s, some sense data, use '-v' for more "
288 "information\n", cmd_name);
289 return SG_LIB_CAT_SENSE;
290 }
291 } else {
292 pr2serr("CHECK CONDITION without response code ??\n");
293 return SG_LIB_CAT_SENSE;
294 }
295 if (0x72 != (sense_buffer[0] & 0x7f)) {
296 pr2serr("expected descriptor sense format, response "
297 "code=0x%x\n", sense_buffer[0]);
298 return SG_LIB_CAT_MALFORMED;
299 }
300 } else if (res > 0) {
301 if (SAM_STAT_RESERVATION_CONFLICT == res) {
302 pr2serr("SCSI status: RESERVATION CONFLICT\n");
303 return SG_LIB_CAT_RES_CONFLICT;
304 } else {
305 pr2serr("Unexpected SCSI status=0x%x\n", res);
306 return SG_LIB_CAT_MALFORMED;
307 }
308 } else {
309 pr2serr("%s failed\n", cmd_name);
310 if (op->verbose < 2)
311 pr2serr(" try adding '-v' for more information\n");
312 return -1;
313 }
314
315 if ((SAT_ATA_RETURN_DESC == ata_return_desc[0]) && (! got_ard))
316 pr2serr("Seem to have got ATA Result Descriptor but it was not "
317 "indicated\n");
318 if (got_ard) {
319 if (ata_return_desc[3] & 0x4) {
320 pr2serr("error indication in returned FIS: aborted "
321 "command\n");
322 return SG_LIB_CAT_ABORTED_COMMAND;
323 }
324 }
325 return 0;
326 }
327
328
329 int
main(int argc,char * argv[])330 main(int argc, char * argv[])
331 {
332 bool verbose_given = false;
333 bool version_given = false;
334 int c, ret, res, n;
335 int sg_fd = -1;
336 int ata_cmd = ATA_READ_LOG_EXT;
337 uint8_t *inbuff = NULL;
338 uint8_t *free_inbuff = NULL;
339 struct opts_t opts;
340 struct opts_t * op;
341
342 op = &opts;
343 memset(op, 0, sizeof(opts));
344 op->cdb_len = SAT_ATA_PASS_THROUGH16_LEN;
345 op->count = 1;
346 while (1) {
347 int option_index = 0;
348
349 c = getopt_long(argc, argv, "c:CdhHl:L:p:rvV", long_options,
350 &option_index);
351 if (c == -1)
352 break;
353
354 switch (c) {
355 case 'c':
356 op->count = sg_get_num(optarg);
357 if ((op->count < 1) || (op->count > 0xffff)) {
358 pr2serr("bad argument for '--count'\n");
359 return SG_LIB_SYNTAX_ERROR;
360 }
361 break;
362 case 'C':
363 op->ck_cond = true;
364 break;
365 case 'd':
366 ata_cmd = ATA_READ_LOG_DMA_EXT;
367 break;
368 case 'h':
369 case '?':
370 usage();
371 return 0;
372 case 'H':
373 ++op->hex;
374 break;
375 case 'l':
376 op->cdb_len = sg_get_num(optarg);
377 if (! ((op->cdb_len == 12) || (op->cdb_len == 16))) {
378 pr2serr("argument to '--len' should be 12 or 16\n");
379 return SG_LIB_SYNTAX_ERROR;
380 }
381 break;
382 case 'L':
383 op->la = sg_get_num(optarg);
384 if (op->la < 0 || op->la > 0xff) {
385 pr2serr("bad argument for '--log'\n");
386 return SG_LIB_SYNTAX_ERROR;
387 }
388 break;
389 case 'p':
390 op->pn = sg_get_num(optarg);
391 if ((op->pn < 0) || (op->pn > 0xffff)) {
392 pr2serr("bad argument for '--page'\n");
393 return SG_LIB_SYNTAX_ERROR;
394 }
395 break;
396 case 'r':
397 op->rdonly = true;
398 break;
399 case 'v':
400 verbose_given = true;
401 ++op->verbose;
402 break;
403 case 'V':
404 version_given = true;
405 break;
406 default:
407 pr2serr("unrecognised option code 0x%x ??\n", c);
408 usage();
409 return SG_LIB_SYNTAX_ERROR;
410 }
411 }
412 if (optind < argc) {
413 if (NULL == op->device_name) {
414 op->device_name = argv[optind];
415 ++optind;
416 }
417 if (optind < argc) {
418 for (; optind < argc; ++optind)
419 pr2serr("Unexpected extra argument: %s\n",
420 argv[optind]);
421 usage();
422 return SG_LIB_SYNTAX_ERROR;
423 }
424 }
425
426 #ifdef DEBUG
427 pr2serr("In DEBUG mode, ");
428 if (verbose_given && version_given) {
429 pr2serr("but override: '-vV' given, zero verbose and continue\n");
430 verbose_given = false;
431 version_given = false;
432 op->verbose = 0;
433 } else if (! verbose_given) {
434 pr2serr("set '-vv'\n");
435 op->verbose = 2;
436 } else
437 pr2serr("keep verbose=%d\n", op->verbose);
438 #else
439 if (verbose_given && version_given)
440 pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
441 #endif
442 if (version_given) {
443 pr2serr("version: %s\n", version_str);
444 return 0;
445 }
446
447 if (NULL == op->device_name) {
448 pr2serr("Missing device name!\n\n");
449 usage();
450 return 1;
451 }
452
453 if ((op->count > 0xff) && (12 == op->cdb_len)) {
454 op->cdb_len = 16;
455 if (op->verbose)
456 pr2serr("Since count > 0xff, forcing cdb length to "
457 "16\n");
458 }
459
460 n = op->count * 512;
461 inbuff = (uint8_t *)sg_memalign(n, 0, &free_inbuff, op->verbose > 3);
462 if (!inbuff) {
463 pr2serr("Cannot allocate output buffer of size %d\n", n);
464 return SG_LIB_CAT_OTHER;
465 }
466
467 if ((sg_fd = sg_cmds_open_device(op->device_name, op->rdonly,
468 op->verbose)) < 0) {
469 if (op->verbose)
470 pr2serr("error opening file: %s: %s\n", op->device_name,
471 safe_strerror(-sg_fd));
472 ret = sg_convert_errno(-sg_fd);
473 goto fini;
474 }
475
476 ret = do_read_gplog(sg_fd, ata_cmd, inbuff, op);
477
478 fini:
479 if (sg_fd >= 0) {
480 res = sg_cmds_close_device(sg_fd);
481 if (res < 0) {
482 pr2serr("close error: %s\n", safe_strerror(-res));
483 if (0 == ret)
484 ret = sg_convert_errno(-res);
485 }
486 }
487 if (0 == op->verbose) {
488 if (! sg_if_can2stderr("sg_sat_read_gplog failed: ", ret))
489 pr2serr("Some error occurred, try again with '-v' "
490 "or '-vv' for more information\n");
491 }
492 if (free_inbuff)
493 free(free_inbuff);
494 return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
495 }
496