2 * (c) Alexey Starikovskiy, Intel, 2005-2006.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
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.
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
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;
57 #include <sys/types.h>
65 #include <sys/types.h>
68 #include <acpi/acconfig.h>
69 #include <acpi/platform/acenv.h>
70 #include <acpi/actypes.h>
71 #include <acpi/actbl.h>
73 static inline u8 checksum(u8 * buffer, u32 length)
75 u8 sum = 0, *i = buffer;
77 for (; i < buffer; sum += *(i++));
81 static unsigned long psz, addr, length;
82 static int print, connect, skip;
83 static u8 select_sig[4];
85 static unsigned long read_efi_systab( void )
89 FILE *f = fopen("/sys/firmware/efi/systab", "r");
91 while (fgets(buffer, 80, f)) {
92 if (sscanf(buffer, "ACPI20=0x%lx", &addr) == 1)
100 static u8 *acpi_map_memory(unsigned long where, unsigned length)
102 unsigned long offset;
104 int fd = open("/dev/mem", O_RDONLY);
106 fprintf(stderr, "acpi_os_map_memory: cannot open /dev/mem\n");
109 offset = where % psz;
110 there = mmap(NULL, length + offset, PROT_READ, MAP_PRIVATE,
113 if (there == MAP_FAILED) return 0;
114 return (there + offset);
117 static void acpi_unmap_memory(u8 * there, unsigned length)
119 unsigned long offset = (unsigned long)there % psz;
120 munmap(there - offset, length + offset);
123 static struct acpi_table_header *acpi_map_table(unsigned long where, char *sig)
126 struct acpi_table_header *tbl = (struct acpi_table_header *)
127 acpi_map_memory(where, sizeof(struct acpi_table_header));
128 if (!tbl || (sig && memcmp(sig, tbl->signature, 4))) return 0;
130 acpi_unmap_memory((u8 *) tbl, sizeof(struct acpi_table_header));
131 return (struct acpi_table_header *)acpi_map_memory(where, size);
134 static void acpi_unmap_table(struct acpi_table_header *tbl)
136 acpi_unmap_memory((u8 *)tbl, tbl->length);
139 static struct acpi_rsdp_descriptor *acpi_scan_for_rsdp(u8 *begin, u32 length)
141 struct acpi_rsdp_descriptor *rsdp;
142 u8 *i, *end = begin + length;
143 /* Search from given start address for the requested length */
144 for (i = begin; i < end; i += ACPI_RSDP_SCAN_STEP) {
145 /* The signature and checksum must both be correct */
146 if (memcmp((char *)i, "RSD PTR ", 8)) continue;
147 rsdp = (struct acpi_rsdp_descriptor *)i;
148 /* Signature matches, check the appropriate checksum */
149 if (!checksum((u8 *) rsdp, (rsdp->revision < 2) ?
150 ACPI_RSDP_CHECKSUM_LENGTH :
151 ACPI_RSDP_XCHECKSUM_LENGTH))
152 /* Checksum valid, we have found a valid RSDP */
155 /* Searched entire block, no RSDP was found */
162 static void acpi_show_data(int fd, u8 * data, int size)
166 int i, remain = size;
168 len = snprintf(buffer, 256, " %04x:", size - remain);
169 for (i = 0; i < 16 && i < remain; i++) {
171 snprintf(&buffer[len], 256 - len, " %02x", data[i]);
173 for (; i < 16; i++) {
174 len += snprintf(&buffer[len], 256 - len, " ");
176 len += snprintf(&buffer[len], 256 - len, " ");
177 for (i = 0; i < 16 && i < remain; i++) {
178 buffer[len++] = (isprint(data[i])) ? data[i] : '.';
180 buffer[len++] = '\n';
181 write(fd, buffer, len);
190 static void acpi_show_table(int fd, struct acpi_table_header *table, unsigned long addr)
193 int len = snprintf(buff, 80, "%.4s @ %p\n", table->signature, (void *)addr);
194 write(fd, buff, len);
195 acpi_show_data(fd, (u8 *) table, table->length);
200 static void write_table(int fd, struct acpi_table_header *tbl, unsigned long addr)
202 static int select_done = 0;
203 if (!select_sig[0]) {
205 acpi_show_table(fd, tbl, addr);
207 write(fd, tbl, tbl->length);
209 } else if (!select_done && !memcmp(select_sig, tbl->signature, 4)) {
215 acpi_show_table(fd, tbl, addr);
217 write(fd, tbl, tbl->length);
223 static void acpi_dump_FADT(int fd, struct acpi_table_header *tbl, unsigned long xaddr) {
224 struct acpi_fadt_descriptor x;
226 size_t len = sizeof(struct acpi_fadt_descriptor);
227 if (len > tbl->length) len = tbl->length;
228 memcpy(&x, tbl, len);
229 x.header.length = len;
230 if (checksum((u8 *)tbl, len)) {
231 fprintf(stderr, "Wrong checksum for FADT!\n");
233 if (x.header.length >= 148 && x.Xdsdt) {
234 addr = (unsigned long)x.Xdsdt;
236 x.Xdsdt = lseek(fd, 0, SEEK_CUR);
238 } else if (x.header.length >= 44 && x.dsdt) {
239 addr = (unsigned long)x.dsdt;
241 x.dsdt = lseek(fd, 0, SEEK_CUR);
244 fprintf(stderr, "No DSDT in FADT!\n");
247 tbl = acpi_map_table(addr, DSDT_SIG);
248 if (!tbl) goto no_dsdt;
249 if (checksum((u8 *)tbl, tbl->length))
250 fprintf(stderr, "Wrong checksum for DSDT!\n");
251 write_table(fd, tbl, addr);
252 acpi_unmap_table(tbl);
254 if (x.header.length >= 140 && x.xfirmware_ctrl) {
255 addr = (unsigned long)x.xfirmware_ctrl;
257 x.xfirmware_ctrl = lseek(fd, 0, SEEK_CUR);
259 } else if (x.header.length >= 40 && x.firmware_ctrl) {
260 addr = (unsigned long)x.firmware_ctrl;
262 x.firmware_ctrl = lseek(fd, 0, SEEK_CUR);
265 fprintf(stderr, "No FACS in FADT!\n");
268 tbl = acpi_map_table(addr, FACS_SIG);
269 if (!tbl) goto no_facs;
270 /* do not checksum FACS */
271 write_table(fd, tbl, addr);
272 acpi_unmap_table(tbl);
274 write_table(fd, (struct acpi_table_header *)&x, xaddr);
277 static int acpi_dump_SDT(int fd, struct acpi_rsdp_descriptor *rsdp)
279 struct acpi_table_header *sdt, *tbl = 0;
280 int xsdt = 1, i, num;
283 if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
284 tbl = acpi_map_table(rsdp->xsdt_physical_address, "XSDT");
286 if (!tbl && rsdp->rsdt_physical_address) {
288 tbl = acpi_map_table(rsdp->rsdt_physical_address, "RSDT");
291 sdt = malloc(tbl->length);
292 memcpy(sdt, tbl, tbl->length);
293 acpi_unmap_table(tbl);
294 if (checksum((u8 *)sdt, sdt->length))
295 fprintf(stderr, "Wrong checksum for %s!\n", (xsdt)?"XSDT":"RSDT");
296 num = (sdt->length - sizeof(struct acpi_table_header))/((xsdt)?sizeof(u64):sizeof(u32));
297 offset = (char *)sdt + sizeof(struct acpi_table_header);
298 for (i = 0; i < num; ++i, offset += ((xsdt) ? sizeof(u64) : sizeof(u32))) {
299 addr = (xsdt) ? (unsigned long)(*(u64 *)offset):
300 (unsigned long)(*(u32 *)offset);
302 tbl = acpi_map_table(addr, 0);
304 if (!memcmp(tbl->signature, FADT_SIG, 4)) {
305 acpi_dump_FADT(fd, tbl, addr);
307 if (checksum((u8 *)tbl, tbl->length))
308 fprintf(stderr, "Wrong checksum for generic table!\n");
309 write_table(fd, tbl, addr);
311 acpi_unmap_table(tbl);
314 (*(u64*)offset) = lseek(fd, 0, SEEK_CUR);
316 (*(u32*)offset) = lseek(fd, 0, SEEK_CUR);
320 addr = (unsigned long)rsdp->xsdt_physical_address;
322 rsdp->xsdt_physical_address = lseek(fd, 0, SEEK_CUR);
325 addr = (unsigned long)rsdp->rsdt_physical_address;
327 rsdp->rsdt_physical_address = lseek(fd, 0, SEEK_CUR);
330 write_table(fd, sdt, addr);
335 #define DYNAMIC_SSDT "/sys/firmware/acpi/tables/dynamic"
337 static void acpi_dump_dynamic_SSDT(int fd)
339 struct stat file_stat;
340 char filename[256], *ptr;
342 struct dirent *entry;
344 int count, readcount, length;
345 struct acpi_table_header table_header, *ptable;
347 if (stat(DYNAMIC_SSDT, &file_stat) == -1) {
348 /* The directory doesn't exist */
351 tabledir = opendir(DYNAMIC_SSDT);
353 /*can't open the directory */
357 while ((entry = readdir(tabledir)) != 0){
358 /* skip the file of . /.. */
359 if (entry->d_name[0] == '.')
362 sprintf(filename, "%s/%s", DYNAMIC_SSDT, entry->d_name);
363 fp = fopen(filename, "r");
365 fprintf(stderr, "Can't open the file of %s\n",
369 /* Read the Table header to parse the table length */
370 count = fread(&table_header, 1, sizeof(struct acpi_table_header), fp);
371 if (count < sizeof(table_header)) {
372 /* the length is lessn than ACPI table header. skip it */
376 length = table_header.length;
377 ptr = malloc(table_header.length);
378 fseek(fp, 0, SEEK_SET);
380 while(!feof(fp) && readcount < length) {
381 count = fread(ptr + readcount, 1, 256, fp);
385 ptable = (struct acpi_table_header *) ptr;
386 if (checksum((u8 *) ptable, ptable->length))
387 fprintf(stderr, "Wrong checksum "
388 "for dynamic SSDT table!\n");
389 write_table(fd, ptable, 0);
396 static void usage(const char *progname)
399 printf("%s [--addr 0x1234][--table DSDT][--output filename]"
400 "[--binary][--length 0x456][--help]\n", progname);
401 puts("\t--addr 0x1234 or -a 0x1234 -- look for tables at this physical address");
402 puts("\t--table DSDT or -t DSDT -- only dump table with DSDT signature");
403 puts("\t--output filename or -o filename -- redirect output from stdin to filename");
404 puts("\t--binary or -b -- dump data in binary form rather than in hex-dump format");
405 puts("\t--length 0x456 or -l 0x456 -- works only with --addr, dump physical memory"
406 "\n\t\tregion without trying to understand it's contents");
407 puts("\t--skip 2 or -s 2 -- skip 2 tables of the given name and output only 3rd one");
408 puts("\t--help or -h -- this help message");
412 static struct option long_options[] = {
422 int main(int argc, char **argv)
424 int option_index, c, fd;
426 struct acpi_rsdp_descriptor rsdpx, *x = 0;
429 memset(select_sig, 0, 4);
436 c = getopt_long(argc, argv, "a:t:o:bl:s:h",
437 long_options, &option_index);
443 switch (option_index) {
445 addr = strtoul(optarg, (char **)NULL, 16);
448 memcpy(select_sig, optarg, 4);
457 length = strtoul(optarg, (char **)NULL, 16);
460 skip = strtoul(optarg, (char **)NULL, 10);
468 addr = strtoul(optarg, (char **)NULL, 16);
471 memcpy(select_sig, optarg, 4);
480 length = strtoul(optarg, (char **)NULL, 16);
483 skip = strtoul(optarg, (char **)NULL, 10);
489 printf("Unknown option!\n");
497 fd = creat(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
502 if (!select_sig[0] && !print) {
506 psz = sysconf(_SC_PAGESIZE);
507 if (length && addr) {
508 /* We know length and address, it means we just want a memory dump */
509 if (!(raw = acpi_map_memory(addr, length)))
511 write(fd, raw, length);
512 acpi_unmap_memory(raw, length);
517 length = sizeof(struct acpi_rsdp_descriptor);
519 addr = read_efi_systab();
521 addr = ACPI_HI_RSDP_WINDOW_BASE;
522 length = ACPI_HI_RSDP_WINDOW_SIZE;
526 if (!(raw = acpi_map_memory(addr, length)) ||
527 !(x = acpi_scan_for_rsdp(raw, length)))
530 /* Find RSDP and print all found tables */
531 memcpy(&rsdpx, x, sizeof(struct acpi_rsdp_descriptor));
532 acpi_unmap_memory(raw, length);
534 lseek(fd, sizeof(struct acpi_rsdp_descriptor), SEEK_SET);
536 if (!acpi_dump_SDT(fd, &rsdpx))
539 lseek(fd, 0, SEEK_SET);
540 write(fd, x, (rsdpx.revision < 2) ?
541 ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
542 } else if (!select_sig[0] || !memcmp("RSD PTR ", select_sig, 4)) {
543 addr += (long)x - (long)raw;
544 length = snprintf(buff, 80, "RSD PTR @ %p\n", (void *)addr);
545 write(fd, buff, length);
546 acpi_show_data(fd, (u8 *) & rsdpx, (rsdpx.revision < 2) ?
547 ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
551 acpi_dump_dynamic_SSDT(fd);
556 fprintf(stderr, "ACPI tables were not found. If you know location "
557 "of RSD PTR table (from dmesg, etc), "
558 "supply it with either --addr or -a option\n");