]>
Commit | Line | Data |
---|---|---|
832b75ed GG |
1 | /* |
2 | * os_openbsd.c | |
3 | * | |
a86ec89e | 4 | * Home page of code is: http://www.smartmontools.org |
832b75ed | 5 | * |
f9e10201 | 6 | * Copyright (C) 2004-10 David Snyder |
832b75ed | 7 | * |
f9e10201 | 8 | * Derived from os_netbsd.cpp by Sergey Svishchev, Copyright (C) 2003-8 |
832b75ed | 9 | * |
ff28b140 | 10 | * SPDX-License-Identifier: GPL-2.0-or-later |
832b75ed GG |
11 | */ |
12 | ||
13 | #include "config.h" | |
ff28b140 | 14 | |
832b75ed GG |
15 | #include "atacmds.h" |
16 | #include "scsicmds.h" | |
17 | #include "utility.h" | |
18 | #include "os_openbsd.h" | |
19 | ||
cfbba5b9 GI |
20 | #include <errno.h> |
21 | ||
ff28b140 | 22 | const char * os_openbsd_cpp_cvsid = "$Id: os_openbsd.cpp 4842 2018-12-02 16:07:26Z chrfranke $" |
cfbba5b9 | 23 | OS_OPENBSD_H_CVSID; |
832b75ed | 24 | |
832b75ed | 25 | enum warnings { |
a86ec89e | 26 | BAD_SMART, MAX_MSG |
832b75ed GG |
27 | }; |
28 | ||
29 | /* Utility function for printing warnings */ | |
30 | void | |
31 | printwarning(int msgNo, const char *extra) | |
32 | { | |
33 | static int printed[] = {0, 0}; | |
34 | static const char *message[] = { | |
35 | "Error: SMART Status command failed.\nPlease get assistance from \n" PACKAGE_HOMEPAGE "\nRegister values returned from SMART Status command are:\n", | |
ff28b140 | 36 | PACKAGE_STRING " does not currently support twe(4) devices (3ware Escalade)\n", |
832b75ed GG |
37 | }; |
38 | ||
39 | if (msgNo >= 0 && msgNo <= MAX_MSG) { | |
40 | if (!printed[msgNo]) { | |
41 | printed[msgNo] = 1; | |
42 | pout("%s", message[msgNo]); | |
43 | if (extra) | |
44 | pout("%s", extra); | |
45 | } | |
46 | } | |
47 | return; | |
48 | } | |
49 | ||
50 | static const char *net_dev_prefix = "/dev/"; | |
51 | static const char *net_dev_ata_disk = "wd"; | |
52 | static const char *net_dev_scsi_disk = "sd"; | |
53 | static const char *net_dev_scsi_tape = "st"; | |
54 | ||
55 | /* Guess device type(ata or scsi) based on device name */ | |
56 | int | |
57 | guess_device_type(const char *dev_name) | |
58 | { | |
59 | int len; | |
60 | int dev_prefix_len = strlen(net_dev_prefix); | |
61 | ||
62 | if (!dev_name || !(len = strlen(dev_name))) | |
63 | return CONTROLLER_UNKNOWN; | |
64 | ||
65 | if (!strncmp(net_dev_prefix, dev_name, dev_prefix_len)) { | |
66 | if (len <= dev_prefix_len) | |
67 | return CONTROLLER_UNKNOWN; | |
68 | else | |
69 | dev_name += dev_prefix_len; | |
70 | } | |
71 | if (!strncmp(net_dev_ata_disk, dev_name, strlen(net_dev_ata_disk))) | |
72 | return CONTROLLER_ATA; | |
73 | ||
74 | if (!strncmp(net_dev_scsi_disk, dev_name, strlen(net_dev_scsi_disk))) | |
75 | return CONTROLLER_SCSI; | |
76 | ||
77 | if (!strncmp(net_dev_scsi_tape, dev_name, strlen(net_dev_scsi_tape))) | |
78 | return CONTROLLER_SCSI; | |
79 | ||
80 | return CONTROLLER_UNKNOWN; | |
81 | } | |
82 | ||
83 | int | |
84 | get_dev_names(char ***names, const char *prefix) | |
85 | { | |
86 | char *disknames, *p, **mp; | |
87 | int n = 0; | |
88 | int sysctl_mib[2]; | |
89 | size_t sysctl_len; | |
90 | ||
91 | *names = NULL; | |
92 | ||
93 | sysctl_mib[0] = CTL_HW; | |
94 | sysctl_mib[1] = HW_DISKNAMES; | |
95 | if (-1 == sysctl(sysctl_mib, 2, NULL, &sysctl_len, NULL, 0)) { | |
96 | pout("Failed to get value of sysctl `hw.disknames'\n"); | |
97 | return -1; | |
98 | } | |
a37e7145 | 99 | if (!(disknames = (char *)malloc(sysctl_len))) { |
832b75ed GG |
100 | pout("Out of memory constructing scan device list\n"); |
101 | return -1; | |
102 | } | |
103 | if (-1 == sysctl(sysctl_mib, 2, disknames, &sysctl_len, NULL, 0)) { | |
104 | pout("Failed to get value of sysctl `hw.disknames'\n"); | |
105 | return -1; | |
106 | } | |
107 | if (!(mp = (char **) calloc(strlen(disknames) / 2, sizeof(char *)))) { | |
108 | pout("Out of memory constructing scan device list\n"); | |
109 | return -1; | |
110 | } | |
111 | for (p = strtok(disknames, ","); p; p = strtok(NULL, ",")) { | |
112 | if (strncmp(p, prefix, strlen(prefix))) { | |
113 | continue; | |
114 | } | |
cfbba5b9 GI |
115 | char * u = strchr(p, ':'); |
116 | if (u) | |
117 | *u = 0; | |
a37e7145 | 118 | mp[n] = (char *)malloc(strlen(net_dev_prefix) + strlen(p) + 2); |
832b75ed GG |
119 | if (!mp[n]) { |
120 | pout("Out of memory constructing scan device list\n"); | |
121 | return -1; | |
122 | } | |
123 | sprintf(mp[n], "%s%s%c", net_dev_prefix, p, 'a' + getrawpartition()); | |
832b75ed GG |
124 | n++; |
125 | } | |
126 | ||
a86ec89e GI |
127 | char ** tmp = (char **)realloc(mp, n * (sizeof(char *))); |
128 | if (NULL == tmp) { | |
129 | pout("Out of memory constructing scan device list\n"); | |
130 | free(mp); | |
131 | return -1; | |
132 | } | |
133 | else | |
134 | mp = tmp; | |
832b75ed GG |
135 | *names = mp; |
136 | return n; | |
137 | } | |
138 | ||
139 | int | |
140 | make_device_names(char ***devlist, const char *name) | |
141 | { | |
142 | if (!strcmp(name, "SCSI")) | |
143 | return get_dev_names(devlist, net_dev_scsi_disk); | |
144 | else if (!strcmp(name, "ATA")) | |
145 | return get_dev_names(devlist, net_dev_ata_disk); | |
146 | else | |
147 | return 0; | |
148 | } | |
149 | ||
150 | int | |
151 | deviceopen(const char *pathname, char *type) | |
152 | { | |
153 | if (!strcmp(type, "SCSI")) { | |
154 | int fd = open(pathname, O_RDWR | O_NONBLOCK); | |
155 | if (fd < 0 && errno == EROFS) | |
156 | fd = open(pathname, O_RDONLY | O_NONBLOCK); | |
157 | return fd; | |
158 | } else if (!strcmp(type, "ATA")) | |
159 | return open(pathname, O_RDWR | O_NONBLOCK); | |
160 | else | |
161 | return -1; | |
162 | } | |
163 | ||
164 | int | |
165 | deviceclose(int fd) | |
166 | { | |
167 | return close(fd); | |
168 | } | |
169 | ||
832b75ed GG |
170 | int |
171 | ata_command_interface(int fd, smart_command_set command, int select, char *data) | |
172 | { | |
173 | struct atareq req; | |
174 | unsigned char inbuf[DEV_BSIZE]; | |
175 | int retval, copydata = 0; | |
176 | ||
177 | memset(&req, 0, sizeof(req)); | |
178 | memset(&inbuf, 0, sizeof(inbuf)); | |
179 | ||
180 | switch (command) { | |
181 | case READ_VALUES: | |
182 | req.flags = ATACMD_READ; | |
183 | req.features = ATA_SMART_READ_VALUES; | |
184 | req.command = ATAPI_SMART; | |
185 | req.databuf = (caddr_t) inbuf; | |
186 | req.datalen = sizeof(inbuf); | |
187 | req.cylinder = htole16(WDSMART_CYL); | |
188 | req.timeout = 1000; | |
189 | copydata = 1; | |
190 | break; | |
191 | case READ_THRESHOLDS: | |
192 | req.flags = ATACMD_READ; | |
193 | req.features = ATA_SMART_READ_THRESHOLDS; | |
194 | req.command = ATAPI_SMART; | |
195 | req.databuf = (caddr_t) inbuf; | |
196 | req.datalen = sizeof(inbuf); | |
197 | req.cylinder = htole16(WDSMART_CYL); | |
198 | req.timeout = 1000; | |
199 | copydata = 1; | |
200 | break; | |
201 | case READ_LOG: | |
202 | req.flags = ATACMD_READ; | |
203 | req.features = ATA_SMART_READ_LOG_SECTOR; /* XXX missing from wdcreg.h */ | |
204 | req.command = ATAPI_SMART; | |
205 | req.databuf = (caddr_t) inbuf; | |
206 | req.datalen = sizeof(inbuf); | |
207 | req.cylinder = htole16(WDSMART_CYL); | |
208 | req.sec_num = select; | |
209 | req.sec_count = 1; | |
210 | req.timeout = 1000; | |
211 | copydata = 1; | |
212 | break; | |
213 | case WRITE_LOG: | |
214 | memcpy(inbuf, data, 512); | |
215 | req.flags = ATACMD_WRITE; | |
216 | req.features = ATA_SMART_WRITE_LOG_SECTOR; /* XXX missing from wdcreg.h */ | |
217 | req.command = ATAPI_SMART; | |
218 | req.databuf = (caddr_t) inbuf; | |
219 | req.datalen = sizeof(inbuf); | |
220 | req.cylinder = htole16(WDSMART_CYL); | |
221 | req.sec_num = select; | |
222 | req.sec_count = 1; | |
223 | req.timeout = 1000; | |
224 | break; | |
225 | case IDENTIFY: | |
226 | req.flags = ATACMD_READ; | |
227 | req.command = WDCC_IDENTIFY; | |
228 | req.databuf = (caddr_t) inbuf; | |
229 | req.datalen = sizeof(inbuf); | |
230 | req.timeout = 1000; | |
231 | copydata = 1; | |
232 | break; | |
233 | case PIDENTIFY: | |
234 | req.flags = ATACMD_READ; | |
235 | req.command = ATAPI_IDENTIFY_DEVICE; | |
236 | req.databuf = (caddr_t) inbuf; | |
237 | req.datalen = sizeof(inbuf); | |
238 | req.timeout = 1000; | |
239 | copydata = 1; | |
240 | break; | |
241 | case ENABLE: | |
242 | req.flags = ATACMD_READ; | |
243 | req.features = ATA_SMART_ENABLE; | |
244 | req.command = ATAPI_SMART; | |
245 | req.cylinder = htole16(WDSMART_CYL); | |
246 | req.timeout = 1000; | |
247 | break; | |
248 | case DISABLE: | |
249 | req.flags = ATACMD_READ; | |
250 | req.features = ATA_SMART_DISABLE; | |
251 | req.command = ATAPI_SMART; | |
252 | req.cylinder = htole16(WDSMART_CYL); | |
253 | req.timeout = 1000; | |
254 | break; | |
255 | case AUTO_OFFLINE: | |
256 | /* NOTE: According to ATAPI 4 and UP, this command is obsolete */ | |
257 | req.flags = ATACMD_READ; | |
258 | req.features = ATA_SMART_AUTO_OFFLINE; /* XXX missing from wdcreg.h */ | |
259 | req.command = ATAPI_SMART; | |
260 | req.databuf = (caddr_t) inbuf; | |
261 | req.datalen = sizeof(inbuf); | |
262 | req.cylinder = htole16(WDSMART_CYL); | |
263 | req.sec_num = select; | |
264 | req.sec_count = 1; | |
265 | req.timeout = 1000; | |
266 | break; | |
267 | case AUTOSAVE: | |
268 | req.flags = ATACMD_READ; | |
269 | req.features = ATA_SMART_AUTOSAVE; /* XXX missing from wdcreg.h */ | |
270 | req.command = ATAPI_SMART; | |
271 | req.cylinder = htole16(WDSMART_CYL); | |
272 | req.sec_count = 0xf1; | |
273 | /* to enable autosave */ | |
274 | req.timeout = 1000; | |
275 | break; | |
276 | case IMMEDIATE_OFFLINE: | |
277 | /* NOTE: According to ATAPI 4 and UP, this command is obsolete */ | |
278 | req.flags = ATACMD_READ; | |
279 | req.features = ATA_SMART_IMMEDIATE_OFFLINE; /* XXX missing from wdcreg.h */ | |
280 | req.command = ATAPI_SMART; | |
281 | req.databuf = (caddr_t) inbuf; | |
282 | req.datalen = sizeof(inbuf); | |
283 | req.cylinder = htole16(WDSMART_CYL); | |
284 | req.sec_num = select; | |
285 | req.sec_count = 1; | |
286 | req.timeout = 1000; | |
287 | break; | |
288 | case STATUS_CHECK: | |
289 | /* same command, no HDIO in NetBSD */ | |
290 | case STATUS: | |
291 | req.flags = ATACMD_READ; | |
292 | req.features = ATA_SMART_STATUS; | |
293 | req.command = ATAPI_SMART; | |
294 | req.cylinder = htole16(WDSMART_CYL); | |
295 | req.timeout = 1000; | |
296 | break; | |
297 | case CHECK_POWER_MODE: | |
298 | req.flags = ATACMD_READREG; | |
299 | req.command = WDCC_CHECK_PWR; | |
300 | req.timeout = 1000; | |
301 | break; | |
302 | default: | |
303 | pout("Unrecognized command %d in ata_command_interface()\n", command); | |
304 | errno = ENOSYS; | |
305 | return -1; | |
306 | } | |
307 | ||
308 | if (command == STATUS_CHECK) { | |
309 | char buf[512]; | |
310 | ||
311 | unsigned const short normal = WDSMART_CYL, failed = 0x2cf4; | |
312 | ||
a86ec89e GI |
313 | retval = ioctl(fd, ATAIOCCOMMAND, &req); |
314 | if (retval < 0) { | |
832b75ed GG |
315 | perror("Failed command"); |
316 | return -1; | |
317 | } | |
318 | /* Cyl low and Cyl high unchanged means "Good SMART status" */ | |
319 | if (letoh16(req.cylinder) == normal) | |
320 | return 0; | |
321 | ||
322 | /* These values mean "Bad SMART status" */ | |
323 | if (letoh16(req.cylinder) == failed) | |
324 | return 1; | |
325 | ||
326 | /* We haven't gotten output that makes sense; | |
327 | * print out some debugging info */ | |
328 | snprintf(buf, sizeof(buf), | |
329 | "CMD=0x%02x\nFR =0x%02x\nNS =0x%02x\nSC =0x%02x\nCL =0x%02x\nCH =0x%02x\nRETURN =0x%04x\n", | |
330 | (int) req.command, (int) req.features, (int) req.sec_count, (int) req.sec_num, | |
331 | (int) (letoh16(req.cylinder) & 0xff), (int) ((letoh16(req.cylinder) >> 8) & 0xff), | |
332 | (int) req.error); | |
333 | printwarning(BAD_SMART, buf); | |
334 | return 0; | |
335 | } | |
336 | if ((retval = ioctl(fd, ATAIOCCOMMAND, &req))) { | |
337 | perror("Failed command"); | |
338 | return -1; | |
339 | } | |
340 | if (command == CHECK_POWER_MODE) | |
341 | data[0] = req.sec_count; | |
342 | ||
343 | if (copydata) | |
344 | memcpy(data, inbuf, 512); | |
345 | ||
346 | return 0; | |
347 | } | |
348 | ||
832b75ed GG |
349 | int |
350 | do_scsi_cmnd_io(int fd, struct scsi_cmnd_io * iop, int report) | |
351 | { | |
352 | struct scsireq sc; | |
353 | ||
354 | if (report > 0) { | |
355 | size_t k; | |
356 | ||
357 | const unsigned char *ucp = iop->cmnd; | |
358 | const char *np; | |
359 | ||
360 | np = scsi_get_opcode_name(ucp[0]); | |
361 | pout(" [%s: ", np ? np : "<unknown opcode>"); | |
362 | for (k = 0; k < iop->cmnd_len; ++k) | |
363 | pout("%02x ", ucp[k]); | |
364 | if ((report > 1) && | |
365 | (DXFER_TO_DEVICE == iop->dxfer_dir) && (iop->dxferp)) { | |
366 | int trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
367 | ||
368 | pout("]\n Outgoing data, len=%d%s:\n", (int) iop->dxfer_len, | |
369 | (trunc ? " [only first 256 bytes shown]" : "")); | |
370 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len), 1); | |
371 | } else | |
372 | pout("]"); | |
373 | } | |
374 | memset(&sc, 0, sizeof(sc)); | |
375 | memcpy(sc.cmd, iop->cmnd, iop->cmnd_len); | |
376 | sc.cmdlen = iop->cmnd_len; | |
a37e7145 | 377 | sc.databuf = (char *)iop->dxferp; |
832b75ed GG |
378 | sc.datalen = iop->dxfer_len; |
379 | sc.senselen = iop->max_sense_len; | |
380 | sc.timeout = iop->timeout == 0 ? 60000 : iop->timeout; /* XXX */ | |
381 | sc.flags = | |
a37e7145 | 382 | (iop->dxfer_dir == DXFER_NONE ? SCCMD_READ : |
832b75ed GG |
383 | (iop->dxfer_dir == DXFER_FROM_DEVICE ? SCCMD_READ : SCCMD_WRITE)); |
384 | ||
385 | if (ioctl(fd, SCIOCCOMMAND, &sc) < 0) { | |
386 | warn("error sending SCSI ccb"); | |
387 | return -1; | |
388 | } | |
389 | iop->resid = sc.datalen - sc.datalen_used; | |
390 | iop->scsi_status = sc.status; | |
391 | if (iop->sensep) { | |
392 | memcpy(iop->sensep, sc.sense, sc.senselen_used); | |
393 | iop->resp_sense_len = sc.senselen_used; | |
394 | } | |
395 | if (report > 0) { | |
396 | int trunc; | |
397 | ||
398 | pout(" status=0\n"); | |
399 | trunc = (iop->dxfer_len > 256) ? 1 : 0; | |
400 | ||
401 | pout(" Incoming data, len=%d%s:\n", (int) iop->dxfer_len, | |
402 | (trunc ? " [only first 256 bytes shown]" : "")); | |
403 | dStrHex(iop->dxferp, (trunc ? 256 : iop->dxfer_len), 1); | |
404 | } | |
405 | return 0; | |
406 | } | |
407 | ||
408 | /* print examples for smartctl */ | |
409 | void | |
410 | print_smartctl_examples() | |
411 | { | |
412 | char p; | |
413 | ||
414 | p = 'a' + getrawpartition(); | |
832b75ed | 415 | printf( |
ff28b140 | 416 | "=================================================== SMARTCTL EXAMPLES =====\n\n" |
832b75ed GG |
417 | " smartctl -a /dev/wd0%c (Prints all SMART information)\n\n" |
418 | " smartctl --smart=on --offlineauto=on --saveauto=on /dev/wd0%c\n" | |
419 | " (Enables SMART on first disk)\n\n" | |
420 | " smartctl -t long /dev/wd0%c (Executes extended disk self-test)\n\n" | |
421 | " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/wd0%c\n" | |
422 | " (Prints Self-Test & Attribute errors)\n", | |
423 | p, p, p, p | |
424 | ); | |
832b75ed GG |
425 | return; |
426 | } |