acpidump.c revision 1.2 1 1.2 cegger /* $NetBSD: acpidump.c,v 1.2 2009/12/22 08:44:03 cegger Exp $ */
2 1.1 christos
3 1.1 christos /*-
4 1.1 christos * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki (at) FreeBSD.org>
5 1.1 christos * All rights reserved.
6 1.1 christos *
7 1.1 christos * Redistribution and use in source and binary forms, with or without
8 1.1 christos * modification, are permitted provided that the following conditions
9 1.1 christos * are met:
10 1.1 christos * 1. Redistributions of source code must retain the above copyright
11 1.1 christos * notice, this list of conditions and the following disclaimer.
12 1.1 christos * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 christos * notice, this list of conditions and the following disclaimer in the
14 1.1 christos * documentation and/or other materials provided with the distribution.
15 1.1 christos *
16 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 christos * SUCH DAMAGE.
27 1.1 christos *
28 1.2 cegger * $FreeBSD: src/usr.sbin/acpi/acpidump/acpidump.c,v 1.13 2009/08/25 20:35:57 jhb Exp $
29 1.1 christos */
30 1.2 cegger
31 1.1 christos #include <sys/cdefs.h>
32 1.2 cegger __RCSID("$NetBSD: acpidump.c,v 1.2 2009/12/22 08:44:03 cegger Exp $");
33 1.2 cegger
34 1.1 christos
35 1.1 christos #include <sys/param.h>
36 1.1 christos #include <assert.h>
37 1.1 christos #include <err.h>
38 1.1 christos #include <stdio.h>
39 1.1 christos #include <stdlib.h>
40 1.1 christos #include <string.h>
41 1.2 cegger #include <unistd.h>
42 1.1 christos
43 1.1 christos #include "acpidump.h"
44 1.1 christos
45 1.2 cegger int dflag; /* Disassemble AML using iasl(8) */
46 1.2 cegger int tflag; /* Dump contents of SDT tables */
47 1.2 cegger int vflag; /* Use verbose messages */
48 1.1 christos
49 1.1 christos static void
50 1.2 cegger usage(void)
51 1.1 christos {
52 1.2 cegger const char *progname = getprogname();
53 1.1 christos
54 1.2 cegger fprintf(stderr, "usage: %s [-d] [-t] [-h] [-v] [-f dsdt_input] "
55 1.2 cegger "[-o dsdt_output]\n", progname);
56 1.2 cegger fprintf(stderr, "To send ASL:\n\t%s -dt | gzip -c9 > foo.asl.gz\n",
57 1.2 cegger progname);
58 1.2 cegger exit(EXIT_FAILURE);
59 1.1 christos }
60 1.1 christos
61 1.2 cegger int
62 1.2 cegger main(int argc, char *argv[])
63 1.1 christos {
64 1.2 cegger ACPI_TABLE_HEADER *rsdt, *sdt;
65 1.2 cegger char c;
66 1.2 cegger char *dsdt_input_file, *dsdt_output_file;
67 1.1 christos
68 1.2 cegger dsdt_input_file = dsdt_output_file = NULL;
69 1.1 christos
70 1.2 cegger if (argc < 2)
71 1.2 cegger usage();
72 1.1 christos
73 1.2 cegger while ((c = getopt(argc, argv, "dhtvf:o:")) != -1) {
74 1.1 christos switch (c) {
75 1.2 cegger case 'd':
76 1.2 cegger dflag = 1;
77 1.2 cegger break;
78 1.2 cegger case 't':
79 1.2 cegger tflag = 1;
80 1.2 cegger break;
81 1.2 cegger case 'v':
82 1.2 cegger vflag = 1;
83 1.2 cegger break;
84 1.1 christos case 'f':
85 1.2 cegger dsdt_input_file = optarg;
86 1.2 cegger break;
87 1.1 christos case 'o':
88 1.2 cegger dsdt_output_file = optarg;
89 1.1 christos break;
90 1.1 christos case 'h':
91 1.1 christos default:
92 1.2 cegger usage();
93 1.2 cegger /* NOTREACHED */
94 1.2 cegger }
95 1.2 cegger }
96 1.2 cegger argc -= optind;
97 1.2 cegger argv += optind;
98 1.2 cegger
99 1.2 cegger /* Get input either from file or /dev/mem */
100 1.2 cegger if (dsdt_input_file != NULL) {
101 1.2 cegger if (dflag == 0 && tflag == 0) {
102 1.2 cegger warnx("Need to specify -d or -t with DSDT input file");
103 1.2 cegger usage();
104 1.2 cegger } else if (tflag != 0) {
105 1.2 cegger warnx("Can't use -t with DSDT input file");
106 1.2 cegger usage();
107 1.1 christos }
108 1.2 cegger if (vflag)
109 1.2 cegger warnx("loading DSDT file: %s", dsdt_input_file);
110 1.2 cegger rsdt = dsdt_load_file(dsdt_input_file);
111 1.2 cegger } else {
112 1.2 cegger if (vflag)
113 1.2 cegger warnx("loading RSD PTR from /dev/mem");
114 1.2 cegger rsdt = sdt_load_devmem();
115 1.2 cegger }
116 1.2 cegger
117 1.2 cegger /* Display misc. SDT tables (only available when using /dev/mem) */
118 1.2 cegger if (tflag) {
119 1.2 cegger if (vflag)
120 1.2 cegger warnx("printing various SDT tables");
121 1.2 cegger sdt_print_all(rsdt);
122 1.2 cegger }
123 1.2 cegger
124 1.2 cegger /* Translate RSDT to DSDT pointer */
125 1.2 cegger if (dsdt_input_file == NULL) {
126 1.2 cegger sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
127 1.2 cegger sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
128 1.2 cegger } else {
129 1.2 cegger sdt = rsdt;
130 1.2 cegger rsdt = NULL;
131 1.2 cegger }
132 1.2 cegger
133 1.2 cegger /* Dump the DSDT and SSDTs to a file */
134 1.2 cegger if (dsdt_output_file != NULL) {
135 1.2 cegger if (vflag)
136 1.2 cegger warnx("saving DSDT file: %s", dsdt_output_file);
137 1.2 cegger dsdt_save_file(dsdt_output_file, rsdt, sdt);
138 1.2 cegger }
139 1.2 cegger
140 1.2 cegger /* Disassemble the DSDT into ASL */
141 1.2 cegger if (dflag) {
142 1.2 cegger if (vflag)
143 1.2 cegger warnx("disassembling DSDT, iasl messages follow");
144 1.2 cegger aml_disassemble(rsdt, sdt);
145 1.2 cegger if (vflag)
146 1.2 cegger warnx("iasl processing complete");
147 1.1 christos }
148 1.1 christos
149 1.2 cegger exit(EXIT_SUCCESS);
150 1.1 christos }
151