xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision aa64588d28258aef88cc33b8043112e8856948d0)
1 /*-
2  * Copyright (c) 1998 Doug Rabson
3  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
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  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 #include <sys/param.h>
31 #include <sys/endian.h>
32 #include <sys/stat.h>
33 #include <sys/wait.h>
34 #include <assert.h>
35 #include <err.h>
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdint.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include "acpidump.h"
45 
46 #define BEGIN_COMMENT	"/*\n"
47 #define END_COMMENT	" */\n"
48 
49 static void	acpi_print_string(char *s, size_t length);
50 static void	acpi_print_gas(ACPI_GENERIC_ADDRESS *gas);
51 static int	acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt);
52 static void	acpi_handle_fadt(ACPI_TABLE_HEADER *fadt);
53 static void	acpi_print_cpu(u_char cpu_id);
54 static void	acpi_print_cpu_uid(uint32_t uid, char *uid_string);
55 static void	acpi_print_local_apic(uint32_t apic_id, uint32_t flags);
56 static void	acpi_print_io_apic(uint32_t apic_id, uint32_t int_base,
57 		    uint64_t apic_addr);
58 static void	acpi_print_mps_flags(uint16_t flags);
59 static void	acpi_print_intr(uint32_t intr, uint16_t mps_flags);
60 static void	acpi_print_local_nmi(u_int lint, uint16_t mps_flags);
61 static void	acpi_print_madt(ACPI_SUBTABLE_HEADER *mp);
62 static void	acpi_handle_madt(ACPI_TABLE_HEADER *sdp);
63 static void	acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp);
64 static void	acpi_handle_hpet(ACPI_TABLE_HEADER *sdp);
65 static void	acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp);
66 static void	acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
67 		    uint32_t flags);
68 static void	acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp);
69 static void	acpi_print_srat(ACPI_SUBTABLE_HEADER *srat);
70 static void	acpi_handle_srat(ACPI_TABLE_HEADER *sdp);
71 static void	acpi_print_sdt(ACPI_TABLE_HEADER *sdp);
72 static void	acpi_print_fadt(ACPI_TABLE_HEADER *sdp);
73 static void	acpi_print_facs(ACPI_TABLE_FACS *facs);
74 static void	acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp);
75 static ACPI_TABLE_HEADER *acpi_map_sdt(vm_offset_t pa);
76 static void	acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp);
77 static void	acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp);
78 static void	acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
79 		    void (*action)(ACPI_SUBTABLE_HEADER *));
80 
81 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
82 static int addr_size;
83 
84 static void
85 acpi_print_string(char *s, size_t length)
86 {
87 	int	c;
88 
89 	/* Trim trailing spaces and NULLs */
90 	while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
91 		length--;
92 
93 	while (length--) {
94 		c = *s++;
95 		putchar(c);
96 	}
97 }
98 
99 static void
100 acpi_print_gas(ACPI_GENERIC_ADDRESS *gas)
101 {
102 	switch(gas->SpaceId) {
103 	case ACPI_GAS_MEMORY:
104 		printf("0x%08lx:%u[%u] (Memory)", (u_long)gas->Address,
105 		       gas->BitOffset, gas->BitWidth);
106 		break;
107 	case ACPI_GAS_IO:
108 		printf("0x%02lx:%u[%u] (IO)", (u_long)gas->Address,
109 		       gas->BitOffset, gas->BitWidth);
110 		break;
111 	case ACPI_GAS_PCI:
112 		printf("%x:%x+0x%x (PCI)", (uint16_t)(gas->Address >> 32),
113 		       (uint16_t)((gas->Address >> 16) & 0xffff),
114 		       (uint16_t)gas->Address);
115 		break;
116 	/* XXX How to handle these below? */
117 	case ACPI_GAS_EMBEDDED:
118 		printf("0x%x:%u[%u] (EC)", (uint16_t)gas->Address,
119 		       gas->BitOffset, gas->BitWidth);
120 		break;
121 	case ACPI_GAS_SMBUS:
122 		printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->Address,
123 		       gas->BitOffset, gas->BitWidth);
124 		break;
125 	case ACPI_GAS_CMOS:
126 	case ACPI_GAS_PCIBAR:
127 	case ACPI_GAS_DATATABLE:
128 	case ACPI_GAS_FIXED:
129 	default:
130 		printf("0x%08lx (?)", (u_long)gas->Address);
131 		break;
132 	}
133 }
134 
135 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
136 static int
137 acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt)
138 {
139 	int fadt_revision;
140 
141 	/* Set the FADT revision separately from the RSDP version. */
142 	if (addr_size == 8) {
143 		fadt_revision = 2;
144 
145 		/*
146 		 * A few systems (e.g., IBM T23) have an RSDP that claims
147 		 * revision 2 but the 64 bit addresses are invalid.  If
148 		 * revision 2 and the 32 bit address is non-zero but the
149 		 * 32 and 64 bit versions don't match, prefer the 32 bit
150 		 * version for all subsequent tables.
151 		 */
152 		if (fadt->Facs != 0 &&
153 		    (fadt->XFacs & 0xffffffff) != fadt->Facs)
154 			fadt_revision = 1;
155 	} else
156 		fadt_revision = 1;
157 	return (fadt_revision);
158 }
159 
160 static void
161 acpi_handle_fadt(ACPI_TABLE_HEADER *sdp)
162 {
163 	ACPI_TABLE_HEADER *dsdp;
164 	ACPI_TABLE_FACS	*facs;
165 	ACPI_TABLE_FADT *fadt;
166 	int		fadt_revision;
167 
168 	fadt = (ACPI_TABLE_FADT *)sdp;
169 	acpi_print_fadt(sdp);
170 
171 	fadt_revision = acpi_get_fadt_revision(fadt);
172 	if (fadt_revision == 1)
173 		facs = (ACPI_TABLE_FACS *)acpi_map_sdt(fadt->Facs);
174 	else
175 		facs = (ACPI_TABLE_FACS *)acpi_map_sdt(fadt->XFacs);
176 	if (memcmp(facs->Signature, ACPI_SIG_FACS, 4) != 0 || facs->Length < 64)
177 		errx(1, "FACS is corrupt");
178 	acpi_print_facs(facs);
179 
180 	if (fadt_revision == 1)
181 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
182 	else
183 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
184 	if (acpi_checksum(dsdp, dsdp->Length))
185 		errx(1, "DSDT is corrupt");
186 	acpi_print_dsdt(dsdp);
187 }
188 
189 static void
190 acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
191     void (*action)(ACPI_SUBTABLE_HEADER *))
192 {
193 	ACPI_SUBTABLE_HEADER *subtable;
194 	char *end;
195 
196 	subtable = first;
197 	end = (char *)table + table->Length;
198 	while ((char *)subtable < end) {
199 		printf("\n");
200 		action(subtable);
201 		subtable = (ACPI_SUBTABLE_HEADER *)((char *)subtable +
202 		    subtable->Length);
203 	}
204 }
205 
206 static void
207 acpi_print_cpu(u_char cpu_id)
208 {
209 
210 	printf("\tACPI CPU=");
211 	if (cpu_id == 0xff)
212 		printf("ALL\n");
213 	else
214 		printf("%d\n", (u_int)cpu_id);
215 }
216 
217 static void
218 acpi_print_cpu_uid(uint32_t uid, char *uid_string)
219 {
220 
221 	printf("\tUID=%d", uid);
222 	if (uid_string != NULL)
223 		printf(" (%s)", uid_string);
224 	printf("\n");
225 }
226 
227 static void
228 acpi_print_local_apic(uint32_t apic_id, uint32_t flags)
229 {
230 
231 	printf("\tFlags={");
232 	if (flags & ACPI_MADT_ENABLED)
233 		printf("ENABLED");
234 	else
235 		printf("DISABLED");
236 	printf("}\n");
237 	printf("\tAPIC ID=%d\n", apic_id);
238 }
239 
240 static void
241 acpi_print_io_apic(uint32_t apic_id, uint32_t int_base, uint64_t apic_addr)
242 {
243 
244 	printf("\tAPIC ID=%d\n", apic_id);
245 	printf("\tINT BASE=%d\n", int_base);
246 	printf("\tADDR=0x%016jx\n", (uintmax_t)apic_addr);
247 }
248 
249 static void
250 acpi_print_mps_flags(uint16_t flags)
251 {
252 
253 	printf("\tFlags={Polarity=");
254 	switch (flags & ACPI_MADT_POLARITY_MASK) {
255 	case ACPI_MADT_POLARITY_CONFORMS:
256 		printf("conforming");
257 		break;
258 	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
259 		printf("active-hi");
260 		break;
261 	case ACPI_MADT_POLARITY_ACTIVE_LOW:
262 		printf("active-lo");
263 		break;
264 	default:
265 		printf("0x%x", flags & ACPI_MADT_POLARITY_MASK);
266 		break;
267 	}
268 	printf(", Trigger=");
269 	switch (flags & ACPI_MADT_TRIGGER_MASK) {
270 	case ACPI_MADT_TRIGGER_CONFORMS:
271 		printf("conforming");
272 		break;
273 	case ACPI_MADT_TRIGGER_EDGE:
274 		printf("edge");
275 		break;
276 	case ACPI_MADT_TRIGGER_LEVEL:
277 		printf("level");
278 		break;
279 	default:
280 		printf("0x%x", (flags & ACPI_MADT_TRIGGER_MASK) >> 2);
281 	}
282 	printf("}\n");
283 }
284 
285 static void
286 acpi_print_intr(uint32_t intr, uint16_t mps_flags)
287 {
288 
289 	printf("\tINTR=%d\n", intr);
290 	acpi_print_mps_flags(mps_flags);
291 }
292 
293 static void
294 acpi_print_local_nmi(u_int lint, uint16_t mps_flags)
295 {
296 
297 	printf("\tLINT Pin=%d\n", lint);
298 	acpi_print_mps_flags(mps_flags);
299 }
300 
301 const char *apic_types[] = { "Local APIC", "IO APIC", "INT Override", "NMI",
302 			     "Local APIC NMI", "Local APIC Override",
303 			     "IO SAPIC", "Local SAPIC", "Platform Interrupt",
304 			     "Local X2APIC", "Local X2APIC NMI" };
305 const char *platform_int_types[] = { "0 (unknown)", "PMI", "INIT",
306 				     "Corrected Platform Error" };
307 
308 static void
309 acpi_print_madt(ACPI_SUBTABLE_HEADER *mp)
310 {
311 	ACPI_MADT_LOCAL_APIC *lapic;
312 	ACPI_MADT_IO_APIC *ioapic;
313 	ACPI_MADT_INTERRUPT_OVERRIDE *over;
314 	ACPI_MADT_NMI_SOURCE *nmi;
315 	ACPI_MADT_LOCAL_APIC_NMI *lapic_nmi;
316 	ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_over;
317 	ACPI_MADT_IO_SAPIC *iosapic;
318 	ACPI_MADT_LOCAL_SAPIC *lsapic;
319 	ACPI_MADT_INTERRUPT_SOURCE *isrc;
320 	ACPI_MADT_LOCAL_X2APIC *x2apic;
321 	ACPI_MADT_LOCAL_X2APIC_NMI *x2apic_nmi;
322 
323 	if (mp->Type < sizeof(apic_types) / sizeof(apic_types[0]))
324 		printf("\tType=%s\n", apic_types[mp->Type]);
325 	else
326 		printf("\tType=%d (unknown)\n", mp->Type);
327 	switch (mp->Type) {
328 	case ACPI_MADT_TYPE_LOCAL_APIC:
329 		lapic = (ACPI_MADT_LOCAL_APIC *)mp;
330 		acpi_print_cpu(lapic->ProcessorId);
331 		acpi_print_local_apic(lapic->Id, lapic->LapicFlags);
332 		break;
333 	case ACPI_MADT_TYPE_IO_APIC:
334 		ioapic = (ACPI_MADT_IO_APIC *)mp;
335 		acpi_print_io_apic(ioapic->Id, ioapic->GlobalIrqBase,
336 		    ioapic->Address);
337 		break;
338 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
339 		over = (ACPI_MADT_INTERRUPT_OVERRIDE *)mp;
340 		printf("\tBUS=%d\n", (u_int)over->Bus);
341 		printf("\tIRQ=%d\n", (u_int)over->SourceIrq);
342 		acpi_print_intr(over->GlobalIrq, over->IntiFlags);
343 		break;
344 	case ACPI_MADT_TYPE_NMI_SOURCE:
345 		nmi = (ACPI_MADT_NMI_SOURCE *)mp;
346 		acpi_print_intr(nmi->GlobalIrq, nmi->IntiFlags);
347 		break;
348 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
349 		lapic_nmi = (ACPI_MADT_LOCAL_APIC_NMI *)mp;
350 		acpi_print_cpu(lapic_nmi->ProcessorId);
351 		acpi_print_local_nmi(lapic_nmi->Lint, lapic_nmi->IntiFlags);
352 		break;
353 	case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
354 		lapic_over = (ACPI_MADT_LOCAL_APIC_OVERRIDE *)mp;
355 		printf("\tLocal APIC ADDR=0x%016jx\n",
356 		    (uintmax_t)lapic_over->Address);
357 		break;
358 	case ACPI_MADT_TYPE_IO_SAPIC:
359 		iosapic = (ACPI_MADT_IO_SAPIC *)mp;
360 		acpi_print_io_apic(iosapic->Id, iosapic->GlobalIrqBase,
361 		    iosapic->Address);
362 		break;
363 	case ACPI_MADT_TYPE_LOCAL_SAPIC:
364 		lsapic = (ACPI_MADT_LOCAL_SAPIC *)mp;
365 		acpi_print_cpu(lsapic->ProcessorId);
366 		acpi_print_local_apic(lsapic->Id, lsapic->LapicFlags);
367 		printf("\tAPIC EID=%d\n", (u_int)lsapic->Eid);
368 		if (mp->Length > __offsetof(ACPI_MADT_LOCAL_SAPIC, Uid))
369 			acpi_print_cpu_uid(lsapic->Uid, lsapic->UidString);
370 		break;
371 	case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
372 		isrc = (ACPI_MADT_INTERRUPT_SOURCE *)mp;
373 		if (isrc->Type < sizeof(platform_int_types) /
374 		    sizeof(platform_int_types[0]))
375 			printf("\tType=%s\n", platform_int_types[isrc->Type]);
376 		else
377 			printf("\tType=%d (unknown)\n", isrc->Type);
378 		printf("\tAPIC ID=%d\n", (u_int)isrc->Id);
379 		printf("\tAPIC EID=%d\n", (u_int)isrc->Eid);
380 		printf("\tSAPIC Vector=%d\n", (u_int)isrc->IoSapicVector);
381 		acpi_print_intr(isrc->GlobalIrq, isrc->IntiFlags);
382 		break;
383 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
384 		x2apic = (ACPI_MADT_LOCAL_X2APIC *)mp;
385 		acpi_print_cpu_uid(x2apic->Uid, NULL);
386 		acpi_print_local_apic(x2apic->LocalApicId, x2apic->LapicFlags);
387 		break;
388 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
389 		x2apic_nmi = (ACPI_MADT_LOCAL_X2APIC_NMI *)mp;
390 		acpi_print_cpu_uid(x2apic_nmi->Uid, NULL);
391 		acpi_print_local_nmi(x2apic_nmi->Lint, x2apic_nmi->IntiFlags);
392 		break;
393 	}
394 }
395 
396 static void
397 acpi_handle_madt(ACPI_TABLE_HEADER *sdp)
398 {
399 	ACPI_TABLE_MADT *madt;
400 
401 	printf(BEGIN_COMMENT);
402 	acpi_print_sdt(sdp);
403 	madt = (ACPI_TABLE_MADT *)sdp;
404 	printf("\tLocal APIC ADDR=0x%08x\n", madt->Address);
405 	printf("\tFlags={");
406 	if (madt->Flags & ACPI_MADT_PCAT_COMPAT)
407 		printf("PC-AT");
408 	printf("}\n");
409 	acpi_walk_subtables(sdp, (madt + 1), acpi_print_madt);
410 	printf(END_COMMENT);
411 }
412 
413 static void
414 acpi_handle_hpet(ACPI_TABLE_HEADER *sdp)
415 {
416 	ACPI_TABLE_HPET *hpet;
417 
418 	printf(BEGIN_COMMENT);
419 	acpi_print_sdt(sdp);
420 	hpet = (ACPI_TABLE_HPET *)sdp;
421 	printf("\tHPET Number=%d\n", hpet->Sequence);
422 	printf("\tADDR=");
423 	acpi_print_gas(&hpet->Address);
424 	printf("\tHW Rev=0x%x\n", hpet->Id & ACPI_HPET_ID_HARDWARE_REV_ID);
425 	printf("\tComparators=%d\n", (hpet->Id & ACPI_HPET_ID_COMPARATORS) >>
426 	    8);
427 	printf("\tCounter Size=%d\n", hpet->Id & ACPI_HPET_ID_COUNT_SIZE_CAP ?
428 	    1 : 0);
429 	printf("\tLegacy IRQ routing capable={");
430 	if (hpet->Id & ACPI_HPET_ID_LEGACY_CAPABLE)
431 		printf("TRUE}\n");
432 	else
433 		printf("FALSE}\n");
434 	printf("\tPCI Vendor ID=0x%04x\n", hpet->Id >> 16);
435 	printf("\tMinimal Tick=%d\n", hpet->MinimumTick);
436 	printf(END_COMMENT);
437 }
438 
439 static void
440 acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp)
441 {
442 	ACPI_TABLE_ECDT *ecdt;
443 
444 	printf(BEGIN_COMMENT);
445 	acpi_print_sdt(sdp);
446 	ecdt = (ACPI_TABLE_ECDT *)sdp;
447 	printf("\tEC_CONTROL=");
448 	acpi_print_gas(&ecdt->Control);
449 	printf("\n\tEC_DATA=");
450 	acpi_print_gas(&ecdt->Data);
451 	printf("\n\tUID=%#x, ", ecdt->Uid);
452 	printf("GPE_BIT=%#x\n", ecdt->Gpe);
453 	printf("\tEC_ID=%s\n", ecdt->Id);
454 	printf(END_COMMENT);
455 }
456 
457 static void
458 acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp)
459 {
460 	ACPI_TABLE_MCFG *mcfg;
461 	ACPI_MCFG_ALLOCATION *alloc;
462 	u_int i, entries;
463 
464 	printf(BEGIN_COMMENT);
465 	acpi_print_sdt(sdp);
466 	mcfg = (ACPI_TABLE_MCFG *)sdp;
467 	entries = (sdp->Length - sizeof(ACPI_TABLE_MCFG)) /
468 	    sizeof(ACPI_MCFG_ALLOCATION);
469 	alloc = (ACPI_MCFG_ALLOCATION *)(mcfg + 1);
470 	for (i = 0; i < entries; i++, alloc++) {
471 		printf("\n");
472 		printf("\tBase Address=0x%016jx\n", alloc->Address);
473 		printf("\tSegment Group=0x%04x\n", alloc->PciSegment);
474 		printf("\tStart Bus=%d\n", alloc->StartBusNumber);
475 		printf("\tEnd Bus=%d\n", alloc->EndBusNumber);
476 	}
477 	printf(END_COMMENT);
478 }
479 
480 static void
481 acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
482     uint32_t flags)
483 {
484 
485 	printf("\tFlags={");
486 	if (flags & ACPI_SRAT_CPU_ENABLED)
487 		printf("ENABLED");
488 	else
489 		printf("DISABLED");
490 	printf("}\n");
491 	printf("\tAPIC ID=%d\n", apic_id);
492 	printf("\tProximity Domain=%d\n", proximity_domain);
493 }
494 
495 static void
496 acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
497 {
498 
499 	printf("\tFlags={");
500 	if (mp->Flags & ACPI_SRAT_MEM_ENABLED)
501 		printf("ENABLED");
502 	else
503 		printf("DISABLED");
504 	if (mp->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
505 		printf(",HOT_PLUGGABLE");
506 	if (mp->Flags & ACPI_SRAT_MEM_NON_VOLATILE)
507 		printf(",NON_VOLATILE");
508 	printf("}\n");
509 	printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->BaseAddress);
510 	printf("\tLength=0x%016jx\n", (uintmax_t)mp->Length);
511 	printf("\tProximity Domain=%d\n", mp->ProximityDomain);
512 }
513 
514 const char *srat_types[] = { "CPU", "Memory", "X2APIC" };
515 
516 static void
517 acpi_print_srat(ACPI_SUBTABLE_HEADER *srat)
518 {
519 	ACPI_SRAT_CPU_AFFINITY *cpu;
520 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
521 
522 	if (srat->Type < sizeof(srat_types) / sizeof(srat_types[0]))
523 		printf("\tType=%s\n", srat_types[srat->Type]);
524 	else
525 		printf("\tType=%d (unknown)\n", srat->Type);
526 	switch (srat->Type) {
527 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
528 		cpu = (ACPI_SRAT_CPU_AFFINITY *)srat;
529 		acpi_print_srat_cpu(cpu->ApicId,
530 		    cpu->ProximityDomainHi[2] << 24 |
531 		    cpu->ProximityDomainHi[1] << 16 |
532 		    cpu->ProximityDomainHi[0] << 0 |
533 		    cpu->ProximityDomainLo, cpu->Flags);
534 		break;
535 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
536 		acpi_print_srat_memory((ACPI_SRAT_MEM_AFFINITY *)srat);
537 		break;
538 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
539 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)srat;
540 		acpi_print_srat_cpu(x2apic->ApicId, x2apic->ProximityDomain,
541 		    x2apic->Flags);
542 		break;
543 	}
544 }
545 
546 static void
547 acpi_handle_srat(ACPI_TABLE_HEADER *sdp)
548 {
549 	ACPI_TABLE_SRAT *srat;
550 
551 	printf(BEGIN_COMMENT);
552 	acpi_print_sdt(sdp);
553 	srat = (ACPI_TABLE_SRAT *)sdp;
554 	printf("\tTable Revision=%d\n", srat->TableRevision);
555 	acpi_walk_subtables(sdp, (srat + 1), acpi_print_srat);
556 	printf(END_COMMENT);
557 }
558 
559 static void
560 acpi_print_sdt(ACPI_TABLE_HEADER *sdp)
561 {
562 	printf("  ");
563 	acpi_print_string(sdp->Signature, ACPI_NAME_SIZE);
564 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
565 	       sdp->Length, sdp->Revision, sdp->Checksum);
566 	printf("\tOEMID=");
567 	acpi_print_string(sdp->OemId, ACPI_OEM_ID_SIZE);
568 	printf(", OEM Table ID=");
569 	acpi_print_string(sdp->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
570 	printf(", OEM Revision=0x%x,\n", sdp->OemRevision);
571 	printf("\tCreator ID=");
572 	acpi_print_string(sdp->AslCompilerId, ACPI_NAME_SIZE);
573 	printf(", Creator Revision=0x%x\n", sdp->AslCompilerRevision);
574 }
575 
576 static void
577 acpi_print_rsdt(ACPI_TABLE_HEADER *rsdp)
578 {
579 	ACPI_TABLE_RSDT *rsdt;
580 	ACPI_TABLE_XSDT *xsdt;
581 	int	i, entries;
582 	u_long	addr;
583 
584 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
585 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
586 	printf(BEGIN_COMMENT);
587 	acpi_print_sdt(rsdp);
588 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
589 	printf("\tEntries={ ");
590 	for (i = 0; i < entries; i++) {
591 		if (i > 0)
592 			printf(", ");
593 		switch (addr_size) {
594 		case 4:
595 			addr = le32toh(rsdt->TableOffsetEntry[i]);
596 			break;
597 		case 8:
598 			addr = le64toh(xsdt->TableOffsetEntry[i]);
599 			break;
600 		default:
601 			addr = 0;
602 		}
603 		assert(addr != 0);
604 		printf("0x%08lx", addr);
605 	}
606 	printf(" }\n");
607 	printf(END_COMMENT);
608 }
609 
610 static const char *acpi_pm_profiles[] = {
611 	"Unspecified", "Desktop", "Mobile", "Workstation",
612 	"Enterprise Server", "SOHO Server", "Appliance PC"
613 };
614 
615 static void
616 acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
617 {
618 	ACPI_TABLE_FADT *fadt;
619 	const char *pm;
620 	char	    sep;
621 
622 	fadt = (ACPI_TABLE_FADT *)sdp;
623 	printf(BEGIN_COMMENT);
624 	acpi_print_sdt(sdp);
625 	printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->Facs,
626 	       fadt->Dsdt);
627 	printf("\tINT_MODEL=%s\n", fadt->Model ? "APIC" : "PIC");
628 	if (fadt->PreferredProfile >= sizeof(acpi_pm_profiles) / sizeof(char *))
629 		pm = "Reserved";
630 	else
631 		pm = acpi_pm_profiles[fadt->PreferredProfile];
632 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->PreferredProfile);
633 	printf("\tSCI_INT=%d\n", fadt->SciInterrupt);
634 	printf("\tSMI_CMD=0x%x, ", fadt->SmiCommand);
635 	printf("ACPI_ENABLE=0x%x, ", fadt->AcpiEnable);
636 	printf("ACPI_DISABLE=0x%x, ", fadt->AcpiDisable);
637 	printf("S4BIOS_REQ=0x%x\n", fadt->S4BiosRequest);
638 	printf("\tPSTATE_CNT=0x%x\n", fadt->PstateControl);
639 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
640 	       fadt->Pm1aEventBlock,
641 	       fadt->Pm1aEventBlock + fadt->Pm1EventLength - 1);
642 	if (fadt->Pm1bEventBlock != 0)
643 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
644 		       fadt->Pm1bEventBlock,
645 		       fadt->Pm1bEventBlock + fadt->Pm1EventLength - 1);
646 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
647 	       fadt->Pm1aControlBlock,
648 	       fadt->Pm1aControlBlock + fadt->Pm1ControlLength - 1);
649 	if (fadt->Pm1bControlBlock != 0)
650 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
651 		       fadt->Pm1bControlBlock,
652 		       fadt->Pm1bControlBlock + fadt->Pm1ControlLength - 1);
653 	if (fadt->Pm2ControlBlock != 0)
654 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
655 		       fadt->Pm2ControlBlock,
656 		       fadt->Pm2ControlBlock + fadt->Pm2ControlLength - 1);
657 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
658 	       fadt->PmTimerBlock,
659 	       fadt->PmTimerBlock + fadt->PmTimerLength - 1);
660 	if (fadt->Gpe0Block != 0)
661 		printf("\tGPE0_BLK=0x%x-0x%x\n",
662 		       fadt->Gpe0Block,
663 		       fadt->Gpe0Block + fadt->Gpe0BlockLength - 1);
664 	if (fadt->Gpe1Block != 0)
665 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
666 		       fadt->Gpe1Block,
667 		       fadt->Gpe1Block + fadt->Gpe1BlockLength - 1,
668 		       fadt->Gpe1Base);
669 	if (fadt->CstControl != 0)
670 		printf("\tCST_CNT=0x%x\n", fadt->CstControl);
671 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
672 	       fadt->C2Latency, fadt->C3Latency);
673 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
674 	       fadt->FlushSize, fadt->FlushStride);
675 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
676 	       fadt->DutyOffset, fadt->DutyWidth);
677 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
678 	       fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
679 
680 #define PRINTFLAG(var, flag) do {			\
681 	if ((var) & ACPI_FADT_## flag) {		\
682 		printf("%c%s", sep, #flag); sep = ',';	\
683 	}						\
684 } while (0)
685 
686 	printf("\tIAPC_BOOT_ARCH=");
687 	sep = '{';
688 	PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
689 	PRINTFLAG(fadt->BootFlags, 8042);
690 	PRINTFLAG(fadt->BootFlags, NO_VGA);
691 	PRINTFLAG(fadt->BootFlags, NO_MSI);
692 	PRINTFLAG(fadt->BootFlags, NO_ASPM);
693 	if (fadt->BootFlags != 0)
694 		printf("}");
695 	printf("\n");
696 
697 	printf("\tFlags=");
698 	sep = '{';
699 	PRINTFLAG(fadt->Flags, WBINVD);
700 	PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
701 	PRINTFLAG(fadt->Flags, C1_SUPPORTED);
702 	PRINTFLAG(fadt->Flags, C2_MP_SUPPORTED);
703 	PRINTFLAG(fadt->Flags, POWER_BUTTON);
704 	PRINTFLAG(fadt->Flags, SLEEP_BUTTON);
705 	PRINTFLAG(fadt->Flags, FIXED_RTC);
706 	PRINTFLAG(fadt->Flags, S4_RTC_WAKE);
707 	PRINTFLAG(fadt->Flags, 32BIT_TIMER);
708 	PRINTFLAG(fadt->Flags, DOCKING_SUPPORTED);
709 	PRINTFLAG(fadt->Flags, RESET_REGISTER);
710 	PRINTFLAG(fadt->Flags, SEALED_CASE);
711 	PRINTFLAG(fadt->Flags, HEADLESS);
712 	PRINTFLAG(fadt->Flags, SLEEP_TYPE);
713 	PRINTFLAG(fadt->Flags, PCI_EXPRESS_WAKE);
714 	PRINTFLAG(fadt->Flags, PLATFORM_CLOCK);
715 	PRINTFLAG(fadt->Flags, S4_RTC_VALID);
716 	PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
717 	PRINTFLAG(fadt->Flags, APIC_CLUSTER);
718 	PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
719 	if (fadt->Flags != 0)
720 		printf("}\n");
721 
722 #undef PRINTFLAG
723 
724 	if (fadt->Flags & ACPI_FADT_RESET_REGISTER) {
725 		printf("\tRESET_REG=");
726 		acpi_print_gas(&fadt->ResetRegister);
727 		printf(", RESET_VALUE=%#x\n", fadt->ResetValue);
728 	}
729 	if (acpi_get_fadt_revision(fadt) > 1) {
730 		printf("\tX_FACS=0x%08lx, ", (u_long)fadt->XFacs);
731 		printf("X_DSDT=0x%08lx\n", (u_long)fadt->XDsdt);
732 		printf("\tX_PM1a_EVT_BLK=");
733 		acpi_print_gas(&fadt->XPm1aEventBlock);
734 		if (fadt->XPm1bEventBlock.Address != 0) {
735 			printf("\n\tX_PM1b_EVT_BLK=");
736 			acpi_print_gas(&fadt->XPm1bEventBlock);
737 		}
738 		printf("\n\tX_PM1a_CNT_BLK=");
739 		acpi_print_gas(&fadt->XPm1aControlBlock);
740 		if (fadt->XPm1bControlBlock.Address != 0) {
741 			printf("\n\tX_PM1b_CNT_BLK=");
742 			acpi_print_gas(&fadt->XPm1bControlBlock);
743 		}
744 		if (fadt->XPm2ControlBlock.Address != 0) {
745 			printf("\n\tX_PM2_CNT_BLK=");
746 			acpi_print_gas(&fadt->XPm2ControlBlock);
747 		}
748 		printf("\n\tX_PM_TMR_BLK=");
749 		acpi_print_gas(&fadt->XPmTimerBlock);
750 		if (fadt->XGpe0Block.Address != 0) {
751 			printf("\n\tX_GPE0_BLK=");
752 			acpi_print_gas(&fadt->XGpe0Block);
753 		}
754 		if (fadt->XGpe1Block.Address != 0) {
755 			printf("\n\tX_GPE1_BLK=");
756 			acpi_print_gas(&fadt->XGpe1Block);
757 		}
758 		printf("\n");
759 	}
760 
761 	printf(END_COMMENT);
762 }
763 
764 static void
765 acpi_print_facs(ACPI_TABLE_FACS *facs)
766 {
767 	printf(BEGIN_COMMENT);
768 	printf("  FACS:\tLength=%u, ", facs->Length);
769 	printf("HwSig=0x%08x, ", facs->HardwareSignature);
770 	printf("Firm_Wake_Vec=0x%08x\n", facs->FirmwareWakingVector);
771 
772 	printf("\tGlobal_Lock=");
773 	if (facs->GlobalLock != 0) {
774 		if (facs->GlobalLock & ACPI_GLOCK_PENDING)
775 			printf("PENDING,");
776 		if (facs->GlobalLock & ACPI_GLOCK_OWNED)
777 			printf("OWNED");
778 	}
779 	printf("\n");
780 
781 	printf("\tFlags=");
782 	if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
783 		printf("S4BIOS");
784 	printf("\n");
785 
786 	if (facs->XFirmwareWakingVector != 0) {
787 		printf("\tX_Firm_Wake_Vec=%08lx\n",
788 		       (u_long)facs->XFirmwareWakingVector);
789 	}
790 	printf("\tVersion=%u\n", facs->Version);
791 
792 	printf(END_COMMENT);
793 }
794 
795 static void
796 acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp)
797 {
798 	printf(BEGIN_COMMENT);
799 	acpi_print_sdt(dsdp);
800 	printf(END_COMMENT);
801 }
802 
803 int
804 acpi_checksum(void *p, size_t length)
805 {
806 	uint8_t *bp;
807 	uint8_t sum;
808 
809 	bp = p;
810 	sum = 0;
811 	while (length--)
812 		sum += *bp++;
813 
814 	return (sum);
815 }
816 
817 static ACPI_TABLE_HEADER *
818 acpi_map_sdt(vm_offset_t pa)
819 {
820 	ACPI_TABLE_HEADER *sp;
821 
822 	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
823 	sp = acpi_map_physical(pa, sp->Length);
824 	return (sp);
825 }
826 
827 static void
828 acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp)
829 {
830 	printf(BEGIN_COMMENT);
831 	printf("  RSD PTR: OEM=");
832 	acpi_print_string(rp->OemId, ACPI_OEM_ID_SIZE);
833 	printf(", ACPI_Rev=%s (%d)\n", rp->Revision < 2 ? "1.0x" : "2.0x",
834 	       rp->Revision);
835 	if (rp->Revision < 2) {
836 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->RsdtPhysicalAddress,
837 		    rp->Checksum);
838 	} else {
839 		printf("\tXSDT=0x%08lx, length=%u, cksum=%u\n",
840 		    (u_long)rp->XsdtPhysicalAddress, rp->Length,
841 		    rp->ExtendedChecksum);
842 	}
843 	printf(END_COMMENT);
844 }
845 
846 static void
847 acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
848 {
849 	ACPI_TABLE_HEADER *sdp;
850 	ACPI_TABLE_RSDT *rsdt;
851 	ACPI_TABLE_XSDT *xsdt;
852 	vm_offset_t addr;
853 	int entries, i;
854 
855 	acpi_print_rsdt(rsdp);
856 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
857 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
858 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
859 	for (i = 0; i < entries; i++) {
860 		switch (addr_size) {
861 		case 4:
862 			addr = le32toh(rsdt->TableOffsetEntry[i]);
863 			break;
864 		case 8:
865 			addr = le64toh(xsdt->TableOffsetEntry[i]);
866 			break;
867 		default:
868 			assert((addr = 0));
869 		}
870 
871 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
872 		if (acpi_checksum(sdp, sdp->Length)) {
873 			warnx("RSDT entry %d (sig %.4s) is corrupt", i,
874 			    sdp->Signature);
875 			continue;
876 		}
877 		if (!memcmp(sdp->Signature, ACPI_SIG_FADT, 4))
878 			acpi_handle_fadt(sdp);
879 		else if (!memcmp(sdp->Signature, ACPI_SIG_MADT, 4))
880 			acpi_handle_madt(sdp);
881 		else if (!memcmp(sdp->Signature, ACPI_SIG_HPET, 4))
882 			acpi_handle_hpet(sdp);
883 		else if (!memcmp(sdp->Signature, ACPI_SIG_ECDT, 4))
884 			acpi_handle_ecdt(sdp);
885 		else if (!memcmp(sdp->Signature, ACPI_SIG_MCFG, 4))
886 			acpi_handle_mcfg(sdp);
887 		else if (!memcmp(sdp->Signature, ACPI_SIG_SRAT, 4))
888 			acpi_handle_srat(sdp);
889 		else {
890 			printf(BEGIN_COMMENT);
891 			acpi_print_sdt(sdp);
892 			printf(END_COMMENT);
893 		}
894 	}
895 }
896 
897 ACPI_TABLE_HEADER *
898 sdt_load_devmem(void)
899 {
900 	ACPI_TABLE_RSDP *rp;
901 	ACPI_TABLE_HEADER *rsdp;
902 
903 	rp = acpi_find_rsd_ptr();
904 	if (!rp)
905 		errx(1, "Can't find ACPI information");
906 
907 	if (tflag)
908 		acpi_print_rsd_ptr(rp);
909 	if (rp->Revision < 2) {
910 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->RsdtPhysicalAddress);
911 		if (memcmp(rsdp->Signature, "RSDT", 4) != 0 ||
912 		    acpi_checksum(rsdp, rsdp->Length) != 0)
913 			errx(1, "RSDT is corrupted");
914 		addr_size = sizeof(uint32_t);
915 	} else {
916 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->XsdtPhysicalAddress);
917 		if (memcmp(rsdp->Signature, "XSDT", 4) != 0 ||
918 		    acpi_checksum(rsdp, rsdp->Length) != 0)
919 			errx(1, "XSDT is corrupted");
920 		addr_size = sizeof(uint64_t);
921 	}
922 	return (rsdp);
923 }
924 
925 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
926 static int
927 write_dsdt(int fd, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdt)
928 {
929 	ACPI_TABLE_HEADER sdt;
930 	ACPI_TABLE_HEADER *ssdt;
931 	uint8_t sum;
932 
933 	/* Create a new checksum to account for the DSDT and any SSDTs. */
934 	sdt = *dsdt;
935 	if (rsdt != NULL) {
936 		sdt.Checksum = 0;
937 		sum = acpi_checksum(dsdt + 1, dsdt->Length -
938 		    sizeof(ACPI_TABLE_HEADER));
939 		ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, NULL);
940 		while (ssdt != NULL) {
941 			sdt.Length += ssdt->Length - sizeof(ACPI_TABLE_HEADER);
942 			sum += acpi_checksum(ssdt + 1,
943 			    ssdt->Length - sizeof(ACPI_TABLE_HEADER));
944 			ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, ssdt);
945 		}
946 		sum += acpi_checksum(&sdt, sizeof(ACPI_TABLE_HEADER));
947 		sdt.Checksum -= sum;
948 	}
949 
950 	/* Write out the DSDT header and body. */
951 	write(fd, &sdt, sizeof(ACPI_TABLE_HEADER));
952 	write(fd, dsdt + 1, dsdt->Length - sizeof(ACPI_TABLE_HEADER));
953 
954 	/* Write out any SSDTs (if present.) */
955 	if (rsdt != NULL) {
956 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
957 		while (ssdt != NULL) {
958 			write(fd, ssdt + 1, ssdt->Length -
959 			    sizeof(ACPI_TABLE_HEADER));
960 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
961 		}
962 	}
963 	return (0);
964 }
965 
966 void
967 dsdt_save_file(char *outfile, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
968 {
969 	int	fd;
970 	mode_t	mode;
971 
972 	assert(outfile != NULL);
973 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
974 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
975 	if (fd == -1) {
976 		perror("dsdt_save_file");
977 		return;
978 	}
979 	write_dsdt(fd, rsdt, dsdp);
980 	close(fd);
981 }
982 
983 void
984 aml_disassemble(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
985 {
986 	char buf[PATH_MAX], tmpstr[PATH_MAX];
987 	const char *tmpdir;
988 	char *tmpext;
989 	FILE *fp;
990 	size_t len;
991 	int fd;
992 
993 	tmpdir = getenv("TMPDIR");
994 	if (tmpdir == NULL)
995 		tmpdir = _PATH_TMP;
996 	strncpy(tmpstr, tmpdir, sizeof(tmpstr));
997 	strncat(tmpstr, "/acpidump.", sizeof(tmpstr) - strlen(tmpdir));
998 	if (realpath(tmpstr, buf) == NULL) {
999 		perror("realpath tmp file");
1000 		return;
1001 	}
1002 	strncpy(tmpstr, buf, sizeof(tmpstr));
1003 	len = strlen(buf);
1004 	tmpext = tmpstr + len;
1005 	strncpy(tmpext, "XXXXXX", sizeof(tmpstr) - len);
1006 	fd = mkstemp(tmpstr);
1007 	if (fd < 0) {
1008 		perror("iasl tmp file");
1009 		return;
1010 	}
1011 	write_dsdt(fd, rsdt, dsdp);
1012 	close(fd);
1013 
1014 	/* Run iasl -d on the temp file */
1015 	if (fork() == 0) {
1016 		close(STDOUT_FILENO);
1017 		if (vflag == 0)
1018 			close(STDERR_FILENO);
1019 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
1020 		err(1, "exec");
1021 	}
1022 
1023 	wait(NULL);
1024 	unlink(tmpstr);
1025 
1026 	/* Dump iasl's output to stdout */
1027 	strncpy(tmpext, "dsl", sizeof(tmpstr) - len);
1028 	fp = fopen(tmpstr, "r");
1029 	unlink(tmpstr);
1030 	if (fp == NULL) {
1031 		perror("iasl tmp file (read)");
1032 		return;
1033 	}
1034 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
1035 		fwrite(buf, 1, len, stdout);
1036 	fclose(fp);
1037 }
1038 
1039 void
1040 sdt_print_all(ACPI_TABLE_HEADER *rsdp)
1041 {
1042 	acpi_handle_rsdt(rsdp);
1043 }
1044 
1045 /* Fetch a table matching the given signature via the RSDT. */
1046 ACPI_TABLE_HEADER *
1047 sdt_from_rsdt(ACPI_TABLE_HEADER *rsdp, const char *sig, ACPI_TABLE_HEADER *last)
1048 {
1049 	ACPI_TABLE_HEADER *sdt;
1050 	ACPI_TABLE_RSDT *rsdt;
1051 	ACPI_TABLE_XSDT *xsdt;
1052 	vm_offset_t addr;
1053 	int entries, i;
1054 
1055 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
1056 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
1057 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
1058 	for (i = 0; i < entries; i++) {
1059 		switch (addr_size) {
1060 		case 4:
1061 			addr = le32toh(rsdt->TableOffsetEntry[i]);
1062 			break;
1063 		case 8:
1064 			addr = le64toh(xsdt->TableOffsetEntry[i]);
1065 			break;
1066 		default:
1067 			assert((addr = 0));
1068 		}
1069 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
1070 		if (last != NULL) {
1071 			if (sdt == last)
1072 				last = NULL;
1073 			continue;
1074 		}
1075 		if (memcmp(sdt->Signature, sig, strlen(sig)))
1076 			continue;
1077 		if (acpi_checksum(sdt, sdt->Length))
1078 			errx(1, "RSDT entry %d is corrupt", i);
1079 		return (sdt);
1080 	}
1081 
1082 	return (NULL);
1083 }
1084 
1085 ACPI_TABLE_HEADER *
1086 dsdt_from_fadt(ACPI_TABLE_FADT *fadt)
1087 {
1088 	ACPI_TABLE_HEADER	*sdt;
1089 
1090 	/* Use the DSDT address if it is version 1, otherwise use XDSDT. */
1091 	if (acpi_get_fadt_revision(fadt) == 1)
1092 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
1093 	else
1094 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
1095 	if (acpi_checksum(sdt, sdt->Length))
1096 		errx(1, "DSDT is corrupt\n");
1097 	return (sdt);
1098 }
1099