xref: /freebsd/usr.sbin/acpi/acpidump/acpidump.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
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, "dhtvsf: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 's':
86 			dflag = 2;
87 			break;
88 		case 'h':
89 		default:
90 			usage(progname);
91 			/* NOTREACHED */
92 		}
93 	}
94 	argc -= optind;
95 	argv += optind;
96 
97 	/* Get input either from file or /dev/mem */
98 	if (dsdt_input_file != NULL) {
99 		if (dflag == 0 && tflag == 0) {
100 			warnx("Need to specify -d or -t with DSDT input file");
101 			usage(progname);
102 		} else if (tflag != 0) {
103 			warnx("Can't use -t with DSDT input file");
104 			usage(progname);
105 		}
106 		if (vflag)
107 			warnx("loading DSDT file: %s", dsdt_input_file);
108 		rsdt = dsdt_load_file(dsdt_input_file);
109 	} else {
110 		if (vflag)
111 			warnx("loading RSD PTR from /dev/mem");
112 		rsdt = sdt_load_devmem();
113 	}
114 
115 	/* Display misc. SDT tables (only available when using /dev/mem) */
116 	if (tflag) {
117 		if (vflag)
118 			warnx("printing various SDT tables");
119 		sdt_print_all(rsdt);
120 	}
121 
122 	/* Translate RSDT to DSDT pointer */
123 	if (dsdt_input_file == NULL) {
124 		sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
125 		sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
126 	} else {
127 		sdt = rsdt;
128 		rsdt = NULL;
129 	}
130 
131 	/* Dump the DSDT and SSDTs to a file */
132 	if (dsdt_output_file != NULL) {
133 		if (vflag)
134 			warnx("saving DSDT file: %s", dsdt_output_file);
135 		dsdt_save_file(dsdt_output_file, rsdt, sdt);
136 	}
137 
138 	/* Disassemble the DSDT into ASL */
139 	if (dflag) {
140 		if (vflag)
141 			warnx("disassembling DSDT, iasl messages follow");
142 		if (dflag == 1) {
143 			aml_disassemble(rsdt, sdt);
144 		} else {
145 			aml_disassemble_separate(rsdt, sdt);
146 		}
147 		if (vflag)
148 			warnx("iasl processing complete");
149 	}
150 
151 	exit(0);
152 }
153