-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
smc.c
700 lines (532 loc) · 16.9 KB
/
smc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/*
* Apple System Management Controller (SMC) API from user space for Intel based
* Macs. Works by talking to the AppleSMC.kext (kernel extension), the driver
* for the SMC.
*
* smc.c
* libsmc
*
* Copyright (C) 2014 beltex <https://github.com/beltex>
*
* Based off of fork from:
* osx-cpu-temp <https://github.com/lavoiesl/osx-cpu-temp>
*
* With credits to:
*
* Copyright (C) 2006 devnull
* Apple System Management Control (SMC) Tool
*
* Copyright (C) 2006 Hendrik Holtmann
* smcFanControl <https://github.com/hholtmann/smcFanControl>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <string.h>
#include "smc.h"
//------------------------------------------------------------------------------
// MARK: MACROS
//------------------------------------------------------------------------------
/**
Name of the SMC IOService as seen in the IORegistry. You can view it either via
command line with ioreg or through the IORegistryExplorer app (found on Apple's
developer site - Hardware IO Tools for Xcode)
*/
#define IOSERVICE_SMC "AppleSMC"
/**
IOService for getting machine model name
*/
#define IOSERVICE_MODEL "IOPlatformExpertDevice"
/**
SMC data types - 4 byte multi-character constants
Sources: See TMP SMC keys in smc.h
http://stackoverflow.com/questions/22160746/fpe2-and-sp78-data-types
*/
#define DATA_TYPE_UINT8 "ui8 "
#define DATA_TYPE_UINT16 "ui16"
#define DATA_TYPE_UINT32 "ui32"
#define DATA_TYPE_FLAG "flag"
#define DATA_TYPE_FPE2 "fpe2"
#define DATA_TYPE_SFDS "{fds"
#define DATA_TYPE_SP78 "sp78"
//------------------------------------------------------------------------------
// MARK: GLOBAL VARS
//------------------------------------------------------------------------------
/**
Our connection to the SMC
*/
static io_connect_t conn;
/**
Number of characters in an SMC key
*/
static const int SMC_KEY_SIZE = 4;
/**
Number of characters in a data type "key" returned from the SMC. See data type
macros.
*/
static const int DATA_TYPE_SIZE = 4;
//------------------------------------------------------------------------------
// MARK: ENUMS
//------------------------------------------------------------------------------
/**
Defined by AppleSMC.kext. See SMCParamStruct.
These are SMC specific return codes
*/
typedef enum {
kSMCSuccess = 0,
kSMCError = 1,
kSMCKeyNotFound = 0x84
} kSMC_t;
/**
Defined by AppleSMC.kext. See SMCParamStruct.
Function selectors. Used to tell the SMC which function inside it to call.
*/
typedef enum {
kSMCUserClientOpen = 0,
kSMCUserClientClose = 1,
kSMCHandleYPCEvent = 2,
kSMCReadKey = 5,
kSMCWriteKey = 6,
kSMCGetKeyCount = 7,
kSMCGetKeyFromIndex = 8,
kSMCGetKeyInfo = 9
} selector_t;
//------------------------------------------------------------------------------
// MARK: STRUCTS
//------------------------------------------------------------------------------
/**
Defined by AppleSMC.kext. See SMCParamStruct.
*/
typedef struct {
unsigned char major;
unsigned char minor;
unsigned char build;
unsigned char reserved;
unsigned short release;
} SMCVersion;
/**
Defined by AppleSMC.kext. See SMCParamStruct.
*/
typedef struct {
uint16_t version;
uint16_t length;
uint32_t cpuPLimit;
uint32_t gpuPLimit;
uint32_t memPLimit;
} SMCPLimitData;
/**
Defined by AppleSMC.kext. See SMCParamStruct.
- dataSize : How many values written to SMCParamStruct.bytes
- dataType : Type of data written to SMCParamStruct.bytes. This lets us know how
to interpret it (translate it to human readable)
*/
typedef struct {
IOByteCount dataSize;
uint32_t dataType;
uint8_t dataAttributes;
} SMCKeyInfoData;
/**
Defined by AppleSMC.kext.
This is the predefined struct that must be passed to communicate with the
AppleSMC driver. While the driver is closed source, the definition of this
struct happened to appear in the Apple PowerManagement project at around
version 211, and soon after disappeared. It can be seen in the PrivateLib.c
file under pmconfigd.
https://www.opensource.apple.com/source/PowerManagement/PowerManagement-211/
*/
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;
/**
Used for returning data from the SMC.
*/
typedef struct {
uint8_t data[32];
uint32_t dataType;
uint32_t dataSize;
kSMC_t kSMC;
} smc_return_t;
//------------------------------------------------------------------------------
// MARK: HELPERS - TYPE CONVERSION
//------------------------------------------------------------------------------
/**
Convert data from SMC of fpe2 type to human readable.
:param: data Data from the SMC to be converted. Assumed data size of 2.
:returns: Converted data
*/
static unsigned int from_fpe2(uint8_t data[32])
{
unsigned int ans = 0;
// Data type for fan calls - fpe2
// This is assumend to mean floating point, with 2 exponent bits
// http://stackoverflow.com/questions/22160746/fpe2-and-sp78-data-types
ans += data[0] << 6;
ans += data[1] << 2;
return ans;
}
/**
Convert to fpe2 data type to be passed to SMC.
:param: val Value to convert
:param: data Pointer to data array to place result
*/
static void to_fpe2(unsigned int val, uint8_t *data)
{
data[0] = val >> 6;
data[1] = (val << 2) ^ (data[0] << 8);
}
/**
Convert SMC key to uint32_t. This must be done to pass it to the SMC.
:param: key The SMC key to convert
:returns: uint32_t translation.
Returns zero if key is not 4 characters in length.
*/
static uint32_t to_uint32_t(char *key)
{
uint32_t ans = 0;
uint32_t shift = 24;
// SMC key is expected to be 4 bytes - thus 4 chars
if (strlen(key) != SMC_KEY_SIZE) {
return 0;
}
for (int i = 0; i < SMC_KEY_SIZE; i++) {
ans += key[i] << shift;
shift -= 8;
}
return ans;
}
/**
For converting the dataType return from the SMC to human readable 4 byte
multi-character constant.
*/
static void to_string(uint32_t val, char *dataType)
{
int shift = 24;
for (int i = 0; i < DATA_TYPE_SIZE; i++) {
// To get each char, we shift it into the lower 8 bits, and then & by
// 255 to insolate it
dataType[i] = (val >> shift) & 0xff;
shift -= 8;
}
}
//------------------------------------------------------------------------------
// MARK: HELPERS - TMP CONVERSION
//------------------------------------------------------------------------------
/**
Celsius to Fahrenheit
*/
static double to_fahrenheit(double tmp)
{
// http://en.wikipedia.org/wiki/Fahrenheit#Definition_and_conversions
return (tmp * 1.8) + 32;
}
/**
Celsius to Kelvin
*/
static double to_kelvin(double tmp)
{
// http://en.wikipedia.org/wiki/Kelvin
return tmp + 273.15;
}
//------------------------------------------------------------------------------
// MARK: "PRIVATE" FUNCTIONS
//------------------------------------------------------------------------------
/**
Make a call to the SMC
:param: inputStruct Struct that holds data telling the SMC what you want
:param: outputStruct Struct holding the SMC's response
:returns: I/O Kit return code
*/
static kern_return_t call_smc(SMCParamStruct *inputStruct,
SMCParamStruct *outputStruct)
{
kern_return_t result;
size_t inputStructCnt = sizeof(SMCParamStruct);
size_t outputStructCnt = sizeof(SMCParamStruct);
result = IOConnectCallStructMethod(conn, kSMCHandleYPCEvent,
inputStruct,
inputStructCnt,
outputStruct,
&outputStructCnt);
if (result != kIOReturnSuccess) {
// IOReturn error code lookup. See "Accessing Hardware From Applications
// -> Handling Errors" Apple doc
result = err_get_code(result);
}
return result;
}
/**
Read data from the SMC
:param: key The SMC key
*/
static kern_return_t read_smc(char *key, smc_return_t *result_smc)
{
kern_return_t result;
SMCParamStruct inputStruct;
SMCParamStruct outputStruct;
memset(&inputStruct, 0, sizeof(SMCParamStruct));
memset(&outputStruct, 0, sizeof(SMCParamStruct));
memset(result_smc, 0, sizeof(smc_return_t));
// First call to AppleSMC - get key info
inputStruct.key = to_uint32_t(key);
inputStruct.data8 = kSMCGetKeyInfo;
result = call_smc(&inputStruct, &outputStruct);
result_smc->kSMC = outputStruct.result;
if (result != kIOReturnSuccess || outputStruct.result != kSMCSuccess) {
return result;
}
// Store data for return
result_smc->dataSize = outputStruct.keyInfo.dataSize;
result_smc->dataType = outputStruct.keyInfo.dataType;
// Second call to AppleSMC - now we can get the data
inputStruct.keyInfo.dataSize = outputStruct.keyInfo.dataSize;
inputStruct.data8 = kSMCReadKey;
result = call_smc(&inputStruct, &outputStruct);
result_smc->kSMC = outputStruct.result;
if (result != kIOReturnSuccess || outputStruct.result != kSMCSuccess) {
return result;
}
memcpy(result_smc->data, outputStruct.bytes, sizeof(outputStruct.bytes));
return result;
}
/**
Write data to the SMC.
:returns: IOReturn IOKit return code
*/
static kern_return_t write_smc(char *key, smc_return_t *result_smc)
{
kern_return_t result;
SMCParamStruct inputStruct;
SMCParamStruct outputStruct;
memset(&inputStruct, 0, sizeof(SMCParamStruct));
memset(&outputStruct, 0, sizeof(SMCParamStruct));
// First call to AppleSMC - get key info
inputStruct.key = to_uint32_t(key);
inputStruct.data8 = kSMCGetKeyInfo;
result = call_smc(&inputStruct, &outputStruct);
result_smc->kSMC = outputStruct.result;
if (result != kIOReturnSuccess || outputStruct.result != kSMCSuccess) {
return result;
}
// Check data is correct
if (result_smc->dataSize != outputStruct.keyInfo.dataSize ||
result_smc->dataType != outputStruct.keyInfo.dataType) {
return kIOReturnBadArgument;
}
// Second call to AppleSMC - now we can write the data
inputStruct.data8 = kSMCWriteKey;
inputStruct.keyInfo.dataSize = outputStruct.keyInfo.dataSize;
// Set data to write
memcpy(inputStruct.bytes, result_smc->data, sizeof(result_smc->data));
result = call_smc(&inputStruct, &outputStruct);
result_smc->kSMC = outputStruct.result;
return result;
}
/**
Get the model name of the machine.
*/
static kern_return_t get_machine_model(io_name_t model)
{
io_service_t service;
kern_return_t result;
service = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching(IOSERVICE_MODEL));
if (service == 0) {
printf("ERROR: %s NOT FOUND\n", IOSERVICE_MODEL);
return kIOReturnError;
}
// Get the model name
result = IORegistryEntryGetName(service, model);
IOObjectRelease(service);
return result;
}
//------------------------------------------------------------------------------
// MARK: "PUBLIC" FUNCTIONS
//------------------------------------------------------------------------------
kern_return_t open_smc(void)
{
kern_return_t result;
io_service_t service;
service = IOServiceGetMatchingService(kIOMasterPortDefault,
IOServiceMatching(IOSERVICE_SMC));
if (service == 0) {
// NOTE: IOServiceMatching documents 0 on failure
printf("ERROR: %s NOT FOUND\n", IOSERVICE_SMC);
return kIOReturnError;
}
result = IOServiceOpen(service, mach_task_self(), 0, &conn);
IOObjectRelease(service);
return result;
}
kern_return_t close_smc(void)
{
return IOServiceClose(conn);
}
bool is_key_valid(char *key)
{
bool ans = false;
kern_return_t result;
smc_return_t result_smc;
if (strlen(key) != SMC_KEY_SIZE) {
printf("ERROR: Invalid key size - must be 4 chars\n");
return ans;
}
// Try a read and see if it succeeds
result = read_smc(key, &result_smc);
if (result == kIOReturnSuccess && result_smc.kSMC == kSMCSuccess) {
ans = true;
}
return ans;
}
double get_tmp(char *key, tmp_unit_t unit)
{
kern_return_t result;
smc_return_t result_smc;
result = read_smc(key, &result_smc);
if (!(result == kIOReturnSuccess &&
result_smc.dataSize == 2 &&
result_smc.dataType == to_uint32_t(DATA_TYPE_SP78))) {
// Error
return 0.0;
}
// TODO: Create from_sp78() convert function
double tmp = result_smc.data[0];
switch (unit) {
case CELSIUS:
break;
case FAHRENHEIT:
tmp = to_fahrenheit(tmp);
break;
case KELVIN:
tmp = to_kelvin(tmp);
break;
}
return tmp;
}
bool is_battery_powered(void)
{
kern_return_t result;
smc_return_t result_smc;
result = read_smc(BATT_PWR, &result_smc);
if (!(result == kIOReturnSuccess &&
result_smc.dataSize == 1 &&
result_smc.dataType == to_uint32_t(DATA_TYPE_FLAG))) {
// Error
return false;
}
return result_smc.data[0];
}
bool is_optical_disk_drive_full(void)
{
kern_return_t result;
smc_return_t result_smc;
result = read_smc(ODD_FULL, &result_smc);
if (!(result == kIOReturnSuccess &&
result_smc.dataSize == 1 &&
result_smc.dataType == to_uint32_t(DATA_TYPE_FLAG))) {
// Error
return false;
}
return result_smc.data[0];
}
//------------------------------------------------------------------------------
// MARK: FAN FUNCTIONS
//------------------------------------------------------------------------------
bool get_fan_name(unsigned int fan_num, fan_name_t name)
{
char key[5];
kern_return_t result;
smc_return_t result_smc;
sprintf(key, "F%dID", fan_num);
result = read_smc(key, &result_smc);
if (!(result == kIOReturnSuccess &&
result_smc.dataSize == 16 &&
result_smc.dataType == to_uint32_t(DATA_TYPE_SFDS))) {
return false;
}
/*
We know the data size is 16 bytes and the type is "{fds", a custom
struct defined by the AppleSMC.kext. See TMP enum sources for the
struct.
The last 12 bytes contain the name of the fan, an array of chars, hence
the loop range.
*/
int index = 0;
for (int i = 4; i < 16; i++) {
// Check if at the end (name may not be full 12 bytes)
// Could check for 0 (null), but instead we check for 32 (space). This
// is a hack to remove whitespace. :)
if (result_smc.data[i] == 32) {
break;
}
name[index] = result_smc.data[i];
index++;
}
return true;
}
int get_num_fans(void)
{
kern_return_t result;
smc_return_t result_smc;
result = read_smc(NUM_FANS, &result_smc);
if (!(result == kIOReturnSuccess &&
result_smc.dataSize == 1 &&
result_smc.dataType == to_uint32_t(DATA_TYPE_UINT8))) {
// Error
return -1;
}
return result_smc.data[0];
}
unsigned int get_fan_rpm(unsigned int fan_num)
{
char key[5];
kern_return_t result;
smc_return_t result_smc;
sprintf(key, "F%dAc", fan_num);
result = read_smc(key, &result_smc);
if (!(result == kIOReturnSuccess &&
result_smc.dataSize == 2 &&
result_smc.dataType == to_uint32_t(DATA_TYPE_FPE2))) {
// Error
return 0;
}
return from_fpe2(result_smc.data);
}
bool set_fan_min_rpm(unsigned int fan_num, unsigned int rpm, bool auth)
{
// TODO: Add rpm val safety check
char key[5];
bool ans = false;
kern_return_t result;
smc_return_t result_smc;
memset(&result_smc, 0, sizeof(smc_return_t));
// TODO: Don't use magic number
result_smc.dataSize = 2;
result_smc.dataType = to_uint32_t(DATA_TYPE_FPE2);
to_fpe2(rpm, result_smc.data);
sprintf(key, "F%dMn", fan_num);
result = write_smc(key, &result_smc);
if (result == kIOReturnSuccess && result_smc.kSMC == kSMCSuccess) {
ans = true;
}
return ans;
}