Home | History | Annotate | Line # | Download | only in acpidump
      1  1.6  jmcneill /* $NetBSD: acpi_user.c,v 1.6 2020/12/06 02:57:30 jmcneill 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.4   msaitoh  *	$FreeBSD: head/usr.sbin/acpi/acpidump/acpi_user.c 251186 2013-05-31 17:23:38Z jkim $
     30  1.1  christos  */
     31  1.2    cegger 
     32  1.1  christos #include <sys/cdefs.h>
     33  1.6  jmcneill __RCSID("$NetBSD: acpi_user.c,v 1.6 2020/12/06 02:57:30 jmcneill 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.6  jmcneill #include <errno.h>
     43  1.1  christos #include <fcntl.h>
     44  1.1  christos #include <stdlib.h>
     45  1.1  christos #include <string.h>
     46  1.1  christos #include <unistd.h>
     47  1.1  christos 
     48  1.1  christos #include "acpidump.h"
     49  1.1  christos 
     50  1.3   msaitoh static char	machdep_acpi_root[] = "hw.acpi.root";
     51  1.1  christos static int      acpi_mem_fd = -1;
     52  1.6  jmcneill static bool	acpi_mem_mmap = false;
     53  1.1  christos 
     54  1.1  christos struct acpi_user_mapping {
     55  1.1  christos 	LIST_ENTRY(acpi_user_mapping) link;
     56  1.1  christos 	vm_offset_t     pa;
     57  1.1  christos 	caddr_t         va;
     58  1.1  christos 	size_t          size;
     59  1.1  christos };
     60  1.1  christos 
     61  1.4   msaitoh static LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;
     62  1.1  christos 
     63  1.1  christos static void
     64  1.1  christos acpi_user_init(void)
     65  1.1  christos {
     66  1.6  jmcneill 	int saved_errno, use_devmem;
     67  1.6  jmcneill 	const char *s;
     68  1.1  christos 
     69  1.1  christos 	if (acpi_mem_fd == -1) {
     70  1.6  jmcneill 		s = getenv("ACPIDUMP_USE_DEVMEM");
     71  1.6  jmcneill 		use_devmem = s != NULL && *s == '1';
     72  1.6  jmcneill 		if (!use_devmem) {
     73  1.6  jmcneill 			acpi_mem_fd = open("/dev/acpi", O_RDONLY);
     74  1.6  jmcneill 		}
     75  1.6  jmcneill 		if (acpi_mem_fd == -1) {
     76  1.6  jmcneill 			saved_errno = errno;
     77  1.6  jmcneill 			acpi_mem_fd = open("/dev/mem", O_RDONLY);
     78  1.6  jmcneill 			if (acpi_mem_fd == -1) {
     79  1.6  jmcneill 				errno = saved_errno;
     80  1.6  jmcneill 			} else {
     81  1.6  jmcneill 				acpi_mem_mmap = true;
     82  1.6  jmcneill 			}
     83  1.6  jmcneill 		}
     84  1.6  jmcneill 		if (acpi_mem_fd == -1) {
     85  1.6  jmcneill 			err(EXIT_FAILURE, "opening /dev/acpi");
     86  1.6  jmcneill 		}
     87  1.1  christos 		LIST_INIT(&maplist);
     88  1.1  christos 	}
     89  1.1  christos }
     90  1.1  christos 
     91  1.6  jmcneill static void *
     92  1.6  jmcneill acpi_user_read_table(vm_offset_t pa, size_t psize)
     93  1.6  jmcneill {
     94  1.6  jmcneill 	void *data;
     95  1.6  jmcneill 	ssize_t len;
     96  1.6  jmcneill 
     97  1.6  jmcneill 	data = calloc(1, psize);
     98  1.6  jmcneill 	if (!data)
     99  1.6  jmcneill 		errx(EXIT_FAILURE, "out of memory");
    100  1.6  jmcneill 
    101  1.6  jmcneill 	len = pread(acpi_mem_fd, data, psize, pa);
    102  1.6  jmcneill 	if (len == -1)
    103  1.6  jmcneill 		errx(EXIT_FAILURE, "can't read table");
    104  1.6  jmcneill 
    105  1.6  jmcneill 	return data;
    106  1.6  jmcneill }
    107  1.6  jmcneill 
    108  1.1  christos static struct acpi_user_mapping *
    109  1.1  christos acpi_user_find_mapping(vm_offset_t pa, size_t size)
    110  1.1  christos {
    111  1.1  christos 	struct	acpi_user_mapping *map;
    112  1.1  christos 
    113  1.1  christos 	/* First search for an existing mapping */
    114  1.1  christos 	for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {
    115  1.1  christos 		if (map->pa <= pa && map->size >= pa + size - map->pa)
    116  1.2    cegger 			return map;
    117  1.1  christos 	}
    118  1.1  christos 
    119  1.1  christos 	/* Then create a new one */
    120  1.6  jmcneill 	if (acpi_mem_mmap) {
    121  1.6  jmcneill 		size = round_page(pa + size) - trunc_page(pa);
    122  1.6  jmcneill 		pa = trunc_page(pa);
    123  1.6  jmcneill 	}
    124  1.1  christos 	map = malloc(sizeof(struct acpi_user_mapping));
    125  1.1  christos 	if (!map)
    126  1.2    cegger 		errx(EXIT_FAILURE, "out of memory");
    127  1.1  christos 	map->pa = pa;
    128  1.6  jmcneill 	if (acpi_mem_mmap) {
    129  1.6  jmcneill 		map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);
    130  1.6  jmcneill 		if ((intptr_t) map->va == -1)
    131  1.6  jmcneill 			err(EXIT_FAILURE, "can't map address");
    132  1.6  jmcneill 	} else {
    133  1.6  jmcneill 		map->va = acpi_user_read_table(pa, size);
    134  1.6  jmcneill 	}
    135  1.1  christos 	map->size = size;
    136  1.1  christos 	LIST_INSERT_HEAD(&maplist, map, link);
    137  1.1  christos 
    138  1.2    cegger 	return map;
    139  1.2    cegger }
    140  1.2    cegger 
    141  1.2    cegger static ACPI_TABLE_RSDP *
    142  1.2    cegger acpi_get_rsdp(u_long addr)
    143  1.2    cegger {
    144  1.2    cegger 	ACPI_TABLE_RSDP rsdp;
    145  1.2    cegger 	size_t len;
    146  1.2    cegger 
    147  1.2    cegger 	/* Read in the table signature and check it. */
    148  1.2    cegger 	pread(acpi_mem_fd, &rsdp, 8, addr);
    149  1.2    cegger 	if (memcmp(rsdp.Signature, "RSD PTR ", 8))
    150  1.2    cegger 		return (NULL);
    151  1.2    cegger 
    152  1.2    cegger 	/* Read the entire table. */
    153  1.2    cegger 	pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr);
    154  1.2    cegger 
    155  1.2    cegger 	/* Check the standard checksum. */
    156  1.2    cegger 	if (acpi_checksum(&rsdp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)
    157  1.2    cegger 		return (NULL);
    158  1.2    cegger 
    159  1.2    cegger 	/* Check extended checksum if table version >= 2. */
    160  1.2    cegger 	if (rsdp.Revision >= 2 &&
    161  1.2    cegger 	    acpi_checksum(&rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)
    162  1.2    cegger 		return (NULL);
    163  1.2    cegger 
    164  1.2    cegger 	/* If the revision is 0, assume a version 1 length. */
    165  1.2    cegger 	if (rsdp.Revision == 0)
    166  1.4   msaitoh 		len = sizeof(ACPI_RSDP_COMMON);
    167  1.2    cegger 	else
    168  1.2    cegger 		len = rsdp.Length;
    169  1.2    cegger 
    170  1.2    cegger 	return (acpi_map_physical(addr, len));
    171  1.2    cegger }
    172  1.2    cegger 
    173  1.2    cegger static ACPI_TABLE_RSDP *
    174  1.2    cegger acpi_scan_rsd_ptr(void)
    175  1.2    cegger {
    176  1.2    cegger #if defined(__amd64__) || defined(__i386__)
    177  1.2    cegger 	ACPI_TABLE_RSDP *rsdp;
    178  1.2    cegger 	u_long		addr, end;
    179  1.2    cegger 
    180  1.2    cegger 	/*
    181  1.2    cegger 	 * On ia32, scan physical memory for the RSD PTR if above failed.
    182  1.2    cegger 	 * According to section 5.2.2 of the ACPI spec, we only consider
    183  1.2    cegger 	 * two regions for the base address:
    184  1.2    cegger 	 * 1. EBDA (1 KB area addressed by the 16 bit pointer at 0x40E
    185  1.2    cegger 	 * 2. High memory (0xE0000 - 0xFFFFF)
    186  1.2    cegger 	 */
    187  1.2    cegger 	addr = ACPI_EBDA_PTR_LOCATION;
    188  1.2    cegger 	pread(acpi_mem_fd, &addr, sizeof(uint16_t), addr);
    189  1.2    cegger 	addr <<= 4;
    190  1.2    cegger 	end = addr + ACPI_EBDA_WINDOW_SIZE;
    191  1.2    cegger 	for (; addr < end; addr += 16)
    192  1.2    cegger 		if ((rsdp = acpi_get_rsdp(addr)) != NULL)
    193  1.2    cegger 			return rsdp;
    194  1.2    cegger 	addr = ACPI_HI_RSDP_WINDOW_BASE;
    195  1.2    cegger 	end = addr + ACPI_HI_RSDP_WINDOW_SIZE;
    196  1.2    cegger 	for (; addr < end; addr += 16)
    197  1.2    cegger 		if ((rsdp = acpi_get_rsdp(addr)) != NULL)
    198  1.2    cegger 			return rsdp;
    199  1.2    cegger #endif /* __amd64__ || __i386__ */
    200  1.2    cegger 	return NULL;
    201  1.1  christos }
    202  1.1  christos 
    203  1.1  christos /*
    204  1.1  christos  * Public interfaces
    205  1.1  christos  */
    206  1.2    cegger ACPI_TABLE_RSDP *
    207  1.2    cegger acpi_find_rsd_ptr(void)
    208  1.1  christos {
    209  1.3   msaitoh 	ACPI_TABLE_RSDP	*rsdp;
    210  1.3   msaitoh 	u_long		addr = 0;
    211  1.3   msaitoh 	size_t		len = sizeof(addr);
    212  1.1  christos 
    213  1.1  christos 	acpi_user_init();
    214  1.3   msaitoh 
    215  1.3   msaitoh 	if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) != 0)
    216  1.3   msaitoh 		addr = 0;
    217  1.3   msaitoh 	if (addr != 0 && (rsdp = acpi_get_rsdp(addr)) != NULL)
    218  1.3   msaitoh 		return rsdp;
    219  1.1  christos 
    220  1.2    cegger 	return acpi_scan_rsd_ptr();
    221  1.1  christos }
    222  1.1  christos 
    223  1.1  christos void *
    224  1.1  christos acpi_map_physical(vm_offset_t pa, size_t size)
    225  1.1  christos {
    226  1.1  christos 	struct	acpi_user_mapping *map;
    227  1.1  christos 
    228  1.1  christos 	map = acpi_user_find_mapping(pa, size);
    229  1.1  christos 	return (map->va + (pa - map->pa));
    230  1.1  christos }
    231  1.1  christos 
    232  1.2    cegger ACPI_TABLE_HEADER *
    233  1.2    cegger dsdt_load_file(char *infile)
    234  1.1  christos {
    235  1.2    cegger 	ACPI_TABLE_HEADER *sdt;
    236  1.2    cegger 	uint8_t		*dp;
    237  1.2    cegger 	struct stat	 sb;
    238  1.1  christos 
    239  1.2    cegger 	if ((acpi_mem_fd = open(infile, O_RDONLY)) == -1)
    240  1.2    cegger 		errx(EXIT_FAILURE, "opening %s", infile);
    241  1.1  christos 
    242  1.1  christos 	LIST_INIT(&maplist);
    243  1.1  christos 
    244  1.2    cegger 	if (fstat(acpi_mem_fd, &sb) == -1)
    245  1.2    cegger 		errx(EXIT_FAILURE, "fstat %s", infile);
    246  1.1  christos 
    247  1.1  christos 	dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);
    248  1.5  riastrad 	if (dp == MAP_FAILED)
    249  1.2    cegger 		errx(EXIT_FAILURE, "mmap %s", infile);
    250  1.2    cegger 
    251  1.2    cegger 	sdt = (ACPI_TABLE_HEADER *)dp;
    252  1.2    cegger 	if (strncmp((char *)dp, ACPI_SIG_DSDT, 4) != 0)
    253  1.2    cegger 	    errx(EXIT_FAILURE, "DSDT signature mismatch");
    254  1.2    cegger 
    255  1.2    cegger 	if (sdt->Length > sb.st_size)
    256  1.2    cegger 	    errx(EXIT_FAILURE,
    257  1.2    cegger 		"corrupt DSDT: table size (%"PRIu32" bytes), file size "
    258  1.2    cegger 		"(%"PRIu64" bytes)",
    259  1.2    cegger 		sdt->Length, sb.st_size);
    260  1.1  christos 
    261  1.2    cegger         if (acpi_checksum(sdt, sdt->Length) != 0)
    262  1.2    cegger 	    errx(EXIT_FAILURE, "DSDT checksum mismatch");
    263  1.1  christos 
    264  1.2    cegger 	return sdt;
    265  1.1  christos }
    266