1*25f11de3Sriastradh /* $NetBSD: amldb.c,v 1.5 2020/08/20 15:54:12 riastradh Exp $ */
253e202c1Schristos
353e202c1Schristos /*-
453e202c1Schristos * Copyright (c) 1999 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
553e202c1Schristos * All rights reserved.
653e202c1Schristos *
753e202c1Schristos * Redistribution and use in source and binary forms, with or without
853e202c1Schristos * modification, are permitted provided that the following conditions
953e202c1Schristos * are met:
1053e202c1Schristos * 1. Redistributions of source code must retain the above copyright
1153e202c1Schristos * notice, this list of conditions and the following disclaimer.
1253e202c1Schristos * 2. Redistributions in binary form must reproduce the above copyright
1353e202c1Schristos * notice, this list of conditions and the following disclaimer in the
1453e202c1Schristos * documentation and/or other materials provided with the distribution.
1553e202c1Schristos *
1653e202c1Schristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1753e202c1Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1853e202c1Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1953e202c1Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2053e202c1Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2153e202c1Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2253e202c1Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2353e202c1Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2453e202c1Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2553e202c1Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2653e202c1Schristos * SUCH DAMAGE.
2753e202c1Schristos *
2853e202c1Schristos * Id: amldb.c,v 1.8 2000/08/08 14:12:24 iwasaki Exp
2953e202c1Schristos * $FreeBSD: src/usr.sbin/acpi/amldb/amldb.c,v 1.3 2001/10/22 17:25:32 iwasaki Exp $
3053e202c1Schristos */
3153e202c1Schristos #include <sys/cdefs.h>
32*25f11de3Sriastradh __RCSID("$NetBSD: amldb.c,v 1.5 2020/08/20 15:54:12 riastradh Exp $");
3353e202c1Schristos
3453e202c1Schristos #include <sys/param.h>
3553e202c1Schristos #include <sys/mman.h>
3653e202c1Schristos #include <sys/stat.h>
3753e202c1Schristos
3853e202c1Schristos #include <acpi_common.h>
3953e202c1Schristos #include <aml/aml_amlmem.h>
4053e202c1Schristos #include <aml/aml_common.h>
4153e202c1Schristos #include <aml/aml_env.h>
4253e202c1Schristos #include <aml/aml_parse.h>
4353e202c1Schristos #include <aml/aml_region.h>
4453e202c1Schristos
4553e202c1Schristos #include <assert.h>
4653e202c1Schristos #include <err.h>
4753e202c1Schristos #include <fcntl.h>
4853e202c1Schristos #include <stdio.h>
4953e202c1Schristos #include <string.h>
5053e202c1Schristos #include <unistd.h>
5153e202c1Schristos #include <stdlib.h>
5253e202c1Schristos
5353e202c1Schristos #include "debug.h"
5453e202c1Schristos
5553e202c1Schristos int regdump_enabled = 0;
5653e202c1Schristos int memstat_enabled = 0;
5753e202c1Schristos int showtree_enabled = 0;
5853e202c1Schristos
5953e202c1Schristos static void aml_init_namespace(void);
6053e202c1Schristos
6153e202c1Schristos void
aml_init_namespace(void)6253e202c1Schristos aml_init_namespace(void)
6353e202c1Schristos {
6453e202c1Schristos struct aml_environ env;
6553e202c1Schristos struct aml_name *newname;
6653e202c1Schristos
677ac7d15dSdogcow aml_new_name_group((void *)AML_NAME_GROUP_OS_DEFINED);
6853e202c1Schristos env.curname = aml_get_rootname();
6953e202c1Schristos newname = aml_create_name(&env, (const u_int8_t *)"\\_OS_");
7053e202c1Schristos newname->property = aml_alloc_object(aml_t_string, NULL);
7153e202c1Schristos newname->property->str.needfree = 0;
7253e202c1Schristos newname->property->str.string = __UNCONST("Microsoft Windows NT");
7353e202c1Schristos }
7453e202c1Schristos
7553e202c1Schristos static int
load_dsdt(const char * dsdtfile)7653e202c1Schristos load_dsdt(const char *dsdtfile)
7753e202c1Schristos {
7853e202c1Schristos struct aml_environ env;
7953e202c1Schristos u_int8_t *code;
8053e202c1Schristos struct stat sb;
8153e202c1Schristos int fd;
8253e202c1Schristos
8353e202c1Schristos printf("Loading %s...", dsdtfile);
8453e202c1Schristos
8553e202c1Schristos fd = open(dsdtfile, O_RDONLY, 0);
8653e202c1Schristos if (fd == -1) {
8753e202c1Schristos perror("open");
8853e202c1Schristos exit(-1);
8953e202c1Schristos }
9053e202c1Schristos if (fstat(fd, &sb) == -1) {
9153e202c1Schristos perror("fstat");
9253e202c1Schristos exit(-1);
9353e202c1Schristos }
94*25f11de3Sriastradh if ((code = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) ==
95*25f11de3Sriastradh MAP_FAILED) {
9653e202c1Schristos perror("mmap");
9753e202c1Schristos exit(-1);
9853e202c1Schristos }
9953e202c1Schristos aml_init_namespace();
10053e202c1Schristos
1017ac7d15dSdogcow aml_new_name_group(code);
10253e202c1Schristos bzero(&env, sizeof(env));
10353e202c1Schristos
10453e202c1Schristos #define SIZEOF_SDT_HDR 36 /* struct size except body */
10553e202c1Schristos if (strncmp((const char *)code, "DSDT", 4) == 0) {
10653e202c1Schristos env.dp = code + SIZEOF_SDT_HDR;
10753e202c1Schristos } else {
10853e202c1Schristos env.dp = code;
10953e202c1Schristos }
11053e202c1Schristos env.end = code + sb.st_size;
11153e202c1Schristos env.curname = aml_get_rootname();
11253e202c1Schristos
11353e202c1Schristos aml_local_stack_push(aml_local_stack_create());
11453e202c1Schristos aml_parse_objectlist(&env, 0);
11553e202c1Schristos aml_local_stack_delete(aml_local_stack_pop());
11653e202c1Schristos
11753e202c1Schristos assert(env.dp == env.end);
11853e202c1Schristos env.dp = code;
11953e202c1Schristos env.end = code + sb.st_size;
12053e202c1Schristos
12153e202c1Schristos printf("done\n");
12253e202c1Schristos
12353e202c1Schristos aml_debug = 1; /* debug print enabled */
12453e202c1Schristos
12553e202c1Schristos if (showtree_enabled == 1) {
12653e202c1Schristos aml_showtree(env.curname, 0);
12753e202c1Schristos }
12853e202c1Schristos do {
12953e202c1Schristos aml_dbgr(&env, &env);
13053e202c1Schristos } while (env.stat != aml_stat_panic);
13153e202c1Schristos
13253e202c1Schristos aml_debug = 0; /* debug print disabled */
13353e202c1Schristos
13453e202c1Schristos if (regdump_enabled == 1) {
13553e202c1Schristos aml_simulation_regdump("region.dmp");
13653e202c1Schristos }
13753e202c1Schristos while (name_group_list->id != AML_NAME_GROUP_ROOT) {
13853e202c1Schristos aml_delete_name_group(name_group_list);
13953e202c1Schristos }
14053e202c1Schristos
14153e202c1Schristos if (memstat_enabled == 1) {
14253e202c1Schristos memman_statistics(aml_memman);
14353e202c1Schristos }
14453e202c1Schristos memman_freeall(aml_memman);
14553e202c1Schristos
14653e202c1Schristos return (0);
14753e202c1Schristos }
14853e202c1Schristos
149dccf569eSjoerg __dead static void
usage(const char * progname)15053e202c1Schristos usage(const char *progname)
15153e202c1Schristos {
15253e202c1Schristos
15353e202c1Schristos printf("usage: %s [-d] [-s] [-t] [-h] dsdt_files...\n", progname);
15453e202c1Schristos exit(1);
15553e202c1Schristos }
15653e202c1Schristos
15753e202c1Schristos int
main(int argc,char * argv[])15853e202c1Schristos main(int argc, char *argv[])
15953e202c1Schristos {
1608ebf8cb0Sjmcneill char *progname;
1618ebf8cb0Sjmcneill int c, i;
16253e202c1Schristos
16353e202c1Schristos progname = argv[0];
16453e202c1Schristos while ((c = getopt(argc, argv, "dsth")) != -1) {
16553e202c1Schristos switch (c) {
16653e202c1Schristos case 'd':
16753e202c1Schristos regdump_enabled = 1;
16853e202c1Schristos break;
16953e202c1Schristos case 's':
17053e202c1Schristos memstat_enabled = 1;
17153e202c1Schristos break;
17253e202c1Schristos case 't':
17353e202c1Schristos showtree_enabled = 1;
17453e202c1Schristos break;
17553e202c1Schristos case 'h':
17653e202c1Schristos default:
17753e202c1Schristos usage(progname);
17853e202c1Schristos /* NOTREACHED */
17953e202c1Schristos }
18053e202c1Schristos }
18153e202c1Schristos argc -= optind;
18253e202c1Schristos argv += optind;
18353e202c1Schristos
18453e202c1Schristos if (argc == 0) {
18553e202c1Schristos usage(progname);
18653e202c1Schristos }
18753e202c1Schristos for (i = 0; i < argc; i++) {
18853e202c1Schristos load_dsdt(argv[i]);
18953e202c1Schristos }
19053e202c1Schristos
19153e202c1Schristos return (0);
19253e202c1Schristos }
193