Path: blob/main/usr.sbin/acpi/acpidump/acpi_user.c
105911 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 1999 Doug Rabson4* Copyright (c) 2000 Mitsuru IWASAKI <[email protected]>5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/mman.h>31#include <sys/queue.h>32#include <sys/stat.h>33#include <sys/sysctl.h>3435#include <err.h>36#include <fcntl.h>37#include <kenv.h>38#include <stdlib.h>39#include <string.h>40#include <unistd.h>4142#include "acpidump.h"4344static char acpi_rsdp[] = "acpi.rsdp";45static char machdep_acpi_root[] = "machdep.acpi_root";46static int acpi_mem_fd = -1;4748struct acpi_user_mapping {49LIST_ENTRY(acpi_user_mapping) link;50vm_offset_t pa;51caddr_t va;52size_t size;53};5455static LIST_HEAD(acpi_user_mapping_list, acpi_user_mapping) maplist;5657static void58acpi_user_init(void)59{6061if (acpi_mem_fd == -1) {62acpi_mem_fd = open("/dev/mem", O_RDONLY);63if (acpi_mem_fd == -1)64err(1, "opening /dev/mem");65LIST_INIT(&maplist);66}67}6869static struct acpi_user_mapping *70acpi_user_find_mapping(vm_offset_t pa, size_t size)71{72struct acpi_user_mapping *map;7374/* First search for an existing mapping */75for (map = LIST_FIRST(&maplist); map; map = LIST_NEXT(map, link)) {76if (map->pa <= pa && map->size >= pa + size - map->pa)77return (map);78}7980/* Then create a new one */81size = round_page(pa + size) - trunc_page(pa);82pa = trunc_page(pa);83map = malloc(sizeof(struct acpi_user_mapping));84if (!map)85errx(1, "out of memory");86map->pa = pa;87map->va = mmap(0, size, PROT_READ, MAP_SHARED, acpi_mem_fd, pa);88map->size = size;89if ((intptr_t) map->va == -1)90err(1, "can't map address");91LIST_INSERT_HEAD(&maplist, map, link);9293return (map);94}9596static ACPI_TABLE_RSDP *97acpi_get_rsdp(u_long addr)98{99ACPI_TABLE_RSDP rsdp;100size_t len;101102/* Read in the table signature and check it. */103pread(acpi_mem_fd, &rsdp, 8, addr);104if (memcmp(rsdp.Signature, "RSD PTR ", 8))105return (NULL);106107/* Read the entire table. */108pread(acpi_mem_fd, &rsdp, sizeof(rsdp), addr);109110/* Check the standard checksum. */111if (acpi_checksum(&rsdp, ACPI_RSDP_CHECKSUM_LENGTH) != 0)112return (NULL);113114/* Check extended checksum if table version >= 2. */115if (rsdp.Revision >= 2 &&116acpi_checksum(&rsdp, ACPI_RSDP_XCHECKSUM_LENGTH) != 0)117return (NULL);118119/* If the revision is 0, assume a version 1 length. */120if (rsdp.Revision == 0)121len = sizeof(ACPI_RSDP_COMMON);122else123len = rsdp.Length;124125return (acpi_map_physical(addr, len));126}127128static ACPI_TABLE_RSDP *129acpi_scan_rsd_ptr(void)130{131#if defined(__amd64__) || defined(__i386__)132ACPI_TABLE_RSDP *rsdp;133u_long addr, end;134135/*136* On ia32, scan physical memory for the RSD PTR if above failed.137* According to section 5.2.2 of the ACPI spec, we only consider138* two regions for the base address:139* 1. EBDA (1 KB area addressed by the 16 bit pointer at 0x40E140* 2. High memory (0xE0000 - 0xFFFFF)141*/142addr = ACPI_EBDA_PTR_LOCATION;143pread(acpi_mem_fd, &addr, sizeof(uint16_t), addr);144addr <<= 4;145end = addr + ACPI_EBDA_WINDOW_SIZE;146for (; addr < end; addr += 16)147if ((rsdp = acpi_get_rsdp(addr)) != NULL)148return (rsdp);149addr = ACPI_HI_RSDP_WINDOW_BASE;150end = addr + ACPI_HI_RSDP_WINDOW_SIZE;151for (; addr < end; addr += 16)152if ((rsdp = acpi_get_rsdp(addr)) != NULL)153return (rsdp);154#endif /* __amd64__ || __i386__ */155return (NULL);156}157158/*159* Public interfaces160*/161ACPI_TABLE_RSDP *162acpi_find_rsd_ptr(void)163{164ACPI_TABLE_RSDP *rsdp;165char buf[20];166u_long addr;167size_t len;168169acpi_user_init();170171addr = 0;172173/* Attempt to use kenv or sysctl to find RSD PTR record. */174if (kenv(KENV_GET, acpi_rsdp, buf, 20) > 0)175addr = strtoul(buf, NULL, 0);176if (addr == 0) {177len = sizeof(addr);178if (sysctlbyname(machdep_acpi_root, &addr, &len, NULL, 0) != 0)179addr = 0;180}181if (addr != 0 && (rsdp = acpi_get_rsdp(addr)) != NULL)182return (rsdp);183184return (acpi_scan_rsd_ptr());185}186187void *188acpi_map_physical(vm_offset_t pa, size_t size)189{190struct acpi_user_mapping *map;191192map = acpi_user_find_mapping(pa, size);193return (map->va + (pa - map->pa));194}195196ACPI_TABLE_HEADER *197dsdt_load_file(char *infile)198{199ACPI_TABLE_HEADER *sdt;200uint8_t *dp;201struct stat sb;202203if ((acpi_mem_fd = open(infile, O_RDONLY)) == -1)204errx(1, "opening %s", infile);205206LIST_INIT(&maplist);207208if (fstat(acpi_mem_fd, &sb) == -1)209errx(1, "fstat %s", infile);210211dp = mmap(0, sb.st_size, PROT_READ, MAP_PRIVATE, acpi_mem_fd, 0);212if (dp == NULL)213errx(1, "mmap %s", infile);214215sdt = (ACPI_TABLE_HEADER *)dp;216if (strncmp(dp, ACPI_SIG_DSDT, 4) != 0 ||217acpi_checksum(sdt, sdt->Length) != 0)218return (NULL);219220return (sdt);221}222223224