-
Notifications
You must be signed in to change notification settings - Fork 304
/
smc.c
executable file
·796 lines (672 loc) · 21.6 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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
* Apple System Management Control (SMC) Tool
* Copyright (C) 2006 devnull
* Portions Copyright (C) 2013 Michael Wilber
*
* 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 <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <IOKit/IOKitLib.h>
#include "smc.h"
#include <libkern/OSAtomic.h>
// Cache the keyInfo to lower the energy impact of SMCReadKey() / SMCReadKey2()
#define KEY_INFO_CACHE_SIZE 100
struct {
UInt32 key;
SMCKeyData_keyInfo_t keyInfo;
} g_keyInfoCache[KEY_INFO_CACHE_SIZE];
int g_keyInfoCacheCount = 0;
OSSpinLock g_keyInfoSpinLock = 0;
kern_return_t SMCCall2(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure, io_connect_t conn);
#pragma mark C Helpers
UInt32 _strtoul(char *str, int size, int base)
{
UInt32 total = 0;
int i;
for (i = 0; i < size; i++)
{
if (base == 16)
total += str[i] << (size - 1 - i) * 8;
else
total += ((unsigned char) (str[i]) << (size - 1 - i) * 8);
}
return total;
}
void _ultostr(char *str, UInt32 val)
{
str[0] = '\0';
sprintf(str, "%c%c%c%c",
(unsigned int) val >> 24,
(unsigned int) val >> 16,
(unsigned int) val >> 8,
(unsigned int) val);
}
float _strtof(unsigned char *str, int size, int e)
{
float total = 0;
int i;
for (i = 0; i < size; i++)
{
if (i == (size - 1))
total += (str[i] & 0xff) >> e;
else
total += str[i] << (size - 1 - i) * (8 - e);
}
total += (str[size-1] & 0x03) * 0.25;
return total;
}
void printFLT(SMCVal_t val)
{
float fval;
memcpy(&fval,val.bytes,sizeof(float));
printf("%.0f ", fval);
}
void printFP1F(SMCVal_t val)
{
printf("%.5f ", ntohs(*(UInt16*)val.bytes) / 32768.0);
}
void printFP4C(SMCVal_t val)
{
printf("%.5f ", ntohs(*(UInt16*)val.bytes) / 4096.0);
}
void printFP5B(SMCVal_t val)
{
printf("%.5f ", ntohs(*(UInt16*)val.bytes) / 2048.0);
}
void printFP6A(SMCVal_t val)
{
printf("%.4f ", ntohs(*(UInt16*)val.bytes) / 1024.0);
}
void printFP79(SMCVal_t val)
{
printf("%.4f ", ntohs(*(UInt16*)val.bytes) / 512.0);
}
void printFP88(SMCVal_t val)
{
printf("%.3f ", ntohs(*(UInt16*)val.bytes) / 256.0);
}
void printFPA6(SMCVal_t val)
{
printf("%.2f ", ntohs(*(UInt16*)val.bytes) / 64.0);
}
void printFPC4(SMCVal_t val)
{
printf("%.2f ", ntohs(*(UInt16*)val.bytes) / 16.0);
}
void printFPE2(SMCVal_t val)
{
printf("%.2f ", ntohs(*(UInt16*)val.bytes) / 4.0);
}
void printUInt(SMCVal_t val)
{
printf("%u ", (unsigned int) _strtoul((char *)val.bytes, val.dataSize, 10));
}
void printSP1E(SMCVal_t val)
{
printf("%.5f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 16384.0);
}
void printSP3C(SMCVal_t val)
{
printf("%.5f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 4096.0);
}
void printSP4B(SMCVal_t val)
{
printf("%.4f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 2048.0);
}
void printSP5A(SMCVal_t val)
{
printf("%.4f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 1024.0);
}
void printSP69(SMCVal_t val)
{
printf("%.3f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 512.0);
}
void printSP78(SMCVal_t val)
{
printf("%.3f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 256.0);
}
void printSP87(SMCVal_t val)
{
printf("%.3f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 128.0);
}
void printSP96(SMCVal_t val)
{
printf("%.2f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 64.0);
}
void printSPB4(SMCVal_t val)
{
printf("%.2f ", ((SInt16)ntohs(*(UInt16*)val.bytes)) / 16.0);
}
void printSPF0(SMCVal_t val)
{
printf("%.0f ", (float)ntohs(*(UInt16*)val.bytes));
}
void printSI8(SMCVal_t val)
{
printf("%d ", (signed char)*val.bytes);
}
void printSI16(SMCVal_t val)
{
printf("%d ", ntohs(*(SInt16*)val.bytes));
}
void printPWM(SMCVal_t val)
{
printf("%.1f%% ", ntohs(*(UInt16*)val.bytes) * 100 / 65536.0);
}
void printBytesHex(SMCVal_t val)
{
int i;
printf("(bytes");
for (i = 0; i < val.dataSize; i++)
printf(" %02x", (unsigned char) val.bytes[i]);
printf(")\n");
}
void printVal(SMCVal_t val)
{
printf(" %-4s [%-4s] ", val.key, val.dataType);
if (val.dataSize > 0)
{
if ((strcmp(val.dataType, DATATYPE_UINT8) == 0) ||
(strcmp(val.dataType, DATATYPE_UINT16) == 0) ||
(strcmp(val.dataType, DATATYPE_UINT32) == 0))
printUInt(val);
else if (strcmp(val.dataType, DATATYPE_FLT) == 0 && val.dataSize == 4)
printFLT(val);
else if (strcmp(val.dataType, DATATYPE_FP1F) == 0 && val.dataSize == 2)
printFP1F(val);
else if (strcmp(val.dataType, DATATYPE_FP4C) == 0 && val.dataSize == 2)
printFP4C(val);
else if (strcmp(val.dataType, DATATYPE_FP5B) == 0 && val.dataSize == 2)
printFP5B(val);
else if (strcmp(val.dataType, DATATYPE_FP6A) == 0 && val.dataSize == 2)
printFP6A(val);
else if (strcmp(val.dataType, DATATYPE_FP79) == 0 && val.dataSize == 2)
printFP79(val);
else if (strcmp(val.dataType, DATATYPE_FP88) == 0 && val.dataSize == 2)
printFP88(val);
else if (strcmp(val.dataType, DATATYPE_FPA6) == 0 && val.dataSize == 2)
printFPA6(val);
else if (strcmp(val.dataType, DATATYPE_FPC4) == 0 && val.dataSize == 2)
printFPC4(val);
else if (strcmp(val.dataType, DATATYPE_FPE2) == 0 && val.dataSize == 2)
printFPE2(val);
else if (strcmp(val.dataType, DATATYPE_SP1E) == 0 && val.dataSize == 2)
printSP1E(val);
else if (strcmp(val.dataType, DATATYPE_SP3C) == 0 && val.dataSize == 2)
printSP3C(val);
else if (strcmp(val.dataType, DATATYPE_SP4B) == 0 && val.dataSize == 2)
printSP4B(val);
else if (strcmp(val.dataType, DATATYPE_SP5A) == 0 && val.dataSize == 2)
printSP5A(val);
else if (strcmp(val.dataType, DATATYPE_SP69) == 0 && val.dataSize == 2)
printSP69(val);
else if (strcmp(val.dataType, DATATYPE_SP78) == 0 && val.dataSize == 2)
printSP78(val);
else if (strcmp(val.dataType, DATATYPE_SP87) == 0 && val.dataSize == 2)
printSP87(val);
else if (strcmp(val.dataType, DATATYPE_SP96) == 0 && val.dataSize == 2)
printSP96(val);
else if (strcmp(val.dataType, DATATYPE_SPB4) == 0 && val.dataSize == 2)
printSPB4(val);
else if (strcmp(val.dataType, DATATYPE_SPF0) == 0 && val.dataSize == 2)
printSPF0(val);
else if (strcmp(val.dataType, DATATYPE_SI8) == 0 && val.dataSize == 1)
printSI8(val);
else if (strcmp(val.dataType, DATATYPE_SI16) == 0 && val.dataSize == 2)
printSI16(val);
else if (strcmp(val.dataType, DATATYPE_PWM) == 0 && val.dataSize == 2)
printPWM(val);
else if (strcmp(val.dataType, DATATYPE_FLT) == 0 && val.dataSize == 4)
printFLT(val);
printBytesHex(val);
}
else
{
printf("no data\n");
}
}
#pragma mark Shared SMC functions
kern_return_t SMCOpen(io_connect_t *conn)
{
kern_return_t result;
mach_port_t masterPort;
io_iterator_t iterator;
io_object_t device;
IOMasterPort(MACH_PORT_NULL, &masterPort);
CFMutableDictionaryRef matchingDictionary = IOServiceMatching("AppleSMC");
result = IOServiceGetMatchingServices(masterPort, matchingDictionary, &iterator);
if (result != kIOReturnSuccess)
{
printf("Error: IOServiceGetMatchingServices() = %08x\n", result);
return 1;
}
device = IOIteratorNext(iterator);
IOObjectRelease(iterator);
if (device == 0)
{
printf("Error: no SMC found\n");
return 1;
}
result = IOServiceOpen(device, mach_task_self(), 0, conn);
IOObjectRelease(device);
if (result != kIOReturnSuccess)
{
printf("Error: IOServiceOpen() = %08x\n", result);
return 1;
}
return kIOReturnSuccess;
}
kern_return_t SMCClose(io_connect_t conn)
{
return IOServiceClose(conn);
}
kern_return_t SMCCall2(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure,io_connect_t conn)
{
size_t structureInputSize;
size_t structureOutputSize;
structureInputSize = sizeof(SMCKeyData_t);
structureOutputSize = sizeof(SMCKeyData_t);
return IOConnectCallStructMethod(conn, index, inputStructure, structureInputSize, outputStructure, &structureOutputSize);
}
// Provides key info, using a cache to dramatically improve the energy impact of smcFanControl
kern_return_t SMCGetKeyInfo(UInt32 key, SMCKeyData_keyInfo_t* keyInfo, io_connect_t conn)
{
SMCKeyData_t inputStructure;
SMCKeyData_t outputStructure;
kern_return_t result = kIOReturnSuccess;
int i = 0;
OSSpinLockLock(&g_keyInfoSpinLock);
for (; i < g_keyInfoCacheCount; ++i)
{
if (key == g_keyInfoCache[i].key)
{
*keyInfo = g_keyInfoCache[i].keyInfo;
break;
}
}
if (i == g_keyInfoCacheCount)
{
// Not in cache, must look it up.
memset(&inputStructure, 0, sizeof(inputStructure));
memset(&outputStructure, 0, sizeof(outputStructure));
inputStructure.key = key;
inputStructure.data8 = SMC_CMD_READ_KEYINFO;
result = SMCCall2(KERNEL_INDEX_SMC, &inputStructure, &outputStructure, conn);
if (result == kIOReturnSuccess)
{
*keyInfo = outputStructure.keyInfo;
if (g_keyInfoCacheCount < KEY_INFO_CACHE_SIZE)
{
g_keyInfoCache[g_keyInfoCacheCount].key = key;
g_keyInfoCache[g_keyInfoCacheCount].keyInfo = outputStructure.keyInfo;
++g_keyInfoCacheCount;
}
}
}
OSSpinLockUnlock(&g_keyInfoSpinLock);
return result;
}
kern_return_t SMCReadKey2(UInt32Char_t key, SMCVal_t *val,io_connect_t conn)
{
kern_return_t result;
SMCKeyData_t inputStructure;
SMCKeyData_t outputStructure;
memset(&inputStructure, 0, sizeof(SMCKeyData_t));
memset(&outputStructure, 0, sizeof(SMCKeyData_t));
memset(val, 0, sizeof(SMCVal_t));
inputStructure.key = _strtoul(key, 4, 16);
sprintf(val->key, key);
result = SMCGetKeyInfo(inputStructure.key, &outputStructure.keyInfo, conn);
if (result != kIOReturnSuccess)
{
return result;
}
val->dataSize = outputStructure.keyInfo.dataSize;
_ultostr(val->dataType, outputStructure.keyInfo.dataType);
inputStructure.keyInfo.dataSize = val->dataSize;
inputStructure.data8 = SMC_CMD_READ_BYTES;
result = SMCCall2(KERNEL_INDEX_SMC, &inputStructure, &outputStructure,conn);
if (result != kIOReturnSuccess)
{
return result;
}
memcpy(val->bytes, outputStructure.bytes, sizeof(outputStructure.bytes));
return kIOReturnSuccess;
}
#pragma mark Command line only
// Exclude command-line only code from smcFanControl UI
#ifdef CMD_TOOL_BUILD
io_connect_t g_conn = 0;
void smc_init(){
SMCOpen(&g_conn);
}
void smc_close(){
SMCClose(g_conn);
}
kern_return_t SMCCall(int index, SMCKeyData_t *inputStructure, SMCKeyData_t *outputStructure)
{
return SMCCall2(index, inputStructure, outputStructure, g_conn);
}
kern_return_t SMCReadKey(UInt32Char_t key, SMCVal_t *val)
{
return SMCReadKey2(key, val, g_conn);
}
kern_return_t SMCWriteKey2(SMCVal_t writeVal, io_connect_t conn)
{
kern_return_t result;
SMCKeyData_t inputStructure;
SMCKeyData_t outputStructure;
SMCVal_t readVal;
result = SMCReadKey2(writeVal.key, &readVal,conn);
if (result != kIOReturnSuccess)
return result;
if (readVal.dataSize != writeVal.dataSize)
return kIOReturnError;
memset(&inputStructure, 0, sizeof(SMCKeyData_t));
memset(&outputStructure, 0, sizeof(SMCKeyData_t));
inputStructure.key = _strtoul(writeVal.key, 4, 16);
inputStructure.data8 = SMC_CMD_WRITE_BYTES;
inputStructure.keyInfo.dataSize = writeVal.dataSize;
memcpy(inputStructure.bytes, writeVal.bytes, sizeof(writeVal.bytes));
result = SMCCall2(KERNEL_INDEX_SMC, &inputStructure, &outputStructure,conn);
if (result != kIOReturnSuccess)
return result;
return kIOReturnSuccess;
}
kern_return_t SMCWriteKey(SMCVal_t writeVal)
{
return SMCWriteKey2(writeVal, g_conn);
}
UInt32 SMCReadIndexCount(void)
{
SMCVal_t val;
SMCReadKey("#KEY", &val);
return _strtoul((char *)val.bytes, val.dataSize, 10);
}
kern_return_t SMCPrintAll(void)
{
kern_return_t result;
SMCKeyData_t inputStructure;
SMCKeyData_t outputStructure;
int totalKeys, i;
UInt32Char_t key;
SMCVal_t val;
totalKeys = SMCReadIndexCount();
for (i = 0; i < totalKeys; i++)
{
memset(&inputStructure, 0, sizeof(SMCKeyData_t));
memset(&outputStructure, 0, sizeof(SMCKeyData_t));
memset(&val, 0, sizeof(SMCVal_t));
inputStructure.data8 = SMC_CMD_READ_INDEX;
inputStructure.data32 = i;
result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
if (result != kIOReturnSuccess)
continue;
_ultostr(key, outputStructure.key);
SMCReadKey(key, &val);
printVal(val);
}
return kIOReturnSuccess;
}
//Fix me with other types
float getFloatFromVal(SMCVal_t val)
{
float fval = -1.0f;
if (val.dataSize > 0)
{
if (strcmp(val.dataType, DATATYPE_FLT) == 0 && val.dataSize == 4) {
memcpy(&fval,val.bytes,sizeof(float));
}
else if (strcmp(val.dataType, DATATYPE_FPE2) == 0 && val.dataSize == 2) {
fval = _strtof(val.bytes, val.dataSize, 2);
}
else if (strcmp(val.dataType, DATATYPE_UINT16) == 0 && val.dataSize == 2) {
fval = (float)_strtoul((char *)val.bytes, val.dataSize, 10);
}
else if (strcmp(val.dataType, DATATYPE_UINT8) == 0 && val.dataSize == 1) {
fval = (float)_strtoul((char *)val.bytes, val.dataSize, 10);
}
}
return fval;
}
kern_return_t SMCPrintFans(void)
{
kern_return_t result;
SMCVal_t val;
UInt32Char_t key;
int totalFans, i;
result = SMCReadKey("FNum", &val);
if (result != kIOReturnSuccess)
return kIOReturnError;
totalFans = _strtoul((char *)val.bytes, val.dataSize, 10);
printf("Total fans in system: %d\n", totalFans);
for (i = 0; i < totalFans; i++)
{
printf("\nFan #%d:\n", i);
sprintf(key, "F%cID", fannum[i]);
SMCReadKey(key, &val);
if(val.dataSize > 0) {
printf(" Fan ID : %s\n", val.bytes+4);
}
sprintf(key, "F%cAc", fannum[i]);
SMCReadKey(key, &val);
printf(" Current speed : %.0f\n", getFloatFromVal(val));
sprintf(key, "F%cMn", fannum[i]);
SMCReadKey(key, &val);
printf(" Minimum speed: %.0f\n", getFloatFromVal(val));
sprintf(key, "F%cMx", fannum[i]);
SMCReadKey(key, &val);
printf(" Maximum speed: %.0f\n", getFloatFromVal(val));
sprintf(key, "F%cSf", fannum[i]);
SMCReadKey(key, &val);
printf(" Safe speed : %.0f\n", getFloatFromVal(val));
sprintf(key, "F%cTg", fannum[i]);
SMCReadKey(key, &val);
printf(" Target speed : %.0f\n", getFloatFromVal(val));
SMCReadKey("FS! ", &val);
if(val.dataSize > 0) {
if ((_strtoul((char *)val.bytes, 2, 16) & (1 << i)) == 0)
printf(" Mode : auto\n");
else
printf(" Mode : forced\n");
}
else {
sprintf(key, "F%dMd", i);
SMCReadKey(key, &val);
if (getFloatFromVal(val))
printf(" Mode : forced\n");
else
printf(" Mode : auto\n");
}
}
return kIOReturnSuccess;
}
kern_return_t SMCPrintTemps(void)
{
kern_return_t result;
SMCKeyData_t inputStructure;
SMCKeyData_t outputStructure;
int totalKeys, i;
UInt32Char_t key;
SMCVal_t val;
totalKeys = SMCReadIndexCount();
for (i = 0; i < totalKeys; i++)
{
memset(&inputStructure, 0, sizeof(SMCKeyData_t));
memset(&outputStructure, 0, sizeof(SMCKeyData_t));
memset(&val, 0, sizeof(SMCVal_t));
inputStructure.data8 = SMC_CMD_READ_INDEX;
inputStructure.data32 = i;
result = SMCCall(KERNEL_INDEX_SMC, &inputStructure, &outputStructure);
if (result != kIOReturnSuccess)
continue;
_ultostr(key, outputStructure.key);
if ( key[0] != 'T' )
continue;
SMCReadKey(key, &val);
//printVal(val);
if (strcmp(val.dataType, DATATYPE_SP78) == 0 && val.dataSize == 2) {
printf("%-4s ", val.key);
printSP78(val);
printf("\n");
}
}
return kIOReturnSuccess;
}
void usage(char* prog)
{
printf("Apple System Management Control (SMC) tool %s\n", VERSION);
printf("Usage:\n");
printf("%s [options]\n", prog);
printf(" -f : fan info decoded\n");
printf(" -t : list all temperatures\n");
printf(" -h : help\n");
printf(" -k <key> : key to manipulate\n");
printf(" -l : list all keys and values\n");
printf(" -r : read the value of a key\n");
printf(" -w <value> : write the specified value to a key\n");
printf(" -v : version\n");
printf("\n");
}
kern_return_t SMCWriteSimple(UInt32Char_t key, char *wvalue, io_connect_t conn)
{
kern_return_t result;
SMCVal_t val;
int i;
char c[3];
for (i = 0; i < strlen(wvalue); i++)
{
sprintf(c, "%c%c", wvalue[i * 2], wvalue[(i * 2) + 1]);
val.bytes[i] = (int) strtol(c, NULL, 16);
}
val.dataSize = i / 2;
sprintf(val.key, key);
result = SMCWriteKey2(val, conn);
if (result != kIOReturnSuccess)
printf("Error: SMCWriteKey() = %08x\n", result);
return result;
}
int main(int argc, char *argv[])
{
int c;
extern char *optarg;
kern_return_t result;
int op = OP_NONE;
UInt32Char_t key = { 0 };
SMCVal_t val;
while ((c = getopt(argc, argv, "fthk:lrw:v")) != -1)
{
switch(c)
{
case 'f':
op = OP_READ_FAN;
break;
case 't':
op = OP_READ_TEMPS;
break;
case 'k':
strncpy(key, optarg, sizeof(key)); //fix for buffer overflow
key[sizeof(key) - 1] = '\0';
break;
case 'l':
op = OP_LIST;
break;
case 'r':
op = OP_READ;
break;
case 'v':
printf("%s\n", VERSION);
return 0;
break;
case 'w':
op = OP_WRITE;
{
int i;
char c[3];
for (i = 0; i < strlen(optarg); i++)
{
sprintf(c, "%c%c", optarg[i * 2], optarg[(i * 2) + 1]);
val.bytes[i] = (int) strtol(c, NULL, 16);
}
val.dataSize = i / 2;
if ((val.dataSize * 2) != strlen(optarg))
{
printf("Error: value is not valid\n");
return 1;
}
}
break;
case 'h':
case '?':
op = OP_NONE;
break;
}
}
if (op == OP_NONE)
{
usage(argv[0]);
return 1;
}
smc_init();
switch(op)
{
case OP_LIST:
result = SMCPrintAll();
if (result != kIOReturnSuccess)
printf("Error: SMCPrintAll() = %08x\n", result);
break;
case OP_READ:
if (strlen(key) > 0)
{
result = SMCReadKey(key, &val);
if (result != kIOReturnSuccess)
printf("Error: SMCReadKey() = %08x\n", result);
else
printVal(val);
}
else
{
printf("Error: specify a key to read\n");
}
break;
case OP_READ_FAN:
result = SMCPrintFans();
if (result != kIOReturnSuccess)
printf("Error: SMCPrintFans() = %08x\n", result);
break;
case OP_READ_TEMPS:
result = SMCPrintTemps();
if (result != kIOReturnSuccess)
printf("Error: SMCPrintFans() = %08x\n", result);
break;
case OP_WRITE:
if (strlen(key) > 0)
{
sprintf(val.key, key);
result = SMCWriteKey(val);
if (result != kIOReturnSuccess)
printf("Error: SMCWriteKey() = %08x\n", result);
}
else
{
printf("Error: specify a key to write\n");
}
break;
}
smc_close();
return 0;
}
#endif //#ifdef CMD_TOOL