1 | /* $Id: RTSystemQueryDmiString-darwin.cpp 98103 2023-01-17 14:15:46Z 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 | kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
|
---|
85 | if (kr != kIOReturnSuccess)
|
---|
86 | {
|
---|
87 | if (kr == KERN_NO_ACCESS)
|
---|
88 | return VERR_ACCESS_DENIED;
|
---|
89 | return RTErrConvertFromDarwinIO(kr);
|
---|
90 | }
|
---|
91 |
|
---|
92 | CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_PLATFORMEXPERTDEVICE);
|
---|
93 | if (!ClassToMatch)
|
---|
94 | return VERR_NOT_SUPPORTED;
|
---|
95 |
|
---|
96 | /* IOServiceGetMatchingServices will always consume ClassToMatch. */
|
---|
97 | io_iterator_t Iterator;
|
---|
98 | kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
|
---|
99 | if (kr != kIOReturnSuccess)
|
---|
100 | return RTErrConvertFromDarwinIO(kr);
|
---|
101 |
|
---|
102 | int rc = VERR_NOT_SUPPORTED;
|
---|
103 | io_service_t ServiceObject;
|
---|
104 | while ((ServiceObject = IOIteratorNext(Iterator)))
|
---|
105 | {
|
---|
106 | if ( enmString == RTSYSDMISTR_PRODUCT_NAME
|
---|
107 | || enmString == RTSYSDMISTR_PRODUCT_VERSION
|
---|
108 | || enmString == RTSYSDMISTR_MANUFACTURER
|
---|
109 | )
|
---|
110 | {
|
---|
111 | CFDataRef DataRef = (CFDataRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef,
|
---|
112 | kCFAllocatorDefault, kNilOptions);
|
---|
113 | if (DataRef)
|
---|
114 | {
|
---|
115 | size_t cbData = CFDataGetLength(DataRef);
|
---|
116 | const char *pchData = (const char *)CFDataGetBytePtr(DataRef);
|
---|
117 | rc = RTStrCopyEx(pszBuf, cbBuf, pchData, cbData);
|
---|
118 | CFRelease(DataRef);
|
---|
119 | break;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | CFStringRef StringRef = (CFStringRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef,
|
---|
125 | kCFAllocatorDefault, kNilOptions);
|
---|
126 | if (StringRef)
|
---|
127 | {
|
---|
128 | Boolean fRc = CFStringGetCString(StringRef, pszBuf, cbBuf, kCFStringEncodingUTF8);
|
---|
129 | if (fRc)
|
---|
130 | rc = VINF_SUCCESS;
|
---|
131 | else
|
---|
132 | {
|
---|
133 | CFIndex cwc = CFStringGetLength(StringRef);
|
---|
134 | size_t cbTmp = cwc + 1;
|
---|
135 | char *pszTmp = (char *)RTMemTmpAlloc(cbTmp);
|
---|
136 | int cTries = 1;
|
---|
137 | while ( pszTmp
|
---|
138 | && (fRc = CFStringGetCString(StringRef, pszTmp, cbTmp, kCFStringEncodingUTF8)) == FALSE
|
---|
139 | && cTries++ < 4)
|
---|
140 | {
|
---|
141 | RTMemTmpFree(pszTmp);
|
---|
142 | cbTmp *= 2;
|
---|
143 | pszTmp = (char *)RTMemTmpAlloc(cbTmp);
|
---|
144 | }
|
---|
145 | if (fRc)
|
---|
146 | rc = RTStrCopy(pszBuf, cbBuf, pszTmp);
|
---|
147 | else if (!pszTmp)
|
---|
148 | rc = VERR_NO_TMP_MEMORY;
|
---|
149 | else
|
---|
150 | rc = VERR_ACCESS_DENIED;
|
---|
151 | RTMemFree(pszTmp);
|
---|
152 | }
|
---|
153 | CFRelease(StringRef);
|
---|
154 | break;
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | IOObjectRelease(ServiceObject);
|
---|
160 | IOObjectRelease(Iterator);
|
---|
161 | return rc;
|
---|
162 | }
|
---|
163 |
|
---|