acpidump.c revision 1.3 1 /* $NetBSD: acpidump.c,v 1.3 2011/02/17 02:55:16 jmcneill Exp $ */
2
3 /*-
4 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki (at) 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: src/usr.sbin/acpi/acpidump/acpidump.c,v 1.13 2009/08/25 20:35:57 jhb Exp $
29 */
30
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: acpidump.c,v 1.3 2011/02/17 02:55:16 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 dflag; /* Disassemble AML using iasl(8) */
46 int tflag; /* Dump contents of SDT tables */
47 int vflag; /* Use verbose messages */
48 int cflag; /* Dump unknown table data as characters */
49
50 static void
51 usage(void)
52 {
53 const char *progname = getprogname();
54
55 fprintf(stderr, "usage: %s [-c] [-d] [-t] [-h] [-v] [-f dsdt_input] "
56 "[-o dsdt_output]\n", progname);
57 fprintf(stderr, "To send ASL:\n\t%s -dt | gzip -c9 > foo.asl.gz\n",
58 progname);
59 exit(EXIT_FAILURE);
60 }
61
62 int
63 main(int argc, char *argv[])
64 {
65 ACPI_TABLE_HEADER *rsdt, *sdt;
66 char c;
67 char *dsdt_input_file, *dsdt_output_file;
68
69 dsdt_input_file = dsdt_output_file = NULL;
70
71 if (argc < 2)
72 usage();
73
74 while ((c = getopt(argc, argv, "cdhtvf:o:")) != -1) {
75 switch (c) {
76 case 'c':
77 cflag = 1;
78 break;
79 case 'd':
80 dflag = 1;
81 break;
82 case 't':
83 tflag = 1;
84 break;
85 case 'v':
86 vflag = 1;
87 break;
88 case 'f':
89 dsdt_input_file = optarg;
90 break;
91 case 'o':
92 dsdt_output_file = optarg;
93 break;
94 case 'h':
95 default:
96 usage();
97 /* NOTREACHED */
98 }
99 }
100 argc -= optind;
101 argv += optind;
102
103 /* Get input either from file or /dev/mem */
104 if (dsdt_input_file != NULL) {
105 if (dflag == 0 && tflag == 0) {
106 warnx("Need to specify -d or -t with DSDT input file");
107 usage();
108 } else if (tflag != 0) {
109 warnx("Can't use -t with DSDT input file");
110 usage();
111 }
112 if (vflag)
113 warnx("loading DSDT file: %s", dsdt_input_file);
114 rsdt = dsdt_load_file(dsdt_input_file);
115 } else {
116 if (vflag)
117 warnx("loading RSD PTR from /dev/mem");
118 rsdt = sdt_load_devmem();
119 }
120
121 /* Display misc. SDT tables (only available when using /dev/mem) */
122 if (tflag) {
123 if (vflag)
124 warnx("printing various SDT tables");
125 sdt_print_all(rsdt);
126 }
127
128 /* Translate RSDT to DSDT pointer */
129 if (dsdt_input_file == NULL) {
130 sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
131 sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
132 } else {
133 sdt = rsdt;
134 rsdt = NULL;
135 }
136
137 /* Dump the DSDT and SSDTs to a file */
138 if (dsdt_output_file != NULL) {
139 if (vflag)
140 warnx("saving DSDT file: %s", dsdt_output_file);
141 dsdt_save_file(dsdt_output_file, rsdt, sdt);
142 }
143
144 /* Disassemble the DSDT into ASL */
145 if (dflag) {
146 if (vflag)
147 warnx("disassembling DSDT, iasl messages follow");
148 aml_disassemble(rsdt, sdt);
149 if (vflag)
150 warnx("iasl processing complete");
151 }
152
153 exit(EXIT_SUCCESS);
154 }
155