File: acpi.cpp

package info (click to toggle)
0ad 0.28.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 182,352 kB
  • sloc: cpp: 201,989; javascript: 19,730; ansic: 15,057; python: 6,597; sh: 2,046; perl: 1,232; xml: 543; java: 533; makefile: 105
file content (173 lines) | stat: -rw-r--r-- 4,808 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright (C) 2025 Wildfire Games.
 *
 * 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:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * 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.
 */

#include "precompiled.h"
#include "lib/sysdep/os/win/acpi.h"

#include "lib/byte_order.h"
#include "lib/module_init.h"

#include "lib/sysdep/os/win/wfirmware.h"

#include <atomic>
#include <cctype>

#pragma pack(1)

typedef const volatile u8* PCV_u8;
typedef const volatile AcpiTable* PCV_AcpiTable;


//-----------------------------------------------------------------------------
// table

static AcpiTable* AllocateTable(size_t size)
{
	ENSURE(size >= sizeof(AcpiTable));
	return (AcpiTable*)malloc(size);
}


template<typename T>
static void DeallocateTable(const T* table)
{
	free((void*)table);
}


// 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++)
		sum = u8((sum + *p) & 0xFF);
	return sum;
}


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;
	}
	// no specific signature is called for, just validate the characters.
	else
	{
		for(size_t i = 0; i < 4; i++)
		{
			const char c = table->signature[i];
			// "ASF!" and "____" have been encountered
			if(!isalpha(c) && c != '_' && c != '!')
				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;
}

static void AllocateAndCopyTables(std::atomic<const AcpiTable**>& tables, size_t& numTables)
{
	const wfirmware::Provider provider = FOURCC_BE('A','C','P','I');
	const wfirmware::TableIds tableIDs = wfirmware::GetTableIDs(provider);

	numTables = tableIDs.size();
	tables = new const AcpiTable*[numTables];

	for(size_t i = 0; i < numTables; i++)
	{
		wfirmware::Table table = wfirmware::GetTable(provider, tableIDs[i]);
		ENSURE(!table.empty());
		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;
		}
	}
}


//-----------------------------------------------------------------------------

// note: avoid global std::map etc. because we may be called before _cinit
static std::atomic<const AcpiTable**> tables{ nullptr };	// tables == nullptr <=> not initialized
static const AcpiTable* invalidTables;	// tables == &invalidTables => init failed
static size_t numTables;

void acpi_Shutdown()
{
	if(tables)
	{
		for(size_t i = 0; i < numTables; i++)
			DeallocateTable(tables[i]);
		SAFE_ARRAY_DELETE(tables);
		numTables = 0;
	}
}


const AcpiTable* acpi_GetTable(const char* signature)
{
	const AcpiTable** initial{ nullptr };
	if(tables.compare_exchange_strong(initial, &invalidTables))
		AllocateAndCopyTables(tables, numTables);

	// (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;
	}

	return 0;	// no matching AND valid table found
}