summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/EFI/VBoxSmcUtil-darwin.cpp
blob: 73ff5ceae4703746d11a5440fd7f00680acc0b21 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* $Id: VBoxSmcUtil-darwin.cpp $ */
/** @file
 * VBoxSmcUtil - Quick hack for viewing SMC data on a mac.
 */

/*
 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <iprt/asm.h>
#include <iprt/err.h>
#include <iprt/ctype.h>
#include <iprt/initterm.h>
#include <iprt/message.h>
#include <iprt/stream.h>
#include <iprt/string.h>

#include <mach/mach.h>
#include <IOKit/IOKitLib.h>
#include <IOKit/IOReturn.h>
#include <CoreFoundation/CoreFoundation.h>
#include <unistd.h>


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
enum
{
    kSMCSuccess = 0,
    kSMCError = 1
};

typedef enum
{
    kSMCUserClientOpen = 0,
    kSMCUserClientClose,
    kSMCHandleYPCEvent,

    kSMCPlaceholder1,
    kSMCNumberOfMethods,

    kSMCReadKey,
    kSMCWriteKey,
    kSMCGetKeyCount,
    kSMCGetKeyFromIndex,
    kSMCGetKeyInfo,

    kSMCFireInterrupt,
    kSMCGetPLimits,
    kSMCGetVers,

    kSMCPlaceholder2,

    kSMCReadStatus,
    kSMCReadResult,
    kSMCVariableCommand
} KSMCFUNCTION;

typedef struct
{
    RTUINT32U           uKey;
    struct
    {
        uint8_t         uMajor;
        uint8_t         uMinor;
        uint8_t         uBuild;
        uint8_t         uReserved;
        uint16_t        uRelease;
    }                   Version;
    struct
    {
        uint16_t        uVer;
        uint16_t        cb;
        uint32_t        uCpuPLimit;
        uint32_t        uGpuPLimit;
        uint32_t        uMemPLimit;
    } SMCPLimitData;

    struct
    {
        IOByteCount     cbData;
        RTUINT32U       uDataType;
        uint8_t         fAttr;
    }                   KeyInfo;

    uint8_t             uResult;
    uint8_t             fStatus;
    uint8_t             bData;
    uint32_t            u32Data;
    uint8_t             abValue[32];
} SMCPARAM;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
io_service_t    g_hSmcService = IO_OBJECT_NULL;
io_connect_t    g_hSmcConnect = IO_OBJECT_NULL;


static int ConnectToSmc(void)
{
    g_hSmcService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleSMC"));
    if (g_hSmcService == IO_OBJECT_NULL)
        return VERR_NOT_FOUND;

    IOReturn rcIo = IOServiceOpen(g_hSmcService, mach_task_self(), 1, &g_hSmcConnect);
    if (rcIo == kIOReturnSuccess && g_hSmcConnect != IO_OBJECT_NULL)
    {
        rcIo = IOConnectCallMethod(g_hSmcConnect, kSMCUserClientOpen, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL);
        if (rcIo == kIOReturnSuccess)
            return VINF_SUCCESS;
        RTMsgError("kSMCUserClientOpen failed: %#x (%#x)\n", rcIo, rcIo);
    }
    else
    {
        RTMsgError("IOServiceOpen failed: %#x (%#x)\n", rcIo, rcIo);
        g_hSmcConnect = IO_OBJECT_NULL;
    }
    return RTErrConvertFromDarwinIO(rcIo);
}


static void DisconnectFromSmc(void)
{
    if (g_hSmcConnect)
    {
        IOConnectCallMethod(g_hSmcConnect, kSMCUserClientClose, NULL, 0, NULL, 0, NULL, NULL, NULL, NULL);
        IOServiceClose(g_hSmcConnect);
        g_hSmcConnect = IO_OBJECT_NULL;
    }

    if (g_hSmcService)
    {
        IOServiceClose(g_hSmcService);
        g_hSmcService = IO_OBJECT_NULL;
    }
}

static int CallSmc(KSMCFUNCTION enmFunction, SMCPARAM *pIn, SMCPARAM *pOut)
{
    RT_ZERO(*pOut);
    pIn->bData    = enmFunction;
    size_t cbOut  = sizeof(*pOut);
    IOReturn rcIo = IOConnectCallStructMethod(g_hSmcConnect, kSMCHandleYPCEvent, pIn, sizeof(*pIn), pOut, &cbOut);
    if (rcIo == kIOReturnSuccess)
        return VINF_SUCCESS;
    RTMsgError("SMC call %d failed: rcIo=%d (%#x)\n", enmFunction, rcIo, rcIo);
    return RTErrConvertFromDarwinIO(rcIo);
}

static int GetKeyCount(uint32_t *pcKeys)
{
    SMCPARAM In;
    SMCPARAM Out;
    RT_ZERO(In); RT_ZERO(Out);
    In.KeyInfo.cbData = sizeof(uint32_t);
    int rc = CallSmc(kSMCGetKeyCount, &In, &Out);
    if (RT_SUCCESS(rc))
        *pcKeys = RT_BE2H_U32(Out.u32Data);
    else
        *pcKeys = 1;
    return rc;
}

static int GetKeyByIndex(uint32_t iKey, SMCPARAM *pKeyData)
{
    SMCPARAM In;
    RT_ZERO(In);
    In.u32Data = iKey;
    int rc = CallSmc(kSMCGetKeyFromIndex, &In, pKeyData);
    if (RT_SUCCESS(rc))
    {
        if (pKeyData->uResult == kSMCSuccess)
        {
            SMCPARAM Tmp = *pKeyData;

            /* Get the key info. */
            RT_ZERO(In);
            In.uKey.u = Tmp.uKey.u;
            rc = CallSmc(kSMCGetKeyInfo, &In, pKeyData);
            if (RT_SUCCESS(rc) && pKeyData->uResult == kSMCSuccess)
            {
                Tmp.KeyInfo = pKeyData->KeyInfo;

                /* Get the key value. */
                RT_ZERO(In);
                In.uKey = Tmp.uKey;
                In.KeyInfo = Tmp.KeyInfo;
                rc = CallSmc(kSMCReadKey, &In, pKeyData);
                if (RT_SUCCESS(rc) && (pKeyData->uResult == kSMCSuccess || pKeyData->uResult == 0x85 /* not readable */))
                {
                    pKeyData->uKey    = Tmp.uKey;
                    pKeyData->KeyInfo = Tmp.KeyInfo;
                    rc = VINF_SUCCESS;
                }
                else if (RT_SUCCESS(rc))
                {
                    RTMsgError("kSMCReadKey failed on #%x/%.4s: %#x\n", iKey, Tmp.uKey.au8, pKeyData->uResult);
                    rc = VERR_IO_GEN_FAILURE;
                }
            }
            else if (RT_SUCCESS(rc))
            {
                RTMsgError("kSMCGetKeyInfo failed on #%x/%.4s: %#x\n", iKey, Tmp.uKey.au8, pKeyData->uResult);
                rc = VERR_IO_GEN_FAILURE;
            }
        }
        else
        {
            RTMsgError("kSMCGetKeyFromIndex failed on #%x: %#x\n", iKey, pKeyData->uResult);
            rc = VERR_IO_GEN_FAILURE;
        }
    }
    return rc;
}


static int GetKeyByName(uint32_t uKey, SMCPARAM *pKeyData)
{
    SMCPARAM In;
    RT_ZERO(In);
    In.uKey.u = uKey;
    int rc = CallSmc(kSMCGetKeyInfo, &In, pKeyData);
    if (RT_SUCCESS(rc) && pKeyData->uResult == kSMCSuccess)
    {
        SMCPARAM Tmp = *pKeyData;

        /* Get the key value. */
        RT_ZERO(In);
        In.uKey.u = uKey;
        In.KeyInfo = Tmp.KeyInfo;
        rc = CallSmc(kSMCReadKey, &In, pKeyData);
        if (RT_SUCCESS(rc) && (pKeyData->uResult == kSMCSuccess || pKeyData->uResult == 0x85 /* not readable */))
        {
            pKeyData->uKey.u  = uKey;
            pKeyData->KeyInfo = Tmp.KeyInfo;
            rc = VINF_SUCCESS;
        }
        else if (RT_SUCCESS(rc))
        {
            RTMsgError("kSMCReadKey failed on %.4s: %#x\n", &uKey, pKeyData->uResult);
            rc = VERR_IO_GEN_FAILURE;
        }
    }
    else if (RT_SUCCESS(rc))
    {
        RTMsgError("kSMCGetKeyInfo failed on %.4s: %#x\n", &uKey, pKeyData->uResult);
        rc = VERR_IO_GEN_FAILURE;
    }
    return rc;
}

static void DisplayKey(SMCPARAM *pKey)
{
    pKey->uKey.u = RT_BE2H_U32(pKey->uKey.u);
    pKey->KeyInfo.uDataType.u = RT_BE2H_U32(pKey->KeyInfo.uDataType.u);
    RTPrintf("key=%4.4s  type=%4.4s  cb=%#04x  fAttr=%#04x",
             pKey->uKey.au8, pKey->KeyInfo.uDataType.au8, pKey->KeyInfo.cbData, pKey->KeyInfo.fAttr);
    if (pKey->uResult == kSMCSuccess)
    {
        bool fPrintable = true;
        for (uint32_t off = 0; off < pKey->KeyInfo.cbData; off++)
            if (!RT_C_IS_PRINT(pKey->abValue[off]))
            {
                fPrintable = false;
                break;
            }
       if (fPrintable)
           RTPrintf("  %.*s\n", pKey->KeyInfo.cbData, pKey->abValue);
       else
           RTPrintf("  %.*Rhxs\n", pKey->KeyInfo.cbData, pKey->abValue);
    }
    else if (pKey->uResult == 0x85)
        RTPrintf("  <not readable>\n");
}

static void DisplayKeyByName(uint32_t uKey)
{
    SMCPARAM Key;
    int rc = GetKeyByName(uKey, &Key);
    if (RT_SUCCESS(rc))
        DisplayKey(&Key);
}


int main(int argc, char **argv)
{
    int rc = RTR3InitExe(argc, &argv, 0);
    if (RT_FAILURE(rc))
        return RTMsgInitFailure(rc);

    rc = ConnectToSmc();
    if (RT_SUCCESS(rc))
    {
        /*
         * Dump the keys.
         */
        uint32_t cKeys;
        rc = GetKeyCount(&cKeys);
        if (RT_SUCCESS(rc))
            RTPrintf("#Keys=%u\n", cKeys);
        for (uint32_t iKey = 0; iKey < cKeys; iKey++)
        {
            SMCPARAM Key;
            rc = GetKeyByIndex(iKey, &Key);
            if (RT_SUCCESS(rc))
            {
                RTPrintf("%#06x: ", iKey);
                DisplayKey(&Key);
            }
        }

        /*
         * Known keys that doesn't make it into the enumeration.
         */
        DisplayKeyByName('OSK0');
        DisplayKeyByName('OSK1');
        DisplayKeyByName('OSK2');

        /* Negative checks, sometimes maybe. */
        DisplayKeyByName('$Num');
        DisplayKeyByName('MSTf');
        DisplayKeyByName('MSDS');
        DisplayKeyByName('LSOF');
    }
    DisconnectFromSmc();

    if (RT_SUCCESS(rc))
        return RTEXITCODE_SUCCESS;
    return RTEXITCODE_FAILURE;
}