acpi_user.c revision 68475
1186284Sbms/*-
2186284Sbms * Copyright (c) 1999 Doug Rabson
3186284Sbms * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org>
4186284Sbms * All rights reserved.
5186284Sbms *
6186284Sbms * Redistribution and use in source and binary forms, with or without
7186284Sbms * modification, are permitted provided that the following conditions
8186284Sbms * are met:
9186284Sbms * 1. Redistributions of source code must retain the above copyright
10186284Sbms *    notice, this list of conditions and the following disclaimer.
11186284Sbms * 2. Redistributions in binary form must reproduce the above copyright
12186284Sbms *    notice, this list of conditions and the following disclaimer in the
13186284Sbms *    documentation and/or other materials provided with the distribution.
14186284Sbms *
15186284Sbms * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16186284Sbms * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17186284Sbms * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18186284Sbms * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19186284Sbms * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20186284Sbms * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21186284Sbms * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22186284Sbms * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23186284Sbms * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24186284Sbms * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25186284Sbms * SUCH DAMAGE.
26186284Sbms *
27186284Sbms *	$Id: acpi_user.c,v 1.5 2000/08/09 14:47:52 iwasaki Exp $
28186284Sbms *	$FreeBSD: head/usr.sbin/acpi/acpidump/acpi_user.c 68475 2000-11-08 02:37:00Z iwasaki $
29186284Sbms */
30186284Sbms
31186284Sbms#include <sys/param.h>
32186284Sbms#include <sys/mman.h>
33186284Sbms#include <sys/queue.h>
34186284Sbms#include <sys/stat.h>
35186284Sbms
36186284Sbms#include <err.h>
37186284Sbms#include <fcntl.h>
38186284Sbms#include <stdlib.h>
39186284Sbms#include <string.h>
40186284Sbms#include <unistd.h>
41186284Sbms
42186284Sbms#include "acpidump.h"
43186284Sbms
44186284Sbmsstatic int      acpi_mem_fd = -1;
45186284Sbms
46186284Sbmsstruct acpi_user_mapping {
47186284Sbms	LIST_ENTRY(acpi_user_mapping) link;
48186284Sbms	vm_offset_t     pa;
49186284Sbms	caddr_t         va;
50186284Sbms	size_t          size;
51186284Sbms};
52186284Sbms
53186284SbmsLIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;
54186284Sbms
55186284Sbmsstatic void
56186284Sbmsacpi_user_init()
57186284Sbms{
58186284Sbms
59186284Sbms	if (acpi_mem_fd == -1) {
60186284Sbms		acpi_mem_fd = open("/dev/mem", O_RDONLY);
61186284Sbms		if (acpi_mem_fd == -1)
62186284Sbms			err(1, "opening /dev/mem");
63186284Sbms		LIST_INIT(&maplist);
64186284Sbms	}
65186284Sbms}
66186284Sbms
67186284Sbmsstatic struct acpi_user_mapping *
68186284Sbmsacpi_user_find_mapping(vm_offset_t pa, size_t size)
69186284Sbms{
70186284Sbms	struct	acpi_user_mapping *map;
71186284Sbms
72186284Sbms	/* First search for an existing mapping */
73186284Sbms	for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {
74186284Sbms		if (map->pa <= pa && map->size >= pa + size - map->pa)
75186284Sbms			return (map);
76186284Sbms	}
77186284Sbms
78186284Sbms	/* Then create a new one */
79186284Sbms	size = round_page(pa + size) - trunc_page(pa);
80186284Sbms	pa = trunc_page(pa);
81186284Sbms	map = malloc(sizeof(struct acpi_user_mapping));
82186284Sbms	if (!map)
83186284Sbms		errx(1, "out of memory");
84186284Sbms	map->pa = pa;
85186284Sbms	map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);
86186284Sbms	map->size = size;
87186284Sbms	if ((intptr_t) map->va == -1)
88186284Sbms		err(1, "can't map address");
89186284Sbms	LIST_INSERT_HEAD(&maplist, map, link);
90186284Sbms
91186284Sbms	return (map);
92186284Sbms}
93186284Sbms
94186284Sbms/*
95186284Sbms * Public interfaces
96186284Sbms */
97186284Sbms
98186284Sbmsstruct ACPIrsdp *
99186284Sbmsacpi_find_rsd_ptr()
100186284Sbms{
101186284Sbms	int		i;
102	u_int8_t	buf[sizeof(struct ACPIrsdp)];
103
104	acpi_user_init();
105	for (i = 0; i < 1024 * 1024; i += 16) {
106		read(acpi_mem_fd, buf, 16);
107		if (!memcmp(buf, "RSD PTR ", 8)) {
108			/* Read the rest of the structure */
109			read(acpi_mem_fd, buf + 16, sizeof(struct ACPIrsdp) - 16);
110
111			/* Verify checksum before accepting it. */
112			if (acpi_checksum(buf, sizeof(struct ACPIrsdp)))
113				continue;
114			return (acpi_map_physical(i, sizeof(struct ACPIrsdp)));
115		}
116	}
117
118	return (0);
119}
120
121void *
122acpi_map_physical(vm_offset_t pa, size_t size)
123{
124	struct	acpi_user_mapping *map;
125
126	map = acpi_user_find_mapping(pa, size);
127	return (map->va + (pa - map->pa));
128}
129
130void
131acpi_load_dsdt(char *dumpfile, u_int8_t **dpp, u_int8_t **endp)
132{
133	u_int8_t	*dp;
134	u_int8_t	*end;
135	struct	stat sb;
136
137	if ((acpi_mem_fd = open(dumpfile, O_RDONLY)) == -1) {
138		errx(1, "opening %s\n", dumpfile);
139	}
140
141	LIST_INIT(&maplist);
142
143	if (fstat(acpi_mem_fd, &sb) == -1) {
144		errx(1, "fstat %s\n", dumpfile);
145	}
146
147	dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);
148	if (dp == NULL) {
149		errx(1, "mmap %s\n", dumpfile);
150	}
151
152	/*
153	 * Microsoft asl.exe generates 0x23 byte additional info.
154	 * at the begining of the file, so just ignore it.
155	 */
156        if (strncmp(dp, "DSDT", 4) == 0) {
157                dp += 0x23;
158		sb.st_size -= 0x23;
159        }
160
161	end = (u_int8_t *) dp + sb.st_size;
162	*dpp = dp;
163	*endp = end;
164}
165