-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathmain.m
214 lines (178 loc) · 6.49 KB
/
main.m
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
//
// main.m
// smcutil
//
// Created by kozlek on 31.03.13.
// Copyright (c) 2013 kozlek. All rights reserved.
//
/*
* Apple System Management Control (SMC) Tool
* Copyright (C) 2006 devnull
*
* 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.
*/
#import <Foundation/Foundation.h>
#import <stdio.h>
#import "SmcHelper.h"
#define NSStr(x) [NSString stringWithCString:(x) encoding:NSASCIIStringEncoding]
#define OPTION_NONE 0
#define OPTION_LIST 1
#define OPTION_READ 2
#define OPTION_WRITE 3
#define OPTION_HELP 4
void usage(const char* prog)
{
printf("smcutil v%s\n", VERSION);
printf("Usage:\n");
printf("%s [options]\n", prog);
printf(" -l : list of all keys\n");
printf(" -r <key> : show key value\n");
printf(" -h : help\n");
printf("\n");
}
UInt32 SMCReadIndexCount(io_connect_t connection)
{
return [[SmcHelper readNumericKey:@"#KEY" connection:connection] intValue];
}
bool printKeyValue(SMCVal_t val)
{
if (val.dataSize > 0)
{
if (!strncasecmp(val.dataType, "ch8*", 4)) {
for (int i = 0; i < val.dataSize; i++)
printf("%c", (char)val.bytes[i]);
}
else if (!strncasecmp(val.dataType, "flag", 4)) {
printf(val.bytes[0] ? "TRUE" : "FALSE");
}
else if ([SmcHelper isValidIntegerSmcType:NSStr(val.dataType)]) {
printf("%d", [SmcHelper decodeNumericValueFromBuffer:val.bytes length:val.dataSize type:val.dataType].unsignedIntValue);
}
else if ([SmcHelper isValidFloatingSmcType:NSStr(val.dataType)]) {
printf("%.2f", [SmcHelper decodeNumericValueFromBuffer:val.bytes length:val.dataSize type:val.dataType].floatValue);
}
else return false;
return true;
}
else
{
printf("no data");
}
return false;
}
void printValueBytes(SMCVal_t val)
{
printf("(bytes");
if (val.dataSize > 0)
{
for (int i = 0; i < val.dataSize; i++)
printf(" %02x", (unsigned char) val.bytes[i]);
}
else {
printf(" -");
}
printf(")");
}
int main(int argc, const char * argv[])
{
@autoreleasepool {
int c, option;
char key[5];
SMCVal_t val;
option = OPTION_HELP;
while ((c = getopt(argc, argv, "lr")) != -1)
{
switch(c)
{
case 'l':
option = OPTION_LIST;
break;
case 'r':
option = OPTION_READ;
break;
case 'w':
option = OPTION_WRITE;
break;
case 'h':
case '?':
default:
usage(argv[0]);
return 1;
}
}
io_connect_t connection;
if (kIOReturnSuccess == SMCOpen("AppleSMC", &connection)) {
switch (option) {
case OPTION_LIST: {
UInt32 count = SMCReadIndexCount(connection);
for (UInt32 index = 0; index < count; index++) {
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.data8 = SMC_CMD_READ_INDEX;
inputStructure.data32 = index;
if (kIOReturnSuccess == SMCCall(connection, KERNEL_INDEX_SMC, &inputStructure, &outputStructure)) {
_ultostr(key, outputStructure.key);
if (kIOReturnSuccess == SMCReadKey(connection, key, &val)) {
printf(" %-4s [%-4s] ", val.key, val.dataType);
if (printKeyValue(val))
printf(" ");
printValueBytes(val);
printf("\n");
}
}
}
break;
}
case OPTION_READ:
snprintf(key, 5, "%s", argv[2]);
if (kIOReturnSuccess == SMCReadKey(connection, key, &val)) {
printKeyValue(val);
}
break;
case OPTION_WRITE: {
const char *args = argv[3];
snprintf(key, 5, "%s", argv[2]);
bcopy(val.key, key, 4);
int i;
char chr[3];
for (i = 0; i < strlen(args); i++)
{
sprintf(chr, "%c%c", args[i * 2], args[(i * 2) + 1]);
val.bytes[i] = (int) strtol(chr, NULL, 16);
}
val.dataSize = i / 2;
if ((val.dataSize * 2) != strlen(args))
{
printf("Error: value is not valid\n");
return 1;
}
if (kIOReturnSuccess == SMCWriteKey(connection, &val)) {
printKeyValue(val);
}
break;
}
case OPTION_HELP:
usage(argv[0]);
break;
}
SMCClose(connection);
}
else {
printf("failed to connect to SMC!\n");
}
}
return 0;
}