vaseboot/VasEBoot-core/kern/acpi.c

136 lines
3.6 KiB
C

/*
* VAS_EBOOT -- GRand Unified Bootloader
* Copyright (C) 2012 Free Software Foundation, Inc.
*
* VAS_EBOOT is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* VAS_EBOOT is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with VAS_EBOOT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <VasEBoot/types.h>
#include <VasEBoot/time.h>
#include <VasEBoot/misc.h>
#include <VasEBoot/mm.h>
#include <VasEBoot/acpi.h>
/* Simple checksum by summing all bytes. Used by ACPI and SMBIOS. */
VasEBoot_uint8_t
VasEBoot_byte_checksum (void *base, VasEBoot_size_t size)
{
VasEBoot_uint8_t *ptr;
VasEBoot_uint8_t ret = 0;
for (ptr = (VasEBoot_uint8_t *) base; ptr < ((VasEBoot_uint8_t *) base) + size;
ptr++)
ret += *ptr;
return ret;
}
static void *
VasEBoot_acpi_rsdt_find_table (struct VasEBoot_acpi_table_header *rsdt, const char *sig)
{
VasEBoot_size_t s;
VasEBoot_unaligned_uint32_t *ptr;
if (!rsdt)
return 0;
if (VasEBoot_memcmp (rsdt->signature, "RSDT", 4) != 0)
return 0;
ptr = (VasEBoot_unaligned_uint32_t *) (rsdt + 1);
s = (rsdt->length - sizeof (*rsdt)) / sizeof (VasEBoot_uint32_t);
for (; s; s--, ptr++)
{
struct VasEBoot_acpi_table_header *tbl;
/* Skip NULL entries in RSDT/XSDT. */
if (!ptr->val)
continue;
tbl = (struct VasEBoot_acpi_table_header *) (VasEBoot_addr_t) ptr->val;
if (VasEBoot_memcmp (tbl->signature, sig, 4) == 0)
return tbl;
}
return 0;
}
static void *
VasEBoot_acpi_xsdt_find_table (struct VasEBoot_acpi_table_header *xsdt, const char *sig)
{
VasEBoot_size_t s;
VasEBoot_unaligned_uint64_t *ptr;
if (!xsdt)
return 0;
if (VasEBoot_memcmp (xsdt->signature, "XSDT", 4) != 0)
return 0;
ptr = (VasEBoot_unaligned_uint64_t *) (xsdt + 1);
s = (xsdt->length - sizeof (*xsdt)) / sizeof (VasEBoot_uint64_t);
for (; s; s--, ptr++)
{
struct VasEBoot_acpi_table_header *tbl;
/* Skip NULL entries in RSDT/XSDT. */
if (!ptr->val)
continue;
#if VAS_EBOOT_CPU_SIZEOF_VOID_P != 8
if (ptr->val >> 32)
continue;
#endif
tbl = (struct VasEBoot_acpi_table_header *) (VasEBoot_addr_t) ptr->val;
if (VasEBoot_memcmp (tbl->signature, sig, 4) == 0)
return tbl;
}
return 0;
}
void *
VasEBoot_acpi_find_table (const char *sig)
{
struct VasEBoot_acpi_fadt *r = NULL;
struct VasEBoot_acpi_rsdp_v10 *rsdpv1;
struct VasEBoot_acpi_rsdp_v20 *rsdpv2;
rsdpv1 = VasEBoot_machine_acpi_get_rsdpv1 ();
if (rsdpv1)
r = VasEBoot_acpi_rsdt_find_table ((struct VasEBoot_acpi_table_header *)
(VasEBoot_addr_t) rsdpv1->rsdt_addr,
sig);
if (r)
return r;
rsdpv2 = VasEBoot_machine_acpi_get_rsdpv2 ();
if (rsdpv2
#if VAS_EBOOT_CPU_SIZEOF_VOID_P != 8
&& !(rsdpv2->xsdt_addr >> 32)
#endif
)
r = VasEBoot_acpi_xsdt_find_table ((struct VasEBoot_acpi_table_header *)
(VasEBoot_addr_t) rsdpv2->xsdt_addr,
sig);
if (r)
return r;
if (rsdpv2)
r = VasEBoot_acpi_rsdt_find_table ((struct VasEBoot_acpi_table_header *)
(VasEBoot_addr_t) rsdpv2->rsdpv1.rsdt_addr,
sig);
if (r)
return r;
return 0;
}
struct VasEBoot_acpi_fadt *
VasEBoot_acpi_find_fadt (void)
{
return VasEBoot_acpi_find_table (VAS_EBOOT_ACPI_FADT_SIGNATURE);
}