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