forked from alexleigh/smc_util
-
Notifications
You must be signed in to change notification settings - Fork 26
/
SmcDumpKey.c
234 lines (196 loc) · 5.9 KB
/
SmcDumpKey.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
/*
* prints out 4-character name of the SMC key at given index position;
*
* by Gabriel L. Somlo <[email protected]>, Summer 2014
*
* Compile with: gcc -O2 -o SmcDumpKey SmcDumpKey.c -Wall
*
* You probably want to "modprobe -r applesmc" before running this...
*
* Code bits and pieces shamelessly ripped from the linux kernel driver
* (drivers/hwmon/applesmc.c by N. Boichat and H. Rydberg)
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License v2 as published by the
* Free Software Foundation.
*
* 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 <stdlib.h>
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <sys/io.h>
#include <linux/byteorder/little_endian.h>
#define APPLESMC_START 0x300
#define APPLESMC_RANGE 0x20
#define APPLESMC_DATA_PORT (APPLESMC_START + 0x00)
#define APPLESMC_CMD_PORT (APPLESMC_START + 0x04)
#define APPLESMC_READ_CMD 0x10
#define APPLESMC_WRITE_CMD 0x11
#define APPLESMC_GET_KEY_BY_INDEX_CMD 0x12
#define APPLESMC_GET_KEY_TYPE_CMD 0x13
/* wait up to 128 ms for a status change. */
#define APPLESMC_MIN_WAIT 0x0010
#define APPLESMC_RETRY_WAIT 0x0100
#define APPLESMC_MAX_WAIT 0x20000
#define APPLESMC_KEY_NAME_LEN 4
#define APPLESMC_KEY_TYPE_LEN 4
typedef struct key_type {
uint8_t data_len;
uint8_t data_type[APPLESMC_KEY_TYPE_LEN];
uint8_t flags;
} __attribute__((packed)) key_type;
/* wait_read - Wait for a byte to appear on SMC port. */
static int
wait_read(void)
{
uint8_t status;
int us;
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
usleep(us);
status = inb(APPLESMC_CMD_PORT);
/* read: wait for smc to settle */
if (status & 0x01)
return 0;
}
fprintf(stderr, "wait_read() fail: 0x%02x\n", status);
return -1;
}
/*send_byte - Write to SMC port, retrying when necessary. */
static int
send_byte(uint8_t cmd, unsigned short port)
{
uint8_t status;
int us;
outb(cmd, port);
for (us = APPLESMC_MIN_WAIT; us < APPLESMC_MAX_WAIT; us <<= 1) {
usleep(us);
status = inb(APPLESMC_CMD_PORT);
/* write: wait for smc to settle */
if (status & 0x02)
continue;
/* ready: cmd accepted, return */
if (status & 0x04)
return 0;
/* timeout: give up */
if (us << 1 == APPLESMC_MAX_WAIT)
break;
/* busy: long wait and resend */
usleep(APPLESMC_RETRY_WAIT);
outb(cmd, port);
}
fprintf(stderr,
"send_byte(0x%02x, 0x%04x) fail: 0x%02x\n", cmd, port, status);
return -1;
}
static int
send_argument(const uint8_t *key)
{
int i;
for (i = 0; i < APPLESMC_KEY_NAME_LEN; i++)
if (send_byte(key[i], APPLESMC_DATA_PORT))
return -1;
return 0;
}
static int
read_smc(uint8_t cmd, const uint8_t *key, uint8_t *buf, uint8_t len)
{
int i;
if (send_byte(cmd, APPLESMC_CMD_PORT) || send_argument(key)) {
fprintf(stderr, "%.4s: read arg fail\n", key);
return -1;
}
if (send_byte(len, APPLESMC_DATA_PORT)) {
fprintf(stderr, "%.4s: read len fail\n", key);
return -1;
}
for (i = 0; i < len; i++) {
if (wait_read()) {
fprintf(stderr, "%.4s: read data[%d] fail\n", key, i);
return -1;
}
buf[i] = inb(APPLESMC_DATA_PORT);
}
return 0;
}
int write_smc(uint8_t cmd, const uint8_t *key, uint8_t *buffer, uint8_t len)
{
int i;
if (send_byte(cmd, APPLESMC_CMD_PORT) || send_argument(key)) {
fprintf(stderr,"%.4s: write arg fail\n", key);
return -1;
}
if (send_byte(len, APPLESMC_DATA_PORT)) {
fprintf(stderr,"%.4s: write len fail\n", key);
return -1;
}
for (i = 0; i < len; i++) {
if (send_byte(buffer[i], APPLESMC_DATA_PORT)) {
fprintf(stderr,"%s: write data fail\n", key);
return -1;
}
}
return 0;
}
int
main(int argc, char **argv)
{
key_type kt;
uint8_t data_buf[UCHAR_MAX];
uint8_t i;
if (argc < 2 || strlen(argv[1]) != APPLESMC_KEY_NAME_LEN) {
fprintf(stderr, "\nUsage: %s <4-char-key-name>\n\n", argv[0]);
return -1;
}
if (ioperm(APPLESMC_START, APPLESMC_RANGE, 1) != 0) {
perror("ioperm failed");
return -2;
}
if (read_smc(APPLESMC_GET_KEY_TYPE_CMD,
(uint8_t *)argv[1], (uint8_t *)&kt, sizeof(kt)) != 0) {
fprintf(stderr, "\nread_smc get_key_type error\n\n");
return -3;
}
printf(" type=\"");
for (i = 0; i < APPLESMC_KEY_TYPE_LEN; i++)
printf(isprint(kt.data_type[i]) ? "%c" : "\\x%02x",
(uint8_t)kt.data_type[i]);
printf("\" length=%d flags=%x\n", kt.data_len, kt.flags);
if (read_smc(APPLESMC_READ_CMD,
(uint8_t *)argv[1], data_buf, kt.data_len) != 0) {
fprintf(stderr, "\nread_smc get_key_data error\n\n");
return -4;
}
printf(" data=\"");
for (i = 0; i < kt.data_len; i++)
printf(isprint(data_buf[i]) ? "%c" : "\\x%02x",
(uint8_t)data_buf[i]);
printf("\"\n");
if (argc == 3) {
uint8_t val = atoi(argv[2]);
printf("target value: %02x\n",val);
if (write_smc(APPLESMC_WRITE_CMD,(uint8_t*)argv[1],&val,1))
fprintf(stderr,"\nwrite_smc get_key_data error\n\n");
}
if (argc == 4) {
uint8_t val[2];
val[0] = atoi(argv[2]);
val[1] = atoi(argv[3]);
printf("target value: %02x%02x\n",val[0],val[1]);
if (write_smc(APPLESMC_WRITE_CMD,(uint8_t*)argv[1],val,2))
fprintf(stderr,"\nwrite_smc get_key_data error\n\n");
}
return 0;
}