acpidump.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * (c) Alexey Starikovskiy, Intel, 2005-2006.
  3. * (c) Len Brown, Intel, 2007.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions, and the following disclaimer,
  11. * without modification.
  12. * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  13. * substantially similar to the "NO WARRANTY" disclaimer below
  14. * ("Disclaimer") and any redistribution must be conditioned upon
  15. * including a substantially similar Disclaimer requirement for further
  16. * binary redistribution.
  17. * 3. Neither the names of the above-listed copyright holders nor the names
  18. * of any contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * NO WARRANTY
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  29. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  34. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  35. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGES.
  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. typedef int s32;
  48. typedef unsigned char u8;
  49. typedef unsigned short u16;
  50. typedef unsigned int u32;
  51. typedef unsigned long long u64;
  52. typedef long long s64;
  53. #endif
  54. #include <sys/mman.h>
  55. #include <sys/types.h>
  56. #include <sys/stat.h>
  57. #include <fcntl.h>
  58. #include <stdio.h>
  59. #include <string.h>
  60. #include <unistd.h>
  61. #include <getopt.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. #define MAX_TABLES 128
  175. int next_table_dump;
  176. u64 dumped_tables[MAX_TABLES];
  177. void
  178. set_table_dumped(u64 address) {
  179. if (next_table_dump >= MAX_TABLES) {
  180. printf("increase MAX_TABLES\n");
  181. exit(1);
  182. }
  183. dumped_tables[next_table_dump++] = address;
  184. }
  185. /*
  186. * list the tables as they are dumped
  187. * check the list so that they are not dumped twice.
  188. *
  189. * this is needed because we follow both the XSDT and RSDT
  190. * which generally point to all duplicate tables
  191. * except the FADT
  192. */
  193. int
  194. check_table_dumped(u64 address) {
  195. int i;
  196. for (i = 0; i < MAX_TABLES; ++i) {
  197. if (address == dumped_tables[i])
  198. return 1;
  199. if (dumped_tables[i] == 0)
  200. return 0;
  201. }
  202. return 0;
  203. }
  204. static void acpi_show_table(int fd, struct acpi_table_header *table, unsigned long addr)
  205. {
  206. char buff[80];
  207. int len = snprintf(buff, 80, "%.4s @ %p\n", table->signature, (void *)addr);
  208. write(fd, buff, len);
  209. acpi_show_data(fd, (u8 *) table, table->length);
  210. buff[0] = '\n';
  211. write(fd, buff, 1);
  212. }
  213. static void write_table(int fd, struct acpi_table_header *tbl, unsigned long addr)
  214. {
  215. static int select_done = 0;
  216. if (check_table_dumped((u64)addr))
  217. return;
  218. if (!select_sig[0]) {
  219. if (print) {
  220. acpi_show_table(fd, tbl, addr);
  221. } else {
  222. write(fd, tbl, tbl->length);
  223. }
  224. } else if (!select_done && !memcmp(select_sig, tbl->signature, 4)) {
  225. if (skip > 0) {
  226. --skip;
  227. return;
  228. }
  229. if (print) {
  230. acpi_show_table(fd, tbl, addr);
  231. } else {
  232. write(fd, tbl, tbl->length);
  233. }
  234. select_done = 1;
  235. }
  236. set_table_dumped((u64) addr);
  237. }
  238. static void acpi_dump_FADT(int fd, struct acpi_table_header *tbl, unsigned long xaddr) {
  239. struct acpi_fadt_descriptor x;
  240. unsigned long addr;
  241. size_t len = sizeof(struct acpi_fadt_descriptor);
  242. if (len > tbl->length) len = tbl->length;
  243. memcpy(&x, tbl, len);
  244. x.header.length = len;
  245. if (checksum((u8 *)tbl, len)) {
  246. fprintf(stderr, "Wrong checksum for FADT!\n");
  247. }
  248. if (x.header.length >= 148 && x.Xdsdt) {
  249. addr = (unsigned long)x.Xdsdt;
  250. if (connect) {
  251. x.Xdsdt = lseek(fd, 0, SEEK_CUR);
  252. }
  253. } else if (x.header.length >= 44 && x.dsdt) {
  254. addr = (unsigned long)x.dsdt;
  255. if (connect) {
  256. x.dsdt = lseek(fd, 0, SEEK_CUR);
  257. }
  258. } else {
  259. fprintf(stderr, "No DSDT in FADT!\n");
  260. goto no_dsdt;
  261. }
  262. tbl = acpi_map_table(addr, DSDT_SIG);
  263. if (!tbl) goto no_dsdt;
  264. if (checksum((u8 *)tbl, tbl->length))
  265. fprintf(stderr, "Wrong checksum for DSDT!\n");
  266. write_table(fd, tbl, addr);
  267. acpi_unmap_table(tbl);
  268. no_dsdt:
  269. if (x.header.length >= 140 && x.xfirmware_ctrl) {
  270. addr = (unsigned long)x.xfirmware_ctrl;
  271. if (connect) {
  272. x.xfirmware_ctrl = lseek(fd, 0, SEEK_CUR);
  273. }
  274. } else if (x.header.length >= 40 && x.firmware_ctrl) {
  275. addr = (unsigned long)x.firmware_ctrl;
  276. if (connect) {
  277. x.firmware_ctrl = lseek(fd, 0, SEEK_CUR);
  278. }
  279. } else {
  280. fprintf(stderr, "No FACS in FADT!\n");
  281. goto no_facs;
  282. }
  283. tbl = acpi_map_table(addr, FACS_SIG);
  284. if (!tbl) goto no_facs;
  285. /* do not checksum FACS */
  286. write_table(fd, tbl, addr);
  287. acpi_unmap_table(tbl);
  288. no_facs:
  289. write_table(fd, (struct acpi_table_header *)&x, xaddr);
  290. }
  291. static int acpi_dump_RSDT(int fd, struct acpi_rsdp_descriptor *rsdp)
  292. {
  293. struct acpi_table_header *sdt, *tbl = 0;
  294. int i, num;
  295. char *offset;
  296. unsigned long addr;
  297. tbl = acpi_map_table(rsdp->rsdt_physical_address, "RSDT");
  298. if (!tbl) return 0;
  299. sdt = malloc(tbl->length);
  300. memcpy(sdt, tbl, tbl->length);
  301. acpi_unmap_table(tbl);
  302. if (checksum((u8 *)sdt, sdt->length))
  303. fprintf(stderr, "Wrong checksum for %s!\n", "RSDT");
  304. num = (sdt->length - sizeof(struct acpi_table_header))/sizeof(u32);
  305. offset = (char *)sdt + sizeof(struct acpi_table_header);
  306. for (i = 0; i < num; ++i, offset += sizeof(u32)) {
  307. addr = (unsigned long)(*(u32 *)offset);
  308. if (!addr) continue;
  309. tbl = acpi_map_table(addr, 0);
  310. if (!tbl) continue;
  311. if (!memcmp(tbl->signature, FADT_SIG, 4)) {
  312. acpi_dump_FADT(fd, tbl, addr);
  313. } else {
  314. if (checksum((u8 *)tbl, tbl->length))
  315. fprintf(stderr, "Wrong checksum for %.4s!\n", tbl->signature);
  316. write_table(fd, tbl, addr);
  317. }
  318. acpi_unmap_table(tbl);
  319. if (connect) {
  320. (*(u32*)offset) = lseek(fd, 0, SEEK_CUR);
  321. }
  322. }
  323. addr = (unsigned long)rsdp->rsdt_physical_address;
  324. if (connect) {
  325. rsdp->rsdt_physical_address = lseek(fd, 0, SEEK_CUR);
  326. }
  327. write_table(fd, sdt, addr);
  328. free (sdt);
  329. return 1;
  330. }
  331. static int acpi_dump_XSDT(int fd, struct acpi_rsdp_descriptor *rsdp)
  332. {
  333. struct acpi_table_header *sdt, *tbl = 0;
  334. int i, num;
  335. char *offset;
  336. unsigned long addr;
  337. if (rsdp->revision > 1 && rsdp->xsdt_physical_address) {
  338. tbl = acpi_map_table(rsdp->xsdt_physical_address, "XSDT");
  339. }
  340. if (!tbl) return 0;
  341. sdt = malloc(tbl->length);
  342. memcpy(sdt, tbl, tbl->length);
  343. acpi_unmap_table(tbl);
  344. if (checksum((u8 *)sdt, sdt->length))
  345. fprintf(stderr, "Wrong checksum for %s!\n", "XSDT");
  346. num = (sdt->length - sizeof(struct acpi_table_header))/sizeof(u64);
  347. offset = (char *)sdt + sizeof(struct acpi_table_header);
  348. for (i = 0; i < num; ++i, offset += sizeof(u64)) {
  349. addr = (unsigned long)(*(u64 *)offset);
  350. if (!addr) continue;
  351. tbl = acpi_map_table(addr, 0);
  352. if (!tbl) continue;
  353. if (!memcmp(tbl->signature, FADT_SIG, 4)) {
  354. acpi_dump_FADT(fd, tbl, addr);
  355. } else {
  356. if (checksum((u8 *)tbl, tbl->length))
  357. fprintf(stderr, "Wrong checksum for %.4s\n", tbl->signature);
  358. write_table(fd, tbl, addr);
  359. }
  360. acpi_unmap_table(tbl);
  361. if (connect) {
  362. (*(u64*)offset) = lseek(fd, 0, SEEK_CUR);
  363. }
  364. }
  365. addr = (unsigned long)rsdp->xsdt_physical_address;
  366. if (connect) {
  367. rsdp->xsdt_physical_address = lseek(fd, 0, SEEK_CUR);
  368. }
  369. write_table(fd, sdt, addr);
  370. free (sdt);
  371. return 1;
  372. }
  373. static void usage(const char *progname)
  374. {
  375. puts("Usage:");
  376. printf("%s [--addr 0x1234][--table DSDT][--output filename]"
  377. "[--binary][--length 0x456][--help]\n", progname);
  378. puts("\t--addr 0x1234 or -a 0x1234 -- look for tables at this physical address");
  379. puts("\t--table DSDT or -t DSDT -- only dump table with DSDT signature");
  380. puts("\t--output filename or -o filename -- redirect output from stdin to filename");
  381. puts("\t--binary or -b -- dump data in binary form rather than in hex-dump format");
  382. puts("\t--length 0x456 or -l 0x456 -- works only with --addr, dump physical memory"
  383. "\n\t\tregion without trying to understand it's contents");
  384. puts("\t--skip 2 or -s 2 -- skip 2 tables of the given name and output only 3rd one");
  385. puts("\t--help or -h -- this help message");
  386. exit(0);
  387. }
  388. static struct option long_options[] = {
  389. {"addr", 1, 0, 0},
  390. {"table", 1, 0, 0},
  391. {"output", 1, 0, 0},
  392. {"binary", 0, 0, 0},
  393. {"length", 1, 0, 0},
  394. {"skip", 1, 0, 0},
  395. {"help", 0, 0, 0},
  396. {0, 0, 0, 0}
  397. };
  398. int main(int argc, char **argv)
  399. {
  400. int option_index, c, fd;
  401. u8 *raw;
  402. struct acpi_rsdp_descriptor rsdpx, *x = 0;
  403. char *filename = 0;
  404. char buff[80];
  405. memset(select_sig, 0, 4);
  406. print = 1;
  407. connect = 0;
  408. addr = length = 0;
  409. skip = 0;
  410. while (1) {
  411. option_index = 0;
  412. c = getopt_long(argc, argv, "a:t:o:bl:s:h",
  413. long_options, &option_index);
  414. if (c == -1)
  415. break;
  416. switch (c) {
  417. case 0:
  418. switch (option_index) {
  419. case 0:
  420. addr = strtoul(optarg, (char **)NULL, 16);
  421. break;
  422. case 1:
  423. memcpy(select_sig, optarg, 4);
  424. break;
  425. case 2:
  426. filename = optarg;
  427. break;
  428. case 3:
  429. print = 0;
  430. break;
  431. case 4:
  432. length = strtoul(optarg, (char **)NULL, 16);
  433. break;
  434. case 5:
  435. skip = strtoul(optarg, (char **)NULL, 10);
  436. break;
  437. case 6:
  438. usage(argv[0]);
  439. exit(0);
  440. }
  441. break;
  442. case 'a':
  443. addr = strtoul(optarg, (char **)NULL, 16);
  444. break;
  445. case 't':
  446. memcpy(select_sig, optarg, 4);
  447. break;
  448. case 'o':
  449. filename = optarg;
  450. break;
  451. case 'b':
  452. print = 0;
  453. break;
  454. case 'l':
  455. length = strtoul(optarg, (char **)NULL, 16);
  456. break;
  457. case 's':
  458. skip = strtoul(optarg, (char **)NULL, 10);
  459. break;
  460. case 'h':
  461. usage(argv[0]);
  462. exit(0);
  463. default:
  464. printf("Unknown option!\n");
  465. usage(argv[0]);
  466. exit(0);
  467. }
  468. }
  469. fd = STDOUT_FILENO;
  470. if (filename) {
  471. fd = creat(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
  472. if (fd < 0)
  473. return fd;
  474. }
  475. if (!select_sig[0] && !print) {
  476. connect = 1;
  477. }
  478. psz = sysconf(_SC_PAGESIZE);
  479. if (length && addr) {
  480. /* We know length and address, it means we just want a memory dump */
  481. if (!(raw = acpi_map_memory(addr, length)))
  482. goto not_found;
  483. write(fd, raw, length);
  484. acpi_unmap_memory(raw, length);
  485. return 0;
  486. }
  487. length = sizeof(struct acpi_rsdp_descriptor);
  488. if (!addr) {
  489. addr = read_efi_systab();
  490. if (!addr) {
  491. addr = ACPI_HI_RSDP_WINDOW_BASE;
  492. length = ACPI_HI_RSDP_WINDOW_SIZE;
  493. }
  494. }
  495. if (!(raw = acpi_map_memory(addr, length)) ||
  496. !(x = acpi_scan_for_rsdp(raw, length)))
  497. goto not_found;
  498. /* Find RSDP and print all found tables */
  499. memcpy(&rsdpx, x, sizeof(struct acpi_rsdp_descriptor));
  500. acpi_unmap_memory(raw, length);
  501. if (connect) {
  502. lseek(fd, sizeof(struct acpi_rsdp_descriptor), SEEK_SET);
  503. }
  504. if (rsdpx.revision > 1 && rsdpx.xsdt_physical_address) {
  505. /* ACPIDUMP uses xsdt table */
  506. if (!acpi_dump_XSDT(fd, &rsdpx))
  507. goto not_found;
  508. }
  509. if (!acpi_dump_RSDT(fd, &rsdpx))
  510. goto not_found;
  511. if (connect) {
  512. lseek(fd, 0, SEEK_SET);
  513. write(fd, x, (rsdpx.revision < 2) ?
  514. ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
  515. } else if (!select_sig[0] || !memcmp("RSD PTR ", select_sig, 4)) {
  516. addr += (long)x - (long)raw;
  517. length = snprintf(buff, 80, "RSD PTR @ %p\n", (void *)addr);
  518. write(fd, buff, length);
  519. acpi_show_data(fd, (u8 *) & rsdpx, (rsdpx.revision < 2) ?
  520. ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH);
  521. buff[0] = '\n';
  522. write(fd, buff, 1);
  523. }
  524. return 0;
  525. not_found:
  526. fprintf(stderr, "ACPI tables were not found. If you know location "
  527. "of RSD PTR table (from dmesg, etc), "
  528. "supply it with either --addr or -a option\n");
  529. return 1;
  530. }