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 | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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 | |
| 48 | typedef int s32; |
| 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; |
| 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 | |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 65 | #include <sys/types.h> |
| 66 | #include <dirent.h> |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 67 | |
| 68 | #include <acpi/acconfig.h> |
| 69 | #include <acpi/platform/acenv.h> |
| 70 | #include <acpi/actypes.h> |
| 71 | #include <acpi/actbl.h> |
| 72 | |
| 73 | static inline u8 checksum(u8 * buffer, u32 length) |
| 74 | { |
| 75 | u8 sum = 0, *i = buffer; |
| 76 | buffer += length; |
| 77 | for (; i < buffer; sum += *(i++)); |
| 78 | return sum; |
| 79 | } |
| 80 | |
| 81 | static unsigned long psz, addr, length; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 82 | static int print, connect, skip; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 83 | static u8 select_sig[4]; |
| 84 | |
| 85 | static unsigned long read_efi_systab( void ) |
| 86 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 87 | char buffer[80]; |
| 88 | unsigned long addr; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 89 | FILE *f = fopen("/sys/firmware/efi/systab", "r"); |
| 90 | if (f) { |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 91 | while (fgets(buffer, 80, f)) { |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 92 | if (sscanf(buffer, "ACPI20=0x%lx", &addr) == 1) |
| 93 | return addr; |
| 94 | } |
| 95 | fclose(f); |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | static u8 *acpi_map_memory(unsigned long where, unsigned length) |
| 101 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 102 | unsigned long offset; |
| 103 | u8 *there; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 104 | int fd = open("/dev/mem", O_RDONLY); |
| 105 | if (fd < 0) { |
| 106 | fprintf(stderr, "acpi_os_map_memory: cannot open /dev/mem\n"); |
| 107 | exit(1); |
| 108 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 109 | offset = where % psz; |
| 110 | there = mmap(NULL, length + offset, PROT_READ, MAP_PRIVATE, |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 111 | fd, where - offset); |
| 112 | close(fd); |
| 113 | if (there == MAP_FAILED) return 0; |
| 114 | return (there + offset); |
| 115 | } |
| 116 | |
| 117 | static void acpi_unmap_memory(u8 * there, unsigned length) |
| 118 | { |
| 119 | unsigned long offset = (unsigned long)there % psz; |
| 120 | munmap(there - offset, length + offset); |
| 121 | } |
| 122 | |
| 123 | static struct acpi_table_header *acpi_map_table(unsigned long where, char *sig) |
| 124 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 125 | unsigned size; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 129 | size = tbl->length; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 130 | acpi_unmap_memory((u8 *) tbl, sizeof(struct acpi_table_header)); |
| 131 | return (struct acpi_table_header *)acpi_map_memory(where, size); |
| 132 | } |
| 133 | |
| 134 | static void acpi_unmap_table(struct acpi_table_header *tbl) |
| 135 | { |
| 136 | acpi_unmap_memory((u8 *)tbl, tbl->length); |
| 137 | } |
| 138 | |
| 139 | static struct acpi_rsdp_descriptor *acpi_scan_for_rsdp(u8 *begin, u32 length) |
| 140 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 141 | struct acpi_rsdp_descriptor *rsdp; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 147 | rsdp = (struct acpi_rsdp_descriptor *)i; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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 */ |
| 153 | return rsdp; |
| 154 | } |
| 155 | /* Searched entire block, no RSDP was found */ |
| 156 | return 0; |
| 157 | } |
| 158 | |
| 159 | /* |
| 160 | * Output data |
| 161 | */ |
| 162 | static void acpi_show_data(int fd, u8 * data, int size) |
| 163 | { |
| 164 | char buffer[256]; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 165 | int len; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 166 | int i, remain = size; |
| 167 | while (remain > 0) { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 168 | len = snprintf(buffer, 256, " %04x:", size - remain); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 169 | for (i = 0; i < 16 && i < remain; i++) { |
| 170 | len += |
| 171 | snprintf(&buffer[len], 256 - len, " %02x", data[i]); |
| 172 | } |
| 173 | for (; i < 16; i++) { |
| 174 | len += snprintf(&buffer[len], 256 - len, " "); |
| 175 | } |
| 176 | len += snprintf(&buffer[len], 256 - len, " "); |
| 177 | for (i = 0; i < 16 && i < remain; i++) { |
| 178 | buffer[len++] = (isprint(data[i])) ? data[i] : '.'; |
| 179 | } |
| 180 | buffer[len++] = '\n'; |
| 181 | write(fd, buffer, len); |
| 182 | data += 16; |
| 183 | remain -= 16; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | * Output ACPI table |
| 189 | */ |
| 190 | static void acpi_show_table(int fd, struct acpi_table_header *table, unsigned long addr) |
| 191 | { |
| 192 | char buff[80]; |
| 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); |
| 196 | buff[0] = '\n'; |
| 197 | write(fd, buff, 1); |
| 198 | } |
| 199 | |
| 200 | static void write_table(int fd, struct acpi_table_header *tbl, unsigned long addr) |
| 201 | { |
| 202 | static int select_done = 0; |
| 203 | if (!select_sig[0]) { |
| 204 | if (print) { |
| 205 | acpi_show_table(fd, tbl, addr); |
| 206 | } else { |
| 207 | write(fd, tbl, tbl->length); |
| 208 | } |
| 209 | } else if (!select_done && !memcmp(select_sig, tbl->signature, 4)) { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 210 | if (skip > 0) { |
| 211 | --skip; |
| 212 | return; |
| 213 | } |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 214 | if (print) { |
| 215 | acpi_show_table(fd, tbl, addr); |
| 216 | } else { |
| 217 | write(fd, tbl, tbl->length); |
| 218 | } |
| 219 | select_done = 1; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static void acpi_dump_FADT(int fd, struct acpi_table_header *tbl, unsigned long xaddr) { |
| 224 | struct acpi_fadt_descriptor x; |
| 225 | unsigned long addr; |
| 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"); |
| 232 | } |
| 233 | if (x.header.length >= 148 && x.Xdsdt) { |
| 234 | addr = (unsigned long)x.Xdsdt; |
| 235 | if (connect) { |
| 236 | x.Xdsdt = lseek(fd, 0, SEEK_CUR); |
| 237 | } |
| 238 | } else if (x.header.length >= 44 && x.dsdt) { |
| 239 | addr = (unsigned long)x.dsdt; |
| 240 | if (connect) { |
| 241 | x.dsdt = lseek(fd, 0, SEEK_CUR); |
| 242 | } |
| 243 | } else { |
| 244 | fprintf(stderr, "No DSDT in FADT!\n"); |
| 245 | goto no_dsdt; |
| 246 | } |
| 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); |
| 253 | no_dsdt: |
| 254 | if (x.header.length >= 140 && x.xfirmware_ctrl) { |
| 255 | addr = (unsigned long)x.xfirmware_ctrl; |
| 256 | if (connect) { |
| 257 | x.xfirmware_ctrl = lseek(fd, 0, SEEK_CUR); |
| 258 | } |
| 259 | } else if (x.header.length >= 40 && x.firmware_ctrl) { |
| 260 | addr = (unsigned long)x.firmware_ctrl; |
| 261 | if (connect) { |
| 262 | x.firmware_ctrl = lseek(fd, 0, SEEK_CUR); |
| 263 | } |
| 264 | } else { |
| 265 | fprintf(stderr, "No FACS in FADT!\n"); |
| 266 | goto no_facs; |
| 267 | } |
| 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); |
| 273 | no_facs: |
| 274 | write_table(fd, (struct acpi_table_header *)&x, xaddr); |
| 275 | } |
| 276 | |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 277 | static int acpi_dump_SDT(int fd, struct acpi_rsdp_descriptor *rsdp) |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 278 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 279 | struct acpi_table_header *sdt, *tbl = 0; |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 280 | int xsdt = 1, i, num; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 281 | char *offset; |
| 282 | unsigned long addr; |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 283 | if (rsdp->revision > 1 && rsdp->xsdt_physical_address) { |
| 284 | tbl = acpi_map_table(rsdp->xsdt_physical_address, "XSDT"); |
| 285 | } |
| 286 | if (!tbl && rsdp->rsdt_physical_address) { |
| 287 | xsdt = 0; |
| 288 | tbl = acpi_map_table(rsdp->rsdt_physical_address, "RSDT"); |
| 289 | } |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 290 | if (!tbl) return 0; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 291 | sdt = malloc(tbl->length); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 292 | memcpy(sdt, tbl, tbl->length); |
| 293 | acpi_unmap_table(tbl); |
| 294 | if (checksum((u8 *)sdt, sdt->length)) |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 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)); |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 297 | offset = (char *)sdt + sizeof(struct acpi_table_header); |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 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); |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 301 | if (!addr) continue; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 302 | tbl = acpi_map_table(addr, 0); |
| 303 | if (!tbl) continue; |
| 304 | if (!memcmp(tbl->signature, FADT_SIG, 4)) { |
| 305 | acpi_dump_FADT(fd, tbl, addr); |
| 306 | } else { |
| 307 | if (checksum((u8 *)tbl, tbl->length)) |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 308 | fprintf(stderr, "Wrong checksum for generic table!\n"); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 309 | write_table(fd, tbl, addr); |
| 310 | } |
| 311 | acpi_unmap_table(tbl); |
| 312 | if (connect) { |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 313 | if (xsdt) |
| 314 | (*(u64*)offset) = lseek(fd, 0, SEEK_CUR); |
| 315 | else |
| 316 | (*(u32*)offset) = lseek(fd, 0, SEEK_CUR); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 317 | } |
| 318 | } |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 319 | if (xsdt) { |
| 320 | addr = (unsigned long)rsdp->xsdt_physical_address; |
| 321 | if (connect) { |
| 322 | rsdp->xsdt_physical_address = lseek(fd, 0, SEEK_CUR); |
| 323 | } |
| 324 | } else { |
| 325 | addr = (unsigned long)rsdp->rsdt_physical_address; |
| 326 | if (connect) { |
| 327 | rsdp->rsdt_physical_address = lseek(fd, 0, SEEK_CUR); |
| 328 | } |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame] | 329 | } |
| 330 | write_table(fd, sdt, addr); |
| 331 | free (sdt); |
| 332 | return 1; |
| 333 | } |
| 334 | |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 335 | #define DYNAMIC_SSDT "/sys/firmware/acpi/tables/dynamic" |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame] | 336 | |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 337 | static void acpi_dump_dynamic_SSDT(int fd) |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame] | 338 | { |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 339 | struct stat file_stat; |
| 340 | char filename[256], *ptr; |
| 341 | DIR *tabledir; |
| 342 | struct dirent *entry; |
| 343 | FILE *fp; |
| 344 | int count, readcount, length; |
| 345 | struct acpi_table_header table_header, *ptable; |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame] | 346 | |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 347 | if (stat(DYNAMIC_SSDT, &file_stat) == -1) { |
| 348 | /* The directory doesn't exist */ |
| 349 | return; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 350 | } |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 351 | tabledir = opendir(DYNAMIC_SSDT); |
| 352 | if(!tabledir){ |
| 353 | /*can't open the directory */ |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | while ((entry = readdir(tabledir)) != 0){ |
| 358 | /* skip the file of . /.. */ |
| 359 | if (entry->d_name[0] == '.') |
| 360 | continue; |
| 361 | |
| 362 | sprintf(filename, "%s/%s", DYNAMIC_SSDT, entry->d_name); |
| 363 | fp = fopen(filename, "r"); |
| 364 | if (fp == NULL) { |
| 365 | fprintf(stderr, "Can't open the file of %s\n", |
| 366 | filename); |
| 367 | continue; |
| 368 | } |
| 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 */ |
| 373 | fclose(fp); |
| 374 | continue; |
| 375 | } |
| 376 | length = table_header.length; |
| 377 | ptr = malloc(table_header.length); |
| 378 | fseek(fp, 0, SEEK_SET); |
| 379 | readcount = 0; |
| 380 | while(!feof(fp) && readcount < length) { |
| 381 | count = fread(ptr + readcount, 1, 256, fp); |
| 382 | readcount += count; |
| 383 | } |
| 384 | fclose(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); |
| 390 | free(ptr); |
Len Brown | 981efe9 | 2012-09-22 22:49:25 -0400 | [diff] [blame] | 391 | } |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 392 | closedir(tabledir); |
| 393 | return; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static void usage(const char *progname) |
| 397 | { |
| 398 | puts("Usage:"); |
| 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"); |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 407 | 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] | 408 | puts("\t--help or -h -- this help message"); |
| 409 | exit(0); |
| 410 | } |
| 411 | |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 412 | static struct option long_options[] = { |
| 413 | {"addr", 1, 0, 0}, |
| 414 | {"table", 1, 0, 0}, |
| 415 | {"output", 1, 0, 0}, |
| 416 | {"binary", 0, 0, 0}, |
| 417 | {"length", 1, 0, 0}, |
| 418 | {"skip", 1, 0, 0}, |
| 419 | {"help", 0, 0, 0}, |
| 420 | {0, 0, 0, 0} |
| 421 | }; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 422 | int main(int argc, char **argv) |
| 423 | { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 424 | int option_index, c, fd; |
| 425 | u8 *raw; |
| 426 | struct acpi_rsdp_descriptor rsdpx, *x = 0; |
| 427 | char *filename = 0; |
| 428 | char buff[80]; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 429 | memset(select_sig, 0, 4); |
| 430 | print = 1; |
| 431 | connect = 0; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 432 | addr = length = 0; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 433 | skip = 0; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 434 | while (1) { |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 435 | option_index = 0; |
| 436 | c = getopt_long(argc, argv, "a:t:o:bl:s:h", |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 437 | long_options, &option_index); |
| 438 | if (c == -1) |
| 439 | break; |
| 440 | |
| 441 | switch (c) { |
| 442 | case 0: |
| 443 | switch (option_index) { |
| 444 | case 0: |
| 445 | addr = strtoul(optarg, (char **)NULL, 16); |
| 446 | break; |
| 447 | case 1: |
| 448 | memcpy(select_sig, optarg, 4); |
| 449 | break; |
| 450 | case 2: |
| 451 | filename = optarg; |
| 452 | break; |
| 453 | case 3: |
| 454 | print = 0; |
| 455 | break; |
| 456 | case 4: |
| 457 | length = strtoul(optarg, (char **)NULL, 16); |
| 458 | break; |
| 459 | case 5: |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 460 | skip = strtoul(optarg, (char **)NULL, 10); |
| 461 | break; |
| 462 | case 6: |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 463 | usage(argv[0]); |
| 464 | exit(0); |
| 465 | } |
| 466 | break; |
| 467 | case 'a': |
| 468 | addr = strtoul(optarg, (char **)NULL, 16); |
| 469 | break; |
| 470 | case 't': |
| 471 | memcpy(select_sig, optarg, 4); |
| 472 | break; |
| 473 | case 'o': |
| 474 | filename = optarg; |
| 475 | break; |
| 476 | case 'b': |
| 477 | print = 0; |
| 478 | break; |
| 479 | case 'l': |
| 480 | length = strtoul(optarg, (char **)NULL, 16); |
| 481 | break; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 482 | case 's': |
| 483 | skip = strtoul(optarg, (char **)NULL, 10); |
| 484 | break; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 485 | case 'h': |
| 486 | usage(argv[0]); |
| 487 | exit(0); |
| 488 | default: |
| 489 | printf("Unknown option!\n"); |
| 490 | usage(argv[0]); |
| 491 | exit(0); |
| 492 | } |
| 493 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 494 | |
| 495 | fd = STDOUT_FILENO; |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 496 | if (filename) { |
| 497 | fd = creat(filename, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); |
| 498 | if (fd < 0) |
| 499 | return fd; |
| 500 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 501 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 502 | if (!select_sig[0] && !print) { |
| 503 | connect = 1; |
| 504 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 505 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 506 | psz = sysconf(_SC_PAGESIZE); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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))) |
| 510 | goto not_found; |
| 511 | write(fd, raw, length); |
| 512 | acpi_unmap_memory(raw, length); |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 513 | close(fd); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 514 | return 0; |
| 515 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 516 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 517 | length = sizeof(struct acpi_rsdp_descriptor); |
| 518 | if (!addr) { |
| 519 | addr = read_efi_systab(); |
| 520 | if (!addr) { |
| 521 | addr = ACPI_HI_RSDP_WINDOW_BASE; |
| 522 | length = ACPI_HI_RSDP_WINDOW_SIZE; |
| 523 | } |
| 524 | } |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 525 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 526 | if (!(raw = acpi_map_memory(addr, length)) || |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 527 | !(x = acpi_scan_for_rsdp(raw, length))) |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 528 | goto not_found; |
Len Brown | 4f10042 | 2012-09-22 22:43:08 -0400 | [diff] [blame] | 529 | |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 530 | /* Find RSDP and print all found tables */ |
| 531 | memcpy(&rsdpx, x, sizeof(struct acpi_rsdp_descriptor)); |
| 532 | acpi_unmap_memory(raw, length); |
| 533 | if (connect) { |
| 534 | lseek(fd, sizeof(struct acpi_rsdp_descriptor), SEEK_SET); |
| 535 | } |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 536 | if (!acpi_dump_SDT(fd, &rsdpx)) |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 537 | goto not_found; |
| 538 | if (connect) { |
| 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)) { |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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); |
| 548 | buff[0] = '\n'; |
| 549 | write(fd, buff, 1); |
| 550 | } |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 551 | acpi_dump_dynamic_SSDT(fd); |
| 552 | close(fd); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 553 | return 0; |
| 554 | not_found: |
Len Brown | d4bb1c9 | 2012-09-22 23:09:15 -0400 | [diff] [blame^] | 555 | close(fd); |
Len Brown | 0efea7b | 2012-09-22 22:33:19 -0400 | [diff] [blame] | 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"); |
| 559 | return 1; |
| 560 | } |