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