xref: /aosp_15_r20/external/sg3_utils/src/sg_read_long.c (revision 44704f698541f6367e81f991ef8bb54ccbf3fc18)
1 /* A utility program for the Linux OS SCSI subsystem.
2  *  Copyright (C) 2004-2018 D. Gilbert
3  *  This program is free software; you can redistribute it and/or modify
4  *  it under the terms of the GNU General Public License as published by
5  *  the Free Software Foundation; either version 2, or (at your option)
6  *  any later version.
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  *
10  * This program issues the SCSI command READ LONG to a given SCSI device.
11  * It sends the command with the logical block address passed as the lba
12  * argument, and the transfer length set to the xfer_len argument. the
13  * buffer to be written to the device filled with 0xff, this buffer includes
14  * the sector data and the ECC bytes.
15  */
16 
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <stdarg.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #include <errno.h>
25 #include <getopt.h>
26 #include <errno.h>
27 #define __STDC_FORMAT_MACROS 1
28 #include <inttypes.h>
29 
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33 
34 #include "sg_lib.h"
35 #include "sg_cmds_basic.h"
36 #include "sg_cmds_extra.h"
37 #include "sg_pr2serr.h"
38 
39 static const char * version_str = "1.27 20180627";
40 
41 #define MAX_XFER_LEN 10000
42 
43 #define ME "sg_read_long: "
44 
45 #define EBUFF_SZ 512
46 
47 
48 static struct option long_options[] = {
49         {"16", no_argument, 0, 'S'},
50         {"correct", no_argument, 0, 'c'},
51         {"help", no_argument, 0, 'h'},
52         {"lba", required_argument, 0, 'l'},
53         {"out", required_argument, 0, 'o'},
54         {"pblock", no_argument, 0, 'p'},
55         {"readonly", no_argument, 0, 'r'},
56         {"verbose", no_argument, 0, 'v'},
57         {"version", no_argument, 0, 'V'},
58         {"xfer_len", required_argument, 0, 'x'},
59         {"xfer-len", required_argument, 0, 'x'},
60         {0, 0, 0, 0},
61 };
62 
63 static void
usage()64 usage()
65 {
66     pr2serr("Usage: sg_read_long [--16] [--correct] [--help] [--lba=LBA] "
67             "[--out=OF]\n"
68             "                    [--pblock] [--readonly] [--verbose] "
69             "[--version]\n"
70             "                    [--xfer_len=BTL] DEVICE\n"
71             "  where:\n"
72             "    --16|-S              do READ LONG(16) (default: "
73             "READ LONG(10))\n"
74             "    --correct|-c         use ECC to correct data "
75             "(default: don't)\n"
76             "    --help|-h            print out usage message\n"
77             "    --lba=LBA|-l LBA     logical block address"
78             " (default: 0)\n"
79             "    --out=OF|-o OF       output in binary to file named OF\n"
80             "    --pblock|-p          fetch physical block containing LBA\n"
81             "    --readonly|-r        open DEVICE read-only (def: open it "
82             "read-write)\n"
83             "    --verbose|-v         increase verbosity\n"
84             "    --version|-V         print version string and"
85             " exit\n"
86             "    --xfer_len=BTL|-x BTL    byte transfer length (< 10000)"
87             " default 520\n\n"
88             "Perform a SCSI READ LONG (10 or 16) command. Reads a single "
89             "block with\nassociated ECC data. The user data could be "
90             "encoded or encrypted.\n");
91 }
92 
93 /* Returns 0 if successful */
94 static int
process_read_long(int sg_fd,bool do_16,bool pblock,bool correct,uint64_t llba,void * data_out,int xfer_len,int verbose)95 process_read_long(int sg_fd, bool do_16, bool pblock, bool correct,
96                   uint64_t llba, void * data_out, int xfer_len, int verbose)
97 {
98     int offset, res;
99     const char * ten_or;
100     char b[80];
101 
102     if (do_16)
103         res = sg_ll_read_long16(sg_fd, pblock, correct, llba, data_out,
104                                 xfer_len, &offset, true, verbose);
105     else
106         res = sg_ll_read_long10(sg_fd, pblock, correct, (unsigned int)llba,
107                                 data_out, xfer_len, &offset, true, verbose);
108     ten_or = do_16 ? "16" : "10";
109     switch (res) {
110     case 0:
111         break;
112     case SG_LIB_CAT_ILLEGAL_REQ_WITH_INFO:
113         pr2serr("<<< device indicates 'xfer_len' should be %d >>>\n",
114                 xfer_len - offset);
115         break;
116     default:
117         sg_get_category_sense_str(res, sizeof(b), b, verbose);
118         pr2serr("  SCSI READ LONG (%s): %s\n", ten_or, b);
119         break;
120     }
121     return res;
122 }
123 
124 
125 int
main(int argc,char * argv[])126 main(int argc, char * argv[])
127 {
128     bool correct = false;
129     bool do_16 = false;
130     bool pblock = false;
131     bool readonly = false;
132     bool got_stdout;
133     bool verbose_given = false;
134     bool version_given = false;
135     int outfd, res, c;
136     int sg_fd = -1;
137     int ret = 0;
138     int xfer_len = 520;
139     int verbose = 0;
140     uint64_t llba = 0;
141     int64_t ll;
142     uint8_t * readLongBuff = NULL;
143     uint8_t * rawp = NULL;
144     uint8_t * free_rawp = NULL;
145     const char * device_name = NULL;
146     char out_fname[256];
147     char ebuff[EBUFF_SZ];
148 
149     memset(out_fname, 0, sizeof out_fname);
150     while (1) {
151         int option_index = 0;
152 
153         c = getopt_long(argc, argv, "chl:o:prSvVx:", long_options,
154                         &option_index);
155         if (c == -1)
156             break;
157 
158         switch (c) {
159         case 'c':
160             correct = true;
161             break;
162         case 'h':
163         case '?':
164             usage();
165             return 0;
166         case 'l':
167             ll = sg_get_llnum(optarg);
168             if (-1 == ll) {
169                 pr2serr("bad argument to '--lba'\n");
170                 return SG_LIB_SYNTAX_ERROR;
171             }
172             llba = (uint64_t)ll;
173             break;
174         case 'o':
175             strncpy(out_fname, optarg, sizeof(out_fname) - 1);
176             break;
177         case 'p':
178             pblock = true;
179             break;
180         case 'r':
181             readonly = true;
182             break;
183         case 'S':
184             do_16 = true;
185             break;
186         case 'v':
187             verbose_given = true;
188             ++verbose;
189             break;
190         case 'V':
191             version_given = true;
192             break;
193         case 'x':
194             xfer_len = sg_get_num(optarg);
195            if (-1 == xfer_len) {
196                 pr2serr("bad argument to '--xfer_len'\n");
197                 return SG_LIB_SYNTAX_ERROR;
198             }
199             break;
200         default:
201             pr2serr("unrecognised option code 0x%x ??\n", c);
202             usage();
203             return SG_LIB_SYNTAX_ERROR;
204         }
205     }
206     if (optind < argc) {
207         if (NULL == device_name) {
208             device_name = argv[optind];
209             ++optind;
210         }
211         if (optind < argc) {
212             for (; optind < argc; ++optind)
213                 pr2serr("Unexpected extra argument: %s\n", argv[optind]);
214             usage();
215             return SG_LIB_SYNTAX_ERROR;
216         }
217     }
218 
219 #ifdef DEBUG
220     pr2serr("In DEBUG mode, ");
221     if (verbose_given && version_given) {
222         pr2serr("but override: '-vV' given, zero verbose and continue\n");
223         verbose_given = false;
224         version_given = false;
225         verbose = 0;
226     } else if (! verbose_given) {
227         pr2serr("set '-vv'\n");
228         verbose = 2;
229     } else
230         pr2serr("keep verbose=%d\n", verbose);
231 #else
232     if (verbose_given && version_given)
233         pr2serr("Not in DEBUG mode, so '-vV' has no special action\n");
234 #endif
235     if (version_given) {
236         pr2serr(ME "version: %s\n", version_str);
237         return 0;
238     }
239 
240     if (NULL == device_name) {
241         pr2serr("Missing device name!\n\n");
242         usage();
243         return SG_LIB_SYNTAX_ERROR;
244     }
245     if (xfer_len >= MAX_XFER_LEN){
246         pr2serr("xfer_len (%d) is out of range ( < %d)\n", xfer_len,
247                 MAX_XFER_LEN);
248         usage();
249         return SG_LIB_SYNTAX_ERROR;
250     }
251     sg_fd = sg_cmds_open_device(device_name, readonly, verbose);
252     if (sg_fd < 0) {
253         if (verbose)
254             pr2serr(ME "open error: %s: %s\n", device_name,
255                     safe_strerror(-sg_fd));
256         ret = sg_convert_errno(-sg_fd);
257         goto err_out;
258     }
259 
260     if (NULL == (rawp = (uint8_t *)sg_memalign(MAX_XFER_LEN, 0, &free_rawp,
261                                                false))) {
262         if (verbose)
263             pr2serr(ME "out of memory\n");
264         ret = sg_convert_errno(ENOMEM);
265         goto err_out;
266     }
267     readLongBuff = (uint8_t *)rawp;
268     memset(rawp, 0x0, MAX_XFER_LEN);
269 
270     pr2serr(ME "issue read long (%s) to device %s\n    xfer_len=%d (0x%x), "
271             "lba=%" PRIu64 " (0x%" PRIx64 "), correct=%d\n",
272             (do_16 ? "16" : "10"), device_name, xfer_len, xfer_len, llba,
273             llba, (int)correct);
274 
275     if ((ret = process_read_long(sg_fd, do_16, pblock, correct, llba,
276                                  readLongBuff, xfer_len, verbose)))
277         goto err_out;
278 
279     if ('\0' == out_fname[0])
280         hex2stdout((const uint8_t *)rawp, xfer_len, 0);
281     else {
282         got_stdout = (0 == strcmp(out_fname, "-"));
283         if (got_stdout)
284             outfd = STDOUT_FILENO;
285         else {
286             if ((outfd = open(out_fname, O_WRONLY | O_CREAT | O_TRUNC,
287                               0666)) < 0) {
288                 snprintf(ebuff, EBUFF_SZ,
289                          ME "could not open %s for writing", out_fname);
290                 perror(ebuff);
291                 goto err_out;
292             }
293         }
294         if (sg_set_binary_mode(outfd) < 0) {
295             perror("sg_set_binary_mode");
296             goto err_out;
297         }
298         res = write(outfd, readLongBuff, xfer_len);
299         if (res < 0) {
300             snprintf(ebuff, EBUFF_SZ, ME "couldn't write to %s", out_fname);
301             perror(ebuff);
302             goto err_out;
303         }
304         if (! got_stdout)
305             close(outfd);
306     }
307 
308 err_out:
309     if (free_rawp)
310         free(free_rawp);
311     if (sg_fd >= 0) {
312         res = sg_cmds_close_device(sg_fd);
313         if (res < 0) {
314             pr2serr("close error: %s\n", safe_strerror(-res));
315             if (0 == ret)
316                 ret = sg_convert_errno(-res);
317         }
318     }
319     if (0 == verbose) {
320         if (! sg_if_can2stderr("sg_read_long failed: ", ret))
321             pr2serr("Some error occurred, try again with '-v' "
322                     "or '-vv' for more information\n");
323     }
324     return (ret >= 0) ? ret : SG_LIB_CAT_OTHER;
325 }
326