165285Siwasaki/*-
265285Siwasaki * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
365285Siwasaki * All rights reserved.
465285Siwasaki *
565285Siwasaki * Redistribution and use in source and binary forms, with or without
665285Siwasaki * modification, are permitted provided that the following conditions
765285Siwasaki * are met:
865285Siwasaki * 1. Redistributions of source code must retain the above copyright
965285Siwasaki *    notice, this list of conditions and the following disclaimer.
1065285Siwasaki * 2. Redistributions in binary form must reproduce the above copyright
1165285Siwasaki *    notice, this list of conditions and the following disclaimer in the
1265285Siwasaki *    documentation and/or other materials provided with the distribution.
1365285Siwasaki *
1465285Siwasaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1565285Siwasaki * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1665285Siwasaki * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1765285Siwasaki * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1865285Siwasaki * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1965285Siwasaki * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2065285Siwasaki * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2165285Siwasaki * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2265285Siwasaki * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2365285Siwasaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2465285Siwasaki * SUCH DAMAGE.
2565285Siwasaki *
2665285Siwasaki *	$FreeBSD$
2765285Siwasaki */
2865285Siwasaki
2965285Siwasaki#include <sys/param.h>
3065285Siwasaki#include <assert.h>
3165285Siwasaki#include <err.h>
3265285Siwasaki#include <stdio.h>
33119515Snjl#include <stdlib.h>
34119515Snjl#include <string.h>
3565285Siwasaki#include <unistd.h>
3665285Siwasaki
3765285Siwasaki#include "acpidump.h"
3865285Siwasaki
39119515Snjlint	dflag;	/* Disassemble AML using iasl(8) */
40119515Snjlint	tflag;	/* Dump contents of SDT tables */
41119515Snjlint	vflag;	/* Use verbose messages */
4265285Siwasaki
4365285Siwasakistatic void
4465285Siwasakiusage(const char *progname)
4565285Siwasaki{
4665285Siwasaki
47136165Snjl	fprintf(stderr, "usage: %s [-d] [-t] [-h] [-v] [-f dsdt_input] "
48119515Snjl			"[-o dsdt_output]\n", progname);
49167579Snjl	fprintf(stderr, "To send ASL:\n\t%s -dt | gzip -c9 > foo.asl.gz\n",
50167579Snjl	    progname);
5165285Siwasaki	exit(1);
5265285Siwasaki}
5365285Siwasaki
5465285Siwasakiint
5565285Siwasakimain(int argc, char *argv[])
5665285Siwasaki{
57196555Sjhb	ACPI_TABLE_HEADER *rsdt, *sdt;
5865285Siwasaki	char	c, *progname;
59119515Snjl	char	*dsdt_input_file, *dsdt_output_file;
6065285Siwasaki
61119515Snjl	dsdt_input_file = dsdt_output_file = NULL;
6265285Siwasaki	progname = argv[0];
63119515Snjl
64119515Snjl	if (argc < 2)
65119515Snjl		usage(progname);
66119515Snjl
67136165Snjl	while ((c = getopt(argc, argv, "dhtvf:o:")) != -1) {
6865285Siwasaki		switch (c) {
69119515Snjl		case 'd':
70119515Snjl			dflag = 1;
71119515Snjl			break;
72119515Snjl		case 't':
73119515Snjl			tflag = 1;
74119515Snjl			break;
75119515Snjl		case 'v':
76119515Snjl			vflag = 1;
77119515Snjl			break;
7865285Siwasaki		case 'f':
79119515Snjl			dsdt_input_file = optarg;
80119515Snjl			break;
8165285Siwasaki		case 'o':
82119515Snjl			dsdt_output_file = optarg;
8365285Siwasaki			break;
8465285Siwasaki		case 'h':
85119515Snjl		default:
8665285Siwasaki			usage(progname);
87119515Snjl			/* NOTREACHED */
8865285Siwasaki		}
8965285Siwasaki	}
90119515Snjl	argc -= optind;
91119515Snjl	argv += optind;
9265285Siwasaki
93119515Snjl	/* Get input either from file or /dev/mem */
94119515Snjl	if (dsdt_input_file != NULL) {
95119515Snjl		if (dflag == 0 && tflag == 0) {
96119515Snjl			warnx("Need to specify -d or -t with DSDT input file");
97119515Snjl			usage(progname);
98119515Snjl		} else if (tflag != 0) {
99119515Snjl			warnx("Can't use -t with DSDT input file");
100119515Snjl			usage(progname);
101119515Snjl		}
102119515Snjl		if (vflag)
103119515Snjl			warnx("loading DSDT file: %s", dsdt_input_file);
104133679Smarcel		rsdt = dsdt_load_file(dsdt_input_file);
105119515Snjl	} else {
106119515Snjl		if (vflag)
107119515Snjl			warnx("loading RSD PTR from /dev/mem");
108133679Smarcel		rsdt = sdt_load_devmem();
109119515Snjl	}
110119515Snjl
111119515Snjl	/* Display misc. SDT tables (only available when using /dev/mem) */
112119515Snjl	if (tflag) {
113119515Snjl		if (vflag)
114119515Snjl			warnx("printing various SDT tables");
115133679Smarcel		sdt_print_all(rsdt);
116119515Snjl	}
117119515Snjl
118119515Snjl	/* Translate RSDT to DSDT pointer */
119119515Snjl	if (dsdt_input_file == NULL) {
120196555Sjhb		sdt = sdt_from_rsdt(rsdt, ACPI_SIG_FADT, NULL);
121196555Sjhb		sdt = dsdt_from_fadt((ACPI_TABLE_FADT *)sdt);
122133679Smarcel	} else {
123133679Smarcel		sdt = rsdt;
124133679Smarcel		rsdt = NULL;
125119515Snjl	}
126119515Snjl
127136128Snjl	/* Dump the DSDT and SSDTs to a file */
128119515Snjl	if (dsdt_output_file != NULL) {
129119515Snjl		if (vflag)
130119515Snjl			warnx("saving DSDT file: %s", dsdt_output_file);
131133679Smarcel		dsdt_save_file(dsdt_output_file, rsdt, sdt);
132119515Snjl	}
133119515Snjl
134119515Snjl	/* Disassemble the DSDT into ASL */
135119515Snjl	if (dflag) {
136119515Snjl		if (vflag)
137119515Snjl			warnx("disassembling DSDT, iasl messages follow");
138133679Smarcel		aml_disassemble(rsdt, sdt);
139119515Snjl		if (vflag)
140119515Snjl			warnx("iasl processing complete");
141119515Snjl	}
142119515Snjl
143119515Snjl	exit(0);
14465285Siwasaki}
145