]>
Commit | Line | Data |
---|---|---|
832b75ed | 1 | /* |
4d59bff9 | 2 | * smartctl.cpp |
832b75ed GG |
3 | * |
4 | * Home page of code is: http://smartmontools.sourceforge.net | |
5 | * | |
cfbba5b9 | 6 | * Copyright (C) 2002-11 Bruce Allen <smartmontools-support@lists.sourceforge.net> |
d008864d | 7 | * Copyright (C) 2008-12 Christian Franke <smartmontools-support@lists.sourceforge.net> |
832b75ed GG |
8 | * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org> |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2, or (at your option) | |
13 | * any later version. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
ee38a438 | 16 | * (for example COPYING); If not, see <http://www.gnu.org/licenses/>. |
832b75ed GG |
17 | * |
18 | * This code was originally developed as a Senior Thesis by Michael Cornwell | |
19 | * at the Concurrent Systems Laboratory (now part of the Storage Systems | |
20 | * Research Center), Jack Baskin School of Engineering, University of | |
21 | * California, Santa Cruz. http://ssrc.soe.ucsc.edu/ | |
22 | * | |
23 | */ | |
24 | ||
25 | #include <errno.h> | |
26 | #include <stdio.h> | |
27 | #include <sys/types.h> | |
28 | #include <string.h> | |
29 | #include <stdarg.h> | |
2127e193 GI |
30 | #include <stdexcept> |
31 | #include <getopt.h> | |
832b75ed GG |
32 | |
33 | #include "config.h" | |
2127e193 GI |
34 | |
35 | #ifdef HAVE_UNISTD_H | |
832b75ed GG |
36 | #include <unistd.h> |
37 | #endif | |
38 | ||
2127e193 GI |
39 | #if defined(__FreeBSD__) |
40 | #include <sys/param.h> | |
a37e7145 GG |
41 | #endif |
42 | ||
832b75ed GG |
43 | #include "int64.h" |
44 | #include "atacmds.h" | |
2127e193 | 45 | #include "dev_interface.h" |
832b75ed | 46 | #include "ataprint.h" |
832b75ed GG |
47 | #include "knowndrives.h" |
48 | #include "scsicmds.h" | |
49 | #include "scsiprint.h" | |
50 | #include "smartctl.h" | |
51 | #include "utility.h" | |
52 | ||
ee38a438 | 53 | const char * smartctl_cpp_cvsid = "$Id: smartctl.cpp 3726 2012-12-12 20:02:48Z chrfranke $" |
cfbba5b9 | 54 | CONFIG_H_CVSID SMARTCTL_H_CVSID; |
832b75ed | 55 | |
cfbba5b9 GI |
56 | // Globals to control printing |
57 | bool printing_is_switchable = false; | |
58 | bool printing_is_off = false; | |
832b75ed | 59 | |
2127e193 GI |
60 | static void printslogan() |
61 | { | |
62 | pout("%s\n", format_version_info("smartctl").c_str()); | |
832b75ed GG |
63 | } |
64 | ||
cfbba5b9 GI |
65 | static void UsageSummary() |
66 | { | |
832b75ed GG |
67 | pout("\nUse smartctl -h to get a usage summary\n\n"); |
68 | return; | |
69 | } | |
70 | ||
d008864d | 71 | static std::string getvalidarglist(int opt); |
2127e193 | 72 | |
832b75ed | 73 | /* void prints help information for command syntax */ |
cfbba5b9 GI |
74 | static void Usage() |
75 | { | |
832b75ed | 76 | printf("Usage: smartctl [options] device\n\n"); |
832b75ed | 77 | printf( |
2127e193 | 78 | "============================================ SHOW INFORMATION OPTIONS =====\n\n" |
832b75ed GG |
79 | " -h, --help, --usage\n" |
80 | " Display this help and exit\n\n" | |
81 | " -V, --version, --copyright, --license\n" | |
82 | " Print license, copyright, and version information and exit\n\n" | |
d008864d | 83 | " -i, --info\n" |
832b75ed | 84 | " Show identity information for device\n\n" |
ee38a438 GI |
85 | " --identify[=[w][nvb]]\n" |
86 | " Show words and bits from IDENTIFY DEVICE data (ATA)\n\n" | |
d008864d | 87 | " -g NAME, --get=NAME\n" |
ee38a438 | 88 | " Get device setting: all, aam, apm, lookahead, security, wcache, rcache\n\n" |
d008864d | 89 | " -a, --all\n" |
832b75ed | 90 | " Show all SMART information for device\n\n" |
2127e193 GI |
91 | " -x, --xall\n" |
92 | " Show all information for device\n\n" | |
e9583e0c GI |
93 | " --scan\n" |
94 | " Scan for devices\n\n" | |
95 | " --scan-open\n" | |
96 | " Scan for devices and try to open each device\n\n" | |
832b75ed | 97 | ); |
832b75ed | 98 | printf( |
2127e193 | 99 | "================================== SMARTCTL RUN-TIME BEHAVIOR OPTIONS =====\n\n" |
832b75ed | 100 | " -q TYPE, --quietmode=TYPE (ATA)\n" |
a37e7145 | 101 | " Set smartctl quiet mode to one of: errorsonly, silent, noserial\n\n" |
832b75ed | 102 | " -d TYPE, --device=TYPE\n" |
2127e193 | 103 | " Specify device type to one of: %s\n\n" |
832b75ed GG |
104 | " -T TYPE, --tolerance=TYPE (ATA)\n" |
105 | " Tolerance: normal, conservative, permissive, verypermissive\n\n" | |
106 | " -b TYPE, --badsum=TYPE (ATA)\n" | |
107 | " Set action on bad checksum to one of: warn, exit, ignore\n\n" | |
108 | " -r TYPE, --report=TYPE\n" | |
109 | " Report transactions (see man page)\n\n" | |
4d59bff9 | 110 | " -n MODE, --nocheck=MODE (ATA)\n" |
2127e193 | 111 | " No check if: never, sleep, standby, idle (see man page)\n\n", |
54965743 | 112 | getvalidarglist('d').c_str()); // TODO: Use this function also for other options ? |
832b75ed | 113 | printf( |
2127e193 | 114 | "============================== DEVICE FEATURE ENABLE/DISABLE COMMANDS =====\n\n" |
832b75ed GG |
115 | " -s VALUE, --smart=VALUE\n" |
116 | " Enable/disable SMART on device (on/off)\n\n" | |
117 | " -o VALUE, --offlineauto=VALUE (ATA)\n" | |
118 | " Enable/disable automatic offline testing on device (on/off)\n\n" | |
119 | " -S VALUE, --saveauto=VALUE (ATA)\n" | |
120 | " Enable/disable Attribute autosave on device (on/off)\n\n" | |
d008864d GI |
121 | " -s NAME[,VALUE], --set=NAME[,VALUE]\n" |
122 | " Enable/disable/change device setting: aam,[N|off], apm,[N|off],\n" | |
123 | " lookahead,[on|off], security-freeze, standby,[N|off|now],\n" | |
ee38a438 | 124 | " wcache,[on|off], rcache,[on|off]\n\n" |
832b75ed | 125 | ); |
832b75ed | 126 | printf( |
2127e193 | 127 | "======================================= READ AND DISPLAY DATA OPTIONS =====\n\n" |
832b75ed GG |
128 | " -H, --health\n" |
129 | " Show device SMART health status\n\n" | |
130 | " -c, --capabilities (ATA)\n" | |
131 | " Show device SMART capabilities\n\n" | |
a7e8ffec | 132 | " -A, --attributes\n" |
832b75ed | 133 | " Show device SMART vendor-specific Attributes and values\n\n" |
a7e8ffec | 134 | " -f FORMAT, --format=FORMAT (ATA)\n" |
e165493d | 135 | " Set output format for attributes: old, brief, hex[,id|val]\n\n" |
832b75ed | 136 | " -l TYPE, --log=TYPE\n" |
2127e193 | 137 | " Show device log. TYPE: error, selftest, selective, directory[,g|s],\n" |
d008864d | 138 | " xerror[,N][,error], xselftest[,N][,selftest],\n" |
2127e193 | 139 | " background, sasphy[,reset], sataphy[,reset],\n" |
d008864d GI |
140 | " scttemp[sts,hist], scttempint,N[,p],\n" |
141 | " scterc[,N,M], devstat[,N], ssd,\n" | |
142 | " gplog,N[,RANGE], smartlog,N[,RANGE]\n\n" | |
832b75ed GG |
143 | " -v N,OPTION , --vendorattribute=N,OPTION (ATA)\n" |
144 | " Set display OPTION for vendor Attribute N (see man page)\n\n" | |
145 | " -F TYPE, --firmwarebug=TYPE (ATA)\n" | |
ee38a438 GI |
146 | " Use firmware bug workaround:\n" |
147 | " %s, swapid\n\n" | |
832b75ed GG |
148 | " -P TYPE, --presets=TYPE (ATA)\n" |
149 | " Drive-specific presets: use, ignore, show, showall\n\n" | |
2127e193 GI |
150 | " -B [+]FILE, --drivedb=[+]FILE (ATA)\n" |
151 | " Read and replace [add] drive database from FILE\n" | |
e9583e0c | 152 | " [default is +%s", |
ee38a438 | 153 | get_valid_firmwarebug_args(), |
e9583e0c GI |
154 | get_drivedb_path_add() |
155 | ); | |
2127e193 | 156 | #ifdef SMARTMONTOOLS_DRIVEDBDIR |
e9583e0c GI |
157 | printf( |
158 | "\n" | |
159 | " and then %s", | |
160 | get_drivedb_path_default() | |
2127e193 | 161 | ); |
e9583e0c | 162 | #endif |
832b75ed | 163 | printf( |
e9583e0c | 164 | "]\n\n" |
2127e193 | 165 | "============================================ DEVICE SELF-TEST OPTIONS =====\n\n" |
832b75ed | 166 | " -t TEST, --test=TEST\n" |
d008864d GI |
167 | " Run test. TEST: offline, short, long, conveyance, force, vendor,N,\n" |
168 | " select,M-N, pending,N, afterselect,[on|off]\n\n" | |
832b75ed GG |
169 | " -C, --captive\n" |
170 | " Do test in captive mode (along with -t)\n\n" | |
171 | " -X, --abort\n" | |
172 | " Abort any non-captive test on device\n\n" | |
173 | ); | |
54965743 GI |
174 | std::string examples = smi()->get_app_examples("smartctl"); |
175 | if (!examples.empty()) | |
176 | printf("%s\n", examples.c_str()); | |
832b75ed GG |
177 | } |
178 | ||
d008864d | 179 | // Values for --long only options, see parse_options() |
ee38a438 | 180 | enum { opt_identify = 1000, opt_scan, opt_scan_open, opt_set, opt_smart }; |
d008864d | 181 | |
54965743 GI |
182 | /* Returns a string containing a formatted list of the valid arguments |
183 | to the option opt or empty on failure. Note 'v' case different */ | |
d008864d | 184 | static std::string getvalidarglist(int opt) |
2127e193 | 185 | { |
832b75ed GG |
186 | switch (opt) { |
187 | case 'q': | |
a37e7145 | 188 | return "errorsonly, silent, noserial"; |
832b75ed | 189 | case 'd': |
cfbba5b9 | 190 | return smi()->get_valid_dev_types_str() + ", auto, test"; |
832b75ed GG |
191 | case 'T': |
192 | return "normal, conservative, permissive, verypermissive"; | |
193 | case 'b': | |
194 | return "warn, exit, ignore"; | |
195 | case 'r': | |
196 | return "ioctl[,N], ataioctl[,N], scsiioctl[,N]"; | |
d008864d | 197 | case opt_smart: |
832b75ed GG |
198 | case 'o': |
199 | case 'S': | |
200 | return "on, off"; | |
201 | case 'l': | |
d008864d GI |
202 | return "error, selftest, selective, directory[,g|s], " |
203 | "xerror[,N][,error], xselftest[,N][,selftest], " | |
204 | "background, sasphy[,reset], sataphy[,reset], " | |
205 | "scttemp[sts,hist], scttempint,N[,p], " | |
206 | "scterc[,N,M], devstat[,N], ssd, " | |
207 | "gplog,N[,RANGE], smartlog,N[,RANGE]"; | |
208 | ||
832b75ed GG |
209 | case 'P': |
210 | return "use, ignore, show, showall"; | |
211 | case 't': | |
d008864d GI |
212 | return "offline, short, long, conveyance, force, vendor,N, select,M-N, " |
213 | "pending,N, afterselect,[on|off]"; | |
832b75ed | 214 | case 'F': |
ee38a438 | 215 | return std::string(get_valid_firmwarebug_args()) + ", swapid"; |
4d59bff9 GG |
216 | case 'n': |
217 | return "never, sleep, standby, idle"; | |
a7e8ffec | 218 | case 'f': |
e165493d | 219 | return "old, brief, hex[,id|val]"; |
d008864d | 220 | case 'g': |
ee38a438 | 221 | return "aam, apm, lookahead, security, wcache, rcache"; |
d008864d GI |
222 | case opt_set: |
223 | return "aam,[N|off], apm,[N|off], lookahead,[on|off], security-freeze, " | |
ee38a438 | 224 | "standby,[N|off|now], wcache,[on|off], rcache,[on|off]"; |
d008864d GI |
225 | case 's': |
226 | return getvalidarglist(opt_smart)+", "+getvalidarglist(opt_set); | |
ee38a438 GI |
227 | case opt_identify: |
228 | return "n, wn, w, v, wv, wb"; | |
832b75ed GG |
229 | case 'v': |
230 | default: | |
54965743 | 231 | return ""; |
832b75ed GG |
232 | } |
233 | } | |
234 | ||
235 | /* Prints the message "=======> VALID ARGUMENTS ARE: <LIST> \n", where | |
236 | <LIST> is the list of valid arguments for option opt. */ | |
d008864d | 237 | static void printvalidarglistmessage(int opt) |
cfbba5b9 | 238 | { |
832b75ed | 239 | if (opt=='v'){ |
2127e193 GI |
240 | pout("=======> VALID ARGUMENTS ARE:\n\thelp\n%s\n<=======\n", |
241 | create_vendor_attribute_arg_list().c_str()); | |
832b75ed GG |
242 | } |
243 | else { | |
244 | // getvalidarglist() might produce a multiline or single line string. We | |
245 | // need to figure out which to get the formatting right. | |
54965743 GI |
246 | std::string s = getvalidarglist(opt); |
247 | char separator = strchr(s.c_str(), '\n') ? '\n' : ' '; | |
248 | pout("=======> VALID ARGUMENTS ARE:%c%s%c<=======\n", separator, s.c_str(), separator); | |
832b75ed GG |
249 | } |
250 | ||
251 | return; | |
252 | } | |
253 | ||
2127e193 GI |
254 | // Checksum error mode |
255 | enum checksum_err_mode_t { | |
256 | CHECKSUM_ERR_WARN, CHECKSUM_ERR_EXIT, CHECKSUM_ERR_IGNORE | |
257 | }; | |
258 | ||
259 | static checksum_err_mode_t checksum_err_mode = CHECKSUM_ERR_WARN; | |
260 | ||
cfbba5b9 | 261 | static void scan_devices(const char * type, bool with_open, char ** argv); |
e9583e0c | 262 | |
d008864d | 263 | |
832b75ed | 264 | /* Takes command options and sets features to be run */ |
cfbba5b9 | 265 | static const char * parse_options(int argc, char** argv, |
d008864d GI |
266 | ata_print_options & ataopts, scsi_print_options & scsiopts, |
267 | bool & print_type_only) | |
2127e193 | 268 | { |
832b75ed | 269 | // Please update getvalidarglist() if you edit shortopts |
d008864d | 270 | const char *shortopts = "h?Vq:d:T:b:r:s:o:S:HcAl:iaxv:P:t:CXF:n:B:f:g:"; |
832b75ed GG |
271 | // Please update getvalidarglist() if you edit longopts |
272 | struct option longopts[] = { | |
273 | { "help", no_argument, 0, 'h' }, | |
274 | { "usage", no_argument, 0, 'h' }, | |
275 | { "version", no_argument, 0, 'V' }, | |
276 | { "copyright", no_argument, 0, 'V' }, | |
277 | { "license", no_argument, 0, 'V' }, | |
278 | { "quietmode", required_argument, 0, 'q' }, | |
279 | { "device", required_argument, 0, 'd' }, | |
280 | { "tolerance", required_argument, 0, 'T' }, | |
281 | { "badsum", required_argument, 0, 'b' }, | |
282 | { "report", required_argument, 0, 'r' }, | |
d008864d | 283 | { "smart", required_argument, 0, opt_smart }, |
832b75ed GG |
284 | { "offlineauto", required_argument, 0, 'o' }, |
285 | { "saveauto", required_argument, 0, 'S' }, | |
286 | { "health", no_argument, 0, 'H' }, | |
287 | { "capabilities", no_argument, 0, 'c' }, | |
288 | { "attributes", no_argument, 0, 'A' }, | |
289 | { "log", required_argument, 0, 'l' }, | |
290 | { "info", no_argument, 0, 'i' }, | |
291 | { "all", no_argument, 0, 'a' }, | |
2127e193 | 292 | { "xall", no_argument, 0, 'x' }, |
832b75ed GG |
293 | { "vendorattribute", required_argument, 0, 'v' }, |
294 | { "presets", required_argument, 0, 'P' }, | |
295 | { "test", required_argument, 0, 't' }, | |
296 | { "captive", no_argument, 0, 'C' }, | |
297 | { "abort", no_argument, 0, 'X' }, | |
298 | { "firmwarebug", required_argument, 0, 'F' }, | |
4d59bff9 | 299 | { "nocheck", required_argument, 0, 'n' }, |
2127e193 | 300 | { "drivedb", required_argument, 0, 'B' }, |
a7e8ffec | 301 | { "format", required_argument, 0, 'f' }, |
d008864d | 302 | { "get", required_argument, 0, 'g' }, |
ee38a438 | 303 | { "identify", optional_argument, 0, opt_identify }, |
d008864d | 304 | { "set", required_argument, 0, opt_set }, |
e9583e0c GI |
305 | { "scan", no_argument, 0, opt_scan }, |
306 | { "scan-open", no_argument, 0, opt_scan_open }, | |
832b75ed GG |
307 | { 0, 0, 0, 0 } |
308 | }; | |
2127e193 GI |
309 | |
310 | char extraerror[256]; | |
832b75ed | 311 | memset(extraerror, 0, sizeof(extraerror)); |
832b75ed | 312 | opterr=optopt=0; |
2127e193 GI |
313 | |
314 | const char * type = 0; // set to -d optarg | |
315 | bool no_defaultdb = false; // set true on '-B FILE' | |
a7e8ffec | 316 | bool output_format_set = false; // set true on '-f FORMAT' |
e9583e0c | 317 | int scan = 0; // set by --scan, --scan-open |
2127e193 GI |
318 | bool badarg = false, captive = false; |
319 | int testcnt = 0; // number of self-tests requested | |
320 | ||
321 | int optchar; | |
322 | char *arg; | |
323 | ||
e9583e0c | 324 | while ((optchar = getopt_long(argc, argv, shortopts, longopts, 0)) != -1) { |
832b75ed GG |
325 | switch (optchar){ |
326 | case 'V': | |
cfbba5b9 | 327 | printing_is_off = false; |
2127e193 GI |
328 | pout("%s", format_version_info("smartctl", true /*full*/).c_str()); |
329 | EXIT(0); | |
832b75ed GG |
330 | break; |
331 | case 'q': | |
332 | if (!strcmp(optarg,"errorsonly")) { | |
cfbba5b9 GI |
333 | printing_is_switchable = true; |
334 | printing_is_off = false; | |
832b75ed | 335 | } else if (!strcmp(optarg,"silent")) { |
cfbba5b9 GI |
336 | printing_is_switchable = false; |
337 | printing_is_off = true; | |
a37e7145 | 338 | } else if (!strcmp(optarg,"noserial")) { |
cfbba5b9 | 339 | dont_print_serial_number = true; |
832b75ed | 340 | } else { |
2127e193 | 341 | badarg = true; |
832b75ed GG |
342 | } |
343 | break; | |
344 | case 'd': | |
d008864d GI |
345 | if (!strcmp(optarg, "test")) |
346 | print_type_only = true; | |
347 | else | |
348 | type = (strcmp(optarg, "auto") ? optarg : (char *)0); | |
832b75ed GG |
349 | break; |
350 | case 'T': | |
351 | if (!strcmp(optarg,"normal")) { | |
cfbba5b9 GI |
352 | failuretest_conservative = false; |
353 | failuretest_permissive = 0; | |
832b75ed | 354 | } else if (!strcmp(optarg,"conservative")) { |
cfbba5b9 | 355 | failuretest_conservative = true; |
832b75ed | 356 | } else if (!strcmp(optarg,"permissive")) { |
cfbba5b9 GI |
357 | if (failuretest_permissive < 0xff) |
358 | failuretest_permissive++; | |
832b75ed | 359 | } else if (!strcmp(optarg,"verypermissive")) { |
cfbba5b9 | 360 | failuretest_permissive = 0xff; |
832b75ed | 361 | } else { |
2127e193 | 362 | badarg = true; |
832b75ed GG |
363 | } |
364 | break; | |
365 | case 'b': | |
366 | if (!strcmp(optarg,"warn")) { | |
2127e193 | 367 | checksum_err_mode = CHECKSUM_ERR_WARN; |
832b75ed | 368 | } else if (!strcmp(optarg,"exit")) { |
2127e193 | 369 | checksum_err_mode = CHECKSUM_ERR_EXIT; |
832b75ed | 370 | } else if (!strcmp(optarg,"ignore")) { |
2127e193 | 371 | checksum_err_mode = CHECKSUM_ERR_IGNORE; |
832b75ed | 372 | } else { |
2127e193 | 373 | badarg = true; |
832b75ed GG |
374 | } |
375 | break; | |
376 | case 'r': | |
377 | { | |
378 | int i; | |
379 | char *s; | |
380 | ||
381 | // split_report_arg() may modify its first argument string, so use a | |
382 | // copy of optarg in case we want optarg for an error message. | |
383 | if (!(s = strdup(optarg))) { | |
2127e193 | 384 | throw std::bad_alloc(); |
832b75ed GG |
385 | } |
386 | if (split_report_arg(s, &i)) { | |
2127e193 | 387 | badarg = true; |
832b75ed | 388 | } else if (!strcmp(s,"ioctl")) { |
cfbba5b9 | 389 | ata_debugmode = scsi_debugmode = i; |
832b75ed | 390 | } else if (!strcmp(s,"ataioctl")) { |
cfbba5b9 | 391 | ata_debugmode = i; |
832b75ed | 392 | } else if (!strcmp(s,"scsiioctl")) { |
cfbba5b9 | 393 | scsi_debugmode = i; |
832b75ed | 394 | } else { |
2127e193 | 395 | badarg = true; |
832b75ed GG |
396 | } |
397 | free(s); | |
398 | } | |
399 | break; | |
d008864d | 400 | |
832b75ed | 401 | case 's': |
d008864d | 402 | case opt_smart: // --smart |
832b75ed | 403 | if (!strcmp(optarg,"on")) { |
2127e193 GI |
404 | ataopts.smart_enable = scsiopts.smart_enable = true; |
405 | ataopts.smart_disable = scsiopts.smart_disable = false; | |
832b75ed | 406 | } else if (!strcmp(optarg,"off")) { |
2127e193 GI |
407 | ataopts.smart_disable = scsiopts.smart_disable = true; |
408 | ataopts.smart_enable = scsiopts.smart_enable = false; | |
d008864d GI |
409 | } else if (optchar == 's') { |
410 | goto case_s_continued; // --set, see below | |
832b75ed | 411 | } else { |
2127e193 | 412 | badarg = true; |
832b75ed GG |
413 | } |
414 | break; | |
d008864d | 415 | |
832b75ed GG |
416 | case 'o': |
417 | if (!strcmp(optarg,"on")) { | |
2127e193 GI |
418 | ataopts.smart_auto_offl_enable = true; |
419 | ataopts.smart_auto_offl_disable = false; | |
832b75ed | 420 | } else if (!strcmp(optarg,"off")) { |
2127e193 GI |
421 | ataopts.smart_auto_offl_disable = true; |
422 | ataopts.smart_auto_offl_enable = false; | |
832b75ed | 423 | } else { |
2127e193 | 424 | badarg = true; |
832b75ed GG |
425 | } |
426 | break; | |
427 | case 'S': | |
428 | if (!strcmp(optarg,"on")) { | |
2127e193 GI |
429 | ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = true; |
430 | ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = false; | |
832b75ed | 431 | } else if (!strcmp(optarg,"off")) { |
2127e193 GI |
432 | ataopts.smart_auto_save_disable = scsiopts.smart_auto_save_disable = true; |
433 | ataopts.smart_auto_save_enable = scsiopts.smart_auto_save_enable = false; | |
832b75ed | 434 | } else { |
2127e193 | 435 | badarg = true; |
832b75ed GG |
436 | } |
437 | break; | |
438 | case 'H': | |
2127e193 | 439 | ataopts.smart_check_status = scsiopts.smart_check_status = true; |
d008864d | 440 | scsiopts.smart_ss_media_log = true; |
832b75ed GG |
441 | break; |
442 | case 'F': | |
ee38a438 | 443 | if (!strcmp(optarg, "swapid")) |
2127e193 | 444 | ataopts.fix_swapped_id = true; |
ee38a438 | 445 | else if (!parse_firmwarebug_def(optarg, ataopts.firmwarebugs)) |
2127e193 | 446 | badarg = true; |
832b75ed GG |
447 | break; |
448 | case 'c': | |
2127e193 | 449 | ataopts.smart_general_values = true; |
832b75ed GG |
450 | break; |
451 | case 'A': | |
2127e193 | 452 | ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true; |
832b75ed GG |
453 | break; |
454 | case 'l': | |
455 | if (!strcmp(optarg,"error")) { | |
2127e193 | 456 | ataopts.smart_error_log = scsiopts.smart_error_log = true; |
832b75ed | 457 | } else if (!strcmp(optarg,"selftest")) { |
2127e193 | 458 | ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true; |
832b75ed | 459 | } else if (!strcmp(optarg, "selective")) { |
2127e193 | 460 | ataopts.smart_selective_selftest_log = true; |
832b75ed | 461 | } else if (!strcmp(optarg,"directory")) { |
2127e193 GI |
462 | ataopts.smart_logdir = ataopts.gp_logdir = true; // SMART+GPL |
463 | } else if (!strcmp(optarg,"directory,s")) { | |
464 | ataopts.smart_logdir = true; // SMART | |
465 | } else if (!strcmp(optarg,"directory,g")) { | |
466 | ataopts.gp_logdir = true; // GPL | |
467 | } else if (!strcmp(optarg,"sasphy")) { | |
468 | scsiopts.sasphy = true; | |
469 | } else if (!strcmp(optarg,"sasphy,reset")) { | |
470 | scsiopts.sasphy = scsiopts.sasphy_reset = true; | |
471 | } else if (!strcmp(optarg,"sataphy")) { | |
472 | ataopts.sataphy = true; | |
473 | } else if (!strcmp(optarg,"sataphy,reset")) { | |
474 | ataopts.sataphy = ataopts.sataphy_reset = true; | |
4d59bff9 | 475 | } else if (!strcmp(optarg,"background")) { |
2127e193 | 476 | scsiopts.smart_background_log = true; |
d008864d GI |
477 | } else if (!strcmp(optarg,"ssd")) { |
478 | ataopts.devstat_ssd_page = true; | |
479 | scsiopts.smart_ss_media_log = true; | |
7f0798ef GI |
480 | } else if (!strcmp(optarg,"scterc")) { |
481 | ataopts.sct_erc_get = true; | |
a37e7145 | 482 | } else if (!strcmp(optarg,"scttemp")) { |
2127e193 | 483 | ataopts.sct_temp_sts = ataopts.sct_temp_hist = true; |
a37e7145 | 484 | } else if (!strcmp(optarg,"scttempsts")) { |
2127e193 | 485 | ataopts.sct_temp_sts = true; |
a37e7145 | 486 | } else if (!strcmp(optarg,"scttemphist")) { |
2127e193 GI |
487 | ataopts.sct_temp_hist = true; |
488 | ||
d008864d GI |
489 | } else if (!strncmp(optarg, "scttempint,", sizeof("scstempint,")-1)) { |
490 | unsigned interval = 0; int n1 = -1, n2 = -1, len = strlen(optarg); | |
491 | if (!( sscanf(optarg,"scttempint,%u%n,p%n", &interval, &n1, &n2) == 1 | |
492 | && 0 < interval && interval <= 0xffff && (n1 == len || n2 == len))) { | |
ee38a438 | 493 | snprintf(extraerror, sizeof(extraerror), "Option -l scttempint,N[,p] must have positive integer N\n"); |
d008864d GI |
494 | badarg = true; |
495 | } | |
496 | ataopts.sct_temp_int = interval; | |
497 | ataopts.sct_temp_int_pers = (n2 == len); | |
498 | ||
499 | } else if (!strncmp(optarg, "devstat", sizeof("devstat")-1)) { | |
500 | int n1 = -1, n2 = -1, len = strlen(optarg); | |
501 | unsigned val = ~0; | |
502 | sscanf(optarg, "devstat%n,%u%n", &n1, &val, &n2); | |
503 | if (n1 == len) | |
504 | ataopts.devstat_all_pages = true; | |
505 | else if (n2 == len && val <= 255) | |
506 | ataopts.devstat_pages.push_back(val); | |
507 | else | |
508 | badarg = true; | |
509 | ||
2127e193 GI |
510 | } else if (!strncmp(optarg, "xerror", sizeof("xerror")-1)) { |
511 | int n1 = -1, n2 = -1, len = strlen(optarg); | |
512 | unsigned val = 8; | |
513 | sscanf(optarg, "xerror%n,error%n", &n1, &n2); | |
514 | if (!(n1 == len || n2 == len)) { | |
515 | n1 = n2 = -1; | |
516 | sscanf(optarg, "xerror,%u%n,error%n", &val, &n1, &n2); | |
517 | } | |
518 | if ((n1 == len || n2 == len) && val > 0) { | |
519 | ataopts.smart_ext_error_log = val; | |
520 | ataopts.retry_error_log = (n2 == len); | |
521 | } | |
522 | else | |
523 | badarg = true; | |
524 | ||
525 | } else if (!strncmp(optarg, "xselftest", sizeof("xselftest")-1)) { | |
526 | int n1 = -1, n2 = -1, len = strlen(optarg); | |
527 | unsigned val = 25; | |
528 | sscanf(optarg, "xselftest%n,selftest%n", &n1, &n2); | |
529 | if (!(n1 == len || n2 == len)) { | |
530 | n1 = n2 = -1; | |
531 | sscanf(optarg, "xselftest,%u%n,selftest%n", &val, &n1, &n2); | |
532 | } | |
533 | if ((n1 == len || n2 == len) && val > 0) { | |
534 | ataopts.smart_ext_selftest_log = val; | |
535 | ataopts.retry_selftest_log = (n2 == len); | |
536 | } | |
537 | else | |
538 | badarg = true; | |
539 | ||
7f0798ef GI |
540 | } else if (!strncmp(optarg, "scterc,", sizeof("scterc,")-1)) { |
541 | unsigned rt = ~0, wt = ~0; int n = -1; | |
542 | sscanf(optarg,"scterc,%u,%u%n", &rt, &wt, &n); | |
543 | if (n == (int)strlen(optarg) && rt <= 999 && wt <= 999) { | |
544 | ataopts.sct_erc_set = true; | |
545 | ataopts.sct_erc_readtime = rt; | |
546 | ataopts.sct_erc_writetime = wt; | |
547 | } | |
548 | else { | |
ee38a438 | 549 | snprintf(extraerror, sizeof(extraerror), "Option -l scterc,[READTIME,WRITETIME] syntax error\n"); |
7f0798ef GI |
550 | badarg = true; |
551 | } | |
2127e193 GI |
552 | } else if ( !strncmp(optarg, "gplog," , sizeof("gplog," )-1) |
553 | || !strncmp(optarg, "smartlog,", sizeof("smartlog,")-1)) { | |
554 | unsigned logaddr = ~0U; unsigned page = 0, nsectors = 1; char sign = 0; | |
555 | int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg); | |
556 | sscanf(optarg, "%*[a-z],0x%x%n,%u%n%c%u%n", | |
557 | &logaddr, &n1, &page, &n2, &sign, &nsectors, &n3); | |
558 | if (len > n2 && n3 == -1 && !strcmp(optarg+n2, "-max")) { | |
559 | nsectors = ~0U; sign = '+'; n3 = len; | |
560 | } | |
561 | bool gpl = (optarg[0] == 'g'); | |
562 | const char * erropt = (gpl ? "gplog" : "smartlog"); | |
563 | if (!( n1 == len || n2 == len | |
564 | || (n3 == len && (sign == '+' || sign == '-')))) { | |
ee38a438 | 565 | snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] syntax error\n", erropt); |
2127e193 GI |
566 | badarg = true; |
567 | } | |
568 | else if (!( logaddr <= 0xff && page <= (gpl ? 0xffffU : 0x00ffU) | |
569 | && 0 < nsectors | |
570 | && (nsectors <= (gpl ? 0xffffU : 0xffU) || nsectors == ~0U) | |
571 | && (sign != '-' || page <= nsectors) )) { | |
ee38a438 | 572 | snprintf(extraerror, sizeof(extraerror), "Option -l %s,ADDR[,FIRST[-LAST|+SIZE]] parameter out of range\n", erropt); |
2127e193 GI |
573 | badarg = true; |
574 | } | |
575 | else { | |
576 | ata_log_request req; | |
577 | req.gpl = gpl; req.logaddr = logaddr; req.page = page; | |
578 | req.nsectors = (sign == '-' ? nsectors-page+1 : nsectors); | |
579 | ataopts.log_requests.push_back(req); | |
580 | } | |
832b75ed | 581 | } else { |
2127e193 | 582 | badarg = true; |
832b75ed GG |
583 | } |
584 | break; | |
585 | case 'i': | |
2127e193 GI |
586 | ataopts.drive_info = scsiopts.drive_info = true; |
587 | break; | |
ee38a438 GI |
588 | |
589 | case opt_identify: | |
590 | ataopts.identify_word_level = ataopts.identify_bit_level = 0; | |
591 | if (optarg) { | |
592 | for (int i = 0; optarg[i]; i++) { | |
593 | switch (optarg[i]) { | |
594 | case 'w': ataopts.identify_word_level = 1; break; | |
595 | case 'n': ataopts.identify_bit_level = -1; break; | |
596 | case 'v': ataopts.identify_bit_level = 1; break; | |
597 | case 'b': ataopts.identify_bit_level = 2; break; | |
598 | default: badarg = true; | |
599 | } | |
600 | } | |
601 | } | |
602 | break; | |
603 | ||
832b75ed | 604 | case 'a': |
2127e193 GI |
605 | ataopts.drive_info = scsiopts.drive_info = true; |
606 | ataopts.smart_check_status = scsiopts.smart_check_status = true; | |
607 | ataopts.smart_general_values = true; | |
608 | ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true; | |
609 | ataopts.smart_error_log = scsiopts.smart_error_log = true; | |
610 | ataopts.smart_selftest_log = scsiopts.smart_selftest_log = true; | |
611 | ataopts.smart_selective_selftest_log = true; | |
612 | /* scsiopts.smart_background_log = true; */ | |
d008864d | 613 | scsiopts.smart_ss_media_log = true; |
2127e193 GI |
614 | break; |
615 | case 'x': | |
616 | ataopts.drive_info = scsiopts.drive_info = true; | |
617 | ataopts.smart_check_status = scsiopts.smart_check_status = true; | |
618 | ataopts.smart_general_values = true; | |
619 | ataopts.smart_vendor_attrib = scsiopts.smart_vendor_attrib = true; | |
620 | ataopts.smart_ext_error_log = 8; | |
621 | ataopts.retry_error_log = true; | |
622 | ataopts.smart_ext_selftest_log = 25; | |
623 | ataopts.retry_selftest_log = true; | |
624 | scsiopts.smart_error_log = scsiopts.smart_selftest_log = true; | |
625 | ataopts.smart_selective_selftest_log = true; | |
626 | ataopts.smart_logdir = ataopts.gp_logdir = true; | |
627 | ataopts.sct_temp_sts = ataopts.sct_temp_hist = true; | |
7f0798ef | 628 | ataopts.sct_erc_get = true; |
ee38a438 | 629 | ataopts.devstat_all_pages = true; |
2127e193 | 630 | ataopts.sataphy = true; |
d008864d GI |
631 | ataopts.get_set_used = true; |
632 | ataopts.get_aam = ataopts.get_apm = true; | |
633 | ataopts.get_security = true; | |
634 | ataopts.get_lookahead = ataopts.get_wcache = true; | |
ee38a438 | 635 | scsiopts.get_rcd = scsiopts.get_wce = true; |
2127e193 | 636 | scsiopts.smart_background_log = true; |
d008864d | 637 | scsiopts.smart_ss_media_log = true; |
2127e193 | 638 | scsiopts.sasphy = true; |
a7e8ffec | 639 | if (!output_format_set) |
e165493d | 640 | ataopts.output_format |= ata_print_options::FMT_BRIEF; |
832b75ed GG |
641 | break; |
642 | case 'v': | |
643 | // parse vendor-specific definitions of attributes | |
644 | if (!strcmp(optarg,"help")) { | |
cfbba5b9 | 645 | printing_is_off = false; |
832b75ed | 646 | printslogan(); |
2127e193 GI |
647 | pout("The valid arguments to -v are:\n\thelp\n%s\n", |
648 | create_vendor_attribute_arg_list().c_str()); | |
832b75ed GG |
649 | EXIT(0); |
650 | } | |
bed94269 | 651 | if (!parse_attribute_def(optarg, ataopts.attribute_defs, PRIOR_USER)) |
2127e193 | 652 | badarg = true; |
832b75ed GG |
653 | break; |
654 | case 'P': | |
655 | if (!strcmp(optarg, "use")) { | |
2127e193 | 656 | ataopts.ignore_presets = false; |
832b75ed | 657 | } else if (!strcmp(optarg, "ignore")) { |
2127e193 | 658 | ataopts.ignore_presets = true; |
832b75ed | 659 | } else if (!strcmp(optarg, "show")) { |
2127e193 | 660 | ataopts.show_presets = true; |
832b75ed | 661 | } else if (!strcmp(optarg, "showall")) { |
2127e193 GI |
662 | if (!no_defaultdb && !read_default_drive_databases()) |
663 | EXIT(FAILCMD); | |
832b75ed GG |
664 | if (optind < argc) { // -P showall MODEL [FIRMWARE] |
665 | int cnt = showmatchingpresets(argv[optind], (optind+1<argc ? argv[optind+1] : NULL)); | |
666 | EXIT(cnt); // report #matches | |
667 | } | |
668 | if (showallpresets()) | |
669 | EXIT(FAILCMD); // report regexp syntax error | |
670 | EXIT(0); | |
671 | } else { | |
2127e193 | 672 | badarg = true; |
832b75ed GG |
673 | } |
674 | break; | |
675 | case 't': | |
676 | if (!strcmp(optarg,"offline")) { | |
2127e193 GI |
677 | testcnt++; |
678 | ataopts.smart_selftest_type = OFFLINE_FULL_SCAN; | |
679 | scsiopts.smart_default_selftest = true; | |
832b75ed | 680 | } else if (!strcmp(optarg,"short")) { |
2127e193 GI |
681 | testcnt++; |
682 | ataopts.smart_selftest_type = SHORT_SELF_TEST; | |
683 | scsiopts.smart_short_selftest = true; | |
832b75ed | 684 | } else if (!strcmp(optarg,"long")) { |
2127e193 GI |
685 | testcnt++; |
686 | ataopts.smart_selftest_type = EXTEND_SELF_TEST; | |
687 | scsiopts.smart_extend_selftest = true; | |
832b75ed | 688 | } else if (!strcmp(optarg,"conveyance")) { |
2127e193 GI |
689 | testcnt++; |
690 | ataopts.smart_selftest_type = CONVEYANCE_SELF_TEST; | |
d008864d GI |
691 | } else if (!strcmp(optarg,"force")) { |
692 | ataopts.smart_selftest_force = true; | |
ee38a438 | 693 | scsiopts.smart_selftest_force = true; |
832b75ed | 694 | } else if (!strcmp(optarg,"afterselect,on")) { |
2127e193 GI |
695 | // scan remainder of disk after doing selected segment |
696 | ataopts.smart_selective_args.scan_after_select = 2; | |
832b75ed | 697 | } else if (!strcmp(optarg,"afterselect,off")) { |
2127e193 GI |
698 | // don't scan remainder of disk after doing selected segments |
699 | ataopts.smart_selective_args.scan_after_select = 1; | |
832b75ed GG |
700 | } else if (!strncmp(optarg,"pending,",strlen("pending,"))) { |
701 | // parse number of minutes that test should be pending | |
702 | int i; | |
703 | char *tailptr=NULL; | |
704 | errno=0; | |
705 | i=(int)strtol(optarg+strlen("pending,"), &tailptr, 10); | |
706 | if (errno || *tailptr != '\0') { | |
ee38a438 | 707 | snprintf(extraerror, sizeof(extraerror), "Option -t pending,N requires N to be a non-negative integer\n"); |
2127e193 | 708 | badarg = true; |
832b75ed | 709 | } else if (i<0 || i>65535) { |
ee38a438 | 710 | snprintf(extraerror, sizeof(extraerror), "Option -t pending,N (N=%d) must have 0 <= N <= 65535\n", i); |
2127e193 | 711 | badarg = true; |
832b75ed | 712 | } else { |
2127e193 | 713 | ataopts.smart_selective_args.pending_time = i+1; |
832b75ed GG |
714 | } |
715 | } else if (!strncmp(optarg,"select",strlen("select"))) { | |
e9583e0c GI |
716 | if (ataopts.smart_selective_args.num_spans == 0) |
717 | testcnt++; | |
a37e7145 GG |
718 | // parse range of LBAs to test |
719 | uint64_t start, stop; int mode; | |
720 | if (split_selective_arg(optarg, &start, &stop, &mode)) { | |
ee38a438 | 721 | snprintf(extraerror, sizeof(extraerror), "Option -t select,M-N must have non-negative integer M and N\n"); |
2127e193 | 722 | badarg = true; |
832b75ed | 723 | } else { |
2127e193 | 724 | if (ataopts.smart_selective_args.num_spans >= 5 || start > stop) { |
832b75ed | 725 | if (start > stop) { |
ee38a438 | 726 | snprintf(extraerror, sizeof(extraerror), "ERROR: Start LBA (%"PRIu64") > ending LBA (%"PRId64") in argument \"%s\"\n", |
832b75ed GG |
727 | start, stop, optarg); |
728 | } else { | |
ee38a438 | 729 | snprintf(extraerror, sizeof(extraerror),"ERROR: No more than five selective self-test spans may be" |
832b75ed GG |
730 | " defined\n"); |
731 | } | |
2127e193 | 732 | badarg = true; |
832b75ed | 733 | } |
2127e193 GI |
734 | ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].start = start; |
735 | ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].end = stop; | |
736 | ataopts.smart_selective_args.span[ataopts.smart_selective_args.num_spans].mode = mode; | |
737 | ataopts.smart_selective_args.num_spans++; | |
738 | ataopts.smart_selftest_type = SELECTIVE_SELF_TEST; | |
832b75ed | 739 | } |
d008864d | 740 | } else if (!strncmp(optarg, "scttempint", sizeof("scstempint")-1)) { |
ee38a438 | 741 | snprintf(extraerror, sizeof(extraerror), "-t scttempint is no longer supported, use -l scttempint instead\n"); |
d008864d | 742 | badarg = true; |
cfbba5b9 GI |
743 | } else if (!strncmp(optarg, "vendor,", sizeof("vendor,")-1)) { |
744 | unsigned subcmd = ~0U; int n = -1; | |
745 | if (!( sscanf(optarg, "%*[a-z],0x%x%n", &subcmd, &n) == 1 | |
746 | && subcmd <= 0xff && n == (int)strlen(optarg))) { | |
ee38a438 | 747 | snprintf(extraerror, sizeof(extraerror), "Option -t vendor,0xNN syntax error\n"); |
cfbba5b9 GI |
748 | badarg = true; |
749 | } | |
750 | else | |
751 | ataopts.smart_selftest_type = subcmd; | |
832b75ed | 752 | } else { |
2127e193 | 753 | badarg = true; |
832b75ed GG |
754 | } |
755 | break; | |
756 | case 'C': | |
2127e193 | 757 | captive = true; |
832b75ed GG |
758 | break; |
759 | case 'X': | |
2127e193 GI |
760 | testcnt++; |
761 | scsiopts.smart_selftest_abort = true; | |
762 | ataopts.smart_selftest_type = ABORT_SELF_TEST; | |
832b75ed | 763 | break; |
4d59bff9 GG |
764 | case 'n': |
765 | // skip disk check if in low-power mode | |
766 | if (!strcmp(optarg, "never")) | |
2127e193 | 767 | ataopts.powermode = 1; // do not skip, but print mode |
4d59bff9 | 768 | else if (!strcmp(optarg, "sleep")) |
2127e193 | 769 | ataopts.powermode = 2; |
4d59bff9 | 770 | else if (!strcmp(optarg, "standby")) |
2127e193 | 771 | ataopts.powermode = 3; |
4d59bff9 | 772 | else if (!strcmp(optarg, "idle")) |
2127e193 | 773 | ataopts.powermode = 4; |
4d59bff9 | 774 | else |
2127e193 GI |
775 | badarg = true; |
776 | break; | |
a7e8ffec | 777 | case 'f': |
e165493d GI |
778 | if (!strcmp(optarg, "old")) { |
779 | ataopts.output_format &= ~ata_print_options::FMT_BRIEF; | |
780 | output_format_set = true; | |
781 | } | |
782 | else if (!strcmp(optarg, "brief")) { | |
783 | ataopts.output_format |= ata_print_options::FMT_BRIEF; | |
784 | output_format_set = true; | |
a7e8ffec | 785 | } |
e165493d GI |
786 | else if (!strcmp(optarg, "hex")) |
787 | ataopts.output_format |= ata_print_options::FMT_HEX_ID | |
788 | | ata_print_options::FMT_HEX_VAL; | |
789 | else if (!strcmp(optarg, "hex,id")) | |
790 | ataopts.output_format |= ata_print_options::FMT_HEX_ID; | |
791 | else if (!strcmp(optarg, "hex,val")) | |
792 | ataopts.output_format |= ata_print_options::FMT_HEX_VAL; | |
793 | else | |
794 | badarg = true; | |
a7e8ffec | 795 | break; |
2127e193 GI |
796 | case 'B': |
797 | { | |
798 | const char * path = optarg; | |
799 | if (*path == '+' && path[1]) | |
800 | path++; | |
801 | else | |
802 | no_defaultdb = true; | |
803 | if (!read_drive_database(path)) | |
804 | EXIT(FAILCMD); | |
805 | } | |
4d59bff9 | 806 | break; |
832b75ed | 807 | case 'h': |
cfbba5b9 | 808 | printing_is_off = false; |
832b75ed GG |
809 | printslogan(); |
810 | Usage(); | |
811 | EXIT(0); | |
812 | break; | |
e9583e0c | 813 | |
d008864d GI |
814 | case 'g': |
815 | case_s_continued: // -s, see above | |
816 | case opt_set: // --set | |
817 | { | |
818 | ataopts.get_set_used = true; | |
819 | bool get = (optchar == 'g'); | |
820 | char name[16+1]; unsigned val; | |
821 | int n1 = -1, n2 = -1, n3 = -1, len = strlen(optarg); | |
822 | if (sscanf(optarg, "%16[^,=]%n%*[,=]%n%u%n", name, &n1, &n2, &val, &n3) >= 1 | |
823 | && (n1 == len || (!get && n2 > 0))) { | |
824 | bool on = (n2 > 0 && !strcmp(optarg+n2, "on")); | |
825 | bool off = (n2 > 0 && !strcmp(optarg+n2, "off")); | |
826 | if (n3 != len) | |
827 | val = ~0U; | |
828 | ||
829 | if (get && !strcmp(name, "all")) { | |
830 | ataopts.get_aam = ataopts.get_apm = true; | |
831 | ataopts.get_security = true; | |
832 | ataopts.get_lookahead = ataopts.get_wcache = true; | |
ee38a438 | 833 | scsiopts.get_rcd = scsiopts.get_wce = true; |
d008864d GI |
834 | } |
835 | else if (!strcmp(name, "aam")) { | |
836 | if (get) | |
837 | ataopts.get_aam = true; | |
838 | else if (off) | |
839 | ataopts.set_aam = -1; | |
840 | else if (val <= 254) | |
841 | ataopts.set_aam = val + 1; | |
842 | else { | |
ee38a438 | 843 | snprintf(extraerror, sizeof(extraerror), "Option -s aam,N must have 0 <= N <= 254\n"); |
d008864d GI |
844 | badarg = true; |
845 | } | |
846 | } | |
847 | else if (!strcmp(name, "apm")) { | |
848 | if (get) | |
849 | ataopts.get_apm = true; | |
850 | else if (off) | |
851 | ataopts.set_apm = -1; | |
852 | else if (1 <= val && val <= 254) | |
853 | ataopts.set_apm = val + 1; | |
854 | else { | |
ee38a438 | 855 | snprintf(extraerror, sizeof(extraerror), "Option -s apm,N must have 1 <= N <= 254\n"); |
d008864d GI |
856 | badarg = true; |
857 | } | |
858 | } | |
859 | else if (!strcmp(name, "lookahead")) { | |
ee38a438 | 860 | if (get) { |
d008864d | 861 | ataopts.get_lookahead = true; |
ee38a438 | 862 | } |
d008864d GI |
863 | else if (off) |
864 | ataopts.set_lookahead = -1; | |
865 | else if (on) | |
866 | ataopts.set_lookahead = 1; | |
867 | else | |
868 | badarg = true; | |
869 | } | |
ee38a438 GI |
870 | else if (!strcmp(name, "rcache")) { |
871 | if (get) | |
872 | scsiopts.get_rcd = true; | |
873 | else if (off) | |
874 | scsiopts.set_rcd = -1; | |
875 | else if (on) | |
876 | scsiopts.set_rcd = 1; | |
877 | else | |
878 | badarg = true; | |
879 | } | |
d008864d GI |
880 | else if (get && !strcmp(name, "security")) { |
881 | ataopts.get_security = true; | |
882 | } | |
883 | else if (!get && !strcmp(optarg, "security-freeze")) { | |
884 | ataopts.set_security_freeze = true; | |
885 | } | |
886 | else if (!get && !strcmp(optarg, "standby,now")) { | |
887 | ataopts.set_standby_now = true; | |
888 | } | |
889 | else if (!get && !strcmp(name, "standby")) { | |
890 | if (off) | |
891 | ataopts.set_standby = 0 + 1; | |
892 | else if (val <= 255) | |
893 | ataopts.set_standby = val + 1; | |
894 | else { | |
ee38a438 | 895 | snprintf(extraerror, sizeof(extraerror), "Option -s standby,N must have 0 <= N <= 255\n"); |
d008864d GI |
896 | badarg = true; |
897 | } | |
898 | } | |
899 | else if (!strcmp(name, "wcache")) { | |
ee38a438 | 900 | if (get) { |
d008864d | 901 | ataopts.get_wcache = true; |
ee38a438 GI |
902 | scsiopts.get_wce = true; |
903 | } | |
904 | else if (off) { | |
d008864d | 905 | ataopts.set_wcache = -1; |
ee38a438 GI |
906 | scsiopts.set_wce = -1; |
907 | } | |
908 | else if (on) { | |
d008864d | 909 | ataopts.set_wcache = 1; |
ee38a438 GI |
910 | scsiopts.set_wce = 1; |
911 | } | |
d008864d GI |
912 | else |
913 | badarg = true; | |
914 | } | |
915 | else | |
916 | badarg = true; | |
917 | } | |
918 | else | |
919 | badarg = true; | |
920 | } | |
921 | break; | |
922 | ||
e9583e0c GI |
923 | case opt_scan: |
924 | case opt_scan_open: | |
925 | scan = optchar; | |
926 | break; | |
927 | ||
832b75ed GG |
928 | case '?': |
929 | default: | |
cfbba5b9 | 930 | printing_is_off = false; |
832b75ed | 931 | printslogan(); |
832b75ed GG |
932 | // Point arg to the argument in which this option was found. |
933 | arg = argv[optind-1]; | |
934 | // Check whether the option is a long option that doesn't map to -h. | |
935 | if (arg[1] == '-' && optchar != 'h') { | |
936 | // Iff optopt holds a valid option then argument must be missing. | |
d008864d | 937 | if (optopt && (optopt >= opt_scan || strchr(shortopts, optopt))) { |
832b75ed GG |
938 | pout("=======> ARGUMENT REQUIRED FOR OPTION: %s\n", arg+2); |
939 | printvalidarglistmessage(optopt); | |
940 | } else | |
941 | pout("=======> UNRECOGNIZED OPTION: %s\n",arg+2); | |
942 | if (extraerror[0]) | |
943 | pout("=======> %s", extraerror); | |
944 | UsageSummary(); | |
945 | EXIT(FAILCMD); | |
946 | } | |
d008864d | 947 | if (0 < optopt && optopt < '~') { |
832b75ed GG |
948 | // Iff optopt holds a valid option then argument must be |
949 | // missing. Note (BA) this logic seems to fail using Solaris | |
950 | // getopt! | |
951 | if (strchr(shortopts, optopt) != NULL) { | |
952 | pout("=======> ARGUMENT REQUIRED FOR OPTION: %c\n", optopt); | |
953 | printvalidarglistmessage(optopt); | |
954 | } else | |
955 | pout("=======> UNRECOGNIZED OPTION: %c\n",optopt); | |
956 | if (extraerror[0]) | |
957 | pout("=======> %s", extraerror); | |
958 | UsageSummary(); | |
959 | EXIT(FAILCMD); | |
960 | } | |
961 | Usage(); | |
962 | EXIT(0); | |
963 | } // closes switch statement to process command-line options | |
964 | ||
965 | // Check to see if option had an unrecognized or incorrect argument. | |
966 | if (badarg) { | |
967 | printslogan(); | |
968 | // It would be nice to print the actual option name given by the user | |
969 | // here, but we just print the short form. Please fix this if you know | |
970 | // a clean way to do it. | |
d008864d GI |
971 | char optstr[] = { (char)optchar, 0 }; |
972 | pout("=======> INVALID ARGUMENT TO -%s: %s\n", | |
ee38a438 GI |
973 | (optchar == opt_identify ? "-identify" : |
974 | optchar == opt_set ? "-set" : | |
d008864d | 975 | optchar == opt_smart ? "-smart" : optstr), optarg); |
832b75ed GG |
976 | printvalidarglistmessage(optchar); |
977 | if (extraerror[0]) | |
978 | pout("=======> %s", extraerror); | |
979 | UsageSummary(); | |
980 | EXIT(FAILCMD); | |
981 | } | |
982 | } | |
e9583e0c GI |
983 | |
984 | // Special handling of --scan, --scanopen | |
985 | if (scan) { | |
cfbba5b9 GI |
986 | // Read or init drive database to allow USB ID check. |
987 | if (!no_defaultdb && !read_default_drive_databases()) | |
988 | EXIT(FAILCMD); | |
989 | scan_devices(type, (scan == opt_scan_open), argv + optind); | |
e9583e0c GI |
990 | EXIT(0); |
991 | } | |
992 | ||
832b75ed GG |
993 | // At this point we have processed all command-line options. If the |
994 | // print output is switchable, then start with the print output | |
995 | // turned off | |
cfbba5b9 GI |
996 | if (printing_is_switchable) |
997 | printing_is_off = true; | |
832b75ed GG |
998 | |
999 | // error message if user has asked for more than one test | |
2127e193 | 1000 | if (testcnt > 1) { |
cfbba5b9 | 1001 | printing_is_off = false; |
832b75ed GG |
1002 | printslogan(); |
1003 | pout("\nERROR: smartctl can only run a single test type (or abort) at a time.\n"); | |
1004 | UsageSummary(); | |
1005 | EXIT(FAILCMD); | |
1006 | } | |
1007 | ||
1008 | // error message if user has set selective self-test options without | |
1009 | // asking for a selective self-test | |
2127e193 GI |
1010 | if ( (ataopts.smart_selective_args.pending_time || ataopts.smart_selective_args.scan_after_select) |
1011 | && !ataopts.smart_selective_args.num_spans) { | |
cfbba5b9 | 1012 | printing_is_off = false; |
832b75ed | 1013 | printslogan(); |
2127e193 | 1014 | if (ataopts.smart_selective_args.pending_time) |
832b75ed GG |
1015 | pout("\nERROR: smartctl -t pending,N must be used with -t select,N-M.\n"); |
1016 | else | |
1017 | pout("\nERROR: smartctl -t afterselect,(on|off) must be used with -t select,N-M.\n"); | |
1018 | UsageSummary(); | |
1019 | EXIT(FAILCMD); | |
1020 | } | |
1021 | ||
1022 | // If captive option was used, change test type if appropriate. | |
2127e193 GI |
1023 | if (captive) |
1024 | switch (ataopts.smart_selftest_type) { | |
1025 | case SHORT_SELF_TEST: | |
1026 | ataopts.smart_selftest_type = SHORT_CAPTIVE_SELF_TEST; | |
1027 | scsiopts.smart_short_selftest = false; | |
1028 | scsiopts.smart_short_cap_selftest = true; | |
1029 | break; | |
1030 | case EXTEND_SELF_TEST: | |
1031 | ataopts.smart_selftest_type = EXTEND_CAPTIVE_SELF_TEST; | |
1032 | scsiopts.smart_extend_selftest = false; | |
1033 | scsiopts.smart_extend_cap_selftest = true; | |
1034 | break; | |
1035 | case CONVEYANCE_SELF_TEST: | |
1036 | ataopts.smart_selftest_type = CONVEYANCE_CAPTIVE_SELF_TEST; | |
1037 | break; | |
1038 | case SELECTIVE_SELF_TEST: | |
1039 | ataopts.smart_selftest_type = SELECTIVE_CAPTIVE_SELF_TEST; | |
1040 | break; | |
1041 | } | |
1042 | ||
832b75ed GG |
1043 | // From here on, normal operations... |
1044 | printslogan(); | |
1045 | ||
1046 | // Warn if the user has provided no device name | |
1047 | if (argc-optind<1){ | |
1048 | pout("ERROR: smartctl requires a device name as the final command-line argument.\n\n"); | |
1049 | UsageSummary(); | |
1050 | EXIT(FAILCMD); | |
1051 | } | |
1052 | ||
1053 | // Warn if the user has provided more than one device name | |
1054 | if (argc-optind>1){ | |
1055 | int i; | |
1056 | pout("ERROR: smartctl takes ONE device name as the final command-line argument.\n"); | |
1057 | pout("You have provided %d device names:\n",argc-optind); | |
1058 | for (i=0; i<argc-optind; i++) | |
1059 | pout("%s\n",argv[optind+i]); | |
1060 | UsageSummary(); | |
1061 | EXIT(FAILCMD); | |
2127e193 GI |
1062 | } |
1063 | ||
1064 | // Read or init drive database | |
1065 | if (!no_defaultdb && !read_default_drive_databases()) | |
1066 | EXIT(FAILCMD); | |
1067 | ||
1068 | return type; | |
832b75ed GG |
1069 | } |
1070 | ||
cfbba5b9 | 1071 | // Printing function (controlled by global printing_is_off) |
832b75ed GG |
1072 | // [From GLIBC Manual: Since the prototype doesn't specify types for |
1073 | // optional arguments, in a call to a variadic function the default | |
1074 | // argument promotions are performed on the optional argument | |
1075 | // values. This means the objects of type char or short int (whether | |
1076 | // signed or not) are promoted to either int or unsigned int, as | |
1077 | // appropriate.] | |
4d59bff9 | 1078 | void pout(const char *fmt, ...){ |
832b75ed GG |
1079 | va_list ap; |
1080 | ||
1081 | // initialize variable argument list | |
1082 | va_start(ap,fmt); | |
cfbba5b9 | 1083 | if (printing_is_off) { |
832b75ed GG |
1084 | va_end(ap); |
1085 | return; | |
1086 | } | |
1087 | ||
1088 | // print out | |
1089 | vprintf(fmt,ap); | |
1090 | va_end(ap); | |
1091 | fflush(stdout); | |
1092 | return; | |
1093 | } | |
1094 | ||
cfbba5b9 GI |
1095 | // Globals to set failuretest() policy |
1096 | bool failuretest_conservative = false; | |
1097 | unsigned char failuretest_permissive = 0; | |
832b75ed | 1098 | |
cfbba5b9 GI |
1099 | // Compares failure type to policy in effect, and either exits or |
1100 | // simply returns to the calling routine. | |
1101 | // Used in ataprint.cpp and scsiprint.cpp. | |
1102 | void failuretest(failure_type type, int returnvalue) | |
1103 | { | |
1104 | // If this is an error in an "optional" SMART command | |
1105 | if (type == OPTIONAL_CMD) { | |
1106 | if (!failuretest_conservative) | |
1107 | return; | |
1108 | pout("An optional SMART command failed: exiting. Remove '-T conservative' option to continue.\n"); | |
1109 | EXIT(returnvalue); | |
1110 | } | |
832b75ed | 1111 | |
cfbba5b9 GI |
1112 | // If this is an error in a "mandatory" SMART command |
1113 | if (type == MANDATORY_CMD) { | |
1114 | if (failuretest_permissive--) | |
1115 | return; | |
1116 | pout("A mandatory SMART command failed: exiting. To continue, add one or more '-T permissive' options.\n"); | |
1117 | EXIT(returnvalue); | |
1118 | } | |
1119 | ||
1120 | throw std::logic_error("failuretest: Unknown type"); | |
832b75ed GG |
1121 | } |
1122 | ||
2127e193 GI |
1123 | // Used to warn users about invalid checksums. Called from atacmds.cpp. |
1124 | // Action to be taken may be altered by the user. | |
1125 | void checksumwarning(const char * string) | |
1126 | { | |
1127 | // user has asked us to ignore checksum errors | |
1128 | if (checksum_err_mode == CHECKSUM_ERR_IGNORE) | |
1129 | return; | |
832b75ed | 1130 | |
2127e193 | 1131 | pout("Warning! %s error: invalid SMART checksum.\n", string); |
832b75ed | 1132 | |
2127e193 GI |
1133 | // user has asked us to fail on checksum errors |
1134 | if (checksum_err_mode == CHECKSUM_ERR_EXIT) | |
1135 | EXIT(FAILSMART); | |
1136 | } | |
832b75ed | 1137 | |
2127e193 GI |
1138 | // Return info string about device protocol |
1139 | static const char * get_protocol_info(const smart_device * dev) | |
1140 | { | |
1141 | switch ((int)dev->is_ata() | ((int)dev->is_scsi() << 1)) { | |
1142 | case 0x1: return "ATA"; | |
1143 | case 0x2: return "SCSI"; | |
1144 | case 0x3: return "ATA+SCSI"; | |
1145 | default: return "Unknown"; | |
1146 | } | |
1147 | } | |
832b75ed | 1148 | |
e9583e0c | 1149 | // Device scan |
cfbba5b9 GI |
1150 | // smartctl [-d type] --scan[-open] -- [PATTERN] [smartd directive ...] |
1151 | void scan_devices(const char * type, bool with_open, char ** argv) | |
e9583e0c | 1152 | { |
cfbba5b9 GI |
1153 | bool dont_print = !(ata_debugmode || scsi_debugmode); |
1154 | ||
1155 | const char * pattern = 0; | |
1156 | int ai = 0; | |
1157 | if (argv[ai] && argv[ai][0] != '-') | |
1158 | pattern = argv[ai++]; | |
e9583e0c | 1159 | |
cfbba5b9 GI |
1160 | smart_device_list devlist; |
1161 | printing_is_off = dont_print; | |
e9583e0c | 1162 | bool ok = smi()->scan_smart_devices(devlist, type , pattern); |
cfbba5b9 | 1163 | printing_is_off = false; |
e9583e0c GI |
1164 | |
1165 | if (!ok) { | |
cfbba5b9 | 1166 | pout("# scan_smart_devices: %s\n", smi()->get_errmsg()); |
e9583e0c GI |
1167 | return; |
1168 | } | |
1169 | ||
1170 | for (unsigned i = 0; i < devlist.size(); i++) { | |
cfbba5b9 | 1171 | smart_device_auto_ptr dev( devlist.release(i) ); |
e9583e0c | 1172 | |
e9583e0c | 1173 | if (with_open) { |
cfbba5b9 GI |
1174 | printing_is_off = dont_print; |
1175 | dev.replace ( dev->autodetect_open() ); | |
1176 | printing_is_off = false; | |
e9583e0c | 1177 | |
cfbba5b9 GI |
1178 | if (!dev->is_open()) { |
1179 | pout("# %s -d %s # %s, %s device open failed: %s\n", dev->get_dev_name(), | |
1180 | dev->get_dev_type(), dev->get_info_name(), | |
1181 | get_protocol_info(dev.get()), dev->get_errmsg()); | |
1182 | continue; | |
1183 | } | |
1184 | } | |
1185 | ||
1186 | pout("%s -d %s", dev->get_dev_name(), dev->get_dev_type()); | |
1187 | if (!argv[ai]) | |
1188 | pout(" # %s, %s device\n", dev->get_info_name(), get_protocol_info(dev.get())); | |
1189 | else { | |
1190 | for (int j = ai; argv[j]; j++) | |
1191 | pout(" %s", argv[j]); | |
1192 | pout("\n"); | |
e9583e0c GI |
1193 | } |
1194 | ||
e9583e0c GI |
1195 | if (dev->is_open()) |
1196 | dev->close(); | |
1197 | } | |
1198 | } | |
1199 | ||
2127e193 | 1200 | // Main program without exception handling |
cfbba5b9 | 1201 | static int main_worker(int argc, char **argv) |
2127e193 | 1202 | { |
e9583e0c GI |
1203 | // Throw if CPU endianess does not match compile time test. |
1204 | check_endianness(); | |
1205 | ||
2127e193 GI |
1206 | // Initialize interface |
1207 | smart_interface::init(); | |
1208 | if (!smi()) | |
1209 | return 1; | |
1210 | ||
bed94269 GI |
1211 | // Parse input arguments |
1212 | ata_print_options ataopts; | |
1213 | scsi_print_options scsiopts; | |
d008864d GI |
1214 | bool print_type_only = false; |
1215 | const char * type = parse_options(argc, argv, ataopts, scsiopts, print_type_only); | |
bed94269 GI |
1216 | |
1217 | const char * name = argv[argc-1]; | |
1218 | ||
1219 | smart_device_auto_ptr dev; | |
1220 | if (!strcmp(name,"-")) { | |
1221 | // Parse "smartctl -r ataioctl,2 ..." output from stdin | |
1222 | if (type || print_type_only) { | |
ee38a438 | 1223 | pout("-d option is not allowed in conjunction with device name \"-\".\n"); |
a37e7145 GG |
1224 | UsageSummary(); |
1225 | return FAILCMD; | |
1226 | } | |
bed94269 GI |
1227 | dev = get_parsed_ata_device(smi(), name); |
1228 | } | |
1229 | else | |
1230 | // get device of appropriate type | |
1231 | dev = smi()->get_smart_device(name, type); | |
1232 | ||
1233 | if (!dev) { | |
1234 | pout("%s: %s\n", name, smi()->get_errmsg()); | |
1235 | if (type) | |
1236 | printvalidarglistmessage('d'); | |
1237 | else | |
ee38a438 | 1238 | pout("Please specify device type with the -d option.\n"); |
bed94269 GI |
1239 | UsageSummary(); |
1240 | return FAILCMD; | |
1241 | } | |
a37e7145 | 1242 | |
bed94269 GI |
1243 | if (print_type_only) |
1244 | // Report result of first autodetection | |
1245 | pout("%s: Device of type '%s' [%s] detected\n", | |
1246 | dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get())); | |
2127e193 | 1247 | |
bed94269 GI |
1248 | // Open device |
1249 | { | |
1250 | // Save old info | |
1251 | smart_device::device_info oldinfo = dev->get_info(); | |
2127e193 | 1252 | |
bed94269 GI |
1253 | // Open with autodetect support, may return 'better' device |
1254 | dev.replace( dev->autodetect_open() ); | |
2127e193 | 1255 | |
bed94269 GI |
1256 | // Report if type has changed |
1257 | if ((type || print_type_only) && oldinfo.dev_type != dev->get_dev_type()) | |
1258 | pout("%s: Device open changed type from '%s' to '%s'\n", | |
1259 | dev->get_info_name(), oldinfo.dev_type.c_str(), dev->get_dev_type()); | |
2127e193 | 1260 | } |
bed94269 GI |
1261 | if (!dev->is_open()) { |
1262 | pout("Smartctl open device: %s failed: %s\n", dev->get_info_name(), dev->get_errmsg()); | |
1263 | return FAILDEV; | |
832b75ed | 1264 | } |
bed94269 GI |
1265 | |
1266 | // now call appropriate ATA or SCSI routine | |
1267 | int retval = 0; | |
1268 | if (print_type_only) | |
1269 | pout("%s: Device of type '%s' [%s] opened\n", | |
1270 | dev->get_info_name(), dev->get_dev_type(), get_protocol_info(dev.get())); | |
1271 | else if (dev->is_ata()) | |
1272 | retval = ataPrintMain(dev->to_ata(), ataopts); | |
1273 | else if (dev->is_scsi()) | |
1274 | retval = scsiPrintMain(dev->to_scsi(), scsiopts); | |
1275 | else | |
1276 | // we should never fall into this branch! | |
1277 | pout("%s: Neither ATA nor SCSI device\n", dev->get_info_name()); | |
1278 | ||
1279 | dev->close(); | |
2127e193 GI |
1280 | return retval; |
1281 | } | |
832b75ed | 1282 | |
2127e193 GI |
1283 | |
1284 | // Main program | |
1285 | int main(int argc, char **argv) | |
1286 | { | |
1287 | int status; | |
1288 | try { | |
1289 | // Do the real work ... | |
1290 | status = main_worker(argc, argv); | |
832b75ed | 1291 | } |
2127e193 GI |
1292 | catch (int ex) { |
1293 | // EXIT(status) arrives here | |
1294 | status = ex; | |
832b75ed | 1295 | } |
2127e193 GI |
1296 | catch (const std::bad_alloc & /*ex*/) { |
1297 | // Memory allocation failed (also thrown by std::operator new) | |
bed94269 | 1298 | printf("Smartctl: Out of memory\n"); |
2127e193 | 1299 | status = FAILCMD; |
832b75ed | 1300 | } |
2127e193 GI |
1301 | catch (const std::exception & ex) { |
1302 | // Other fatal errors | |
bed94269 | 1303 | printf("Smartctl: Exception: %s\n", ex.what()); |
2127e193 | 1304 | status = FAILCMD; |
832b75ed | 1305 | } |
2127e193 | 1306 | return status; |
832b75ed | 1307 | } |
2127e193 | 1308 |