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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
|
/* $Id: DevSMC.cpp $ */
/** @file
* DevSMC - SMC device emulation.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
* --------------------------------------------------------------------
*
* This code is based on:
*
* Apple SMC controller
*
* Copyright (c) 2007 Alexander Graf
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* *****************************************************************
*
* In all Intel-based Apple hardware there is an SMC chip to control the
* backlight, fans and several other generic device parameters. It also
* contains the magic keys used to dongle Mac OS X to the device.
*
* This driver was mostly created by looking at the Linux AppleSMC driver
* implementation and does not support IRQ.
*
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_SMC
#include <VBox/pdmdev.h>
#include <VBox/log.h>
#include <VBox/stam.h>
#include <iprt/assert.h>
#include <iprt/string.h>
#ifdef IN_RING0
# include <iprt/asm-amd64-x86.h>
# include <iprt/once.h>
# include <iprt/thread.h>
#endif
#include "../Builtins2.h"
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/* data port used by Apple SMC */
#define APPLESMC_DATA_PORT 0x300
/* command/status port used by Apple SMC */
#define APPLESMC_CMD_PORT 0x304
#define APPLESMC_NR_PORTS 32 /* 0x300-0x31f */
#define APPLESMC_MAX_DATA_LENGTH 32
#define APPLESMC_READ_CMD 0x10
#define APPLESMC_WRITE_CMD 0x11
#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
#define APPLESMC_GET_KEY_TYPE_CMD 0x13
/** The version of the saved state. */
#define SMC_SAVED_STATE_VERSION 1
/** The ring-0 operation number that attempts to get OSK0 and OSK1 from the real
* SMC. */
#define SMC_CALLR0_READ_OSK 1
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct AppleSMCData
{
uint8_t len;
const char *key;
const char *data;
} AppleSMCData;
typedef struct
{
PPDMDEVINSR3 pDevIns;
uint8_t cmd;
uint8_t status;
uint8_t key[4];
uint8_t read_pos;
uint8_t data_len;
uint8_t data_pos;
uint8_t data[255];
/** The OSK0 value. This is currently only used in the constructor. */
uint8_t abOsk0[32];
/** The OSK1 value. This is currently only used in the constructor */
uint8_t abOsk1[32];
} SMCState;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef IN_RING3
static char osk[64];
/* See http://www.mactel-linux.org/wiki/AppleSMC */
static struct AppleSMCData data[] =
{
{6, "REV ", "\0x01\0x13\0x0f\0x00\0x00\0x03"},
{32,"OSK0", osk },
{32,"OSK1", osk+32 },
{1, "NATJ", "\0" },
{1, "MSSP", "\0" },
{1, "MSSD", "\0x3" },
{1, "NTOK", "\0"},
{0, NULL, NULL }
};
#endif /* IN_RING3 */
#ifdef IN_RING0
/** Do once for the SMC ring-0 static data (g_abOsk0, g_abOsk1, g_fHaveOsk). */
static RTONCE g_SmcR0Once = RTONCE_INITIALIZER;
/** Indicates whether we've successfully queried the OSK* keys. */
static bool g_fHaveOsk = false;
/** The OSK0 value. */
static uint8_t g_abOsk0[32];
/** The OSK1 value. */
static uint8_t g_abOsk1[32];
#endif /* IN_RING0 */
#ifndef VBOX_DEVICE_STRUCT_TESTCASE
#ifdef IN_RING0
/**
* Waits for the specified status on the host SMC.
*
* @returns success indicator.
* @param bStatus The desired status.
* @param pszWhat What we're currently doing. For the log.
*/
static bool devR0SmcWaitHostStatus(uint8_t bStatus, const char *pszWhat)
{
uint8_t bCurStatus;
for (uint32_t cMsSleep = 1; cMsSleep <= 64; cMsSleep <<= 1)
{
RTThreadSleep(cMsSleep);
bCurStatus = ASMInU8(APPLESMC_CMD_PORT);
if ((bCurStatus & 0xf) == bStatus)
return true;
}
LogRel(("devR0Smc: %s: bCurStatus=%#x, wanted %#x.\n", pszWhat, bCurStatus, bStatus));
return false;
}
/**
* Reads a key by name from the host SMC.
*
* @returns success indicator.
* @param pszName The key name, must be exactly 4 chars long.
* @param pbBuf The output buffer.
* @param cbBuf The buffer size. Max 32 bytes.
*/
static bool devR0SmcQueryHostKey(const char *pszName, uint8_t *pbBuf, size_t cbBuf)
{
Assert(strlen(pszName) == 4);
Assert(cbBuf <= 32);
Assert(cbBuf > 0);
/*
* Issue the READ command.
*/
uint32_t cMsSleep = 1;
for (;;)
{
ASMOutU8(APPLESMC_CMD_PORT, APPLESMC_READ_CMD);
RTThreadSleep(cMsSleep);
uint8_t bCurStatus = ASMInU8(APPLESMC_CMD_PORT);
if ((bCurStatus & 0xf) == 0xc)
break;
cMsSleep <<= 1;
if (cMsSleep > 64)
{
LogRel(("devR0Smc: %s: bCurStatus=%#x, wanted %#x.\n", "cmd", bCurStatus, 0xc));
return false;
}
}
/*
* Send it the key.
*/
for (unsigned off = 0; off < 4; off++)
{
ASMOutU8(APPLESMC_DATA_PORT, pszName[off]);
if (!devR0SmcWaitHostStatus(4, "key"))
return false;
}
/*
* The desired amount of output.
*/
ASMOutU8(APPLESMC_DATA_PORT, (uint8_t)cbBuf);
/*
* Read the output.
*/
for (size_t off = 0; off < cbBuf; off++)
{
if (!devR0SmcWaitHostStatus(5, off ? "data" : "len"))
return false;
pbBuf[off] = ASMInU8(APPLESMC_DATA_PORT);
}
return true;
}
/**
* RTOnce callback that initializes g_fHaveOsk, g_abOsk0 and g_abOsk1.
*
* @returns VINF_SUCCESS.
* @param pvUser1Ignored Ignored.
* @param pvUser2Ignored Ignored.
*/
static DECLCALLBACK(int) devR0SmcInitOnce(void *pvUser1Ignored, void *pvUser2Ignored)
{
g_fHaveOsk = devR0SmcQueryHostKey("OSK0", &g_abOsk0[0], sizeof(g_abOsk0))
&& devR0SmcQueryHostKey("OSK1", &g_abOsk1[0], sizeof(g_abOsk1));
NOREF(pvUser1Ignored); NOREF(pvUser2Ignored);
return VINF_SUCCESS;
}
/**
* @interface_method_impl{FNPDMDEVREQHANDLERR0}
*/
PDMBOTHCBDECL(int) devR0SmcReqHandler(PPDMDEVINS pDevIns, uint32_t uOperation, uint64_t u64Arg)
{
SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
int rc = VERR_INVALID_FUNCTION;
if (uOperation == SMC_CALLR0_READ_OSK)
{
rc = RTOnce(&g_SmcR0Once, devR0SmcInitOnce, NULL, NULL);
if ( RT_SUCCESS(rc)
&& g_fHaveOsk)
{
AssertCompile(sizeof(g_abOsk0) == sizeof(pThis->abOsk0));
AssertCompile(sizeof(g_abOsk1) == sizeof(pThis->abOsk1));
memcpy(pThis->abOsk0, g_abOsk0, sizeof(pThis->abOsk0));
memcpy(pThis->abOsk1, g_abOsk1, sizeof(pThis->abOsk1));
}
}
return rc;
}
#endif /* IN_RING0 */
#ifdef IN_RING3
/**
* Saves a state of the SMC device.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to save the state to.
*/
static DECLCALLBACK(int) smcSaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle)
{
SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
/** @todo: implement serialization */
return VINF_SUCCESS;
}
/**
* Loads a SMC device state.
*
* @returns VBox status code.
* @param pDevIns The device instance.
* @param pSSMHandle The handle to the saved state.
* @param uVersion The data unit version number.
* @param uPass The data pass.
*/
static DECLCALLBACK(int) smcLoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSMHandle, uint32_t uVersion, uint32_t uPass)
{
SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
if (uVersion != SMC_SAVED_STATE_VERSION)
return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
/** @todo: implement serialization */
return VINF_SUCCESS;
}
/**
* Reset notification.
*
* @returns VBox status.
* @param pDevIns The device instance data.
*/
static DECLCALLBACK(void) smcReset(PPDMDEVINS pDevIns)
{
SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
LogFlow(("smcReset: \n"));
}
/**
* Info handler, device version.
*
* @param pDevIns Device instance which registered the info.
* @param pHlp Callback functions for doing output.
* @param pszArgs Argument string. Optional and specific to the handler.
*/
static DECLCALLBACK(void) smcInfo(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
{
SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
}
static void applesmc_fill_data(SMCState *s)
{
struct AppleSMCData *d;
for (d=data; d->len; d++)
{
uint32_t key_data = *((uint32_t*)d->key);
uint32_t key_current = *((uint32_t*)s->key);
if (key_data == key_current)
{
Log(("APPLESMC: Key matched (%s Len=%d Data=%s)\n", d->key, d->len, d->data));
memcpy(s->data, d->data, d->len);
return;
}
}
}
/**
* Port I/O Handler for IN operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param uPort Port number used for the IN operation.
* @param pu32 Where to store the result.
* @param cb Number of bytes read.
*/
PDMBOTHCBDECL(int) smcIOPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t *pu32, unsigned cb)
{
SMCState * s = PDMINS_2_DATA(pDevIns, SMCState *);
uint8_t retval = 0;
NOREF(pvUser);
Log(("SMC port read: %x (%d)\n", Port, cb));
/** @todo: status code? */
if (cb != 1)
return VERR_IOM_IOPORT_UNUSED;
switch (Port)
{
case APPLESMC_CMD_PORT:
{
retval = s->status;
break;
}
case APPLESMC_DATA_PORT:
{
switch (s->cmd) {
case APPLESMC_READ_CMD:
if(s->data_pos < s->data_len)
{
retval = s->data[s->data_pos];
Log(("APPLESMC: READ_DATA[%d] = %#hhx\n", s->data_pos, retval));
s->data_pos++;
if(s->data_pos == s->data_len)
{
s->status = 0x00;
Log(("APPLESMC: EOF\n"));
}
else
s->status = 0x05;
}
}
break;
}
}
*pu32 = retval;
return VINF_SUCCESS;
}
/**
* Port I/O Handler for OUT operations.
*
* @returns VBox status code.
*
* @param pDevIns The device instance.
* @param pvUser User argument - ignored.
* @param uPort Port number used for the IN operation.
* @param u32 The value to output.
* @param cb The value size in bytes.
*/
PDMBOTHCBDECL(int) smcIOPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT Port, uint32_t u32, unsigned cb)
{
SMCState* s = PDMINS_2_DATA(pDevIns, SMCState *);
NOREF(pvUser);
Log(("SMC port write: %x (%d) %x\n", Port, cb, u32));
/** @todo: status code? */
if (cb != 1)
return VINF_SUCCESS;
switch (Port)
{
case APPLESMC_CMD_PORT:
{
switch (u32)
{
case APPLESMC_READ_CMD:
s->status = 0x0c;
break;
}
s->cmd = u32;
s->read_pos = 0;
s->data_pos = 0;
break;
}
case APPLESMC_DATA_PORT:
{
switch(s->cmd)
{
case APPLESMC_READ_CMD:
if (s->read_pos < 4)
{
s->key[s->read_pos] = u32;
s->status = 0x04;
}
else
if (s->read_pos == 4)
{
s->data_len = u32;
s->status = 0x05;
s->data_pos = 0;
Log(("APPLESMC: Key = %c%c%c%c Len = %d\n", s->key[0], s->key[1], s->key[2], s->key[3], u32));
applesmc_fill_data(s);
}
s->read_pos++;
break;
}
}
}
return VINF_SUCCESS;
}
/**
* @interface_method_impl{PDMDEVREG,pfnConstruct}
*/
static DECLCALLBACK(int) smcConstruct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
{
SMCState *pThis = PDMINS_2_DATA(pDevIns, SMCState *);
Assert(iInstance == 0);
/*
* Store state.
*/
pThis->pDevIns = pDevIns;
/*
* Validate and read the configuration.
*/
PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "DeviceKey|GetKeyFromRealSMC", "");
/*
* Read the DeviceKey config value.
*/
char *pszDeviceKey;
int rc = CFGMR3QueryStringAllocDef(pCfg, "DeviceKey", &pszDeviceKey, "");
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("Configuration error: Querying \"DeviceKey\" as a string failed"));
size_t cchDeviceKey = strlen(pszDeviceKey);
if (cchDeviceKey > 0)
memcpy(&pThis->abOsk0[0], pszDeviceKey, RT_MIN(cchDeviceKey, sizeof(pThis->abOsk0)));
if (cchDeviceKey > sizeof(pThis->abOsk0))
memcpy(&pThis->abOsk1[0], &pszDeviceKey[sizeof(pThis->abOsk0)],
RT_MIN(cchDeviceKey - sizeof(pThis->abOsk0), sizeof(pThis->abOsk1)));
MMR3HeapFree(pszDeviceKey);
/*
* Query the key from the real hardware if asked to do so.
*/
bool fGetKeyFromRealSMC;
rc = CFGMR3QueryBoolDef(pCfg, "GetKeyFromRealSMC", &fGetKeyFromRealSMC, false);
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("Configuration error: Querying \"GetKeyFromRealSMC\" as a boolean failed"));
if (fGetKeyFromRealSMC)
{
rc = PDMDevHlpCallR0(pDevIns, SMC_CALLR0_READ_OSK, 0 /*u64Arg*/);
if (RT_FAILURE(rc))
return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
N_("Failed to query SMC value from the host"));
}
/*
* For practical/historical reasons, the OSK[0|1] data is stored in a
* global buffer in ring-3.
*/
AssertCompile(sizeof(osk) == sizeof(pThis->abOsk0) + sizeof(pThis->abOsk1));
AssertCompile(sizeof(char) == sizeof(uint8_t));
memcpy(osk, pThis->abOsk0, sizeof(pThis->abOsk0));
memcpy(&osk[sizeof(pThis->abOsk0)], pThis->abOsk1, sizeof(pThis->abOsk1));
/*
* Register the IO ports.
*/
rc = PDMDevHlpIOPortRegister(pDevIns, APPLESMC_DATA_PORT, 1, NULL,
smcIOPortWrite, smcIOPortRead,
NULL, NULL, "SMC Data");
if (RT_FAILURE(rc))
return rc;
rc = PDMDevHlpIOPortRegister(pDevIns, APPLESMC_CMD_PORT, 1, NULL,
smcIOPortWrite, smcIOPortRead,
NULL, NULL, "SMC Commands");
if (RT_FAILURE(rc))
return rc;
/* Register saved state management */
rc = PDMDevHlpSSMRegister(pDevIns, SMC_SAVED_STATE_VERSION, sizeof(*pThis), smcSaveExec, smcLoadExec);
if (RT_FAILURE(rc))
return rc;
/*
* Initialize the device state.
*/
smcReset(pDevIns);
/**
* @todo: Register statistics.
*/
PDMDevHlpDBGFInfoRegister(pDevIns, "smc", "Display SMC status. (no arguments)", smcInfo);
return VINF_SUCCESS;
}
/**
* The device registration structure.
*/
const PDMDEVREG g_DeviceSMC =
{
/* u32Version */
PDM_DEVREG_VERSION,
/* szName */
"smc",
/* szRCMod */
"VBoxDD2GC.gc",
/* szR0Mod */
"VBoxDD2R0.r0",
/* pszDescription */
"System Management Controller (SMC) Device",
/* fFlags */
PDM_DEVREG_FLAGS_HOST_BITS_DEFAULT | PDM_DEVREG_FLAGS_GUEST_BITS_32_64 | PDM_DEVREG_FLAGS_PAE36| PDM_DEVREG_FLAGS_R0,
/* fClass */
PDM_DEVREG_CLASS_MISC,
/* cMaxInstances */
1,
/* cbInstance */
sizeof(SMCState),
/* pfnConstruct */
smcConstruct,
/* pfnDestruct */
NULL,
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
smcReset,
/* pfnSuspend */
NULL,
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnQueryInterface. */
NULL,
/* pfnInitComplete */
NULL,
/* pfnPowerOff */
NULL,
/* pfnSoftReset */
NULL,
/* u32VersionEnd */
PDM_DEVREG_VERSION
};
#endif /* IN_RING3 */
#endif /* VBOX_DEVICE_STRUCT_TESTCASE */
|