tools/power/acpi/acpidump: version 20060606
[firefly-linux-kernel-4.4.55.git] / tools / power / acpi / acpidump.c
1 /*
2  * (c) Alexey Starikovskiy, Intel, 2005-2006.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions, and the following disclaimer,
10  *    without modification.
11  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12  *    substantially similar to the "NO WARRANTY" disclaimer below
13  *    ("Disclaimer") and any redistribution must be conditioned upon
14  *    including a substantially similar Disclaimer requirement for further
15  *    binary redistribution.
16  * 3. Neither the names of the above-listed copyright holders nor the names
17  *    of any contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * Alternatively, this software may be distributed under the terms of the
21  * GNU General Public License ("GPL") version 2 as published by the Free
22  * Software Foundation.
23  *
24  * NO WARRANTY
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
34  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGES.
36  */
37
38 #ifdef DEFINE_ALTERNATE_TYPES
39 /* hack to enable building old application with new headers -lenb */
40 #define acpi_fadt_descriptor acpi_table_fadt
41 #define acpi_rsdp_descriptor acpi_table_rsdp
42 #define DSDT_SIG ACPI_SIG_DSDT
43 #define FACS_SIG ACPI_SIG_FACS
44 #define FADT_SIG ACPI_SIG_FADT
45 #define xfirmware_ctrl Xfacs
46 #define firmware_ctrl facs
47
48 typedef int                             s32;
49 typedef unsigned char                   u8;
50 typedef unsigned short                  u16;
51 typedef unsigned int                    u32;
52 typedef unsigned long long              u64;
53 typedef long long                       s64;
54 #endif
55
56 #include <sys/mman.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <unistd.h>
63 #include <getopt.h>
64
65
66 #include <acpi/acconfig.h>
67 #include <acpi/platform/acenv.h>
68 #include <acpi/actypes.h>
69 #include <acpi/actbl.h>
70
71 static inline u8 checksum(u8 * buffer, u32 length)
72 {
73         u8 sum = 0, *i = buffer;
74         buffer += length;
75         for (; i < buffer; sum += *(i++));
76         return sum;
77 }
78
79 static unsigned long psz, addr, length;
80 static int print, connect, skip;
81 static u8 select_sig[4];
82
83 static unsigned long read_efi_systab( void )
84 {
85         char buffer[80];
86         unsigned long addr;
87         FILE *f = fopen("/sys/firmware/efi/systab", "r");
88         if (f) {
89                 while (fgets(buffer, 80, f)) {
90                         if (sscanf(buffer, "ACPI20=0x%lx", &addr) == 1)
91                                 return addr;
92                 }
93                 fclose(f);
94         }
95         return 0;
96 }
97
98 static u8 *acpi_map_memory(unsigned long where, unsigned length)
99 {
100         unsigned long offset;
101         u8 *there;
102         int fd = open("/dev/mem", O_RDONLY);
103         if (fd < 0) {
104                 fprintf(stderr, "acpi_os_map_memory: cannot open /dev/mem\n");
105                 exit(1);
106         }
107         offset = where % psz;
108         there = mmap(NULL, length + offset, PROT_READ, MAP_PRIVATE,
109                          fd, where - offset);
110         close(fd);
111         if (there == MAP_FAILED) return 0;
112         return (there + offset);
113 }
114
115 static void acpi_unmap_memory(u8 * there, unsigned length)
116 {
117         unsigned long offset = (unsigned long)there % psz;
118         munmap(there - offset, length + offset);
119 }
120
121 static struct acpi_table_header *acpi_map_table(unsigned long where, char *sig)
122 {
123         unsigned size;
124         struct acpi_table_header *tbl = (struct acpi_table_header *)
125             acpi_map_memory(where, sizeof(struct acpi_table_header));
126         if (!tbl || (sig && memcmp(sig, tbl->signature, 4))) return 0;
127         size = tbl->length;
128         acpi_unmap_memory((u8 *) tbl, sizeof(struct acpi_table_header));
129         return (struct acpi_table_header *)acpi_map_memory(where, size);
130 }
131
132 static void acpi_unmap_table(struct acpi_table_header *tbl)
133 {
134         acpi_unmap_memory((u8 *)tbl, tbl->length);
135 }
136
137 static struct acpi_rsdp_descriptor *acpi_scan_for_rsdp(u8 *begin, u32 length)
138 {
139         struct acpi_rsdp_descriptor *rsdp;
140         u8 *i, *end = begin + length;
141         /* Search from given start address for the requested length */
142         for (i = begin; i < end; i += ACPI_RSDP_SCAN_STEP) {
143                 /* The signature and checksum must both be correct */
144                 if (memcmp((char *)i, "RSD PTR ", 8)) continue;
145                 rsdp = (struct acpi_rsdp_descriptor *)i;
146                 /* Signature matches, check the appropriate checksum */
147                 if (!checksum((u8 *) rsdp, (rsdp->revision < 2) ?
148                               ACPI_RSDP_CHECKSUM_LENGTH :
149                               ACPI_RSDP_XCHECKSUM_LENGTH))
150                         /* Checksum valid, we have found a valid RSDP */
151                         return rsdp;
152         }
153         /* Searched entire block, no RSDP was found */
154         return 0;
155 }
156
157 /*
158  * Output data
159  */
160 static void acpi_show_data(int fd, u8 * data, int size)
161 {
162         char buffer[256];
163         int len;
164         int i, remain = size;
165         while (remain > 0) {
166                 len = snprintf(buffer, 256, "  %04x:", size - remain);
167                 for (i = 0; i < 16 && i < remain; i++) {
168                         len +=
169                             snprintf(&buffer[len], 256 - len, " %02x", data[i]);
170                 }
171                 for (; i < 16; i++) {
172                         len += snprintf(&buffer[len], 256 - len, "   ");
173                 }
174                 len += snprintf(&buffer[len], 256 - len, "  ");
175                 for (i = 0; i < 16 && i < remain; i++) {
176                         buffer[len++] = (isprint(data[i])) ? data[i] : '.';
177                 }
178                 buffer[len++] = '\n';
179                 write(fd, buffer, len);
180                 data += 16;
181                 remain -= 16;
182         }
183 }
184
185 /*
186  * Output ACPI table
187  */
188 static void acpi_show_table(int fd, struct acpi_table_header *table, unsigned long addr)
189 {
190         char buff[80];
191         int len = snprintf(buff, 80, "%.4s @ %p\n", table->signature, (void *)addr);
192         write(fd, buff, len);
193         acpi_show_data(fd, (u8 *) table, table->length);
194         buff[0] = '\n';
195         write(fd, buff, 1);
196 }
197
198 static void write_table(int fd, struct acpi_table_header *tbl, unsigned long addr)
199 {
200         static int select_done = 0;
201         if (!select_sig[0]) {
202                 if (print) {
203                         acpi_show_table(fd, tbl, addr);
204                 } else {
205                         write(fd, tbl, tbl->length);
206                 }
207         } else if (!select_done && !memcmp(select_sig, tbl->signature, 4)) {
208                 if (skip > 0) {
209                         --skip;
210                         return;
211                 }
212                 if (print) {
213                         acpi_show_table(fd, tbl, addr);
214                 } else {
215                         write(fd, tbl, tbl->length);
216                 }
217                 select_done = 1;
218         }
219 }
220
221 static void acpi_dump_FADT(int fd, struct acpi_table_header *tbl, unsigned long xaddr) {
222         struct acpi_fadt_descriptor x;
223         unsigned long addr;
224         size_t len = sizeof(struct acpi_fadt_descriptor);
225         if (len > tbl->length) len = tbl->length;
226         memcpy(&x, tbl, len);
227         x.header.length = len;
228         if (checksum((u8 *)tbl, len)) {
229                 fprintf(stderr, "Wrong checksum for FADT!\n");
230         }
231         if (x.header.length >= 148 && x.Xdsdt) {
232                 addr = (unsigned long)x.Xdsdt;
233                 if (connect) {
234                         x.Xdsdt = lseek(fd, 0, SEEK_CUR);
235                 }
236         } else if (x.header.length >= 44 && x.dsdt) {
237                 addr = (unsigned long)x.dsdt;
238                 if (connect) {
239                         x.dsdt = lseek(fd, 0, SEEK_CUR);
240                 }
241         } else {
242                 fprintf(stderr, "No DSDT in FADT!\n");
243                 goto no_dsdt;
244         }
245         tbl = acpi_map_table(addr, DSDT_SIG);
246         if (!tbl) goto no_dsdt;
247         if (checksum((u8 *)tbl, tbl->length))
248                 fprintf(stderr, "Wrong checksum for DSDT!\n");
249         write_table(fd, tbl, addr);
250         acpi_unmap_table(tbl);
251 no_dsdt:
252         if (x.header.length >= 140 && x.xfirmware_ctrl) {
253                 addr = (unsigned long)x.xfirmware_ctrl;
254                 if (connect) {
255                         x.xfirmware_ctrl = lseek(fd, 0, SEEK_CUR);
256                 }
257         } else if (x.header.length >= 40 && x.firmware_ctrl) {
258                 addr = (unsigned long)x.firmware_ctrl;
259                 if (connect) {
260                         x.firmware_ctrl = lseek(fd, 0, SEEK_CUR);
261                 }
262         } else {
263                 fprintf(stderr, "No FACS in FADT!\n");
264                 goto no_facs;
265         }
266         tbl = acpi_map_table(addr, FACS_SIG);
267         if (!tbl) goto no_facs;
268         /* do not checksum FACS */
269         write_table(fd, tbl, addr);
270         acpi_unmap_table(tbl);
271 no_facs:
272         write_table(fd, (struct acpi_table_header *)&x, xaddr);
273 }
274
275 static int acpi_dump_SDT(int fd, struct acpi_rsdp_descriptor *rsdp)
276 {
277         struct acpi_table_header *sdt, *tbl = 0;
278         int xsdt = 1, i, num;
279         char *offset;
280         unsigned long addr;
281         if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
282                 tbl = acpi_map_table(rsdp->xsdt_physical_address, "XSDT");
283         }
284         if (!tbl && rsdp->rsdt_physical_address) {
285                 xsdt = 0;
286                 tbl = acpi_map_table(rsdp->rsdt_physical_address, "RSDT");
287         }
288         if (!tbl) return 0;
289         sdt = malloc(tbl->length);
290         memcpy(sdt, tbl, tbl->length);
291         acpi_unmap_table(tbl);
292         if (checksum((u8 *)sdt, sdt->length))
293                 fprintf(stderr, "Wrong checksum for %s!\n", (xsdt)?"XSDT":"RSDT");
294         num = (sdt->length - sizeof(struct acpi_table_header))/((xsdt)?sizeof(u64):sizeof(u32));
295         offset = (char *)sdt + sizeof(struct acpi_table_header);
296         for (i = 0; i < num; ++i, offset += ((xsdt) ? sizeof(u64) : sizeof(u32))) {
297                 addr = (xsdt) ? (unsigned long)(*(u64 *)offset):
298                                 (unsigned long)(*(u32 *)offset);
299                 if (!addr) continue;
300                 tbl = acpi_map_table(addr, 0);
301                 if (!tbl) continue;
302                 if (!memcmp(tbl->signature, FADT_SIG, 4)) {
303                         acpi_dump_FADT(fd, tbl, addr);
304                 } else {
305                         if (checksum((u8 *)tbl, tbl->length))
306                                 fprintf(stderr, "Wrong checksum for generic table!\n");
307                         write_table(fd, tbl, addr);
308                 }
309                 acpi_unmap_table(tbl);
310                 if (connect) {
311                         if (xsdt)
312                                 (*(u64*)offset) = lseek(fd, 0, SEEK_CUR);
313                         else
314                                 (*(u32*)offset) = lseek(fd, 0, SEEK_CUR);
315                 }
316         }
317         if (xsdt) {
318                 addr = (unsigned long)rsdp->xsdt_physical_address;
319                 if (connect) {
320                         rsdp->xsdt_physical_address = lseek(fd, 0, SEEK_CUR);
321                 }
322         } else {
323                 addr = (unsigned long)rsdp->rsdt_physical_address;
324                 if (connect) {
325                         rsdp->rsdt_physical_address = lseek(fd, 0, SEEK_CUR);
326                 }
327         }
328         write_table(fd, sdt, addr);
329         free (sdt);
330         return 1;
331 }
332
333 static void usage(const char *progname)
334 {
335         puts("Usage:");
336         printf("%s [--addr 0x1234][--table DSDT][--output filename]"
337                 "[--binary][--length 0x456][--help]\n", progname);
338         puts("\t--addr 0x1234 or -a 0x1234 -- look for tables at this physical address");
339         puts("\t--table DSDT or -t DSDT -- only dump table with DSDT signature");
340         puts("\t--output filename or -o filename -- redirect output from stdin to filename");
341         puts("\t--binary or -b -- dump data in binary form rather than in hex-dump format");
342         puts("\t--length 0x456 or -l 0x456 -- works only with --addr, dump physical memory"
343                 "\n\t\tregion without trying to understand it's contents");
344         puts("\t--skip 2 or -s 2 -- skip 2 tables of the given name and output only 3rd one");
345         puts("\t--help or -h -- this help message");
346         exit(0);
347 }
348
349 static struct option long_options[] = {
350         {"addr", 1, 0, 0},
351         {"table", 1, 0, 0},
352         {"output", 1, 0, 0},
353         {"binary", 0, 0, 0},
354         {"length", 1, 0, 0},
355         {"skip", 1, 0, 0},
356         {"help", 0, 0, 0},
357         {0, 0, 0, 0}
358 };
359 int main(int argc, char **argv)
360 {
361         int option_index, c, fd;
362         u8 *raw;
363         struct acpi_rsdp_descriptor rsdpx, *x = 0;
364         char *filename = 0;
365         char buff[80];
366         memset(select_sig, 0, 4);
367         print = 1;
368         connect = 0;
369         addr = length = 0;
370         skip = 0;
371         while (1) {
372                 option_index = 0;
373                 c = getopt_long(argc, argv, "a:t:o:bl:s:h",
374                                     long_options, &option_index);
375                 if (c == -1)
376                         break;
377
378                 switch (c) {
379                 case 0:
380                         switch (option_index) {
381                         case 0:
382                                 addr = strtoul(optarg, (char **)NULL, 16);
383                                 break;
384                         case 1:
385                                 memcpy(select_sig, optarg, 4);
386                                 break;
387                         case 2:
388                                 filename = optarg;
389                                 break;
390                         case 3:
391                                 print = 0;
392                                 break;
393                         case 4:
394                                 length = strtoul(optarg, (char **)NULL, 16);
395                                 break;
396                         case 5:
397                                 skip = strtoul(optarg, (char **)NULL, 10);
398                                 break;
399                         case 6:
400                                 usage(argv[0]);
401                                 exit(0);
402                         }
403                         break;
404                 case 'a':
405                         addr = strtoul(optarg, (char **)NULL, 16);
406                         break;
407                 case 't':
408                         memcpy(select_sig, optarg, 4);
409                         break;
410                 case 'o':
411                         filename = optarg;
412                         break;
413                 case 'b':
414                         print = 0;
415                         break;
416                 case 'l':
417                         length = strtoul(optarg, (char **)NULL, 16);
418                         break;
419                 case 's':
420                         skip = strtoul(optarg, (char **)NULL, 10);
421                         break;
422                 case 'h':
423                         usage(argv[0]);
424                         exit(0);
425                 default:
426                         printf("Unknown option!\n");
427                         usage(argv[0]);
428                         exit(0);
429                 }
430         }
431
432         fd = STDOUT_FILENO;
433         if (filename) {
434                 fd = creat(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
435                 if (fd < 0)
436                         return fd;
437         }
438
439         if (!select_sig[0] && !print) {
440                 connect = 1;
441         }
442
443         psz = sysconf(_SC_PAGESIZE);
444         if (length && addr) {
445                 /* We know length and address, it means we just want a memory dump */
446                 if (!(raw = acpi_map_memory(addr, length)))
447                         goto not_found;
448                 write(fd, raw, length);
449                 acpi_unmap_memory(raw, length);
450                 return 0;
451         }
452
453         length = sizeof(struct acpi_rsdp_descriptor);
454         if (!addr) {
455                 addr = read_efi_systab();
456                 if (!addr) {
457                         addr = ACPI_HI_RSDP_WINDOW_BASE;
458                         length = ACPI_HI_RSDP_WINDOW_SIZE;
459                 }
460         }
461
462         if (!(raw = acpi_map_memory(addr, length)) ||
463             !(x = acpi_scan_for_rsdp(raw, length)))
464                 goto not_found;
465
466         /* Find RSDP and print all found tables */
467         memcpy(&rsdpx, x, sizeof(struct acpi_rsdp_descriptor));
468         acpi_unmap_memory(raw, length);
469         if (connect) {
470                 lseek(fd, sizeof(struct acpi_rsdp_descriptor), SEEK_SET);
471         }
472         if (!acpi_dump_SDT(fd, &rsdpx))
473                 goto not_found;
474         if (connect) {
475                 lseek(fd, 0, SEEK_SET);
476                 write(fd, x, (rsdpx.revision < 2) ?
477                         ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
478         } else if (!select_sig[0] || !memcmp("RSD PTR ", select_sig, 4)) {
479                 addr += (long)x - (long)raw;
480                 length = snprintf(buff, 80, "RSD PTR @ %p\n", (void *)addr);
481                 write(fd, buff, length);
482                 acpi_show_data(fd, (u8 *) & rsdpx, (rsdpx.revision < 2) ?
483                                 ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
484                 buff[0] = '\n';
485                 write(fd, buff, 1);
486         }
487         return 0;
488 not_found:
489         fprintf(stderr, "ACPI tables were not found. If you know location "
490                 "of RSD PTR table (from dmesg, etc), "
491                 "supply it with either --addr or -a option\n");
492         return 1;
493 }