ohmu
·
2026-10-15
1#include "host.h"
2#include "common/apple/cf_helpers.h"
3#include "common/debug.h"
4#include "common/sysctl.h"
5
6#include <IOKit/IOKitLib.h>
7
8const char* getProductNameWithIokit(FFstrbuf* result) {
9 FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t registryEntry = IORegistryEntryFromPath(MACH_PORT_NULL, "IODeviceTree:/product");
10 if (!registryEntry) {
11 FF_DEBUG("IORegistryEntryFromPath(IODeviceTree:/product) failed");
12 return "IOServiceGetMatchingService() failed";
13 }
14
15 FF_CFTYPE_AUTO_RELEASE CFStringRef productName = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("product-name"), kCFAllocatorDefault, kNilOptions);
16 if (!productName) {
17 FF_DEBUG("IORegistryEntryCreateCFProperty(product-name) failed");
18 return "IORegistryEntryCreateCFProperty() failed";
19 }
20
21 return ffCfStrGetString(productName, result);
22}
23
24const char* getOthersByIokit(FFHostResult* host) {
25 FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t registryEntry = IOServiceGetMatchingService(MACH_PORT_NULL, IOServiceMatching("IOPlatformExpertDevice"));
26 if (!registryEntry) {
27 FF_DEBUG("IOServiceGetMatchingService(IOPlatformExpertDevice) failed");
28 return "IOServiceGetMatchingService() failed";
29 }
30
31 FF_CFTYPE_AUTO_RELEASE CFStringRef serialNumber = IORegistryEntryCreateCFProperty(registryEntry, CFSTR(kIOPlatformSerialNumberKey), kCFAllocatorDefault, kNilOptions);
32 if (serialNumber) {
33 ffCfStrGetString(serialNumber, &host->serial);
34 }
35
36 FF_CFTYPE_AUTO_RELEASE CFStringRef uuid = IORegistryEntryCreateCFProperty(registryEntry, CFSTR(kIOPlatformUUIDKey), kCFAllocatorDefault, kNilOptions);
37 if (uuid) {
38 ffCfStrGetString(uuid, &host->uuid);
39 }
40
41 FF_CFTYPE_AUTO_RELEASE CFStringRef manufacturer = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("manufacturer"), kCFAllocatorDefault, kNilOptions);
42 if (manufacturer) {
43 ffCfStrGetString(manufacturer, &host->vendor);
44 }
45
46 FF_CFTYPE_AUTO_RELEASE CFStringRef version = IORegistryEntryCreateCFProperty(registryEntry, CFSTR("version"), kCFAllocatorDefault, kNilOptions);
47 if (version) {
48 ffCfStrGetString(version, &host->version);
49 }
50
51 return nullptr;
52}
53
54const char* ffDetectHost(FFHostResult* host) {
55 const char* error = ffSysctlGetString("hw.product", &host->family);
56 if (error) {
57 error = ffSysctlGetString("hw.model", &host->family);
58 }
59 if (error) {
60 return error;
61 }
62
63 ffStrbufSetStatic(&host->name, ffHostGetMacProductNameWithHwModel(&host->family));
64 if (host->name.length == 0) {
65 getProductNameWithIokit(&host->name);
66 }
67 if (host->name.length == 0) {
68 ffStrbufSet(&host->name, &host->family);
69 }
70 getOthersByIokit(host);
71 return nullptr;
72}