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