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