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