]> git.proxmox.com Git - mirror_smartmontools-debian.git/blob - os_darwin.cpp
import smartmontools 7.0
[mirror_smartmontools-debian.git] / os_darwin.cpp
1 /*
2 * os_darwin.cpp
3 *
4 * Home page of code is: http://www.smartmontools.org
5 *
6 * Copyright (C) 2004-8 Geoffrey Keating <geoffk@geoffk.org>
7 * Copyright (C) 2014 Alex Samorukov <samm@os2.kiev.ua>
8 *
9 * SPDX-License-Identifier: GPL-2.0-or-later
10 */
11
12 #include <stdbool.h>
13 #include <errno.h>
14 #include <unistd.h>
15 #include <mach/mach.h>
16 #include <mach/mach_error.h>
17 #include <mach/mach_init.h>
18 #include <sys/utsname.h>
19 #include <IOKit/IOCFPlugIn.h>
20 #include <IOKit/IOKitLib.h>
21 #include <IOKit/IOReturn.h>
22 #include <IOKit/IOBSD.h>
23 #include <IOKit/storage/IOBlockStorageDevice.h>
24 #include <IOKit/storage/IOStorageDeviceCharacteristics.h>
25 #include <IOKit/storage/IOMedia.h>
26 #include <IOKit/storage/ata/IOATAStorageDefines.h>
27 #include <IOKit/storage/ata/ATASMARTLib.h>
28 #include <CoreFoundation/CoreFoundation.h>
29
30 #include "config.h"
31
32 #include "atacmds.h"
33 #include "scsicmds.h"
34 #include "nvmecmds.h"
35 #include "utility.h"
36 #include "os_darwin.h"
37 #include "dev_interface.h"
38
39 #define ARGUSED(x) ((void)(x))
40 // Needed by '-V' option (CVS versioning) of smartd/smartctl
41 const char *os_darwin_cpp_cvsid="$Id: os_darwin.cpp 4831 2018-11-09 07:18:23Z samm2 $" \
42 ATACMDS_H_CVSID CONFIG_H_CVSID OS_DARWIN_H_CVSID SCSICMDS_H_CVSID UTILITY_H_CVSID;
43
44 // examples for smartctl
45 static const char smartctl_examples[] =
46 "=================================================== SMARTCTL EXAMPLES =====\n\n"
47 " smartctl -a disk0 (Prints all SMART information)\n\n"
48 " smartctl -t long /dev/disk0 (Executes extended disk self-test)\n\n"
49 " smartctl --smart=on --saveauto=on /dev/rdisk0 (Enables SMART on first disk)\n\n"
50 " smartctl --attributes --log=selftest --quietmode=errorsonly /dev/disk0\n"
51 " (Prints Self-Test & Attribute errors)\n\n"
52 " smartctl -a IOService:/MacRISC2PE/pci@f4000000/AppleMacRiscPCI/ata-6@D/AppleKauaiATA/ATADeviceNub@0/IOATABlockStorageDriver/IOATABlockStorageDevice\n"
53 " (You can use IOService: ...)\n\n"
54 " smartctl -c IODeviceTree:/pci@f4000000/ata-6@D/@0:0\n"
55 " (... Or IODeviceTree:)\n"
56 ;
57
58
59 // Information that we keep about each device.
60
61 static struct {
62 io_object_t ioob;
63 IOCFPlugInInterface **plugin;
64 IOATASMARTInterface **smartIf; // ATA devices
65 IONVMeSMARTInterface **smartIfNVMe;
66 } devices[20];
67
68 const char * dev_darwin_cpp_cvsid = "$Id: os_darwin.cpp 4831 2018-11-09 07:18:23Z samm2 $"
69 DEV_INTERFACE_H_CVSID;
70
71 /////////////////////////////////////////////////////////////////////////////
72
73 namespace os { // No need to publish anything, name provided for Doxygen
74
75 /////////////////////////////////////////////////////////////////////////////
76 /// Implement shared open/close routines with old functions.
77
78 class darwin_smart_device
79 : virtual public /*implements*/ smart_device
80 {
81 public:
82 explicit darwin_smart_device(const char * mode)
83 : smart_device(never_called),
84 m_fd(-1), m_mode(mode) { }
85
86 virtual ~darwin_smart_device() throw();
87
88 virtual bool is_open() const;
89
90 virtual bool open();
91
92 virtual bool close();
93
94 protected:
95 /// Return filedesc for derived classes.
96 int get_fd() const
97 { return m_fd; }
98
99 private:
100 int m_fd; ///< filedesc, -1 if not open.
101 const char * m_mode; ///< Mode string for deviceopen().
102 };
103
104
105 darwin_smart_device::~darwin_smart_device() throw()
106 {
107 if (m_fd >= 0)
108 darwin_smart_device::close();
109 }
110
111 bool darwin_smart_device::is_open() const
112 {
113 return (m_fd >= 0);
114 }
115
116 // Determine whether 'dev' is a SMART-capable device.
117 static bool is_smart_capable (io_object_t dev, const char * type) {
118 CFTypeRef smartCapableKey = NULL;
119 CFDictionaryRef diskChars;
120
121 // If the device has kIOPropertySMARTCapableKey, then it's capable,
122 // no matter what it looks like.
123 if (!strcmp("ATA", type)) {
124 smartCapableKey = IORegistryEntryCreateCFProperty
125 (dev, CFSTR (kIOPropertySMARTCapableKey),
126 kCFAllocatorDefault, 0);
127 }
128
129 else if (!strcmp("NVME", type)) {
130 smartCapableKey = IORegistryEntryCreateCFProperty
131 (dev, CFSTR (kIOPropertyNVMeSMARTCapableKey),
132 kCFAllocatorDefault, 0);
133 }
134
135 if (smartCapableKey)
136 {
137 CFRelease (smartCapableKey);
138 return true;
139 }
140
141 // If it's an kIOATABlockStorageDeviceClass then we're successful
142 // only if its ATA features indicate it supports SMART.
143 // This will be broken for NVMe, however it is not needed
144 if (IOObjectConformsTo (dev, kIOATABlockStorageDeviceClass)
145 && (diskChars = (CFDictionaryRef)IORegistryEntryCreateCFProperty
146 (dev, CFSTR (kIOPropertyDeviceCharacteristicsKey),
147 kCFAllocatorDefault, kNilOptions)) != NULL)
148 {
149 CFNumberRef diskFeatures = NULL;
150 UInt32 ataFeatures = 0;
151
152 if (CFDictionaryGetValueIfPresent (diskChars, CFSTR ("ATA Features"),
153 (const void **)&diskFeatures))
154 CFNumberGetValue (diskFeatures, kCFNumberLongType,
155 &ataFeatures);
156 CFRelease (diskChars);
157 if (diskFeatures)
158 CFRelease (diskFeatures);
159
160 return (ataFeatures & kIOATAFeatureSMART) != 0;
161 }
162 return false;
163 }
164
165 bool darwin_smart_device::open()
166 {
167 // Acceptable device names are:
168 // /dev/disk*
169 // /dev/rdisk*
170 // disk*
171 // IOService:*
172 // IODeviceTree:*
173 size_t devnum;
174 const char *devname;
175 io_object_t disk;
176 const char *pathname = get_dev_name();
177 char *type = const_cast<char*>(m_mode);
178
179 if (!(strcmp("ATA", type) || strcmp("NVME", type)))
180 {
181 set_err (EINVAL);
182 return false;
183 }
184
185 // Find a free device number.
186 for (devnum = 0; devnum < sizeof (devices) / sizeof (devices[0]); devnum++)
187 if (! devices[devnum].ioob)
188 break;
189 if (devnum == sizeof (devices) / sizeof (devices[0]))
190 {
191 set_err (EMFILE);
192 return false;
193 }
194
195 devname = NULL;
196 if (strncmp (pathname, "/dev/rdisk", 10) == 0)
197 devname = pathname + 6;
198 else if (strncmp (pathname, "/dev/disk", 9) == 0)
199 devname = pathname + 5;
200 else if (strncmp (pathname, "disk", 4) == 0)
201 // allow user to just say 'disk0'
202 devname = pathname;
203
204 // Find the device. This part should be the same for the NVMe and ATA
205 if (devname)
206 {
207 CFMutableDictionaryRef matcher;
208 matcher = IOBSDNameMatching (kIOMasterPortDefault, 0, devname);
209 disk = IOServiceGetMatchingService (kIOMasterPortDefault, matcher);
210 }
211 else
212 {
213 disk = IORegistryEntryFromPath (kIOMasterPortDefault, pathname);
214 }
215 if (! disk)
216 {
217 set_err(ENOENT);
218 return false;
219 }
220 // Find a SMART-capable driver which is a parent of this device.
221 while (! is_smart_capable (disk, type))
222 {
223 IOReturn err;
224 io_object_t prevdisk = disk;
225
226 // Find this device's parent and try again.
227 err = IORegistryEntryGetParentEntry (disk, kIOServicePlane, &disk);
228 if (err != kIOReturnSuccess || ! disk)
229 {
230 set_err(ENODEV);
231 IOObjectRelease (prevdisk);
232 return false;
233 }
234 }
235
236 devices[devnum].ioob = disk;
237
238 {
239 SInt32 dummy;
240
241 devices[devnum].plugin = NULL;
242 devices[devnum].smartIf = NULL;
243 devices[devnum].smartIfNVMe = NULL;
244
245 CFUUIDRef pluginType = NULL;
246 CFUUIDRef smartInterfaceId = NULL;
247 void ** SMARTptr = NULL;
248
249 if (!strcmp("ATA", type)) {
250 pluginType = kIOATASMARTUserClientTypeID;
251 smartInterfaceId = kIOATASMARTInterfaceID;
252 SMARTptr = (void **)&devices[devnum].smartIf;
253 }
254 else if (!strcmp("NVME", type)) {
255 pluginType = kIONVMeSMARTUserClientTypeID;
256 smartInterfaceId = kIONVMeSMARTInterfaceID;
257 SMARTptr = (void **)&devices[devnum].smartIfNVMe;
258 }
259
260 // Create an interface to the ATA SMART library.
261 if (IOCreatePlugInInterfaceForService (disk,
262 pluginType,
263 kIOCFPlugInInterfaceID,
264 &devices[devnum].plugin,
265 &dummy) == kIOReturnSuccess)
266 (*devices[devnum].plugin)->QueryInterface
267 (devices[devnum].plugin,
268 CFUUIDGetUUIDBytes ( smartInterfaceId),
269 SMARTptr);
270 else
271 return set_err(ENOSYS, "IOCreatePlugInInterfaceForService failed");
272 }
273
274
275 m_fd = devnum;
276 if (m_fd < 0) {
277 set_err((errno==ENOENT || errno==ENOTDIR) ? ENODEV : errno);
278 return false;
279 }
280 return true;
281 }
282
283 bool darwin_smart_device::close()
284 {
285 int fd = m_fd; m_fd = -1;
286 if (devices[fd].smartIf)
287 (*devices[fd].smartIf)->Release (devices[fd].smartIf);
288 if (devices[fd].smartIfNVMe)
289 (*devices[fd].smartIfNVMe)->Release (devices[fd].smartIfNVMe);
290 if (devices[fd].plugin)
291 IODestroyPlugInInterface (devices[fd].plugin);
292 IOObjectRelease (devices[fd].ioob);
293 devices[fd].ioob = MACH_PORT_NULL;
294 return true;
295 }
296
297 // makes a list of ATA or SCSI devices for the DEVICESCAN directive of
298 // smartd. Returns number N of devices, or -1 if out of
299 // memory. Allocates N+1 arrays: one of N pointers (devlist); the
300 // other N arrays each contain null-terminated character strings. In
301 // the case N==0, no arrays are allocated because the array of 0
302 // pointers has zero length, equivalent to calling malloc(0).
303 static int make_device_names (char*** devlist, const char* name) {
304 IOReturn err;
305 io_iterator_t i;
306 io_object_t device = MACH_PORT_NULL;
307 int result;
308 int index;
309
310 if (!(strcmp("ATA", name) || strcmp("NVME", name))) {
311 return 0;
312 }
313
314 err = IOServiceGetMatchingServices
315 (kIOMasterPortDefault, IOServiceMatching (kIOBlockStorageDeviceClass), &i);
316 if (err != kIOReturnSuccess)
317 return -1;
318
319 // Count the devices.
320 result = 0;
321 while ((device = IOIteratorNext (i)) != MACH_PORT_NULL) {
322 if (is_smart_capable (device, name))
323 result++;
324 IOObjectRelease (device);
325 }
326
327 // Create an array of service names.
328 IOIteratorReset (i);
329 if (! result)
330 goto error;
331 *devlist = (char**)calloc (result, sizeof (char *));
332 index = 0;
333 while ((device = IOIteratorNext (i)) != MACH_PORT_NULL) {
334 if (is_smart_capable (device, name))
335 {
336 io_string_t devName;
337 IORegistryEntryGetPath(device, kIOServicePlane, devName);
338 (*devlist)[index] = strdup (devName);
339 if (! (*devlist)[index])
340 goto error;
341 index++;
342 }
343 IOObjectRelease (device);
344 }
345
346 IOObjectRelease (i);
347 return result;
348
349 error:
350 if (device != MACH_PORT_NULL)
351 IOObjectRelease (device);
352 IOObjectRelease (i);
353 if (*devlist)
354 {
355 for (index = 0; index < result; index++)
356 if ((*devlist)[index])
357 free ((*devlist)[index]);
358 free (*devlist);
359 }
360 if(!result) // no devs found
361 return 0;
362
363 return -1;
364 }
365
366 /////////////////////////////////////////////////////////////////////////////
367 /// Implement standard ATA support
368
369 class darwin_ata_device
370 : public /*implements*/ ata_device,
371 public /*extends*/ darwin_smart_device
372 {
373 public:
374 darwin_ata_device(smart_interface * intf, const char * dev_name, const char * req_type);
375 virtual bool ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out);
376
377 protected:
378 // virtual int ata_command_interface(smart_command_set command, int select, char * data);
379 };
380
381 darwin_ata_device::darwin_ata_device(smart_interface * intf, const char * dev_name, const char * req_type)
382 : smart_device(intf, dev_name, "ata", req_type),
383 darwin_smart_device("ATA")
384 {
385 }
386
387 bool darwin_ata_device::ata_pass_through(const ata_cmd_in & in, ata_cmd_out & out)
388 {
389 if (!ata_cmd_is_ok(in,
390 true, // data_out_support
391 true, // multi_sector_support
392 false) // not supported by API
393 )
394 return false;
395
396 int select = 0;
397 char * data = (char *)in.buffer;
398 int fd = get_fd();
399 IOATASMARTInterface **ifp = devices[fd].smartIf;
400 IOATASMARTInterface *smartIf;
401 IOReturn err;
402 int timeoutCount = 5;
403 int rc = 0;
404
405 if (! ifp)
406 return false;
407 smartIf = *ifp;
408 clear_err(); errno = 0;
409 do {
410 switch (in.in_regs.command) {
411 case ATA_IDENTIFY_DEVICE:
412 {
413 UInt32 dummy;
414 err = smartIf->GetATAIdentifyData (ifp, data, 512, &dummy);
415 if (err != kIOReturnSuccess && err != kIOReturnTimeout
416 && err != kIOReturnNotResponding)
417 printf ("identify failed: %#x\n", (unsigned) rc);
418 if (err == kIOReturnSuccess && isbigendian())
419 {
420 int i;
421 /* The system has already byte-swapped, undo it. */
422 for (i = 0; i < 256; i+=2)
423 swap2 (data + i);
424 }
425 }
426 break;
427 case ATA_IDENTIFY_PACKET_DEVICE:
428 case ATA_CHECK_POWER_MODE:
429 errno = ENOTSUP;
430 err = -1;
431 break;
432 case ATA_SMART_CMD:
433 switch (in.in_regs.features) {
434 case ATA_SMART_READ_VALUES:
435 err = smartIf->SMARTReadData (ifp, (ATASMARTData *)data);
436 break;
437 case ATA_SMART_READ_THRESHOLDS:
438 err = smartIf->SMARTReadDataThresholds (ifp,
439 (ATASMARTDataThresholds *)data);
440 break;
441 case ATA_SMART_READ_LOG_SECTOR:
442 err = smartIf->SMARTReadLogAtAddress (ifp, in.in_regs.lba_low, data, 512 * in.in_regs.sector_count);
443 break;
444 case ATA_SMART_WRITE_LOG_SECTOR:
445 err = smartIf->SMARTWriteLogAtAddress (ifp, in.in_regs.lba_low, data, 512 * in.in_regs.sector_count);
446 break;
447 case ATA_SMART_ENABLE:
448 case ATA_SMART_DISABLE:
449 err = smartIf->SMARTEnableDisableOperations (ifp, in.in_regs.features == ATA_SMART_ENABLE);
450 break;
451 case ATA_SMART_STATUS:
452 if (in.out_needed.lba_high) // statuscheck
453 {
454 Boolean is_failing;
455 err = smartIf->SMARTReturnStatus (ifp, &is_failing);
456 if (err == kIOReturnSuccess && is_failing) {
457 err = -1; // thresholds exceeded condition
458 out.out_regs.lba_high = 0x2c; out.out_regs.lba_mid = 0xf4;
459 }
460 else {
461 out.out_regs.lba_high = 0xc2; out.out_regs.lba_mid = 0x4f;
462 }
463 break;
464 }
465 else err = 0;
466 break;
467 case ATA_SMART_AUTOSAVE:
468 err = smartIf->SMARTEnableDisableAutosave (ifp,
469 (in.in_regs.sector_count == 241 ? true : false));
470 break;
471 case ATA_SMART_IMMEDIATE_OFFLINE:
472 select = in.in_regs.lba_low;
473 if (select != SHORT_SELF_TEST && select != EXTEND_SELF_TEST)
474 {
475 errno = EINVAL;
476 return set_err(ENOSYS, "Unsupported SMART self-test mode");
477 }
478 err = smartIf->SMARTExecuteOffLineImmediate (ifp,
479 select == EXTEND_SELF_TEST);
480 break;
481 case ATA_SMART_AUTO_OFFLINE:
482 return set_err(ENOSYS, "SMART command not supported");
483 default:
484 return set_err(ENOSYS, "Unknown SMART command");
485 }
486 break;
487 default:
488 return set_err(ENOSYS, "Non-SMART commands not implemented");
489 }
490 } while ((err == kIOReturnTimeout || err == kIOReturnNotResponding)
491 && timeoutCount-- > 0);
492 if (err == kIOReturnExclusiveAccess)
493 errno = EBUSY;
494 rc = err == kIOReturnSuccess ? 0 : -1;
495 if (rc < 0) {
496 if (!get_errno())
497 set_err(errno);
498 return false;
499 }
500 return true;
501 }
502
503 /////////////////////////////////////////////////////////////////////////////
504 /// Implement platform interface
505
506 class darwin_smart_interface
507 : public /*implements*/ smart_interface
508 {
509 public:
510 virtual std::string get_os_version_str();
511
512 virtual std::string get_app_examples(const char * appname);
513
514 virtual bool scan_smart_devices(smart_device_list & devlist, const char * type,
515 const char * pattern = 0);
516
517 protected:
518 virtual ata_device * get_ata_device(const char * name, const char * type);
519
520 virtual scsi_device * get_scsi_device(const char * name, const char * type);
521
522 virtual nvme_device * get_nvme_device(const char * name, const char * type,
523 unsigned nsid);
524
525 virtual smart_device * autodetect_smart_device(const char * name);
526
527 };
528
529 /////////////////////////////////////////////////////////////////////////////
530 /// NVMe support
531
532 class darwin_nvme_device
533 : public /*implements*/ nvme_device,
534 public /*extends*/ darwin_smart_device
535 {
536 public:
537 darwin_nvme_device(smart_interface * intf, const char * dev_name,
538 const char * req_type, unsigned nsid);
539
540 virtual bool nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out);
541 };
542
543 darwin_nvme_device::darwin_nvme_device(smart_interface * intf, const char * dev_name,
544 const char * req_type, unsigned nsid)
545 : smart_device(intf, dev_name, "nvme", req_type),
546 nvme_device(nsid),
547 darwin_smart_device("NVME")
548 {
549 }
550
551 bool darwin_nvme_device::nvme_pass_through(const nvme_cmd_in & in, nvme_cmd_out & out)
552 {
553 ARGUSED(out);
554 int fd = get_fd();
555 IONVMeSMARTInterface **ifp = devices[fd].smartIfNVMe;
556 IONVMeSMARTInterface *smartIfNVMe ;
557 IOReturn err = 0;
558 unsigned int page = in.cdw10 & 0xff;
559
560 if (! ifp)
561 return false;
562 smartIfNVMe = *ifp;
563 // currently only GetIdentifyData and SMARTReadData are supported
564 switch (in.opcode) {
565 case smartmontools::nvme_admin_identify:
566 err = smartIfNVMe->GetIdentifyData(ifp, (struct nvme_id_ctrl *) in.buffer, in.nsid); // FIXME
567 break;
568 case smartmontools::nvme_admin_get_log_page:
569 if(page == 0x02)
570 err = smartIfNVMe->SMARTReadData(ifp, (struct nvme_smart_log *) in.buffer);
571 else /* GetLogPage() is not working yet */
572 return set_err(ENOSYS, "NVMe admin command:0x%02x/page:0x%02x is not supported",
573 in.opcode, page);
574 break;
575 default:
576 return set_err(ENOSYS, "NVMe admin command 0x%02x is not supported", in.opcode);
577 }
578 return true;
579 }
580 //////////////////////////////////////////////////////////////////////
581
582 std::string darwin_smart_interface::get_os_version_str()
583 {
584 // now we are just getting darwin runtime version, to get OSX version more things needs to be done, see
585 // http://stackoverflow.com/questions/11072804/how-do-i-determine-the-os-version-at-runtime-in-os-x-or-ios-without-using-gesta
586 struct utsname osname;
587 uname(&osname);
588 return strprintf("%s %s %s", osname.sysname, osname.release, osname.machine);
589 }
590
591 std::string darwin_smart_interface::get_app_examples(const char * appname)
592 {
593 if (!strcmp(appname, "smartctl"))
594 return smartctl_examples;
595 return ""; // ... so don't print again.
596 }
597
598 ata_device * darwin_smart_interface::get_ata_device(const char * name, const char * type)
599 {
600 return new darwin_ata_device(this, name, type);
601 }
602
603 scsi_device * darwin_smart_interface::get_scsi_device(const char *, const char *)
604 {
605 return 0; // scsi devices are not supported [yet]
606 }
607
608 nvme_device * darwin_smart_interface::get_nvme_device(const char * name, const char * type,
609 unsigned nsid)
610 {
611 return new darwin_nvme_device(this, name, type, nsid);
612 }
613
614 smart_device * darwin_smart_interface::autodetect_smart_device(const char * name)
615 { // TODO - refactor as a function
616 // Acceptable device names are:
617 // /dev/disk*
618 // /dev/rdisk*
619 // disk*
620 // IOService:*
621 // IODeviceTree:*
622 const char *devname = NULL;
623 io_object_t disk;
624
625 if (strncmp (name, "/dev/rdisk", 10) == 0)
626 devname = name + 6;
627 else if (strncmp (name, "/dev/disk", 9) == 0)
628 devname = name + 5;
629 else if (strncmp (name, "disk", 4) == 0)
630 // allow user to just say 'disk0'
631 devname = name;
632 // Find the device. This part should be the same for the NVMe and ATA
633 if (devname) {
634 CFMutableDictionaryRef matcher;
635 matcher = IOBSDNameMatching (kIOMasterPortDefault, 0, devname);
636 disk = IOServiceGetMatchingService (kIOMasterPortDefault, matcher);
637 }
638 else {
639 disk = IORegistryEntryFromPath (kIOMasterPortDefault, name);
640 }
641 if (! disk) {
642 return 0;
643 }
644 io_registry_entry_t tmpdisk=disk;
645
646
647 while (! is_smart_capable (tmpdisk, "ATA"))
648 {
649 IOReturn err;
650 io_object_t prevdisk = tmpdisk;
651
652 // Find this device's parent and try again.
653 err = IORegistryEntryGetParentEntry (tmpdisk, kIOServicePlane, &tmpdisk);
654 if (err != kIOReturnSuccess || ! tmpdisk)
655 {
656 IOObjectRelease (prevdisk);
657 break;
658 }
659 }
660 if (tmpdisk)
661 return new darwin_ata_device(this, name, "");
662 tmpdisk=disk;
663 while (! is_smart_capable (tmpdisk, "NVME"))
664 {
665 IOReturn err;
666 io_object_t prevdisk = tmpdisk;
667
668 // Find this device's parent and try again.
669 err = IORegistryEntryGetParentEntry (tmpdisk, kIOServicePlane, &tmpdisk);
670 if (err != kIOReturnSuccess || ! tmpdisk)
671 {
672 IOObjectRelease (prevdisk);
673 break;
674 }
675 }
676 if (tmpdisk)
677 return new darwin_nvme_device(this, name, "", 0);
678
679 // try ATA as a last option, for compatibility
680 return new darwin_ata_device(this, name, "");
681 }
682
683 static void free_devnames(char * * devnames, int numdevs)
684 {
685 for (int i = 0; i < numdevs; i++)
686 free(devnames[i]);
687 free(devnames);
688 }
689
690 bool darwin_smart_interface::scan_smart_devices(smart_device_list & devlist,
691 const char * type, const char * pattern /*= 0*/)
692 {
693 if (pattern) {
694 set_err(EINVAL, "DEVICESCAN with pattern not implemented yet");
695 return false;
696 }
697
698 // Make namelists
699 char * * atanames = 0; int numata = 0;
700 if (!type || !strcmp(type, "ata")) {
701 numata = make_device_names(&atanames, "ATA");
702 if (numata < 0) {
703 set_err(ENOMEM);
704 return false;
705 }
706 }
707 char * * nvmenames = 0; int numnvme = 0;
708 if (
709 #ifdef WITH_NVME_DEVICESCAN // TODO: Remove when NVMe support is no longer EXPERIMENTAL
710 !type ||
711 #else
712 type &&
713 #endif
714 !strcmp(type, "nvme")) {
715 numnvme = make_device_names(&nvmenames, "NVME");
716 if (numnvme < 0) {
717 set_err(ENOMEM);
718 return false;
719 }
720 }
721
722 // Add to devlist
723 int i;
724 if (!type)
725 type="";
726 for (i = 0; i < numata; i++) {
727 ata_device * atadev = get_ata_device(atanames[i], type);
728 if (atadev)
729 devlist.push_back(atadev);
730 }
731 free_devnames(atanames, numata);
732
733 for (i = 0; i < numnvme; i++) {
734 nvme_device * nvmedev = get_nvme_device(nvmenames[i], type, 0); // default nsid
735 if (nvmedev)
736 devlist.push_back(nvmedev);
737 }
738 free_devnames(nvmenames, numnvme);
739
740 return true;
741 }
742
743 } // namespace
744
745
746 /////////////////////////////////////////////////////////////////////////////
747 /// Initialize platform interface and register with smi()
748
749 void smart_interface::init()
750 {
751 static os::darwin_smart_interface the_interface;
752 smart_interface::set(&the_interface);
753 }