Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 1 | /* |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 2 | * (c) Alexey Starikovskiy, Intel, 2005-2006. |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 3 | * (c) Len Brown, Intel, 2007. |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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 | |
| 39 | #ifdef DEFINE_ALTERNATE_TYPES |
| 40 | /* hack to enable building old application with new headers -lenb */ |
| 41 | #define acpi_fadt_descriptor acpi_table_fadt |
| 42 | #define acpi_rsdp_descriptor acpi_table_rsdp |
| 43 | #define DSDT_SIG ACPI_SIG_DSDT |
| 44 | #define FACS_SIG ACPI_SIG_FACS |
| 45 | #define FADT_SIG ACPI_SIG_FADT |
| 46 | #define xfirmware_ctrl Xfacs |
| 47 | #define firmware_ctrl facs |
| 48 | |
| 49 | typedef int s32; |
| 50 | typedef unsigned char u8; |
| 51 | typedef unsigned short u16; |
| 52 | typedef unsigned int u32; |
| 53 | typedef unsigned long long u64; |
| 54 | typedef long long s64; |
| 55 | #endif |
| 56 | |
| 57 | #include <sys/mman.h> |
| 58 | #include <sys/types.h> |
| 59 | #include <sys/stat.h> |
| 60 | #include <fcntl.h> |
| 61 | #include <stdio.h> |
| 62 | #include <string.h> |
| 63 | #include <unistd.h> |
| 64 | #include <getopt.h> |
| 65 | |
| 66 | |
| 67 | #include <acpi/acconfig.h> |
| 68 | #include <acpi/platform/acenv.h> |
| 69 | #include <acpi/actypes.h> |
| 70 | #include <acpi/actbl.h> |
| 71 | |
| 72 | static 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 | |
| 80 | static unsigned long psz, addr, length; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 81 | static int print, connect, skip; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 82 | static u8 select_sig[4]; |
| 83 | |
| 84 | static unsigned long read_efi_systab( void ) |
| 85 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 86 | char buffer[80]; |
| 87 | unsigned long addr; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 88 | FILE *f = fopen("/sys/firmware/efi/systab", "r"); |
| 89 | if (f) { |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 90 | while (fgets(buffer, 80, f)) { |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 91 | if (sscanf(buffer, "ACPI20=0x%lx", &addr) == 1) |
| 92 | return addr; |
| 93 | } |
| 94 | fclose(f); |
| 95 | } |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | static u8 *acpi_map_memory(unsigned long where, unsigned length) |
| 100 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 101 | unsigned long offset; |
| 102 | u8 *there; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 108 | offset = where % psz; |
| 109 | there = mmap(NULL, length + offset, PROT_READ, MAP_PRIVATE, |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 110 | fd, where - offset); |
| 111 | close(fd); |
| 112 | if (there == MAP_FAILED) return 0; |
| 113 | return (there + offset); |
| 114 | } |
| 115 | |
| 116 | static 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 | |
| 122 | static struct acpi_table_header *acpi_map_table(unsigned long where, char *sig) |
| 123 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 124 | unsigned size; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 128 | size = tbl->length; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 129 | acpi_unmap_memory((u8 *) tbl, sizeof(struct acpi_table_header)); |
| 130 | return (struct acpi_table_header *)acpi_map_memory(where, size); |
| 131 | } |
| 132 | |
| 133 | static void acpi_unmap_table(struct acpi_table_header *tbl) |
| 134 | { |
| 135 | acpi_unmap_memory((u8 *)tbl, tbl->length); |
| 136 | } |
| 137 | |
| 138 | static struct acpi_rsdp_descriptor *acpi_scan_for_rsdp(u8 *begin, u32 length) |
| 139 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 140 | struct acpi_rsdp_descriptor *rsdp; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 146 | rsdp = (struct acpi_rsdp_descriptor *)i; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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 | */ |
| 161 | static void acpi_show_data(int fd, u8 * data, int size) |
| 162 | { |
| 163 | char buffer[256]; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 164 | int len; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 165 | int i, remain = size; |
| 166 | while (remain > 0) { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 167 | len = snprintf(buffer, 256, " %04x:", size - remain); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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 | */ |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 189 | |
| 190 | #define MAX_TABLES 128 |
| 191 | int next_table_dump; |
| 192 | u64 dumped_tables[MAX_TABLES]; |
| 193 | |
| 194 | void |
| 195 | set_table_dumped(u64 address) { |
| 196 | if (next_table_dump >= MAX_TABLES) { |
| 197 | printf("increase MAX_TABLES\n"); |
| 198 | exit(1); |
| 199 | } |
| 200 | dumped_tables[next_table_dump++] = address; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * list the tables as they are dumped |
| 205 | * check the list so that they are not dumped twice. |
| 206 | * |
| 207 | * this is needed because we follow both the XSDT and RSDT |
| 208 | * which generally point to all duplicate tables |
| 209 | * except the FADT |
| 210 | */ |
| 211 | int |
| 212 | check_table_dumped(u64 address) { |
| 213 | int i; |
| 214 | |
| 215 | for (i = 0; i < MAX_TABLES; ++i) { |
| 216 | if (address == dumped_tables[i]) |
| 217 | return 1; |
| 218 | if (dumped_tables[i] == 0) |
| 219 | return 0; |
| 220 | } |
| 221 | return 0; |
| 222 | } |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 223 | static void acpi_show_table(int fd, struct acpi_table_header *table, unsigned long addr) |
| 224 | { |
| 225 | char buff[80]; |
| 226 | int len = snprintf(buff, 80, "%.4s @ %p\n", table->signature, (void *)addr); |
| 227 | write(fd, buff, len); |
| 228 | acpi_show_data(fd, (u8 *) table, table->length); |
| 229 | buff[0] = '\n'; |
| 230 | write(fd, buff, 1); |
| 231 | } |
| 232 | |
| 233 | static void write_table(int fd, struct acpi_table_header *tbl, unsigned long addr) |
| 234 | { |
| 235 | static int select_done = 0; |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 236 | |
| 237 | if (check_table_dumped((u64)addr)) |
| 238 | return; |
| 239 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 240 | if (!select_sig[0]) { |
| 241 | if (print) { |
| 242 | acpi_show_table(fd, tbl, addr); |
| 243 | } else { |
| 244 | write(fd, tbl, tbl->length); |
| 245 | } |
| 246 | } else if (!select_done && !memcmp(select_sig, tbl->signature, 4)) { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 247 | if (skip > 0) { |
| 248 | --skip; |
| 249 | return; |
| 250 | } |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 251 | if (print) { |
| 252 | acpi_show_table(fd, tbl, addr); |
| 253 | } else { |
| 254 | write(fd, tbl, tbl->length); |
| 255 | } |
| 256 | select_done = 1; |
| 257 | } |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 258 | set_table_dumped((u64) addr); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | static void acpi_dump_FADT(int fd, struct acpi_table_header *tbl, unsigned long xaddr) { |
| 262 | struct acpi_fadt_descriptor x; |
| 263 | unsigned long addr; |
| 264 | size_t len = sizeof(struct acpi_fadt_descriptor); |
| 265 | if (len > tbl->length) len = tbl->length; |
| 266 | memcpy(&x, tbl, len); |
| 267 | x.header.length = len; |
| 268 | if (checksum((u8 *)tbl, len)) { |
| 269 | fprintf(stderr, "Wrong checksum for FADT!\n"); |
| 270 | } |
| 271 | if (x.header.length >= 148 && x.Xdsdt) { |
| 272 | addr = (unsigned long)x.Xdsdt; |
| 273 | if (connect) { |
| 274 | x.Xdsdt = lseek(fd, 0, SEEK_CUR); |
| 275 | } |
| 276 | } else if (x.header.length >= 44 && x.dsdt) { |
| 277 | addr = (unsigned long)x.dsdt; |
| 278 | if (connect) { |
| 279 | x.dsdt = lseek(fd, 0, SEEK_CUR); |
| 280 | } |
| 281 | } else { |
| 282 | fprintf(stderr, "No DSDT in FADT!\n"); |
| 283 | goto no_dsdt; |
| 284 | } |
| 285 | tbl = acpi_map_table(addr, DSDT_SIG); |
| 286 | if (!tbl) goto no_dsdt; |
| 287 | if (checksum((u8 *)tbl, tbl->length)) |
| 288 | fprintf(stderr, "Wrong checksum for DSDT!\n"); |
| 289 | write_table(fd, tbl, addr); |
| 290 | acpi_unmap_table(tbl); |
| 291 | no_dsdt: |
| 292 | if (x.header.length >= 140 && x.xfirmware_ctrl) { |
| 293 | addr = (unsigned long)x.xfirmware_ctrl; |
| 294 | if (connect) { |
| 295 | x.xfirmware_ctrl = lseek(fd, 0, SEEK_CUR); |
| 296 | } |
| 297 | } else if (x.header.length >= 40 && x.firmware_ctrl) { |
| 298 | addr = (unsigned long)x.firmware_ctrl; |
| 299 | if (connect) { |
| 300 | x.firmware_ctrl = lseek(fd, 0, SEEK_CUR); |
| 301 | } |
| 302 | } else { |
| 303 | fprintf(stderr, "No FACS in FADT!\n"); |
| 304 | goto no_facs; |
| 305 | } |
| 306 | tbl = acpi_map_table(addr, FACS_SIG); |
| 307 | if (!tbl) goto no_facs; |
| 308 | /* do not checksum FACS */ |
| 309 | write_table(fd, tbl, addr); |
| 310 | acpi_unmap_table(tbl); |
| 311 | no_facs: |
| 312 | write_table(fd, (struct acpi_table_header *)&x, xaddr); |
| 313 | } |
| 314 | |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 315 | |
| 316 | static int acpi_dump_RSDT(int fd, struct acpi_rsdp_descriptor *rsdp) |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 317 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 318 | struct acpi_table_header *sdt, *tbl = 0; |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 319 | int i, num; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 320 | char *offset; |
| 321 | unsigned long addr; |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 322 | |
| 323 | tbl = acpi_map_table(rsdp->rsdt_physical_address, "RSDT"); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 324 | if (!tbl) return 0; |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 325 | |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 326 | sdt = malloc(tbl->length); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 327 | memcpy(sdt, tbl, tbl->length); |
| 328 | acpi_unmap_table(tbl); |
| 329 | if (checksum((u8 *)sdt, sdt->length)) |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 330 | fprintf(stderr, "Wrong checksum for %s!\n", "RSDT"); |
| 331 | num = (sdt->length - sizeof(struct acpi_table_header))/sizeof(u32); |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 332 | offset = (char *)sdt + sizeof(struct acpi_table_header); |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 333 | for (i = 0; i < num; ++i, offset += sizeof(u32)) { |
| 334 | addr = (unsigned long)(*(u32 *)offset); |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 335 | if (!addr) continue; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 336 | tbl = acpi_map_table(addr, 0); |
| 337 | if (!tbl) continue; |
| 338 | if (!memcmp(tbl->signature, FADT_SIG, 4)) { |
| 339 | acpi_dump_FADT(fd, tbl, addr); |
| 340 | } else { |
| 341 | if (checksum((u8 *)tbl, tbl->length)) |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 342 | fprintf(stderr, "Wrong checksum for %.4s!\n", tbl->signature); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 343 | write_table(fd, tbl, addr); |
| 344 | } |
| 345 | acpi_unmap_table(tbl); |
| 346 | if (connect) { |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 347 | (*(u32*)offset) = lseek(fd, 0, SEEK_CUR); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 348 | } |
| 349 | } |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 350 | addr = (unsigned long)rsdp->rsdt_physical_address; |
| 351 | if (connect) { |
| 352 | rsdp->rsdt_physical_address = lseek(fd, 0, SEEK_CUR); |
| 353 | } |
| 354 | write_table(fd, sdt, addr); |
| 355 | free (sdt); |
| 356 | return 1; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | static int acpi_dump_XSDT(int fd, struct acpi_rsdp_descriptor *rsdp) |
| 361 | { |
| 362 | struct acpi_table_header *sdt, *tbl = 0; |
| 363 | int i, num; |
| 364 | char *offset; |
| 365 | unsigned long addr; |
| 366 | if (rsdp->revision > 1 && rsdp->xsdt_physical_address) { |
| 367 | tbl = acpi_map_table(rsdp->xsdt_physical_address, "XSDT"); |
| 368 | } |
| 369 | if (!tbl) return 0; |
| 370 | |
| 371 | sdt = malloc(tbl->length); |
| 372 | memcpy(sdt, tbl, tbl->length); |
| 373 | acpi_unmap_table(tbl); |
| 374 | if (checksum((u8 *)sdt, sdt->length)) |
| 375 | fprintf(stderr, "Wrong checksum for %s!\n", "XSDT"); |
| 376 | num = (sdt->length - sizeof(struct acpi_table_header))/sizeof(u64); |
| 377 | offset = (char *)sdt + sizeof(struct acpi_table_header); |
| 378 | for (i = 0; i < num; ++i, offset += sizeof(u64)) { |
| 379 | addr = (unsigned long)(*(u64 *)offset); |
| 380 | if (!addr) continue; |
| 381 | tbl = acpi_map_table(addr, 0); |
| 382 | if (!tbl) continue; |
| 383 | if (!memcmp(tbl->signature, FADT_SIG, 4)) { |
| 384 | acpi_dump_FADT(fd, tbl, addr); |
| 385 | } else { |
| 386 | if (checksum((u8 *)tbl, tbl->length)) |
| 387 | fprintf(stderr, "Wrong checksum for %.4s\n", tbl->signature); |
| 388 | write_table(fd, tbl, addr); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 389 | } |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 390 | acpi_unmap_table(tbl); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 391 | if (connect) { |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 392 | (*(u64*)offset) = lseek(fd, 0, SEEK_CUR); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 393 | } |
| 394 | } |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 395 | addr = (unsigned long)rsdp->xsdt_physical_address; |
| 396 | if (connect) { |
| 397 | rsdp->xsdt_physical_address = lseek(fd, 0, SEEK_CUR); |
| 398 | } |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 399 | write_table(fd, sdt, addr); |
| 400 | free (sdt); |
| 401 | return 1; |
| 402 | } |
| 403 | |
| 404 | static void usage(const char *progname) |
| 405 | { |
| 406 | puts("Usage:"); |
| 407 | printf("%s [--addr 0x1234][--table DSDT][--output filename]" |
| 408 | "[--binary][--length 0x456][--help]\n", progname); |
| 409 | puts("\t--addr 0x1234 or -a 0x1234 -- look for tables at this physical address"); |
| 410 | puts("\t--table DSDT or -t DSDT -- only dump table with DSDT signature"); |
| 411 | puts("\t--output filename or -o filename -- redirect output from stdin to filename"); |
| 412 | puts("\t--binary or -b -- dump data in binary form rather than in hex-dump format"); |
| 413 | puts("\t--length 0x456 or -l 0x456 -- works only with --addr, dump physical memory" |
| 414 | "\n\t\tregion without trying to understand it's contents"); |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 415 | puts("\t--skip 2 or -s 2 -- skip 2 tables of the given name and output only 3rd one"); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 416 | puts("\t--help or -h -- this help message"); |
| 417 | exit(0); |
| 418 | } |
| 419 | |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 420 | static struct option long_options[] = { |
| 421 | {"addr", 1, 0, 0}, |
| 422 | {"table", 1, 0, 0}, |
| 423 | {"output", 1, 0, 0}, |
| 424 | {"binary", 0, 0, 0}, |
| 425 | {"length", 1, 0, 0}, |
| 426 | {"skip", 1, 0, 0}, |
| 427 | {"help", 0, 0, 0}, |
| 428 | {0, 0, 0, 0} |
| 429 | }; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 430 | int main(int argc, char **argv) |
| 431 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 432 | int option_index, c, fd; |
| 433 | u8 *raw; |
| 434 | struct acpi_rsdp_descriptor rsdpx, *x = 0; |
| 435 | char *filename = 0; |
| 436 | char buff[80]; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 437 | memset(select_sig, 0, 4); |
| 438 | print = 1; |
| 439 | connect = 0; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 440 | addr = length = 0; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 441 | skip = 0; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 442 | while (1) { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 443 | option_index = 0; |
| 444 | c = getopt_long(argc, argv, "a:t:o:bl:s:h", |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 445 | long_options, &option_index); |
| 446 | if (c == -1) |
| 447 | break; |
| 448 | |
| 449 | switch (c) { |
| 450 | case 0: |
| 451 | switch (option_index) { |
| 452 | case 0: |
| 453 | addr = strtoul(optarg, (char **)NULL, 16); |
| 454 | break; |
| 455 | case 1: |
| 456 | memcpy(select_sig, optarg, 4); |
| 457 | break; |
| 458 | case 2: |
| 459 | filename = optarg; |
| 460 | break; |
| 461 | case 3: |
| 462 | print = 0; |
| 463 | break; |
| 464 | case 4: |
| 465 | length = strtoul(optarg, (char **)NULL, 16); |
| 466 | break; |
| 467 | case 5: |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 468 | skip = strtoul(optarg, (char **)NULL, 10); |
| 469 | break; |
| 470 | case 6: |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 471 | usage(argv[0]); |
| 472 | exit(0); |
| 473 | } |
| 474 | break; |
| 475 | case 'a': |
| 476 | addr = strtoul(optarg, (char **)NULL, 16); |
| 477 | break; |
| 478 | case 't': |
| 479 | memcpy(select_sig, optarg, 4); |
| 480 | break; |
| 481 | case 'o': |
| 482 | filename = optarg; |
| 483 | break; |
| 484 | case 'b': |
| 485 | print = 0; |
| 486 | break; |
| 487 | case 'l': |
| 488 | length = strtoul(optarg, (char **)NULL, 16); |
| 489 | break; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 490 | case 's': |
| 491 | skip = strtoul(optarg, (char **)NULL, 10); |
| 492 | break; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 493 | case 'h': |
| 494 | usage(argv[0]); |
| 495 | exit(0); |
| 496 | default: |
| 497 | printf("Unknown option!\n"); |
| 498 | usage(argv[0]); |
| 499 | exit(0); |
| 500 | } |
| 501 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 502 | |
| 503 | fd = STDOUT_FILENO; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 504 | if (filename) { |
| 505 | fd = creat(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); |
| 506 | if (fd < 0) |
| 507 | return fd; |
| 508 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 509 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 510 | if (!select_sig[0] && !print) { |
| 511 | connect = 1; |
| 512 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 513 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 514 | psz = sysconf(_SC_PAGESIZE); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 515 | if (length && addr) { |
| 516 | /* We know length and address, it means we just want a memory dump */ |
| 517 | if (!(raw = acpi_map_memory(addr, length))) |
| 518 | goto not_found; |
| 519 | write(fd, raw, length); |
| 520 | acpi_unmap_memory(raw, length); |
| 521 | return 0; |
| 522 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 523 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 524 | length = sizeof(struct acpi_rsdp_descriptor); |
| 525 | if (!addr) { |
| 526 | addr = read_efi_systab(); |
| 527 | if (!addr) { |
| 528 | addr = ACPI_HI_RSDP_WINDOW_BASE; |
| 529 | length = ACPI_HI_RSDP_WINDOW_SIZE; |
| 530 | } |
| 531 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 532 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 533 | if (!(raw = acpi_map_memory(addr, length)) || |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 534 | !(x = acpi_scan_for_rsdp(raw, length))) |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 535 | goto not_found; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 536 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 537 | /* Find RSDP and print all found tables */ |
| 538 | memcpy(&rsdpx, x, sizeof(struct acpi_rsdp_descriptor)); |
| 539 | acpi_unmap_memory(raw, length); |
| 540 | if (connect) { |
| 541 | lseek(fd, sizeof(struct acpi_rsdp_descriptor), SEEK_SET); |
| 542 | } |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame^] | 543 | if (!acpi_dump_XSDT(fd, &rsdpx)) |
| 544 | goto not_found; |
| 545 | if (!acpi_dump_RSDT(fd, &rsdpx)) |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 546 | goto not_found; |
| 547 | if (connect) { |
| 548 | lseek(fd, 0, SEEK_SET); |
| 549 | write(fd, x, (rsdpx.revision < 2) ? |
| 550 | ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH); |
| 551 | } else if (!select_sig[0] || !memcmp("RSD PTR ", select_sig, 4)) { |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 552 | addr += (long)x - (long)raw; |
| 553 | length = snprintf(buff, 80, "RSD PTR @ %p\n", (void *)addr); |
| 554 | write(fd, buff, length); |
| 555 | acpi_show_data(fd, (u8 *) & rsdpx, (rsdpx.revision < 2) ? |
| 556 | ACPI_RSDP_CHECKSUM_LENGTH : ACPI_RSDP_XCHECKSUM_LENGTH); |
| 557 | buff[0] = '\n'; |
| 558 | write(fd, buff, 1); |
| 559 | } |
| 560 | return 0; |
| 561 | not_found: |
| 562 | fprintf(stderr, "ACPI tables were not found. If you know location " |
| 563 | "of RSD PTR table (from dmesg, etc), " |
| 564 | "supply it with either --addr or -a option\n"); |
| 565 | return 1; |
| 566 | } |