xref: /aosp_15_r20/external/sg3_utils/examples/sg__sat_identify.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1 /*
2  * Copyright (c) 2006-2018 Douglas Gilbert.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  */
29 
30 #include <unistd.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <sys/ioctl.h>
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include "sg_lib.h"
41 #include "sg_io_linux.h"
42 
43 /* This program uses a ATA PASS-THROUGH (16) SCSI command to package
44    an ATA IDENTIFY DEVICE (A1h) command. If the '-p' option is given,
45    it will package an ATA IDENTIFY PACKET DEVICE (Ech) instead (for
46    ATAPI device like cd/dvd drives) See http://www.t10.org
47    SAT draft at time of writing: sat-r08a.pdf
48 
49    Invocation: sg__sat_identify [-p] [-v] [-V] <device>
50 
51    With SAT, the user can find out whether a device is an ATA disk or
52    an ATAPI device. The ATA Information VPD page contains a "command
53    code" field in byte 56. Its values are either ECh for a (s/p)ATA
54    disk, A1h for a (s/p)ATAPI device, or 0 for unknown.
55 
56 */
57 
58 #define SAT_ATA_PASS_THROUGH16 0x85
59 #define SAT_ATA_PASS_THROUGH16_LEN 16
60 #define SAT_ATA_RETURN_DESC 9  /* ATA Return (sense) Descriptor */
61 
62 #define ATA_IDENTIFY_DEVICE 0xec
63 #define ATA_IDENTIFY_PACKET_DEVICE 0xa1
64 #define ID_RESPONSE_LEN 512
65 
66 #define EBUFF_SZ 256
67 
68 static char * version_str = "1.04 20180220";
69 
usage()70 static void usage()
71 {
72     fprintf(stderr, "Usage: "
73           "sg__sat_identify [-p] [-v] [-V] <device>\n"
74           "  where: -p    do IDENTIFY PACKET DEVICE (def: IDENTIFY "
75           "DEVICE) command\n"
76           "         -v    increase verbosity\n"
77           "         -V    print version string and exit\n\n"
78           "Performs a IDENTIFY (PACKET) DEVICE ATA command via a SAT "
79           "pass through\n");
80 }
81 
main(int argc,char * argv[])82 int main(int argc, char * argv[])
83 {
84     int sg_fd, k, ok;
85     uint8_t apt_cdb[SAT_ATA_PASS_THROUGH16_LEN] =
86                 {SAT_ATA_PASS_THROUGH16, 0, 0, 0, 0, 0, 0, 0,
87                  0, 0, 0, 0, 0, 0, 0, 0};
88     sg_io_hdr_t io_hdr;
89     char * file_name = 0;
90     char ebuff[EBUFF_SZ];
91     uint8_t inBuff[ID_RESPONSE_LEN];
92     uint8_t sense_buffer[32];
93     int do_packet = 0;
94     int verbose = 0;
95     int extend = 0;
96     int chk_cond = 0;   /* set to 1 to read register(s) back */
97     int protocol = 4;   /* PIO data-in */
98     int t_dir = 1;      /* 0 -> to device, 1 -> from device */
99     int byte_block = 1; /* 0 -> bytes, 1 -> 512 byte blocks */
100     int t_length = 2;   /* 0 -> no data transferred, 2 -> sector count */
101     const uint8_t * cucp;
102 
103     memset(inBuff, 0, sizeof(inBuff));
104     for (k = 1; k < argc; ++k) {
105         if (0 == strcmp(argv[k], "-p"))
106             ++do_packet;
107         else if (0 == strcmp(argv[k], "-v"))
108             ++verbose;
109         else if (0 == strcmp(argv[k], "-vv"))
110             verbose += 2;
111         else if (0 == strcmp(argv[k], "-vvv"))
112             verbose += 3;
113         else if (0 == strcmp(argv[k], "-V")) {
114             fprintf(stderr, "version: %s\n", version_str);
115             exit(0);
116         } else if (*argv[k] == '-') {
117             printf("Unrecognized switch: %s\n", argv[k]);
118             file_name = 0;
119             break;
120         }
121         else if (0 == file_name)
122             file_name = argv[k];
123         else {
124             printf("too many arguments\n");
125             file_name = 0;
126             break;
127         }
128     }
129     if (0 == file_name) {
130         usage();
131         return 1;
132     }
133 
134     if ((sg_fd = open(file_name, O_RDWR)) < 0) {
135         snprintf(ebuff, EBUFF_SZ,
136                  "sg__sat_identify: error opening file: %s", file_name);
137         perror(ebuff);
138         return 1;
139     }
140 
141     /* Prepare ATA PASS-THROUGH COMMAND (16) command */
142     apt_cdb[6] = 1;   /* sector count */
143     apt_cdb[14] = (do_packet ? ATA_IDENTIFY_PACKET_DEVICE :
144                                  ATA_IDENTIFY_DEVICE);
145     apt_cdb[1] = (protocol << 1) | extend;
146     apt_cdb[2] = (chk_cond << 5) | (t_dir << 3) |
147                  (byte_block << 2) | t_length;
148     if (verbose) {
149         fprintf(stderr, "    ata pass through(16) cdb: ");
150         for (k = 0; k < SAT_ATA_PASS_THROUGH16_LEN; ++k)
151             fprintf(stderr, "%02x ", apt_cdb[k]);
152         fprintf(stderr, "\n");
153     }
154 
155     memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
156     io_hdr.interface_id = 'S';
157     io_hdr.cmd_len = sizeof(apt_cdb);
158     /* io_hdr.iovec_count = 0; */  /* memset takes care of this */
159     io_hdr.mx_sb_len = sizeof(sense_buffer);
160     io_hdr.dxfer_direction = SG_DXFER_FROM_DEV;
161     io_hdr.dxfer_len = ID_RESPONSE_LEN;
162     io_hdr.dxferp = inBuff;
163     io_hdr.cmdp = apt_cdb;
164     io_hdr.sbp = sense_buffer;
165     io_hdr.timeout = 20000;     /* 20000 millisecs == 20 seconds */
166     /* io_hdr.flags = 0; */     /* take defaults: indirect IO, etc */
167     /* io_hdr.pack_id = 0; */
168     /* io_hdr.usr_ptr = NULL; */
169 
170     if (ioctl(sg_fd, SG_IO, &io_hdr) < 0) {
171         perror("sg__sat_identify: SG_IO ioctl error");
172         close(sg_fd);
173         return 1;
174     }
175 
176     /* now for the error processing */
177     ok = 0;
178     switch (sg_err_category3(&io_hdr)) {
179     case SG_LIB_CAT_CLEAN:
180         ok = 1;
181         break;
182     case SG_LIB_CAT_RECOVERED:
183         if (verbose)
184             sg_chk_n_print3(">>> ATA_16 command", &io_hdr, 1);
185         /* check for ATA Return Descriptor */
186         cucp = sg_scsi_sense_desc_find(io_hdr.sbp, io_hdr.sb_len_wr,
187                                        SAT_ATA_RETURN_DESC);
188         if (cucp && (cucp[3])) {
189             if (cucp[3] & 0x4) {
190                 printf("error in returned FIS: aborted command\n");
191                 printf("    try again with%s '-p' option\n",
192                        (do_packet ? "out" : ""));
193                 break;
194             }
195         }
196         ok = 1;         /* not sure what is happening so output response */
197         if (0 == verbose) {
198             printf(">>> Recovered error on ATA_16, may have failed\n");
199             printf("    Add '-v' for more information\n");
200         }
201         break;
202     default: /* won't bother decoding other categories */
203         sg_chk_n_print3("ATA_16 command error", &io_hdr, 1);
204         break;
205     }
206 
207     if (ok) { /* output result if it is available */
208         printf("Response for IDENTIFY %sDEVICE ATA command:\n",
209                (do_packet ? "PACKET " : ""));
210         dWordHex((const unsigned short *)inBuff, 256, 0,
211                  sg_is_big_endian());
212     }
213 
214     close(sg_fd);
215     return 0;
216 }
217