xref: /aosp_15_r20/external/sg3_utils/src/sg_rep_pip.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1 /*
2  * Copyright (c) 2014-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 <errno.h>
18 #include <ctype.h>
19 #include <getopt.h>
20 #define __STDC_FORMAT_MACROS 1
21 #include <inttypes.h>
22 
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include "sg_lib.h"
28 #include "sg_lib_data.h"
29 #include "sg_pt.h"
30 #include "sg_cmds_basic.h"
31 #include "sg_unaligned.h"
32 #include "sg_pr2serr.h"
33 
34 /* A utility program originally written for the Linux OS SCSI subsystem.
35  *
36  *
37  * This program issues the SCSI REPORT PROVISIONING INITIALIZATION PATTERN
38  * command to the given SCSI device and outputs the response. Based on
39  * sbc4r21.pdf
40  */
41 
42 static const char * version_str = "1.04 20220120";
43 
44 #define MAX_RPIP_BUFF_LEN (1024 * 1024)
45 #define DEF_RPIP_BUFF_LEN 512
46 
47 #define SG_MAINT_IN_CMDLEN 12
48 
49 #define REPORT_PROVISIONING_INITIALIZATION_PATTERN_SA 0x1d
50 
51 #define SENSE_BUFF_LEN 64       /* Arbitrary, could be larger */
52 #define DEF_PT_TIMEOUT  60      /* 60 seconds */
53 
54 static const char * rpip_s = "Report provisioning initialization pattern";
55 
56 
57 static struct option long_options[] = {
58         {"help", no_argument, 0, 'h'},
59         {"hex", no_argument, 0, 'H'},
60         {"maxlen", required_argument, 0, 'm'},
61         {"raw", no_argument, 0, 'r'},
62         {"readonly", no_argument, 0, 'R'},
63         {"verbose", no_argument, 0, 'v'},
64         {"version", no_argument, 0, 'V'},
65         {0, 0, 0, 0},
66 };
67 
68 
69 static void
usage(void)70 usage(void)
71 {
72     pr2serr("Usage: "
73             "sg_rep_pip  [--help] [--hex] [--maxlen=LEN] [--raw] "
74             "[--readonly]\n"
75             "                   [--verbose] [--version] DEVICE\n");
76     pr2serr("  where:\n"
77             "    --help|-h          prints out this usage message\n"
78             "    --hex|-H           output response in hexadecimal "
79             "(default); used\n"
80             "                       twice: hex without addresses at start "
81             "of line\n"
82             "    --maxlen=LEN|-m LEN    max response length (allocation "
83             "length in cdb)\n"
84             "                           (def: 512 bytes)\n"
85             "    --raw|-r           output response in binary\n"
86             "    --readonly|-R      open DEVICE read-only (def: read-write)\n"
87             "    --verbose|-v       increase verbosity\n"
88             "    --version|-V       print version string and exit\n\n"
89             "Sends a SCSI REPORT PROVISIONING INITIALIZATION PATTERN "
90             "command and outputs\nthe response in ASCII hexadecimal or "
91             "binary.\n");
92 }
93 
94 /* Invokes a SCSI REPORT PROVISIONING INITIALIZATION PATTERN command (SBC).
95  * Return of 0 -> success, various SG_LIB_CAT_* positive values or
96  * -1 -> other errors */
97 static int
sg_ll_report_pip(int sg_fd,void * resp,int mx_resp_len,int * residp,bool noisy,int verbose)98 sg_ll_report_pip(int sg_fd, void * resp, int mx_resp_len, int * residp,
99                  bool noisy, int verbose)
100 {
101     int ret, res, sense_cat;
102     uint8_t rpip_cdb[SG_MAINT_IN_CMDLEN] =
103           {SG_MAINTENANCE_IN, REPORT_PROVISIONING_INITIALIZATION_PATTERN_SA,
104            0, 0,  0, 0, 0, 0,  0, 0, 0, 0};
105     uint8_t sense_b[SENSE_BUFF_LEN] SG_C_CPP_ZERO_INIT;
106     struct sg_pt_base * ptvp;
107 
108     sg_put_unaligned_be32((uint32_t)mx_resp_len, rpip_cdb + 6);
109     if (verbose) {
110         char b[128];
111 
112         pr2serr("    %s cdb: %s\n", rpip_s,
113                 sg_get_command_str(rpip_cdb, SG_MAINT_IN_CMDLEN, false,
114                                    sizeof(b), b));
115     }
116     ptvp = construct_scsi_pt_obj();
117     if (NULL == ptvp) {
118         pr2serr("%s: out of memory\n", __func__);
119         return -1;
120     }
121     set_scsi_pt_cdb(ptvp, rpip_cdb, sizeof(rpip_cdb));
122     set_scsi_pt_sense(ptvp, sense_b, sizeof(sense_b));
123     set_scsi_pt_data_in(ptvp, (uint8_t *)resp, mx_resp_len);
124     res = do_scsi_pt(ptvp, sg_fd, DEF_PT_TIMEOUT, verbose);
125     ret = sg_cmds_process_resp(ptvp, rpip_s, res, noisy, verbose, &sense_cat);
126     if (-1 == ret) {
127         if (get_scsi_pt_transport_err(ptvp))
128             ret = SG_LIB_TRANSPORT_ERROR;
129         else
130             ret = sg_convert_errno(get_scsi_pt_os_err(ptvp));
131     } else if (-2 == ret) {
132         switch (sense_cat) {
133         case SG_LIB_CAT_RECOVERED:
134         case SG_LIB_CAT_NO_SENSE:
135             ret = 0;
136             break;
137         default:
138             ret = sense_cat;
139             break;
140         }
141     } else
142         ret = 0;
143     if (residp)
144         *residp = get_scsi_pt_resid(ptvp);
145     destruct_scsi_pt_obj(ptvp);
146     return ret;
147 }
148 
149 static void
dStrRaw(const uint8_t * str,int len)150 dStrRaw(const uint8_t * str, int len)
151 {
152     int k;
153 
154     for (k = 0; k < len; ++k)
155         printf("%c", str[k]);
156 }
157 
158 
159 int
main(int argc,char * argv[])160 main(int argc, char * argv[])
161 {
162     bool do_raw = false;
163     bool o_readonly = false;
164     bool verbose_given = false;
165     bool version_given = false;
166     int res, c, resid, rlen;
167     int sg_fd = -1;
168     int do_help = 0;
169     int do_hex = 0;
170     int maxlen = 0;
171     int ret = 0;
172     int verbose = 0;
173     const char * device_name = NULL;
174     uint8_t * rpipBuff = NULL;
175     uint8_t * free_rpip = NULL;
176     char b[80];
177 
178     while (1) {
179         int option_index = 0;
180 
181         c = getopt_long(argc, argv, "hHm:rRvV", long_options,
182                         &option_index);
183         if (c == -1)
184             break;
185 
186         switch (c) {
187         case 'h':
188         case '?':
189             ++do_help;
190             break;
191         case 'H':
192             ++do_hex;
193             break;
194         case 'm':
195             maxlen = sg_get_num(optarg);
196             if ((maxlen < 0) || (maxlen > MAX_RPIP_BUFF_LEN)) {
197                 pr2serr("argument to '--maxlen' should be %d or less\n",
198                         MAX_RPIP_BUFF_LEN);
199                 return SG_LIB_SYNTAX_ERROR;
200             }
201             break;
202         case 'r':
203             do_raw = true;
204             break;
205         case 'R':
206             o_readonly = true;
207             break;
208         case 'v':
209             verbose_given = true;
210             ++verbose;
211             break;
212         case 'V':
213             version_given = true;
214             break;
215         default:
216             pr2serr("unrecognised option code 0x%x ??\n", c);
217             usage();
218             return SG_LIB_SYNTAX_ERROR;
219         }
220     }
221     if (optind < argc) {
222         if (NULL == device_name) {
223             device_name = argv[optind];
224             ++optind;
225         }
226         if (optind < argc) {
227             for (; optind < argc; ++optind)
228                 pr2serr("Unexpected extra argument: %s\n", argv[optind]);
229             usage();
230             return SG_LIB_SYNTAX_ERROR;
231         }
232     }
233 #ifdef DEBUG
234     pr2serr("In DEBUG mode, ");
235     if (verbose_given && version_given) {
236         pr2serr("but override: '-vV' given, zero verbose and continue\n");
237         verbose_given = false;
238         version_given = false;
239         verbose = 0;
240     } else if (! verbose_given) {
241         pr2serr("set '-vv'\n");
242         verbose = 2;
243     } else
244         pr2serr("keep verbose=%d\n", verbose);
245 #else
246     if (verbose_given && version_given)
247         pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
248 #endif
249     if (version_given) {
250         pr2serr("version: %s\n", version_str);
251         return 0;
252     }
253 
254     if (do_help) {
255         usage();
256         return 0;
257     }
258     if (NULL == device_name) {
259         pr2serr("missing device name!\n\n");
260         usage();
261         return SG_LIB_SYNTAX_ERROR;
262     }
263 
264     if (do_raw) {
265         if (sg_set_binary_mode(STDOUT_FILENO) < 0) {
266             perror("sg_set_binary_mode");
267             return SG_LIB_FILE_ERROR;
268         }
269     }
270 
271     sg_fd = sg_cmds_open_device(device_name, o_readonly, verbose);
272     if (sg_fd < 0) {
273         if (verbose)
274             pr2serr("open error: %s: %s\n", device_name,
275                     safe_strerror(-sg_fd));
276         ret = sg_convert_errno(-sg_fd);
277         goto the_end;
278     }
279 
280     if (0 == maxlen)
281         maxlen = DEF_RPIP_BUFF_LEN;
282     rpipBuff = (uint8_t *)sg_memalign(maxlen, 0, &free_rpip, verbose > 3);
283     if (NULL == rpipBuff) {
284         pr2serr("unable to sg_memalign %d bytes\n", maxlen);
285         return sg_convert_errno(ENOMEM);
286     }
287     res = sg_ll_report_pip(sg_fd, rpipBuff, maxlen, &resid, true, verbose);
288     ret = res;
289     if (0 == res) {
290         rlen = maxlen - resid;
291         if (rlen < 4) {
292             pr2serr("Response length (%d) too short\n", rlen);
293             ret = SG_LIB_CAT_MALFORMED;
294             goto the_end;
295         }
296         if (do_raw) {
297             dStrRaw(rpipBuff, rlen);
298             goto the_end;
299         }
300         if (do_hex) {
301             if (2 != do_hex)
302                 hex2stdout(rpipBuff, rlen, ((1 == do_hex) ? 1 : -1));
303             else
304                 hex2stdout(rpipBuff, rlen, 0);
305             goto the_end;
306         } else {
307             printf("Printing response in hex:\n");
308             hex2stdout(rpipBuff, rlen, 1);
309             goto the_end;
310         }
311     } else if (SG_LIB_CAT_INVALID_OP == res)
312         pr2serr("%s command not supported\n", rpip_s);
313     else {
314         sg_get_category_sense_str(res, sizeof(b), b, verbose);
315         pr2serr("%s command: %s\n", rpip_s, b);
316     }
317 
318 the_end:
319     if (free_rpip)
320         free(free_rpip);
321     if (sg_fd >= 0) {
322         res = sg_cmds_close_device(sg_fd);
323         if (res < 0) {
324             pr2serr("close error: %s\n", safe_strerror(-res));
325             if (0 == ret)
326                 ret = sg_convert_errno(-res);
327         }
328     }
329     if (0 == verbose) {
330         if (! sg_if_can2stderr("sg_rep_pip failed: ", ret))
331             pr2serr("Some error occurred, try again with '-v' "
332                     "or '-vv' for more information\n");
333     }
334     return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
335 }
336