msrtool: More trivial rearrangement
[coreboot.git] / util / msrtool / msrtool.c
1 /*
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 #include <pci/pci.h>
29
30 #include "msrtool.h"
31
32 #define DEFAULT_CPU 0
33 static uint8_t cpu = DEFAULT_CPU;
34
35 uint8_t targets_found = 0;
36 const struct targetdef **targets = NULL;
37 const struct sysdef *sys = NULL;
38 uint8_t reserved = 0, verbose = 0, quiet = 0;
39
40 struct pci_access *pacc = NULL;
41
42 static struct targetdef alltargets[] = {
43         { "geodegx2", "AMD Geode(tm) GX2", geodegx2_probe, geodegx2_msrs },
44         { "geodelx", "AMD Geode(tm) LX", geodelx_probe, geodelx_msrs },
45         { "cs5536", "AMD Geode(tm) CS5536", cs5536_probe, cs5536_msrs },
46         { "K8", "AMD K8 Family", k8_probe, k8_msrs },
47         { TARGET_EOT }
48 };
49
50 static struct sysdef allsystems[] = {
51         { "linux", "Linux with /dev/cpu/*/msr", linux_probe, linux_open, linux_close, linux_rdmsr },
52         { "darwin", "OS X with DirectIO", darwin_probe, darwin_open, darwin_close, darwin_rdmsr },
53         { "freebsd", "FreeBSD with /dev/cpuctl*", freebsd_probe, freebsd_open, freebsd_close, freebsd_rdmsr },
54         { SYSTEM_EOT }
55 };
56
57 static void syntax(char *argv[]) {
58         printf("syntax: %s [-hvqrkl] [-c cpu] [-m system] [-t target ...]\n", argv[0]);
59         printf("\t [-i addr=hi[:]lo] | [-s file] | [-d [:]file] | addr...\n");
60         printf("  -h\t show this help text\n");
61         printf("  -v\t be verbose\n");
62         printf("  -q\t be quiet (overrides -v)\n");
63         printf("  -r\t include [Reserved] values\n");
64         printf("  -k\t list all known systems and targets\n");
65         printf("  -l\t list MSRs and bit fields for current target(s) (-kl for ALL targets!)\n");
66         printf("  -c\t access MSRs on the specified CPU, default=%d\n", DEFAULT_CPU);
67         printf("  -m\t force a system, e.g: -m linux\n");
68         printf("  -t\t force a target, can be used multiple times, e.g: -t geodelx -t cs5536\n");
69         printf("  -i\t immediate mode\n");
70         printf("\t decode hex addr=hi:lo for the target without reading hw value\n");
71         printf("\t e.g: -i 4c00000f=f2f100ff56960004\n");
72         printf("  -s\t stream mode\n");
73         printf("\t read one MSR address per line and append current hw value to the line\n");
74         printf("\t use the filename - for stdin/stdout\n");
75         printf("\t using -l -s ignores input and will output all MSRs with values\n");
76         printf("  -d\t diff mode\n");
77         printf("\t read one address and value per line and compare with current hw value,\n");
78         printf("\t printing differences to stdout. use the filename - to read from stdin\n");
79         printf("\t use :file or :- to reverse diff, normally hw values are considered new\n");
80         printf("  addr.. direct mode, read and decode values for the given MSR address(es)\n");
81 }
82
83 static void *add_target(const struct targetdef *t) {
84         void *tmp;
85         tmp = realloc(targets, (targets_found + 2) * sizeof(struct targetdef *));
86         if (NULL == tmp) {
87                 perror("realloc");
88                 return tmp;
89         }
90         targets = tmp;
91         targets[targets_found++] = t;
92         targets[targets_found] = NULL;
93         return targets;
94 }
95
96 int do_stream(const char *streamfn, uint8_t ignoreinput) {
97         char tmpfn[20], line[256];
98         uint8_t tn;
99         size_t start, len;
100         int ret = 1;
101         int fdout = -1;
102         FILE *fin = NULL, *fout = NULL;
103         uint32_t addr, linenum;
104         struct msr m = MSR1(0);
105
106         if (0 == strcmp(streamfn, "-")) {
107                 fin = stdin;
108                 fout = stdout;
109         } else {
110                 if (!ignoreinput) {
111                         if (NULL == (fin = fopen(streamfn, "r"))) {
112                                 perror("fopen()");
113                                 return 1;
114                         }
115                         if (snprintf(tmpfn, sizeof(tmpfn), "msrtoolXXXXXX") >= sizeof(tmpfn)) {
116                                 perror("snprintf");
117                                 return 1;
118                         }
119                         if (-1 == (fdout = mkstemp(tmpfn))) {
120                                 perror("mkstemp");
121                                 return 1;
122                         }
123                         if (NULL == (fout = fdopen(fdout, "w"))) {
124                                 perror("fdopen");
125                                 return 1;
126                         }
127                 } else {
128                         if (NULL == (fout = fopen(streamfn, "w"))) {
129                                 perror("fopen");
130                                 return 1;
131                         }
132                 }
133         }
134
135         if (!sys->open(cpu, SYS_RDONLY))
136                 goto done;
137         if (ignoreinput) {
138                 for (tn = 0; tn < targets_found; tn++)
139                         if (dumpmsrdefsvals(fout, targets[tn], cpu)) {
140                                 ret = 1;
141                                 break;
142                         }
143         } else {
144                 for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
145                         start = (0 == strncmp("0x", line, 2)) ? 2 : 0;
146                         if (1 == sscanf(line + start, "%8x", &addr)) {
147                                 if (!sys->rdmsr(cpu, addr, &m))
148                                         goto done;
149                                 fprintf(fout, "0x%08x 0x%08x%08x\n", addr, m.hi, m.lo);
150                                 continue;
151                         }
152                         while (1) {
153                                 fprintf(fout, "%s", line);
154                                 len = strlen(line);
155                                 if (NULL != strchr("\r\n", line[len - 1]))
156                                         break;
157                                 if (NULL == fgets(line, sizeof(line), fin))
158                                         goto read_done;
159                         }
160                 }
161 read_done:
162                 if (!feof(fin)) {
163                         fprintf(stderr, "%s:%d: fgets: %s\n", streamfn, linenum, strerror(errno));
164                         goto done;
165                 }
166         }
167         ret = 0;
168 done:
169         sys->close(cpu);
170         if (strcmp(streamfn, "-")) {
171                 if (ret)
172                         unlink(tmpfn);
173                 else if (!ignoreinput)
174                         rename(tmpfn, streamfn);
175         }
176         if (!ignoreinput)
177                 fclose(fin);
178         fclose(fout);
179         return ret;
180 }
181
182 int do_diff(const char *difffn) {
183         char tmpfn[20], line[512], *m1start;
184         size_t len;
185         int ret = 1, tmp, m1pos;
186         FILE *fin = NULL, *fout = stdout;
187         uint8_t rev = 0;
188         uint32_t addr, linenum;
189         struct msr m1 = MSR1(0), m2 = MSR1(0);
190
191         if (':' == difffn[0]) {
192                 rev = 1;
193                 ++difffn;
194         }
195         if (0 == strcmp(difffn, "-"))
196                 fin = stdin;
197         else if (NULL == (fin = fopen(difffn, "r"))) {
198                 perror("fopen()");
199                 return 1;
200         }
201
202         if (!sys->open(cpu, SYS_RDONLY))
203                 goto done;
204         for (linenum = 1; NULL != fgets(line, sizeof(line), fin); ++linenum) {
205                 tmp = strncmp("0x", line, 2) ? 0 : 2;
206                 if (sscanf(line + tmp, "%8x %n%*x", &addr, &m1pos) < 1)
207                         continue;
208                 m1start = line + tmp + m1pos;
209                 for (len = strlen(m1start) - 1; NULL != strchr("\r\n", m1start[len]); --len)
210                         m1start[len] = 0;
211                 if (!str2msr(m1start, &m1)) {
212                         fprintf(stderr, "%s:%d: invalid MSR value '%s'\n", difffn, linenum, m1start);
213                         continue;
214                 }
215                 if (!sys->rdmsr(cpu, addr, &m2))
216                         goto done;
217                 if (diff_msr(fout, addr, rev ? m2 : m1, rev ? m1 : m2))
218                         fprintf(fout, "\n");
219         }
220         if (!feof(fin))
221                 fprintf(stderr, "%s:%d: fgets: %s\n", difffn, linenum, strerror(errno));
222         else
223                 ret = 0;
224 done:
225         sys->close(cpu);
226         if (strcmp(difffn, "-")) {
227                 if (ret)
228                         unlink(tmpfn);
229                 else
230                         rename(tmpfn, difffn);
231                 fclose(fin);
232                 fclose(fout);
233         }
234         return ret;
235 }
236
237 int main(int argc, char *argv[]) {
238         char c;
239         int ret = 1;
240         const struct sysdef *s;
241         const struct targetdef *t;
242         uint8_t tn, listmsrs = 0, listknown = 0, input = 0;
243         uint32_t addr = 0;
244         const char *streamfn = NULL, *difffn = NULL;
245         struct msr msrval = MSR2(-1, -1);
246         while ((c = getopt(argc, argv, "hqvrklc:m:t:a:i:s:d:")) != -1)
247                 switch (c) {
248                 case 'h':
249                         syntax(argv);
250                         return 0;
251                 case 'q':
252                         quiet = 1;
253                         break;
254                 case 'v':
255                         ++verbose;
256                         break;
257                 case 'r':
258                         reserved = 1;
259                         break;
260                 case 'k':
261                         listknown = 1;
262                         break;
263                 case 'l':
264                         listmsrs = 1;
265                         break;
266                 case 'c':
267                         cpu = atoi(optarg);
268                         break;
269                 case 'm':
270                         for (s = allsystems; !SYSTEM_ISEOT(*s); s++)
271                                 if (!strcmp(s->name, optarg)) {
272                                         sys = s;
273                                         break;
274                                 }
275                         break;
276                 case 't':
277                         for (t = alltargets; !TARGET_ISEOT(*t); t++)
278                                 if (!strcmp(t->name, optarg)) {
279                                         add_target(t);
280                                         break;
281                                 }
282                         break;
283                 case 'i':
284                         input = 1;
285                         addr = msraddrbyname(optarg);
286                         optarg = strchr(optarg, '=');
287                         if (NULL == optarg) {
288                                 fprintf(stderr, "missing value in -i argument!\n");
289                                 break;
290                         }
291                         if (!str2msr(++optarg, &msrval))
292                                 fprintf(stderr, "invalid value in -i argument!\n");
293                         break;
294                 case 's':
295                         streamfn = optarg;
296                         break;
297                 case 'd':
298                         difffn = optarg;
299                         break;
300                 default:
301                         break;
302                 }
303
304         printf_quiet("msrtool %s\n", VERSION);
305
306         pacc = pci_alloc();
307         if (NULL == pacc) {
308                 fprintf(stderr, "Could not initialize PCI library! pci_alloc() failed.\n");
309                 return 1;
310         }
311         pci_init(pacc);
312         pci_scan_bus(pacc);
313
314         if (!sys && !input && !listknown)
315                 for (sys = allsystems; !SYSTEM_ISEOT(*sys); sys++) {
316                         printf_verbose("Probing for system %s: %s\n", sys->name, sys->prettyname);
317                         if (!sys->probe(sys))
318                                 continue;
319                         printf_quiet("Detected system %s: %s\n", sys->name, sys->prettyname);
320                         break;
321                 }
322
323         if (targets)
324                 for (tn = 0; tn < targets_found; tn++)
325                         printf_quiet("Forced target %s: %s\n", targets[tn]->name, targets[tn]->prettyname);
326         else 
327                 for (t = alltargets; !TARGET_ISEOT(*t); t++) {
328                         printf_verbose("Probing for target %s: %s\n", t->name, t->prettyname);
329                         if (!t->probe(t))
330                                 continue;
331                         printf_quiet("Detected target %s: %s\n", t->name, t->prettyname);
332                         add_target(t);
333                 }
334
335         printf_quiet("\n");
336         fflush(stdout);
337
338         if (listknown) {
339                 printf("Known systems:\n");
340                 for (s = allsystems; s->name; s++)
341                         printf("%s: %s\n", s->name, s->prettyname);
342                 printf("\nKnown targets:\n");
343                 for (t = alltargets; t->name; t++) {
344                         if (listmsrs && alltargets != t)
345                                 printf("\n");
346                         printf("%s: %s\n", t->name, t->prettyname);
347                         if (listmsrs)
348                                 dumpmsrdefs(t);
349                 }
350                 printf("\n");
351                 return 0;
352         }
353
354         if (sys && !sys->name) {
355                 fprintf(stderr, "Unable to detect the current operating system!\n");
356                 fprintf(stderr, "On Linux, please do 'modprobe msr' and retry.\n");
357                 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
358                 fprintf(stderr, "\n");
359         }
360
361         if (!targets_found || !targets) {
362                 fprintf(stderr, "Unable to detect a known target; can not decode any MSRs! (Use -t to force)\n");
363                 fprintf(stderr, "Please send a report or patch to coreboot@coreboot.org. Thanks for your help!\n");
364                 fprintf(stderr, "\n");
365                 return 1;
366         }
367
368         if (input) {
369                 decodemsr(cpu, addr, msrval);
370                 return 0;
371         }
372
373         if (sys && !sys->name)
374                 return 1;
375
376         if (listmsrs) {
377                 if (streamfn)
378                         return do_stream(streamfn, 1);
379                 for (tn = 0; tn < targets_found; tn++) {
380                         if (tn)
381                                 printf("\n");
382                         dumpmsrdefs(targets[tn]);
383                 }
384                 printf("\n");
385                 return 0;
386         }
387
388         if (streamfn)
389                 return do_stream(streamfn, 0);
390
391         if (!sys->open(cpu, SYS_RDONLY))
392                 return 1;
393
394         if (difffn) {
395                 ret = do_diff(difffn);
396                 goto done;
397         }
398
399         if (optind == argc) {
400                 syntax(argv);
401                 printf("\nNo mode or address(es) specified!\n");
402                 goto done;
403         }
404
405         for (; optind < argc; optind++) {
406                 addr = msraddrbyname(argv[optind]);
407                 if (!sys->rdmsr(cpu, addr, &msrval))
408                         break;
409                 decodemsr(cpu, addr, msrval);
410         }
411         ret = 0;
412 done:
413         sys->close(cpu);
414         return ret;
415 }