xref: /freebsd/usr.sbin/acpi/acpidump/acpidump.c (revision dab59af3bcc7cb7ba01569d3044894b3e860ad56)
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] [-T table_name]\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 	char	*tbl = NULL;
62 
63 	dsdt_input_file = dsdt_output_file = NULL;
64 	progname = argv[0];
65 
66 	if (argc < 2)
67 		usage(progname);
68 
69 	while ((c = getopt(argc, argv, "df:ho:tT:vs")) != -1) {
70 		switch (c) {
71 		case 'd':
72 			dflag = 1;
73 			break;
74 		case 'T':
75 			tbl = optarg;
76 			if (strlen(tbl) != 4) {
77 				warnx("Illegal table name %s", tbl);
78 				usage(progname);
79 			}
80 			break;
81 		case 't':
82 			tflag = 1;
83 			break;
84 		case 'v':
85 			vflag = 1;
86 			break;
87 		case 'f':
88 			dsdt_input_file = optarg;
89 			break;
90 		case 'o':
91 			dsdt_output_file = optarg;
92 			break;
93 		case 's':
94 			dflag = 2;
95 			break;
96 		case 'h':
97 		default:
98 			usage(progname);
99 			/* NOTREACHED */
100 		}
101 	}
102 	argc -= optind;
103 	argv += optind;
104 
105 	/* Get input either from file or /dev/mem */
106 	if (dsdt_input_file != NULL) {
107 		if (dflag == 0 && tflag == 0) {
108 			warnx("Need to specify -d or -t with DSDT input file");
109 			usage(progname);
110 		} else if (tflag != 0) {
111 			warnx("Can't use -t with DSDT input file");
112 			usage(progname);
113 		}
114 		if (vflag)
115 			warnx("loading DSDT file: %s", dsdt_input_file);
116 		rsdt = dsdt_load_file(dsdt_input_file);
117 	} else {
118 		if (vflag)
119 			warnx("loading RSD PTR from /dev/mem");
120 		rsdt = sdt_load_devmem();
121 	}
122 
123 	/* Display misc. SDT tables (only available when using /dev/mem) */
124 	if (tflag || tbl != NULL) {
125 		if (vflag)
126 			warnx("printing various SDT tables");
127 		sdt_print_all(rsdt, tbl);
128 	}
129 
130 	/* Translate RSDT to DSDT pointer */
131 	if (dsdt_input_file == NULL) {
132 		sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
133 		sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
134 	} else {
135 		sdt = rsdt;
136 		rsdt = NULL;
137 	}
138 
139 	/* Dump the DSDT and SSDTs to a file */
140 	if (dsdt_output_file != NULL) {
141 		if (vflag)
142 			warnx("saving DSDT file: %s", dsdt_output_file);
143 		dsdt_save_file(dsdt_output_file, rsdt, sdt);
144 	}
145 
146 	/* Disassemble the DSDT into ASL */
147 	if (dflag) {
148 		if (vflag)
149 			warnx("disassembling DSDT, iasl messages follow");
150 		if (dflag == 1) {
151 			aml_disassemble(rsdt, sdt);
152 		} else {
153 			aml_disassemble_separate(rsdt, sdt);
154 		}
155 		if (vflag)
156 			warnx("iasl processing complete");
157 	}
158 
159 	exit(0);
160 }
161