xref: /netbsd-src/usr.sbin/acpitools/acpidump/acpidump.c (revision 161b30af53dfaf1375e7d54bc282eb25c6a2f685)
1 /* $NetBSD: acpidump.c,v 1.8 2020/12/06 02:57:30 jmcneill Exp $ */
2 
3 /*-
4  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	$FreeBSD: head/usr.sbin/acpi/acpidump/acpidump.c 302788 2016-07-13 22:53:30Z andrew $
29  */
30 
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: acpidump.c,v 1.8 2020/12/06 02:57:30 jmcneill Exp $");
33 
34 
35 #include <sys/param.h>
36 #include <assert.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include "acpidump.h"
44 
45 int	cflag;	/* Dump unknown table data as characters */
46 int	dflag;	/* Disassemble AML using iasl(8) */
47 int	sflag;	/* Skip tables with bad checksums */
48 int	tflag;	/* Dump contents of SDT tables */
49 int	vflag;	/* Use verbose messages */
50 
51 __dead static void
usage(void)52 usage(void)
53 {
54 	const char *progname = getprogname();
55 
56 	fprintf(stderr, "usage: %s [-cdhstv] "
57 			"[-f dsdt_input] [-o dsdt_output]\n", progname);
58 	fprintf(stderr, "To send ASL:\n\t%s -dt | gzip -c9 > foo.asl.gz\n",
59 	    progname);
60 	exit(EXIT_FAILURE);
61 }
62 
63 int
main(int argc,char * argv[])64 main(int argc, char *argv[])
65 {
66 	ACPI_TABLE_HEADER *rsdt, *sdt;
67 	int	c;
68 	char	*dsdt_input_file, *dsdt_output_file;
69 
70 	dsdt_input_file = dsdt_output_file = NULL;
71 
72 	if (argc < 2)
73 		usage();
74 
75 	while ((c = getopt(argc, argv, "cdhtsvf:o:")) != -1) {
76 		switch (c) {
77 		case 'c':
78 			cflag = 1;
79 			break;
80 		case 'd':
81 			dflag = 1;
82 			break;
83 		case 's':
84 			sflag = 1;
85 			break;
86 		case 't':
87 			tflag = 1;
88 			break;
89 		case 'v':
90 			vflag = 1;
91 			break;
92 		case 'f':
93 			dsdt_input_file = optarg;
94 			break;
95 		case 'o':
96 			dsdt_output_file = optarg;
97 			break;
98 		case 'h':
99 		default:
100 			usage();
101 			/* NOTREACHED */
102 		}
103 	}
104 	argc -= optind;
105 	argv += optind;
106 
107 	/* Get input either from file or /dev/acpi */
108 	if (dsdt_input_file != NULL) {
109 		if (dflag == 0 && tflag == 0) {
110 			warnx("Need to specify -d or -t with DSDT input file");
111 			usage();
112 		} else if (tflag != 0) {
113 			warnx("Can't use -t with DSDT input file");
114 			usage();
115 		}
116 		if (vflag)
117 			warnx("loading DSDT file: %s", dsdt_input_file);
118 		rsdt = dsdt_load_file(dsdt_input_file);
119 	} else {
120 		if (vflag)
121 			warnx("loading RSD PTR from /dev/acpi");
122 		rsdt = sdt_load_devmem();
123 	}
124 
125 	/* Display misc. SDT tables (only available when using /dev/acpi) */
126 	if (tflag) {
127 		if (vflag)
128 			warnx("printing various SDT tables");
129 		sdt_print_all(rsdt);
130 	}
131 
132 	/* Translate RSDT to DSDT pointer */
133 	if (dsdt_input_file == NULL) {
134 		sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
135 		sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
136 	} else {
137 		sdt = rsdt;
138 		rsdt = NULL;
139 	}
140 
141 	/* Dump the DSDT and SSDTs to a file */
142 	if (dsdt_output_file != NULL) {
143 		if (vflag)
144 			warnx("saving DSDT file: %s", dsdt_output_file);
145 		dsdt_save_file(dsdt_output_file, rsdt, sdt);
146 	}
147 
148 	/* Disassemble the DSDT into ASL */
149 	if (dflag) {
150 		if (vflag)
151 			warnx("disassembling DSDT, iasl messages follow");
152 		aml_disassemble(rsdt, sdt);
153 		if (vflag)
154 			warnx("iasl processing complete");
155 	}
156 
157 	exit(EXIT_SUCCESS);
158 }
159