blob: 6f0b3a01095959739a37208fb4cc0f88efb3eb99 [file] [log] [blame]
Peter Stugedad1e302008-11-22 17:13:36 +00001/*
2 * This file is part of msrtool.
3 *
4 * Copyright (c) 2008 Peter Stuge <peter@stuge.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/stat.h>
26#include <fcntl.h>
27#include <errno.h>
28
29#include "msrtool.h"
30
31#define DEFAULT_CPU 0
32static uint8_t cpu = DEFAULT_CPU;
33
34uint8_t targets_found = 0;
35const struct targetdef **targets = NULL;
36const struct sysdef *sys = NULL;
37uint8_t reserved = 0, verbose = 0, quiet = 0;
38
39static struct targetdef alltargets[] = {
40 { "geodelx", "AMD Geode(tm) LX", geodelx_probe, geodelx_msrs },
41 { "cs5536", "AMD Geode(tm) CS5536", cs5536_probe, cs5536_msrs },
42 { TARGET_EOT }
43};
44
45static struct sysdef allsystems[] = {
46 { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
47 { SYSTEM_EOT }
48};
49
50static void syntax(char *argv[]) {
51 printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
52 printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
53 printf(" -h\t show this help text\n");
54 printf(" -v\t be verbose\n");
55 printf(" -q\t be quiet (overrides -v)\n");
56 printf(" -r\t include [Reserved] values\n");
57 printf(" -k\t list all known systems and targets\n");
58 printf(" -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
59 printf(" -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
60 printf(" -m\t force a system, e.g: -m linux\n");
61 printf(" -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
62 printf(" -i\t immediate mode\n");
63 printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
64 printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
65 printf(" -s\t stream mode\n");
66 printf("\t read one MSR address per line and append current hw value to the line\n");
67 printf("\t use the filename - for stdin/stdout\n");
68 printf("\t using -l -s ignores input and will output all MSRs with values\n");
69 printf(" -d\t diff mode\n");
70 printf("\t read one address and value per line and compare with current hw value,\n");
71 printf("\t printing differences to stdout. use the filename - to read from stdin\n");
72 printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
73 printf(" addr.. direct mode, read and decode values for the given MSR address(es)\n");
74}
75
76static void *add_target(const struct targetdef *t) {
77 void *tmp;
78 tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
79 if (NULL == tmp) {
80 perror("realloc");
81 return tmp;
82 }
83 targets = tmp;
84 targets[targets_found++] = t;
85 targets[targets_found] = NULL;
86 return targets;
87}
88
89int do_stream(const char *streamfn, uint8_t ignoreinput) {
90 char tmpfn[20], line[256];
91 uint8_t tn;
92 size_t start, len;
93 int ret = 1;
94 int fdout = -1;
95 FILE *fin = NULL, *fout = NULL;
96 uint32_t addr, linenum;
97 struct msr m = MSR1(0);
98
99 if (0 == strcmp(streamfn, "-")) {
100 fin = stdin;
101 fout = stdout;
102 } else {
103 if (!ignoreinput) {
104 if (NULL == (fin = fopen(streamfn, "r"))) {
105 perror("fopen()");
106 return 1;
107 }
108 if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
109 perror("snprintf");
110 return 1;
111 }
112 if (-1 == (fdout = mkstemp(tmpfn))) {
113 perror("mkstemp");
114 return 1;
115 }
116 if (NULL == (fout = fdopen(fdout, "w"))) {
117 perror("fdopen");
118 return 1;
119 }
120 } else {
121 if (NULL == (fout = fopen(streamfn, "w"))) {
122 perror("fopen");
123 return 1;
124 }
125 }
126 }
127
128 if (!sys->open(cpu, SYS_RDONLY))
129 goto done;
130 if (ignoreinput) {
131 for (tn = 0; tn < targets_found; tn++)
132 if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
133 ret = 1;
134 break;
135 }
136 } else {
137 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
138 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
139 if (1 == sscanf(line + start, "%8x", &addr)) {
140 if (!sys->rdmsr(cpu, addr, &m))
141 goto done;
142 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
143 continue;
144 }
145 while (1) {
146 fprintf(fout, "%s", line);
147 len = strlen(line);
148 if (NULL != strchr("\r\n", line[len - 1]))
149 break;
150 if (NULL == fgets(line, sizeof(line), fin))
151 goto read_done;
152 }
153 }
154read_done:
155 if (!feof(fin)) {
156 fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
157 goto done;
158 }
159 }
160 ret = 0;
161done:
162 sys->close(cpu);
163 if (strcmp(streamfn, "-")) {
164 if (ret)
165 unlink(tmpfn);
166 else if (!ignoreinput)
167 rename(tmpfn, streamfn);
168 }
169 if (!ignoreinput)
170 fclose(fin);
171 fclose(fout);
172 return ret;
173}
174
175int do_diff(const char *difffn) {
176 char tmpfn[20], line[512];
177 size_t start, len;
178 int ret = 1, tmp;
179 FILE *fin = NULL, *fout = stdout;
180 uint8_t rev = 0;
181 uint32_t addr, linenum;
182 struct msr mf = MSR1(0), mhw = MSR1(0);
183
184 if (':' == difffn[0]) {
185 rev = 1;
186 ++difffn;
187 }
188 if (0 == strcmp(difffn, "-"))
189 fin = stdin;
190 else if (NULL == (fin = fopen(difffn, "r"))) {
191 perror("fopen()");
192 return 1;
193 }
194
195 if (!sys->open(cpu, SYS_RDONLY))
196 goto done;
197 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
198 start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
199 if (sscanf(line + start, "%8x %n%*x", &addr, &tmp) >= 1) {
200 start += tmp;
201 for (len = strlen(line) - 1; NULL != strchr("\r\n", line[len]); --len)
202 line[len] = 0;
203 if (!str2msr(line + start, &mf)) {
204 fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, line + start);
205 continue;
206 }
207 if (!sys->rdmsr(cpu, addr, &mhw))
208 goto done;
209 if (diff_msr(fout, addr, rev ? mhw : mf, rev ? mf : mhw))
210 fprintf(fout, "\n");
211 }
212 }
213 if (!feof(fin))
214 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
215 else
216 ret = 0;
217done:
218 sys->close(cpu);
219 if (strcmp(difffn, "-")) {
220 if (ret)
221 unlink(tmpfn);
222 else
223 rename(tmpfn, difffn);
224 fclose(fin);
225 fclose(fout);
226 }
227 return ret;
228}
229
230int main(int argc, char *argv[]) {
231 char c;
232 int ret = 1;
233 const struct sysdef *s;
234 const struct targetdef *t;
235 uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
236 uint32_t addr = 0;
237 const char *streamfn = NULL, *difffn = NULL;
238 struct msr msrval = MSR2(-1, -1);
239 while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
240 switch (c) {
241 case 'h':
242 syntax(argv);
243 return 0;
244 case 'q':
245 quiet = 1;
246 break;
247 case 'v':
248 ++verbose;
249 break;
250 case 'r':
251 reserved = 1;
252 break;
253 case 'k':
254 listknown = 1;
255 break;
256 case 'l':
257 listmsrs = 1;
258 break;
259 case 'c':
260 cpu = atoi(optarg);
261 break;
262 case 'm':
263 for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
264 if (!strcmp(s->name, optarg)) {
265 sys = s;
266 break;
267 }
268 break;
269 case 't':
270 for (t = alltargets; !TARGET_ISEOT(*t); t++)
271 if (!strcmp(t->name, optarg)) {
272 add_target(t);
273 break;
274 }
275 break;
276 case 'i':
277 input = 1;
278 addr = strtoul(optarg, NULL, 16);
279 optarg = strchr(optarg, '=');
280 if (NULL == optarg) {
281 fprintf(stderr, "missing value in -i argument!\n");
282 break;
283 }
284 if (!str2msr(++optarg, &msrval))
285 fprintf(stderr, "invalid value in -i argument!\n");
286 break;
287 case 's':
288 streamfn = optarg;
289 break;
290 case 'd':
291 difffn = optarg;
292 break;
293 default:
294 break;
295 }
296
297 printf_quiet("msrtool %s\n", VERSION);
298
299 if (!sys && !input && !listknown)
300 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
301 printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
302 if (!sys->probe(sys))
303 continue;
304 printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
305 break;
306 }
307
308 if (targets)
309 for (tn = 0; tn < targets_found; tn++)
310 printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
311 else
312 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
313 printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
314 if (!t->probe(t))
315 continue;
316 printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
317 add_target(t);
318 }
319
320 printf_quiet("\n");
321 fflush(stdout);
322
323 if (listknown) {
324 printf("Known systems:\n");
325 for (s = allsystems; s->name; s++)
326 printf("%s: %s\n", s->name, s->prettyname);
327 printf("\nKnown targets:\n");
328 for (t = alltargets; t->name; t++) {
329 if (listmsrs && alltargets != t)
330 printf("\n");
331 printf("%s: %s\n", t->name, t->prettyname);
332 if (listmsrs)
333 dumpmsrdefs(t);
334 }
335 printf("\n");
336 return 0;
337 }
338
339 if (sys && !sys->name) {
340 fprintf(stderr, "Unable to detect the current operating system!\n");
341 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
342 fprintf(stderr, "\n");
343 }
344
345 if (!targets_found || !targets) {
346 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
347 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
348 fprintf(stderr, "\n");
349 return 1;
350 }
351
352 if (input) {
353 decodemsr(cpu, addr, msrval);
354 return 0;
355 }
356
357 if (sys && !sys->name)
358 return 1;
359
360 if (listmsrs) {
361 if (streamfn)
362 return do_stream(streamfn, 1);
363 for (tn = 0; tn < targets_found; tn++) {
364 if (tn)
365 printf("\n");
366 dumpmsrdefs(targets[tn]);
367 }
368 printf("\n");
369 return 0;
370 }
371
372 if (streamfn)
373 return do_stream(streamfn, 0);
374
375 if (!sys->open(cpu, SYS_RDONLY))
376 return 1;
377
378 if (difffn) {
379 ret = do_diff(difffn);
380 goto done;
381 }
382
383 if (optind == argc) {
384 syntax(argv);
385 printf("\nNo mode or address(es) specified!\n");
386 goto done;
387 }
388
389 for (; optind < argc; optind++) {
390 addr = strtoul(argv[optind], NULL, 16);
391 if (!sys->rdmsr(cpu, addr, &msrval))
392 break;
393 decodemsr(cpu, addr, msrval);
394 }
395 ret = 0;
396done:
397 sys->close(cpu);
398 return ret;
399}