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