2025-07-23 07:17:27 -07:00
|
|
|
/* Copyright (C) 2025 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-18 09:14:48 -07:00
|
|
|
#include "precompiled.h"
|
2020-12-09 06:39:14 -08:00
|
|
|
#include "lib/sysdep/os/win/acpi.h"
|
2009-04-18 09:14:48 -07:00
|
|
|
|
2011-04-03 08:38:48 -07:00
|
|
|
#include "lib/byte_order.h"
|
2009-04-18 09:14:48 -07:00
|
|
|
#include "lib/module_init.h"
|
|
|
|
|
|
2020-12-09 06:39:14 -08:00
|
|
|
#include "lib/sysdep/os/win/wfirmware.h"
|
2010-07-08 03:33:38 -07:00
|
|
|
|
2024-09-16 14:02:32 -07:00
|
|
|
#include <atomic>
|
2025-07-23 07:17:27 -07:00
|
|
|
#include <cctype>
|
2024-09-16 14:02:32 -07:00
|
|
|
|
2009-04-18 09:14:48 -07:00
|
|
|
#pragma pack(1)
|
|
|
|
|
|
|
|
|
|
typedef const volatile u8* PCV_u8;
|
|
|
|
|
typedef const volatile AcpiTable* PCV_AcpiTable;
|
|
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// table
|
|
|
|
|
|
|
|
|
|
static AcpiTable* AllocateTable(size_t size)
|
|
|
|
|
{
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(size >= sizeof(AcpiTable));
|
2010-07-08 03:33:38 -07:00
|
|
|
return (AcpiTable*)malloc(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
|
static void DeallocateTable(const T* table)
|
|
|
|
|
{
|
|
|
|
|
free((void*)table);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-04-18 09:14:48 -07:00
|
|
|
// return 8-bit checksum of a buffer (should be 0)
|
|
|
|
|
static u8 ComputeChecksum(PCV_u8 buf, size_t numBytes)
|
|
|
|
|
{
|
|
|
|
|
// (can't use std::accumulate - we need 8-bit wraparound)
|
|
|
|
|
u8 sum = 0;
|
|
|
|
|
for(PCV_u8 p = buf; p < buf+numBytes; p++)
|
2011-07-16 10:27:07 -07:00
|
|
|
sum = u8((sum + *p) & 0xFF);
|
2009-04-18 09:14:48 -07:00
|
|
|
return sum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
static bool ValidateTable(const AcpiTable* table, const char* signature = 0)
|
|
|
|
|
{
|
|
|
|
|
if(!table)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// caller knowns the signature; make sure it matches
|
|
|
|
|
if(signature)
|
|
|
|
|
{
|
|
|
|
|
if(memcmp(table->signature, signature, 4) != 0)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-07-12 06:01:12 -07:00
|
|
|
// no specific signature is called for, just validate the characters.
|
2010-07-08 03:33:38 -07:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for(size_t i = 0; i < 4; i++)
|
|
|
|
|
{
|
2010-07-12 06:01:12 -07:00
|
|
|
const char c = table->signature[i];
|
|
|
|
|
// "ASF!" and "____" have been encountered
|
|
|
|
|
if(!isalpha(c) && c != '_' && c != '!')
|
2010-07-08 03:33:38 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// must be at least as large as the common header
|
|
|
|
|
if(table->size < sizeof(AcpiTable))
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// checksum of table must be 0
|
|
|
|
|
// .. AMIBIOS OEMB table has an incorrect checksum (off-by-one),
|
|
|
|
|
// so don't complain about any OEM tables (ignored anyway).
|
|
|
|
|
const bool isOemTable = (memcmp(table->signature, "OEM", 3) == 0);
|
|
|
|
|
if(!isOemTable)
|
|
|
|
|
{
|
|
|
|
|
if(ComputeChecksum((PCV_u8)table, table->size) != 0)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-16 14:02:32 -07:00
|
|
|
static void AllocateAndCopyTables(std::atomic<const AcpiTable**>& tables, size_t& numTables)
|
2009-04-18 09:14:48 -07:00
|
|
|
{
|
2011-04-03 08:38:48 -07:00
|
|
|
const wfirmware::Provider provider = FOURCC_BE('A','C','P','I');
|
|
|
|
|
const wfirmware::TableIds tableIDs = wfirmware::GetTableIDs(provider);
|
2009-04-18 09:14:48 -07:00
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
numTables = tableIDs.size();
|
|
|
|
|
tables = new const AcpiTable*[numTables];
|
2009-04-18 09:14:48 -07:00
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
for(size_t i = 0; i < numTables; i++)
|
2009-04-18 09:14:48 -07:00
|
|
|
{
|
2011-04-03 08:38:48 -07:00
|
|
|
wfirmware::Table table = wfirmware::GetTable(provider, tableIDs[i]);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(!table.empty());
|
2010-07-08 03:33:38 -07:00
|
|
|
tables[i] = AllocateTable(table.size());
|
|
|
|
|
memcpy((void*)tables[i], &table[0], table.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// to prevent callers from choking on invalid tables, we
|
|
|
|
|
// zero out the corresponding tables[] entries.
|
|
|
|
|
for(size_t i = 0; i < numTables; i++)
|
|
|
|
|
{
|
|
|
|
|
if(!ValidateTable(tables[i]))
|
|
|
|
|
{
|
|
|
|
|
DeallocateTable(tables[i]);
|
|
|
|
|
tables[i] = 0;
|
|
|
|
|
}
|
2009-04-18 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
// note: avoid global std::map etc. because we may be called before _cinit
|
2024-09-16 14:02:32 -07:00
|
|
|
static std::atomic<const AcpiTable**> tables{ nullptr }; // tables == nullptr <=> not initialized
|
2010-07-08 03:33:38 -07:00
|
|
|
static const AcpiTable* invalidTables; // tables == &invalidTables => init failed
|
2009-04-18 09:14:48 -07:00
|
|
|
static size_t numTables;
|
|
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
void acpi_Shutdown()
|
2009-04-18 09:14:48 -07:00
|
|
|
{
|
|
|
|
|
if(tables)
|
|
|
|
|
{
|
|
|
|
|
for(size_t i = 0; i < numTables; i++)
|
|
|
|
|
DeallocateTable(tables[i]);
|
2010-07-08 03:33:38 -07:00
|
|
|
SAFE_ARRAY_DELETE(tables);
|
|
|
|
|
numTables = 0;
|
2009-04-18 09:14:48 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const AcpiTable* acpi_GetTable(const char* signature)
|
|
|
|
|
{
|
2024-09-16 14:02:32 -07:00
|
|
|
const AcpiTable** initial{ nullptr };
|
|
|
|
|
if(tables.compare_exchange_strong(initial, &invalidTables))
|
2010-07-08 03:33:38 -07:00
|
|
|
AllocateAndCopyTables(tables, numTables);
|
|
|
|
|
|
2009-04-18 09:14:48 -07:00
|
|
|
// (typically only a few tables, linear search is OK)
|
|
|
|
|
for(size_t i = 0; i < numTables; i++)
|
|
|
|
|
{
|
|
|
|
|
const AcpiTable* table = tables[i];
|
|
|
|
|
if(!table)
|
|
|
|
|
continue; // skip invalid tables, e.g. OEM (see above)
|
|
|
|
|
if(strncmp(table->signature, signature, 4) == 0)
|
|
|
|
|
return table;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-08 03:33:38 -07:00
|
|
|
return 0; // no matching AND valid table found
|
2009-04-18 09:14:48 -07:00
|
|
|
}
|