Home | History | Annotate | Line # | Download | only in acpidump
acpi_user.c revision 1.3
      1  1.3   msaitoh /* $NetBSD: acpi_user.c,v 1.3 2017/08/03 05:54:45 msaitoh Exp $ */
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 1999 Doug Rabson
      5  1.1  christos  * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki (at) FreeBSD.org>
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     18  1.1  christos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     19  1.1  christos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     20  1.1  christos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     21  1.1  christos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     23  1.1  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     24  1.1  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     25  1.1  christos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     26  1.1  christos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27  1.1  christos  * SUCH DAMAGE.
     28  1.1  christos  *
     29  1.2    cegger  *	$FreeBSD: src/usr.sbin/acpi/acpidump/acpi_user.c,v 1.15 2009/08/25 20:35:57 jhb Exp $
     30  1.1  christos  */
     31  1.2    cegger 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.3   msaitoh __RCSID("$NetBSD: acpi_user.c,v 1.3 2017/08/03 05:54:45 msaitoh Exp $");
     34  1.1  christos 
     35  1.1  christos #include <sys/param.h>
     36  1.1  christos #include <sys/mman.h>
     37  1.1  christos #include <sys/queue.h>
     38  1.1  christos #include <sys/stat.h>
     39  1.2    cegger #include <sys/sysctl.h>
     40  1.1  christos 
     41  1.1  christos #include <err.h>
     42  1.1  christos #include <fcntl.h>
     43  1.1  christos #include <stdlib.h>
     44  1.1  christos #include <string.h>
     45  1.1  christos #include <unistd.h>
     46  1.1  christos 
     47  1.1  christos #include "acpidump.h"
     48  1.1  christos 
     49  1.3   msaitoh static char	machdep_acpi_root[] = "hw.acpi.root";
     50  1.1  christos static int      acpi_mem_fd = -1;
     51  1.1  christos 
     52  1.1  christos struct acpi_user_mapping {
     53  1.1  christos 	LIST_ENTRY(acpi_user_mapping) link;
     54  1.1  christos 	vm_offset_t     pa;
     55  1.1  christos 	caddr_t         va;
     56  1.1  christos 	size_t          size;
     57  1.1  christos };
     58  1.1  christos 
     59  1.1  christos LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;
     60  1.1  christos 
     61  1.1  christos static void
     62  1.1  christos acpi_user_init(void)
     63  1.1  christos {
     64  1.1  christos 
     65  1.1  christos 	if (acpi_mem_fd == -1) {
     66  1.1  christos 		acpi_mem_fd = open("/dev/mem", O_RDONLY);
     67  1.1  christos 		if (acpi_mem_fd == -1)
     68  1.2    cegger 			err(EXIT_FAILURE, "opening /dev/mem");
     69  1.1  christos 		LIST_INIT(&maplist);
     70  1.1  christos 	}
     71  1.1  christos }
     72  1.1  christos 
     73  1.1  christos static struct acpi_user_mapping *
     74  1.1  christos acpi_user_find_mapping(vm_offset_t pa, size_t size)
     75  1.1  christos {
     76  1.1  christos 	struct	acpi_user_mapping *map;
     77  1.1  christos 
     78  1.1  christos 	/* First search for an existing mapping */
     79  1.1  christos 	for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {
     80  1.1  christos 		if (map->pa <= pa && map->size >= pa + size - map->pa)
     81  1.2    cegger 			return map;
     82  1.1  christos 	}
     83  1.1  christos 
     84  1.1  christos 	/* Then create a new one */
     85  1.1  christos 	size = round_page(pa + size) - trunc_page(pa);
     86  1.1  christos 	pa = trunc_page(pa);
     87  1.1  christos 	map = malloc(sizeof(struct acpi_user_mapping));
     88  1.1  christos 	if (!map)
     89  1.2    cegger 		errx(EXIT_FAILURE, "out of memory");
     90  1.1  christos 	map->pa = pa;
     91  1.1  christos 	map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);
     92  1.1  christos 	map->size = size;
     93  1.1  christos 	if ((intptr_t) map->va == -1)
     94  1.2    cegger 		err(EXIT_FAILURE, "can't map address");
     95  1.1  christos 	LIST_INSERT_HEAD(&maplist, map, link);
     96  1.1  christos 
     97  1.2    cegger 	return map;
     98  1.2    cegger }
     99  1.2    cegger 
    100  1.2    cegger static ACPI_TABLE_RSDP *
    101  1.2    cegger acpi_get_rsdp(u_long addr)
    102  1.2    cegger {
    103  1.2    cegger 	ACPI_TABLE_RSDP rsdp;
    104  1.2    cegger 	size_t len;
    105  1.2    cegger 
    106  1.2    cegger 	/* Read in the table signature and check it. */
    107  1.2    cegger 	pread(acpi_mem_fd, &rsdp, 8, addr);
    108  1.2    cegger 	if (memcmp(rsdp.Signature, "RSD PTR ", 8))
    109  1.2    cegger 		return (NULL);
    110  1.2    cegger 
    111  1.2    cegger 	/* Read the entire table. */
    112  1.2    cegger 	pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr);
    113  1.2    cegger 
    114  1.2    cegger 	/* Check the standard checksum. */
    115  1.2    cegger 	if (acpi_checksum(&rsdp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
    116  1.2    cegger 		return (NULL);
    117  1.2    cegger 
    118  1.2    cegger 	/* Check extended checksum if table version >= 2. */
    119  1.2    cegger 	if (rsdp.Revision >= 2 &&
    120  1.2    cegger 	    acpi_checksum(&rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
    121  1.2    cegger 		return (NULL);
    122  1.2    cegger 
    123  1.2    cegger 	/* If the revision is 0, assume a version 1 length. */
    124  1.2    cegger 	if (rsdp.Revision == 0)
    125  1.2    cegger 		len = ACPI_RSDP_REV0_SIZE;
    126  1.2    cegger 	else
    127  1.2    cegger 		len = rsdp.Length;
    128  1.2    cegger 
    129  1.2    cegger 	return (acpi_map_physical(addr, len));
    130  1.2    cegger }
    131  1.2    cegger 
    132  1.2    cegger static ACPI_TABLE_RSDP *
    133  1.2    cegger acpi_scan_rsd_ptr(void)
    134  1.2    cegger {
    135  1.2    cegger #if defined(__amd64__) || defined(__i386__)
    136  1.2    cegger 	ACPI_TABLE_RSDP *rsdp;
    137  1.2    cegger 	u_long		addr, end;
    138  1.2    cegger 
    139  1.2    cegger 	/*
    140  1.2    cegger 	 * On ia32, scan physical memory for the RSD PTR if above failed.
    141  1.2    cegger 	 * According to section 5.2.2 of the ACPI spec, we only consider
    142  1.2    cegger 	 * two regions for the base address:
    143  1.2    cegger 	 * 1. EBDA (1 KB area addressed by the 16 bit pointer at 0x40E
    144  1.2    cegger 	 * 2. High memory (0xE0000 - 0xFFFFF)
    145  1.2    cegger 	 */
    146  1.2    cegger 	addr = ACPI_EBDA_PTR_LOCATION;
    147  1.2    cegger 	pread(acpi_mem_fd, &addr, sizeof(uint16_t), addr);
    148  1.2    cegger 	addr <<= 4;
    149  1.2    cegger 	end = addr + ACPI_EBDA_WINDOW_SIZE;
    150  1.2    cegger 	for (; addr < end; addr += 16)
    151  1.2    cegger 		if ((rsdp = acpi_get_rsdp(addr)) != NULL)
    152  1.2    cegger 			return rsdp;
    153  1.2    cegger 	addr = ACPI_HI_RSDP_WINDOW_BASE;
    154  1.2    cegger 	end = addr + ACPI_HI_RSDP_WINDOW_SIZE;
    155  1.2    cegger 	for (; addr < end; addr += 16)
    156  1.2    cegger 		if ((rsdp = acpi_get_rsdp(addr)) != NULL)
    157  1.2    cegger 			return rsdp;
    158  1.2    cegger #endif /* __amd64__ || __i386__ */
    159  1.2    cegger 	return NULL;
    160  1.1  christos }
    161  1.1  christos 
    162  1.1  christos /*
    163  1.1  christos  * Public interfaces
    164  1.1  christos  */
    165  1.2    cegger ACPI_TABLE_RSDP *
    166  1.2    cegger acpi_find_rsd_ptr(void)
    167  1.1  christos {
    168  1.3   msaitoh 	ACPI_TABLE_RSDP	*rsdp;
    169  1.3   msaitoh 	u_long		addr = 0;
    170  1.3   msaitoh 	size_t		len = sizeof(addr);
    171  1.1  christos 
    172  1.1  christos 	acpi_user_init();
    173  1.3   msaitoh 
    174  1.3   msaitoh 	if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) != 0)
    175  1.3   msaitoh 		addr = 0;
    176  1.3   msaitoh 	if (addr != 0 && (rsdp = acpi_get_rsdp(addr)) != NULL)
    177  1.3   msaitoh 		return rsdp;
    178  1.1  christos 
    179  1.2    cegger 	return acpi_scan_rsd_ptr();
    180  1.1  christos }
    181  1.1  christos 
    182  1.1  christos void *
    183  1.1  christos acpi_map_physical(vm_offset_t pa, size_t size)
    184  1.1  christos {
    185  1.1  christos 	struct	acpi_user_mapping *map;
    186  1.1  christos 
    187  1.1  christos 	map = acpi_user_find_mapping(pa, size);
    188  1.1  christos 	return (map->va + (pa - map->pa));
    189  1.1  christos }
    190  1.1  christos 
    191  1.2    cegger ACPI_TABLE_HEADER *
    192  1.2    cegger dsdt_load_file(char *infile)
    193  1.1  christos {
    194  1.2    cegger 	ACPI_TABLE_HEADER *sdt;
    195  1.2    cegger 	uint8_t		*dp;
    196  1.2    cegger 	struct stat	 sb;
    197  1.1  christos 
    198  1.2    cegger 	if ((acpi_mem_fd = open(infile, O_RDONLY)) == -1)
    199  1.2    cegger 		errx(EXIT_FAILURE, "opening %s", infile);
    200  1.1  christos 
    201  1.1  christos 	LIST_INIT(&maplist);
    202  1.1  christos 
    203  1.2    cegger 	if (fstat(acpi_mem_fd, &sb) == -1)
    204  1.2    cegger 		errx(EXIT_FAILURE, "fstat %s", infile);
    205  1.1  christos 
    206  1.1  christos 	dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);
    207  1.2    cegger 	if (dp == NULL)
    208  1.2    cegger 		errx(EXIT_FAILURE, "mmap %s", infile);
    209  1.2    cegger 
    210  1.2    cegger 	sdt = (ACPI_TABLE_HEADER *)dp;
    211  1.2    cegger 	if (strncmp((char *)dp, ACPI_SIG_DSDT, 4) != 0)
    212  1.2    cegger 	    errx(EXIT_FAILURE, "DSDT signature mismatch");
    213  1.2    cegger 
    214  1.2    cegger 	if (sdt->Length > sb.st_size)
    215  1.2    cegger 	    errx(EXIT_FAILURE,
    216  1.2    cegger 		"corrupt DSDT: table size (%"PRIu32" bytes), file size "
    217  1.2    cegger 		"(%"PRIu64" bytes)",
    218  1.2    cegger 		sdt->Length, sb.st_size);
    219  1.1  christos 
    220  1.2    cegger         if (acpi_checksum(sdt, sdt->Length) != 0)
    221  1.2    cegger 	    errx(EXIT_FAILURE, "DSDT checksum mismatch");
    222  1.1  christos 
    223  1.2    cegger 	return sdt;
    224  1.1  christos }
    225