xref: /freebsd/usr.sbin/acpi/acpidump/acpi.c (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Doug Rabson
5  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
6  * Copyright (c) 2020 Alexander Motin <mav@FreeBSD.org>
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *	$FreeBSD$
31  */
32 
33 #include <sys/param.h>
34 #include <sys/endian.h>
35 #include <sys/stat.h>
36 #include <sys/wait.h>
37 #include <assert.h>
38 #include <err.h>
39 #include <fcntl.h>
40 #include <paths.h>
41 #include <stdio.h>
42 #include <stdint.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 #include <uuid.h>
47 
48 #include "acpidump.h"
49 
50 #define BEGIN_COMMENT	"/*\n"
51 #define END_COMMENT	" */\n"
52 
53 static void	acpi_print_string(char *s, size_t length);
54 static void	acpi_print_gas(ACPI_GENERIC_ADDRESS *gas);
55 static int	acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt);
56 static void	acpi_handle_fadt(ACPI_TABLE_HEADER *fadt);
57 static void	acpi_print_cpu(u_char cpu_id);
58 static void	acpi_print_cpu_uid(uint32_t uid, char *uid_string);
59 static void	acpi_print_local_apic(uint32_t apic_id, uint32_t flags);
60 static void	acpi_print_io_apic(uint32_t apic_id, uint32_t int_base,
61 		    uint64_t apic_addr);
62 static void	acpi_print_mps_flags(uint16_t flags);
63 static void	acpi_print_intr(uint32_t intr, uint16_t mps_flags);
64 static void	acpi_print_local_nmi(u_int lint, uint16_t mps_flags);
65 static void	acpi_print_madt(ACPI_SUBTABLE_HEADER *mp);
66 static void	acpi_handle_madt(ACPI_TABLE_HEADER *sdp);
67 static void	acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp);
68 static void	acpi_handle_hpet(ACPI_TABLE_HEADER *sdp);
69 static void	acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp);
70 static void	acpi_handle_slit(ACPI_TABLE_HEADER *sdp);
71 static void	acpi_handle_wddt(ACPI_TABLE_HEADER *sdp);
72 static void	acpi_handle_lpit(ACPI_TABLE_HEADER *sdp);
73 static void	acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
74 		    uint32_t flags);
75 static void	acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp);
76 static void	acpi_print_srat(ACPI_SUBTABLE_HEADER *srat);
77 static void	acpi_handle_srat(ACPI_TABLE_HEADER *sdp);
78 static void	acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp);
79 static void	acpi_print_nfit(ACPI_NFIT_HEADER *nfit);
80 static void	acpi_handle_nfit(ACPI_TABLE_HEADER *sdp);
81 static void	acpi_print_sdt(ACPI_TABLE_HEADER *sdp);
82 static void	acpi_print_fadt(ACPI_TABLE_HEADER *sdp);
83 static void	acpi_print_facs(ACPI_TABLE_FACS *facs);
84 static void	acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp);
85 static ACPI_TABLE_HEADER *acpi_map_sdt(vm_offset_t pa);
86 static void	acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp);
87 static void	acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp);
88 static void	acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
89 		    void (*action)(ACPI_SUBTABLE_HEADER *));
90 static void	acpi_walk_nfit(ACPI_TABLE_HEADER *table, void *first,
91 		    void (*action)(ACPI_NFIT_HEADER *));
92 
93 /* Size of an address. 32-bit for ACPI 1.0, 64-bit for ACPI 2.0 and up. */
94 static int addr_size;
95 
96 /* Strings used in the TCPA table */
97 static const char *tcpa_event_type_strings[] = {
98 	"PREBOOT Certificate",
99 	"POST Code",
100 	"Unused",
101 	"No Action",
102 	"Separator",
103 	"Action",
104 	"Event Tag",
105 	"S-CRTM Contents",
106 	"S-CRTM Version",
107 	"CPU Microcode",
108 	"Platform Config Flags",
109 	"Table of Devices",
110 	"Compact Hash",
111 	"IPL",
112 	"IPL Partition Data",
113 	"Non-Host Code",
114 	"Non-Host Config",
115 	"Non-Host Info"
116 };
117 
118 static const char *TCPA_pcclient_strings[] = {
119 	"<undefined>",
120 	"SMBIOS",
121 	"BIS Certificate",
122 	"POST BIOS ROM Strings",
123 	"ESCD",
124 	"CMOS",
125 	"NVRAM",
126 	"Option ROM Execute",
127 	"Option ROM Configurateion",
128 	"<undefined>",
129 	"Option ROM Microcode Update ",
130 	"S-CRTM Version String",
131 	"S-CRTM Contents",
132 	"POST Contents",
133 	"Table of Devices",
134 };
135 
136 #define	PRINTFLAG_END()		printflag_end()
137 
138 static char pf_sep = '{';
139 
140 static void
141 printflag_end(void)
142 {
143 
144 	if (pf_sep != '{') {
145 		printf("}");
146 		pf_sep = '{';
147 	}
148 	printf("\n");
149 }
150 
151 static void
152 printflag(uint64_t var, uint64_t mask, const char *name)
153 {
154 
155 	if (var & mask) {
156 		printf("%c%s", pf_sep, name);
157 		pf_sep = ',';
158 	}
159 }
160 
161 static void
162 acpi_print_string(char *s, size_t length)
163 {
164 	int	c;
165 
166 	/* Trim trailing spaces and NULLs */
167 	while (length > 0 && (s[length - 1] == ' ' || s[length - 1] == '\0'))
168 		length--;
169 
170 	while (length--) {
171 		c = *s++;
172 		putchar(c);
173 	}
174 }
175 
176 static void
177 acpi_print_gas(ACPI_GENERIC_ADDRESS *gas)
178 {
179 	switch(gas->SpaceId) {
180 	case ACPI_GAS_MEMORY:
181 		printf("0x%016jx:%u[%u] (Memory)", (uintmax_t)gas->Address,
182 		    gas->BitOffset, gas->BitWidth);
183 		break;
184 	case ACPI_GAS_IO:
185 		printf("0x%02jx:%u[%u] (IO)", (uintmax_t)gas->Address,
186 		    gas->BitOffset, gas->BitWidth);
187 		break;
188 	case ACPI_GAS_PCI:
189 		printf("%x:%x+0x%x:%u[%u] (PCI)", (uint16_t)(gas->Address >> 32),
190 		       (uint16_t)((gas->Address >> 16) & 0xffff),
191 		       (uint16_t)gas->Address, gas->BitOffset, gas->BitWidth);
192 		break;
193 	/* XXX How to handle these below? */
194 	case ACPI_GAS_EMBEDDED:
195 		printf("0x%x:%u[%u] (EC)", (uint16_t)gas->Address,
196 		       gas->BitOffset, gas->BitWidth);
197 		break;
198 	case ACPI_GAS_SMBUS:
199 		printf("0x%x:%u[%u] (SMBus)", (uint16_t)gas->Address,
200 		       gas->BitOffset, gas->BitWidth);
201 		break;
202 	case ACPI_GAS_CMOS:
203 	case ACPI_GAS_PCIBAR:
204 	case ACPI_GAS_DATATABLE:
205 	case ACPI_GAS_FIXED:
206 	default:
207 		printf("0x%016jx (?)", (uintmax_t)gas->Address);
208 		break;
209 	}
210 }
211 
212 /* The FADT revision indicates whether we use the DSDT or X_DSDT addresses. */
213 static int
214 acpi_get_fadt_revision(ACPI_TABLE_FADT *fadt)
215 {
216 	int fadt_revision;
217 
218 	/* Set the FADT revision separately from the RSDP version. */
219 	if (addr_size == 8) {
220 		fadt_revision = 2;
221 
222 #if defined(__i386__)
223 		/*
224 		 * A few systems (e.g., IBM T23) have an RSDP that claims
225 		 * revision 2 but the 64 bit addresses are invalid.  If
226 		 * revision 2 and the 32 bit address is non-zero but the
227 		 * 32 and 64 bit versions don't match, prefer the 32 bit
228 		 * version for all subsequent tables.
229 		 *
230 		 * The only known ACPI systems this affects are early
231 		 * implementations on 32-bit x86. Because of this limit the
232 		 * workaround to i386.
233 		 */
234 		if (fadt->Facs != 0 &&
235 		    (fadt->XFacs & 0xffffffff) != fadt->Facs)
236 			fadt_revision = 1;
237 #endif
238 	} else
239 		fadt_revision = 1;
240 	return (fadt_revision);
241 }
242 
243 static void
244 acpi_handle_fadt(ACPI_TABLE_HEADER *sdp)
245 {
246 	ACPI_TABLE_HEADER *dsdp;
247 	ACPI_TABLE_FACS	*facs;
248 	ACPI_TABLE_FADT *fadt;
249 	vm_offset_t	addr;
250 	int		fadt_revision;
251 
252 	fadt = (ACPI_TABLE_FADT *)sdp;
253 	acpi_print_fadt(sdp);
254 
255 	fadt_revision = acpi_get_fadt_revision(fadt);
256 	if (fadt_revision == 1)
257 		addr = fadt->Facs;
258 	else
259 		addr = fadt->XFacs;
260 	if (addr != 0) {
261 		facs = (ACPI_TABLE_FACS *)acpi_map_sdt(addr);
262 
263 		if (memcmp(facs->Signature, ACPI_SIG_FACS, 4) != 0 ||
264 		    facs->Length < 64)
265 			errx(1, "FACS is corrupt");
266 		acpi_print_facs(facs);
267 	}
268 
269 	if (fadt_revision == 1)
270 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
271 	else
272 		dsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
273 	if (acpi_checksum(dsdp, dsdp->Length))
274 		errx(1, "DSDT is corrupt");
275 	acpi_print_dsdt(dsdp);
276 }
277 
278 static void
279 acpi_walk_subtables(ACPI_TABLE_HEADER *table, void *first,
280     void (*action)(ACPI_SUBTABLE_HEADER *))
281 {
282 	ACPI_SUBTABLE_HEADER *subtable;
283 	char *end;
284 
285 	subtable = first;
286 	end = (char *)table + table->Length;
287 	while ((char *)subtable < end) {
288 		printf("\n");
289 		if (subtable->Length < sizeof(ACPI_SUBTABLE_HEADER)) {
290 			warnx("invalid subtable length %u", subtable->Length);
291 			return;
292 		}
293 		action(subtable);
294 		subtable = (ACPI_SUBTABLE_HEADER *)((char *)subtable +
295 		    subtable->Length);
296 	}
297 }
298 
299 static void
300 acpi_walk_nfit(ACPI_TABLE_HEADER *table, void *first,
301     void (*action)(ACPI_NFIT_HEADER *))
302 {
303 	ACPI_NFIT_HEADER *subtable;
304 	char *end;
305 
306 	subtable = first;
307 	end = (char *)table + table->Length;
308 	while ((char *)subtable < end) {
309 		printf("\n");
310 		if (subtable->Length < sizeof(ACPI_NFIT_HEADER)) {
311 			warnx("invalid subtable length %u", subtable->Length);
312 			return;
313 		}
314 		action(subtable);
315 		subtable = (ACPI_NFIT_HEADER *)((char *)subtable +
316 		    subtable->Length);
317 	}
318 }
319 
320 static void
321 acpi_print_cpu(u_char cpu_id)
322 {
323 
324 	printf("\tACPI CPU=");
325 	if (cpu_id == 0xff)
326 		printf("ALL\n");
327 	else
328 		printf("%d\n", (u_int)cpu_id);
329 }
330 
331 static void
332 acpi_print_cpu_uid(uint32_t uid, char *uid_string)
333 {
334 
335 	printf("\tUID=%d", uid);
336 	if (uid_string != NULL)
337 		printf(" (%s)", uid_string);
338 	printf("\n");
339 }
340 
341 static void
342 acpi_print_local_apic(uint32_t apic_id, uint32_t flags)
343 {
344 
345 	printf("\tFlags={");
346 	if (flags & ACPI_MADT_ENABLED)
347 		printf("ENABLED");
348 	else
349 		printf("DISABLED");
350 	printf("}\n");
351 	printf("\tAPIC ID=%d\n", apic_id);
352 }
353 
354 static void
355 acpi_print_io_apic(uint32_t apic_id, uint32_t int_base, uint64_t apic_addr)
356 {
357 
358 	printf("\tAPIC ID=%d\n", apic_id);
359 	printf("\tINT BASE=%d\n", int_base);
360 	printf("\tADDR=0x%016jx\n", (uintmax_t)apic_addr);
361 }
362 
363 static void
364 acpi_print_mps_flags(uint16_t flags)
365 {
366 
367 	printf("\tFlags={Polarity=");
368 	switch (flags & ACPI_MADT_POLARITY_MASK) {
369 	case ACPI_MADT_POLARITY_CONFORMS:
370 		printf("conforming");
371 		break;
372 	case ACPI_MADT_POLARITY_ACTIVE_HIGH:
373 		printf("active-hi");
374 		break;
375 	case ACPI_MADT_POLARITY_ACTIVE_LOW:
376 		printf("active-lo");
377 		break;
378 	default:
379 		printf("0x%x", flags & ACPI_MADT_POLARITY_MASK);
380 		break;
381 	}
382 	printf(", Trigger=");
383 	switch (flags & ACPI_MADT_TRIGGER_MASK) {
384 	case ACPI_MADT_TRIGGER_CONFORMS:
385 		printf("conforming");
386 		break;
387 	case ACPI_MADT_TRIGGER_EDGE:
388 		printf("edge");
389 		break;
390 	case ACPI_MADT_TRIGGER_LEVEL:
391 		printf("level");
392 		break;
393 	default:
394 		printf("0x%x", (flags & ACPI_MADT_TRIGGER_MASK) >> 2);
395 	}
396 	printf("}\n");
397 }
398 
399 static void
400 acpi_print_gicc_flags(uint32_t flags)
401 {
402 
403 	printf("\tFlags={Performance intr=");
404 	if (flags & ACPI_MADT_PERFORMANCE_IRQ_MODE)
405 		printf("edge");
406 	else
407 		printf("level");
408 	printf(", VGIC intr=");
409 	if (flags & ACPI_MADT_VGIC_IRQ_MODE)
410 		printf("edge");
411 	else
412 		printf("level");
413 	printf("}\n");
414 }
415 
416 static void
417 acpi_print_intr(uint32_t intr, uint16_t mps_flags)
418 {
419 
420 	printf("\tINTR=%d\n", intr);
421 	acpi_print_mps_flags(mps_flags);
422 }
423 
424 static void
425 acpi_print_local_nmi(u_int lint, uint16_t mps_flags)
426 {
427 
428 	printf("\tLINT Pin=%d\n", lint);
429 	acpi_print_mps_flags(mps_flags);
430 }
431 
432 static const char *apic_types[] = {
433     [ACPI_MADT_TYPE_LOCAL_APIC] = "Local APIC",
434     [ACPI_MADT_TYPE_IO_APIC] = "IO APIC",
435     [ACPI_MADT_TYPE_INTERRUPT_OVERRIDE] = "INT Override",
436     [ACPI_MADT_TYPE_NMI_SOURCE] = "NMI",
437     [ACPI_MADT_TYPE_LOCAL_APIC_NMI] = "Local APIC NMI",
438     [ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE] = "Local APIC Override",
439     [ACPI_MADT_TYPE_IO_SAPIC] = "IO SAPIC",
440     [ACPI_MADT_TYPE_LOCAL_SAPIC] = "Local SAPIC",
441     [ACPI_MADT_TYPE_INTERRUPT_SOURCE] = "Platform Interrupt",
442     [ACPI_MADT_TYPE_LOCAL_X2APIC] = "Local X2APIC",
443     [ACPI_MADT_TYPE_LOCAL_X2APIC_NMI] = "Local X2APIC NMI",
444     [ACPI_MADT_TYPE_GENERIC_INTERRUPT] = "GIC CPU Interface Structure",
445     [ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR] = "GIC Distributor Structure",
446     [ACPI_MADT_TYPE_GENERIC_MSI_FRAME] = "GICv2m MSI Frame",
447     [ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR] = "GIC Redistributor Structure",
448     [ACPI_MADT_TYPE_GENERIC_TRANSLATOR] = "GIC ITS Structure"
449 };
450 
451 static const char *platform_int_types[] = { "0 (unknown)", "PMI", "INIT",
452 					    "Corrected Platform Error" };
453 
454 static void
455 acpi_print_madt(ACPI_SUBTABLE_HEADER *mp)
456 {
457 	ACPI_MADT_LOCAL_APIC *lapic;
458 	ACPI_MADT_IO_APIC *ioapic;
459 	ACPI_MADT_INTERRUPT_OVERRIDE *over;
460 	ACPI_MADT_NMI_SOURCE *nmi;
461 	ACPI_MADT_LOCAL_APIC_NMI *lapic_nmi;
462 	ACPI_MADT_LOCAL_APIC_OVERRIDE *lapic_over;
463 	ACPI_MADT_IO_SAPIC *iosapic;
464 	ACPI_MADT_LOCAL_SAPIC *lsapic;
465 	ACPI_MADT_INTERRUPT_SOURCE *isrc;
466 	ACPI_MADT_LOCAL_X2APIC *x2apic;
467 	ACPI_MADT_LOCAL_X2APIC_NMI *x2apic_nmi;
468 	ACPI_MADT_GENERIC_INTERRUPT *gicc;
469 	ACPI_MADT_GENERIC_DISTRIBUTOR *gicd;
470 	ACPI_MADT_GENERIC_REDISTRIBUTOR *gicr;
471 	ACPI_MADT_GENERIC_TRANSLATOR *gict;
472 
473 	if (mp->Type < nitems(apic_types))
474 		printf("\tType=%s\n", apic_types[mp->Type]);
475 	else
476 		printf("\tType=%d (unknown)\n", mp->Type);
477 	switch (mp->Type) {
478 	case ACPI_MADT_TYPE_LOCAL_APIC:
479 		lapic = (ACPI_MADT_LOCAL_APIC *)mp;
480 		acpi_print_cpu(lapic->ProcessorId);
481 		acpi_print_local_apic(lapic->Id, lapic->LapicFlags);
482 		break;
483 	case ACPI_MADT_TYPE_IO_APIC:
484 		ioapic = (ACPI_MADT_IO_APIC *)mp;
485 		acpi_print_io_apic(ioapic->Id, ioapic->GlobalIrqBase,
486 		    ioapic->Address);
487 		break;
488 	case ACPI_MADT_TYPE_INTERRUPT_OVERRIDE:
489 		over = (ACPI_MADT_INTERRUPT_OVERRIDE *)mp;
490 		printf("\tBUS=%d\n", (u_int)over->Bus);
491 		printf("\tIRQ=%d\n", (u_int)over->SourceIrq);
492 		acpi_print_intr(over->GlobalIrq, over->IntiFlags);
493 		break;
494 	case ACPI_MADT_TYPE_NMI_SOURCE:
495 		nmi = (ACPI_MADT_NMI_SOURCE *)mp;
496 		acpi_print_intr(nmi->GlobalIrq, nmi->IntiFlags);
497 		break;
498 	case ACPI_MADT_TYPE_LOCAL_APIC_NMI:
499 		lapic_nmi = (ACPI_MADT_LOCAL_APIC_NMI *)mp;
500 		acpi_print_cpu(lapic_nmi->ProcessorId);
501 		acpi_print_local_nmi(lapic_nmi->Lint, lapic_nmi->IntiFlags);
502 		break;
503 	case ACPI_MADT_TYPE_LOCAL_APIC_OVERRIDE:
504 		lapic_over = (ACPI_MADT_LOCAL_APIC_OVERRIDE *)mp;
505 		printf("\tLocal APIC ADDR=0x%016jx\n",
506 		    (uintmax_t)lapic_over->Address);
507 		break;
508 	case ACPI_MADT_TYPE_IO_SAPIC:
509 		iosapic = (ACPI_MADT_IO_SAPIC *)mp;
510 		acpi_print_io_apic(iosapic->Id, iosapic->GlobalIrqBase,
511 		    iosapic->Address);
512 		break;
513 	case ACPI_MADT_TYPE_LOCAL_SAPIC:
514 		lsapic = (ACPI_MADT_LOCAL_SAPIC *)mp;
515 		acpi_print_cpu(lsapic->ProcessorId);
516 		acpi_print_local_apic(lsapic->Id, lsapic->LapicFlags);
517 		printf("\tAPIC EID=%d\n", (u_int)lsapic->Eid);
518 		if (mp->Length > __offsetof(ACPI_MADT_LOCAL_SAPIC, Uid))
519 			acpi_print_cpu_uid(lsapic->Uid, lsapic->UidString);
520 		break;
521 	case ACPI_MADT_TYPE_INTERRUPT_SOURCE:
522 		isrc = (ACPI_MADT_INTERRUPT_SOURCE *)mp;
523 		if (isrc->Type < nitems(platform_int_types))
524 			printf("\tType=%s\n", platform_int_types[isrc->Type]);
525 		else
526 			printf("\tType=%d (unknown)\n", isrc->Type);
527 		printf("\tAPIC ID=%d\n", (u_int)isrc->Id);
528 		printf("\tAPIC EID=%d\n", (u_int)isrc->Eid);
529 		printf("\tSAPIC Vector=%d\n", (u_int)isrc->IoSapicVector);
530 		acpi_print_intr(isrc->GlobalIrq, isrc->IntiFlags);
531 		break;
532 	case ACPI_MADT_TYPE_LOCAL_X2APIC:
533 		x2apic = (ACPI_MADT_LOCAL_X2APIC *)mp;
534 		acpi_print_cpu_uid(x2apic->Uid, NULL);
535 		acpi_print_local_apic(x2apic->LocalApicId, x2apic->LapicFlags);
536 		break;
537 	case ACPI_MADT_TYPE_LOCAL_X2APIC_NMI:
538 		x2apic_nmi = (ACPI_MADT_LOCAL_X2APIC_NMI *)mp;
539 		acpi_print_cpu_uid(x2apic_nmi->Uid, NULL);
540 		acpi_print_local_nmi(x2apic_nmi->Lint, x2apic_nmi->IntiFlags);
541 		break;
542 	case ACPI_MADT_TYPE_GENERIC_INTERRUPT:
543 		gicc = (ACPI_MADT_GENERIC_INTERRUPT *)mp;
544 		acpi_print_cpu_uid(gicc->Uid, NULL);
545 		printf("\tCPU INTERFACE=%x\n", gicc->CpuInterfaceNumber);
546 		acpi_print_gicc_flags(gicc->Flags);
547 		printf("\tParking Protocol Version=%x\n", gicc->ParkingVersion);
548 		printf("\tPERF INTR=%d\n", gicc->PerformanceInterrupt);
549 		printf("\tParked ADDR=%016jx\n",
550 		    (uintmax_t)gicc->ParkedAddress);
551 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gicc->BaseAddress);
552 		printf("\tGICV=%016jx\n", (uintmax_t)gicc->GicvBaseAddress);
553 		printf("\tGICH=%016jx\n", (uintmax_t)gicc->GichBaseAddress);
554 		printf("\tVGIC INTR=%d\n", gicc->VgicInterrupt);
555 		printf("\tGICR ADDR=%016jx\n",
556 		    (uintmax_t)gicc->GicrBaseAddress);
557 		printf("\tMPIDR=%jx\n", (uintmax_t)gicc->ArmMpidr);
558 		printf("\tEfficency Class=%d\n", (u_int)gicc->EfficiencyClass);
559 		break;
560 	case ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR:
561 		gicd = (ACPI_MADT_GENERIC_DISTRIBUTOR *)mp;
562 		printf("\tGIC ID=%d\n", (u_int)gicd->GicId);
563 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gicd->BaseAddress);
564 		printf("\tVector Base=%d\n", gicd->GlobalIrqBase);
565 		printf("\tGIC VERSION=%d\n", (u_int)gicd->Version);
566 		break;
567 	case ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR:
568 		gicr = (ACPI_MADT_GENERIC_REDISTRIBUTOR *)mp;
569 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gicr->BaseAddress);
570 		printf("\tLength=%08x\n", gicr->Length);
571 		break;
572 	case ACPI_MADT_TYPE_GENERIC_TRANSLATOR:
573 		gict = (ACPI_MADT_GENERIC_TRANSLATOR *)mp;
574 		printf("\tGIC ITS ID=%d\n", gict->TranslationId);
575 		printf("\tBase ADDR=%016jx\n", (uintmax_t)gict->BaseAddress);
576 		break;
577 	}
578 }
579 
580 static void
581 acpi_handle_madt(ACPI_TABLE_HEADER *sdp)
582 {
583 	ACPI_TABLE_MADT *madt;
584 
585 	printf(BEGIN_COMMENT);
586 	acpi_print_sdt(sdp);
587 	madt = (ACPI_TABLE_MADT *)sdp;
588 	printf("\tLocal APIC ADDR=0x%08x\n", madt->Address);
589 	printf("\tFlags={");
590 	if (madt->Flags & ACPI_MADT_PCAT_COMPAT)
591 		printf("PC-AT");
592 	printf("}\n");
593 	acpi_walk_subtables(sdp, (madt + 1), acpi_print_madt);
594 	printf(END_COMMENT);
595 }
596 
597 static void
598 acpi_handle_bert(ACPI_TABLE_HEADER *sdp)
599 {
600 	ACPI_TABLE_BERT *bert;
601 
602 	printf(BEGIN_COMMENT);
603 	acpi_print_sdt(sdp);
604 	bert = (ACPI_TABLE_BERT *)sdp;
605 	printf("\tRegionLength=%d\n", bert->RegionLength);
606 	printf("\tAddress=0x%016jx\n", bert->Address);
607 	printf(END_COMMENT);
608 }
609 
610 static void
611 acpi_print_whea(ACPI_WHEA_HEADER *w)
612 {
613 
614 	printf("\n\tAction=%d\n", w->Action);
615 	printf("\tInstruction=%d\n", w->Instruction);
616 	printf("\tFlags=%02x\n", w->Flags);
617 	printf("\tRegisterRegion=");
618 	acpi_print_gas(&w->RegisterRegion);
619 	printf("\n\tValue=0x%016jx\n", w->Value);
620 	printf("\tMask=0x%016jx\n", w->Mask);
621 }
622 
623 static void
624 acpi_handle_einj(ACPI_TABLE_HEADER *sdp)
625 {
626 	ACPI_TABLE_EINJ *einj;
627 	ACPI_WHEA_HEADER *w;
628 	u_int i;
629 
630 	printf(BEGIN_COMMENT);
631 	acpi_print_sdt(sdp);
632 	einj = (ACPI_TABLE_EINJ *)sdp;
633 	printf("\tHeaderLength=%d\n", einj->HeaderLength);
634 	printf("\tFlags=0x%02x\n", einj->Flags);
635 	printf("\tEntries=%d\n", einj->Entries);
636 	w = (ACPI_WHEA_HEADER *)(einj + 1);
637 	for (i = 0; i < MIN(einj->Entries, (sdp->Length -
638 	    sizeof(ACPI_TABLE_EINJ)) / sizeof(ACPI_WHEA_HEADER)); i++)
639 		acpi_print_whea(w + i);
640 	printf(END_COMMENT);
641 }
642 
643 static void
644 acpi_handle_erst(ACPI_TABLE_HEADER *sdp)
645 {
646 	ACPI_TABLE_ERST *erst;
647 	ACPI_WHEA_HEADER *w;
648 	u_int i;
649 
650 	printf(BEGIN_COMMENT);
651 	acpi_print_sdt(sdp);
652 	erst = (ACPI_TABLE_ERST *)sdp;
653 	printf("\tHeaderLength=%d\n", erst->HeaderLength);
654 	printf("\tEntries=%d\n", erst->Entries);
655 	w = (ACPI_WHEA_HEADER *)(erst + 1);
656 	for (i = 0; i < MIN(erst->Entries, (sdp->Length -
657 	    sizeof(ACPI_TABLE_ERST)) / sizeof(ACPI_WHEA_HEADER)); i++)
658 		acpi_print_whea(w + i);
659 	printf(END_COMMENT);
660 }
661 
662 static void
663 acpi_print_hest_bank(ACPI_HEST_IA_ERROR_BANK *b)
664 {
665 
666 	printf("\tBank:\n");
667 	printf("\t\tBankNumber=%d\n", b->BankNumber);
668 	printf("\t\tClearStatusOnInit=%d\n", b->ClearStatusOnInit);
669 	printf("\t\tStatusFormat=%d\n", b->StatusFormat);
670 	printf("\t\tControlRegister=%x\n", b->ControlRegister);
671 	printf("\t\tControlData=%jx\n", b->ControlData);
672 	printf("\t\tStatusRegister=%x\n", b->StatusRegister);
673 	printf("\t\tAddressRegister=%x\n", b->AddressRegister);
674 	printf("\t\tMiscRegister=%x\n", b->MiscRegister);
675 }
676 
677 static void
678 acpi_print_hest_notify(ACPI_HEST_NOTIFY *n)
679 {
680 
681 	printf("\t\tType=%d\n", n->Type);
682 	printf("\t\tLength=%d\n", n->Length);
683 	printf("\t\tConfigWriteEnable=%04x\n", n->ConfigWriteEnable);
684 	printf("\t\tPollInterval=%d\n", n->PollInterval);
685 	printf("\t\tVector=%d\n", n->Vector);
686 	printf("\t\tPollingThresholdValue=%d\n", n->PollingThresholdValue);
687 	printf("\t\tPollingThresholdWindow=%d\n", n->PollingThresholdWindow);
688 	printf("\t\tErrorThresholdValue=%d\n", n->ErrorThresholdValue);
689 	printf("\t\tErrorThresholdWindow=%d\n", n->ErrorThresholdWindow);
690 }
691 
692 static void
693 acpi_print_hest_aer(ACPI_HEST_AER_COMMON *a)
694 {
695 
696 	printf("\tFlags=%02x\n", a->Flags);
697 	printf("\tEnabled=%d\n", a->Enabled);
698 	printf("\tRecordsToPreallocate=%d\n", a->RecordsToPreallocate);
699 	printf("\tMaxSectionsPerRecord=%d\n", a->MaxSectionsPerRecord);
700 	printf("\tBus=%d\n", a->Bus);
701 	printf("\tDevice=%d\n", a->Device);
702 	printf("\tFunction=%d\n", a->Function);
703 	printf("\tDeviceControl=%d\n", a->DeviceControl);
704 	printf("\tUncorrectableMask=%d\n", a->UncorrectableMask);
705 	printf("\tUncorrectableSeverity=%d\n", a->UncorrectableSeverity);
706 	printf("\tCorrectableMask=%d\n", a->CorrectableMask);
707 	printf("\tAdvancedCapabilities=%d\n", a->AdvancedCapabilities);
708 }
709 
710 static int
711 acpi_handle_hest_structure(void *addr, int remaining)
712 {
713 	ACPI_HEST_HEADER *hdr = addr;
714 	int i;
715 
716 	if (remaining < (int)sizeof(ACPI_HEST_HEADER))
717 		return (-1);
718 
719 	printf("\n\tType=%d\n", hdr->Type);
720 	printf("\tSourceId=%d\n", hdr->SourceId);
721 	switch (hdr->Type) {
722 	case ACPI_HEST_TYPE_IA32_CHECK: {
723 		ACPI_HEST_IA_MACHINE_CHECK *s = addr;
724 		printf("\tFlags=%02x\n", s->Flags);
725 		printf("\tEnabled=%d\n", s->Enabled);
726 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
727 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
728 		printf("\tGlobalCapabilityData=%jd\n", s->GlobalCapabilityData);
729 		printf("\tGlobalControlData=%jd\n", s->GlobalControlData);
730 		printf("\tNumHardwareBanks=%d\n", s->NumHardwareBanks);
731 		for (i = 0; i < s->NumHardwareBanks; i++) {
732 			acpi_print_hest_bank((ACPI_HEST_IA_ERROR_BANK *)
733 			    (s + 1) + i);
734 		}
735 		return (sizeof(*s) + s->NumHardwareBanks *
736 		    sizeof(ACPI_HEST_IA_ERROR_BANK));
737 	}
738 	case ACPI_HEST_TYPE_IA32_CORRECTED_CHECK: {
739 		ACPI_HEST_IA_CORRECTED *s = addr;
740 		printf("\tFlags=%02x\n", s->Flags);
741 		printf("\tEnabled=%d\n", s->Enabled);
742 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
743 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
744 		printf("\tNotify:\n");
745 		acpi_print_hest_notify(&s->Notify);
746 		printf("\tNumHardwareBanks=%d\n", s->NumHardwareBanks);
747 		for (i = 0; i < s->NumHardwareBanks; i++) {
748 			acpi_print_hest_bank((ACPI_HEST_IA_ERROR_BANK *)
749 			    (s + 1) + i);
750 		}
751 		return (sizeof(*s) + s->NumHardwareBanks *
752 		    sizeof(ACPI_HEST_IA_ERROR_BANK));
753 	}
754 	case ACPI_HEST_TYPE_IA32_NMI: {
755 		ACPI_HEST_IA_NMI *s = addr;
756 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
757 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
758 		printf("\tMaxRawDataLength=%d\n", s->MaxRawDataLength);
759 		return (sizeof(*s));
760 	}
761 	case ACPI_HEST_TYPE_AER_ROOT_PORT: {
762 		ACPI_HEST_AER_ROOT *s = addr;
763 		acpi_print_hest_aer(&s->Aer);
764 		printf("\tRootErrorCommand=%d\n", s->RootErrorCommand);
765 		return (sizeof(*s));
766 	}
767 	case ACPI_HEST_TYPE_AER_ENDPOINT: {
768 		ACPI_HEST_AER *s = addr;
769 		acpi_print_hest_aer(&s->Aer);
770 		return (sizeof(*s));
771 	}
772 	case ACPI_HEST_TYPE_AER_BRIDGE: {
773 		ACPI_HEST_AER_BRIDGE *s = addr;
774 		acpi_print_hest_aer(&s->Aer);
775 		printf("\tUncorrectableMask2=%d\n", s->UncorrectableMask2);
776 		printf("\tUncorrectableSeverity2=%d\n", s->UncorrectableSeverity2);
777 		printf("\tAdvancedCapabilities2=%d\n", s->AdvancedCapabilities2);
778 		return (sizeof(*s));
779 	}
780 	case ACPI_HEST_TYPE_GENERIC_ERROR: {
781 		ACPI_HEST_GENERIC *s = addr;
782 		printf("\tRelatedSourceId=%d\n", s->RelatedSourceId);
783 		printf("\tEnabled=%d\n", s->Enabled);
784 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
785 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
786 		printf("\tMaxRawDataLength=%d\n", s->MaxRawDataLength);
787 		printf("\tErrorStatusAddress=");
788 		acpi_print_gas(&s->ErrorStatusAddress);
789 		printf("\n");
790 		printf("\tNotify:\n");
791 		acpi_print_hest_notify(&s->Notify);
792 		printf("\tErrorBlockLength=%d\n", s->ErrorBlockLength);
793 		return (sizeof(*s));
794 	}
795 	case ACPI_HEST_TYPE_GENERIC_ERROR_V2: {
796 		ACPI_HEST_GENERIC_V2 *s = addr;
797 		printf("\tRelatedSourceId=%d\n", s->RelatedSourceId);
798 		printf("\tEnabled=%d\n", s->Enabled);
799 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
800 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
801 		printf("\tMaxRawDataLength=%d\n", s->MaxRawDataLength);
802 		printf("\tErrorStatusAddress=");
803 		acpi_print_gas(&s->ErrorStatusAddress);
804 		printf("\n");
805 		printf("\tNotify:\n");
806 		acpi_print_hest_notify(&s->Notify);
807 		printf("\tErrorBlockLength=%d\n", s->ErrorBlockLength);
808 		printf("\tReadAckRegister=");
809 		acpi_print_gas(&s->ReadAckRegister);
810 		printf("\n");
811 		printf("\tReadAckPreserve=%jd\n", s->ReadAckPreserve);
812 		printf("\tReadAckWrite=%jd\n", s->ReadAckWrite);
813 		return (sizeof(*s));
814 	}
815 	case ACPI_HEST_TYPE_IA32_DEFERRED_CHECK: {
816 		ACPI_HEST_IA_DEFERRED_CHECK *s = addr;
817 		printf("\tFlags=%02x\n", s->Flags);
818 		printf("\tEnabled=%d\n", s->Enabled);
819 		printf("\tRecordsToPreallocate=%d\n", s->RecordsToPreallocate);
820 		printf("\tMaxSectionsPerRecord=%d\n", s->MaxSectionsPerRecord);
821 		printf("\tNotify:\n");
822 		acpi_print_hest_notify(&s->Notify);
823 		printf("\tNumHardwareBanks=%d\n", s->NumHardwareBanks);
824 		for (i = 0; i < s->NumHardwareBanks; i++) {
825 			acpi_print_hest_bank((ACPI_HEST_IA_ERROR_BANK *)
826 			    (s + 1) + i);
827 		}
828 		return (sizeof(*s) + s->NumHardwareBanks *
829 		    sizeof(ACPI_HEST_IA_ERROR_BANK));
830 	}
831 	default:
832 		return (-1);
833 	}
834 }
835 
836 static void
837 acpi_handle_hest(ACPI_TABLE_HEADER *sdp)
838 {
839 	char *cp;
840 	int remaining, consumed;
841 	ACPI_TABLE_HEST *hest;
842 
843 	printf(BEGIN_COMMENT);
844 	acpi_print_sdt(sdp);
845 	hest = (ACPI_TABLE_HEST *)sdp;
846 	printf("\tErrorSourceCount=%d\n", hest->ErrorSourceCount);
847 
848 	remaining = sdp->Length - sizeof(ACPI_TABLE_HEST);
849 	while (remaining > 0) {
850 		cp = (char *)sdp + sdp->Length - remaining;
851 		consumed = acpi_handle_hest_structure(cp, remaining);
852 		if (consumed <= 0)
853 			break;
854 		else
855 			remaining -= consumed;
856 	}
857 	printf(END_COMMENT);
858 }
859 
860 static void
861 acpi_handle_hpet(ACPI_TABLE_HEADER *sdp)
862 {
863 	ACPI_TABLE_HPET *hpet;
864 
865 	printf(BEGIN_COMMENT);
866 	acpi_print_sdt(sdp);
867 	hpet = (ACPI_TABLE_HPET *)sdp;
868 	printf("\tHPET Number=%d\n", hpet->Sequence);
869 	printf("\tADDR=");
870 	acpi_print_gas(&hpet->Address);
871 	printf("\n\tHW Rev=0x%x\n", hpet->Id & ACPI_HPET_ID_HARDWARE_REV_ID);
872 	printf("\tComparators=%d\n", (hpet->Id & ACPI_HPET_ID_COMPARATORS) >>
873 	    8);
874 	printf("\tCounter Size=%d\n", hpet->Id & ACPI_HPET_ID_COUNT_SIZE_CAP ?
875 	    1 : 0);
876 	printf("\tLegacy IRQ routing capable={");
877 	if (hpet->Id & ACPI_HPET_ID_LEGACY_CAPABLE)
878 		printf("TRUE}\n");
879 	else
880 		printf("FALSE}\n");
881 	printf("\tPCI Vendor ID=0x%04x\n", hpet->Id >> 16);
882 	printf("\tMinimal Tick=%d\n", hpet->MinimumTick);
883 	printf("\tFlags=0x%02x\n", hpet->Flags);
884 	printf(END_COMMENT);
885 }
886 
887 static void
888 acpi_handle_ecdt(ACPI_TABLE_HEADER *sdp)
889 {
890 	ACPI_TABLE_ECDT *ecdt;
891 
892 	printf(BEGIN_COMMENT);
893 	acpi_print_sdt(sdp);
894 	ecdt = (ACPI_TABLE_ECDT *)sdp;
895 	printf("\tEC_CONTROL=");
896 	acpi_print_gas(&ecdt->Control);
897 	printf("\n\tEC_DATA=");
898 	acpi_print_gas(&ecdt->Data);
899 	printf("\n\tUID=%#x, ", ecdt->Uid);
900 	printf("GPE_BIT=%#x\n", ecdt->Gpe);
901 	printf("\tEC_ID=%s\n", ecdt->Id);
902 	printf(END_COMMENT);
903 }
904 
905 static void
906 acpi_handle_mcfg(ACPI_TABLE_HEADER *sdp)
907 {
908 	ACPI_TABLE_MCFG *mcfg;
909 	ACPI_MCFG_ALLOCATION *alloc;
910 	u_int i, entries;
911 
912 	printf(BEGIN_COMMENT);
913 	acpi_print_sdt(sdp);
914 	mcfg = (ACPI_TABLE_MCFG *)sdp;
915 	entries = (sdp->Length - sizeof(ACPI_TABLE_MCFG)) /
916 	    sizeof(ACPI_MCFG_ALLOCATION);
917 	alloc = (ACPI_MCFG_ALLOCATION *)(mcfg + 1);
918 	for (i = 0; i < entries; i++, alloc++) {
919 		printf("\n");
920 		printf("\tBase Address=0x%016jx\n", (uintmax_t)alloc->Address);
921 		printf("\tSegment Group=0x%04x\n", alloc->PciSegment);
922 		printf("\tStart Bus=%d\n", alloc->StartBusNumber);
923 		printf("\tEnd Bus=%d\n", alloc->EndBusNumber);
924 	}
925 	printf(END_COMMENT);
926 }
927 
928 static void
929 acpi_handle_slit(ACPI_TABLE_HEADER *sdp)
930 {
931 	ACPI_TABLE_SLIT *slit;
932 	UINT64 i, j;
933 
934 	printf(BEGIN_COMMENT);
935 	acpi_print_sdt(sdp);
936 	slit = (ACPI_TABLE_SLIT *)sdp;
937 	printf("\tLocality Count=%ju\n", (uintmax_t)slit->LocalityCount);
938 	printf("\n\t      ");
939 	for (i = 0; i < slit->LocalityCount; i++)
940 		printf(" %3ju", (uintmax_t)i);
941 	printf("\n\t     +");
942 	for (i = 0; i < slit->LocalityCount; i++)
943 		printf("----");
944 	printf("\n");
945 	for (i = 0; i < slit->LocalityCount; i++) {
946 		printf("\t %3ju |", (uintmax_t)i);
947 		for (j = 0; j < slit->LocalityCount; j++)
948 			printf(" %3d",
949 			    slit->Entry[i * slit->LocalityCount + j]);
950 		printf("\n");
951 	}
952 	printf(END_COMMENT);
953 }
954 
955 static void
956 acpi_handle_wddt(ACPI_TABLE_HEADER *sdp)
957 {
958 	ACPI_TABLE_WDDT *wddt;
959 
960 	printf(BEGIN_COMMENT);
961 	acpi_print_sdt(sdp);
962 	wddt = (ACPI_TABLE_WDDT *)sdp;
963 	printf("\tSpecVersion=0x%04x, TableVersion=0x%04x\n",
964 	    wddt->SpecVersion, wddt->TableVersion);
965 	printf("\tPciVendorId=0x%04x, Address=", wddt->PciVendorId);
966 	acpi_print_gas(&wddt->Address);
967 	printf("\n\tMaxCount=%u, MinCount=%u, Period=%ums\n",
968 	    wddt->MaxCount, wddt->MinCount, wddt->Period);
969 
970 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_WDDT_## flag, #flag)
971 	printf("\tStatus=");
972 	PRINTFLAG(wddt->Status, AVAILABLE);
973 	PRINTFLAG(wddt->Status, ACTIVE);
974 	PRINTFLAG(wddt->Status, TCO_OS_OWNED);
975 	PRINTFLAG(wddt->Status, USER_RESET);
976 	PRINTFLAG(wddt->Status, WDT_RESET);
977 	PRINTFLAG(wddt->Status, POWER_FAIL);
978 	PRINTFLAG(wddt->Status, UNKNOWN_RESET);
979 	PRINTFLAG_END();
980 	printf("\tCapability=");
981 	PRINTFLAG(wddt->Capability, AUTO_RESET);
982 	PRINTFLAG(wddt->Capability, ALERT_SUPPORT);
983 	PRINTFLAG_END();
984 #undef PRINTFLAG
985 
986 	printf(END_COMMENT);
987 }
988 
989 static void
990 acpi_print_native_lpit(ACPI_LPIT_NATIVE *nl)
991 {
992 	printf("\tEntryTrigger=");
993 	acpi_print_gas(&nl->EntryTrigger);
994 	printf("\n\tResidency=%u\n", nl->Residency);
995 	printf("\tLatency=%u\n", nl->Latency);
996 	if (nl->Header.Flags & ACPI_LPIT_NO_COUNTER)
997 		printf("\tResidencyCounter=Not Present");
998 	else {
999 		printf("\tResidencyCounter=");
1000 		acpi_print_gas(&nl->ResidencyCounter);
1001 		printf("\n");
1002 	}
1003 	if (nl->CounterFrequency)
1004 		printf("\tCounterFrequency=%ju\n", nl->CounterFrequency);
1005 	else
1006 		printf("\tCounterFrequency=TSC\n");
1007 }
1008 
1009 static void
1010 acpi_print_lpit(ACPI_LPIT_HEADER *lpit)
1011 {
1012 	if (lpit->Type == ACPI_LPIT_TYPE_NATIVE_CSTATE)
1013 		printf("\tType=ACPI_LPIT_TYPE_NATIVE_CSTATE\n");
1014 	else
1015 		warnx("unknown LPIT type %u", lpit->Type);
1016 
1017 	printf("\tLength=%u\n", lpit->Length);
1018 	printf("\tUniqueId=0x%04x\n", lpit->UniqueId);
1019 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_LPIT_## flag, #flag)
1020 	printf("\tFlags=");
1021 	PRINTFLAG(lpit->Flags, STATE_DISABLED);
1022 	PRINTFLAG_END();
1023 #undef PRINTFLAG
1024 
1025 	if (lpit->Type == ACPI_LPIT_TYPE_NATIVE_CSTATE)
1026 		return acpi_print_native_lpit((ACPI_LPIT_NATIVE *)lpit);
1027 }
1028 
1029 static void
1030 acpi_walk_lpit(ACPI_TABLE_HEADER *table, void *first,
1031     void (*action)(ACPI_LPIT_HEADER *))
1032 {
1033 	ACPI_LPIT_HEADER *subtable;
1034 	char *end;
1035 
1036 	subtable = first;
1037 	end = (char *)table + table->Length;
1038 	while ((char *)subtable < end) {
1039 		printf("\n");
1040 		if (subtable->Length < sizeof(ACPI_LPIT_HEADER)) {
1041 			warnx("invalid subtable length %u", subtable->Length);
1042 			return;
1043 		}
1044 		action(subtable);
1045 		subtable = (ACPI_LPIT_HEADER *)((char *)subtable +
1046 		    subtable->Length);
1047 	}
1048 }
1049 
1050 static void
1051 acpi_handle_lpit(ACPI_TABLE_HEADER *sdp)
1052 {
1053 	ACPI_TABLE_LPIT *lpit;
1054 
1055 	printf(BEGIN_COMMENT);
1056 	acpi_print_sdt(sdp);
1057 	lpit = (ACPI_TABLE_LPIT *)sdp;
1058 	acpi_walk_lpit(sdp, (lpit + 1), acpi_print_lpit);
1059 
1060 	printf(END_COMMENT);
1061 }
1062 
1063 static void
1064 acpi_print_srat_cpu(uint32_t apic_id, uint32_t proximity_domain,
1065     uint32_t flags)
1066 {
1067 
1068 	printf("\tFlags={");
1069 	if (flags & ACPI_SRAT_CPU_ENABLED)
1070 		printf("ENABLED");
1071 	else
1072 		printf("DISABLED");
1073 	printf("}\n");
1074 	printf("\tAPIC ID=%d\n", apic_id);
1075 	printf("\tProximity Domain=%d\n", proximity_domain);
1076 }
1077 
1078 static char *
1079 acpi_tcpa_evname(struct TCPAevent *event)
1080 {
1081 	struct TCPApc_event *pc_event;
1082 	char *eventname = NULL;
1083 
1084 	pc_event = (struct TCPApc_event *)(event + 1);
1085 
1086 	switch(event->event_type) {
1087 	case PREBOOT:
1088 	case POST_CODE:
1089 	case UNUSED:
1090 	case NO_ACTION:
1091 	case SEPARATOR:
1092 	case SCRTM_CONTENTS:
1093 	case SCRTM_VERSION:
1094 	case CPU_MICROCODE:
1095 	case PLATFORM_CONFIG_FLAGS:
1096 	case TABLE_OF_DEVICES:
1097 	case COMPACT_HASH:
1098 	case IPL:
1099 	case IPL_PARTITION_DATA:
1100 	case NONHOST_CODE:
1101 	case NONHOST_CONFIG:
1102 	case NONHOST_INFO:
1103 		asprintf(&eventname, "%s",
1104 		    tcpa_event_type_strings[event->event_type]);
1105 		break;
1106 
1107 	case ACTION:
1108 		eventname = calloc(event->event_size + 1, sizeof(char));
1109 		memcpy(eventname, pc_event, event->event_size);
1110 		break;
1111 
1112 	case EVENT_TAG:
1113 		switch (pc_event->event_id) {
1114 		case SMBIOS:
1115 		case BIS_CERT:
1116 		case CMOS:
1117 		case NVRAM:
1118 		case OPTION_ROM_EXEC:
1119 		case OPTION_ROM_CONFIG:
1120 		case S_CRTM_VERSION:
1121 		case POST_BIOS_ROM:
1122 		case ESCD:
1123 		case OPTION_ROM_MICROCODE:
1124 		case S_CRTM_CONTENTS:
1125 		case POST_CONTENTS:
1126 			asprintf(&eventname, "%s",
1127 			    TCPA_pcclient_strings[pc_event->event_id]);
1128 			break;
1129 
1130 		default:
1131 			asprintf(&eventname, "<unknown tag 0x%02x>",
1132 			    pc_event->event_id);
1133 			break;
1134 		}
1135 		break;
1136 
1137 	default:
1138 		asprintf(&eventname, "<unknown 0x%02x>", event->event_type);
1139 		break;
1140 	}
1141 
1142 	return eventname;
1143 }
1144 
1145 static void
1146 acpi_print_tcpa(struct TCPAevent *event)
1147 {
1148 	int i;
1149 	char *eventname;
1150 
1151 	eventname = acpi_tcpa_evname(event);
1152 
1153 	printf("\t%d", event->pcr_index);
1154 	printf(" 0x");
1155 	for (i = 0; i < 20; i++)
1156 		printf("%02x", event->pcr_value[i]);
1157 	printf(" [%s]\n", eventname ? eventname : "<unknown>");
1158 
1159 	free(eventname);
1160 }
1161 
1162 static void
1163 acpi_handle_tcpa(ACPI_TABLE_HEADER *sdp)
1164 {
1165 	struct TCPAbody *tcpa;
1166 	struct TCPAevent *event;
1167 	uintmax_t len, paddr;
1168 	unsigned char *vaddr = NULL;
1169 	unsigned char *vend = NULL;
1170 
1171 	printf(BEGIN_COMMENT);
1172 	acpi_print_sdt(sdp);
1173 	tcpa = (struct TCPAbody *) sdp;
1174 
1175 	switch (tcpa->platform_class) {
1176 	case ACPI_TCPA_BIOS_CLIENT:
1177 		len = tcpa->client.log_max_len;
1178 		paddr = tcpa->client.log_start_addr;
1179 		break;
1180 
1181 	case ACPI_TCPA_BIOS_SERVER:
1182 		len = tcpa->server.log_max_len;
1183 		paddr = tcpa->server.log_start_addr;
1184 		break;
1185 
1186 	default:
1187 		printf("XXX");
1188 		printf(END_COMMENT);
1189 		return;
1190 	}
1191 	printf("\tClass %u Base Address 0x%jx Length %ju\n\n",
1192 	    tcpa->platform_class, paddr, len);
1193 
1194 	if (len == 0) {
1195 		printf("\tEmpty TCPA table\n");
1196 		printf(END_COMMENT);
1197 		return;
1198 	}
1199 	if(sdp->Revision == 1){
1200 		printf("\tOLD TCPA spec log found. Dumping not supported.\n");
1201 		printf(END_COMMENT);
1202 		return;
1203 	}
1204 
1205 	vaddr = (unsigned char *)acpi_map_physical(paddr, len);
1206 	vend = vaddr + len;
1207 
1208 	while (vaddr != NULL) {
1209 		if ((vaddr + sizeof(struct TCPAevent) >= vend)||
1210 		    (vaddr + sizeof(struct TCPAevent) < vaddr))
1211 			break;
1212 		event = (struct TCPAevent *)(void *)vaddr;
1213 		if (vaddr + event->event_size >= vend)
1214 			break;
1215 		if (vaddr + event->event_size < vaddr)
1216 			break;
1217 		if (event->event_type == 0 && event->event_size == 0)
1218 			break;
1219 #if 0
1220 		{
1221 		unsigned int i, j, k;
1222 
1223 		printf("\n\tsize %d\n\t\t%p ", event->event_size, vaddr);
1224 		for (j = 0, i = 0; i <
1225 		    sizeof(struct TCPAevent) + event->event_size; i++) {
1226 			printf("%02x ", vaddr[i]);
1227 			if ((i+1) % 8 == 0) {
1228 				for (k = 0; k < 8; k++)
1229 					printf("%c", isprint(vaddr[j+k]) ?
1230 					    vaddr[j+k] : '.');
1231 				printf("\n\t\t%p ", &vaddr[i + 1]);
1232 				j = i + 1;
1233 			}
1234 		}
1235 		printf("\n"); }
1236 #endif
1237 		acpi_print_tcpa(event);
1238 
1239 		vaddr += sizeof(struct TCPAevent) + event->event_size;
1240 	}
1241 
1242 	printf(END_COMMENT);
1243 }
1244 static void acpi_handle_tpm2(ACPI_TABLE_HEADER *sdp)
1245 {
1246 	ACPI_TABLE_TPM2 *tpm2;
1247 
1248 	printf (BEGIN_COMMENT);
1249 	acpi_print_sdt(sdp);
1250 	tpm2 = (ACPI_TABLE_TPM2 *) sdp;
1251 	printf ("\t\tControlArea=%jx\n", tpm2->ControlAddress);
1252 	printf ("\t\tStartMethod=%x\n", tpm2->StartMethod);
1253 	printf (END_COMMENT);
1254 }
1255 
1256 static const char *
1257 devscope_type2str(int type)
1258 {
1259 	static char typebuf[16];
1260 
1261 	switch (type) {
1262 	case 1:
1263 		return ("PCI Endpoint Device");
1264 	case 2:
1265 		return ("PCI Sub-Hierarchy");
1266 	case 3:
1267 		return ("IOAPIC");
1268 	case 4:
1269 		return ("HPET");
1270 	default:
1271 		snprintf(typebuf, sizeof(typebuf), "%d", type);
1272 		return (typebuf);
1273 	}
1274 }
1275 
1276 static int
1277 acpi_handle_dmar_devscope(void *addr, int remaining)
1278 {
1279 	char sep;
1280 	int pathlen;
1281 	ACPI_DMAR_PCI_PATH *path, *pathend;
1282 	ACPI_DMAR_DEVICE_SCOPE *devscope = addr;
1283 
1284 	if (remaining < (int)sizeof(ACPI_DMAR_DEVICE_SCOPE))
1285 		return (-1);
1286 
1287 	if (remaining < devscope->Length)
1288 		return (-1);
1289 
1290 	printf("\n");
1291 	printf("\t\tType=%s\n", devscope_type2str(devscope->EntryType));
1292 	printf("\t\tLength=%d\n", devscope->Length);
1293 	printf("\t\tEnumerationId=%d\n", devscope->EnumerationId);
1294 	printf("\t\tStartBusNumber=%d\n", devscope->Bus);
1295 
1296 	path = (ACPI_DMAR_PCI_PATH *)(devscope + 1);
1297 	pathlen = devscope->Length - sizeof(ACPI_DMAR_DEVICE_SCOPE);
1298 	pathend = path + pathlen / sizeof(ACPI_DMAR_PCI_PATH);
1299 	if (path < pathend) {
1300 		sep = '{';
1301 		printf("\t\tPath=");
1302 		do {
1303 			printf("%c%d:%d", sep, path->Device, path->Function);
1304 			sep=',';
1305 			path++;
1306 		} while (path < pathend);
1307 		printf("}\n");
1308 	}
1309 
1310 	return (devscope->Length);
1311 }
1312 
1313 static void
1314 acpi_handle_dmar_drhd(ACPI_DMAR_HARDWARE_UNIT *drhd)
1315 {
1316 	char *cp;
1317 	int remaining, consumed;
1318 
1319 	printf("\n");
1320 	printf("\tType=DRHD\n");
1321 	printf("\tLength=%d\n", drhd->Header.Length);
1322 
1323 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1324 
1325 	printf("\tFlags=");
1326 	PRINTFLAG(drhd->Flags, INCLUDE_ALL);
1327 	PRINTFLAG_END();
1328 
1329 #undef PRINTFLAG
1330 
1331 	printf("\tSegment=%d\n", drhd->Segment);
1332 	printf("\tAddress=0x%016jx\n", (uintmax_t)drhd->Address);
1333 
1334 	remaining = drhd->Header.Length - sizeof(ACPI_DMAR_HARDWARE_UNIT);
1335 	if (remaining > 0)
1336 		printf("\tDevice Scope:");
1337 	while (remaining > 0) {
1338 		cp = (char *)drhd + drhd->Header.Length - remaining;
1339 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1340 		if (consumed <= 0)
1341 			break;
1342 		else
1343 			remaining -= consumed;
1344 	}
1345 }
1346 
1347 static void
1348 acpi_handle_dmar_rmrr(ACPI_DMAR_RESERVED_MEMORY *rmrr)
1349 {
1350 	char *cp;
1351 	int remaining, consumed;
1352 
1353 	printf("\n");
1354 	printf("\tType=RMRR\n");
1355 	printf("\tLength=%d\n", rmrr->Header.Length);
1356 	printf("\tSegment=%d\n", rmrr->Segment);
1357 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rmrr->BaseAddress);
1358 	printf("\tLimitAddress=0x%016jx\n", (uintmax_t)rmrr->EndAddress);
1359 
1360 	remaining = rmrr->Header.Length - sizeof(ACPI_DMAR_RESERVED_MEMORY);
1361 	if (remaining > 0)
1362 		printf("\tDevice Scope:");
1363 	while (remaining > 0) {
1364 		cp = (char *)rmrr + rmrr->Header.Length - remaining;
1365 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1366 		if (consumed <= 0)
1367 			break;
1368 		else
1369 			remaining -= consumed;
1370 	}
1371 }
1372 
1373 static void
1374 acpi_handle_dmar_atsr(ACPI_DMAR_ATSR *atsr)
1375 {
1376 	char *cp;
1377 	int remaining, consumed;
1378 
1379 	printf("\n");
1380 	printf("\tType=ATSR\n");
1381 	printf("\tLength=%d\n", atsr->Header.Length);
1382 
1383 #define	PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1384 
1385 	printf("\tFlags=");
1386 	PRINTFLAG(atsr->Flags, ALL_PORTS);
1387 	PRINTFLAG_END();
1388 
1389 #undef PRINTFLAG
1390 
1391 	printf("\tSegment=%d\n", atsr->Segment);
1392 
1393 	remaining = atsr->Header.Length - sizeof(ACPI_DMAR_ATSR);
1394 	if (remaining > 0)
1395 		printf("\tDevice Scope:");
1396 	while (remaining > 0) {
1397 		cp = (char *)atsr + atsr->Header.Length - remaining;
1398 		consumed = acpi_handle_dmar_devscope(cp, remaining);
1399 		if (consumed <= 0)
1400 			break;
1401 		else
1402 			remaining -= consumed;
1403 	}
1404 }
1405 
1406 static void
1407 acpi_handle_dmar_rhsa(ACPI_DMAR_RHSA *rhsa)
1408 {
1409 
1410 	printf("\n");
1411 	printf("\tType=RHSA\n");
1412 	printf("\tLength=%d\n", rhsa->Header.Length);
1413 	printf("\tBaseAddress=0x%016jx\n", (uintmax_t)rhsa->BaseAddress);
1414 	printf("\tProximityDomain=0x%08x\n", rhsa->ProximityDomain);
1415 }
1416 
1417 static int
1418 acpi_handle_dmar_remapping_structure(void *addr, int remaining)
1419 {
1420 	ACPI_DMAR_HEADER *hdr = addr;
1421 
1422 	if (remaining < (int)sizeof(ACPI_DMAR_HEADER))
1423 		return (-1);
1424 
1425 	if (remaining < hdr->Length)
1426 		return (-1);
1427 
1428 	switch (hdr->Type) {
1429 	case ACPI_DMAR_TYPE_HARDWARE_UNIT:
1430 		acpi_handle_dmar_drhd(addr);
1431 		break;
1432 	case ACPI_DMAR_TYPE_RESERVED_MEMORY:
1433 		acpi_handle_dmar_rmrr(addr);
1434 		break;
1435 	case ACPI_DMAR_TYPE_ROOT_ATS:
1436 		acpi_handle_dmar_atsr(addr);
1437 		break;
1438 	case ACPI_DMAR_TYPE_HARDWARE_AFFINITY:
1439 		acpi_handle_dmar_rhsa(addr);
1440 		break;
1441 	default:
1442 		printf("\n");
1443 		printf("\tType=%d\n", hdr->Type);
1444 		printf("\tLength=%d\n", hdr->Length);
1445 		break;
1446 	}
1447 	return (hdr->Length);
1448 }
1449 
1450 #ifndef ACPI_DMAR_X2APIC_OPT_OUT
1451 #define	ACPI_DMAR_X2APIC_OPT_OUT	(0x2)
1452 #endif
1453 
1454 static void
1455 acpi_handle_dmar(ACPI_TABLE_HEADER *sdp)
1456 {
1457 	char *cp;
1458 	int remaining, consumed;
1459 	ACPI_TABLE_DMAR *dmar;
1460 
1461 	printf(BEGIN_COMMENT);
1462 	acpi_print_sdt(sdp);
1463 	dmar = (ACPI_TABLE_DMAR *)sdp;
1464 	printf("\tHost Address Width=%d\n", dmar->Width + 1);
1465 
1466 #define PRINTFLAG(var, flag)	printflag((var), ACPI_DMAR_## flag, #flag)
1467 
1468 	printf("\tFlags=");
1469 	PRINTFLAG(dmar->Flags, INTR_REMAP);
1470 	PRINTFLAG(dmar->Flags, X2APIC_OPT_OUT);
1471 	PRINTFLAG_END();
1472 
1473 #undef PRINTFLAG
1474 
1475 	remaining = sdp->Length - sizeof(ACPI_TABLE_DMAR);
1476 	while (remaining > 0) {
1477 		cp = (char *)sdp + sdp->Length - remaining;
1478 		consumed = acpi_handle_dmar_remapping_structure(cp, remaining);
1479 		if (consumed <= 0)
1480 			break;
1481 		else
1482 			remaining -= consumed;
1483 	}
1484 
1485 	printf(END_COMMENT);
1486 }
1487 
1488 static void
1489 acpi_print_srat_memory(ACPI_SRAT_MEM_AFFINITY *mp)
1490 {
1491 
1492 	printf("\tFlags={");
1493 	if (mp->Flags & ACPI_SRAT_MEM_ENABLED)
1494 		printf("ENABLED");
1495 	else
1496 		printf("DISABLED");
1497 	if (mp->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)
1498 		printf(",HOT_PLUGGABLE");
1499 	if (mp->Flags & ACPI_SRAT_MEM_NON_VOLATILE)
1500 		printf(",NON_VOLATILE");
1501 	printf("}\n");
1502 	printf("\tBase Address=0x%016jx\n", (uintmax_t)mp->BaseAddress);
1503 	printf("\tLength=0x%016jx\n", (uintmax_t)mp->Length);
1504 	printf("\tProximity Domain=%d\n", mp->ProximityDomain);
1505 }
1506 
1507 static const char *srat_types[] = {
1508     [ACPI_SRAT_TYPE_CPU_AFFINITY] = "CPU",
1509     [ACPI_SRAT_TYPE_MEMORY_AFFINITY] = "Memory",
1510     [ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY] = "X2APIC",
1511     [ACPI_SRAT_TYPE_GICC_AFFINITY] = "GICC",
1512     [ACPI_SRAT_TYPE_GIC_ITS_AFFINITY] = "GIC ITS",
1513 };
1514 
1515 static void
1516 acpi_print_srat(ACPI_SUBTABLE_HEADER *srat)
1517 {
1518 	ACPI_SRAT_CPU_AFFINITY *cpu;
1519 	ACPI_SRAT_X2APIC_CPU_AFFINITY *x2apic;
1520 	ACPI_SRAT_GICC_AFFINITY *gic;
1521 
1522 	if (srat->Type < nitems(srat_types))
1523 		printf("\tType=%s\n", srat_types[srat->Type]);
1524 	else
1525 		printf("\tType=%d (unknown)\n", srat->Type);
1526 	switch (srat->Type) {
1527 	case ACPI_SRAT_TYPE_CPU_AFFINITY:
1528 		cpu = (ACPI_SRAT_CPU_AFFINITY *)srat;
1529 		acpi_print_srat_cpu(cpu->ApicId,
1530 		    cpu->ProximityDomainHi[2] << 24 |
1531 		    cpu->ProximityDomainHi[1] << 16 |
1532 		    cpu->ProximityDomainHi[0] << 0 |
1533 		    cpu->ProximityDomainLo, cpu->Flags);
1534 		break;
1535 	case ACPI_SRAT_TYPE_MEMORY_AFFINITY:
1536 		acpi_print_srat_memory((ACPI_SRAT_MEM_AFFINITY *)srat);
1537 		break;
1538 	case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY:
1539 		x2apic = (ACPI_SRAT_X2APIC_CPU_AFFINITY *)srat;
1540 		acpi_print_srat_cpu(x2apic->ApicId, x2apic->ProximityDomain,
1541 		    x2apic->Flags);
1542 		break;
1543 	case ACPI_SRAT_TYPE_GICC_AFFINITY:
1544 		gic = (ACPI_SRAT_GICC_AFFINITY *)srat;
1545 		acpi_print_srat_cpu(gic->AcpiProcessorUid, gic->ProximityDomain,
1546 		    gic->Flags);
1547 		break;
1548 	}
1549 }
1550 
1551 static void
1552 acpi_handle_srat(ACPI_TABLE_HEADER *sdp)
1553 {
1554 	ACPI_TABLE_SRAT *srat;
1555 
1556 	printf(BEGIN_COMMENT);
1557 	acpi_print_sdt(sdp);
1558 	srat = (ACPI_TABLE_SRAT *)sdp;
1559 	printf("\tTable Revision=%d\n", srat->TableRevision);
1560 	acpi_walk_subtables(sdp, (srat + 1), acpi_print_srat);
1561 	printf(END_COMMENT);
1562 }
1563 
1564 static const char *nfit_types[] = {
1565     [ACPI_NFIT_TYPE_SYSTEM_ADDRESS] = "System Address",
1566     [ACPI_NFIT_TYPE_MEMORY_MAP] = "Memory Map",
1567     [ACPI_NFIT_TYPE_INTERLEAVE] = "Interleave",
1568     [ACPI_NFIT_TYPE_SMBIOS] = "SMBIOS",
1569     [ACPI_NFIT_TYPE_CONTROL_REGION] = "Control Region",
1570     [ACPI_NFIT_TYPE_DATA_REGION] = "Data Region",
1571     [ACPI_NFIT_TYPE_FLUSH_ADDRESS] = "Flush Address",
1572     [ACPI_NFIT_TYPE_CAPABILITIES] = "Platform Capabilities"
1573 };
1574 
1575 
1576 static void
1577 acpi_print_nfit(ACPI_NFIT_HEADER *nfit)
1578 {
1579 	char *uuidstr;
1580 	uint32_t status;
1581 
1582 	ACPI_NFIT_SYSTEM_ADDRESS *sysaddr;
1583 	ACPI_NFIT_MEMORY_MAP *mmap;
1584 	ACPI_NFIT_INTERLEAVE *ileave;
1585 	ACPI_NFIT_SMBIOS *smbios;
1586 	ACPI_NFIT_CONTROL_REGION *ctlreg;
1587 	ACPI_NFIT_DATA_REGION *datareg;
1588 	ACPI_NFIT_FLUSH_ADDRESS *fladdr;
1589 	ACPI_NFIT_CAPABILITIES *caps;
1590 
1591 	if (nfit->Type < nitems(nfit_types))
1592 		printf("\tType=%s\n", nfit_types[nfit->Type]);
1593 	else
1594 		printf("\tType=%u (unknown)\n", nfit->Type);
1595 	switch (nfit->Type) {
1596 	case ACPI_NFIT_TYPE_SYSTEM_ADDRESS:
1597 		sysaddr = (ACPI_NFIT_SYSTEM_ADDRESS *)nfit;
1598 		printf("\tRangeIndex=%u\n", (u_int)sysaddr->RangeIndex);
1599 		printf("\tProximityDomain=%u\n",
1600 		    (u_int)sysaddr->ProximityDomain);
1601 		uuid_to_string((uuid_t *)(sysaddr->RangeGuid),
1602 		    &uuidstr, &status);
1603 		if (status != uuid_s_ok)
1604 			errx(1, "uuid_to_string: status=%u", status);
1605 		printf("\tRangeGuid=%s\n", uuidstr);
1606 		free(uuidstr);
1607 		printf("\tAddress=0x%016jx\n", (uintmax_t)sysaddr->Address);
1608 		printf("\tLength=0x%016jx\n", (uintmax_t)sysaddr->Length);
1609 		printf("\tMemoryMapping=0x%016jx\n",
1610 		    (uintmax_t)sysaddr->MemoryMapping);
1611 
1612 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_## flag, #flag)
1613 
1614 		printf("\tFlags=");
1615 		PRINTFLAG(sysaddr->Flags, ADD_ONLINE_ONLY);
1616 		PRINTFLAG(sysaddr->Flags, PROXIMITY_VALID);
1617 		PRINTFLAG_END();
1618 
1619 #undef PRINTFLAG
1620 
1621 		break;
1622 	case ACPI_NFIT_TYPE_MEMORY_MAP:
1623 		mmap = (ACPI_NFIT_MEMORY_MAP *)nfit;
1624 		printf("\tDeviceHandle=0x%x\n", (u_int)mmap->DeviceHandle);
1625 		printf("\tPhysicalId=0x%04x\n", (u_int)mmap->PhysicalId);
1626 		printf("\tRegionId=%u\n", (u_int)mmap->RegionId);
1627 		printf("\tRangeIndex=%u\n", (u_int)mmap->RangeIndex);
1628 		printf("\tRegionIndex=%u\n", (u_int)mmap->RegionIndex);
1629 		printf("\tRegionSize=0x%016jx\n", (uintmax_t)mmap->RegionSize);
1630 		printf("\tRegionOffset=0x%016jx\n",
1631 		    (uintmax_t)mmap->RegionOffset);
1632 		printf("\tAddress=0x%016jx\n", (uintmax_t)mmap->Address);
1633 		printf("\tInterleaveIndex=%u\n", (u_int)mmap->InterleaveIndex);
1634 		printf("\tInterleaveWays=%u\n", (u_int)mmap->InterleaveWays);
1635 
1636 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_MEM_## flag, #flag)
1637 
1638 		printf("\tFlags=");
1639 		PRINTFLAG(mmap->Flags, SAVE_FAILED);
1640 		PRINTFLAG(mmap->Flags, RESTORE_FAILED);
1641 		PRINTFLAG(mmap->Flags, FLUSH_FAILED);
1642 		PRINTFLAG(mmap->Flags, NOT_ARMED);
1643 		PRINTFLAG(mmap->Flags, HEALTH_OBSERVED);
1644 		PRINTFLAG(mmap->Flags, HEALTH_ENABLED);
1645 		PRINTFLAG(mmap->Flags, MAP_FAILED);
1646 		PRINTFLAG_END();
1647 
1648 #undef PRINTFLAG
1649 
1650 		break;
1651 	case ACPI_NFIT_TYPE_INTERLEAVE:
1652 		ileave = (ACPI_NFIT_INTERLEAVE *)nfit;
1653 		printf("\tInterleaveIndex=%u\n",
1654 		    (u_int)ileave->InterleaveIndex);
1655 		printf("\tLineCount=%u\n", (u_int)ileave->LineCount);
1656 		printf("\tLineSize=%u\n", (u_int)ileave->LineSize);
1657 		/* XXX ileave->LineOffset[i] output is not supported */
1658 		break;
1659 	case ACPI_NFIT_TYPE_SMBIOS:
1660 		smbios = (ACPI_NFIT_SMBIOS *)nfit;
1661 		/* XXX smbios->Data[x] output is not supported */
1662 		break;
1663 	case ACPI_NFIT_TYPE_CONTROL_REGION:
1664 		ctlreg = (ACPI_NFIT_CONTROL_REGION *)nfit;
1665 		printf("\tRegionIndex=%u\n", (u_int)ctlreg->RegionIndex);
1666 		printf("\tVendorId=0x%04x\n", (u_int)ctlreg->VendorId);
1667 		printf("\tDeviceId=0x%04x\n", (u_int)ctlreg->DeviceId);
1668 		printf("\tRevisionId=0x%02x\n", (u_int)ctlreg->RevisionId);
1669 		printf("\tSubsystemVendorId=0x%04x\n",
1670 		    (u_int)ctlreg->SubsystemVendorId);
1671 		printf("\tSubsystemDeviceId=0x%04x\n",
1672 		    (u_int)ctlreg->SubsystemDeviceId);
1673 		printf("\tSubsystemRevisionId=0x%02x\n",
1674 		    (u_int)ctlreg->SubsystemRevisionId);
1675 		printf("\tValidFields=0x%02x\n", (u_int)ctlreg->ValidFields);
1676 		printf("\tManufacturingLocation=0x%02x\n",
1677 		    (u_int)ctlreg->ManufacturingLocation);
1678 		printf("\tManufacturingDate=%04x\n",
1679 		    (u_int)be16toh(ctlreg->ManufacturingDate));
1680 		printf("\tSerialNumber=%08X\n",
1681 		    (u_int)be32toh(ctlreg->SerialNumber));
1682 		printf("\tCode=0x%04x\n", (u_int)ctlreg->Code);
1683 		printf("\tWindows=%u\n", (u_int)ctlreg->Windows);
1684 		printf("\tWindowSize=0x%016jx\n",
1685 		    (uintmax_t)ctlreg->WindowSize);
1686 		printf("\tCommandOffset=0x%016jx\n",
1687 		    (uintmax_t)ctlreg->CommandOffset);
1688 		printf("\tCommandSize=0x%016jx\n",
1689 		    (uintmax_t)ctlreg->CommandSize);
1690 		printf("\tStatusOffset=0x%016jx\n",
1691 		    (uintmax_t)ctlreg->StatusOffset);
1692 		printf("\tStatusSize=0x%016jx\n",
1693 		    (uintmax_t)ctlreg->StatusSize);
1694 
1695 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_## flag, #flag)
1696 
1697 		printf("\tFlags=");
1698 		PRINTFLAG(ctlreg->Flags, CONTROL_BUFFERED);
1699 		PRINTFLAG_END();
1700 
1701 #undef PRINTFLAG
1702 
1703 		break;
1704 	case ACPI_NFIT_TYPE_DATA_REGION:
1705 		datareg = (ACPI_NFIT_DATA_REGION *)nfit;
1706 		printf("\tRegionIndex=%u\n", (u_int)datareg->RegionIndex);
1707 		printf("\tWindows=%u\n", (u_int)datareg->Windows);
1708 		printf("\tOffset=0x%016jx\n", (uintmax_t)datareg->Offset);
1709 		printf("\tSize=0x%016jx\n", (uintmax_t)datareg->Size);
1710 		printf("\tCapacity=0x%016jx\n", (uintmax_t)datareg->Capacity);
1711 		printf("\tStartAddress=0x%016jx\n",
1712 		    (uintmax_t)datareg->StartAddress);
1713 		break;
1714 	case ACPI_NFIT_TYPE_FLUSH_ADDRESS:
1715 		fladdr = (ACPI_NFIT_FLUSH_ADDRESS *)nfit;
1716 		printf("\tDeviceHandle=%u\n", (u_int)fladdr->DeviceHandle);
1717 		printf("\tHintCount=%u\n", (u_int)fladdr->HintCount);
1718 		/* XXX fladdr->HintAddress[i] output is not supported */
1719 		break;
1720 	case ACPI_NFIT_TYPE_CAPABILITIES:
1721 		caps = (ACPI_NFIT_CAPABILITIES *)nfit;
1722 		printf("\tHighestCapability=%u\n", (u_int)caps->HighestCapability);
1723 
1724 #define PRINTFLAG(var, flag)	printflag((var), ACPI_NFIT_CAPABILITY_## flag, #flag)
1725 
1726 		printf("\tCapabilities=");
1727 		PRINTFLAG(caps->Capabilities, CACHE_FLUSH);
1728 		PRINTFLAG(caps->Capabilities, MEM_FLUSH);
1729 		PRINTFLAG(caps->Capabilities, MEM_MIRRORING);
1730 		PRINTFLAG_END();
1731 
1732 #undef PRINTFLAG
1733 		break;
1734 	}
1735 }
1736 
1737 static void
1738 acpi_handle_nfit(ACPI_TABLE_HEADER *sdp)
1739 {
1740 	ACPI_TABLE_NFIT *nfit;
1741 
1742 	printf(BEGIN_COMMENT);
1743 	acpi_print_sdt(sdp);
1744 	nfit = (ACPI_TABLE_NFIT *)sdp;
1745 	acpi_walk_nfit(sdp, (nfit + 1), acpi_print_nfit);
1746 	printf(END_COMMENT);
1747 }
1748 
1749 static void
1750 acpi_print_sdt(ACPI_TABLE_HEADER *sdp)
1751 {
1752 	printf("  ");
1753 	acpi_print_string(sdp->Signature, ACPI_NAMESEG_SIZE);
1754 	printf(": Length=%d, Revision=%d, Checksum=%d,\n",
1755 	       sdp->Length, sdp->Revision, sdp->Checksum);
1756 	printf("\tOEMID=");
1757 	acpi_print_string(sdp->OemId, ACPI_OEM_ID_SIZE);
1758 	printf(", OEM Table ID=");
1759 	acpi_print_string(sdp->OemTableId, ACPI_OEM_TABLE_ID_SIZE);
1760 	printf(", OEM Revision=0x%x,\n", sdp->OemRevision);
1761 	printf("\tCreator ID=");
1762 	acpi_print_string(sdp->AslCompilerId, ACPI_NAMESEG_SIZE);
1763 	printf(", Creator Revision=0x%x\n", sdp->AslCompilerRevision);
1764 }
1765 
1766 static void
1767 acpi_print_rsdt(ACPI_TABLE_HEADER *rsdp)
1768 {
1769 	ACPI_TABLE_RSDT *rsdt;
1770 	ACPI_TABLE_XSDT *xsdt;
1771 	int	i, entries;
1772 
1773 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
1774 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
1775 	printf(BEGIN_COMMENT);
1776 	acpi_print_sdt(rsdp);
1777 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
1778 	printf("\tEntries={ ");
1779 	for (i = 0; i < entries; i++) {
1780 		if (i > 0)
1781 			printf(", ");
1782 		if (addr_size == 4)
1783 			printf("0x%08x", le32toh(rsdt->TableOffsetEntry[i]));
1784 		else
1785 			printf("0x%016jx",
1786 			    (uintmax_t)le64toh(xsdt->TableOffsetEntry[i]));
1787 	}
1788 	printf(" }\n");
1789 	printf(END_COMMENT);
1790 }
1791 
1792 static const char *acpi_pm_profiles[] = {
1793 	"Unspecified", "Desktop", "Mobile", "Workstation",
1794 	"Enterprise Server", "SOHO Server", "Appliance PC"
1795 };
1796 
1797 static void
1798 acpi_print_fadt(ACPI_TABLE_HEADER *sdp)
1799 {
1800 	ACPI_TABLE_FADT *fadt;
1801 	const char *pm;
1802 
1803 	fadt = (ACPI_TABLE_FADT *)sdp;
1804 	printf(BEGIN_COMMENT);
1805 	acpi_print_sdt(sdp);
1806 	printf(" \tFACS=0x%x, DSDT=0x%x\n", fadt->Facs,
1807 	       fadt->Dsdt);
1808 	printf("\tINT_MODEL=%s\n", fadt->Model ? "APIC" : "PIC");
1809 	if (fadt->PreferredProfile >= sizeof(acpi_pm_profiles) / sizeof(char *))
1810 		pm = "Reserved";
1811 	else
1812 		pm = acpi_pm_profiles[fadt->PreferredProfile];
1813 	printf("\tPreferred_PM_Profile=%s (%d)\n", pm, fadt->PreferredProfile);
1814 	printf("\tSCI_INT=%d\n", fadt->SciInterrupt);
1815 	printf("\tSMI_CMD=0x%x, ", fadt->SmiCommand);
1816 	printf("ACPI_ENABLE=0x%x, ", fadt->AcpiEnable);
1817 	printf("ACPI_DISABLE=0x%x, ", fadt->AcpiDisable);
1818 	printf("S4BIOS_REQ=0x%x\n", fadt->S4BiosRequest);
1819 	printf("\tPSTATE_CNT=0x%x\n", fadt->PstateControl);
1820 	printf("\tPM1a_EVT_BLK=0x%x-0x%x\n",
1821 	       fadt->Pm1aEventBlock,
1822 	       fadt->Pm1aEventBlock + fadt->Pm1EventLength - 1);
1823 	if (fadt->Pm1bEventBlock != 0)
1824 		printf("\tPM1b_EVT_BLK=0x%x-0x%x\n",
1825 		       fadt->Pm1bEventBlock,
1826 		       fadt->Pm1bEventBlock + fadt->Pm1EventLength - 1);
1827 	printf("\tPM1a_CNT_BLK=0x%x-0x%x\n",
1828 	       fadt->Pm1aControlBlock,
1829 	       fadt->Pm1aControlBlock + fadt->Pm1ControlLength - 1);
1830 	if (fadt->Pm1bControlBlock != 0)
1831 		printf("\tPM1b_CNT_BLK=0x%x-0x%x\n",
1832 		       fadt->Pm1bControlBlock,
1833 		       fadt->Pm1bControlBlock + fadt->Pm1ControlLength - 1);
1834 	if (fadt->Pm2ControlBlock != 0)
1835 		printf("\tPM2_CNT_BLK=0x%x-0x%x\n",
1836 		       fadt->Pm2ControlBlock,
1837 		       fadt->Pm2ControlBlock + fadt->Pm2ControlLength - 1);
1838 	printf("\tPM_TMR_BLK=0x%x-0x%x\n",
1839 	       fadt->PmTimerBlock,
1840 	       fadt->PmTimerBlock + fadt->PmTimerLength - 1);
1841 	if (fadt->Gpe0Block != 0)
1842 		printf("\tGPE0_BLK=0x%x-0x%x\n",
1843 		       fadt->Gpe0Block,
1844 		       fadt->Gpe0Block + fadt->Gpe0BlockLength - 1);
1845 	if (fadt->Gpe1Block != 0)
1846 		printf("\tGPE1_BLK=0x%x-0x%x, GPE1_BASE=%d\n",
1847 		       fadt->Gpe1Block,
1848 		       fadt->Gpe1Block + fadt->Gpe1BlockLength - 1,
1849 		       fadt->Gpe1Base);
1850 	if (fadt->CstControl != 0)
1851 		printf("\tCST_CNT=0x%x\n", fadt->CstControl);
1852 	printf("\tP_LVL2_LAT=%d us, P_LVL3_LAT=%d us\n",
1853 	       fadt->C2Latency, fadt->C3Latency);
1854 	printf("\tFLUSH_SIZE=%d, FLUSH_STRIDE=%d\n",
1855 	       fadt->FlushSize, fadt->FlushStride);
1856 	printf("\tDUTY_OFFSET=%d, DUTY_WIDTH=%d\n",
1857 	       fadt->DutyOffset, fadt->DutyWidth);
1858 	printf("\tDAY_ALRM=%d, MON_ALRM=%d, CENTURY=%d\n",
1859 	       fadt->DayAlarm, fadt->MonthAlarm, fadt->Century);
1860 
1861 #define PRINTFLAG(var, flag)	printflag((var), ACPI_FADT_## flag, #flag)
1862 
1863 	printf("\tIAPC_BOOT_ARCH=");
1864 	PRINTFLAG(fadt->BootFlags, LEGACY_DEVICES);
1865 	PRINTFLAG(fadt->BootFlags, 8042);
1866 	PRINTFLAG(fadt->BootFlags, NO_VGA);
1867 	PRINTFLAG(fadt->BootFlags, NO_MSI);
1868 	PRINTFLAG(fadt->BootFlags, NO_ASPM);
1869 	PRINTFLAG(fadt->BootFlags, NO_CMOS_RTC);
1870 	PRINTFLAG_END();
1871 
1872 	printf("\tFlags=");
1873 	PRINTFLAG(fadt->Flags, WBINVD);
1874 	PRINTFLAG(fadt->Flags, WBINVD_FLUSH);
1875 	PRINTFLAG(fadt->Flags, C1_SUPPORTED);
1876 	PRINTFLAG(fadt->Flags, C2_MP_SUPPORTED);
1877 	PRINTFLAG(fadt->Flags, POWER_BUTTON);
1878 	PRINTFLAG(fadt->Flags, SLEEP_BUTTON);
1879 	PRINTFLAG(fadt->Flags, FIXED_RTC);
1880 	PRINTFLAG(fadt->Flags, S4_RTC_WAKE);
1881 	PRINTFLAG(fadt->Flags, 32BIT_TIMER);
1882 	PRINTFLAG(fadt->Flags, DOCKING_SUPPORTED);
1883 	PRINTFLAG(fadt->Flags, RESET_REGISTER);
1884 	PRINTFLAG(fadt->Flags, SEALED_CASE);
1885 	PRINTFLAG(fadt->Flags, HEADLESS);
1886 	PRINTFLAG(fadt->Flags, SLEEP_TYPE);
1887 	PRINTFLAG(fadt->Flags, PCI_EXPRESS_WAKE);
1888 	PRINTFLAG(fadt->Flags, PLATFORM_CLOCK);
1889 	PRINTFLAG(fadt->Flags, S4_RTC_VALID);
1890 	PRINTFLAG(fadt->Flags, REMOTE_POWER_ON);
1891 	PRINTFLAG(fadt->Flags, APIC_CLUSTER);
1892 	PRINTFLAG(fadt->Flags, APIC_PHYSICAL);
1893 	PRINTFLAG(fadt->Flags, HW_REDUCED);
1894 	PRINTFLAG(fadt->Flags, LOW_POWER_S0);
1895 	PRINTFLAG_END();
1896 
1897 #undef PRINTFLAG
1898 
1899 	if (fadt->Flags & ACPI_FADT_RESET_REGISTER) {
1900 		printf("\tRESET_REG=");
1901 		acpi_print_gas(&fadt->ResetRegister);
1902 		printf(", RESET_VALUE=%#x\n", fadt->ResetValue);
1903 	}
1904 	if (acpi_get_fadt_revision(fadt) > 1) {
1905 		printf("\tX_FACS=0x%016jx, ", (uintmax_t)fadt->XFacs);
1906 		printf("X_DSDT=0x%016jx\n", (uintmax_t)fadt->XDsdt);
1907 		printf("\tX_PM1a_EVT_BLK=");
1908 		acpi_print_gas(&fadt->XPm1aEventBlock);
1909 		if (fadt->XPm1bEventBlock.Address != 0) {
1910 			printf("\n\tX_PM1b_EVT_BLK=");
1911 			acpi_print_gas(&fadt->XPm1bEventBlock);
1912 		}
1913 		printf("\n\tX_PM1a_CNT_BLK=");
1914 		acpi_print_gas(&fadt->XPm1aControlBlock);
1915 		if (fadt->XPm1bControlBlock.Address != 0) {
1916 			printf("\n\tX_PM1b_CNT_BLK=");
1917 			acpi_print_gas(&fadt->XPm1bControlBlock);
1918 		}
1919 		if (fadt->XPm2ControlBlock.Address != 0) {
1920 			printf("\n\tX_PM2_CNT_BLK=");
1921 			acpi_print_gas(&fadt->XPm2ControlBlock);
1922 		}
1923 		printf("\n\tX_PM_TMR_BLK=");
1924 		acpi_print_gas(&fadt->XPmTimerBlock);
1925 		if (fadt->XGpe0Block.Address != 0) {
1926 			printf("\n\tX_GPE0_BLK=");
1927 			acpi_print_gas(&fadt->XGpe0Block);
1928 		}
1929 		if (fadt->XGpe1Block.Address != 0) {
1930 			printf("\n\tX_GPE1_BLK=");
1931 			acpi_print_gas(&fadt->XGpe1Block);
1932 		}
1933 		printf("\n");
1934 	}
1935 
1936 	printf(END_COMMENT);
1937 }
1938 
1939 static void
1940 acpi_print_facs(ACPI_TABLE_FACS *facs)
1941 {
1942 	printf(BEGIN_COMMENT);
1943 	printf("  FACS:\tLength=%u, ", facs->Length);
1944 	printf("HwSig=0x%08x, ", facs->HardwareSignature);
1945 	printf("Firm_Wake_Vec=0x%08x\n", facs->FirmwareWakingVector);
1946 
1947 	printf("\tGlobal_Lock=");
1948 	if (facs->GlobalLock != 0) {
1949 		if (facs->GlobalLock & ACPI_GLOCK_PENDING)
1950 			printf("PENDING,");
1951 		if (facs->GlobalLock & ACPI_GLOCK_OWNED)
1952 			printf("OWNED");
1953 	}
1954 	printf("\n");
1955 
1956 	printf("\tFlags=");
1957 	if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT)
1958 		printf("S4BIOS");
1959 	printf("\n");
1960 
1961 	if (facs->XFirmwareWakingVector != 0)
1962 		printf("\tX_Firm_Wake_Vec=%016jx\n",
1963 		    (uintmax_t)facs->XFirmwareWakingVector);
1964 	printf("\tVersion=%u\n", facs->Version);
1965 
1966 	printf(END_COMMENT);
1967 }
1968 
1969 static void
1970 acpi_print_dsdt(ACPI_TABLE_HEADER *dsdp)
1971 {
1972 	printf(BEGIN_COMMENT);
1973 	acpi_print_sdt(dsdp);
1974 	printf(END_COMMENT);
1975 }
1976 
1977 int
1978 acpi_checksum(void *p, size_t length)
1979 {
1980 	uint8_t *bp;
1981 	uint8_t sum;
1982 
1983 	bp = p;
1984 	sum = 0;
1985 	while (length--)
1986 		sum += *bp++;
1987 
1988 	return (sum);
1989 }
1990 
1991 static ACPI_TABLE_HEADER *
1992 acpi_map_sdt(vm_offset_t pa)
1993 {
1994 	ACPI_TABLE_HEADER *sp;
1995 
1996 	sp = acpi_map_physical(pa, sizeof(ACPI_TABLE_HEADER));
1997 	sp = acpi_map_physical(pa, sp->Length);
1998 	return (sp);
1999 }
2000 
2001 static void
2002 acpi_print_rsd_ptr(ACPI_TABLE_RSDP *rp)
2003 {
2004 	printf(BEGIN_COMMENT);
2005 	printf("  RSD PTR: OEM=");
2006 	acpi_print_string(rp->OemId, ACPI_OEM_ID_SIZE);
2007 	printf(", ACPI_Rev=%s (%d)\n", rp->Revision < 2 ? "1.0x" : "2.0x",
2008 	       rp->Revision);
2009 	if (rp->Revision < 2) {
2010 		printf("\tRSDT=0x%08x, cksum=%u\n", rp->RsdtPhysicalAddress,
2011 		    rp->Checksum);
2012 	} else {
2013 		printf("\tXSDT=0x%016jx, length=%u, cksum=%u\n",
2014 		    (uintmax_t)rp->XsdtPhysicalAddress, rp->Length,
2015 		    rp->ExtendedChecksum);
2016 	}
2017 	printf(END_COMMENT);
2018 }
2019 
2020 static void
2021 acpi_handle_rsdt(ACPI_TABLE_HEADER *rsdp)
2022 {
2023 	ACPI_TABLE_HEADER *sdp;
2024 	ACPI_TABLE_RSDT *rsdt;
2025 	ACPI_TABLE_XSDT *xsdt;
2026 	vm_offset_t addr;
2027 	int entries, i;
2028 
2029 	acpi_print_rsdt(rsdp);
2030 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2031 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2032 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2033 	for (i = 0; i < entries; i++) {
2034 		if (addr_size == 4)
2035 			addr = le32toh(rsdt->TableOffsetEntry[i]);
2036 		else
2037 			addr = le64toh(xsdt->TableOffsetEntry[i]);
2038 		if (addr == 0)
2039 			continue;
2040 		sdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
2041 		if (acpi_checksum(sdp, sdp->Length)) {
2042 			warnx("RSDT entry %d (sig %.4s) is corrupt", i,
2043 			    sdp->Signature);
2044 			continue;
2045 		}
2046 		if (!memcmp(sdp->Signature, ACPI_SIG_BERT, 4))
2047 			acpi_handle_bert(sdp);
2048 		else if (!memcmp(sdp->Signature, ACPI_SIG_EINJ, 4))
2049 			acpi_handle_einj(sdp);
2050 		else if (!memcmp(sdp->Signature, ACPI_SIG_ERST, 4))
2051 			acpi_handle_erst(sdp);
2052 		else if (!memcmp(sdp->Signature, ACPI_SIG_FADT, 4))
2053 			acpi_handle_fadt(sdp);
2054 		else if (!memcmp(sdp->Signature, ACPI_SIG_MADT, 4))
2055 			acpi_handle_madt(sdp);
2056 		else if (!memcmp(sdp->Signature, ACPI_SIG_HEST, 4))
2057 			acpi_handle_hest(sdp);
2058 		else if (!memcmp(sdp->Signature, ACPI_SIG_HPET, 4))
2059 			acpi_handle_hpet(sdp);
2060 		else if (!memcmp(sdp->Signature, ACPI_SIG_ECDT, 4))
2061 			acpi_handle_ecdt(sdp);
2062 		else if (!memcmp(sdp->Signature, ACPI_SIG_MCFG, 4))
2063 			acpi_handle_mcfg(sdp);
2064 		else if (!memcmp(sdp->Signature, ACPI_SIG_SLIT, 4))
2065 			acpi_handle_slit(sdp);
2066 		else if (!memcmp(sdp->Signature, ACPI_SIG_SRAT, 4))
2067 			acpi_handle_srat(sdp);
2068 		else if (!memcmp(sdp->Signature, ACPI_SIG_TCPA, 4))
2069 			acpi_handle_tcpa(sdp);
2070 		else if (!memcmp(sdp->Signature, ACPI_SIG_DMAR, 4))
2071 			acpi_handle_dmar(sdp);
2072 		else if (!memcmp(sdp->Signature, ACPI_SIG_NFIT, 4))
2073 			acpi_handle_nfit(sdp);
2074 		else if (!memcmp(sdp->Signature, ACPI_SIG_WDDT, 4))
2075 			acpi_handle_wddt(sdp);
2076 		else if (!memcmp(sdp->Signature, ACPI_SIG_LPIT, 4))
2077 			acpi_handle_lpit(sdp);
2078 		else if (!memcmp(sdp->Signature, ACPI_SIG_TPM2, 4))
2079 			acpi_handle_tpm2(sdp);
2080 		else {
2081 			printf(BEGIN_COMMENT);
2082 			acpi_print_sdt(sdp);
2083 			printf(END_COMMENT);
2084 		}
2085 	}
2086 }
2087 
2088 ACPI_TABLE_HEADER *
2089 sdt_load_devmem(void)
2090 {
2091 	ACPI_TABLE_RSDP *rp;
2092 	ACPI_TABLE_HEADER *rsdp;
2093 
2094 	rp = acpi_find_rsd_ptr();
2095 	if (!rp)
2096 		errx(1, "Can't find ACPI information");
2097 
2098 	if (tflag)
2099 		acpi_print_rsd_ptr(rp);
2100 	if (rp->Revision < 2) {
2101 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->RsdtPhysicalAddress);
2102 		if (memcmp(rsdp->Signature, "RSDT", 4) != 0 ||
2103 		    acpi_checksum(rsdp, rsdp->Length) != 0)
2104 			errx(1, "RSDT is corrupted");
2105 		addr_size = sizeof(uint32_t);
2106 	} else {
2107 		rsdp = (ACPI_TABLE_HEADER *)acpi_map_sdt(rp->XsdtPhysicalAddress);
2108 		if (memcmp(rsdp->Signature, "XSDT", 4) != 0 ||
2109 		    acpi_checksum(rsdp, rsdp->Length) != 0)
2110 			errx(1, "XSDT is corrupted");
2111 		addr_size = sizeof(uint64_t);
2112 	}
2113 	return (rsdp);
2114 }
2115 
2116 /* Write the DSDT to a file, concatenating any SSDTs (if present). */
2117 static int
2118 write_dsdt(int fd, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdt)
2119 {
2120 	ACPI_TABLE_HEADER sdt;
2121 	ACPI_TABLE_HEADER *ssdt;
2122 	uint8_t sum;
2123 
2124 	/* Create a new checksum to account for the DSDT and any SSDTs. */
2125 	sdt = *dsdt;
2126 	if (rsdt != NULL) {
2127 		sdt.Checksum = 0;
2128 		sum = acpi_checksum(dsdt + 1, dsdt->Length -
2129 		    sizeof(ACPI_TABLE_HEADER));
2130 		ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, NULL);
2131 		while (ssdt != NULL) {
2132 			sdt.Length += ssdt->Length - sizeof(ACPI_TABLE_HEADER);
2133 			sum += acpi_checksum(ssdt + 1,
2134 			    ssdt->Length - sizeof(ACPI_TABLE_HEADER));
2135 			ssdt = sdt_from_rsdt(rsdt, ACPI_SIG_SSDT, ssdt);
2136 		}
2137 		sum += acpi_checksum(&sdt, sizeof(ACPI_TABLE_HEADER));
2138 		sdt.Checksum -= sum;
2139 	}
2140 
2141 	/* Write out the DSDT header and body. */
2142 	write(fd, &sdt, sizeof(ACPI_TABLE_HEADER));
2143 	write(fd, dsdt + 1, dsdt->Length - sizeof(ACPI_TABLE_HEADER));
2144 
2145 	/* Write out any SSDTs (if present.) */
2146 	if (rsdt != NULL) {
2147 		ssdt = sdt_from_rsdt(rsdt, "SSDT", NULL);
2148 		while (ssdt != NULL) {
2149 			write(fd, ssdt + 1, ssdt->Length -
2150 			    sizeof(ACPI_TABLE_HEADER));
2151 			ssdt = sdt_from_rsdt(rsdt, "SSDT", ssdt);
2152 		}
2153 	}
2154 	return (0);
2155 }
2156 
2157 void
2158 dsdt_save_file(char *outfile, ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2159 {
2160 	int	fd;
2161 	mode_t	mode;
2162 
2163 	assert(outfile != NULL);
2164 	mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
2165 	fd = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, mode);
2166 	if (fd == -1) {
2167 		perror("dsdt_save_file");
2168 		return;
2169 	}
2170 	write_dsdt(fd, rsdt, dsdp);
2171 	close(fd);
2172 }
2173 
2174 void
2175 aml_disassemble(ACPI_TABLE_HEADER *rsdt, ACPI_TABLE_HEADER *dsdp)
2176 {
2177 	char buf[PATH_MAX], tmpstr[PATH_MAX], wrkdir[PATH_MAX];
2178 	const char *iname = "/acpdump.din";
2179 	const char *oname = "/acpdump.dsl";
2180 	const char *tmpdir;
2181 	FILE *fp;
2182 	size_t len;
2183 	int fd, status;
2184 	pid_t pid;
2185 
2186 	tmpdir = getenv("TMPDIR");
2187 	if (tmpdir == NULL)
2188 		tmpdir = _PATH_TMP;
2189 	if (realpath(tmpdir, buf) == NULL) {
2190 		perror("realpath tmp dir");
2191 		return;
2192 	}
2193 	len = sizeof(wrkdir) - strlen(iname);
2194 	if ((size_t)snprintf(wrkdir, len, "%s/acpidump.XXXXXX", buf) > len-1 ) {
2195 		fprintf(stderr, "$TMPDIR too long\n");
2196 		return;
2197 	}
2198 	if  (mkdtemp(wrkdir) == NULL) {
2199 		perror("mkdtemp tmp working dir");
2200 		return;
2201 	}
2202 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, iname);
2203 	assert(len <= sizeof(tmpstr) - 1);
2204 	fd = open(tmpstr, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR);
2205 	if (fd < 0) {
2206 		perror("iasl tmp file");
2207 		return;
2208 	}
2209 	write_dsdt(fd, rsdt, dsdp);
2210 	close(fd);
2211 
2212 	/* Run iasl -d on the temp file */
2213 	if ((pid = fork()) == 0) {
2214 		close(STDOUT_FILENO);
2215 		if (vflag == 0)
2216 			close(STDERR_FILENO);
2217 		execl("/usr/sbin/iasl", "iasl", "-d", tmpstr, NULL);
2218 		err(1, "exec");
2219 	}
2220 	if (pid > 0)
2221 		wait(&status);
2222 	if (unlink(tmpstr) < 0) {
2223 		perror("unlink");
2224 		goto out;
2225 	}
2226 	if (pid < 0) {
2227 		perror("fork");
2228 		goto out;
2229 	}
2230 	if (status != 0) {
2231 		fprintf(stderr, "iast exit status = %d\n", status);
2232 	}
2233 
2234 	/* Dump iasl's output to stdout */
2235 	len = (size_t)snprintf(tmpstr, sizeof(tmpstr), "%s%s", wrkdir, oname);
2236 	assert(len <= sizeof(tmpstr) - 1);
2237 	fp = fopen(tmpstr, "r");
2238 	if (unlink(tmpstr) < 0) {
2239 		perror("unlink");
2240 		goto out;
2241 	}
2242 	if (fp == NULL) {
2243 		perror("iasl tmp file (read)");
2244 		goto out;
2245 	}
2246 	while ((len = fread(buf, 1, sizeof(buf), fp)) > 0)
2247 		fwrite(buf, 1, len, stdout);
2248 	fclose(fp);
2249 
2250     out:
2251 	if (rmdir(wrkdir) < 0)
2252 		perror("rmdir");
2253 }
2254 
2255 void
2256 sdt_print_all(ACPI_TABLE_HEADER *rsdp)
2257 {
2258 	acpi_handle_rsdt(rsdp);
2259 }
2260 
2261 /* Fetch a table matching the given signature via the RSDT. */
2262 ACPI_TABLE_HEADER *
2263 sdt_from_rsdt(ACPI_TABLE_HEADER *rsdp, const char *sig, ACPI_TABLE_HEADER *last)
2264 {
2265 	ACPI_TABLE_HEADER *sdt;
2266 	ACPI_TABLE_RSDT *rsdt;
2267 	ACPI_TABLE_XSDT *xsdt;
2268 	vm_offset_t addr;
2269 	int entries, i;
2270 
2271 	rsdt = (ACPI_TABLE_RSDT *)rsdp;
2272 	xsdt = (ACPI_TABLE_XSDT *)rsdp;
2273 	entries = (rsdp->Length - sizeof(ACPI_TABLE_HEADER)) / addr_size;
2274 	for (i = 0; i < entries; i++) {
2275 		if (addr_size == 4)
2276 			addr = le32toh(rsdt->TableOffsetEntry[i]);
2277 		else
2278 			addr = le64toh(xsdt->TableOffsetEntry[i]);
2279 		if (addr == 0)
2280 			continue;
2281 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(addr);
2282 		if (last != NULL) {
2283 			if (sdt == last)
2284 				last = NULL;
2285 			continue;
2286 		}
2287 		if (memcmp(sdt->Signature, sig, strlen(sig)))
2288 			continue;
2289 		if (acpi_checksum(sdt, sdt->Length))
2290 			errx(1, "RSDT entry %d is corrupt", i);
2291 		return (sdt);
2292 	}
2293 
2294 	return (NULL);
2295 }
2296 
2297 ACPI_TABLE_HEADER *
2298 dsdt_from_fadt(ACPI_TABLE_FADT *fadt)
2299 {
2300 	ACPI_TABLE_HEADER	*sdt;
2301 
2302 	/* Use the DSDT address if it is version 1, otherwise use XDSDT. */
2303 	if (acpi_get_fadt_revision(fadt) == 1)
2304 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->Dsdt);
2305 	else
2306 		sdt = (ACPI_TABLE_HEADER *)acpi_map_sdt(fadt->XDsdt);
2307 	if (acpi_checksum(sdt, sdt->Length))
2308 		errx(1, "DSDT is corrupt\n");
2309 	return (sdt);
2310 }
2311