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