2010-06-30 12:41:12 +04:00
|
|
|
/*
|
|
|
|
* Apple SMC controller
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Alexander Graf
|
|
|
|
*
|
|
|
|
* Authors: Alexander Graf <agraf@suse.de>
|
|
|
|
* Susanne Graf <suse@csgraf.de>
|
|
|
|
*
|
|
|
|
* 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
|
2020-10-23 15:44:24 +03:00
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
2010-06-30 12:41:12 +04:00
|
|
|
*
|
|
|
|
* 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* *****************************************************************
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-01-26 21:17:17 +03:00
|
|
|
#include "qemu/osdep.h"
|
2013-02-05 20:06:20 +04:00
|
|
|
#include "hw/isa/isa.h"
|
2019-08-12 08:23:51 +03:00
|
|
|
#include "hw/qdev-properties.h"
|
2012-11-28 15:06:30 +04:00
|
|
|
#include "ui/console.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2012-12-17 21:20:00 +04:00
|
|
|
#include "qemu/timer.h"
|
2020-09-03 23:43:22 +03:00
|
|
|
#include "qom/object.h"
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2021-10-04 11:46:37 +03:00
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
#include <IOKit/IOKitLib.h>
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kSMCSuccess = 0x00,
|
|
|
|
kSMCKeyNotFound = 0x84
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
kSMCUserClientOpen = 0x00,
|
|
|
|
kSMCUserClientClose = 0x01,
|
|
|
|
kSMCHandleYPCEvent = 0x02,
|
|
|
|
kSMCReadKey = 0x05,
|
|
|
|
kSMCGetKeyInfo = 0x09
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct SMCVersion {
|
|
|
|
uint8_t major;
|
|
|
|
uint8_t minor;
|
|
|
|
uint8_t build;
|
|
|
|
uint8_t reserved;
|
|
|
|
uint16_t release;
|
|
|
|
} SMCVersion;
|
|
|
|
|
|
|
|
typedef struct SMCPLimitData {
|
|
|
|
uint16_t version;
|
|
|
|
uint16_t length;
|
|
|
|
uint32_t cpuPLimit;
|
|
|
|
uint32_t gpuPLimit;
|
|
|
|
uint32_t memPLimit;
|
|
|
|
} SMCPLimitData;
|
|
|
|
|
|
|
|
typedef struct SMCKeyInfoData {
|
|
|
|
IOByteCount dataSize;
|
|
|
|
uint32_t dataType;
|
|
|
|
uint8_t dataAttributes;
|
|
|
|
} SMCKeyInfoData;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t key;
|
|
|
|
SMCVersion vers;
|
|
|
|
SMCPLimitData pLimitData;
|
|
|
|
SMCKeyInfoData keyInfo;
|
|
|
|
uint8_t result;
|
|
|
|
uint8_t status;
|
|
|
|
uint8_t data8;
|
|
|
|
uint32_t data32;
|
|
|
|
uint8_t bytes[32];
|
|
|
|
} SMCParamStruct;
|
|
|
|
|
|
|
|
static IOReturn smc_call_struct_method(uint32_t selector,
|
|
|
|
SMCParamStruct *inputStruct,
|
|
|
|
SMCParamStruct *outputStruct)
|
|
|
|
{
|
|
|
|
IOReturn ret;
|
|
|
|
|
|
|
|
size_t inputStructCnt = sizeof(SMCParamStruct);
|
|
|
|
size_t outputStructCnt = sizeof(SMCParamStruct);
|
|
|
|
|
|
|
|
io_service_t smcService = IO_OBJECT_NULL;
|
|
|
|
io_connect_t smcConnect = IO_OBJECT_NULL;
|
|
|
|
|
|
|
|
smcService = IOServiceGetMatchingService(kIOMasterPortDefault,
|
|
|
|
IOServiceMatching("AppleSMC"));
|
|
|
|
if (smcService == IO_OBJECT_NULL) {
|
|
|
|
ret = kIOReturnNotFound;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = IOServiceOpen(smcService, mach_task_self(), 1, &smcConnect);
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
smcConnect = IO_OBJECT_NULL;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (smcConnect == IO_OBJECT_NULL) {
|
|
|
|
ret = kIOReturnError;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = IOConnectCallMethod(smcConnect, kSMCUserClientOpen,
|
|
|
|
NULL, 0, NULL, 0,
|
|
|
|
NULL, NULL, NULL, NULL);
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = IOConnectCallStructMethod(smcConnect, selector,
|
|
|
|
inputStruct, inputStructCnt,
|
|
|
|
outputStruct, &outputStructCnt);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
if (smcConnect != IO_OBJECT_NULL) {
|
|
|
|
IOConnectCallMethod(smcConnect, kSMCUserClientClose,
|
|
|
|
NULL, 0, NULL, 0, NULL,
|
|
|
|
NULL, NULL, NULL);
|
|
|
|
IOServiceClose(smcConnect);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static IOReturn smc_read_key(uint32_t key,
|
|
|
|
uint8_t *bytes,
|
|
|
|
IOByteCount *dataSize)
|
|
|
|
{
|
|
|
|
IOReturn ret;
|
|
|
|
|
|
|
|
SMCParamStruct inputStruct;
|
|
|
|
SMCParamStruct outputStruct;
|
|
|
|
|
|
|
|
if (key == 0 || bytes == NULL) {
|
|
|
|
ret = kIOReturnCannotWire;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* determine key's data size */
|
|
|
|
memset(&inputStruct, 0, sizeof(SMCParamStruct));
|
|
|
|
inputStruct.data8 = kSMCGetKeyInfo;
|
|
|
|
inputStruct.key = key;
|
|
|
|
|
|
|
|
memset(&outputStruct, 0, sizeof(SMCParamStruct));
|
|
|
|
ret = smc_call_struct_method(kSMCHandleYPCEvent, &inputStruct, &outputStruct);
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (outputStruct.result == kSMCKeyNotFound) {
|
|
|
|
ret = kIOReturnNotFound;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (outputStruct.result != kSMCSuccess) {
|
|
|
|
ret = kIOReturnInternalError;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get key value */
|
|
|
|
memset(&inputStruct, 0, sizeof(SMCParamStruct));
|
|
|
|
inputStruct.data8 = kSMCReadKey;
|
|
|
|
inputStruct.key = key;
|
|
|
|
inputStruct.keyInfo.dataSize = outputStruct.keyInfo.dataSize;
|
|
|
|
|
|
|
|
memset(&outputStruct, 0, sizeof(SMCParamStruct));
|
|
|
|
ret = smc_call_struct_method(kSMCHandleYPCEvent, &inputStruct, &outputStruct);
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (outputStruct.result == kSMCKeyNotFound) {
|
|
|
|
ret = kIOReturnNotFound;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (outputStruct.result != kSMCSuccess) {
|
|
|
|
ret = kIOReturnInternalError;
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(bytes, 0, *dataSize);
|
|
|
|
if (*dataSize > inputStruct.keyInfo.dataSize) {
|
|
|
|
*dataSize = inputStruct.keyInfo.dataSize;
|
|
|
|
}
|
|
|
|
memcpy(bytes, outputStruct.bytes, *dataSize);
|
|
|
|
|
|
|
|
exit:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-06-30 12:41:12 +04:00
|
|
|
/* #define DEBUG_SMC */
|
|
|
|
|
|
|
|
#define APPLESMC_DEFAULT_IOBASE 0x300
|
|
|
|
|
2017-06-16 21:55:14 +03:00
|
|
|
enum {
|
|
|
|
APPLESMC_DATA_PORT = 0x00,
|
|
|
|
APPLESMC_CMD_PORT = 0x04,
|
2017-06-16 21:55:15 +03:00
|
|
|
APPLESMC_ERR_PORT = 0x1e,
|
2017-06-16 21:55:14 +03:00
|
|
|
APPLESMC_NUM_PORTS = 0x20,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
APPLESMC_READ_CMD = 0x10,
|
|
|
|
APPLESMC_WRITE_CMD = 0x11,
|
|
|
|
APPLESMC_GET_KEY_BY_INDEX_CMD = 0x12,
|
|
|
|
APPLESMC_GET_KEY_TYPE_CMD = 0x13,
|
|
|
|
};
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
enum {
|
|
|
|
APPLESMC_ST_CMD_DONE = 0x00,
|
|
|
|
APPLESMC_ST_DATA_READY = 0x01,
|
|
|
|
APPLESMC_ST_BUSY = 0x02,
|
|
|
|
APPLESMC_ST_ACK = 0x04,
|
|
|
|
APPLESMC_ST_NEW_CMD = 0x08,
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
APPLESMC_ST_1E_CMD_INTRUPTED = 0x80,
|
|
|
|
APPLESMC_ST_1E_STILL_BAD_CMD = 0x81,
|
|
|
|
APPLESMC_ST_1E_BAD_CMD = 0x82,
|
|
|
|
APPLESMC_ST_1E_NOEXIST = 0x84,
|
|
|
|
APPLESMC_ST_1E_WRITEONLY = 0x85,
|
|
|
|
APPLESMC_ST_1E_READONLY = 0x86,
|
|
|
|
APPLESMC_ST_1E_BAD_INDEX = 0xb8,
|
|
|
|
};
|
|
|
|
|
2010-06-30 12:41:12 +04:00
|
|
|
#ifdef DEBUG_SMC
|
|
|
|
#define smc_debug(...) fprintf(stderr, "AppleSMC: " __VA_ARGS__)
|
|
|
|
#else
|
2017-06-16 21:55:14 +03:00
|
|
|
#define smc_debug(...) do { } while (0)
|
2010-06-30 12:41:12 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static char default_osk[64] = "This is a dummy key. Enter the real key "
|
|
|
|
"using the -osk parameter";
|
|
|
|
|
|
|
|
struct AppleSMCData {
|
|
|
|
uint8_t len;
|
|
|
|
const char *key;
|
|
|
|
const char *data;
|
|
|
|
QLIST_ENTRY(AppleSMCData) node;
|
|
|
|
};
|
|
|
|
|
2020-09-16 21:25:19 +03:00
|
|
|
OBJECT_DECLARE_SIMPLE_TYPE(AppleSMCState, APPLE_SMC)
|
2013-04-28 00:18:36 +04:00
|
|
|
|
|
|
|
struct AppleSMCState {
|
|
|
|
ISADevice parent_obj;
|
|
|
|
|
2013-06-22 10:06:55 +04:00
|
|
|
MemoryRegion io_data;
|
|
|
|
MemoryRegion io_cmd;
|
2017-06-16 21:55:15 +03:00
|
|
|
MemoryRegion io_err;
|
2010-06-30 12:41:12 +04:00
|
|
|
uint32_t iobase;
|
|
|
|
uint8_t cmd;
|
|
|
|
uint8_t status;
|
2017-06-16 21:55:15 +03:00
|
|
|
uint8_t status_1e;
|
|
|
|
uint8_t last_ret;
|
2017-06-16 21:55:14 +03:00
|
|
|
char key[4];
|
2010-06-30 12:41:12 +04:00
|
|
|
uint8_t read_pos;
|
|
|
|
uint8_t data_len;
|
|
|
|
uint8_t data_pos;
|
|
|
|
uint8_t data[255];
|
|
|
|
char *osk;
|
|
|
|
QLIST_HEAD(, AppleSMCData) data_def;
|
|
|
|
};
|
|
|
|
|
2013-06-22 10:06:55 +04:00
|
|
|
static void applesmc_io_cmd_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned size)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
2013-04-28 00:18:36 +04:00
|
|
|
AppleSMCState *s = opaque;
|
2017-06-16 21:55:15 +03:00
|
|
|
uint8_t status = s->status & 0x0f;
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
smc_debug("CMD received: 0x%02x\n", (uint8_t)val);
|
2017-06-16 21:55:14 +03:00
|
|
|
switch (val) {
|
|
|
|
case APPLESMC_READ_CMD:
|
2017-06-16 21:55:15 +03:00
|
|
|
/* did last command run through OK? */
|
|
|
|
if (status == APPLESMC_ST_CMD_DONE || status == APPLESMC_ST_NEW_CMD) {
|
|
|
|
s->cmd = val;
|
|
|
|
s->status = APPLESMC_ST_NEW_CMD | APPLESMC_ST_ACK;
|
|
|
|
} else {
|
|
|
|
smc_debug("ERROR: previous command interrupted!\n");
|
|
|
|
s->status = APPLESMC_ST_NEW_CMD;
|
|
|
|
s->status_1e = APPLESMC_ST_1E_CMD_INTRUPTED;
|
|
|
|
}
|
2017-06-16 21:55:14 +03:00
|
|
|
break;
|
2017-06-16 21:55:15 +03:00
|
|
|
default:
|
|
|
|
smc_debug("UNEXPECTED CMD 0x%02x\n", (uint8_t)val);
|
|
|
|
s->status = APPLESMC_ST_NEW_CMD;
|
|
|
|
s->status_1e = APPLESMC_ST_1E_BAD_CMD;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
s->read_pos = 0;
|
|
|
|
s->data_pos = 0;
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
static struct AppleSMCData *applesmc_find_key(AppleSMCState *s)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
|
|
|
struct AppleSMCData *d;
|
|
|
|
|
|
|
|
QLIST_FOREACH(d, &s->data_def, node) {
|
|
|
|
if (!memcmp(d->key, s->key, 4)) {
|
2017-06-16 21:55:15 +03:00
|
|
|
return d;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
}
|
2017-06-16 21:55:15 +03:00
|
|
|
return NULL;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
|
2013-06-22 10:06:55 +04:00
|
|
|
static void applesmc_io_data_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned size)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
2013-04-28 00:18:36 +04:00
|
|
|
AppleSMCState *s = opaque;
|
2017-06-16 21:55:15 +03:00
|
|
|
struct AppleSMCData *d;
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
smc_debug("DATA received: 0x%02x\n", (uint8_t)val);
|
2017-06-16 21:55:14 +03:00
|
|
|
switch (s->cmd) {
|
|
|
|
case APPLESMC_READ_CMD:
|
2017-06-16 21:55:15 +03:00
|
|
|
if ((s->status & 0x0f) == APPLESMC_ST_CMD_DONE) {
|
|
|
|
break;
|
|
|
|
}
|
2017-06-16 21:55:14 +03:00
|
|
|
if (s->read_pos < 4) {
|
|
|
|
s->key[s->read_pos] = val;
|
2017-06-16 21:55:15 +03:00
|
|
|
s->status = APPLESMC_ST_ACK;
|
2017-06-16 21:55:14 +03:00
|
|
|
} else if (s->read_pos == 4) {
|
2017-06-16 21:55:15 +03:00
|
|
|
d = applesmc_find_key(s);
|
|
|
|
if (d != NULL) {
|
|
|
|
memcpy(s->data, d->data, d->len);
|
|
|
|
s->data_len = d->len;
|
|
|
|
s->data_pos = 0;
|
|
|
|
s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
|
|
|
|
s->status_1e = APPLESMC_ST_CMD_DONE; /* clear on valid key */
|
|
|
|
} else {
|
|
|
|
smc_debug("READ_CMD: key '%c%c%c%c' not found!\n",
|
|
|
|
s->key[0], s->key[1], s->key[2], s->key[3]);
|
|
|
|
s->status = APPLESMC_ST_CMD_DONE;
|
|
|
|
s->status_1e = APPLESMC_ST_1E_NOEXIST;
|
|
|
|
}
|
2017-06-16 21:55:14 +03:00
|
|
|
}
|
|
|
|
s->read_pos++;
|
|
|
|
break;
|
2017-06-16 21:55:15 +03:00
|
|
|
default:
|
|
|
|
s->status = APPLESMC_ST_CMD_DONE;
|
|
|
|
s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
static void applesmc_io_err_write(void *opaque, hwaddr addr, uint64_t val,
|
|
|
|
unsigned size)
|
|
|
|
{
|
|
|
|
smc_debug("ERR_CODE received: 0x%02x, ignoring!\n", (uint8_t)val);
|
|
|
|
/* NOTE: writing to the error port not supported! */
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:55:14 +03:00
|
|
|
static uint64_t applesmc_io_data_read(void *opaque, hwaddr addr, unsigned size)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
2013-04-28 00:18:36 +04:00
|
|
|
AppleSMCState *s = opaque;
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:14 +03:00
|
|
|
switch (s->cmd) {
|
|
|
|
case APPLESMC_READ_CMD:
|
2017-06-16 21:55:15 +03:00
|
|
|
if (!(s->status & APPLESMC_ST_DATA_READY)) {
|
|
|
|
break;
|
|
|
|
}
|
2017-06-16 21:55:14 +03:00
|
|
|
if (s->data_pos < s->data_len) {
|
2017-06-16 21:55:15 +03:00
|
|
|
s->last_ret = s->data[s->data_pos];
|
|
|
|
smc_debug("READ '%c%c%c%c'[%d] = %02x\n",
|
|
|
|
s->key[0], s->key[1], s->key[2], s->key[3],
|
|
|
|
s->data_pos, s->last_ret);
|
2017-06-16 21:55:14 +03:00
|
|
|
s->data_pos++;
|
|
|
|
if (s->data_pos == s->data_len) {
|
2017-06-16 21:55:15 +03:00
|
|
|
s->status = APPLESMC_ST_CMD_DONE;
|
|
|
|
smc_debug("READ '%c%c%c%c' Len=%d complete!\n",
|
|
|
|
s->key[0], s->key[1], s->key[2], s->key[3],
|
|
|
|
s->data_len);
|
2017-06-16 21:55:14 +03:00
|
|
|
} else {
|
2017-06-16 21:55:15 +03:00
|
|
|
s->status = APPLESMC_ST_ACK | APPLESMC_ST_DATA_READY;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
2017-06-16 21:55:14 +03:00
|
|
|
}
|
2017-06-16 21:55:15 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
s->status = APPLESMC_ST_CMD_DONE;
|
|
|
|
s->status_1e = APPLESMC_ST_1E_STILL_BAD_CMD;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
2017-06-16 21:55:15 +03:00
|
|
|
smc_debug("DATA sent: 0x%02x\n", s->last_ret);
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
return s->last_ret;
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
|
2017-06-16 21:55:14 +03:00
|
|
|
static uint64_t applesmc_io_cmd_read(void *opaque, hwaddr addr, unsigned size)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
2013-04-28 00:18:36 +04:00
|
|
|
AppleSMCState *s = opaque;
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
smc_debug("CMD sent: 0x%02x\n", s->status);
|
2010-06-30 12:41:12 +04:00
|
|
|
return s->status;
|
|
|
|
}
|
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
static uint64_t applesmc_io_err_read(void *opaque, hwaddr addr, unsigned size)
|
|
|
|
{
|
|
|
|
AppleSMCState *s = opaque;
|
|
|
|
|
|
|
|
/* NOTE: read does not clear the 1e status */
|
|
|
|
smc_debug("ERR_CODE sent: 0x%02x\n", s->status_1e);
|
|
|
|
return s->status_1e;
|
|
|
|
}
|
|
|
|
|
2013-04-28 00:18:36 +04:00
|
|
|
static void applesmc_add_key(AppleSMCState *s, const char *key,
|
2010-06-30 12:41:12 +04:00
|
|
|
int len, const char *data)
|
|
|
|
{
|
|
|
|
struct AppleSMCData *def;
|
|
|
|
|
2011-08-21 07:09:37 +04:00
|
|
|
def = g_malloc0(sizeof(struct AppleSMCData));
|
2010-06-30 12:41:12 +04:00
|
|
|
def->key = key;
|
|
|
|
def->len = len;
|
|
|
|
def->data = data;
|
|
|
|
|
|
|
|
QLIST_INSERT_HEAD(&s->data_def, def, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void qdev_applesmc_isa_reset(DeviceState *dev)
|
|
|
|
{
|
2013-04-28 00:18:36 +04:00
|
|
|
AppleSMCState *s = APPLE_SMC(dev);
|
2010-06-30 12:41:12 +04:00
|
|
|
struct AppleSMCData *d, *next;
|
|
|
|
|
|
|
|
/* Remove existing entries */
|
|
|
|
QLIST_FOREACH_SAFE(d, &s->data_def, node, next) {
|
|
|
|
QLIST_REMOVE(d, node);
|
|
|
|
}
|
2017-06-16 21:55:15 +03:00
|
|
|
s->status = 0x00;
|
|
|
|
s->status_1e = 0x00;
|
|
|
|
s->last_ret = 0x00;
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2011-03-21 13:33:21 +03:00
|
|
|
applesmc_add_key(s, "REV ", 6, "\x01\x13\x0f\x00\x00\x03");
|
2010-06-30 12:41:12 +04:00
|
|
|
applesmc_add_key(s, "OSK0", 32, s->osk);
|
|
|
|
applesmc_add_key(s, "OSK1", 32, s->osk + 32);
|
|
|
|
applesmc_add_key(s, "NATJ", 1, "\0");
|
|
|
|
applesmc_add_key(s, "MSSP", 1, "\0");
|
|
|
|
applesmc_add_key(s, "MSSD", 1, "\0x3");
|
|
|
|
}
|
|
|
|
|
2013-06-22 10:06:55 +04:00
|
|
|
static const MemoryRegionOps applesmc_data_io_ops = {
|
|
|
|
.write = applesmc_io_data_write,
|
|
|
|
.read = applesmc_io_data_read,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
static const MemoryRegionOps applesmc_cmd_io_ops = {
|
|
|
|
.write = applesmc_io_cmd_write,
|
|
|
|
.read = applesmc_io_cmd_read,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
static const MemoryRegionOps applesmc_err_io_ops = {
|
|
|
|
.write = applesmc_io_err_write,
|
|
|
|
.read = applesmc_io_err_read,
|
|
|
|
.endianness = DEVICE_NATIVE_ENDIAN,
|
|
|
|
.impl = {
|
|
|
|
.min_access_size = 1,
|
|
|
|
.max_access_size = 1,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2012-11-25 05:37:14 +04:00
|
|
|
static void applesmc_isa_realize(DeviceState *dev, Error **errp)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
2013-04-28 00:18:36 +04:00
|
|
|
AppleSMCState *s = APPLE_SMC(dev);
|
2021-10-04 11:46:37 +03:00
|
|
|
bool valid_key = false;
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&s->io_data, OBJECT(s), &applesmc_data_io_ops, s,
|
2017-06-16 21:55:16 +03:00
|
|
|
"applesmc-data", 1);
|
2013-06-22 10:06:55 +04:00
|
|
|
isa_register_ioport(&s->parent_obj, &s->io_data,
|
|
|
|
s->iobase + APPLESMC_DATA_PORT);
|
|
|
|
|
2013-06-07 05:25:08 +04:00
|
|
|
memory_region_init_io(&s->io_cmd, OBJECT(s), &applesmc_cmd_io_ops, s,
|
2017-06-16 21:55:16 +03:00
|
|
|
"applesmc-cmd", 1);
|
2013-06-22 10:06:55 +04:00
|
|
|
isa_register_ioport(&s->parent_obj, &s->io_cmd,
|
|
|
|
s->iobase + APPLESMC_CMD_PORT);
|
2010-06-30 12:41:12 +04:00
|
|
|
|
2017-06-16 21:55:15 +03:00
|
|
|
memory_region_init_io(&s->io_err, OBJECT(s), &applesmc_err_io_ops, s,
|
|
|
|
"applesmc-err", 1);
|
|
|
|
isa_register_ioport(&s->parent_obj, &s->io_err,
|
|
|
|
s->iobase + APPLESMC_ERR_PORT);
|
|
|
|
|
2021-10-04 11:46:37 +03:00
|
|
|
if (s->osk) {
|
|
|
|
valid_key = strlen(s->osk) == 64;
|
|
|
|
} else {
|
|
|
|
#if defined(__APPLE__) && defined(__MACH__)
|
|
|
|
IOReturn ret;
|
|
|
|
IOByteCount size = 32;
|
|
|
|
|
|
|
|
ret = smc_read_key('OSK0', (uint8_t *) default_osk, &size);
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = smc_read_key('OSK1', (uint8_t *) default_osk + size, &size);
|
|
|
|
if (ret != kIOReturnSuccess) {
|
|
|
|
goto failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
warn_report("Using AppleSMC with host key");
|
|
|
|
valid_key = true;
|
|
|
|
s->osk = default_osk;
|
|
|
|
failure:;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid_key) {
|
2017-09-11 22:52:50 +03:00
|
|
|
warn_report("Using AppleSMC with invalid key");
|
2010-06-30 12:41:12 +04:00
|
|
|
s->osk = default_osk;
|
|
|
|
}
|
|
|
|
|
|
|
|
QLIST_INIT(&s->data_def);
|
2012-11-25 05:37:14 +04:00
|
|
|
qdev_applesmc_isa_reset(dev);
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
|
2011-12-08 07:34:16 +04:00
|
|
|
static Property applesmc_isa_properties[] = {
|
2015-02-20 21:22:11 +03:00
|
|
|
DEFINE_PROP_UINT32(APPLESMC_PROP_IO_BASE, AppleSMCState, iobase,
|
|
|
|
APPLESMC_DEFAULT_IOBASE),
|
2013-04-28 00:18:36 +04:00
|
|
|
DEFINE_PROP_STRING("osk", AppleSMCState, osk),
|
2011-12-08 07:34:16 +04:00
|
|
|
DEFINE_PROP_END_OF_LIST(),
|
|
|
|
};
|
|
|
|
|
2011-12-04 21:52:49 +04:00
|
|
|
static void qdev_applesmc_class_init(ObjectClass *klass, void *data)
|
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
DeviceClass *dc = DEVICE_CLASS(klass);
|
2012-11-25 05:37:14 +04:00
|
|
|
|
|
|
|
dc->realize = applesmc_isa_realize;
|
2011-12-08 07:34:16 +04:00
|
|
|
dc->reset = qdev_applesmc_isa_reset;
|
2020-01-10 18:30:32 +03:00
|
|
|
device_class_set_props(dc, applesmc_isa_properties);
|
2013-07-29 18:17:45 +04:00
|
|
|
set_bit(DEVICE_CATEGORY_MISC, dc->categories);
|
2011-12-04 21:52:49 +04:00
|
|
|
}
|
|
|
|
|
2013-01-10 19:19:07 +04:00
|
|
|
static const TypeInfo applesmc_isa_info = {
|
2013-04-28 00:18:36 +04:00
|
|
|
.name = TYPE_APPLE_SMC,
|
2011-12-08 07:34:16 +04:00
|
|
|
.parent = TYPE_ISA_DEVICE,
|
2013-04-28 00:18:36 +04:00
|
|
|
.instance_size = sizeof(AppleSMCState),
|
2011-12-08 07:34:16 +04:00
|
|
|
.class_init = qdev_applesmc_class_init,
|
2010-06-30 12:41:12 +04:00
|
|
|
};
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
static void applesmc_register_types(void)
|
2010-06-30 12:41:12 +04:00
|
|
|
{
|
2011-12-08 07:34:16 +04:00
|
|
|
type_register_static(&applesmc_isa_info);
|
2010-06-30 12:41:12 +04:00
|
|
|
}
|
|
|
|
|
2012-02-09 18:20:55 +04:00
|
|
|
type_init(applesmc_register_types)
|