Home | History | Annotate | Line # | Download | only in amldb
      1  1.5  riastrad /*	$NetBSD: amldb.c,v 1.5 2020/08/20 15:54:12 riastradh Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1999 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.1  christos  *	Id: amldb.c,v 1.8 2000/08/08 14:12:24 iwasaki Exp
     29  1.1  christos  *	$FreeBSD: src/usr.sbin/acpi/amldb/amldb.c,v 1.3 2001/10/22 17:25:32 iwasaki Exp $
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.5  riastrad __RCSID("$NetBSD: amldb.c,v 1.5 2020/08/20 15:54:12 riastradh Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/param.h>
     35  1.1  christos #include <sys/mman.h>
     36  1.1  christos #include <sys/stat.h>
     37  1.1  christos 
     38  1.1  christos #include <acpi_common.h>
     39  1.1  christos #include <aml/aml_amlmem.h>
     40  1.1  christos #include <aml/aml_common.h>
     41  1.1  christos #include <aml/aml_env.h>
     42  1.1  christos #include <aml/aml_parse.h>
     43  1.1  christos #include <aml/aml_region.h>
     44  1.1  christos 
     45  1.1  christos #include <assert.h>
     46  1.1  christos #include <err.h>
     47  1.1  christos #include <fcntl.h>
     48  1.1  christos #include <stdio.h>
     49  1.1  christos #include <string.h>
     50  1.1  christos #include <unistd.h>
     51  1.1  christos #include <stdlib.h>
     52  1.1  christos 
     53  1.1  christos #include "debug.h"
     54  1.1  christos 
     55  1.1  christos int	regdump_enabled = 0;
     56  1.1  christos int	memstat_enabled = 0;
     57  1.1  christos int	showtree_enabled = 0;
     58  1.1  christos 
     59  1.1  christos static void     aml_init_namespace(void);
     60  1.1  christos 
     61  1.1  christos void
     62  1.1  christos aml_init_namespace(void)
     63  1.1  christos {
     64  1.1  christos 	struct	aml_environ env;
     65  1.1  christos 	struct	aml_name *newname;
     66  1.1  christos 
     67  1.2    dogcow 	aml_new_name_group((void *)AML_NAME_GROUP_OS_DEFINED);
     68  1.1  christos 	env.curname = aml_get_rootname();
     69  1.1  christos 	newname = aml_create_name(&env, (const u_int8_t *)"\\_OS_");
     70  1.1  christos 	newname->property = aml_alloc_object(aml_t_string, NULL);
     71  1.1  christos 	newname->property->str.needfree = 0;
     72  1.1  christos 	newname->property->str.string = __UNCONST("Microsoft Windows NT");
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos static int
     76  1.1  christos load_dsdt(const char *dsdtfile)
     77  1.1  christos {
     78  1.1  christos 	struct aml_environ	env;
     79  1.1  christos 	u_int8_t		*code;
     80  1.1  christos 	struct stat		sb;
     81  1.1  christos 	int			fd;
     82  1.1  christos 
     83  1.1  christos 	printf("Loading %s...", dsdtfile);
     84  1.1  christos 
     85  1.1  christos 	fd = open(dsdtfile, O_RDONLY, 0);
     86  1.1  christos 	if (fd == -1) {
     87  1.1  christos 		perror("open");
     88  1.1  christos 		exit(-1);
     89  1.1  christos 	}
     90  1.1  christos 	if (fstat(fd, &sb) == -1) {
     91  1.1  christos 		perror("fstat");
     92  1.1  christos 		exit(-1);
     93  1.1  christos 	}
     94  1.5  riastrad 	if ((code = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) ==
     95  1.5  riastrad 	    MAP_FAILED) {
     96  1.1  christos 		perror("mmap");
     97  1.1  christos 		exit(-1);
     98  1.1  christos 	}
     99  1.1  christos 	aml_init_namespace();
    100  1.1  christos 
    101  1.2    dogcow 	aml_new_name_group(code);
    102  1.1  christos 	bzero(&env, sizeof(env));
    103  1.1  christos 
    104  1.1  christos #define SIZEOF_SDT_HDR 36	/* struct size except body */
    105  1.1  christos 	if (strncmp((const char *)code, "DSDT", 4) == 0) {
    106  1.1  christos 		env.dp = code + SIZEOF_SDT_HDR;
    107  1.1  christos 	} else {
    108  1.1  christos 		env.dp = code;
    109  1.1  christos 	}
    110  1.1  christos 	env.end = code + sb.st_size;
    111  1.1  christos 	env.curname = aml_get_rootname();
    112  1.1  christos 
    113  1.1  christos 	aml_local_stack_push(aml_local_stack_create());
    114  1.1  christos 	aml_parse_objectlist(&env, 0);
    115  1.1  christos 	aml_local_stack_delete(aml_local_stack_pop());
    116  1.1  christos 
    117  1.1  christos 	assert(env.dp == env.end);
    118  1.1  christos 	env.dp = code;
    119  1.1  christos 	env.end = code + sb.st_size;
    120  1.1  christos 
    121  1.1  christos 	printf("done\n");
    122  1.1  christos 
    123  1.1  christos 	aml_debug = 1;		/* debug print enabled */
    124  1.1  christos 
    125  1.1  christos 	if (showtree_enabled == 1) {
    126  1.1  christos 		aml_showtree(env.curname, 0);
    127  1.1  christos 	}
    128  1.1  christos 	do {
    129  1.1  christos 		aml_dbgr(&env, &env);
    130  1.1  christos 	} while (env.stat != aml_stat_panic);
    131  1.1  christos 
    132  1.1  christos 	aml_debug = 0;		/* debug print disabled */
    133  1.1  christos 
    134  1.1  christos 	if (regdump_enabled == 1) {
    135  1.1  christos 		aml_simulation_regdump("region.dmp");
    136  1.1  christos 	}
    137  1.1  christos 	while (name_group_list->id != AML_NAME_GROUP_ROOT) {
    138  1.1  christos 		aml_delete_name_group(name_group_list);
    139  1.1  christos 	}
    140  1.1  christos 
    141  1.1  christos 	if (memstat_enabled == 1) {
    142  1.1  christos 		memman_statistics(aml_memman);
    143  1.1  christos 	}
    144  1.1  christos 	memman_freeall(aml_memman);
    145  1.1  christos 
    146  1.1  christos 	return (0);
    147  1.1  christos }
    148  1.1  christos 
    149  1.3     joerg __dead static void
    150  1.1  christos usage(const char *progname)
    151  1.1  christos {
    152  1.1  christos 
    153  1.1  christos 	printf("usage: %s [-d] [-s] [-t] [-h] dsdt_files...\n", progname);
    154  1.1  christos 	exit(1);
    155  1.1  christos }
    156  1.1  christos 
    157  1.1  christos int
    158  1.1  christos main(int argc, char *argv[])
    159  1.1  christos {
    160  1.4  jmcneill 	char	*progname;
    161  1.4  jmcneill 	int	c, i;
    162  1.1  christos 
    163  1.1  christos 	progname = argv[0];
    164  1.1  christos 	while ((c = getopt(argc, argv, "dsth")) != -1) {
    165  1.1  christos 		switch (c) {
    166  1.1  christos 		case 'd':
    167  1.1  christos 			regdump_enabled = 1;
    168  1.1  christos 			break;
    169  1.1  christos 		case 's':
    170  1.1  christos 			memstat_enabled = 1;
    171  1.1  christos 			break;
    172  1.1  christos 		case 't':
    173  1.1  christos 			showtree_enabled = 1;
    174  1.1  christos 			break;
    175  1.1  christos 		case 'h':
    176  1.1  christos 		default:
    177  1.1  christos 			usage(progname);
    178  1.1  christos 			/* NOTREACHED */
    179  1.1  christos 		}
    180  1.1  christos 	}
    181  1.1  christos 	argc -= optind;
    182  1.1  christos 	argv += optind;
    183  1.1  christos 
    184  1.1  christos 	if (argc == 0) {
    185  1.1  christos 		usage(progname);
    186  1.1  christos 	}
    187  1.1  christos 	for (i = 0; i < argc; i++) {
    188  1.1  christos 		load_dsdt(argv[i]);
    189  1.1  christos 	}
    190  1.1  christos 
    191  1.1  christos 	return (0);
    192  1.1  christos }
    193