acpidump.c 15 KB

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