xref: /freebsd/usr.sbin/acpi/acpidump/acpidump.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 
29 #include <sys/param.h>
30 #include <assert.h>
31 #include <err.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include "acpidump.h"
38 
39 int	dflag;	/* Disassemble AML using iasl(8) */
40 int	tflag;	/* Dump contents of SDT tables */
41 int	vflag;	/* Use verbose messages */
42 
43 static void
44 usage(const char *progname)
45 {
46 
47 	fprintf(stderr, "usage: %s [-d] [-t] [-h] [-v] [-f dsdt_input] "
48 			"[-o dsdt_output]\n", progname);
49 	fprintf(stderr, "To send ASL:\n\t%s -dt | gzip -c9 > foo.asl.gz\n",
50 	    progname);
51 	exit(1);
52 }
53 
54 int
55 main(int argc, char *argv[])
56 {
57 	ACPI_TABLE_HEADER *rsdt, *sdt;
58 	int	c;
59 	char	*progname;
60 	char	*dsdt_input_file, *dsdt_output_file;
61 
62 	dsdt_input_file = dsdt_output_file = NULL;
63 	progname = argv[0];
64 
65 	if (argc < 2)
66 		usage(progname);
67 
68 	while ((c = getopt(argc, argv, "dhtvf:o:")) != -1) {
69 		switch (c) {
70 		case 'd':
71 			dflag = 1;
72 			break;
73 		case 't':
74 			tflag = 1;
75 			break;
76 		case 'v':
77 			vflag = 1;
78 			break;
79 		case 'f':
80 			dsdt_input_file = optarg;
81 			break;
82 		case 'o':
83 			dsdt_output_file = optarg;
84 			break;
85 		case 'h':
86 		default:
87 			usage(progname);
88 			/* NOTREACHED */
89 		}
90 	}
91 	argc -= optind;
92 	argv += optind;
93 
94 	/* Get input either from file or /dev/mem */
95 	if (dsdt_input_file != NULL) {
96 		if (dflag == 0 && tflag == 0) {
97 			warnx("Need to specify -d or -t with DSDT input file");
98 			usage(progname);
99 		} else if (tflag != 0) {
100 			warnx("Can't use -t with DSDT input file");
101 			usage(progname);
102 		}
103 		if (vflag)
104 			warnx("loading DSDT file: %s", dsdt_input_file);
105 		rsdt = dsdt_load_file(dsdt_input_file);
106 	} else {
107 		if (vflag)
108 			warnx("loading RSD PTR from /dev/mem");
109 		rsdt = sdt_load_devmem();
110 	}
111 
112 	/* Display misc. SDT tables (only available when using /dev/mem) */
113 	if (tflag) {
114 		if (vflag)
115 			warnx("printing various SDT tables");
116 		sdt_print_all(rsdt);
117 	}
118 
119 	/* Translate RSDT to DSDT pointer */
120 	if (dsdt_input_file == NULL) {
121 		sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
122 		sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
123 	} else {
124 		sdt = rsdt;
125 		rsdt = NULL;
126 	}
127 
128 	/* Dump the DSDT and SSDTs to a file */
129 	if (dsdt_output_file != NULL) {
130 		if (vflag)
131 			warnx("saving DSDT file: %s", dsdt_output_file);
132 		dsdt_save_file(dsdt_output_file, rsdt, sdt);
133 	}
134 
135 	/* Disassemble the DSDT into ASL */
136 	if (dflag) {
137 		if (vflag)
138 			warnx("disassembling DSDT, iasl messages follow");
139 		aml_disassemble(rsdt, sdt);
140 		if (vflag)
141 			warnx("iasl processing complete");
142 	}
143 
144 	exit(0);
145 }
146