linux/tools/power/acpi/acpidump.c
<<
>>
Prefs
   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
  48typedef int                             s32;
  49typedef unsigned char                   u8;
  50typedef unsigned short                  u16;
  51typedef unsigned int                    u32;
  52typedef unsigned long long              u64;
  53typedef 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#include <dirent.h>
  66
  67#include <acpi/acconfig.h>
  68#include <acpi/platform/acenv.h>
  69#include <acpi/actypes.h>
  70#include <acpi/actbl.h>
  71
  72static inline u8 checksum(u8 * buffer, u32 length)
  73{
  74        u8 sum = 0, *i = buffer;
  75        buffer += length;
  76        for (; i < buffer; sum += *(i++));
  77        return sum;
  78}
  79
  80static unsigned long psz, addr, length;
  81static int print, connect, skip;
  82static u8 select_sig[4];
  83
  84static unsigned long read_efi_systab( void )
  85{
  86        char buffer[80];
  87        unsigned long addr;
  88        FILE *f = fopen("/sys/firmware/efi/systab", "r");
  89        if (f) {
  90                while (fgets(buffer, 80, f)) {
  91                        if (sscanf(buffer, "ACPI20=0x%lx", &addr) == 1)
  92                                return addr;
  93                }
  94                fclose(f);
  95        }
  96        return 0;
  97}
  98
  99static u8 *acpi_map_memory(unsigned long where, unsigned length)
 100{
 101        unsigned long offset;
 102        u8 *there;
 103        int fd = open("/dev/mem", O_RDONLY);
 104        if (fd < 0) {
 105                fprintf(stderr, "acpi_os_map_memory: cannot open /dev/mem\n");
 106                exit(1);
 107        }
 108        offset = where % psz;
 109        there = mmap(NULL, length + offset, PROT_READ, MAP_PRIVATE,
 110                         fd, where - offset);
 111        close(fd);
 112        if (there == MAP_FAILED) return 0;
 113        return (there + offset);
 114}
 115
 116static void acpi_unmap_memory(u8 * there, unsigned length)
 117{
 118        unsigned long offset = (unsigned long)there % psz;
 119        munmap(there - offset, length + offset);
 120}
 121
 122static struct acpi_table_header *acpi_map_table(unsigned long where, char *sig)
 123{
 124        unsigned size;
 125        struct acpi_table_header *tbl = (struct acpi_table_header *)
 126            acpi_map_memory(where, sizeof(struct acpi_table_header));
 127        if (!tbl || (sig && memcmp(sig, tbl->signature, 4))) return 0;
 128        size = tbl->length;
 129        acpi_unmap_memory((u8 *) tbl, sizeof(struct acpi_table_header));
 130        return (struct acpi_table_header *)acpi_map_memory(where, size);
 131}
 132
 133static void acpi_unmap_table(struct acpi_table_header *tbl)
 134{
 135        acpi_unmap_memory((u8 *)tbl, tbl->length);
 136}
 137
 138static struct acpi_rsdp_descriptor *acpi_scan_for_rsdp(u8 *begin, u32 length)
 139{
 140        struct acpi_rsdp_descriptor *rsdp;
 141        u8 *i, *end = begin + length;
 142        /* Search from given start address for the requested length */
 143        for (i = begin; i < end; i += ACPI_RSDP_SCAN_STEP) {
 144                /* The signature and checksum must both be correct */
 145                if (memcmp((char *)i, "RSD PTR ", 8)) continue;
 146                rsdp = (struct acpi_rsdp_descriptor *)i;
 147                /* Signature matches, check the appropriate checksum */
 148                if (!checksum((u8 *) rsdp, (rsdp->revision < 2) ?
 149                              ACPI_RSDP_CHECKSUM_LENGTH :
 150                              ACPI_RSDP_XCHECKSUM_LENGTH))
 151                        /* Checksum valid, we have found a valid RSDP */
 152                        return rsdp;
 153        }
 154        /* Searched entire block, no RSDP was found */
 155        return 0;
 156}
 157
 158/*
 159 * Output data
 160 */
 161static void acpi_show_data(int fd, u8 * data, int size)
 162{
 163        char buffer[256];
 164        int len;
 165        int i, remain = size;
 166        while (remain > 0) {
 167                len = snprintf(buffer, 256, "  %04x:", size - remain);
 168                for (i = 0; i < 16 && i < remain; i++) {
 169                        len +=
 170                            snprintf(&buffer[len], 256 - len, " %02x", data[i]);
 171                }
 172                for (; i < 16; i++) {
 173                        len += snprintf(&buffer[len], 256 - len, "   ");
 174                }
 175                len += snprintf(&buffer[len], 256 - len, "  ");
 176                for (i = 0; i < 16 && i < remain; i++) {
 177                        buffer[len++] = (isprint(data[i])) ? data[i] : '.';
 178                }
 179                buffer[len++] = '\n';
 180                write(fd, buffer, len);
 181                data += 16;
 182                remain -= 16;
 183        }
 184}
 185
 186/*
 187 * Output ACPI table
 188 */
 189static void acpi_show_table(int fd, struct acpi_table_header *table, unsigned long addr)
 190{
 191        char buff[80];
 192        int len = snprintf(buff, 80, "%.4s @ %p\n", table->signature, (void *)addr);
 193        write(fd, buff, len);
 194        acpi_show_data(fd, (u8 *) table, table->length);
 195        buff[0] = '\n';
 196        write(fd, buff, 1);
 197}
 198
 199static void write_table(int fd, struct acpi_table_header *tbl, unsigned long addr)
 200{
 201        static int select_done = 0;
 202        if (!select_sig[0]) {
 203                if (print) {
 204                        acpi_show_table(fd, tbl, addr);
 205                } else {
 206                        write(fd, tbl, tbl->length);
 207                }
 208        } else if (!select_done && !memcmp(select_sig, tbl->signature, 4)) {
 209                if (skip > 0) {
 210                        --skip;
 211                        return;
 212                }
 213                if (print) {
 214                        acpi_show_table(fd, tbl, addr);
 215                } else {
 216                        write(fd, tbl, tbl->length);
 217                }
 218                select_done = 1;
 219        }
 220}
 221
 222static void acpi_dump_FADT(int fd, struct acpi_table_header *tbl, unsigned long xaddr) {
 223        struct acpi_fadt_descriptor x;
 224        unsigned long addr;
 225        size_t len = sizeof(struct acpi_fadt_descriptor);
 226        if (len > tbl->length) len = tbl->length;
 227        memcpy(&x, tbl, len);
 228        x.header.length = len;
 229        if (checksum((u8 *)tbl, len)) {
 230                fprintf(stderr, "Wrong checksum for FADT!\n");
 231        }
 232        if (x.header.length >= 148 && x.Xdsdt) {
 233                addr = (unsigned long)x.Xdsdt;
 234                if (connect) {
 235                        x.Xdsdt = lseek(fd, 0, SEEK_CUR);
 236                }
 237        } else if (x.header.length >= 44 && x.dsdt) {
 238                addr = (unsigned long)x.dsdt;
 239                if (connect) {
 240                        x.dsdt = lseek(fd, 0, SEEK_CUR);
 241                }
 242        } else {
 243                fprintf(stderr, "No DSDT in FADT!\n");
 244                goto no_dsdt;
 245        }
 246        tbl = acpi_map_table(addr, DSDT_SIG);
 247        if (!tbl) goto no_dsdt;
 248        if (checksum((u8 *)tbl, tbl->length))
 249                fprintf(stderr, "Wrong checksum for DSDT!\n");
 250        write_table(fd, tbl, addr);
 251        acpi_unmap_table(tbl);
 252no_dsdt:
 253        if (x.header.length >= 140 && x.xfirmware_ctrl) {
 254                addr = (unsigned long)x.xfirmware_ctrl;
 255                if (connect) {
 256                        x.xfirmware_ctrl = lseek(fd, 0, SEEK_CUR);
 257                }
 258        } else if (x.header.length >= 40 && x.firmware_ctrl) {
 259                addr = (unsigned long)x.firmware_ctrl;
 260                if (connect) {
 261                        x.firmware_ctrl = lseek(fd, 0, SEEK_CUR);
 262                }
 263        } else {
 264                fprintf(stderr, "No FACS in FADT!\n");
 265                goto no_facs;
 266        }
 267        tbl = acpi_map_table(addr, FACS_SIG);
 268        if (!tbl) goto no_facs;
 269        /* do not checksum FACS */
 270        write_table(fd, tbl, addr);
 271        acpi_unmap_table(tbl);
 272no_facs:
 273        write_table(fd, (struct acpi_table_header *)&x, xaddr);
 274}
 275
 276static int acpi_dump_SDT(int fd, struct acpi_rsdp_descriptor *rsdp)
 277{
 278        struct acpi_table_header *sdt, *tbl = 0;
 279        int xsdt = 1, i, num;
 280        char *offset;
 281        unsigned long addr;
 282        if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
 283                tbl = acpi_map_table(rsdp->xsdt_physical_address, "XSDT");
 284        }
 285        if (!tbl && rsdp->rsdt_physical_address) {
 286                xsdt = 0;
 287                tbl = acpi_map_table(rsdp->rsdt_physical_address, "RSDT");
 288        }
 289        if (!tbl) return 0;
 290        sdt = malloc(tbl->length);
 291        memcpy(sdt, tbl, tbl->length);
 292        acpi_unmap_table(tbl);
 293        if (checksum((u8 *)sdt, sdt->length))
 294                fprintf(stderr, "Wrong checksum for %s!\n", (xsdt)?"XSDT":"RSDT");
 295        num = (sdt->length - sizeof(struct acpi_table_header))/((xsdt)?sizeof(u64):sizeof(u32));
 296        offset = (char *)sdt + sizeof(struct acpi_table_header);
 297        for (i = 0; i < num; ++i, offset += ((xsdt) ? sizeof(u64) : sizeof(u32))) {
 298                addr = (xsdt) ? (unsigned long)(*(u64 *)offset):
 299                                (unsigned long)(*(u32 *)offset);
 300                if (!addr) continue;
 301                tbl = acpi_map_table(addr, 0);
 302                if (!tbl) continue;
 303                if (!memcmp(tbl->signature, FADT_SIG, 4)) {
 304                        acpi_dump_FADT(fd, tbl, addr);
 305                } else {
 306                        if (checksum((u8 *)tbl, tbl->length))
 307                                fprintf(stderr, "Wrong checksum for generic table!\n");
 308                        write_table(fd, tbl, addr);
 309                }
 310                acpi_unmap_table(tbl);
 311                if (connect) {
 312                        if (xsdt)
 313                                (*(u64*)offset) = lseek(fd, 0, SEEK_CUR);
 314                        else
 315                                (*(u32*)offset) = lseek(fd, 0, SEEK_CUR);
 316                }
 317        }
 318        if (xsdt) {
 319                addr = (unsigned long)rsdp->xsdt_physical_address;
 320                if (connect) {
 321                        rsdp->xsdt_physical_address = lseek(fd, 0, SEEK_CUR);
 322                }
 323        } else {
 324                addr = (unsigned long)rsdp->rsdt_physical_address;
 325                if (connect) {
 326                        rsdp->rsdt_physical_address = lseek(fd, 0, SEEK_CUR);
 327                }
 328        }
 329        write_table(fd, sdt, addr);
 330        free (sdt);
 331        return 1;
 332}
 333
 334#define DYNAMIC_SSDT    "/sys/firmware/acpi/tables/dynamic"
 335
 336static void acpi_dump_dynamic_SSDT(int fd)
 337{
 338        struct stat file_stat;
 339        char filename[256], *ptr;
 340        DIR *tabledir;
 341        struct dirent *entry;
 342        FILE *fp;
 343        int count, readcount, length;
 344        struct acpi_table_header table_header, *ptable;
 345
 346        if (stat(DYNAMIC_SSDT, &file_stat) == -1) {
 347                /* The directory doesn't exist */
 348                return;
 349        }
 350        tabledir = opendir(DYNAMIC_SSDT);
 351        if(!tabledir){
 352                /*can't open the directory */
 353                return;
 354         }
 355
 356        while ((entry = readdir(tabledir)) != 0){
 357                /* skip the file of . /.. */
 358                if (entry->d_name[0] == '.')
 359                        continue;
 360
 361                sprintf(filename, "%s/%s", DYNAMIC_SSDT, entry->d_name);
 362                fp = fopen(filename, "r");
 363                if (fp == NULL) {
 364                        fprintf(stderr, "Can't open the file of %s\n",
 365                                                filename);
 366                        continue;
 367                }
 368                /* Read the Table header to parse the table length */
 369                count = fread(&table_header, 1, sizeof(struct acpi_table_header), fp);
 370                if (count < sizeof(table_header)) {
 371                        /* the length is lessn than ACPI table header. skip it */
 372                        fclose(fp);
 373                        continue;
 374                }
 375                length = table_header.length;
 376                ptr = malloc(table_header.length);
 377                fseek(fp, 0, SEEK_SET);
 378                readcount = 0;
 379                while(!feof(fp) && readcount < length) {
 380                        count = fread(ptr + readcount, 1, 256, fp);
 381                        readcount += count;
 382                }
 383                fclose(fp);
 384                ptable = (struct acpi_table_header *) ptr;
 385                if (checksum((u8 *) ptable, ptable->length))
 386                        fprintf(stderr, "Wrong checksum "
 387                                        "for dynamic SSDT table!\n");
 388                write_table(fd, ptable, 0);
 389                free(ptr);
 390        }
 391        closedir(tabledir);
 392        return;
 393}
 394
 395static void usage(const char *progname)
 396{
 397        puts("Usage:");
 398        printf("%s [--addr 0x1234][--table DSDT][--output filename]"
 399                "[--binary][--length 0x456][--help]\n", progname);
 400        puts("\t--addr 0x1234 or -a 0x1234 -- look for tables at this physical address");
 401        puts("\t--table DSDT or -t DSDT -- only dump table with DSDT signature");
 402        puts("\t--output filename or -o filename -- redirect output from stdin to filename");
 403        puts("\t--binary or -b -- dump data in binary form rather than in hex-dump format");
 404        puts("\t--length 0x456 or -l 0x456 -- works only with --addr, dump physical memory"
 405                "\n\t\tregion without trying to understand it's contents");
 406        puts("\t--skip 2 or -s 2 -- skip 2 tables of the given name and output only 3rd one");
 407        puts("\t--help or -h -- this help message");
 408        exit(0);
 409}
 410
 411static struct option long_options[] = {
 412        {"addr", 1, 0, 0},
 413        {"table", 1, 0, 0},
 414        {"output", 1, 0, 0},
 415        {"binary", 0, 0, 0},
 416        {"length", 1, 0, 0},
 417        {"skip", 1, 0, 0},
 418        {"help", 0, 0, 0},
 419        {0, 0, 0, 0}
 420};
 421int main(int argc, char **argv)
 422{
 423        int option_index, c, fd;
 424        u8 *raw;
 425        struct acpi_rsdp_descriptor rsdpx, *x = 0;
 426        char *filename = 0;
 427        char buff[80];
 428        memset(select_sig, 0, 4);
 429        print = 1;
 430        connect = 0;
 431        addr = length = 0;
 432        skip = 0;
 433        while (1) {
 434                option_index = 0;
 435                c = getopt_long(argc, argv, "a:t:o:bl:s:h",
 436                                    long_options, &option_index);
 437                if (c == -1)
 438                        break;
 439
 440                switch (c) {
 441                case 0:
 442                        switch (option_index) {
 443                        case 0:
 444                                addr = strtoul(optarg, (char **)NULL, 16);
 445                                break;
 446                        case 1:
 447                                memcpy(select_sig, optarg, 4);
 448                                break;
 449                        case 2:
 450                                filename = optarg;
 451                                break;
 452                        case 3:
 453                                print = 0;
 454                                break;
 455                        case 4:
 456                                length = strtoul(optarg, (char **)NULL, 16);
 457                                break;
 458                        case 5:
 459                                skip = strtoul(optarg, (char **)NULL, 10);
 460                                break;
 461                        case 6:
 462                                usage(argv[0]);
 463                                exit(0);
 464                        }
 465                        break;
 466                case 'a':
 467                        addr = strtoul(optarg, (char **)NULL, 16);
 468                        break;
 469                case 't':
 470                        memcpy(select_sig, optarg, 4);
 471                        break;
 472                case 'o':
 473                        filename = optarg;
 474                        break;
 475                case 'b':
 476                        print = 0;
 477                        break;
 478                case 'l':
 479                        length = strtoul(optarg, (char **)NULL, 16);
 480                        break;
 481                case 's':
 482                        skip = strtoul(optarg, (char **)NULL, 10);
 483                        break;
 484                case 'h':
 485                        usage(argv[0]);
 486                        exit(0);
 487                default:
 488                        printf("Unknown option!\n");
 489                        usage(argv[0]);
 490                        exit(0);
 491                }
 492        }
 493
 494        fd = STDOUT_FILENO;
 495        if (filename) {
 496                fd = creat(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
 497                if (fd < 0)
 498                        return fd;
 499        }
 500
 501        if (!select_sig[0] && !print) {
 502                connect = 1;
 503        }
 504
 505        psz = sysconf(_SC_PAGESIZE);
 506        if (length && addr) {
 507                /* We know length and address, it means we just want a memory dump */
 508                if (!(raw = acpi_map_memory(addr, length)))
 509                        goto not_found;
 510                write(fd, raw, length);
 511                acpi_unmap_memory(raw, length);
 512                close(fd);
 513                return 0;
 514        }
 515
 516        length = sizeof(struct acpi_rsdp_descriptor);
 517        if (!addr) {
 518                addr = read_efi_systab();
 519                if (!addr) {
 520                        addr = ACPI_HI_RSDP_WINDOW_BASE;
 521                        length = ACPI_HI_RSDP_WINDOW_SIZE;
 522                }
 523        }
 524
 525        if (!(raw = acpi_map_memory(addr, length)) ||
 526            !(x = acpi_scan_for_rsdp(raw, length)))
 527                goto not_found;
 528
 529        /* Find RSDP and print all found tables */
 530        memcpy(&rsdpx, x, sizeof(struct acpi_rsdp_descriptor));
 531        acpi_unmap_memory(raw, length);
 532        if (connect) {
 533                lseek(fd, sizeof(struct acpi_rsdp_descriptor), SEEK_SET);
 534        }
 535        if (!acpi_dump_SDT(fd, &rsdpx))
 536                goto not_found;
 537        if (connect) {
 538                lseek(fd, 0, SEEK_SET);
 539                write(fd, x, (rsdpx.revision < 2) ?
 540                        ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
 541        } else if (!select_sig[0] || !memcmp("RSD PTR ", select_sig, 4)) {
 542                addr += (long)x - (long)raw;
 543                length = snprintf(buff, 80, "RSD PTR @ %p\n", (void *)addr);
 544                write(fd, buff, length);
 545                acpi_show_data(fd, (u8 *) & rsdpx, (rsdpx.revision < 2) ?
 546                                ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
 547                buff[0] = '\n';
 548                write(fd, buff, 1);
 549        }
 550        acpi_dump_dynamic_SSDT(fd);
 551        close(fd);
 552        return 0;
 553not_found:
 554        close(fd);
 555        fprintf(stderr, "ACPI tables were not found. If you know location "
 556                "of RSD PTR table (from dmesg, etc), "
 557                "supply it with either --addr or -a option\n");
 558        return 1;
 559}
 560