summaryrefslogtreecommitdiff
path: root/lib/acpi.c3l/acpi.c3
blob: 26021dc4841ca5715d9df0bfd5f60c43b1cc0054 (plain)
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
module acpi;

import c3os::util;

extern fn int memcmp(void*, void*, usz);

alias Oemid = char[6];

excuse INVALID_DESCRIPTION_HEADER;

enum DescriptionHeaderSignature
{
	AEST,
	AGDI,
	APIC,
	APMT,
	ASPT,
	BDAT,
	BERT,
	BGRT,
	BOOT,
	CCEL,
	CEDT,
	CPEP,
	CSRT,
	DBGP,
	DBG2,
	DMAR,
	DRTM,
	DSDT,
	DTPR,
	ECDT,
	EINJ,
	ERST,
	ETDT,
	FACP,
	FACS,
	FPDT,
	GTDT,
	HEST,
	HPET,
	IBFT,
	IERS,
	IORT,
	IOVT,
	IRDT,
	IVRS,
	KEYP,
	LPIT,
	MCFG,
	MCHI,
	MHSP,
	MISC,
	MPAM,
	MPST,
	MSCT,
	MSDM,
	NBFT,
	NFIT,
	NHLT,
	PCCT,
	PHAT,
	PMTT,
	PPTT,
	PRMT,
	PSDT,
	RASF,
	RAS2,
	RGRT,
	RHCT,
	RIMT,
	RQSC,
	RSDT,
	SBST,
	SDEI,
	SDEV,
	SLIC,
	SLIT,
	SPCR,
	SPMI,
	SRAT,
	SSDT,
	STAO,
	SVKL,
	SWFT,
	TCPA,
	TPM2,
	UEFI,
	WAET,
	WDAT,
	WDDT,
	WDRT,
	WPBT,
	WSMT,
	XENV,
	XSDT,
}

$assert(DescriptionHeader::size == 36);
$assert(RootSystemDescriptionPointer::size == 35);
struct RootSystemDescriptionPointer @packed
{
	char[8] signature;
	char checksum;
	Oemid oemid;
	char revision;
	uint rsdtAddress;
	uint length;
	ExtendedSystemDescriptionTable* xsdtAddress;
	char[3] _res;
}

fn bool RootSystemDescriptionPointer.is_valid(&self)
{
	if (memcmp(&self.signature, "RSD PTR ", 8) != 0) return false;
	if (self.revision != 2) return false;
	char sum = 0;
	for (int i = 0; i < 20; i++) sum += ((char*)self)[i];
	if (sum != 0) return false;
	for (int i = 0; i < RootSystemDescriptionPointer::size; i++) sum += ((char*)self)[i];
	return (sum == 0);
}

$assert(ExtendedSystemDescriptionTable::size == 36);
struct ExtendedSystemDescriptionTable @packed
{
	inline DescriptionHeader header;
	ulong[*] descriptionHeaders;
}

fn bool ExtendedSystemDescriptionTable.is_valid(&self)
{
	if (memcmp(&self.signature, "XSDT", 4) != 0) return false;
	if (self.revision != 1) return false;
	char sum = 0;
	for (uint i = 0; i < self.length; i++) sum += ((char*)self)[i];
	if (sum != 0) return false;
	return true;
}

struct DescriptionHeader @packed
{
	char[4] signature;
	uint length;
	char revision;
	char checksum;
	Oemid oemid;
	char[8] oemTableId;
	uint oemRevision;
	uint creatorId;
	uint creatorRevision;
}

fn bool DescriptionHeader.is_valid(&self)
{
	if (catch excuse = self.sig()) return false;
	char sum = 0;
	for (uint i = 0; i < self.length; i++) sum += ((char*)self)[i];
	if (sum != 0) return false;
	return true;
}

fn DescriptionHeaderSignature? DescriptionHeader.sig(&self)
{
	foreach (i, name : DescriptionHeaderSignature::names)
	{
		if (memcmp(name, &self.signature, 4) == 0) return (DescriptionHeaderSignature)i;
	}
	return INVALID_DESCRIPTION_HEADER~;
}