1 | /* $Id: RTSystemQueryDmiString-darwin.cpp 100108 2023-06-07 20:05:13Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - RTSystemQueryDmiString, darwin ring-3.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/system.h>
|
---|
42 | #include "internal/iprt.h"
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/mem.h>
|
---|
47 | #include <iprt/string.h>
|
---|
48 |
|
---|
49 | #include <mach/mach_port.h>
|
---|
50 | #include <IOKit/IOKitLib.h>
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Defined Constants And Macros *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | #define IOCLASS_PLATFORMEXPERTDEVICE "IOPlatformExpertDevice"
|
---|
57 | #define PROP_PRODUCT_NAME "product-name"
|
---|
58 | #define PROP_PRODUCT_VERSION "version"
|
---|
59 | #define PROP_PRODUCT_SERIAL "IOPlatformSerialNumber"
|
---|
60 | #define PROP_PRODUCT_UUID "IOPlatformUUID"
|
---|
61 | #define PROP_MANUFACTURER "manufacturer"
|
---|
62 |
|
---|
63 |
|
---|
64 | RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
|
---|
65 | {
|
---|
66 | AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
|
---|
67 | AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
|
---|
68 | *pszBuf = '\0';
|
---|
69 | AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
|
---|
70 |
|
---|
71 | CFStringRef PropStringRef = NULL;
|
---|
72 | switch (enmString)
|
---|
73 | {
|
---|
74 | case RTSYSDMISTR_PRODUCT_NAME: PropStringRef = CFSTR(PROP_PRODUCT_NAME); break;
|
---|
75 | case RTSYSDMISTR_PRODUCT_VERSION: PropStringRef = CFSTR(PROP_PRODUCT_VERSION); break;
|
---|
76 | case RTSYSDMISTR_PRODUCT_SERIAL: PropStringRef = CFSTR(PROP_PRODUCT_SERIAL); break;
|
---|
77 | case RTSYSDMISTR_PRODUCT_UUID: PropStringRef = CFSTR(PROP_PRODUCT_UUID); break;
|
---|
78 | case RTSYSDMISTR_MANUFACTURER: PropStringRef = CFSTR(PROP_MANUFACTURER); break;
|
---|
79 | default:
|
---|
80 | return VERR_NOT_SUPPORTED;
|
---|
81 | }
|
---|
82 |
|
---|
83 | mach_port_t MasterPort;
|
---|
84 |
|
---|
85 | RT_GCC_NO_WARN_DEPRECATED_BEGIN
|
---|
86 | kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort); /* Deprecated since 12.0. */
|
---|
87 | RT_GCC_NO_WARN_DEPRECATED_END
|
---|
88 | if (kr != kIOReturnSuccess)
|
---|
89 | {
|
---|
90 | if (kr == KERN_NO_ACCESS)
|
---|
91 | return VERR_ACCESS_DENIED;
|
---|
92 | return RTErrConvertFromDarwinIO(kr);
|
---|
93 | }
|
---|
94 |
|
---|
95 | CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_PLATFORMEXPERTDEVICE);
|
---|
96 | if (!ClassToMatch)
|
---|
97 | return VERR_NOT_SUPPORTED;
|
---|
98 |
|
---|
99 | /* IOServiceGetMatchingServices will always consume ClassToMatch. */
|
---|
100 | io_iterator_t Iterator;
|
---|
101 | kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
|
---|
102 | if (kr != kIOReturnSuccess)
|
---|
103 | return RTErrConvertFromDarwinIO(kr);
|
---|
104 |
|
---|
105 | int rc = VERR_NOT_SUPPORTED;
|
---|
106 | io_service_t ServiceObject;
|
---|
107 | while ((ServiceObject = IOIteratorNext(Iterator)))
|
---|
108 | {
|
---|
109 | if ( enmString == RTSYSDMISTR_PRODUCT_NAME
|
---|
110 | || enmString == RTSYSDMISTR_PRODUCT_VERSION
|
---|
111 | || enmString == RTSYSDMISTR_MANUFACTURER
|
---|
112 | )
|
---|
113 | {
|
---|
114 | CFDataRef DataRef = (CFDataRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef,
|
---|
115 | kCFAllocatorDefault, kNilOptions);
|
---|
116 | if (DataRef)
|
---|
117 | {
|
---|
118 | size_t cbData = CFDataGetLength(DataRef);
|
---|
119 | const char *pchData = (const char *)CFDataGetBytePtr(DataRef);
|
---|
120 | rc = RTStrCopyEx(pszBuf, cbBuf, pchData, cbData);
|
---|
121 | CFRelease(DataRef);
|
---|
122 | break;
|
---|
123 | }
|
---|
124 | }
|
---|
125 | else
|
---|
126 | {
|
---|
127 | CFStringRef StringRef = (CFStringRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef,
|
---|
128 | kCFAllocatorDefault, kNilOptions);
|
---|
129 | if (StringRef)
|
---|
130 | {
|
---|
131 | Boolean fRc = CFStringGetCString(StringRef, pszBuf, cbBuf, kCFStringEncodingUTF8);
|
---|
132 | if (fRc)
|
---|
133 | rc = VINF_SUCCESS;
|
---|
134 | else
|
---|
135 | {
|
---|
136 | CFIndex cwc = CFStringGetLength(StringRef);
|
---|
137 | size_t cbTmp = cwc + 1;
|
---|
138 | char *pszTmp = (char *)RTMemTmpAlloc(cbTmp);
|
---|
139 | int cTries = 1;
|
---|
140 | while ( pszTmp
|
---|
141 | && (fRc = CFStringGetCString(StringRef, pszTmp, cbTmp, kCFStringEncodingUTF8)) == FALSE
|
---|
142 | && cTries++ < 4)
|
---|
143 | {
|
---|
144 | RTMemTmpFree(pszTmp);
|
---|
145 | cbTmp *= 2;
|
---|
146 | pszTmp = (char *)RTMemTmpAlloc(cbTmp);
|
---|
147 | }
|
---|
148 | if (fRc)
|
---|
149 | rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
|
---|
150 | else if (!pszTmp)
|
---|
151 | rc = VERR_NO_TMP_MEMORY;
|
---|
152 | else
|
---|
153 | rc = VERR_ACCESS_DENIED;
|
---|
154 | RTMemFree(pszTmp);
|
---|
155 | }
|
---|
156 | CFRelease(StringRef);
|
---|
157 | break;
|
---|
158 | }
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | IOObjectRelease(ServiceObject);
|
---|
163 | IOObjectRelease(Iterator);
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|