Home | History | Annotate | Line # | Download | only in amldb
amldb.c revision 1.3.40.1
      1  1.3.40.1  pgoyette /*	$NetBSD: amldb.c,v 1.3.40.1 2018/10/20 06:58:47 pgoyette 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.3.40.1  pgoyette __RCSID("$NetBSD: amldb.c,v 1.3.40.1 2018/10/20 06:58:47 pgoyette 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.1  christos 	if ((code = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == NULL) {
     95       1.1  christos 		perror("mmap");
     96       1.1  christos 		exit(-1);
     97       1.1  christos 	}
     98       1.1  christos 	aml_init_namespace();
     99       1.1  christos 
    100       1.2    dogcow 	aml_new_name_group(code);
    101       1.1  christos 	bzero(&env, sizeof(env));
    102       1.1  christos 
    103       1.1  christos #define SIZEOF_SDT_HDR 36	/* struct size except body */
    104       1.1  christos 	if (strncmp((const char *)code, "DSDT", 4) == 0) {
    105       1.1  christos 		env.dp = code + SIZEOF_SDT_HDR;
    106       1.1  christos 	} else {
    107       1.1  christos 		env.dp = code;
    108       1.1  christos 	}
    109       1.1  christos 	env.end = code + sb.st_size;
    110       1.1  christos 	env.curname = aml_get_rootname();
    111       1.1  christos 
    112       1.1  christos 	aml_local_stack_push(aml_local_stack_create());
    113       1.1  christos 	aml_parse_objectlist(&env, 0);
    114       1.1  christos 	aml_local_stack_delete(aml_local_stack_pop());
    115       1.1  christos 
    116       1.1  christos 	assert(env.dp == env.end);
    117       1.1  christos 	env.dp = code;
    118       1.1  christos 	env.end = code + sb.st_size;
    119       1.1  christos 
    120       1.1  christos 	printf("done\n");
    121       1.1  christos 
    122       1.1  christos 	aml_debug = 1;		/* debug print enabled */
    123       1.1  christos 
    124       1.1  christos 	if (showtree_enabled == 1) {
    125       1.1  christos 		aml_showtree(env.curname, 0);
    126       1.1  christos 	}
    127       1.1  christos 	do {
    128       1.1  christos 		aml_dbgr(&env, &env);
    129       1.1  christos 	} while (env.stat != aml_stat_panic);
    130       1.1  christos 
    131       1.1  christos 	aml_debug = 0;		/* debug print disabled */
    132       1.1  christos 
    133       1.1  christos 	if (regdump_enabled == 1) {
    134       1.1  christos 		aml_simulation_regdump("region.dmp");
    135       1.1  christos 	}
    136       1.1  christos 	while (name_group_list->id != AML_NAME_GROUP_ROOT) {
    137       1.1  christos 		aml_delete_name_group(name_group_list);
    138       1.1  christos 	}
    139       1.1  christos 
    140       1.1  christos 	if (memstat_enabled == 1) {
    141       1.1  christos 		memman_statistics(aml_memman);
    142       1.1  christos 	}
    143       1.1  christos 	memman_freeall(aml_memman);
    144       1.1  christos 
    145       1.1  christos 	return (0);
    146       1.1  christos }
    147       1.1  christos 
    148       1.3     joerg __dead static void
    149       1.1  christos usage(const char *progname)
    150       1.1  christos {
    151       1.1  christos 
    152       1.1  christos 	printf("usage: %s [-d] [-s] [-t] [-h] dsdt_files...\n", progname);
    153       1.1  christos 	exit(1);
    154       1.1  christos }
    155       1.1  christos 
    156       1.1  christos int
    157       1.1  christos main(int argc, char *argv[])
    158       1.1  christos {
    159  1.3.40.1  pgoyette 	char	*progname;
    160  1.3.40.1  pgoyette 	int	c, i;
    161       1.1  christos 
    162       1.1  christos 	progname = argv[0];
    163       1.1  christos 	while ((c = getopt(argc, argv, "dsth")) != -1) {
    164       1.1  christos 		switch (c) {
    165       1.1  christos 		case 'd':
    166       1.1  christos 			regdump_enabled = 1;
    167       1.1  christos 			break;
    168       1.1  christos 		case 's':
    169       1.1  christos 			memstat_enabled = 1;
    170       1.1  christos 			break;
    171       1.1  christos 		case 't':
    172       1.1  christos 			showtree_enabled = 1;
    173       1.1  christos 			break;
    174       1.1  christos 		case 'h':
    175       1.1  christos 		default:
    176       1.1  christos 			usage(progname);
    177       1.1  christos 			/* NOTREACHED */
    178       1.1  christos 		}
    179       1.1  christos 	}
    180       1.1  christos 	argc -= optind;
    181       1.1  christos 	argv += optind;
    182       1.1  christos 
    183       1.1  christos 	if (argc == 0) {
    184       1.1  christos 		usage(progname);
    185       1.1  christos 	}
    186       1.1  christos 	for (i = 0; i < argc; i++) {
    187       1.1  christos 		load_dsdt(argv[i]);
    188       1.1  christos 	}
    189       1.1  christos 
    190       1.1  christos 	return (0);
    191       1.1  christos }
    192