Fixes for modern macos

This commit is contained in:
elvis
2025-11-04 20:50:54 +01:00
parent e1bd672bcd
commit ad77a7b2e8
2 changed files with 328 additions and 332 deletions

View File

@ -1,5 +1,5 @@
CC = gcc CC = clang
CFLAGS = -mmacosx-version-min=10.4 -Wall -g -framework IOKit CFLAGS = -Wall -g -framework IOKit
CPPFLAGS = -DCMD_TOOL_BUILD CPPFLAGS = -DCMD_TOOL_BUILD
all: smc all: smc
@ -11,4 +11,4 @@ smc.o: smc.h smc.c
$(CC) $(CPPFLAGS) -c smc.c $(CC) $(CPPFLAGS) -c smc.c
clean: clean:
-rm -f smc smc.o @-rm -f smc smc.o

View File

@ -25,6 +25,7 @@
#include <IOKit/IOKitLib.h> #include <IOKit/IOKitLib.h>
#include "smc.h" #include "smc.h"
#include <libkern/OSAtomic.h> #include <libkern/OSAtomic.h>
#include <os/lock.h>
// Cache the keyInfo to lower the energy impact of SMCReadKey() / SMCReadKey2() // Cache the keyInfo to lower the energy impact of SMCReadKey() / SMCReadKey2()
#define KEY_INFO_CACHE_SIZE 100 #define KEY_INFO_CACHE_SIZE 100
@ -34,7 +35,7 @@ struct {
} g_keyInfoCache[KEY_INFO_CACHE_SIZE]; } g_keyInfoCache[KEY_INFO_CACHE_SIZE];
int g_keyInfoCacheCount = 0; int g_keyInfoCacheCount = 0;
OSSpinLock g_keyInfoSpinLock = 0; os_unfair_lock g_keyInfoSpinLock = OS_UNFAIR_LOCK_INIT;
kern_return_t SMCCall2(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure, io_connect_t conn); kern_return_t SMCCall2(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure, io_connect_t conn);
@ -275,9 +276,7 @@ void printVal(SMCVal_t val)
printFLT(val); printFLT(val);
printBytesHex(val); printBytesHex(val);
} } else {
else
{
printf("no data\n"); printf("no data\n");
} }
} }
@ -291,7 +290,7 @@ kern_return_t SMCOpen(io_connect_t *conn)
io_iterator_t iterator; io_iterator_t iterator;
io_object_t device; io_object_t device;
IOMasterPort(MACH_PORT_NULL, &masterPort); IOMainPort(MACH_PORT_NULL, &masterPort);
CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC"); CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator); result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
@ -343,7 +342,7 @@ kern_return_t SMCGetKeyInfo(UInt32 key, SMCKeyData_keyInfo_t* keyInfo, io_connec
kern_return_t result = kIOReturnSuccess; kern_return_t result = kIOReturnSuccess;
int i = 0; int i = 0;
OSSpinLockLock(&g_keyInfoSpinLock); os_unfair_lock_lock(&g_keyInfoSpinLock);
for (; i < g_keyInfoCacheCount; ++i) for (; i < g_keyInfoCacheCount; ++i)
{ {
@ -376,7 +375,7 @@ kern_return_t SMCGetKeyInfo(UInt32 key, SMCKeyData_keyInfo_t* keyInfo, io_connec
} }
} }
OSSpinLockUnlock(&g_keyInfoSpinLock); os_unfair_lock_unlock(&g_keyInfoSpinLock);
return result; return result;
} }
@ -392,7 +391,7 @@ kern_return_t SMCReadKey2(UInt32Char_t key, SMCVal_t *val,io_connect_t conn)
memset(val, 0, sizeof(SMCVal_t)); memset(val, 0, sizeof(SMCVal_t));
inputStructure.key = _strtoul(key, 4, 16); inputStructure.key = _strtoul(key, 4, 16);
sprintf(val->key, key); sprintf(val->key, "%s", key);
result = SMCGetKeyInfo(inputStructure.key, &outputStructure.keyInfo, conn); result = SMCGetKeyInfo(inputStructure.key, &outputStructure.keyInfo, conn);
if (result != kIOReturnSuccess) if (result != kIOReturnSuccess)
@ -665,7 +664,7 @@ kern_return_t SMCWriteSimple(UInt32Char_t key, char *wvalue, io_connect_t conn)
val.bytes[i] = (int) strtol(c, NULL, 16); val.bytes[i] = (int) strtol(c, NULL, 16);
} }
val.dataSize = i / 2; val.dataSize = i / 2;
sprintf(val.key, key); sprintf(val.key, "%s", key);
result = SMCWriteKey2(val, conn); result = SMCWriteKey2(val, conn);
if (result != kIOReturnSuccess) if (result != kIOReturnSuccess)
printf("Error: SMCWriteKey() = %08x\n", result); printf("Error: SMCWriteKey() = %08x\n", result);
@ -775,7 +774,7 @@ int main(int argc, char *argv[])
case OP_WRITE: case OP_WRITE:
if (strlen(key) > 0) if (strlen(key) > 0)
{ {
sprintf(val.key, key); sprintf(val.key, "%s", key);
result = SMCWriteKey(val); result = SMCWriteKey(val);
if (result != kIOReturnSuccess) if (result != kIOReturnSuccess)
printf("Error: SMCWriteKey() = %08x\n", result); printf("Error: SMCWriteKey() = %08x\n", result);
@ -791,6 +790,3 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
#endif //#ifdef CMD_TOOL #endif //#ifdef CMD_TOOL