Exemplo n.º 1
0
FakeSMCKey *FakeSMCDevice::getKey(const char *name)
{
    KEYSLOCK;
    OSCollection *snapshotKeys = keys->copyCollection();
    KEYSUNLOCK;
    
    if (OSCollectionIterator *iterator = OSCollectionIterator::withCollection(snapshotKeys)) {

        char validKeyNameBuffer[5];
        copySymbol(name, validKeyNameBuffer);
        
		while (FakeSMCKey *key = OSDynamicCast(FakeSMCKey, iterator->getNextObject())) {
            UInt32 key1 = HWSensorsKeyToInt(&validKeyNameBuffer);
			UInt32 key2 = HWSensorsKeyToInt(key->getKey());
			if (key1 == key2) {
				OSSafeRelease(iterator);
                OSSafeRelease(snapshotKeys);
				return key;
			}
		}
        
        OSSafeRelease(iterator);
	}
    
    OSSafeRelease(snapshotKeys);
    
 	FakeSMCDebugLog("key %s not found", name);
    
	return 0;
}
Exemplo n.º 2
0
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;
}
Exemplo n.º 3
0
FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
	if (FakeSMCKey *key = getKey(name)) {
        
        if (key->getHandler() != NULL) {
            // TODO: check priority?
            
            HWSensorsErrorLog("key %s already handled", name);
            return 0;
        }
        
        key->setType(type);
        key->setSize(size);
        key->setHandler(handler);
		
		return key;
	}
    
	FakeSMCDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
    
	if (FakeSMCKey *key = FakeSMCKey::withHandler(name, type, size, handler)) {
        KEYSLOCK;
		keys->setObject(key);
        KEYSUNLOCK;
		updateKeyCounterKey();
		return key;
	}
    
	HWSensorsErrorLog("failed to create key %s", name);
    
	return 0;
}
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;
}
FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
    IORecursiveLockLock(device_lock);

    FakeSMCKey* key;
	if ((key = getKey(name))) {
		HWSensorsErrorLog("key %s already handled", name);
        if (key->getHandler() != NULL) {
            // TODO: check priority?
            HWSensorsErrorLog("key %s already handled", name);
            key = 0;
        }
        else {
            key->setType(type);
            key->setSize(size);
            key->setHandler(handler);
        }
        
	}
    else {
    
        FakeSMCDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withHandler(name, type, size, handler))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
	}
    IORecursiveLockUnlock(device_lock);
    
    if (!key)
        HWSensorsErrorLog("failed to create key %s", name);
        
	return key;
}
Exemplo n.º 6
0
FakeSMCKey *FakeSMCDevice::getKey(unsigned int index)
{
    KEYSLOCK;
    FakeSMCKey *key = OSDynamicCast(FakeSMCKey, keys->getObject(index));
    KEYSUNLOCK;
    
	if (!key) FakeSMCDebugLog("key with index %d not found", index);

	return key;
}
FakeSMCKey *FakeSMCDevice::getKey(unsigned int index)
{
    IORecursiveLockLock(device_lock);
    FakeSMCKey* key = OSDynamicCast(FakeSMCKey, keys->getObject(index));
    IORecursiveLockUnlock(device_lock);
    if (!key)
        FakeSMCDebugLog("key with index %d not found", index);
    
	return key;
}
FakeSMCKey *FakeSMCDevice::addKeyWithHandler(const char *name, const char *type, unsigned char size, IOService *handler)
{
    KEYSLOCK;
    
    FakeSMCKey *key;
    if ((key = getKey(name))) {
        
        IOService *existedHandler = key->getHandler();
        
        if (getHandlingPriority(handler) < getHandlingPriority(existedHandler)) {
            HWSensorsErrorLog("key %s already handled with prioritized handler %s", name, existedHandler ? existedHandler->getName() : "*Unreferenced*");
            key = 0;
        }
        else {
            HWSensorsInfoLog("key %s handler %s has been replaced with new prioritized handler %s", name, existedHandler ? existedHandler->getName() : "*Unreferenced*", handler ? handler->getName() : "*Unreferenced*");
            
            key->setType(type);
            key->setSize(size);
            key->setHandler(handler);
        }
    }
    else {
        
        FakeSMCDebugLog("adding key %s with handler, type: %s, size: %d", name, type, size);
        
        if ((key = FakeSMCKey::withHandler(name, type, size, handler))) {
            keys->setObject(key);
            updateKeyCounterKey();
        }
        else {
            HWSensorsErrorLog("failed to create key %s", name);
        }
    }
    KEYSUNLOCK;
    
    return key;
}
Exemplo n.º 9
0
bool FakeSMCDevice::initAndStart(IOService *platform, IOService *provider)
{
	if (!provider || !super::init(platform, 0, 0))
		return false;
    
    OSDictionary *properties = OSDynamicCast(OSDictionary, provider->getProperty("Configuration"));
    
    if (!properties)
        return false;
    
	status = (ApleSMCStatus *) IOMalloc(sizeof(struct AppleSMCStatus));
	bzero((void*)status, sizeof(struct AppleSMCStatus));
	interrupt_handler = 0;
    
	keys = OSArray::withCapacity(1);
    types = OSDictionary::withCapacity(0);
    exposedValues = OSDictionary::withCapacity(0);
    
    // Add fist key - counter key
    keyCounterKey = FakeSMCKey::withValue(KEY_COUNTER, TYPE_UI32, TYPE_UI32_SIZE, "\0\0\0\1");
	keys->setObject(keyCounterKey);
    
    fanCounterKey = FakeSMCKey::withValue(KEY_FAN_NUMBER, TYPE_UI8, TYPE_UI8_SIZE, "\0");
    keys->setObject(fanCounterKey);
    
    if (!gKeysLock)
        gKeysLock = IORecursiveLockAlloc();
    
    // Load preconfigured keys
    FakeSMCDebugLog("loading keys...");
    
    if (OSDictionary *dictionary = OSDynamicCast(OSDictionary, properties->getObject("Keys"))) {
		if (OSIterator *iterator = OSCollectionIterator::withCollection(dictionary)) {
			while (const OSSymbol *key = (const OSSymbol *)iterator->getNextObject()) {
				if (OSArray *array = OSDynamicCast(OSArray, dictionary->getObject(key))) {
					if (OSIterator *aiterator = OSCollectionIterator::withCollection(array)) {
                        
						OSString *type = OSDynamicCast(OSString, aiterator->getNextObject());
						OSData *value = OSDynamicCast(OSData, aiterator->getNextObject());
                        
						if (type && value)
							addKeyWithValue(key->getCStringNoCopy(), type->getCStringNoCopy(), value->getLength(), value->getBytesNoCopy());
                        
                        OSSafeRelease(aiterator);
					}
				}
				key = 0;
			}
            
			OSSafeRelease(iterator);
		}
        
		HWSensorsInfoLog("%d preconfigured key%s added", keys->getCount(), keys->getCount() == 1 ? "" : "s");
	}
	else {
		HWSensorsWarningLog("no preconfigured keys found");
	}
    
    // Load wellknown type names
    FakeSMCDebugLog("loading types...");
    
    if (OSDictionary *dictionary = OSDynamicCast(OSDictionary, properties->getObject("Types"))) {
        if (OSIterator *iterator = OSCollectionIterator::withCollection(dictionary)) {
			while (OSString *key = OSDynamicCast(OSString, iterator->getNextObject())) {
                if (OSString *value = OSDynamicCast(OSString, dictionary->getObject(key))) {
                    types->setObject(key, value);
                }
            }
            OSSafeRelease(iterator);
        }
    }
    
    // Set Clover platform keys
    if (OSDictionary *dictionary = OSDynamicCast(OSDictionary, properties->getObject("Clover"))) {
        UInt32 count = 0;
        if (IORegistryEntry* cloverPlatformNode = fromPath("/efi/platform", gIODTPlane)) {
            if (OSIterator *iterator = OSCollectionIterator::withCollection(dictionary)) {
                while (OSString *name = OSDynamicCast(OSString, iterator->getNextObject())) {
                    if (OSData *data = OSDynamicCast(OSData, cloverPlatformNode->getProperty(name))) {
                        if (OSArray *items = OSDynamicCast(OSArray, dictionary->getObject(name))) {
                            OSString *key = OSDynamicCast(OSString, items->getObject(0));
                            OSString *type = OSDynamicCast(OSString, items->getObject(1));
                            
                            if (addKeyWithValue(key->getCStringNoCopy(), type->getCStringNoCopy(), data->getLength(), data->getBytesNoCopy()))
                                count++;
                        }
                    }
                }
                OSSafeRelease(iterator);
            }
        }
        
        if (count)
            HWSensorsInfoLog("%d key%s exported by Clover EFI", count, count == 1 ? "" : "s");
    }
    
    // Start SMC device
    
    if (!super::start(platform))
        return false;
    
	this->setName("SMC");
    
    FakeSMCSetProperty("name", "APP0001");
    
	if (OSString *compatibleKey = OSDynamicCast(OSString, properties->getObject("smc-compatible")))
		FakeSMCSetProperty("compatible", (const char *)compatibleKey->getCStringNoCopy());
	else
		FakeSMCSetProperty("compatible", "smc-napa");
    
	if (!this->setProperty("_STA", (unsigned long long)0x0000000b, 32)) {
        HWSensorsErrorLog("failed to set '_STA' property");
        return false;
    }
    
	if (OSBoolean *debugKey = OSDynamicCast(OSBoolean, properties->getObject("debug")))
		debug = debugKey->getValue();
    else
        debug = false;
    
    if (OSBoolean *traceKey = OSDynamicCast(OSBoolean, properties->getObject("trace")))
		trace = traceKey->getValue();
    else
        trace = false;
    
	IODeviceMemory::InitElement	rangeList[1];
    
	rangeList[0].start = 0x300;
	rangeList[0].length = 0x20;
//    rangeList[1].start = 0xfef00000;
//	rangeList[1].length = 0x10000;
    
	if(OSArray *array = IODeviceMemory::arrayFromList(rangeList, 1)) {
		this->setDeviceMemory(array);
		OSSafeRelease(array);
	}
	else
	{
		HWSensorsFatalLog("failed to create Device memory array");
		return false;
	}
    
	OSArray *controllers = OSArray::withCapacity(1);
    
    if(!controllers) {
		HWSensorsFatalLog("failed to create controllers array");
        return false;
    }
    
    controllers->setObject((OSSymbol *)OSSymbol::withCStringNoCopy("io-apic-0"));
    
	OSArray *specifiers  = OSArray::withCapacity(1);
    
    if(!specifiers) {
		HWSensorsFatalLog("failed to create specifiers array");
        return false;
    }
    
	UInt64 line = 0x06;
    
    OSData *tmpData = OSData::withBytes(&line, sizeof(line));
    
    if (!tmpData) {
		HWSensorsFatalLog("failed to create specifiers data");
        return false;
    }
    
    specifiers->setObject(tmpData);
    
	this->setProperty(gIOInterruptControllersKey, controllers) && this->setProperty(gIOInterruptSpecifiersKey, specifiers);
	this->attachToParent(platform, gIOServicePlane);
    
    registerService();
    
	HWSensorsInfoLog("successfully initialized");
    
	return true;
}
Exemplo n.º 10
0
FakeSMCKey *FakeSMCDevice::addKeyWithValue(const char *name, const char *type, unsigned char size, const void *value)
{
    if (FakeSMCKey *key = getKey(name)) {
        
        if (type && strncmp(type, key->getType(), 4) == 0) {
            key->setType(type);
        }
        
        if (value) {
            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("value updated for key %s, type: %s, size: %d", name, type, size);
        
		return key;
	}
    
	FakeSMCDebugLog("adding key %s with value, type: %s, size: %d", name, type, size);
    
    OSString *wellKnownType = 0;
    
    if (!type) wellKnownType = OSDynamicCast(OSString, types->getObject(name));
    
	if (FakeSMCKey *key = FakeSMCKey::withValue(name, type ? type : wellKnownType ? wellKnownType->getCStringNoCopy() : 0, size, value)) {
        KEYSLOCK;
		keys->setObject(key);
        KEYSUNLOCK;
		updateKeyCounterKey();
		return key;
	}
    
	HWSensorsErrorLog("failed to create key %s", name);
    
	return 0;
}
Exemplo n.º 11
0
void FakeSMCDevice::applesmc_io_data_writeb(void *opaque, uint32_t addr, uint32_t val)
{
    struct AppleSMCStatus *s = (struct AppleSMCStatus *)opaque;
    //    IOLog("APPLESMC: DATA Write B: %#x = %#x\n", addr, val);
    switch(s->cmd) {
        case APPLESMC_READ_CMD:
            if(s->read_pos < 4) {
                s->key[s->read_pos] = val;
                s->status = 0x04;
            } else if(s->read_pos == 4) {
                s->data_len = val;
                s->status = 0x05;
                s->data_pos = 0;
                //                IOLog("APPLESMC: Key = %c%c%c%c Len = %d\n", s->key[0], s->key[1], s->key[2], s->key[3], val);
                applesmc_fill_data(s);
            }
            s->read_pos++;
            break;
		case APPLESMC_WRITE_CMD:
            //			IOLog("FakeSMC: attempting to write(WRITE_CMD) to io port value %x ( %c )\n", val, val);
			if(s->read_pos < 4) {
                s->key[s->read_pos] = val;
                s->status = 0x04;
			} else if(s->read_pos == 4) {
				s->status = 0x05;
				s->data_pos=0;
				s->data_len = val;
                //				IOLog("FakeSMC: System Tried to write Key = %c%c%c%c Len = %d\n", s->key[0], s->key[1], s->key[2], s->key[3], val);
			} else if( s->data_pos < s->data_len ) {
				s->value[s->data_pos] = val;
				s->data_pos++;
				s->status = 0x05;
				if(s->data_pos == s->data_len) {
					s->status = 0x00;
                    
                    // Add or update key
                    char name[5]; name[4] = 0; memcpy(name, s->key, 4);
                    
                    FakeSMCDebugLog("system writing key %s, length %d", name, s->data_len);
                    
                    FakeSMCKey* key = addKeyWithValue(name, 0, s->data_len, s->value);

                    bzero(s->value, 255);
                    
                    if (key) saveKeyToNVRAM(key);
				}
			};
			s->read_pos++;
			break;
		case APPLESMC_GET_KEY_BY_INDEX_CMD:
            //			IOLog("FakeSMC: System Tried to write GETKEYBYINDEX = %x (%c) at pos %x\n",val , val, s->read_pos);
			if(s->read_pos < 4) {
                s->key_index += val << (24 - s->read_pos * 8);
                s->status = 0x04;
				s->read_pos++;
			};
			if(s->read_pos == 4) {
				s->status = 0x05;
                //				IOLog("FakeSMC: trying to find key by index %x\n", s->key_index);
				if(const char * key = applesmc_get_key_by_index(s->key_index, s))
					bcopy(key, s->key, 4);
			}
            
			break;
		case APPLESMC_GET_KEY_TYPE_CMD:
            //			IOLog("FakeSMC: System Tried to write GETKEYTYPE = %x (%c) at pos %x\n",val , val, s->read_pos);
			if(s->read_pos < 4) {
                s->key[s->read_pos] = val;
                s->status = 0x04;
            };
			s->read_pos++;
			if(s->read_pos == 4) {
				s->data_len = 6;  ///s->data_len = val ; ? val should be 6 here too
				s->status = 0x05;
				s->data_pos=0;
				applesmc_fill_info(s);
			}
			break;
    }
}
Exemplo n.º 12
0
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;
}