xref: /netbsd-src/usr.sbin/acpitools/acpidump/acpidump.c (revision 161b30af53dfaf1375e7d54bc282eb25c6a2f685)
1*161b30afSjmcneill /* $NetBSD: acpidump.c,v 1.8 2020/12/06 02:57:30 jmcneill Exp $ */
253e202c1Schristos 
353e202c1Schristos /*-
453e202c1Schristos  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
553e202c1Schristos  * All rights reserved.
653e202c1Schristos  *
753e202c1Schristos  * Redistribution and use in source and binary forms, with or without
853e202c1Schristos  * modification, are permitted provided that the following conditions
953e202c1Schristos  * are met:
1053e202c1Schristos  * 1. Redistributions of source code must retain the above copyright
1153e202c1Schristos  *    notice, this list of conditions and the following disclaimer.
1253e202c1Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1353e202c1Schristos  *    notice, this list of conditions and the following disclaimer in the
1453e202c1Schristos  *    documentation and/or other materials provided with the distribution.
1553e202c1Schristos  *
1653e202c1Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1753e202c1Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1853e202c1Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1953e202c1Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2053e202c1Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2153e202c1Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2253e202c1Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2353e202c1Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2453e202c1Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2553e202c1Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2653e202c1Schristos  * SUCH DAMAGE.
2753e202c1Schristos  *
28c359a213Smsaitoh  *	$FreeBSD: head/usr.sbin/acpi/acpidump/acpidump.c 302788 2016-07-13 22:53:30Z andrew $
2953e202c1Schristos  */
303b140d48Scegger 
3153e202c1Schristos #include <sys/cdefs.h>
32*161b30afSjmcneill __RCSID("$NetBSD: acpidump.c,v 1.8 2020/12/06 02:57:30 jmcneill Exp $");
333b140d48Scegger 
3453e202c1Schristos 
3553e202c1Schristos #include <sys/param.h>
3653e202c1Schristos #include <assert.h>
3753e202c1Schristos #include <err.h>
3853e202c1Schristos #include <stdio.h>
3953e202c1Schristos #include <stdlib.h>
4053e202c1Schristos #include <string.h>
413b140d48Scegger #include <unistd.h>
4253e202c1Schristos 
4353e202c1Schristos #include "acpidump.h"
4453e202c1Schristos 
45d34a996fSjmcneill int	cflag;	/* Dump unknown table data as characters */
463b140d48Scegger int	dflag;	/* Disassemble AML using iasl(8) */
47d34a996fSjmcneill int	sflag;	/* Skip tables with bad checksums */
483b140d48Scegger int	tflag;	/* Dump contents of SDT tables */
493b140d48Scegger int	vflag;	/* Use verbose messages */
5053e202c1Schristos 
51dccf569eSjoerg __dead static void
usage(void)523b140d48Scegger usage(void)
5353e202c1Schristos {
543b140d48Scegger 	const char *progname = getprogname();
5553e202c1Schristos 
56c20b084dSwiz 	fprintf(stderr, "usage: %s [-cdhstv] "
57d34a996fSjmcneill 			"[-f dsdt_input] [-o dsdt_output]\n", progname);
583b140d48Scegger 	fprintf(stderr, "To send ASL:\n\t%s -dt | gzip -c9 > foo.asl.gz\n",
593b140d48Scegger 	    progname);
603b140d48Scegger 	exit(EXIT_FAILURE);
6153e202c1Schristos }
6253e202c1Schristos 
6353e202c1Schristos int
main(int argc,char * argv[])6453e202c1Schristos main(int argc, char *argv[])
6553e202c1Schristos {
663b140d48Scegger 	ACPI_TABLE_HEADER *rsdt, *sdt;
67c359a213Smsaitoh 	int	c;
683b140d48Scegger 	char	*dsdt_input_file, *dsdt_output_file;
6953e202c1Schristos 
703b140d48Scegger 	dsdt_input_file = dsdt_output_file = NULL;
713b140d48Scegger 
723b140d48Scegger 	if (argc < 2)
733b140d48Scegger 		usage();
743b140d48Scegger 
75c20b084dSwiz 	while ((c = getopt(argc, argv, "cdhtsvf:o:")) != -1) {
7653e202c1Schristos 		switch (c) {
77d91f7d52Sjmcneill 		case 'c':
78d91f7d52Sjmcneill 			cflag = 1;
79d91f7d52Sjmcneill 			break;
803b140d48Scegger 		case 'd':
813b140d48Scegger 			dflag = 1;
823b140d48Scegger 			break;
83d34a996fSjmcneill 		case 's':
84d34a996fSjmcneill 			sflag = 1;
85d34a996fSjmcneill 			break;
863b140d48Scegger 		case 't':
873b140d48Scegger 			tflag = 1;
883b140d48Scegger 			break;
893b140d48Scegger 		case 'v':
903b140d48Scegger 			vflag = 1;
913b140d48Scegger 			break;
9253e202c1Schristos 		case 'f':
933b140d48Scegger 			dsdt_input_file = optarg;
943b140d48Scegger 			break;
9553e202c1Schristos 		case 'o':
963b140d48Scegger 			dsdt_output_file = optarg;
9753e202c1Schristos 			break;
9853e202c1Schristos 		case 'h':
9953e202c1Schristos 		default:
1003b140d48Scegger 			usage();
1013b140d48Scegger 			/* NOTREACHED */
1023b140d48Scegger 		}
1033b140d48Scegger 	}
10453e202c1Schristos 	argc -= optind;
10553e202c1Schristos 	argv += optind;
1063b140d48Scegger 
107*161b30afSjmcneill 	/* Get input either from file or /dev/acpi */
1083b140d48Scegger 	if (dsdt_input_file != NULL) {
1093b140d48Scegger 		if (dflag == 0 && tflag == 0) {
1103b140d48Scegger 			warnx("Need to specify -d or -t with DSDT input file");
1113b140d48Scegger 			usage();
1123b140d48Scegger 		} else if (tflag != 0) {
1133b140d48Scegger 			warnx("Can't use -t with DSDT input file");
1143b140d48Scegger 			usage();
11553e202c1Schristos 		}
1163b140d48Scegger 		if (vflag)
1173b140d48Scegger 			warnx("loading DSDT file: %s", dsdt_input_file);
1183b140d48Scegger 		rsdt = dsdt_load_file(dsdt_input_file);
1193b140d48Scegger 	} else {
1203b140d48Scegger 		if (vflag)
121*161b30afSjmcneill 			warnx("loading RSD PTR from /dev/acpi");
1223b140d48Scegger 		rsdt = sdt_load_devmem();
12353e202c1Schristos 	}
12453e202c1Schristos 
125*161b30afSjmcneill 	/* Display misc. SDT tables (only available when using /dev/acpi) */
1263b140d48Scegger 	if (tflag) {
1273b140d48Scegger 		if (vflag)
1283b140d48Scegger 			warnx("printing various SDT tables");
1293b140d48Scegger 		sdt_print_all(rsdt);
1303b140d48Scegger 	}
1313b140d48Scegger 
1323b140d48Scegger 	/* Translate RSDT to DSDT pointer */
1333b140d48Scegger 	if (dsdt_input_file == NULL) {
1343b140d48Scegger 		sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
1353b140d48Scegger 		sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
1363b140d48Scegger 	} else {
1373b140d48Scegger 		sdt = rsdt;
1383b140d48Scegger 		rsdt = NULL;
1393b140d48Scegger 	}
1403b140d48Scegger 
1413b140d48Scegger 	/* Dump the DSDT and SSDTs to a file */
1423b140d48Scegger 	if (dsdt_output_file != NULL) {
1433b140d48Scegger 		if (vflag)
1443b140d48Scegger 			warnx("saving DSDT file: %s", dsdt_output_file);
1453b140d48Scegger 		dsdt_save_file(dsdt_output_file, rsdt, sdt);
1463b140d48Scegger 	}
1473b140d48Scegger 
1483b140d48Scegger 	/* Disassemble the DSDT into ASL */
1493b140d48Scegger 	if (dflag) {
1503b140d48Scegger 		if (vflag)
1513b140d48Scegger 			warnx("disassembling DSDT, iasl messages follow");
1523b140d48Scegger 		aml_disassemble(rsdt, sdt);
1533b140d48Scegger 		if (vflag)
1543b140d48Scegger 			warnx("iasl processing complete");
1553b140d48Scegger 	}
1563b140d48Scegger 
1573b140d48Scegger 	exit(EXIT_SUCCESS);
15853e202c1Schristos }
159