aboutsummaryrefslogtreecommitdiff
path: root/smctool.c
blob: fda79040a5e257cc214491340a2d5183206e6e54 (plain)
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
/*
 * This file is part of the coreboot project.
 *
 * Copyright (C) 2019 Evgeny Zinoviev <me@ch1p.io>
 *
 * 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; version 2 of the License.
 *
 * 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.
 */

#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <endian.h>
#include <sys/io.h>
#include <stdio.h>
#include <errno.h>
#include "smc.h"
#include "smctool.h"

static bool fp_bits(char c, uint8_t *dest)
{
	if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
		*dest = strtol(&c, NULL, 16);
		return true;
	}
	return false;
}

static void print_bits(uint8_t len, uint8_t *ptr)
{
	for (int i = len - 1; i >= 0; i--) {
		for (int j = 7; j >= 0; j--)
			printf("%u", (ptr[i] >> j) & 1);

		if (i > 0)
			printf(" ");
	}
}

static void print_usage(const char *name)
{
	printf("usage: %s <options>\n", name);
	printf("\n"
		"Options:\n"
		"    -h, --help:        print this help\n"
		"    -k, --key <name>:  key name\n"
		"    -t, --type <type>: data type, see below\n"
		"    --output-hex\n"
		"    --output-bin\n"
		"\n"
		"Supported data types:\n"
		"    ui8, ui16, ui32, si8, si16, flag, fpXY, spXY\n"
		"\n"
		"    fp and sp are unsigned and signed fixed point\n"
		"    data types respectively.\n"
		"\n"
		"    The X in fp and sp data types is integer bits count\n"
		"    and Y is fraction bits count.\n"
		"\n"
		"    For example,\n"
		"    fpe2 means 14 integer bits, 2 fraction bits,\n"
		"    sp78 means 7 integer bits, 8 fraction bits\n"
		"    (and one sign bit).\n"
		"\n");
}


enum output_format {dec, hex, bin};

enum smctype {none, ui8, ui16, ui32, si8, si16, flag, fp, sp};
struct {
	enum smctype type;
	uint8_t len;
	char *str;
	char *decfmt;
	char *hexfmt;
} types[] = {
	{ui8,  1, "ui8",  "%u", "0x%02x"},
	{ui16, 2, "ui16", "%u", "0x%04x"},
	{ui32, 4, "ui32", "%u", "0x%08x"},
	{si8,  1, "si8",  "%d", "0x%02x"},
	{si16, 2, "si16", "%d", "0x%04x"},
	{flag, 1, "flag", "%u", "0x%01x"},
};

int main(int argc, char *argv[])
{
	bool show_help = false;
	int opt, option_index = 0;
	char name[KEYBUFSIZE+1] = {0};
	char typebuf[KEYBUFSIZE+1] = {0};
	uint8_t fp_int_bits = 0, fp_fraction_bits = 0;

	enum output_format of = dec;
	char *ofmt = NULL;
	char fmtbuf[32];

	enum smctype type = none;
	uint8_t len;

	struct option long_options[] = {
		{"help",       0, 0, 'h'},
		{"key",	       1, 0, 'k'},
		{"type",       1, 0, 't'},
		{"output-hex", 0, 0, 1000},
		{"output-bin", 0, 0, 1001},
		{0, 0, 0, 0}
	};

	if (argv[1] == NULL) {
		print_usage(argv[0]);
		exit(0);
	}

	while ((opt = getopt_long(argc, argv, "hk:t:",
				  long_options, &option_index)) != EOF) {
		switch (opt) {
		case 'h':
			show_help = true;
			break;

		case 'k':
			snprintf(name, KEYBUFSIZE, "%s", optarg);
			break;

		case 't':
			snprintf(typebuf, KEYBUFSIZE, "%s", optarg);
			break;

		case 1000: // output-hex
			of = hex;
			break;

		case 1001: // output-bin
			of = bin;
			break;
		}
	}

	if (optind < argc) {
		fprintf(stderr, "Error: Extra parameter found.\n");
		print_usage(argv[0]);
		exit(1);
	}

	if (show_help) {
		print_usage(argv[0]);
		exit(0);
	}

	/* Validate key name */
	if (strlen(name) != 4) {
		fprintf(stderr, "Key name must be 4 characters long.\n");
		exit(1);
	}

	/* Validate key type */
	if ((typebuf[0] == 'f' || typebuf[0] == 's') && typebuf[1] == 'p') {
		if (!fp_bits(typebuf[2], &fp_int_bits)
			|| !fp_bits(typebuf[3], &fp_fraction_bits)) {
			fprintf(stderr, "Invalid fixed point data type.\n");
			exit(1);
		}
		if (typebuf[0] == 'f' && fp_int_bits + fp_fraction_bits != 16) {
			fprintf(stderr,
				"Invalid unsigned fixed point data type.\n");
			exit(1);
		}
		if (typebuf[0] == 's' && fp_int_bits + fp_fraction_bits != 15) {
			fprintf(stderr,
				"Invalid signed fixed point data type.\n");
			exit(1);
		}
		type = typebuf[0] == 'f' ? fp : sp;
		len = 2;
		ofmt = "%d.%u";
	}

	if (type == none) {
		for (unsigned int i = 0; i < ARRAY_SIZE(types); i++) {
			if (strcmp(typebuf, types[i].str))
				continue;

			type = types[i].type;
			len = types[i].len;
			if (of == dec)
				ofmt = types[i].decfmt;
			else if (of == hex)
				ofmt = types[i].hexfmt;
			break;
		}
	}

	if (type == none) {
		fprintf(stderr, "Key type \"%s\" is not known.\n", typebuf);
		exit(1);
	}

	/* Check permissions */
	if (geteuid() != 0) {
		fprintf(stderr, "You must be root.\n");
		exit(1);
	}

	/* Open SMC port */
	if (ioperm(APPLESMC_DATA_PORT, 0x10, 1)) {
		fprintf(stderr, "ioperm: %s\n", strerror(errno));
		exit(1);
	}

	/* Read key from SMC */
	int retval;
	uint32_t buf = 0;

	retval = read_smc(APPLESMC_READ_CMD, name, (uint8_t *)&buf, len);
	assert(retval == 0);

	/* Handle returned value according to the requested type */
	buf = be32toh(buf);

	int32_t fp_int = 0;
	uint16_t fp_fraction = 0;
	bool fp_sign = false;

	switch (type) {
	case flag:
		buf = (buf >> 24) & 1;
		break;

	case ui8:
		buf = (buf >> 24) & 0xff;
		break;

	case si8:
		buf = (buf >> 24) & 0xff;
		if (of == dec)
			buf = (int8_t)buf;
		break;

	case ui16:
		buf = (buf >> 16) & 0xffff;
		break;

	case si16:
		buf = (buf >> 16) & 0xffff;
		if (of == dec)
			buf = (int16_t)buf;
		break;

	case fp:
		buf = (buf >> 16) & 0xffff;
		fp_int = buf >> fp_fraction_bits;
		fp_fraction = buf & (0xffff >> fp_int_bits);
		break;

	case sp:
		buf = (buf >> 16) & 0xffff;
		fp_sign = ((buf >> 15) & 1) == 1;

		buf &= ~(1 << 15);

		fp_int = buf >> fp_fraction_bits;
		fp_fraction = buf & (0xffff >> fp_int_bits);

		if (fp_sign)
			fp_int *= -1;
		break;

	case ui32:
	case none: // to avoid gcc warning
		break;
	}

	/* Output the result */
	if (of != bin) {
		strcpy(fmtbuf, "%s = ");
		strcat(fmtbuf, ofmt);
		strcat(fmtbuf, "\n");

		if (type == fp || type == sp)
			printf(fmtbuf, name, fp_int, fp_fraction);
		else
			printf(fmtbuf, name, buf);
	} else {
		printf("%s = ", name);
		print_bits(len, (uint8_t *)&buf);
		printf("\n");
	}

	return 0;
}