VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/darwin/RTSystemQueryDmiString-darwin.cpp@ 26821

Last change on this file since 26821 was 26821, checked in by vboxsync, 15 years ago

Runtime/r3/darwin: RTSystemQueryDmiString fix.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 4.6 KB
Line 
1/* $Id: RTSystemQueryDmiString-darwin.cpp 26821 2010-02-26 10:06:07Z vboxsync $ */
2/** @file
3 * IPRT - RTSystemQueryDmiString, darwin ring-3.
4 */
5
6/*
7 * Copyright (C) 2010 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/system.h>
36#include "internal/iprt.h"
37
38#include <iprt/err.h>
39#include <iprt/assert.h>
40#include <iprt/string.h>
41
42#include <mach/mach_port.h>
43#include <IOKit/IOKitLib.h>
44
45#define IOCLASS_PLATFORMEXPERTDEVICE "IOPlatformExpertDevice"
46#define PROP_PRODUCT_NAME "product-name"
47#define PROP_PRODUCT_VERSION "version"
48#define PROP_PRODUCT_SERIAL "IOPlatformSerialNumber"
49#define PROP_PRODUCT_UUID "IOPlatformUUID"
50
51RTDECL(int) RTSystemQueryDmiString(RTSYSDMISTR enmString, char *pszBuf, size_t cbBuf)
52{
53 AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
54 AssertReturn(cbBuf > 0, VERR_INVALID_PARAMETER);
55 *pszBuf = '\0';
56 AssertReturn(enmString > RTSYSDMISTR_INVALID && enmString < RTSYSDMISTR_END, VERR_INVALID_PARAMETER);
57
58 CFStringRef PropStringRef = NULL;
59 switch (enmString)
60 {
61 case RTSYSDMISTR_PRODUCT_NAME: PropStringRef = CFSTR(PROP_PRODUCT_NAME); break;
62 case RTSYSDMISTR_PRODUCT_VERSION: PropStringRef = CFSTR(PROP_PRODUCT_VERSION); break;
63 case RTSYSDMISTR_PRODUCT_SERIAL: PropStringRef = CFSTR(PROP_PRODUCT_SERIAL); break;
64 case RTSYSDMISTR_PRODUCT_UUID: PropStringRef = CFSTR(PROP_PRODUCT_UUID); break;
65 default:
66 return VERR_NOT_SUPPORTED;
67 }
68
69 mach_port_t MasterPort;
70 kern_return_t kr = IOMasterPort(MACH_PORT_NULL, &MasterPort);
71 if (kr != kIOReturnSuccess)
72 {
73 if (kr == KERN_NO_ACCESS)
74 return VERR_ACCESS_DENIED;
75 return RTErrConvertFromDarwinIO(kr);
76 }
77
78 CFDictionaryRef ClassToMatch = IOServiceMatching(IOCLASS_PLATFORMEXPERTDEVICE);
79 if (!ClassToMatch)
80 return VERR_NOT_SUPPORTED;
81
82 io_iterator_t Iterator;
83 kr = IOServiceGetMatchingServices(MasterPort, ClassToMatch, &Iterator);
84 if (kr != kIOReturnSuccess)
85 return RTErrConvertFromDarwinIO(kr);
86
87 int rc = VERR_NOT_SUPPORTED;
88 io_service_t ServiceObject;
89 while ((ServiceObject = IOIteratorNext(Iterator)))
90 {
91 if ( enmString == RTSYSDMISTR_PRODUCT_NAME
92 || enmString == RTSYSDMISTR_PRODUCT_VERSION)
93 {
94 CFDataRef DataRef = (CFDataRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef, kCFAllocatorDefault, kNilOptions);
95 if (DataRef)
96 {
97 size_t cbData = CFDataGetLength(DataRef);
98 const uint8_t *pu8Data = CFDataGetBytePtr(DataRef);
99 memcpy(pszBuf, pu8Data, RT_MIN(cbData, cbBuf));
100 pszBuf[RT_MIN(cbData + 1, cbBuf)] = '\0';
101 rc = VINF_SUCCESS;
102 break;
103 }
104 }
105 else
106 {
107 CFStringRef StringRef = (CFStringRef)IORegistryEntryCreateCFProperty(ServiceObject, PropStringRef, kCFAllocatorDefault, kNilOptions);
108 if (StringRef)
109 {
110 Boolean res = CFStringGetCString(StringRef, pszBuf, cbBuf, kCFStringEncodingASCII);
111 rc = res == TRUE ? VINF_SUCCESS : VERR_NOT_SUPPORTED;
112 break;
113 }
114 }
115 }
116
117 IOObjectRelease(ServiceObject);
118 IOObjectRelease(Iterator);
119 return rc;
120}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette