-
Notifications
You must be signed in to change notification settings - Fork 5
/
skdump.c
347 lines (272 loc) · 12.4 KB
/
skdump.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of libatasmart.
Copyright 2008 Lennart Poettering
libatasmart is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 2.1 of the
License, or (at your option) any later version.
libatasmart is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with libatasmart. If not, If not, see
<http://www.gnu.org/licenses/>.
***/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include "atasmart.h"
enum {
MODE_DUMP = 10,
MODE_OVERALL,
MODE_POWER_ON,
MODE_POWER_CYCLE,
MODE_BAD,
MODE_TEMPERATURE,
MODE_STATUS,
MODE_CAN_SMART,
MODE_SAVE
};
int mode = MODE_DUMP;
enum {
ARG_SAVE = 256,
ARG_LOAD
};
int main(int argc, char *argv[]) {
int ret;
const char *device = NULL, *argv0, *p, *file = NULL;
SkDisk *d;
int q = 1;
SkBool from_blob = FALSE;
static const struct option long_options[] = {
{"overall", no_argument, &mode, MODE_OVERALL},
{"power-on", no_argument, &mode, MODE_POWER_ON},
{"power-cycle", no_argument, &mode, MODE_POWER_CYCLE},
{"bad", no_argument, &mode, MODE_BAD},
{"temperature", no_argument, &mode, MODE_TEMPERATURE},
{"can-smart", no_argument, &mode, MODE_CAN_SMART},
{"status", no_argument, &mode, MODE_STATUS},
{"save", optional_argument, NULL, ARG_SAVE},
{"load", optional_argument, NULL, ARG_LOAD},
{"help", no_argument, NULL, 'h' },
{0, 0, 0, 0}
};
argv0 = argv[0];
if ((p = strrchr(argv0, '/')))
argv0 = p+1;
for (;;) {
int opt;
if ((opt = getopt_long(argc, argv, "h", long_options, NULL)) < 0)
break;
switch (opt) {
case 0:
break;
case 'h':
fprintf(stderr,
"Usage: %s [PARAMETERS] DEVICE\n"
"Reads ATA SMART data from a device and parses it.\n"
"\n"
"\t--overall \tShow overall status\n"
"\t--status \tShow SMART status\n"
"\t--can-smart \tShow whether SMART is supported\n"
"\t--power-on \tPrint power on time in ms\n"
"\t--power-cycle \tPrint number of power cycles\n"
"\t--bad \tPrint bad sector count\n"
"\t--temperature \tPrint drive temperature in mKelvin\n"
"\t--save[=FILENAME]\tSave raw data to file/STDOUT\n"
"\t--load[=FILENAME]\tRead data from a file/STDIN instead of device\n"
"\t-h | --help \tShow this help\n", argv0);
return 0;
case ARG_SAVE:
file = optarg;
mode = MODE_SAVE;
break;
case ARG_LOAD:
device = optarg ? optarg : "-";
from_blob = TRUE;
break;
case '?':
return 1;
default:
fprintf(stderr, "Invalid arguments.\n");
return 1;
}
}
if (!device) {
if (optind != argc-1) {
fprintf(stderr, "No or more than one device specified.\n");
return 1;
}
device = argv[optind];
} else {
if (optind != argc) {
fprintf(stderr, "Too many arguments.\n");
return 1;
}
}
if (from_blob) {
uint8_t blob[4096];
size_t size;
FILE *f = stdin;
if ((ret = sk_disk_open(NULL, &d)) < 0) {
fprintf(stderr, "Failed to open disk: %s\n", strerror(errno));
return 1;
}
if (strcmp(device, "-")) {
if (!(f = fopen(device, "r"))) {
fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
goto finish;
}
}
size = fread(blob, 1, sizeof(blob), f);
if (f != stdin)
fclose(f);
if (size >= sizeof(blob)) {
fprintf(stderr, "File too large for buffer.\n");
goto finish;
}
if ((ret = sk_disk_set_blob(d, blob, size)) < 0) {
fprintf(stderr, "Failed to set blob: %s\n", strerror(errno));
goto finish;
}
} else {
if ((ret = sk_disk_open(device, &d)) < 0) {
fprintf(stderr, "Failed to open disk %s: %s\n", device, strerror(errno));
return 1;
}
}
switch (mode) {
case MODE_DUMP:
if ((ret = sk_disk_dump(d)) < 0) {
fprintf(stderr, "Failed to dump disk data: %s\n", strerror(errno));
goto finish;
}
break;
case MODE_CAN_SMART: {
SkBool available;
if ((ret = sk_disk_smart_is_available(d, &available)) < 0) {
fprintf(stderr, "Failed to query whether SMART is available: %s\n", strerror(errno));
goto finish;
}
printf("%s\n", available ? "YES" : "NO");
q = available ? 0 : 1;
break;
}
case MODE_OVERALL: {
SkSmartOverall overall;
if ((ret = sk_disk_smart_read_data(d)) < 0) {
fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
goto finish;
}
if ((ret = sk_disk_smart_get_overall(d, &overall)) < 0) {
fprintf(stderr, "Failed to get overall status: %s\n", strerror(errno));
goto finish;
}
printf("%s\n", sk_smart_overall_to_string(overall));
q = overall == SK_SMART_OVERALL_GOOD ? 0 : 1;
goto finish;
}
case MODE_STATUS: {
SkBool good;
if ((ret = sk_disk_smart_status(d, &good)) < 0) {
fprintf(stderr, "Failed to get SMART status: %s\n", strerror(errno));
goto finish;
}
printf("%s\n", good ? "GOOD" : "BAD");
q = good ? 0 : 1;
goto finish;
}
case MODE_POWER_ON: {
uint64_t ms;
if ((ret = sk_disk_smart_read_data(d)) < 0) {
fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
goto finish;
}
if ((ret = sk_disk_smart_get_power_on(d, &ms)) < 0) {
fprintf(stderr, "Failed to get power on time: %s\n", strerror(errno));
goto finish;
}
printf("%llu\n", (unsigned long long) ms);
break;
}
case MODE_POWER_CYCLE: {
uint64_t count;
if ((ret = sk_disk_smart_read_data(d)) < 0) {
fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
goto finish;
}
if ((ret = sk_disk_smart_get_power_cycle(d, &count)) < 0) {
fprintf(stderr, "Failed to get power cycles: %s\n", strerror(errno));
goto finish;
}
printf("%llu\n", (unsigned long long) count);
break;
}
case MODE_BAD: {
uint64_t bad;
if ((ret = sk_disk_smart_read_data(d)) < 0) {
fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
goto finish;
}
if ((ret = sk_disk_smart_get_bad(d, &bad)) < 0) {
fprintf(stderr, "Failed to get bad sectors: %s\n", strerror(errno));
goto finish;
}
printf("%llu\n", (unsigned long long) bad);
q = !!bad;
goto finish;
}
case MODE_TEMPERATURE: {
uint64_t mkelvin;
if ((ret = sk_disk_smart_read_data(d)) < 0) {
fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
goto finish;
}
if ((ret = sk_disk_smart_get_temperature(d, &mkelvin)) < 0) {
fprintf(stderr, "Failed to get temperature: %s\n", strerror(errno));
goto finish;
}
printf("%llu\n", (unsigned long long) mkelvin);
break;
}
case MODE_SAVE: {
const void *blob;
size_t size;
FILE *f = stdout;
size_t n;
if ((ret = sk_disk_smart_read_data(d)) < 0) {
fprintf(stderr, "Failed to read SMART data: %s\n", strerror(errno));
goto finish;
}
if ((ret = sk_disk_get_blob(d, &blob, &size)) < 0) {
fprintf(stderr, "Failed to get blob: %s\n", strerror(errno));
goto finish;
}
if (file && strcmp(file, "-")) {
if (!(f = fopen(file, "w"))) {
fprintf(stderr, "Failed to open '%s': %s\n", file, strerror(errno));
goto finish;
}
}
n = fwrite(blob, 1, size, f);
if (f != stdout)
fclose(f);
if (n != size) {
fprintf(stderr, "Failed to write to disk: %s\n", strerror(errno));
goto finish;
}
break;
}
}
q = 0;
finish:
if (d)
sk_disk_free(d);
return q;
}