xref: /netbsd-src/usr.sbin/acpitools/amldb/amldb.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*	$NetBSD: amldb.c,v 1.2 2007/01/14 05:33:18 dogcow Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	Id: amldb.c,v 1.8 2000/08/08 14:12:24 iwasaki Exp
29  *	$FreeBSD: src/usr.sbin/acpi/amldb/amldb.c,v 1.3 2001/10/22 17:25:32 iwasaki Exp $
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: amldb.c,v 1.2 2007/01/14 05:33:18 dogcow Exp $");
33 
34 #include <sys/param.h>
35 #include <sys/mman.h>
36 #include <sys/stat.h>
37 
38 #include <acpi_common.h>
39 #include <aml/aml_amlmem.h>
40 #include <aml/aml_common.h>
41 #include <aml/aml_env.h>
42 #include <aml/aml_parse.h>
43 #include <aml/aml_region.h>
44 
45 #include <assert.h>
46 #include <err.h>
47 #include <fcntl.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <stdlib.h>
52 
53 #include "debug.h"
54 
55 int	regdump_enabled = 0;
56 int	memstat_enabled = 0;
57 int	showtree_enabled = 0;
58 
59 static void     aml_init_namespace(void);
60 
61 void
62 aml_init_namespace(void)
63 {
64 	struct	aml_environ env;
65 	struct	aml_name *newname;
66 
67 	aml_new_name_group((void *)AML_NAME_GROUP_OS_DEFINED);
68 	env.curname = aml_get_rootname();
69 	newname = aml_create_name(&env, (const u_int8_t *)"\\_OS_");
70 	newname->property = aml_alloc_object(aml_t_string, NULL);
71 	newname->property->str.needfree = 0;
72 	newname->property->str.string = __UNCONST("Microsoft Windows NT");
73 }
74 
75 static int
76 load_dsdt(const char *dsdtfile)
77 {
78 	struct aml_environ	env;
79 	u_int8_t		*code;
80 	struct stat		sb;
81 	int			fd;
82 
83 	printf("Loading %s...", dsdtfile);
84 
85 	fd = open(dsdtfile, O_RDONLY, 0);
86 	if (fd == -1) {
87 		perror("open");
88 		exit(-1);
89 	}
90 	if (fstat(fd, &sb) == -1) {
91 		perror("fstat");
92 		exit(-1);
93 	}
94 	if ((code = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == NULL) {
95 		perror("mmap");
96 		exit(-1);
97 	}
98 	aml_init_namespace();
99 
100 	aml_new_name_group(code);
101 	bzero(&env, sizeof(env));
102 
103 #define SIZEOF_SDT_HDR 36	/* struct size except body */
104 	if (strncmp((const char *)code, "DSDT", 4) == 0) {
105 		env.dp = code + SIZEOF_SDT_HDR;
106 	} else {
107 		env.dp = code;
108 	}
109 	env.end = code + sb.st_size;
110 	env.curname = aml_get_rootname();
111 
112 	aml_local_stack_push(aml_local_stack_create());
113 	aml_parse_objectlist(&env, 0);
114 	aml_local_stack_delete(aml_local_stack_pop());
115 
116 	assert(env.dp == env.end);
117 	env.dp = code;
118 	env.end = code + sb.st_size;
119 
120 	printf("done\n");
121 
122 	aml_debug = 1;		/* debug print enabled */
123 
124 	if (showtree_enabled == 1) {
125 		aml_showtree(env.curname, 0);
126 	}
127 	do {
128 		aml_dbgr(&env, &env);
129 	} while (env.stat != aml_stat_panic);
130 
131 	aml_debug = 0;		/* debug print disabled */
132 
133 	if (regdump_enabled == 1) {
134 		aml_simulation_regdump("region.dmp");
135 	}
136 	while (name_group_list->id != AML_NAME_GROUP_ROOT) {
137 		aml_delete_name_group(name_group_list);
138 	}
139 
140 	if (memstat_enabled == 1) {
141 		memman_statistics(aml_memman);
142 	}
143 	memman_freeall(aml_memman);
144 
145 	return (0);
146 }
147 
148 static void
149 usage(const char *progname)
150 {
151 
152 	printf("usage: %s [-d] [-s] [-t] [-h] dsdt_files...\n", progname);
153 	exit(1);
154 }
155 
156 int
157 main(int argc, char *argv[])
158 {
159 	char	c, *progname;
160 	int	i;
161 
162 	progname = argv[0];
163 	while ((c = getopt(argc, argv, "dsth")) != -1) {
164 		switch (c) {
165 		case 'd':
166 			regdump_enabled = 1;
167 			break;
168 		case 's':
169 			memstat_enabled = 1;
170 			break;
171 		case 't':
172 			showtree_enabled = 1;
173 			break;
174 		case 'h':
175 		default:
176 			usage(progname);
177 			/* NOTREACHED */
178 		}
179 	}
180 	argc -= optind;
181 	argv += optind;
182 
183 	if (argc == 0) {
184 		usage(progname);
185 	}
186 	for (i = 0; i < argc; i++) {
187 		load_dsdt(argv[i]);
188 	}
189 
190 	return (0);
191 }
192