forked from synopse/mORMot2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mormot.core.os.mac.pas
204 lines (178 loc) · 5.92 KB
/
mormot.core.os.mac.pas
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
/// Framework Core Wrappers to High-Level MacOS API
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.core.os.mac;
{
*****************************************************************************
MacOS API calls for FPC, as injected to mormot.core.os.pas
- Gather MacOS Specific Operating System Information
This unit uses MacOSAll and link several toolkits, so is not included
in mormot.core.os.pas to reduce executable size, but inject these methods
at runtime: just include "uses mormot.core.os.mac" in programs needing it.
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
{$ifdef OSDARWIN} // do-nothing unit on other systems
{$modeswitch cvar}
{$linkframework IOKit}
uses
classes,
types,
sysutils,
ctypes,
MacOSAll,
mormot.core.base,
mormot.core.os;
{ ****************** Gather MacOS Specific Operating System Information }
implementation
{ ****************** Gather MacOS Specific Operating System Information }
type
kern_return_t = cint;
natural_t = UInt32;
mach_port_t = natural_t;
io_object_t = mach_port_t;
io_registry_entry_t = io_object_t;
io_service_t = io_object_t;
io_string_t = array[0..511] of AnsiChar;
IOOptionBits = UInt32;
var
kIOMasterPortDefault: mach_port_t; cvar; external;
function IORegistryEntryFromPath(
masterPort: mach_port_t; const path: io_string_t): io_registry_entry_t; cdecl; external;
function IORegistryEntryCreateCFProperty(entry: io_registry_entry_t; key: CFStringRef;
allocator: CFAllocatorRef; options: IOOptionBits): CFTypeRef; cdecl; external;
function IOObjectRelease(entry: io_registry_entry_t): kern_return_t; cdecl; external;
function IOServiceMatching(name: PAnsiChar): CFMutableDictionaryRef; cdecl; external;
function IOServiceGetMatchingService(masterPort: mach_port_t;
matching: CFMutableDictionaryRef): io_service_t; cdecl; external;
function IORegistryEntryCreateCFProperties(entry: io_registry_entry_t;
var properties: CFMutableDictionaryRef; allocator: CFAllocatorRef;
options: IOOptionBits): integer; cdecl; external;
// see https://forum.lazarus.freepascal.org/index.php?topic=49358#msg357641
procedure _Get(id: PChar; out result: RawUtf8);
var
ref, str: pointer;
root: io_registry_entry_t;
typeID: CFTypeID;
begin
try
root := IORegistryEntryFromPath(kIOMasterPortDefault, 'IOService:/');
if root = 0 then
exit;
ref := IORegistryEntryCreateCFProperty(root, CFSTR(id), kCFAllocatorDefault, 0);
IOObjectRelease(root);
if ref = nil then
exit;
typeID := CFGetTypeID(ref);
if typeID = CFStringGetTypeID then
begin
FastSetString(result, nil, 1024);
CFStringGetCString(ref, pointer(result), length(result), kCFStringEncodingMacRoman);
SetLength(result, StrLen(pointer(result)));
end
else if typeID = CFDataGetTypeID then
begin
str := CFDataGetBytePtr(ref);
FastSetString(result, str, StrLen(str));
end;
CFRelease(ref);
except
end;
end;
function _GetSmbios(info: TSmbiosBasicInfo): RawUtf8;
begin
case info of
sbiUuid:
begin
_Get('IOPlatformUUID', result);
if PCardinal(result)^ =
ord('F') + ord('F') shl 8 + ord('F') shl 16 + ord('F') shl 24 then
result := '' // fake ID e.g. on iOS 7+
end;
sbiSerial:
_Get('IOPlatformSerialNumber', result);
sbiBoardSerial:
_Get('board-id', result);
sbiManufacturer,
sbiBiosVendor:
_Get('manufacturer', result);
sbiProductName:
_Get('product-name', result);
sbiVersion:
_Get('model', result);
sbiFamily:
_Get('target-type', result);
else
result := '';
end;
end;
function _GetSmbiosData: RawByteString;
var
ref: pointer;
root: io_registry_entry_t;
serv: io_service_t;
prop: CFMutableDictionaryRef;
begin
// by definition, won't return anything if there is no SMBIOS, e.g. on Mac M1
result := '';
try
// first try the IORegistryEntryFromPath() way
// - see https://github.com/acidanthera/dmidecode
root := IORegistryEntryFromPath(kIOMasterPortDefault,
'IOService:/AppleACPIPlatformExpert/bios/AppleSMBIOS');
if root <> 0 then
begin
ref := IORegistryEntryCreateCFProperty(
root, CFSTR('SMBIOS'), kCFAllocatorDefault, 0);
IOObjectRelease(root);
if (ref <> nil) and
(CFGetTypeID(ref) = CFDataGetTypeID) then
FastSetRawByteString(result, CFDataGetBytePtr(ref), CFDataGetLength(ref));
if ref <> nil then
CFRelease(ref);
if result <> '' then
exit;
end;
except
end;
try
// then try the IOServiceGetMatchingService() way
// - see https://github.com/cavaliercoder/dmidecode-osx
serv := IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching('AppleSMBIOS'));
if serv = 0 then
exit;
prop := nil;
IORegistryEntryCreateCFProperties(serv, prop, kCFAllocatorDefault, 0);
IOObjectRelease(serv);
if prop = nil then
exit;
ref := nil;
CFDictionaryGetValueIfPresent(prop, CFSTR('SMBIOS'), @ref);
try
CFRelease(prop);
except
// throws 'Segmentation fault: 11' since macOS 10.12, if the compiled
// binary is not signed with an Apple developer profile :(
end;
if ref = nil then
exit;
if CFGetTypeID(ref) = CFDataGetTypeID then
FastSetRawByteString(result, CFDataGetBytePtr(ref), CFDataGetLength(ref));
try
CFRelease(ref);
except
// buggy Apple
end;
except
result := '';
end;
end;
initialization
PosixInject.GetSmbios := _GetSmbios;
PosixInject.GetSmbiosData := _GetSmbiosData;
{$else}
implementation
{$endif OSDARWIN}
end.