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