[48435] | 1 | /* $Id: DevSmc.cpp 101222 2023-09-21 14:41:09Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[64805] | 3 | * DevSmc - Apple System Management Controller.
|
---|
[48435] | 4 | *
|
---|
| 5 | * The SMC is controlling power, fans, take measurements (voltage, temperature,
|
---|
| 6 | * fan speed, ++), and lock Mac OS X to Apple hardware. For more details see:
|
---|
| 7 | * - http://en.wikipedia.org/wiki/System_Management_Controller
|
---|
| 8 | * - http://www.parhelia.ch/blog/statics/k3_keys.html
|
---|
| 9 | * - http://www.nosuchcon.org/talks/D1_02_Alex_Ninjas_and_Harry_Potter.pdf
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | /*
|
---|
[98103] | 13 | * Copyright (C) 2013-2023 Oracle and/or its affiliates.
|
---|
[48435] | 14 | *
|
---|
[96407] | 15 | * This file is part of VirtualBox base platform packages, as
|
---|
| 16 | * available from https://www.virtualbox.org.
|
---|
| 17 | *
|
---|
| 18 | * This program is free software; you can redistribute it and/or
|
---|
| 19 | * modify it under the terms of the GNU General Public License
|
---|
| 20 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 21 | * License.
|
---|
| 22 | *
|
---|
| 23 | * This program is distributed in the hope that it will be useful, but
|
---|
| 24 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 26 | * General Public License for more details.
|
---|
| 27 | *
|
---|
| 28 | * You should have received a copy of the GNU General Public License
|
---|
| 29 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 30 | *
|
---|
| 31 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[48435] | 32 | */
|
---|
| 33 |
|
---|
| 34 |
|
---|
[57358] | 35 | /*********************************************************************************************************************************
|
---|
| 36 | * Header Files *
|
---|
| 37 | *********************************************************************************************************************************/
|
---|
[48435] | 38 | #define LOG_GROUP LOG_GROUP_DEV_SMC
|
---|
| 39 | #include <VBox/vmm/pdmdev.h>
|
---|
| 40 | #include <VBox/log.h>
|
---|
| 41 | #include <VBox/err.h>
|
---|
| 42 | #include <iprt/assert.h>
|
---|
| 43 | #include <iprt/string.h>
|
---|
[87284] | 44 | #if defined(IN_RING0) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
|
---|
[48435] | 45 | # include <iprt/asm-amd64-x86.h>
|
---|
| 46 | # include <iprt/once.h>
|
---|
| 47 | #endif
|
---|
[93944] | 48 | #if defined(RT_OS_DARWIN) && defined(IN_RING3) && !defined(VBOX_DEVICE_STRUCT_TESTCASE) /* drags in bad page size define */
|
---|
[55502] | 49 | # include "IOKit/IOKitLib.h"
|
---|
| 50 | #endif
|
---|
[48435] | 51 |
|
---|
| 52 | #include "VBoxDD.h"
|
---|
| 53 |
|
---|
| 54 |
|
---|
[57358] | 55 | /*********************************************************************************************************************************
|
---|
| 56 | * Defined Constants And Macros *
|
---|
| 57 | *********************************************************************************************************************************/
|
---|
[48435] | 58 | /** The current version of the saved state. */
|
---|
| 59 | #define SMC_SAVED_STATE_VERSION 1 /** @todo later 2 */
|
---|
| 60 | /** Empty saved state version. */
|
---|
| 61 | #define SMC_SAVED_STATE_VERSION_BAKA 1
|
---|
| 62 |
|
---|
| 63 | /** The ring-0 operation number that attempts to get OSK0 and OSK1 from the real
|
---|
| 64 | * SMC. */
|
---|
| 65 | #define SMC_CALLR0_READ_OSK 1
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | /** @name Apple SMC port and register definitions.
|
---|
| 69 | * @{ */
|
---|
| 70 |
|
---|
| 71 | /** The first Apple SMC port. */
|
---|
| 72 | #define SMC_PORT_FIRST 0x0300
|
---|
[51520] | 73 | /** The number of registers (also ports). */
|
---|
[48435] | 74 | #define SMC_REG_COUNT 0x0020
|
---|
| 75 |
|
---|
| 76 | /** The data register. */
|
---|
| 77 | #define SMC_REG_DATA 0x00
|
---|
| 78 | #define SMC_PORT_DATA (SMC_PORT_FIRST + SMC_REG_DATA)
|
---|
| 79 |
|
---|
| 80 | /** The command register. */
|
---|
| 81 | #define SMC_REG_CMD 0x04
|
---|
| 82 | #define SMC_PORT_CMD (SMC_PORT_FIRST + SMC_REG_CMD)
|
---|
| 83 |
|
---|
| 84 | /** Status code register. */
|
---|
| 85 | #define SMC_REG_STATUS_CODE 0x1e
|
---|
| 86 | #define SMC_PORT_STATUS_CODE (SMC_PORT_FIRST + SMC_REG_STATUS_CODE)
|
---|
| 87 | /** @} */
|
---|
| 88 |
|
---|
| 89 | /** @name Apple SMC Commands.
|
---|
| 90 | * @{ */
|
---|
[81519] | 91 | #define SMC_CMD_GET_KEY_VALUE 0x10
|
---|
| 92 | #define SMC_CMD_PUT_KEY 0x11
|
---|
[48435] | 93 | #define SMC_CMD_GET_KEY_BY_INDEX 0x12
|
---|
| 94 | #define SMC_CMD_GET_KEY_INFO 0x13
|
---|
| 95 | /** @} */
|
---|
| 96 |
|
---|
| 97 | /** @name Apple SMC Status Codes.
|
---|
| 98 | * @{ */
|
---|
| 99 | #define SMC_STATUS_CD_SUCCESS UINT8_C(0x00)
|
---|
| 100 | #define SMC_STATUS_CD_COMM_COLLISION UINT8_C(0x80)
|
---|
| 101 | #define SMC_STATUS_CD_SPURIOUS_DATA UINT8_C(0x81)
|
---|
| 102 | #define SMC_STATUS_CD_BAD_COMMAND UINT8_C(0x82)
|
---|
| 103 | #define SMC_STATUS_CD_BAD_PARAMETER UINT8_C(0x83)
|
---|
| 104 | #define SMC_STATUS_CD_KEY_NOT_FOUND UINT8_C(0x84)
|
---|
| 105 | #define SMC_STATUS_CD_KEY_NOT_READABLE UINT8_C(0x85)
|
---|
| 106 | #define SMC_STATUS_CD_KEY_NOT_WRITABLE UINT8_C(0x86)
|
---|
| 107 | #define SMC_STATUS_CD_KEY_SIZE_MISMATCH UINT8_C(0x87)
|
---|
| 108 | #define SMC_STATUS_CD_FRAMING_ERROR UINT8_C(0x88)
|
---|
| 109 | #define SMC_STATUS_CD_BAD_ARGUMENT_ERROR UINT8_C(0x89)
|
---|
| 110 | #define SMC_STATUS_CD_TIMEOUT_ERROR UINT8_C(0xb7)
|
---|
| 111 | #define SMC_STATUS_CD_KEY_INDEX_RANGE_ERROR UINT8_C(0xb8)
|
---|
| 112 | #define SMC_STATUS_CD_BAD_FUNC_PARAMETER UINT8_C(0xc0)
|
---|
| 113 | #define SMC_STATUS_CD_EVENT_BUFF_WRONG_ORDER UINT8_C(0x??)
|
---|
| 114 | #define SMC_STATUS_CD_EVENT_BUFF_READ_ERROR UINT8_C(0x??)
|
---|
| 115 | #define SMC_STATUS_CD_DEVICE_ACCESS_ERROR UINT8_C(0xc7)
|
---|
| 116 | #define SMC_STATUS_CD_UNSUPPORTED_FEATURE UINT8_C(0xcb)
|
---|
| 117 | #define SMC_STATUS_CD_SMB_ACCESS_ERROR UINT8_C(0xcc)
|
---|
| 118 | /** @} */
|
---|
| 119 |
|
---|
| 120 | /** @name Apple SMC Key Attributes.
|
---|
| 121 | * @{ */
|
---|
| 122 | #define SMC_KEY_ATTR_PRIVATE UINT8_C(0x01)
|
---|
| 123 | #define SMC_KEY_ATTR_UKN_0x02 UINT8_C(0x02)
|
---|
| 124 | #define SMC_KEY_ATTR_UKN_0x04 UINT8_C(0x04)
|
---|
| 125 | #define SMC_KEY_ATTR_CONST UINT8_C(0x08)
|
---|
| 126 | #define SMC_KEY_ATTR_FUNCTION UINT8_C(0x10)
|
---|
| 127 | #define SMC_KEY_ATTR_UKN_0x20 UINT8_C(0x20)
|
---|
| 128 | #define SMC_KEY_ATTR_WRITE UINT8_C(0x40)
|
---|
| 129 | #define SMC_KEY_ATTR_READ UINT8_C(0x80)
|
---|
| 130 | /** @} */
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | /** The index of the first enumerable key in g_aSmcKeys. */
|
---|
| 134 | #define SMC_KEYIDX_FIRST_ENUM 2
|
---|
| 135 |
|
---|
| 136 | /** Macro for emitting a static DEVSMC4CHID initializer. */
|
---|
[81520] | 137 | #define SMC4CH(ch1, ch2, ch3, ch4) { { ch1, ch2, ch3, ch4 } }
|
---|
[48435] | 138 |
|
---|
| 139 | /**
|
---|
| 140 | * Macro for comparing DEVSMC4CHID with a string value.
|
---|
| 141 | * @returns true if equal, false if not.
|
---|
| 142 | */
|
---|
| 143 | #define SMC4CH_EQ(a_pSmcKey, a_sz4) ( (a_pSmcKey)->u32 == RT_MAKE_U32_FROM_U8(a_sz4[0], a_sz4[1], a_sz4[2], a_sz4[3]) )
|
---|
| 144 |
|
---|
| 145 | /** Indicates the we want a 2.x SMC. */
|
---|
| 146 | #define VBOX_WITH_SMC_2_x
|
---|
| 147 |
|
---|
| 148 |
|
---|
[57358] | 149 | /*********************************************************************************************************************************
|
---|
| 150 | * Structures and Typedefs *
|
---|
| 151 | *********************************************************************************************************************************/
|
---|
[48435] | 152 |
|
---|
| 153 | /**
|
---|
| 154 | * 4 char identifier
|
---|
| 155 | */
|
---|
| 156 | typedef union DEVSMC4CHID
|
---|
| 157 | {
|
---|
| 158 | /** Byte view. */
|
---|
| 159 | uint8_t ab[4];
|
---|
| 160 | /** 32-bit unsigned integer view. */
|
---|
| 161 | uint32_t u32;
|
---|
| 162 | } DEVSMC4CHID;
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 | /**
|
---|
| 166 | * Current key data area for communicating with the guest.
|
---|
| 167 | */
|
---|
| 168 | typedef struct DEVSMCCURKEY
|
---|
| 169 | {
|
---|
| 170 | /** The key. */
|
---|
| 171 | DEVSMC4CHID Key;
|
---|
| 172 | /** The data type. */
|
---|
| 173 | DEVSMC4CHID Type;
|
---|
| 174 | /** Key attributes. */
|
---|
| 175 | uint8_t fAttr;
|
---|
| 176 | /** The value length. */
|
---|
| 177 | uint8_t cbValue;
|
---|
| 178 | uint8_t abAlignment[2];
|
---|
| 179 | /**
|
---|
| 180 | * The value union. 32 bytes is probably sufficient here, but we provide a
|
---|
| 181 | * little more room since it doesn't cost us anything. */
|
---|
| 182 | union
|
---|
| 183 | {
|
---|
| 184 | /** Byte view. */
|
---|
| 185 | uint8_t ab[128];
|
---|
| 186 | /** 16-bit view. */
|
---|
| 187 | uint16_t u16;
|
---|
| 188 | /** 32-bit view. */
|
---|
| 189 | uint32_t u32;
|
---|
| 190 | } Value;
|
---|
| 191 | } DEVSMCCURKEY;
|
---|
| 192 | AssertCompileSize(DEVSMCCURKEY, 128+12);
|
---|
| 193 | /** Pointer to the current key buffer. */
|
---|
| 194 | typedef DEVSMCCURKEY *PDEVSMCCURKEY;
|
---|
| 195 | /** Const pointer to the current key buffer. */
|
---|
| 196 | typedef DEVSMCCURKEY const *PCDEVSMCCURKEY;
|
---|
| 197 |
|
---|
| 198 |
|
---|
| 199 | /**
|
---|
| 200 | * The device
|
---|
| 201 | */
|
---|
| 202 | typedef struct DEVSMC
|
---|
| 203 | {
|
---|
| 204 | /** The current command (SMC_PORT_CMD write). */
|
---|
| 205 | uint8_t bCmd;
|
---|
| 206 | /** Current key offset. */
|
---|
| 207 | uint8_t offKey;
|
---|
| 208 | /** Current value offset. */
|
---|
| 209 | uint8_t offValue;
|
---|
| 210 | /** Number of keys in the aKeys array. */
|
---|
| 211 | uint8_t cKeys;
|
---|
| 212 |
|
---|
| 213 | /** The current key data the user is accessing. */
|
---|
| 214 | DEVSMCCURKEY CurKey;
|
---|
| 215 |
|
---|
| 216 | /**
|
---|
| 217 | * Generic read/write register values.
|
---|
| 218 | *
|
---|
| 219 | * The DATA register entry is not used at all. The CMD register entry contains
|
---|
| 220 | * the state value.
|
---|
| 221 | */
|
---|
| 222 | union
|
---|
| 223 | {
|
---|
| 224 | /** Index register view. */
|
---|
| 225 | uint8_t abRegsRW[SMC_REG_COUNT];
|
---|
| 226 | /** Named register view. */
|
---|
| 227 | struct
|
---|
| 228 | {
|
---|
| 229 | uint8_t abUnknown0[0x04];
|
---|
| 230 | /** The current state (SMC_PORT_CMD read). */
|
---|
| 231 | uint8_t bState;
|
---|
| 232 | uint8_t abUnknown1[0x1e - 0x05];
|
---|
| 233 | /** The current status code (SMC_PORT_STATUS_CODE). */
|
---|
| 234 | uint8_t bStatusCode;
|
---|
| 235 | uint8_t abUnknown2[1];
|
---|
| 236 | } s;
|
---|
| 237 | } u;
|
---|
| 238 |
|
---|
| 239 | /** @name Key data.
|
---|
| 240 | * @{ */
|
---|
| 241 | /** OSK0 and OSK1. */
|
---|
| 242 | char szOsk0And1[64+1];
|
---|
| 243 | /** $Num - unknown function. */
|
---|
| 244 | uint8_t bDollaryNumber;
|
---|
| 245 | /** MSSD - shutdown reason. */
|
---|
| 246 | uint8_t bShutdownReason;
|
---|
| 247 | /** NATJ - Ninja action timer job. */
|
---|
| 248 | uint8_t bNinjaActionTimerJob;
|
---|
| 249 | /** @} */
|
---|
[81519] | 250 |
|
---|
| 251 | /** The I/O port registration handle. */
|
---|
| 252 | IOMIOPORTHANDLE hIoPorts;
|
---|
[48435] | 253 | } DEVSMC;
|
---|
[48760] | 254 | #ifndef _MSC_VER
|
---|
[48435] | 255 | AssertCompileMembersAtSameOffset(DEVSMC, u.abRegsRW[SMC_REG_CMD], DEVSMC, u.s.bState);
|
---|
| 256 | AssertCompileMembersAtSameOffset(DEVSMC, u.abRegsRW[SMC_REG_STATUS_CODE], DEVSMC, u.s.bStatusCode);
|
---|
[48760] | 257 | #endif
|
---|
[48435] | 258 |
|
---|
| 259 | /** Pointer to the SMC state. */
|
---|
| 260 | typedef DEVSMC *PDEVSMC;
|
---|
| 261 |
|
---|
[52508] | 262 | #ifndef VBOX_DEVICE_STRUCT_TESTCASE
|
---|
[48435] | 263 |
|
---|
| 264 |
|
---|
| 265 | /**
|
---|
| 266 | * Method for retriving the key value and/or optionally also attributes.
|
---|
| 267 | *
|
---|
| 268 | * @returns Apple SMC Status Code.
|
---|
| 269 | * @param pThis The SMC instance data.
|
---|
| 270 | * @param pCurKey The current key structure (input / output).
|
---|
| 271 | * @param bCmd The current command (mostly for getters that also
|
---|
| 272 | * provides attributes or type info).
|
---|
| 273 | * @param pKeyDesc Pointer to the key descriptor so that the getter can
|
---|
| 274 | * service more than once key.
|
---|
| 275 | */
|
---|
[85121] | 276 | typedef DECLCALLBACKTYPE(uint8_t, FNDEVSMCKEYGETTER,(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd,
|
---|
| 277 | struct DEVSMCKEYDESC const *pKeyDesc));
|
---|
[48435] | 278 |
|
---|
| 279 | /**
|
---|
| 280 | * Method for setting the key value.
|
---|
| 281 | *
|
---|
| 282 | * @returns Apple SMC Status Code.
|
---|
| 283 | * @param pThis The SMC instance data.
|
---|
| 284 | * @param pCurKey The current key structure (input / output).
|
---|
| 285 | * @param bCmd The current command (currently not relevant).
|
---|
| 286 | * @param pKeyDesc Pointer to the key descriptor so that the getter can
|
---|
| 287 | * service more than once key.
|
---|
| 288 | */
|
---|
[85121] | 289 | typedef DECLCALLBACKTYPE(uint8_t, FNDEVSMCKEYPUTTER,(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd,
|
---|
| 290 | struct DEVSMCKEYDESC const *pKeyDesc));
|
---|
[48435] | 291 |
|
---|
| 292 | /**
|
---|
| 293 | * Key descriptor.
|
---|
| 294 | */
|
---|
| 295 | typedef struct DEVSMCKEYDESC
|
---|
| 296 | {
|
---|
| 297 | /** The key 4 character identifier. */
|
---|
| 298 | DEVSMC4CHID Key;
|
---|
| 299 | /** Type 4 character identifier. 0 means the getter will set it dynamically. */
|
---|
| 300 | DEVSMC4CHID Type;
|
---|
[85121] | 301 | /** Getter method, see FNDEVSMCKEYGETTER. */
|
---|
| 302 | FNDEVSMCKEYGETTER *pfnGet;
|
---|
| 303 | /** Putter method, see FNDEVSMCKEYPUTTER. */
|
---|
| 304 | FNDEVSMCKEYPUTTER *pfnPut;
|
---|
[48435] | 305 | /** The keyvalue size. If 0 the pfnGet/pfnPut will define/check the size. */
|
---|
| 306 | uint8_t cbValue;
|
---|
| 307 | /** Attributes. 0 means the getter will set it dynamically. */
|
---|
| 308 | uint8_t fAttr;
|
---|
| 309 | } DEVSMCKEYDESC;
|
---|
| 310 | /** Pointer to a constant SMC key descriptor. */
|
---|
| 311 | typedef DEVSMCKEYDESC const *PCDEVSMCKEYDESC;
|
---|
| 312 |
|
---|
| 313 |
|
---|
[57358] | 314 | /*********************************************************************************************************************************
|
---|
| 315 | * Internal Functions *
|
---|
| 316 | *********************************************************************************************************************************/
|
---|
[85121] | 317 | static FNDEVSMCKEYGETTER scmKeyGetOSKs;
|
---|
| 318 | static FNDEVSMCKEYGETTER scmKeyGetKeyCount;
|
---|
| 319 | static FNDEVSMCKEYGETTER scmKeyGetRevision;
|
---|
[81519] | 320 | #ifdef VBOX_WITH_SMC_2_x
|
---|
[85121] | 321 | static FNDEVSMCKEYGETTER scmKeyGetDollarAddress;
|
---|
| 322 | static FNDEVSMCKEYGETTER scmKeyGetDollarNumber;
|
---|
| 323 | static FNDEVSMCKEYPUTTER scmKeyPutDollarNumber;
|
---|
[81519] | 324 | #endif
|
---|
[85121] | 325 | static FNDEVSMCKEYGETTER scmKeyGetShutdownReason;
|
---|
| 326 | static FNDEVSMCKEYPUTTER scmKeyPutShutdownReason;
|
---|
| 327 | static FNDEVSMCKEYGETTER scmKeyGetNinjaTimerAction;
|
---|
| 328 | static FNDEVSMCKEYPUTTER scmKeyPutNinjaTimerAction;
|
---|
| 329 | static FNDEVSMCKEYGETTER scmKeyGetOne;
|
---|
| 330 | static FNDEVSMCKEYGETTER scmKeyGetZero;
|
---|
[48435] | 331 |
|
---|
| 332 |
|
---|
[57358] | 333 | /*********************************************************************************************************************************
|
---|
| 334 | * Global Variables *
|
---|
| 335 | *********************************************************************************************************************************/
|
---|
[48435] | 336 | /**
|
---|
| 337 | * Apple SMC key descriptor table.
|
---|
| 338 | */
|
---|
| 339 | static const DEVSMCKEYDESC g_aSmcKeys[] =
|
---|
| 340 | {
|
---|
| 341 | /* Non-enum keys first. */
|
---|
[81520] | 342 | { SMC4CH('O','S','K','0'), SMC4CH('c','h','8','*'), scmKeyGetOSKs, NULL, 32, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_FUNCTION },
|
---|
| 343 | { SMC4CH('O','S','K','1'), SMC4CH('c','h','8','*'), scmKeyGetOSKs, NULL, 32, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_FUNCTION },
|
---|
[48435] | 344 |
|
---|
| 345 | /* The first enum key is the #KEY value. */
|
---|
[81520] | 346 | { SMC4CH('#','K','E','Y'), SMC4CH('u','i','3','2'), scmKeyGetKeyCount, NULL, 4, SMC_KEY_ATTR_READ },
|
---|
[48435] | 347 | # ifdef VBOX_WITH_SMC_2_x
|
---|
[81520] | 348 | { SMC4CH('$','A','d','r'), SMC4CH('u','i','3','2'), scmKeyGetDollarAddress, NULL, 4, SMC_KEY_ATTR_READ },
|
---|
| 349 | { SMC4CH('$','N','u','m'), SMC4CH('u','i','8',' '), scmKeyGetDollarNumber, scmKeyPutDollarNumber, 1, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_WRITE | SMC_KEY_ATTR_PRIVATE },
|
---|
| 350 | { SMC4CH('B','E','M','B'), SMC4CH('f','l','a','g'), scmKeyGetOne, NULL, 1, SMC_KEY_ATTR_READ },
|
---|
[48435] | 351 | # else
|
---|
[81520] | 352 | { SMC4CH('L','S','O','F'), SMC4CH('f','l','a','g'), scmKeyGetZero, NULL, 1, SMC_KEY_ATTR_READ },
|
---|
[48435] | 353 | # endif
|
---|
[81520] | 354 | { SMC4CH('M','S','S','D'), SMC4CH('s','i','8',' '), scmKeyGetShutdownReason, scmKeyPutShutdownReason, 1, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_WRITE | SMC_KEY_ATTR_PRIVATE },
|
---|
[48435] | 355 | /* MSDS is not present on MacPro3,1 nor MacBookPro10,1, so returning not found is fine. */
|
---|
| 356 | # ifdef VBOX_WITH_SMC_2_x
|
---|
[81520] | 357 | { SMC4CH('M','S','T','f'), SMC4CH('u','i','8',' '), scmKeyGetZero, NULL, 1, SMC_KEY_ATTR_READ },
|
---|
[48435] | 358 | # endif
|
---|
[81520] | 359 | { SMC4CH('N','A','T','J'), SMC4CH('u','i','8',' '), scmKeyGetNinjaTimerAction, scmKeyPutNinjaTimerAction, 1, SMC_KEY_ATTR_READ | SMC_KEY_ATTR_WRITE | SMC_KEY_ATTR_PRIVATE },
|
---|
| 360 | { SMC4CH('R','E','V',' '), SMC4CH('{','r','e','v'), scmKeyGetRevision, NULL, 6, SMC_KEY_ATTR_READ },
|
---|
[48435] | 361 | /** @todo MSSP, NTOK and more. */
|
---|
| 362 | };
|
---|
| 363 |
|
---|
[87284] | 364 | #if defined(IN_RING0) && (defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86))
|
---|
| 365 |
|
---|
[48435] | 366 | /** Do once for the SMC ring-0 static data (g_abOsk0And1, g_fHaveOsk). */
|
---|
| 367 | static RTONCE g_SmcR0Once = RTONCE_INITIALIZER;
|
---|
| 368 | /** Indicates whether we've successfully queried the OSK* keys. */
|
---|
[55522] | 369 | static bool g_fHaveOsk = false;
|
---|
[51520] | 370 | /** The OSK0 and OSK1 values. */
|
---|
[48435] | 371 | static uint8_t g_abOsk0And1[32+32];
|
---|
| 372 |
|
---|
| 373 |
|
---|
| 374 | /**
|
---|
| 375 | * Waits for the specified state on the host SMC.
|
---|
| 376 | *
|
---|
| 377 | * @returns success indicator.
|
---|
| 378 | * @param bState The desired state.
|
---|
| 379 | * @param pszWhat What we're currently doing. For the log.
|
---|
| 380 | */
|
---|
| 381 | static bool devR0SmcWaitHostState(uint8_t bState, const char *pszWhat)
|
---|
| 382 | {
|
---|
[62428] | 383 | uint8_t bCurState = 0; /* (MSC is potentially uninitialized) */
|
---|
[48435] | 384 | for (uint32_t cMsSleep = 1; cMsSleep <= 64; cMsSleep <<= 1)
|
---|
| 385 | {
|
---|
| 386 | RTThreadSleep(cMsSleep);
|
---|
| 387 | bCurState = ASMInU16(SMC_PORT_CMD);
|
---|
| 388 | if ((bCurState & 0xf) == bState)
|
---|
| 389 | return true;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | LogRel(("devR0Smc: %s: bCurState=%#x, wanted %#x.\n", pszWhat, bCurState, bState));
|
---|
| 393 | #if 0
|
---|
| 394 | uint8_t bCurStatus2 = ASMInU8(SMC_PORT_STATUS_CODE);
|
---|
| 395 | uint8_t bCurStatus3 = ASMInU8(SMC_PORT_STATUS_CODE);
|
---|
| 396 | uint16_t wCurStatus3 = ASMInU16(SMC_PORT_STATUS_CODE);
|
---|
| 397 | uint32_t dwCurStatus3 = ASMInU32(SMC_PORT_STATUS_CODE);
|
---|
| 398 | LogRel(("SMC: status2=%#x status3=%#x w=%#x dw=%#x\n", bCurStatus2, bCurStatus3, wCurStatus3, dwCurStatus3));
|
---|
| 399 | #endif
|
---|
| 400 | return false;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 |
|
---|
| 404 | /**
|
---|
| 405 | * Reads a key by name from the host SMC.
|
---|
| 406 | *
|
---|
| 407 | * @returns success indicator.
|
---|
| 408 | * @param pszName The key name, must be exactly 4 chars long.
|
---|
| 409 | * @param pbBuf The output buffer.
|
---|
| 410 | * @param cbBuf The buffer size. Max 32 bytes.
|
---|
| 411 | */
|
---|
| 412 | static bool devR0SmcQueryHostKey(const char *pszName, uint8_t *pbBuf, size_t cbBuf)
|
---|
| 413 | {
|
---|
| 414 | Assert(strlen(pszName) == 4);
|
---|
| 415 | Assert(cbBuf <= 32);
|
---|
| 416 | Assert(cbBuf > 0);
|
---|
| 417 |
|
---|
| 418 | /*
|
---|
| 419 | * Issue the READ command.
|
---|
| 420 | */
|
---|
| 421 | uint32_t cMsSleep = 1;
|
---|
| 422 | for (;;)
|
---|
| 423 | {
|
---|
| 424 | ASMOutU32(SMC_PORT_CMD, SMC_CMD_GET_KEY_VALUE);
|
---|
| 425 | RTThreadSleep(cMsSleep);
|
---|
| 426 | uint8_t bCurState = ASMInU8(SMC_PORT_CMD);
|
---|
| 427 | if ((bCurState & 0xf) == 0xc)
|
---|
| 428 | break;
|
---|
| 429 | cMsSleep <<= 1;
|
---|
| 430 | if (cMsSleep > 64)
|
---|
| 431 | {
|
---|
| 432 | LogRel(("devR0Smc: %s: bCurState=%#x, wanted %#x.\n", "cmd", bCurState, 0xc));
|
---|
| 433 | return false;
|
---|
| 434 | }
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /*
|
---|
| 438 | * Send it the key.
|
---|
| 439 | */
|
---|
| 440 | for (unsigned off = 0; off < 4; off++)
|
---|
| 441 | {
|
---|
| 442 | ASMOutU8(SMC_PORT_DATA, pszName[off]);
|
---|
| 443 | if (!devR0SmcWaitHostState(4, "key"))
|
---|
| 444 | return false;
|
---|
| 445 | }
|
---|
| 446 |
|
---|
| 447 | /*
|
---|
| 448 | * The desired amount of output.
|
---|
| 449 | */
|
---|
| 450 | ASMOutU8(SMC_PORT_DATA, (uint8_t)cbBuf);
|
---|
| 451 |
|
---|
| 452 | /*
|
---|
| 453 | * Read the output.
|
---|
| 454 | */
|
---|
| 455 | for (size_t off = 0; off < cbBuf; off++)
|
---|
| 456 | {
|
---|
| 457 | if (!devR0SmcWaitHostState(5, off ? "data" : "len"))
|
---|
| 458 | return false;
|
---|
| 459 | pbBuf[off] = ASMInU8(SMC_PORT_DATA);
|
---|
| 460 | }
|
---|
| 461 |
|
---|
| 462 | LogRel(("SMC: pbBuf=%.*s\n", cbBuf, pbBuf));
|
---|
| 463 | return true;
|
---|
| 464 | }
|
---|
| 465 |
|
---|
| 466 |
|
---|
| 467 | /**
|
---|
| 468 | * RTOnce callback that initializes g_fHaveOsk and g_abOsk0And1.
|
---|
| 469 | *
|
---|
| 470 | * @returns VINF_SUCCESS.
|
---|
| 471 | * @param pvUserIgnored Ignored.
|
---|
| 472 | */
|
---|
| 473 | static DECLCALLBACK(int) devR0SmcInitOnce(void *pvUserIgnored)
|
---|
| 474 | {
|
---|
| 475 | g_fHaveOsk = devR0SmcQueryHostKey("OSK0", &g_abOsk0And1[0], 32)
|
---|
| 476 | && devR0SmcQueryHostKey("OSK1", &g_abOsk0And1[32], 32);
|
---|
| 477 |
|
---|
| 478 | #if 0
|
---|
| 479 | /*
|
---|
| 480 | * Dump the device registers.
|
---|
| 481 | */
|
---|
| 482 | for (uint16_t uPort = 0x300; uPort < 0x320; uPort ++)
|
---|
| 483 | LogRel(("SMC: %#06x=%#010x w={%#06x, %#06x}, b={%#04x %#04x %#04x %#04x}\n", uPort,
|
---|
| 484 | ASMInU32(uPort), ASMInU16(uPort), ASMInU16(uPort + 2),
|
---|
| 485 | ASMInU8(uPort), ASMInU8(uPort + 1), ASMInU8(uPort +2), ASMInU8(uPort + 3) ));
|
---|
| 486 | #endif
|
---|
| 487 |
|
---|
| 488 | NOREF(pvUserIgnored);
|
---|
| 489 | return VINF_SUCCESS;
|
---|
| 490 | }
|
---|
| 491 |
|
---|
| 492 |
|
---|
| 493 | /**
|
---|
[81519] | 494 | * @interface_method_impl{PDMDEVREGR0,pfnRequest}
|
---|
[48435] | 495 | */
|
---|
[81519] | 496 | static DECLCALLBACK(int) devR0SmcReqHandler(PPDMDEVINS pDevIns, uint32_t uReq, uint64_t uArg)
|
---|
[48435] | 497 | {
|
---|
[81591] | 498 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[48435] | 499 | int rc = VERR_INVALID_FUNCTION;
|
---|
[81519] | 500 | RT_NOREF_PV(uArg);
|
---|
[48435] | 501 |
|
---|
[81519] | 502 | if (uReq == SMC_CALLR0_READ_OSK)
|
---|
[48435] | 503 | {
|
---|
| 504 | rc = RTOnce(&g_SmcR0Once, devR0SmcInitOnce, NULL);
|
---|
| 505 | if ( RT_SUCCESS(rc)
|
---|
| 506 | && g_fHaveOsk)
|
---|
| 507 | {
|
---|
| 508 | AssertCompile(sizeof(g_abOsk0And1) + 1 == sizeof(pThis->szOsk0And1));
|
---|
| 509 | memcpy(pThis->szOsk0And1, g_abOsk0And1, sizeof(pThis->szOsk0And1) - 1);
|
---|
| 510 | pThis->szOsk0And1[sizeof(pThis->szOsk0And1) - 1] = '\0';
|
---|
| 511 | }
|
---|
| 512 | }
|
---|
| 513 | return rc;
|
---|
| 514 | }
|
---|
| 515 |
|
---|
[87284] | 516 | #endif /* IN_RING0 && (AMD64 || X86) */
|
---|
[48435] | 517 |
|
---|
[55502] | 518 | #if defined(IN_RING3) && defined(RT_OS_DARWIN)
|
---|
| 519 |
|
---|
| 520 | /**
|
---|
| 521 | * Preferred method to retrieve the SMC key.
|
---|
| 522 | *
|
---|
| 523 | * @param pabKey where to store the key.
|
---|
| 524 | * @param cbKey size of the buffer.
|
---|
| 525 | */
|
---|
| 526 | static int getSmcKeyOs(char *pabKey, uint32_t cbKey)
|
---|
| 527 | {
|
---|
| 528 | /*
|
---|
| 529 | * Method as described in Amit Singh's article:
|
---|
| 530 | * http://osxbook.com/book/bonus/chapter7/tpmdrmmyth/
|
---|
| 531 | */
|
---|
| 532 | typedef struct
|
---|
| 533 | {
|
---|
| 534 | uint32_t key;
|
---|
| 535 | uint8_t pad0[22];
|
---|
| 536 | uint32_t datasize;
|
---|
| 537 | uint8_t pad1[10];
|
---|
| 538 | uint8_t cmd;
|
---|
| 539 | uint32_t pad2;
|
---|
| 540 | uint8_t data[32];
|
---|
| 541 | } AppleSMCBuffer;
|
---|
| 542 |
|
---|
| 543 | AssertReturn(cbKey >= 65, VERR_INTERNAL_ERROR);
|
---|
| 544 |
|
---|
[100108] | 545 | RT_GCC_NO_WARN_DEPRECATED_BEGIN /* kIOMasterPortDefault: Deprecated since 12.0. */
|
---|
[55502] | 546 | io_service_t service = IOServiceGetMatchingService(kIOMasterPortDefault,
|
---|
| 547 | IOServiceMatching("AppleSMC"));
|
---|
[100108] | 548 | RT_GCC_NO_WARN_DEPRECATED_END
|
---|
[55502] | 549 | if (!service)
|
---|
| 550 | return VERR_NOT_FOUND;
|
---|
| 551 |
|
---|
| 552 | io_connect_t port = (io_connect_t)0;
|
---|
| 553 | kern_return_t kr = IOServiceOpen(service, mach_task_self(), 0, &port);
|
---|
| 554 | IOObjectRelease(service);
|
---|
| 555 |
|
---|
| 556 | if (kr != kIOReturnSuccess)
|
---|
| 557 | return RTErrConvertFromDarwin(kr);
|
---|
| 558 |
|
---|
| 559 | AppleSMCBuffer inputStruct = { 0, {0}, 32, {0}, 5, };
|
---|
| 560 | AppleSMCBuffer outputStruct;
|
---|
| 561 | size_t cbOutputStruct = sizeof(outputStruct);
|
---|
| 562 |
|
---|
| 563 | for (int i = 0; i < 2; i++)
|
---|
| 564 | {
|
---|
[101222] | 565 | inputStruct.key = i == 0 ? RT_MAKE_U32_FROM_MSB_U8('O','S','K','0') : RT_MAKE_U32_FROM_MSB_U8('O','S','K','1');
|
---|
[55502] | 566 | kr = IOConnectCallStructMethod((mach_port_t)port,
|
---|
| 567 | (uint32_t)2,
|
---|
| 568 | (const void *)&inputStruct,
|
---|
| 569 | sizeof(inputStruct),
|
---|
| 570 | (void *)&outputStruct,
|
---|
| 571 | &cbOutputStruct);
|
---|
| 572 | if (kr != kIOReturnSuccess)
|
---|
| 573 | {
|
---|
| 574 | IOServiceClose(port);
|
---|
| 575 | return RTErrConvertFromDarwin(kr);
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | for (int j = 0; j < 32; j++)
|
---|
| 579 | pabKey[j + i*32] = outputStruct.data[j];
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | IOServiceClose(port);
|
---|
| 583 |
|
---|
| 584 | pabKey[64] = 0;
|
---|
| 585 |
|
---|
| 586 | return VINF_SUCCESS;
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | #endif /* IN_RING3 && RT_OS_DARWIN */
|
---|
| 590 |
|
---|
[48435] | 591 |
|
---|
[85121] | 592 | /** @callback_method_impl{FNDEVSMCKEYGETTER, OSK0 and OSK1} */
|
---|
[85197] | 593 | static DECLCALLBACK(uint8_t) scmKeyGetOSKs(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 594 | {
|
---|
[62885] | 595 | RT_NOREF1(bCmd);
|
---|
[48435] | 596 | Assert(SMC4CH_EQ(&pKeyDesc->Key, "OSK0") || SMC4CH_EQ(&pKeyDesc->Key, "OSK1"));
|
---|
| 597 | const char *pszSrc = pThis->szOsk0And1;
|
---|
| 598 | if (SMC4CH_EQ(&pKeyDesc->Key, "OSK1"))
|
---|
| 599 | pszSrc += 32;
|
---|
| 600 | memcpy(pCurKey->Value.ab, pszSrc, 32);
|
---|
| 601 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 |
|
---|
[85121] | 605 | /** @callback_method_impl{FNDEVSMCKEYGETTER, \#KEY} */
|
---|
[85197] | 606 | static DECLCALLBACK(uint8_t) scmKeyGetKeyCount(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 607 | {
|
---|
[62885] | 608 | RT_NOREF3(pThis, bCmd, pKeyDesc);
|
---|
[48435] | 609 | Assert(pKeyDesc == &g_aSmcKeys[SMC_KEYIDX_FIRST_ENUM]);
|
---|
| 610 | uint32_t cKeys = RT_ELEMENTS(g_aSmcKeys) - SMC_KEYIDX_FIRST_ENUM;
|
---|
| 611 | pCurKey->Value.u32 = RT_H2BE_U32(cKeys);
|
---|
| 612 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 613 | }
|
---|
| 614 |
|
---|
| 615 |
|
---|
[85121] | 616 | /** @callback_method_impl{FNDEVSMCKEYGETTER, REV - Source revision.} */
|
---|
[85197] | 617 | static DECLCALLBACK(uint8_t) scmKeyGetRevision(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 618 | {
|
---|
[62885] | 619 | RT_NOREF3(pThis, bCmd, pKeyDesc);
|
---|
[48435] | 620 | #ifdef VBOX_WITH_SMC_2_x
|
---|
| 621 | pCurKey->Value.ab[0] = 0x02;
|
---|
| 622 | pCurKey->Value.ab[1] = 0x03;
|
---|
| 623 | pCurKey->Value.ab[2] = 0x0f;
|
---|
| 624 | pCurKey->Value.ab[3] = 0x00;
|
---|
| 625 | pCurKey->Value.ab[4] = 0x00;
|
---|
| 626 | pCurKey->Value.ab[5] = 0x35;
|
---|
| 627 | #else
|
---|
| 628 | pCurKey->Value.ab[0] = 0x01;
|
---|
| 629 | pCurKey->Value.ab[1] = 0x25;
|
---|
| 630 | pCurKey->Value.ab[2] = 0x0f;
|
---|
| 631 | pCurKey->Value.ab[3] = 0x00;
|
---|
| 632 | pCurKey->Value.ab[4] = 0x00;
|
---|
| 633 | pCurKey->Value.ab[5] = 0x04;
|
---|
| 634 | #endif
|
---|
| 635 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | #ifdef VBOX_WITH_SMC_2_x
|
---|
| 639 |
|
---|
[85121] | 640 | /** @callback_method_impl{FNDEVSMCKEYGETTER, $Adr - SMC address.} */
|
---|
[85197] | 641 | static DECLCALLBACK(uint8_t) scmKeyGetDollarAddress(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 642 | {
|
---|
[62885] | 643 | RT_NOREF3(pThis, bCmd, pKeyDesc);
|
---|
[48435] | 644 | pCurKey->Value.u32 = RT_H2BE_U32(SMC_PORT_FIRST);
|
---|
| 645 | return VINF_SUCCESS;
|
---|
| 646 | }
|
---|
| 647 |
|
---|
| 648 |
|
---|
[85121] | 649 | /** @callback_method_impl{FNDEVSMCKEYGETTER, $Num - Some kind of number.} */
|
---|
[85197] | 650 | static DECLCALLBACK(uint8_t) scmKeyGetDollarNumber(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 651 | {
|
---|
[62885] | 652 | RT_NOREF2(bCmd, pKeyDesc);
|
---|
[48435] | 653 | pCurKey->Value.ab[0] = pThis->bDollaryNumber;
|
---|
| 654 | return VINF_SUCCESS;
|
---|
| 655 | }
|
---|
| 656 |
|
---|
[85121] | 657 | /** @callback_method_impl{FNDEVSMCKEYPUTTER, $Num - Some kind of number.} */
|
---|
[85197] | 658 | static DECLCALLBACK(uint8_t) scmKeyPutDollarNumber(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 659 | {
|
---|
[62885] | 660 | RT_NOREF2(bCmd, pKeyDesc);
|
---|
[48435] | 661 | Log(("scmKeyPutDollarNumber: %#x -> %#x\n", pThis->bDollaryNumber, pCurKey->Value.ab[0]));
|
---|
| 662 | pThis->bDollaryNumber = pCurKey->Value.ab[0];
|
---|
| 663 | return VINF_SUCCESS;
|
---|
| 664 | }
|
---|
| 665 |
|
---|
| 666 | #endif /* VBOX_WITH_SMC_2_x */
|
---|
| 667 |
|
---|
[85121] | 668 | /** @callback_method_impl{FNDEVSMCKEYGETTER, MSSD - Machine Shutdown reason.} */
|
---|
[85197] | 669 | static DECLCALLBACK(uint8_t) scmKeyGetShutdownReason(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 670 | {
|
---|
[62885] | 671 | RT_NOREF2(bCmd, pKeyDesc);
|
---|
[48435] | 672 | pCurKey->Value.ab[0] = pThis->bShutdownReason;
|
---|
| 673 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 |
|
---|
[85121] | 677 | /** @callback_method_impl{FNDEVSMCKEYPUTTER, MSSD - Machine Shutdown reason.} */
|
---|
[85197] | 678 | static DECLCALLBACK(uint8_t) scmKeyPutShutdownReason(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 679 | {
|
---|
[62885] | 680 | RT_NOREF2(bCmd, pKeyDesc);
|
---|
[48435] | 681 | Log(("scmKeyPutShutdownReason: %#x -> %#x\n", pThis->bShutdownReason, pCurKey->Value.ab[0]));
|
---|
| 682 | pThis->bShutdownReason = pCurKey->Value.ab[0];
|
---|
| 683 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 684 | }
|
---|
| 685 |
|
---|
| 686 |
|
---|
[85121] | 687 | /** @callback_method_impl{FNDEVSMCKEYGETTER, MSSD - Ninja timer action job.} */
|
---|
[85197] | 688 | static DECLCALLBACK(uint8_t)
|
---|
| 689 | scmKeyGetNinjaTimerAction(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 690 | {
|
---|
[62885] | 691 | RT_NOREF2(bCmd, pKeyDesc);
|
---|
[48435] | 692 | pCurKey->Value.ab[0] = pThis->bNinjaActionTimerJob;
|
---|
| 693 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 694 | }
|
---|
| 695 |
|
---|
| 696 |
|
---|
[85121] | 697 | /** @callback_method_impl{FNDEVSMCKEYPUTTER, NATJ - Ninja timer action job.} */
|
---|
[85197] | 698 | static DECLCALLBACK(uint8_t)
|
---|
| 699 | scmKeyPutNinjaTimerAction(PDEVSMC pThis, PCDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 700 | {
|
---|
[62885] | 701 | RT_NOREF2(bCmd, pKeyDesc);
|
---|
[48435] | 702 | Log(("scmKeyPutNinjaTimerAction: %#x -> %#x\n", pThis->bNinjaActionTimerJob, pCurKey->Value.ab[0]));
|
---|
| 703 | pThis->bNinjaActionTimerJob = pCurKey->Value.ab[0];
|
---|
| 704 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 705 | }
|
---|
| 706 |
|
---|
| 707 | #ifdef VBOX_WITH_SMC_2_x
|
---|
| 708 |
|
---|
[85121] | 709 | /** @callback_method_impl{FNDEVSMCKEYGETTER, Generic one getter.} */
|
---|
[85197] | 710 | static DECLCALLBACK(uint8_t) scmKeyGetOne(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 711 | {
|
---|
[62885] | 712 | RT_NOREF2(pThis, bCmd);
|
---|
[48435] | 713 | memset(&pCurKey->Value.ab[0], 0, pKeyDesc->cbValue);
|
---|
| 714 | pCurKey->Value.ab[pKeyDesc->cbValue - 1] = 1;
|
---|
| 715 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | #endif /* VBOX_WITH_SMC_2_x */
|
---|
| 719 |
|
---|
[85121] | 720 | /** @callback_method_impl{FNDEVSMCKEYGETTER, Generic zero getter.} */
|
---|
[85197] | 721 | static DECLCALLBACK(uint8_t) scmKeyGetZero(PDEVSMC pThis, PDEVSMCCURKEY pCurKey, uint8_t bCmd, PCDEVSMCKEYDESC pKeyDesc)
|
---|
[48435] | 722 | {
|
---|
[62885] | 723 | RT_NOREF2(pThis, bCmd);
|
---|
[48435] | 724 | memset(&pCurKey->Value.ab[0], 0, pKeyDesc->cbValue);
|
---|
| 725 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 |
|
---|
| 729 | /**
|
---|
| 730 | * Looks up a key and copies its value and attributes into the CurKey.
|
---|
| 731 | *
|
---|
| 732 | * @returns Key index on success, UINT32_MAX on failure.
|
---|
| 733 | * @param uKeyValue The key value (DEVSMC4CHID.u32).
|
---|
| 734 | */
|
---|
[62885] | 735 | static uint32_t smcKeyLookup(uint32_t uKeyValue)
|
---|
[48435] | 736 | {
|
---|
| 737 | uint32_t iKey = RT_ELEMENTS(g_aSmcKeys);
|
---|
| 738 | while (iKey-- > 0)
|
---|
| 739 | if (g_aSmcKeys[iKey].Key.u32 == uKeyValue)
|
---|
| 740 | return iKey;
|
---|
| 741 | return UINT32_MAX;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 |
|
---|
| 745 | /**
|
---|
| 746 | * Looks up a key and copies its value and attributes into the CurKey.
|
---|
| 747 | *
|
---|
| 748 | * @returns Apple SMC Status Code.
|
---|
| 749 | * @param pThis The SMC instance data.
|
---|
| 750 | */
|
---|
| 751 | static uint8_t smcKeyGetByName(PDEVSMC pThis)
|
---|
| 752 | {
|
---|
| 753 | uint8_t bRc;
|
---|
| 754 | #ifdef LOG_ENABLED
|
---|
| 755 | uint32_t const uKeyValueLog = RT_H2LE_U32(pThis->CurKey.Key.u32);
|
---|
| 756 | #endif
|
---|
[62885] | 757 | uint32_t iKey = smcKeyLookup(pThis->CurKey.Key.u32);
|
---|
[48435] | 758 | if (iKey != UINT32_MAX)
|
---|
| 759 | {
|
---|
| 760 | if ( g_aSmcKeys[iKey].cbValue == pThis->CurKey.cbValue
|
---|
| 761 | || !g_aSmcKeys[iKey].cbValue)
|
---|
| 762 | {
|
---|
| 763 | pThis->CurKey.Type = g_aSmcKeys[iKey].Type;
|
---|
| 764 | pThis->CurKey.fAttr = g_aSmcKeys[iKey].fAttr;
|
---|
| 765 | RT_ZERO(pThis->CurKey.Value);
|
---|
| 766 | if (g_aSmcKeys[iKey].pfnGet)
|
---|
| 767 | {
|
---|
| 768 | bRc = g_aSmcKeys[iKey].pfnGet(pThis, &pThis->CurKey, pThis->bCmd, &g_aSmcKeys[iKey]);
|
---|
| 769 | if (bRc == SMC_STATUS_CD_SUCCESS)
|
---|
| 770 | {
|
---|
| 771 | LogFlow(("smcKeyGetByName: key=%4.4s value=%.*Rhxs\n",
|
---|
| 772 | &uKeyValueLog, pThis->CurKey.cbValue, &pThis->CurKey.Value));
|
---|
| 773 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 774 | }
|
---|
| 775 |
|
---|
| 776 | Log(("smcKeyGetByName: key=%4.4s getter failed! bRc=%#x\n", &uKeyValueLog, bRc));
|
---|
| 777 | }
|
---|
| 778 | else
|
---|
| 779 | {
|
---|
| 780 | Log(("smcKeyGetByName: key=%4.4s is not readable!\n", &uKeyValueLog));
|
---|
| 781 | bRc = SMC_STATUS_CD_KEY_NOT_READABLE;
|
---|
| 782 | }
|
---|
| 783 | }
|
---|
| 784 | else
|
---|
| 785 | {
|
---|
| 786 | Log(("smcKeyGetByName: Wrong value size; user=%#x smc=%#x key=%4.4s !\n",
|
---|
| 787 | pThis->CurKey.cbValue, g_aSmcKeys[iKey].cbValue, &uKeyValueLog));
|
---|
| 788 | bRc = SMC_STATUS_CD_KEY_SIZE_MISMATCH;
|
---|
| 789 | }
|
---|
| 790 | }
|
---|
| 791 | else
|
---|
| 792 | {
|
---|
| 793 | Log(("smcKeyGetByName: Key not found! key=%4.4s size=%#x\n", &uKeyValueLog, pThis->CurKey.cbValue));
|
---|
| 794 | bRc = SMC_STATUS_CD_KEY_NOT_FOUND;
|
---|
| 795 | }
|
---|
| 796 |
|
---|
| 797 | RT_ZERO(pThis->CurKey);
|
---|
| 798 | return bRc;
|
---|
| 799 | }
|
---|
| 800 |
|
---|
| 801 |
|
---|
| 802 | /**
|
---|
| 803 | * Looks up a key by index and copies its name (and attributes) into the CurKey.
|
---|
| 804 | *
|
---|
| 805 | * @returns Apple SMC Status Code.
|
---|
| 806 | * @param pThis The SMC instance data.
|
---|
| 807 | */
|
---|
| 808 | static uint8_t smcKeyGetByIndex(PDEVSMC pThis)
|
---|
| 809 | {
|
---|
| 810 | uint8_t bRc;
|
---|
| 811 | uint32_t iKey = RT_BE2H_U32(pThis->CurKey.Key.u32);
|
---|
| 812 | if (iKey < RT_ELEMENTS(g_aSmcKeys) - SMC_KEYIDX_FIRST_ENUM)
|
---|
| 813 | {
|
---|
| 814 | pThis->CurKey.Key = g_aSmcKeys[iKey].Key;
|
---|
| 815 | pThis->CurKey.Type = g_aSmcKeys[iKey].Type;
|
---|
| 816 | pThis->CurKey.fAttr = g_aSmcKeys[iKey].fAttr;
|
---|
| 817 | pThis->CurKey.cbValue = g_aSmcKeys[iKey].cbValue;
|
---|
| 818 | RT_ZERO(pThis->CurKey.Value);
|
---|
| 819 | Log(("smcKeyGetByIndex: %#x -> %c%c%c%c\n", iKey,
|
---|
| 820 | pThis->CurKey.Key.ab[3], pThis->CurKey.Key.ab[2], pThis->CurKey.Key.ab[1], pThis->CurKey.Key.ab[0]));
|
---|
| 821 | bRc = SMC_STATUS_CD_SUCCESS;
|
---|
| 822 | }
|
---|
| 823 | else
|
---|
| 824 | {
|
---|
| 825 | Log(("smcKeyGetByIndex: Key out or range: %#x, max %#x\n", iKey, RT_ELEMENTS(g_aSmcKeys) - SMC_KEYIDX_FIRST_ENUM));
|
---|
| 826 | bRc = SMC_STATUS_CD_KEY_NOT_FOUND;
|
---|
| 827 | }
|
---|
| 828 | return bRc;
|
---|
| 829 | }
|
---|
| 830 |
|
---|
| 831 |
|
---|
| 832 | /**
|
---|
| 833 | * Looks up a key by index and copies its attributes into the CurKey.
|
---|
| 834 | *
|
---|
| 835 | * @returns Apple SMC Status Code.
|
---|
| 836 | * @param pThis The SMC instance data.
|
---|
| 837 | */
|
---|
| 838 | static uint8_t smcKeyGetAttrByName(PDEVSMC pThis)
|
---|
| 839 | {
|
---|
| 840 | uint8_t bRc;
|
---|
| 841 | #ifdef LOG_ENABLED
|
---|
| 842 | uint32_t const uKeyValueLog = RT_H2LE_U32(pThis->CurKey.Key.u32);
|
---|
| 843 | #endif
|
---|
[62885] | 844 | uint32_t iKey = smcKeyLookup(pThis->CurKey.Key.u32);
|
---|
[48435] | 845 | if (iKey != UINT32_MAX)
|
---|
| 846 | {
|
---|
| 847 | pThis->CurKey.Type = g_aSmcKeys[iKey].Type;
|
---|
| 848 | pThis->CurKey.fAttr = g_aSmcKeys[iKey].fAttr;
|
---|
| 849 | pThis->CurKey.cbValue = g_aSmcKeys[iKey].cbValue;
|
---|
| 850 | RT_ZERO(pThis->CurKey.Value);
|
---|
| 851 | if (g_aSmcKeys[iKey].cbValue)
|
---|
| 852 | bRc = SMC_STATUS_CD_SUCCESS;
|
---|
| 853 | else
|
---|
| 854 | bRc = g_aSmcKeys[iKey].pfnGet(pThis, &pThis->CurKey, pThis->bCmd, &g_aSmcKeys[iKey]);
|
---|
| 855 | if (bRc == SMC_STATUS_CD_SUCCESS)
|
---|
| 856 | {
|
---|
| 857 | LogFlow(("smcKeyGetAttrByName: key=%4.4s value=%.*Rhxs\n",
|
---|
| 858 | &uKeyValueLog, pThis->CurKey.cbValue, &pThis->CurKey.Value));
|
---|
| 859 | return SMC_STATUS_CD_SUCCESS;
|
---|
| 860 | }
|
---|
| 861 |
|
---|
| 862 | Log(("smcKeyGetAttrByName: key=%4.4s getter failed! bRc=%#x\n", &uKeyValueLog, bRc));
|
---|
| 863 | }
|
---|
| 864 | else
|
---|
| 865 | {
|
---|
| 866 | Log(("smcKeyGetAttrByName: Key not found! key=%4.4s size=%#x\n", &uKeyValueLog, pThis->CurKey.cbValue));
|
---|
| 867 | bRc = SMC_STATUS_CD_KEY_NOT_FOUND;
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | RT_ZERO(pThis->CurKey);
|
---|
| 871 | return bRc;
|
---|
| 872 | }
|
---|
| 873 |
|
---|
| 874 |
|
---|
| 875 | static uint8_t smcKeyPutPrepare(PDEVSMC pThis)
|
---|
| 876 | {
|
---|
[62885] | 877 | RT_NOREF1(pThis);
|
---|
[48435] | 878 | return 0;
|
---|
| 879 | }
|
---|
| 880 |
|
---|
| 881 | static uint8_t smcKeyPutValue(PDEVSMC pThis)
|
---|
| 882 | {
|
---|
[62885] | 883 | RT_NOREF1(pThis);
|
---|
[48435] | 884 | return 0;
|
---|
| 885 | }
|
---|
| 886 |
|
---|
| 887 |
|
---|
| 888 | /**
|
---|
| 889 | * Data register read.
|
---|
| 890 | *
|
---|
| 891 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 892 | * @param uReg The register number.
|
---|
| 893 | * @param pbValue Where to return the value.
|
---|
| 894 | */
|
---|
[81519] | 895 | static VBOXSTRICTRC smcRegData_r(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue)
|
---|
[48435] | 896 | {
|
---|
[62885] | 897 | RT_NOREF1(uReg);
|
---|
[48435] | 898 | switch (pThis->bCmd)
|
---|
| 899 | {
|
---|
| 900 | case SMC_CMD_GET_KEY_VALUE:
|
---|
| 901 | if ( pThis->u.s.bState == 0x05
|
---|
| 902 | && pThis->offValue < pThis->CurKey.cbValue)
|
---|
| 903 | {
|
---|
| 904 | *pbValue = pThis->CurKey.Value.ab[pThis->offValue];
|
---|
| 905 | if (++pThis->offValue >= pThis->CurKey.cbValue)
|
---|
| 906 | pThis->u.s.bState = 0x00;
|
---|
| 907 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
|
---|
| 908 | }
|
---|
| 909 | else
|
---|
| 910 | {
|
---|
| 911 | Log(("smcRegData_r: Reading too much or at wrong time during SMC_CMD_GET_KEY_INFO! bState=%#x offValue=%#x\n",
|
---|
| 912 | pThis->u.s.bState, pThis->offValue));
|
---|
| 913 | pThis->u.s.bState = 0x00;
|
---|
| 914 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA; /** @todo check status code */
|
---|
| 915 | }
|
---|
| 916 | break;
|
---|
| 917 |
|
---|
| 918 | case SMC_CMD_GET_KEY_INFO:
|
---|
| 919 | if ( pThis->u.s.bState == 0x05
|
---|
| 920 | && pThis->offValue < 6)
|
---|
| 921 | {
|
---|
| 922 | if (pThis->offValue == 0)
|
---|
| 923 | *pbValue = pThis->CurKey.cbValue;
|
---|
| 924 | else if (pThis->offValue < 1 + 4)
|
---|
| 925 | *pbValue = pThis->CurKey.Type.ab[pThis->offValue - 1];
|
---|
| 926 | else
|
---|
| 927 | *pbValue = pThis->CurKey.fAttr;
|
---|
| 928 | if (++pThis->offValue >= 6)
|
---|
| 929 | pThis->u.s.bState = 0x00;
|
---|
| 930 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
|
---|
| 931 | }
|
---|
| 932 | else
|
---|
| 933 | {
|
---|
| 934 | Log(("smcRegData_r: Reading too much or at wrong time during SMC_CMD_GET_KEY_INFO! bState=%#x offValue=%#x\n",
|
---|
| 935 | pThis->u.s.bState, pThis->offValue));
|
---|
| 936 | pThis->u.s.bState = 0x00;
|
---|
| 937 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA; /** @todo check status code */
|
---|
| 938 | }
|
---|
| 939 | break;
|
---|
| 940 |
|
---|
| 941 | case SMC_CMD_GET_KEY_BY_INDEX:
|
---|
| 942 | if ( pThis->u.s.bState == 0x05
|
---|
| 943 | && pThis->offValue < sizeof(pThis->CurKey.Key))
|
---|
| 944 | {
|
---|
| 945 | *pbValue = pThis->CurKey.Key.ab[pThis->offValue];
|
---|
| 946 | if (++pThis->offValue >= sizeof(pThis->CurKey.Key))
|
---|
| 947 | pThis->u.s.bState = 0x00;
|
---|
| 948 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
|
---|
| 949 | }
|
---|
| 950 | else
|
---|
| 951 | {
|
---|
| 952 | Log(("smcRegData_r: Reading too much or at wrong time during GET_KEY_BY_INDEX! bState=%#x offValue=%#x\n",
|
---|
| 953 | pThis->u.s.bState, pThis->offValue));
|
---|
| 954 | pThis->u.s.bState = 0x00;
|
---|
| 955 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA; /** @todo check status code */
|
---|
| 956 | }
|
---|
| 957 | break;
|
---|
| 958 |
|
---|
| 959 | case SMC_CMD_PUT_KEY:
|
---|
| 960 | Log(("smcRegData_r: Attempting to read data during PUT_KEY!\n"));
|
---|
| 961 | *pbValue = 0xff;
|
---|
| 962 | pThis->u.s.bState = 0;
|
---|
| 963 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
|
---|
| 964 | break;
|
---|
| 965 |
|
---|
| 966 | default:
|
---|
| 967 | Log(("smcRegData_r: Unknown command attempts reading data\n"));
|
---|
| 968 | *pbValue = 0xff;
|
---|
| 969 | pThis->u.s.bState = 0;
|
---|
| 970 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
|
---|
| 971 | break;
|
---|
| 972 | }
|
---|
| 973 |
|
---|
| 974 | return VINF_SUCCESS;
|
---|
| 975 | }
|
---|
| 976 |
|
---|
| 977 |
|
---|
| 978 | /**
|
---|
| 979 | * Data register write.
|
---|
| 980 | *
|
---|
| 981 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 982 | * @param uReg The register number.
|
---|
| 983 | * @param bValue The value being written.
|
---|
| 984 | */
|
---|
[81519] | 985 | static VBOXSTRICTRC smcRegData_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
|
---|
[48435] | 986 | {
|
---|
[62885] | 987 | RT_NOREF1(uReg);
|
---|
[48435] | 988 | switch (pThis->bCmd)
|
---|
| 989 | {
|
---|
| 990 | /*
|
---|
| 991 | * Get or put key value.
|
---|
| 992 | *
|
---|
| 993 | * 5 bytes written, first 4 is the key the 5th is the value size. In
|
---|
| 994 | * the case of a put the value bytes are then written, while a get will
|
---|
| 995 | * read the value bytes.
|
---|
| 996 | */
|
---|
| 997 | case SMC_CMD_GET_KEY_VALUE:
|
---|
| 998 | case SMC_CMD_PUT_KEY:
|
---|
| 999 | if (pThis->offKey < 4)
|
---|
| 1000 | {
|
---|
| 1001 | /* Key byte. */
|
---|
| 1002 | pThis->CurKey.Key.ab[pThis->offKey++] = bValue;
|
---|
| 1003 | pThis->u.s.bState = 0x04;
|
---|
| 1004 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
|
---|
| 1005 | }
|
---|
| 1006 | else if (pThis->offKey == 4)
|
---|
| 1007 | {
|
---|
| 1008 | /* Data length. */
|
---|
| 1009 | pThis->u.s.bState = 0;
|
---|
| 1010 | if (bValue <= sizeof(pThis->CurKey.Value))
|
---|
| 1011 | {
|
---|
| 1012 | pThis->CurKey.cbValue = bValue;
|
---|
| 1013 | pThis->offKey = 5;
|
---|
| 1014 | Assert(pThis->offValue == 0);
|
---|
| 1015 |
|
---|
| 1016 | if (pThis->bCmd == SMC_CMD_GET_KEY_VALUE)
|
---|
| 1017 | pThis->u.s.bStatusCode = smcKeyGetByName(pThis);
|
---|
| 1018 | else
|
---|
| 1019 | pThis->u.s.bStatusCode = smcKeyPutPrepare(pThis);
|
---|
| 1020 | if (pThis->u.s.bStatusCode == SMC_STATUS_CD_SUCCESS)
|
---|
| 1021 | pThis->u.s.bState = 0x05;
|
---|
| 1022 | }
|
---|
| 1023 | else
|
---|
| 1024 | {
|
---|
| 1025 | Log(("smcRegData_w: Guest attempts to get/put too many value bytes: %#x (max %#x)!\n",
|
---|
| 1026 | bValue, sizeof(pThis->CurKey.Value)));
|
---|
| 1027 | pThis->u.s.bStatusCode = SMC_STATUS_CD_KEY_SIZE_MISMATCH; /** @todo check this case! */
|
---|
| 1028 | }
|
---|
| 1029 | }
|
---|
| 1030 | else if ( pThis->bCmd == SMC_CMD_PUT_KEY
|
---|
| 1031 | && pThis->offValue < pThis->CurKey.cbValue)
|
---|
| 1032 | {
|
---|
| 1033 | /* More value bytes for put key action. */
|
---|
| 1034 | pThis->CurKey.Value.ab[pThis->offValue++] = bValue;
|
---|
| 1035 | if (pThis->offValue != pThis->CurKey.cbValue)
|
---|
| 1036 | pThis->u.s.bState = 0x05;
|
---|
| 1037 | else
|
---|
| 1038 | {
|
---|
| 1039 | pThis->u.s.bState = 0x00;
|
---|
| 1040 | pThis->u.s.bStatusCode = smcKeyPutValue(pThis);
|
---|
| 1041 | }
|
---|
| 1042 | }
|
---|
| 1043 | else
|
---|
| 1044 | {
|
---|
| 1045 | Log(("smcRegData_w: Writing too much data on %s command!\n", pThis->bCmd == SMC_CMD_PUT_KEY ? "put" : "get"));
|
---|
| 1046 | pThis->u.s.bState = 0x00;
|
---|
| 1047 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
|
---|
| 1048 | }
|
---|
| 1049 | break;
|
---|
| 1050 |
|
---|
| 1051 | /*
|
---|
| 1052 | * Get key info and key by index seems to take action after the last
|
---|
| 1053 | * key char is written. They then both go into a data reading phase.
|
---|
| 1054 | */
|
---|
| 1055 | case SMC_CMD_GET_KEY_INFO:
|
---|
| 1056 | case SMC_CMD_GET_KEY_BY_INDEX:
|
---|
| 1057 | if (pThis->offKey < 4)
|
---|
| 1058 | {
|
---|
| 1059 | pThis->CurKey.Key.ab[pThis->offKey] = bValue;
|
---|
| 1060 | if (++pThis->offKey == 4)
|
---|
| 1061 | {
|
---|
| 1062 | if (pThis->bCmd == SMC_CMD_GET_KEY_BY_INDEX)
|
---|
| 1063 | pThis->u.s.bStatusCode = smcKeyGetByIndex(pThis);
|
---|
| 1064 | else
|
---|
| 1065 | pThis->u.s.bStatusCode = smcKeyGetAttrByName(pThis);
|
---|
| 1066 | pThis->u.s.bState = pThis->u.s.bStatusCode == SMC_STATUS_CD_SUCCESS ? 0x05 : 0x00;
|
---|
| 1067 | }
|
---|
| 1068 | else
|
---|
| 1069 | {
|
---|
| 1070 | pThis->u.s.bState = 0x04;
|
---|
| 1071 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
|
---|
| 1072 | }
|
---|
| 1073 | }
|
---|
| 1074 | else
|
---|
| 1075 | {
|
---|
| 1076 | Log(("smcRegData_w: Writing data beyond 5th byte on get %s command!\n",
|
---|
| 1077 | pThis->bCmd == SMC_CMD_GET_KEY_INFO ? "info" : "by index"));
|
---|
| 1078 | pThis->u.s.bState = 0x00;
|
---|
| 1079 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SPURIOUS_DATA;
|
---|
| 1080 | }
|
---|
| 1081 | break;
|
---|
| 1082 |
|
---|
| 1083 | default:
|
---|
| 1084 | Log(("smcRegData_w: Unknown command %#x!\n", bValue));
|
---|
| 1085 | pThis->u.s.bState = 0x00; /** @todo Check statuses with real HW. */
|
---|
| 1086 | pThis->u.s.bStatusCode = SMC_STATUS_CD_BAD_COMMAND;
|
---|
| 1087 | break;
|
---|
| 1088 | }
|
---|
| 1089 | return VINF_SUCCESS;
|
---|
| 1090 | }
|
---|
| 1091 |
|
---|
| 1092 |
|
---|
| 1093 | /**
|
---|
| 1094 | * Command register write.
|
---|
| 1095 | *
|
---|
| 1096 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 1097 | * @param uReg The register number.
|
---|
| 1098 | * @param bValue The value being written.
|
---|
| 1099 | */
|
---|
[81519] | 1100 | static VBOXSTRICTRC smcRegCmd_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
|
---|
[48435] | 1101 | {
|
---|
| 1102 | LogFlow(("smcRegCmd_w: New command: %#x (old=%#x)\n", bValue, pThis->bCmd)); NOREF(uReg);
|
---|
| 1103 |
|
---|
| 1104 | pThis->bCmd = bValue;
|
---|
| 1105 |
|
---|
| 1106 | /* Validate the command. */
|
---|
| 1107 | switch (bValue)
|
---|
| 1108 | {
|
---|
| 1109 | case SMC_CMD_GET_KEY_VALUE:
|
---|
| 1110 | case SMC_CMD_PUT_KEY:
|
---|
| 1111 | case SMC_CMD_GET_KEY_BY_INDEX:
|
---|
| 1112 | case SMC_CMD_GET_KEY_INFO:
|
---|
| 1113 | pThis->u.s.bState = 0x0c;
|
---|
| 1114 | pThis->u.s.bStatusCode = SMC_STATUS_CD_SUCCESS;
|
---|
| 1115 | break;
|
---|
| 1116 |
|
---|
| 1117 | default:
|
---|
| 1118 | Log(("SMC: Unknown command %#x!\n", bValue));
|
---|
| 1119 | pThis->u.s.bState = 0x00; /** @todo Check state with real HW. */
|
---|
| 1120 | pThis->u.s.bStatusCode = SMC_STATUS_CD_BAD_COMMAND;
|
---|
| 1121 | break;
|
---|
| 1122 | }
|
---|
| 1123 |
|
---|
| 1124 | /* Reset the value/key related state. */
|
---|
| 1125 | pThis->offKey = 0;
|
---|
| 1126 | pThis->offValue = 0;
|
---|
| 1127 | pThis->CurKey.Key.u32 = 0;
|
---|
| 1128 | pThis->CurKey.cbValue = 0;
|
---|
| 1129 |
|
---|
| 1130 | return VINF_SUCCESS;
|
---|
| 1131 | }
|
---|
| 1132 |
|
---|
| 1133 |
|
---|
| 1134 | /**
|
---|
| 1135 | * Generic register write.
|
---|
| 1136 | *
|
---|
| 1137 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 1138 | * @param uReg The register number.
|
---|
| 1139 | * @param bValue The value being written.
|
---|
| 1140 | */
|
---|
[81519] | 1141 | static VBOXSTRICTRC smcRegGen_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
|
---|
[48435] | 1142 | {
|
---|
| 1143 | Log(("smcRegGen_w: %#04x: %#x -> %#x (write)\n", uReg, pThis->u.abRegsRW[uReg], bValue));
|
---|
| 1144 | pThis->u.abRegsRW[uReg] = bValue;
|
---|
| 1145 | return VINF_SUCCESS;
|
---|
| 1146 | }
|
---|
| 1147 |
|
---|
| 1148 |
|
---|
| 1149 | /**
|
---|
| 1150 | * Read from register that isn't writable and reads as 0xFF.
|
---|
| 1151 | *
|
---|
| 1152 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 1153 | * @param uReg The register number.
|
---|
| 1154 | * @param pbValue Where to return the value.
|
---|
| 1155 | */
|
---|
[81519] | 1156 | static VBOXSTRICTRC smcRegGen_r(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue)
|
---|
[48435] | 1157 | {
|
---|
| 1158 | Log(("smcRegGen_r: %#04x: %#x (read)\n", uReg, pThis->u.abRegsRW[uReg]));
|
---|
| 1159 | *pbValue = pThis->u.abRegsRW[uReg];
|
---|
| 1160 | return VINF_SUCCESS;
|
---|
| 1161 | }
|
---|
| 1162 |
|
---|
| 1163 |
|
---|
| 1164 | /**
|
---|
| 1165 | * Write to register that isn't writable and reads as 0xFF.
|
---|
| 1166 | *
|
---|
| 1167 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 1168 | * @param uReg The register number.
|
---|
| 1169 | * @param bValue The value being written.
|
---|
| 1170 | */
|
---|
[81519] | 1171 | static VBOXSTRICTRC smcRegFF_w(PDEVSMC pThis, uint8_t uReg, uint8_t bValue)
|
---|
[48435] | 1172 | {
|
---|
[62885] | 1173 | RT_NOREF3(pThis, uReg, bValue);
|
---|
[48435] | 1174 | Log(("SMC: %#04x: Writing %#x to unknown register!\n", uReg, bValue));
|
---|
| 1175 | return VINF_SUCCESS;
|
---|
| 1176 | }
|
---|
| 1177 |
|
---|
| 1178 |
|
---|
| 1179 | /**
|
---|
| 1180 | * Read from register that isn't writable and reads as 0xFF.
|
---|
| 1181 | *
|
---|
| 1182 | * @returns VINF_SUCCESS or VINF_IOM_R3_IOPORT_WRITE.
|
---|
| 1183 | * @param uReg The register number.
|
---|
| 1184 | * @param pbValue Where to return the value.
|
---|
| 1185 | */
|
---|
[81519] | 1186 | static VBOXSTRICTRC smcRegFF_r(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue)
|
---|
[48435] | 1187 | {
|
---|
[62885] | 1188 | RT_NOREF2(pThis, uReg);
|
---|
[48435] | 1189 | Log(("SMC: %#04x: Reading from unknown register!\n", uReg));
|
---|
| 1190 | *pbValue = 0xff;
|
---|
| 1191 | return VINF_SUCCESS;
|
---|
| 1192 | }
|
---|
| 1193 |
|
---|
| 1194 |
|
---|
| 1195 |
|
---|
| 1196 | /**
|
---|
| 1197 | * SMC register handlers (indexed by relative I/O port).
|
---|
| 1198 | *
|
---|
| 1199 | * The device seems to be all byte registers and will split wider
|
---|
| 1200 | * accesses between registers like if it was MMIO. To better illustrate it
|
---|
| 1201 | * here is the output of the code in devR0SmcInitOnce on a MacPro3,1:
|
---|
| 1202 | * @verbatim
|
---|
| 1203 | * SMC: 0x0300=0xffffff63 w={0xff63, 0xffff}, b={0x63 0xff 0xff 0xff}
|
---|
| 1204 | * SMC: 0x0301=0x0cffffff w={0xffff, 0x0cff}, b={0xff 0xff 0xff 0x0c}
|
---|
| 1205 | * SMC: 0x0302=0xff0cffff w={0xffff, 0xff0c}, b={0xff 0xff 0x0c 0xff}
|
---|
| 1206 | * SMC: 0x0303=0xffff0cff w={0x0cff, 0xffff}, b={0xff 0x0c 0xff 0xff}
|
---|
| 1207 | * SMC: 0x0304=0xffffff0c w={0xff0c, 0xffff}, b={0x0c 0xff 0xff 0xff}
|
---|
| 1208 | * SMC: 0x0305=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1209 | * SMC: 0x0306=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1210 | * SMC: 0x0307=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1211 | * SMC: 0x0308=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1212 | * SMC: 0x0309=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1213 | * SMC: 0x030a=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1214 | * SMC: 0x030b=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1215 | * SMC: 0x030c=0xffffffff w={0xffff, 0xffff}, b={0xff 0xff 0xff 0xff}
|
---|
| 1216 | * SMC: 0x030d=0x00ffffff w={0xffff, 0x00ff}, b={0xff 0xff 0xff 0x00}
|
---|
| 1217 | * SMC: 0x030e=0x0000ffff w={0xffff, 0x0000}, b={0xff 0xff 0x00 0x00}
|
---|
| 1218 | * SMC: 0x030f=0x000000ff w={0x00ff, 0x0000}, b={0xff 0x00 0x00 0x00}
|
---|
| 1219 | * SMC: 0x0310=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1220 | * SMC: 0x0311=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1221 | * SMC: 0x0312=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1222 | * SMC: 0x0313=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1223 | * SMC: 0x0314=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1224 | * SMC: 0x0315=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1225 | * SMC: 0x0316=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1226 | * SMC: 0x0317=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1227 | * SMC: 0x0318=0x00000000 w={0x0000, 0x0000}, b={0x00 0x00 0x00 0x00}
|
---|
| 1228 | * SMC: 0x0319=0xbe000000 w={0x0000, 0xbe00}, b={0x00 0x00 0x00 0xbe}
|
---|
| 1229 | * SMC: 0x031a=0xbabe0000 w={0x0000, 0xbabe}, b={0x00 0x00 0xbe 0xba}
|
---|
| 1230 | * SMC: 0x031b=0x00babe00 w={0xbe00, 0x00ba}, b={0x00 0xbe 0xba 0x00}
|
---|
| 1231 | * SMC: 0x031c=0xbe00babe w={0xbabe, 0xbe00}, b={0xbe 0xba 0x00 0xbe}
|
---|
| 1232 | * SMC: 0x031d=0xffbe00ba w={0x00ba, 0xffbe}, b={0xba 0x00 0xbe 0xff}
|
---|
| 1233 | * SMC: 0x031e=0xffffbe00 w={0xbe00, 0xffff}, b={0x00 0xbe 0xff 0xff}
|
---|
| 1234 | * SMC: 0x031f=0xffffffbe w={0xffbe, 0xffff}, b={0xbe 0xff 0xff 0xff}
|
---|
| 1235 | * @endverbatim
|
---|
| 1236 | *
|
---|
| 1237 | * The last dword is writable (0xbeXXbabe) where in the register at 0x1e is some
|
---|
| 1238 | * kind of status register for qualifying search failures and the like and will
|
---|
| 1239 | * be cleared under certain conditions. The whole dword can be written and read
|
---|
| 1240 | * back unchanged, according to my experiments. The 0x00 and 0x04 registers
|
---|
| 1241 | * does not read back what is written.
|
---|
| 1242 | *
|
---|
| 1243 | * My guess is that the 0xff values indicates ports that are not writable and
|
---|
| 1244 | * hardwired to 0xff, while the other values indicates ports that can be written
|
---|
| 1245 | * to and normally read back as written. I'm not going to push my luck too far
|
---|
| 1246 | * wrt to exact behavior until I see the guest using the registers.
|
---|
| 1247 | */
|
---|
| 1248 | static const struct
|
---|
| 1249 | {
|
---|
[81519] | 1250 | VBOXSTRICTRC (*pfnWrite)(PDEVSMC pThis, uint8_t uReg, uint8_t bValue);
|
---|
| 1251 | VBOXSTRICTRC (*pfnRead)(PDEVSMC pThis, uint8_t uReg, uint8_t *pbValue);
|
---|
[48435] | 1252 | } g_aSmcRegs[SMC_REG_COUNT] =
|
---|
| 1253 | {
|
---|
| 1254 | /* [0x00] = */ { smcRegData_w, smcRegData_r },
|
---|
| 1255 | /* [0x01] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1256 | /* [0x02] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1257 | /* [0x03] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1258 | /* [0x04] = */ { smcRegCmd_w, smcRegGen_r },
|
---|
| 1259 | /* [0x05] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1260 | /* [0x06] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1261 | /* [0x07] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1262 | /* [0x08] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1263 | /* [0x09] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1264 | /* [0x0a] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1265 | /* [0x0b] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1266 | /* [0x0c] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1267 | /* [0x0d] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1268 | /* [0x0e] = */ { smcRegFF_w, smcRegFF_r },
|
---|
| 1269 | /* [0x0f] = */ { smcRegFF_w, smcRegFF_r },
|
---|
[52507] | 1270 | /* [0x10] = */ { smcRegGen_w, smcRegGen_r },
|
---|
[48435] | 1271 | /* [0x11] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1272 | /* [0x12] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1273 | /* [0x13] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1274 | /* [0x14] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1275 | /* [0x15] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1276 | /* [0x16] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1277 | /* [0x17] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1278 | /* [0x18] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1279 | /* [0x19] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1280 | /* [0x1a] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1281 | /* [0x1b] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1282 | /* [0x1c] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1283 | /* [0x1d] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1284 | /* [0x1e] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1285 | /* [0x1f] = */ { smcRegGen_w, smcRegGen_r },
|
---|
| 1286 | };
|
---|
| 1287 |
|
---|
| 1288 |
|
---|
[81519] | 1289 | /**
|
---|
| 1290 | * @callback_method_impl{FNIOMIOPORTNEWOUT}
|
---|
| 1291 | */
|
---|
| 1292 | static DECLCALLBACK(VBOXSTRICTRC) smcIoPortWrite(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t u32, unsigned cb)
|
---|
[48435] | 1293 | {
|
---|
[62885] | 1294 | RT_NOREF1(pvUser);
|
---|
[48435] | 1295 | #ifndef IN_RING3
|
---|
| 1296 | if (cb > 1)
|
---|
[81519] | 1297 | {
|
---|
| 1298 | Log3(("smcIoPortWrite: %#04x write access: %#x (LB %u) -> ring-3\n", offPort, u32, cb));
|
---|
[48435] | 1299 | return VINF_IOM_R3_IOPORT_WRITE;
|
---|
[81519] | 1300 | }
|
---|
[48435] | 1301 | #endif
|
---|
[81519] | 1302 | #ifdef LOG_ENABLED
|
---|
| 1303 | RTIOPORT const offPortLog = offPort;
|
---|
| 1304 | unsigned const cbLog = cb;
|
---|
| 1305 | #endif
|
---|
[48435] | 1306 |
|
---|
| 1307 | /*
|
---|
| 1308 | * The first register, usually only one is accessed.
|
---|
| 1309 | */
|
---|
[81591] | 1310 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[81519] | 1311 | AssertReturn(offPort < RT_ELEMENTS(g_aSmcRegs), VERR_INTERNAL_ERROR_3); /* impossible*/
|
---|
| 1312 | VBOXSTRICTRC rc = g_aSmcRegs[offPort].pfnWrite(pThis, offPort, u32);
|
---|
[48435] | 1313 |
|
---|
| 1314 | /*
|
---|
| 1315 | * On the off chance that multiple registers are being read.
|
---|
| 1316 | */
|
---|
| 1317 | if (cb > 1)
|
---|
| 1318 | {
|
---|
[81519] | 1319 | while (cb > 1 && offPort < SMC_REG_COUNT - 1)
|
---|
[48435] | 1320 | {
|
---|
| 1321 | cb--;
|
---|
[81519] | 1322 | offPort++;
|
---|
[48435] | 1323 | u32 >>= 8;
|
---|
[81519] | 1324 | VBOXSTRICTRC rc2 = g_aSmcRegs[offPort].pfnWrite(pThis, offPort, u32);
|
---|
[48435] | 1325 | if (rc2 != VINF_SUCCESS)
|
---|
| 1326 | {
|
---|
| 1327 | if ( rc == VINF_SUCCESS
|
---|
| 1328 | || (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 1329 | || (rc2 < rc && RT_SUCCESS(rc2) && RT_SUCCESS(rc)))
|
---|
| 1330 | rc = rc2;
|
---|
| 1331 | }
|
---|
| 1332 | }
|
---|
| 1333 | }
|
---|
| 1334 |
|
---|
[81519] | 1335 | LogFlow(("smcIoPortWrite: %#04x write access: %#x (LB %u) rc=%Rrc\n", offPortLog, u32, cbLog, VBOXSTRICTRC_VAL(rc) ));
|
---|
[48435] | 1336 | return rc;
|
---|
| 1337 | }
|
---|
| 1338 |
|
---|
| 1339 |
|
---|
[81519] | 1340 | /**
|
---|
| 1341 | * @callback_method_impl{FNIOMIOPORTNEWIN}
|
---|
| 1342 | */
|
---|
| 1343 | static DECLCALLBACK(VBOXSTRICTRC) smcIoPortRead(PPDMDEVINS pDevIns, void *pvUser, RTIOPORT offPort, uint32_t *pu32, unsigned cb)
|
---|
[48435] | 1344 | {
|
---|
[62885] | 1345 | RT_NOREF1(pvUser);
|
---|
[48435] | 1346 | #ifndef IN_RING3
|
---|
| 1347 | if (cb > 1)
|
---|
| 1348 | return VINF_IOM_R3_IOPORT_READ;
|
---|
| 1349 | #endif
|
---|
| 1350 |
|
---|
[81591] | 1351 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[81519] | 1352 | #ifdef LOG_ENABLED
|
---|
| 1353 | RTIOPORT const offPortLog = offPort;
|
---|
| 1354 | unsigned const cbLog = cb;
|
---|
| 1355 | #endif
|
---|
[48435] | 1356 |
|
---|
| 1357 | /*
|
---|
| 1358 | * The first register, usually only one is accessed.
|
---|
| 1359 | */
|
---|
[81519] | 1360 | AssertReturn(offPort < RT_ELEMENTS(g_aSmcRegs), VERR_INTERNAL_ERROR_3); /* impossible*/
|
---|
| 1361 | Log2(("smcIoPortRead: %#04x read access: LB %u\n", offPort, cb));
|
---|
[48435] | 1362 | uint8_t bValue = 0xff;
|
---|
[81519] | 1363 | VBOXSTRICTRC rc = g_aSmcRegs[offPort].pfnRead(pThis, offPort, &bValue);
|
---|
[48435] | 1364 | *pu32 = bValue;
|
---|
| 1365 |
|
---|
| 1366 | /*
|
---|
| 1367 | * On the off chance that multiple registers are being read.
|
---|
| 1368 | */
|
---|
| 1369 | if (cb > 1)
|
---|
| 1370 | {
|
---|
| 1371 | do
|
---|
| 1372 | {
|
---|
| 1373 | cb--;
|
---|
[81519] | 1374 | offPort++;
|
---|
[48435] | 1375 | bValue = 0xff;
|
---|
[81519] | 1376 | if (offPort < SMC_REG_COUNT)
|
---|
[48435] | 1377 | {
|
---|
[81519] | 1378 | VBOXSTRICTRC rc2 = g_aSmcRegs[offPort].pfnRead(pThis, offPort, &bValue);
|
---|
[48435] | 1379 | if (rc2 != VINF_SUCCESS)
|
---|
| 1380 | {
|
---|
| 1381 | if ( rc == VINF_SUCCESS
|
---|
| 1382 | || (RT_FAILURE(rc2) && RT_SUCCESS(rc))
|
---|
| 1383 | || (rc2 < rc && RT_SUCCESS(rc2) && RT_SUCCESS(rc)))
|
---|
| 1384 | rc = rc2;
|
---|
| 1385 | }
|
---|
| 1386 | }
|
---|
| 1387 | *pu32 |= (uint32_t)bValue << ((4 - cb) * 8);
|
---|
| 1388 | } while (cb > 1);
|
---|
| 1389 | }
|
---|
[81519] | 1390 | LogFlow(("smcIoPortRead: %#04x read access: %#x (LB %u) rc=%Rrc\n", offPortLog, *pu32, cbLog, VBOXSTRICTRC_VAL(rc)));
|
---|
[48435] | 1391 | return rc;
|
---|
| 1392 | }
|
---|
| 1393 |
|
---|
| 1394 | #ifdef IN_RING3
|
---|
| 1395 |
|
---|
| 1396 | /** @callback_method_impl{FNSSMDEVSAVEEXEC} */
|
---|
[81519] | 1397 | static DECLCALLBACK(int) smcR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
|
---|
[48435] | 1398 | {
|
---|
[81591] | 1399 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[62885] | 1400 | RT_NOREF2(pSSM, pThis);
|
---|
[48435] | 1401 |
|
---|
[51520] | 1402 | /** @todo */
|
---|
[48435] | 1403 |
|
---|
| 1404 | return VINF_SUCCESS;
|
---|
| 1405 | }
|
---|
| 1406 |
|
---|
| 1407 |
|
---|
| 1408 | /** @callback_method_impl{FNSSMDEVLOADEXEC} */
|
---|
[81519] | 1409 | static DECLCALLBACK(int) smcR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
|
---|
[48435] | 1410 | {
|
---|
[81591] | 1411 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[48435] | 1412 | Assert(uPass == SSM_PASS_FINAL); NOREF(uPass);
|
---|
[62885] | 1413 | RT_NOREF2(pSSM, pThis);
|
---|
[48435] | 1414 |
|
---|
| 1415 | /* Fend off unsupported versions. */
|
---|
| 1416 | if ( uVersion != SMC_SAVED_STATE_VERSION
|
---|
[63459] | 1417 | #if SMC_SAVED_STATE_VERSION != SMC_SAVED_STATE_VERSION_BAKA
|
---|
[48451] | 1418 | && uVersion != SMC_SAVED_STATE_VERSION_BAKA
|
---|
[63459] | 1419 | #endif
|
---|
[48451] | 1420 | && uVersion != SMC_SAVED_STATE_VERSION_BAKA + 1)
|
---|
[48435] | 1421 | return VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION;
|
---|
| 1422 |
|
---|
| 1423 | /*
|
---|
| 1424 | * Do the actual restoring.
|
---|
| 1425 | */
|
---|
| 1426 | if (uVersion == SMC_SAVED_STATE_VERSION)
|
---|
| 1427 | {
|
---|
[51520] | 1428 | /** @todo */
|
---|
[48435] | 1429 | }
|
---|
| 1430 |
|
---|
| 1431 | return VINF_SUCCESS;
|
---|
| 1432 | }
|
---|
| 1433 |
|
---|
| 1434 |
|
---|
| 1435 | /**
|
---|
| 1436 | * @interface_method_impl{PDMDEVREG,pfnConstruct}
|
---|
| 1437 | */
|
---|
[81519] | 1438 | static DECLCALLBACK(int) smcR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
|
---|
[48435] | 1439 | {
|
---|
| 1440 | PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
|
---|
[81591] | 1441 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[81519] | 1442 | PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
|
---|
[48435] | 1443 |
|
---|
[81519] | 1444 | Assert(iInstance == 0); RT_NOREF1(iInstance);
|
---|
| 1445 |
|
---|
[48435] | 1446 | /*
|
---|
| 1447 | * Init the data.
|
---|
| 1448 | */
|
---|
| 1449 | pThis->bDollaryNumber = 1;
|
---|
| 1450 | pThis->bShutdownReason = 3; /* STOP_CAUSE_POWERKEY_GOOD_CODE */
|
---|
| 1451 |
|
---|
| 1452 | /*
|
---|
| 1453 | * Validate configuration.
|
---|
| 1454 | */
|
---|
| 1455 | PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "DeviceKey|GetKeyFromRealSMC", "");
|
---|
| 1456 |
|
---|
| 1457 | /*
|
---|
| 1458 | * Read configuration.
|
---|
| 1459 | */
|
---|
| 1460 |
|
---|
| 1461 | /* The DeviceKey sets OSK0 and OSK1. */
|
---|
[81519] | 1462 | int rc = pHlp->pfnCFGMQueryStringDef(pCfg, "DeviceKey", pThis->szOsk0And1, sizeof(pThis->szOsk0And1), "");
|
---|
[48435] | 1463 | if (RT_FAILURE(rc))
|
---|
| 1464 | return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
|
---|
| 1465 | N_("Configuration error: Querying \"DeviceKey\" as a string failed"));
|
---|
| 1466 |
|
---|
[55522] | 1467 | /* Query the key from the OS / real hardware if asked to do so. */
|
---|
[48435] | 1468 | bool fGetKeyFromRealSMC;
|
---|
[81519] | 1469 | rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "GetKeyFromRealSMC", &fGetKeyFromRealSMC, false);
|
---|
[48435] | 1470 | if (RT_FAILURE(rc))
|
---|
| 1471 | return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
|
---|
| 1472 | N_("Configuration error: Querying \"GetKeyFromRealSMC\" as a boolean failed"));
|
---|
| 1473 | if (fGetKeyFromRealSMC)
|
---|
| 1474 | {
|
---|
[81519] | 1475 | # ifdef RT_OS_DARWIN
|
---|
[55502] | 1476 | rc = getSmcKeyOs(pThis->szOsk0And1, sizeof(pThis->szOsk0And1));
|
---|
[48435] | 1477 | if (RT_FAILURE(rc))
|
---|
[55502] | 1478 | {
|
---|
[55522] | 1479 | LogRel(("SMC: Retrieving the SMC key from the OS failed (%Rrc), trying to read it from hardware\n", rc));
|
---|
[81519] | 1480 | # endif
|
---|
| 1481 | rc = PDMDevHlpCallR0(pDevIns, SMC_CALLR0_READ_OSK, 0 /*uArg*/);
|
---|
[55522] | 1482 | if (RT_SUCCESS(rc))
|
---|
| 1483 | LogRel(("SMC: Successfully retrieved the SMC key from hardware\n"));
|
---|
| 1484 | else
|
---|
| 1485 | LogRel(("SMC: Retrieving the SMC key from hardware failed(%Rrc)\n", rc));
|
---|
[81519] | 1486 | # ifdef RT_OS_DARWIN
|
---|
[55502] | 1487 | }
|
---|
| 1488 | else
|
---|
| 1489 | LogRel(("SMC: Successfully retrieved the SMC key from the OS\n"));
|
---|
[81519] | 1490 | # endif
|
---|
[55502] | 1491 | if (RT_FAILURE(rc))
|
---|
[48435] | 1492 | return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
|
---|
| 1493 | N_("Failed to query SMC value from the host"));
|
---|
| 1494 | }
|
---|
| 1495 |
|
---|
| 1496 | /*
|
---|
| 1497 | * Register I/O Ports
|
---|
| 1498 | */
|
---|
[81519] | 1499 | rc = PDMDevHlpIoPortCreateAndMap(pDevIns, SMC_PORT_FIRST, SMC_REG_COUNT, smcIoPortWrite, smcIoPortRead,
|
---|
| 1500 | "SMC data port", NULL, &pThis->hIoPorts);
|
---|
[48435] | 1501 | AssertRCReturn(rc, rc);
|
---|
| 1502 |
|
---|
| 1503 | /** @todo Newer versions (2.03) have an MMIO mapping as well (ACPI). */
|
---|
| 1504 |
|
---|
| 1505 |
|
---|
| 1506 | /*
|
---|
| 1507 | * Saved state.
|
---|
| 1508 | */
|
---|
[81519] | 1509 | rc = PDMDevHlpSSMRegister(pDevIns, SMC_SAVED_STATE_VERSION, sizeof(*pThis), smcR3SaveExec, smcR3LoadExec);
|
---|
[48435] | 1510 | if (RT_FAILURE(rc))
|
---|
| 1511 | return rc;
|
---|
| 1512 |
|
---|
| 1513 | return VINF_SUCCESS;
|
---|
| 1514 | }
|
---|
| 1515 |
|
---|
[81519] | 1516 | #else /* !IN_RING3 */
|
---|
[48435] | 1517 |
|
---|
[81519] | 1518 | /**
|
---|
| 1519 | * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
|
---|
| 1520 | */
|
---|
| 1521 | static DECLCALLBACK(int) smcRZConstruct(PPDMDEVINS pDevIns)
|
---|
| 1522 | {
|
---|
| 1523 | PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
|
---|
[81591] | 1524 | PDEVSMC pThis = PDMDEVINS_2_DATA(pDevIns, PDEVSMC);
|
---|
[80531] | 1525 |
|
---|
[81519] | 1526 | int rc = PDMDevHlpIoPortSetUpContext(pDevIns, pThis->hIoPorts, smcIoPortWrite, smcIoPortRead, NULL /*pvUser*/);
|
---|
| 1527 | AssertRCReturn(rc, rc);
|
---|
| 1528 | RT_NOREF(pThis);
|
---|
| 1529 |
|
---|
| 1530 | return VINF_SUCCESS;
|
---|
| 1531 | }
|
---|
| 1532 |
|
---|
| 1533 | #endif /* !IN_RING3 */
|
---|
| 1534 |
|
---|
| 1535 |
|
---|
[48435] | 1536 | /**
|
---|
| 1537 | * The device registration structure.
|
---|
| 1538 | */
|
---|
| 1539 | const PDMDEVREG g_DeviceSmc =
|
---|
| 1540 | {
|
---|
[80531] | 1541 | /* .u32Version = */ PDM_DEVREG_VERSION,
|
---|
| 1542 | /* .uReserved0 = */ 0,
|
---|
| 1543 | /* .szName = */ "smc",
|
---|
[81519] | 1544 | /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE,
|
---|
[80531] | 1545 | /* .fClass = */ PDM_DEVREG_CLASS_ARCH,
|
---|
| 1546 | /* .cMaxInstances = */ 1,
|
---|
| 1547 | /* .uSharedVersion = */ 42,
|
---|
| 1548 | /* .cbInstanceShared = */ sizeof(DEVSMC),
|
---|
| 1549 | /* .cbInstanceCC = */ 0,
|
---|
| 1550 | /* .cbInstanceRC = */ 0,
|
---|
[80703] | 1551 | /* .cMaxPciDevices = */ 0,
|
---|
[80704] | 1552 | /* .cMaxMsixVectors = */ 0,
|
---|
[80531] | 1553 | /* .pszDescription = */ "Apple System Management Controller",
|
---|
| 1554 | #if defined(IN_RING3)
|
---|
| 1555 | /* .pszRCMod = */ "VBoxDDRC.rc",
|
---|
| 1556 | /* .pszR0Mod = */ "VBoxDDR0.r0",
|
---|
[81519] | 1557 | /* .pfnConstruct = */ smcR3Construct,
|
---|
[80531] | 1558 | /* .pfnDestruct = */ NULL,
|
---|
| 1559 | /* .pfnRelocate = */ NULL,
|
---|
| 1560 | /* .pfnMemSetup = */ NULL,
|
---|
| 1561 | /* .pfnPowerOn = */ NULL,
|
---|
| 1562 | /* .pfnReset = */ NULL,
|
---|
| 1563 | /* .pfnSuspend = */ NULL,
|
---|
| 1564 | /* .pfnResume = */ NULL,
|
---|
| 1565 | /* .pfnAttach = */ NULL,
|
---|
| 1566 | /* .pfnDetach = */ NULL,
|
---|
| 1567 | /* .pfnQueryInterface = */ NULL,
|
---|
| 1568 | /* .pfnInitComplete = */ NULL,
|
---|
| 1569 | /* .pfnPowerOff = */ NULL,
|
---|
| 1570 | /* .pfnSoftReset = */ NULL,
|
---|
| 1571 | /* .pfnReserved0 = */ NULL,
|
---|
| 1572 | /* .pfnReserved1 = */ NULL,
|
---|
| 1573 | /* .pfnReserved2 = */ NULL,
|
---|
| 1574 | /* .pfnReserved3 = */ NULL,
|
---|
| 1575 | /* .pfnReserved4 = */ NULL,
|
---|
| 1576 | /* .pfnReserved5 = */ NULL,
|
---|
| 1577 | /* .pfnReserved6 = */ NULL,
|
---|
| 1578 | /* .pfnReserved7 = */ NULL,
|
---|
| 1579 | #elif defined(IN_RING0)
|
---|
| 1580 | /* .pfnEarlyConstruct = */ NULL,
|
---|
[81519] | 1581 | /* .pfnConstruct = */ smcRZConstruct,
|
---|
[80531] | 1582 | /* .pfnDestruct = */ NULL,
|
---|
| 1583 | /* .pfnFinalDestruct = */ NULL,
|
---|
[87284] | 1584 | # if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
|
---|
[81519] | 1585 | /* .pfnRequest = */ devR0SmcReqHandler,
|
---|
[87284] | 1586 | # else
|
---|
| 1587 | /* .pfnRequest = */ NULL,
|
---|
| 1588 | # endif
|
---|
[80531] | 1589 | /* .pfnReserved0 = */ NULL,
|
---|
| 1590 | /* .pfnReserved1 = */ NULL,
|
---|
| 1591 | /* .pfnReserved2 = */ NULL,
|
---|
| 1592 | /* .pfnReserved3 = */ NULL,
|
---|
| 1593 | /* .pfnReserved4 = */ NULL,
|
---|
| 1594 | /* .pfnReserved5 = */ NULL,
|
---|
| 1595 | /* .pfnReserved6 = */ NULL,
|
---|
| 1596 | /* .pfnReserved7 = */ NULL,
|
---|
| 1597 | #elif defined(IN_RC)
|
---|
[81519] | 1598 | /* .pfnConstruct = */ smcRZConstruct,
|
---|
[80531] | 1599 | /* .pfnReserved0 = */ NULL,
|
---|
| 1600 | /* .pfnReserved1 = */ NULL,
|
---|
| 1601 | /* .pfnReserved2 = */ NULL,
|
---|
| 1602 | /* .pfnReserved3 = */ NULL,
|
---|
| 1603 | /* .pfnReserved4 = */ NULL,
|
---|
| 1604 | /* .pfnReserved5 = */ NULL,
|
---|
| 1605 | /* .pfnReserved6 = */ NULL,
|
---|
| 1606 | /* .pfnReserved7 = */ NULL,
|
---|
| 1607 | #else
|
---|
| 1608 | # error "Not in IN_RING3, IN_RING0 or IN_RC!"
|
---|
| 1609 | #endif
|
---|
| 1610 | /* .u32VersionEnd = */ PDM_DEVREG_VERSION
|
---|
[48435] | 1611 | };
|
---|
| 1612 |
|
---|
[52508] | 1613 | #endif /* VBOX_DEVICE_STRUCT_TESTCASE */
|
---|