acpi_user.c revision 66490
1/*- 2 * Copyright (c) 1999 Doug Rabson 3 * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $Id: acpi_user.c,v 1.5 2000/08/09 14:47:52 iwasaki Exp $ 28 * $FreeBSD: head/usr.sbin/acpi/acpidump/acpi_user.c 66490 2000-09-30 20:13:57Z msmith $ 29 */ 30 31#include <sys/param.h> 32#include <sys/mman.h> 33#include <sys/queue.h> 34#include <sys/stat.h> 35 36#include <err.h> 37#include <fcntl.h> 38#include <stdlib.h> 39#include <string.h> 40#include <unistd.h> 41 42#include <dev/acpi/acpireg.h> 43 44#include "acpidump.h" 45 46static int acpi_mem_fd = -1; 47 48struct acpi_user_mapping { 49 LIST_ENTRY(acpi_user_mapping) link; 50 vm_offset_t pa; 51 caddr_t va; 52 size_t size; 53}; 54 55LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist; 56 57static void 58acpi_user_init() 59{ 60 61 if (acpi_mem_fd == -1) { 62 acpi_mem_fd = open("/dev/mem", O_RDONLY); 63 if (acpi_mem_fd == -1) 64 err(1, "opening /dev/mem"); 65 LIST_INIT(&maplist); 66 } 67} 68 69static struct acpi_user_mapping * 70acpi_user_find_mapping(vm_offset_t pa, size_t size) 71{ 72 struct acpi_user_mapping *map; 73 74 /* First search for an existing mapping */ 75 for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) { 76 if (map->pa <= pa && map->size >= pa + size - map->pa) 77 return (map); 78 } 79 80 /* Then create a new one */ 81 size = round_page(pa + size) - trunc_page(pa); 82 pa = trunc_page(pa); 83 map = malloc(sizeof(struct acpi_user_mapping)); 84 if (!map) 85 errx(1, "out of memory"); 86 map->pa = pa; 87 map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa); 88 map->size = size; 89 if ((intptr_t) map->va == -1) 90 err(1, "can't map address"); 91 LIST_INSERT_HEAD(&maplist, map, link); 92 93 return (map); 94} 95 96/* 97 * Public interfaces 98 */ 99 100struct ACPIrsdp * 101acpi_find_rsd_ptr() 102{ 103 int i; 104 u_int8_t buf[sizeof(struct ACPIrsdp)]; 105 106 acpi_user_init(); 107 for (i = 0; i < 1024 * 1024; i += 16) { 108 read(acpi_mem_fd, buf, 16); 109 if (!memcmp(buf, "RSD PTR ", 8)) { 110 /* Read the rest of the structure */ 111 read(acpi_mem_fd, buf + 16, sizeof(struct ACPIrsdp) - 16); 112 113 /* Verify checksum before accepting it. */ 114 if (acpi_checksum(buf, sizeof(struct ACPIrsdp))) 115 continue; 116 return (acpi_map_physical(i, sizeof(struct ACPIrsdp))); 117 } 118 } 119 120 return (0); 121} 122 123void * 124acpi_map_physical(vm_offset_t pa, size_t size) 125{ 126 struct acpi_user_mapping *map; 127 128 map = acpi_user_find_mapping(pa, size); 129 return (map->va + (pa - map->pa)); 130} 131 132void 133acpi_load_dsdt(char *dumpfile, u_int8_t **dpp, u_int8_t **endp) 134{ 135 u_int8_t *dp; 136 u_int8_t *end; 137 struct stat sb; 138 139 if ((acpi_mem_fd = open(dumpfile, O_RDONLY)) == -1) { 140 errx(1, "opening %s\n", dumpfile); 141 } 142 143 LIST_INIT(&maplist); 144 145 if (fstat(acpi_mem_fd, &sb) == -1) { 146 errx(1, "fstat %s\n", dumpfile); 147 } 148 149 dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0); 150 if (dp == NULL) { 151 errx(1, "mmap %s\n", dumpfile); 152 } 153 154 /* 155 * Microsoft asl.exe generates 0x23 byte additional info. 156 * at the begining of the file, so just ignore it. 157 */ 158 if (strncmp(dp, "DSDT", 4) == 0) { 159 dp += 0x23; 160 sb.st_size -= 0x23; 161 } 162 163 end = (u_int8_t *) dp + sb.st_size; 164 *dpp = dp; 165 *endp = end; 166} 167