4 * Home page of code is: http://smartmontools.sourceforge.net
6 * Copyright (C) 2004-8 Geoffrey Keating <geoffk@geoffk.org>
7 * Copyright (C) 2014 Alex Samorukov <samm@os2.kiev.ua>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2, or (at your option)
14 * You should have received a copy of the GNU General Public License
15 * along with smartmontools. If not, see <http://www.gnu.org/licenses/>.
22 #include <mach/mach.h>
23 #include <mach/mach_error.h>
24 #include <mach/mach_init.h>
25 #include <IOKit/IOCFPlugIn.h>
26 #include <IOKit/IOKitLib.h>
27 #include <IOKit/IOReturn.h>
28 #include <IOKit/IOBSD.h>
29 #include <IOKit/storage/IOBlockStorageDevice.h>
30 #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
31 #include <IOKit/storage/IOMedia.h>
32 #include <IOKit/storage/ata/IOATAStorageDefines.h>
33 #include <IOKit/storage/ata/ATASMARTLib.h>
34 #include <CoreFoundation/CoreFoundation.h>
36 // No, I don't know why there isn't a header for this.
37 #define kIOATABlockStorageDeviceClass "IOATABlockStorageDevice"
44 #include "os_darwin.h"
45 #include "dev_interface.h"
47 // Needed by '-V' option (CVS versioning) of smartd/smartctl
48 const char *os_darwin_cpp_cvsid
="$Id: os_darwin.cpp 3982 2014-08-16 21:07:19Z samm2 $" \
49 ATACMDS_H_CVSID CONFIG_H_CVSID INT64_H_CVSID OS_DARWIN_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID
;
51 // examples for smartctl
52 static const char smartctl_examples
[] =
53 "=================================================== SMARTCTL EXAMPLES =====\n\n"
54 " smartctl -a disk0 (Prints all SMART information)\n\n"
55 " smartctl -t long /dev/disk0 (Executes extended disk self-test)\n\n"
56 #ifdef HAVE_GETOPT_LONG
57 " smartctl --smart=on --saveauto=on /dev/rdisk0 (Enables SMART on first disk)\n\n"
58 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/disk0\n"
59 " (Prints Self-Test & Attribute errors)\n\n"
61 " smartctl -s on -S on /dev/rdisk0 (Enables SMART on first disk)\n\n"
62 " smartctl -A -l selftest -q errorsonly /dev/disk0\n"
63 " (Prints Self-Test & Attribute errors)\n\n"
65 " smartctl -a IOService:/MacRISC2PE/pci@f4000000/AppleMacRiscPCI/ata-6@D/AppleKauaiATA/ATADeviceNub@0/IOATABlockStorageDriver/IOATABlockStorageDevice\n"
66 " (You can use IOService: ...)\n\n"
67 " smartctl -c IODeviceTree:/pci@f4000000/ata-6@D/@0:0\n"
68 " (... Or IODeviceTree:)\n"
72 // Information that we keep about each device.
76 IOCFPlugInInterface
**plugin
;
77 IOATASMARTInterface
**smartIf
;
80 const char * dev_darwin_cpp_cvsid
= "$Id: os_darwin.cpp 3982 2014-08-16 21:07:19Z samm2 $"
81 DEV_INTERFACE_H_CVSID
;
83 /////////////////////////////////////////////////////////////////////////////
85 namespace os
{ // No need to publish anything, name provided for Doxygen
87 /////////////////////////////////////////////////////////////////////////////
88 /// Implement shared open/close routines with old functions.
90 class darwin_smart_device
91 : virtual public /*implements*/ smart_device
94 explicit darwin_smart_device(const char * mode
)
95 : smart_device(never_called
),
96 m_fd(-1), m_mode(mode
) { }
98 virtual ~darwin_smart_device() throw();
100 virtual bool is_open() const;
104 virtual bool close();
107 /// Return filedesc for derived classes.
113 int m_fd
; ///< filedesc, -1 if not open.
114 const char * m_mode
; ///< Mode string for deviceopen().
118 darwin_smart_device::~darwin_smart_device() throw()
121 darwin_smart_device::close();
124 bool darwin_smart_device::is_open() const
129 // Determine whether 'dev' is a SMART-capable device.
130 static bool is_smart_capable (io_object_t dev
) {
131 CFTypeRef smartCapableKey
;
132 CFDictionaryRef diskChars
;
134 // If the device has kIOPropertySMARTCapableKey, then it's capable,
135 // no matter what it looks like.
136 smartCapableKey
= IORegistryEntryCreateCFProperty
137 (dev
, CFSTR (kIOPropertySMARTCapableKey
),
138 kCFAllocatorDefault
, 0);
141 CFRelease (smartCapableKey
);
145 // If it's an kIOATABlockStorageDeviceClass then we're successful
146 // only if its ATA features indicate it supports SMART.
147 if (IOObjectConformsTo (dev
, kIOATABlockStorageDeviceClass
)
148 && (diskChars
= (CFDictionaryRef
)IORegistryEntryCreateCFProperty
149 (dev
, CFSTR (kIOPropertyDeviceCharacteristicsKey
),
150 kCFAllocatorDefault
, kNilOptions
)) != NULL
)
152 CFNumberRef diskFeatures
= NULL
;
153 UInt32 ataFeatures
= 0;
155 if (CFDictionaryGetValueIfPresent (diskChars
, CFSTR ("ATA Features"),
156 (const void **)&diskFeatures
))
157 CFNumberGetValue (diskFeatures
, kCFNumberLongType
,
159 CFRelease (diskChars
);
161 CFRelease (diskFeatures
);
163 return (ataFeatures
& kIOATAFeatureSMART
) != 0;
168 bool darwin_smart_device::open()
170 // Acceptable device names are:
179 const char *pathname
= get_dev_name();
180 char *type
= const_cast<char*>(m_mode
);
182 if (strcmp (type
, "ATA") != 0)
188 // Find a free device number.
189 for (devnum
= 0; devnum
< sizeof (devices
) / sizeof (devices
[0]); devnum
++)
190 if (! devices
[devnum
].ioob
)
192 if (devnum
== sizeof (devices
) / sizeof (devices
[0]))
199 if (strncmp (pathname
, "/dev/rdisk", 10) == 0)
200 devname
= pathname
+ 6;
201 else if (strncmp (pathname
, "/dev/disk", 9) == 0)
202 devname
= pathname
+ 5;
203 else if (strncmp (pathname
, "disk", 4) == 0)
204 // allow user to just say 'disk0'
210 CFMutableDictionaryRef matcher
;
211 matcher
= IOBSDNameMatching (kIOMasterPortDefault
, 0, devname
);
212 disk
= IOServiceGetMatchingService (kIOMasterPortDefault
, matcher
);
216 disk
= IORegistryEntryFromPath (kIOMasterPortDefault
, pathname
);
225 // Find a SMART-capable driver which is a parent of this device.
226 while (! is_smart_capable (disk
))
229 io_object_t prevdisk
= disk
;
231 // Find this device's parent and try again.
232 err
= IORegistryEntryGetParentEntry (disk
, kIOServicePlane
, &disk
);
233 if (err
!= kIOReturnSuccess
|| ! disk
)
236 IOObjectRelease (prevdisk
);
241 devices
[devnum
].ioob
= disk
;
246 devices
[devnum
].plugin
= NULL
;
247 devices
[devnum
].smartIf
= NULL
;
249 // Create an interface to the ATA SMART library.
250 if (IOCreatePlugInInterfaceForService (disk
,
251 kIOATASMARTUserClientTypeID
,
252 kIOCFPlugInInterfaceID
,
253 &devices
[devnum
].plugin
,
254 &dummy
) == kIOReturnSuccess
)
255 (*devices
[devnum
].plugin
)->QueryInterface
256 (devices
[devnum
].plugin
,
257 CFUUIDGetUUIDBytes ( kIOATASMARTInterfaceID
),
258 (void **)&devices
[devnum
].smartIf
);
264 set_err((errno
==ENOENT
|| errno
==ENOTDIR
) ? ENODEV
: errno
);
270 bool darwin_smart_device::close()
272 int fd
= m_fd
; m_fd
= -1;
273 if (devices
[fd
].smartIf
)
274 (*devices
[fd
].smartIf
)->Release (devices
[fd
].smartIf
);
275 if (devices
[fd
].plugin
)
276 IODestroyPlugInInterface (devices
[fd
].plugin
);
277 IOObjectRelease (devices
[fd
].ioob
);
278 devices
[fd
].ioob
= MACH_PORT_NULL
;
282 // makes a list of ATA or SCSI devices for the DEVICESCAN directive of
283 // smartd. Returns number N of devices, or -1 if out of
284 // memory. Allocates N+1 arrays: one of N pointers (devlist); the
285 // other N arrays each contain null-terminated character strings. In
286 // the case N==0, no arrays are allocated because the array of 0
287 // pointers has zero length, equivalent to calling malloc(0).
288 static int make_device_names (char*** devlist
, const char* name
) {
291 io_object_t device
= MACH_PORT_NULL
;
295 // We treat all devices as ATA so long as they support SMARTLib.
296 if (strcmp (name
, "ATA") != 0)
299 err
= IOServiceGetMatchingServices
300 (kIOMasterPortDefault
, IOServiceMatching (kIOBlockStorageDeviceClass
), &i
);
301 if (err
!= kIOReturnSuccess
)
304 // Count the devices.
306 while ((device
= IOIteratorNext (i
)) != MACH_PORT_NULL
) {
307 if (is_smart_capable (device
))
309 IOObjectRelease (device
);
312 // Create an array of service names.
314 *devlist
= (char**)calloc (result
, sizeof (char *));
318 while ((device
= IOIteratorNext (i
)) != MACH_PORT_NULL
) {
319 if (is_smart_capable (device
))
322 IORegistryEntryGetPath(device
, kIOServicePlane
, devName
);
323 (*devlist
)[index
] = strdup (devName
);
324 if (! (*devlist
)[index
])
328 IOObjectRelease (device
);
335 if (device
!= MACH_PORT_NULL
)
336 IOObjectRelease (device
);
340 for (index
= 0; index
< result
; index
++)
341 if ((*devlist
)[index
])
342 free ((*devlist
)[index
]);
348 /////////////////////////////////////////////////////////////////////////////
349 /// Implement standard ATA support
351 class darwin_ata_device
352 : public /*implements*/ ata_device
,
353 public /*extends*/ darwin_smart_device
356 darwin_ata_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
);
357 virtual bool ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
);
360 // virtual int ata_command_interface(smart_command_set command, int select, char * data);
363 darwin_ata_device::darwin_ata_device(smart_interface
* intf
, const char * dev_name
, const char * req_type
)
364 : smart_device(intf
, dev_name
, "ata", req_type
),
365 darwin_smart_device("ATA")
369 bool darwin_ata_device::ata_pass_through(const ata_cmd_in
& in
, ata_cmd_out
& out
)
371 if (!ata_cmd_is_ok(in
,
372 true, // data_out_support
373 true, // multi_sector_support
374 false) // not supported by API
379 char * data
= (char *)in
.buffer
;
381 IOATASMARTInterface
**ifp
= devices
[fd
].smartIf
;
382 IOATASMARTInterface
*smartIf
;
384 int timeoutCount
= 5;
390 clear_err(); errno
= 0;
392 switch (in
.in_regs
.command
) {
393 case ATA_IDENTIFY_DEVICE
:
396 err
= smartIf
->GetATAIdentifyData (ifp
, data
, 512, &dummy
);
397 if (err
!= kIOReturnSuccess
&& err
!= kIOReturnTimeout
398 && err
!= kIOReturnNotResponding
)
399 printf ("identify failed: %#x\n", (unsigned) rc
);
400 if (err
== kIOReturnSuccess
&& isbigendian())
403 /* The system has already byte-swapped, undo it. */
404 for (i
= 0; i
< 256; i
+=2)
409 case ATA_IDENTIFY_PACKET_DEVICE
:
410 case ATA_CHECK_POWER_MODE
:
415 switch (in
.in_regs
.features
) {
416 case ATA_SMART_READ_VALUES
:
417 err
= smartIf
->SMARTReadData (ifp
, (ATASMARTData
*)data
);
419 case ATA_SMART_READ_THRESHOLDS
:
420 err
= smartIf
->SMARTReadDataThresholds (ifp
,
421 (ATASMARTDataThresholds
*)data
);
423 case ATA_SMART_READ_LOG_SECTOR
:
424 err
= smartIf
->SMARTReadLogAtAddress (ifp
, in
.in_regs
.lba_low
, data
, 512 * in
.in_regs
.sector_count
);
426 case ATA_SMART_WRITE_LOG_SECTOR
:
427 err
= smartIf
->SMARTWriteLogAtAddress (ifp
, in
.in_regs
.lba_low
, data
, 512 * in
.in_regs
.sector_count
);
429 case ATA_SMART_ENABLE
:
430 case ATA_SMART_DISABLE
:
431 err
= smartIf
->SMARTEnableDisableOperations (ifp
, in
.in_regs
.features
== ATA_SMART_ENABLE
);
433 case ATA_SMART_STATUS
:
434 if (in
.out_needed
.lba_high
) // statuscheck
437 err
= smartIf
->SMARTReturnStatus (ifp
, &is_failing
);
438 if (err
== kIOReturnSuccess
&& is_failing
) {
439 err
= -1; // thresholds exceeded condition
440 out
.out_regs
.lba_high
= 0x2c; out
.out_regs
.lba_mid
= 0xf4;
443 out
.out_regs
.lba_high
= 0xc2; out
.out_regs
.lba_mid
= 0x4f;
448 case ATA_SMART_AUTOSAVE
:
449 err
= smartIf
->SMARTEnableDisableAutosave (ifp
,
450 (in
.in_regs
.sector_count
== 241 ? true : false));
452 case ATA_SMART_IMMEDIATE_OFFLINE
:
453 select
= in
.in_regs
.lba_low
;
454 if (select
!= SHORT_SELF_TEST
&& select
!= EXTEND_SELF_TEST
)
459 err
= smartIf
->SMARTExecuteOffLineImmediate (ifp
,
460 select
== EXTEND_SELF_TEST
);
462 case ATA_SMART_AUTO_OFFLINE
:
463 return set_err(ENOSYS
, "SMART command not supported");
465 return set_err(ENOSYS
, "Unknown SMART command");
469 return set_err(ENOSYS
, "Non-SMART commands not implemented");
471 } while ((err
== kIOReturnTimeout
|| err
== kIOReturnNotResponding
)
472 && timeoutCount
-- > 0);
473 if (err
== kIOReturnExclusiveAccess
)
475 rc
= err
== kIOReturnSuccess
? 0 : -1;
484 /////////////////////////////////////////////////////////////////////////////
485 /// Implement platform interface
487 class darwin_smart_interface
488 : public /*implements*/ smart_interface
491 virtual std::string
get_app_examples(const char * appname
);
493 virtual bool scan_smart_devices(smart_device_list
& devlist
, const char * type
,
494 const char * pattern
= 0);
497 virtual ata_device
* get_ata_device(const char * name
, const char * type
);
499 virtual scsi_device
* get_scsi_device(const char * name
, const char * type
);
501 virtual smart_device
* autodetect_smart_device(const char * name
);
506 //////////////////////////////////////////////////////////////////////
508 std::string
darwin_smart_interface::get_app_examples(const char * appname
)
510 if (!strcmp(appname
, "smartctl"))
511 return smartctl_examples
;
512 return ""; // ... so don't print again.
515 ata_device
* darwin_smart_interface::get_ata_device(const char * name
, const char * type
)
517 return new darwin_ata_device(this, name
, type
);
520 scsi_device
* darwin_smart_interface::get_scsi_device(const char *, const char *)
522 return 0; // scsi devices are not supported [yet]
526 smart_device
* darwin_smart_interface::autodetect_smart_device(const char * name
)
528 return new darwin_ata_device(this, name
, "");
531 static void free_devnames(char * * devnames
, int numdevs
)
533 for (int i
= 0; i
< numdevs
; i
++)
538 bool darwin_smart_interface::scan_smart_devices(smart_device_list
& devlist
,
539 const char * type
, const char * pattern
/*= 0*/)
542 set_err(EINVAL
, "DEVICESCAN with pattern not implemented yet");
547 char * * atanames
= 0; int numata
= 0;
548 if (!type
|| !strcmp(type
, "ata")) {
549 numata
= make_device_names(&atanames
, "ATA");
560 for (i
= 0; i
< numata
; i
++) {
561 ata_device
* atadev
= get_ata_device(atanames
[i
], type
);
563 devlist
.push_back(atadev
);
565 free_devnames(atanames
, numata
);
572 /////////////////////////////////////////////////////////////////////////////
573 /// Initialize platform interface and register with smi()
575 void smart_interface::init()
577 static os::darwin_smart_interface the_interface
;
578 smart_interface::set(&the_interface
);