FakeSMCKey *FakeSMCDevice::getKey(const char *name) { KEYSLOCK; FakeSMCKey* key = 0; if (OSCollectionIterator *iterator = OSCollectionIterator::withCollection(keys)) { // Made the key name valid (4 char long): add trailing spaces if needed char validKeyNameBuffer[5]; copySymbol(name, validKeyNameBuffer); while ((key = OSDynamicCast(FakeSMCKey, iterator->getNextObject()))) { UInt32 key1 = HWSensorsKeyToInt(&validKeyNameBuffer); UInt32 key2 = HWSensorsKeyToInt(key->getKey()); if (key1 == key2) { break; } } OSSafeRelease(iterator); } KEYSUNLOCK; if (!key) FakeSMCDebugLog("key %s not found", name); return key; }
FakeSMCKey *FakeSMCDevice::getKey(const char *name) { IORecursiveLockLock(device_lock); FakeSMCKey* key = 0; if (OSCollectionIterator *iterator = OSCollectionIterator::withCollection(keys)) { // Made the key name valid (4 char long): add trailing spaces if needed char validKeyNameBuffer[5]; snprintf(validKeyNameBuffer, 5, "%-4s", name); while ((key = OSDynamicCast(FakeSMCKey, iterator->getNextObject()))) { UInt32 key1 = HWSensorsKeyToInt(&validKeyNameBuffer); UInt32 key2 = HWSensorsKeyToInt(key->getKey()); if (key1 == key2) { break; } } OSSafeRelease(iterator); } IORecursiveLockUnlock(device_lock); if (!key) FakeSMCDebugLog("key %s not found", name); return key; }
IOReturn FakeSMCKeyStoreUserClient::externalMethod(uint32_t selector, IOExternalMethodArguments* arguments, IOExternalMethodDispatch * dispatch, OSObject * target, void * reference ) { IOReturn result = kIOReturnError; if (keyStore == NULL || isInactive()) { result = kIOReturnNotAttached; } else if (!keyStore->isOpen(this)) { result = kIOReturnNotOpen; } SYNCLOCK; switch (selector) { case KERNEL_INDEX_SMC: { SMCKeyData_t *input = (SMCKeyData_t*)arguments->structureInput; SMCKeyData_t *output = (SMCKeyData_t*)arguments->structureOutput; switch (input->data8) { case SMC_CMD_READ_INDEX: { FakeSMCKey *key = keyStore->getKey(input->data32); output->key = _strtoul(key->getKey(), 4, 16); result = kIOReturnSuccess; break; } case SMC_CMD_READ_KEYINFO: { char name[5]; _ultostr(name, input->key); FakeSMCKey *key = keyStore->getKey(name); if (key) { output->keyInfo.dataSize = key->getSize(); output->keyInfo.dataType = _strtoul(key->getType(), 4, 16); result = kIOReturnSuccess; } else result = kIOReturnNotFound; break; } case SMC_CMD_READ_BYTES: { char name[5]; _ultostr(name, input->key); FakeSMCKey *key = keyStore->getKey(name); if (key) { memcpy(output->bytes, key->getValue(), key->getSize()); result = kIOReturnSuccess; } else result = kIOReturnNotFound; break; } case SMC_CMD_WRITE_BYTES: { char name[5]; _ultostr(name, input->key); IOLog("FakeSMCKeyStoreUserClient: SMC_CMD_WRITE_BYTES key=%s", name); FakeSMCKey *key = keyStore->getKey(name); if (key) { key->setValueFromBuffer(input->bytes, input->keyInfo.dataSize); result = kIOReturnSuccess; } else { char type[5]; if (input->keyInfo.dataType) { _ultostr(type, input->keyInfo.dataType); } else { type[0] = '\0'; } keyStore->addKeyWithValue(name, type, input->keyInfo.dataSize, input->bytes); result = kIOReturnSuccess; } break; } default: result = kIOReturnBadArgument; break; } break; } default: result = kIOReturnBadArgument; break; } SYNCUNLOCK; return result; }
FakeSMCKey *FakeSMCDevice::addKeyWithValue(const char *name, const char *type, unsigned char size, const void *value) { KEYSLOCK; FakeSMCKey* key; if ((key = getKey(name))) { if (type && strncmp(type, key->getType(), 4) == 0) { key->setType(type); } if (value) { key->setSize(size); key->setValueFromBuffer(value, size); } #ifdef DEBUG if (debug) { if (strncmp("NATJ", key->getKey(), 5) == 0) { UInt8 val = *(UInt8*)key->getValue(); switch (val) { case 0: HWSensorsInfoLog("Ninja Action Timer Job: do nothing"); break; case 1: HWSensorsInfoLog("Ninja Action Timer Job: force shutdown to S5"); break; case 2: HWSensorsInfoLog("Ninja Action Timer Job: force restart"); break; case 3: HWSensorsInfoLog("Ninja Action Timer Job: force startup"); break; default: break; } } else if (strncmp("NATi", key->getKey(), 5) == 0) { UInt16 val = *(UInt16*)key->getValue(); HWSensorsInfoLog("Ninja Action Timer is set to %d", val); } else if (strncmp("MSDW", key->getKey(), 5) == 0) { UInt8 val = *(UInt8*)key->getValue(); switch (val) { case 0: HWSensorsInfoLog("display is now asleep"); break; case 1: HWSensorsInfoLog("display is now awake"); break; default: break; } } } FakeSMCDebugLog("value updated for key %s, type: %s, size: %d", name, type, size); #endif } else { FakeSMCDebugLog("adding key %s with value, type: %s, size: %d", name, type, size); OSString *wellKnownType = 0; if (!type) wellKnownType = OSDynamicCast(OSString, types->getObject(name)); key = FakeSMCKey::withValue(name, type ? type : wellKnownType ? wellKnownType->getCStringNoCopy() : 0, size, value); if (key) { keys->setObject(key); updateKeyCounterKey(); } } KEYSUNLOCK; if (!key) HWSensorsErrorLog("failed to create key %s", name); return key; }
FakeSMCKey *FakeSMCDevice::addKeyWithValue(const char *name, const char *type, unsigned char size, const void *value) { IORecursiveLockLock(device_lock); FakeSMCKey* key; if ((key = getKey(name))) { if (value) { key->setType(type); key->setSize(size); key->setValueFromBuffer(value, size); } if (debug) { if (strncmp("NATJ", key->getKey(), 5) == 0) { UInt8 val = *(UInt8*)key->getValue(); switch (val) { case 0: HWSensorsInfoLog("Ninja Action Timer Job: do nothing"); break; case 1: HWSensorsInfoLog("Ninja Action Timer Job: force shutdown to S5"); break; case 2: HWSensorsInfoLog("Ninja Action Timer Job: force restart"); break; case 3: HWSensorsInfoLog("Ninja Action Timer Job: force startup"); break; default: break; } } else if (strncmp("NATi", key->getKey(), 5) == 0) { UInt16 val = *(UInt16*)key->getValue(); HWSensorsInfoLog("Ninja Action Timer is set to %d", val); } else if (strncmp("MSDW", key->getKey(), 5) == 0) { UInt8 val = *(UInt8*)key->getValue(); switch (val) { case 0: HWSensorsInfoLog("display is now asleep"); break; case 1: HWSensorsInfoLog("display is now awake"); break; default: break; } } } FakeSMCDebugLog("updating value for key %s, type: %s, size: %d", name, type, size); } else { FakeSMCDebugLog("adding key %s with value, type: %s, size: %d", name, type, size); if ((key = FakeSMCKey::withValue(name, type, size, value))) { keys->setObject(key); updateKeyCounterKey(); } } IORecursiveLockUnlock(device_lock); if (!key) HWSensorsErrorLog("failed to create key %s", name); return key; }