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#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32#include <memory.h>
33#include "acpi/acpi.h"
34
35
36char *flags_to_string(char *buffer, uint16_t flags)
37{
38 memset(buffer, 0, sizeof(buffer));
39 strcpy(buffer, "default");
40 if ((flags & POLARITY_ACTIVE_HIGH) == POLARITY_ACTIVE_HIGH)
41 strcpy(buffer, "high");
42 else if ((flags & POLARITY_ACTIVE_LOW) == POLARITY_ACTIVE_LOW)
43 strcpy(buffer, "low");
44 if ((flags & TRIGGER_EDGE) == TRIGGER_EDGE)
45 strncat(buffer, " edge", 5);
46 else if ((flags & TRIGGER_LEVEL) == TRIGGER_LEVEL)
47 strncat(buffer, " level", 6);
48 else
49 strncat(buffer, " default", 8);
50
51 return buffer;
52}
53
54void dbg_printf(const char *fmt, ...)
55{
56 va_list args;
57 va_start(args, fmt);
58 vfprintf(stderr, fmt, args);
59 va_end(args);
60}
61
62void init_acpi(s_acpi * acpi)
63{
64 memset(acpi, 0, sizeof(s_acpi));
65}
66
67int parse_acpi(s_acpi * acpi)
68{
69 int ret_val;
70 init_acpi(acpi);
71
72
73 if ((ret_val = search_rsdp(acpi)) != RSDP_TABLE_FOUND)
74 return ret_val;
75
76
77
78 parse_rsdt(&acpi->rsdt);
79 if (parse_xsdt(acpi) != XSDT_TABLE_FOUND) {
80 DEBUG_PRINT(("XSDT Detection failed\n"));
81 for (int table=0; table <acpi->rsdt.entry_count; table++) {
82 parse_header((uint64_t *)acpi->rsdt.entry[table],acpi);
83 }
84 }
85 return ACPI_FOUND;
86}
87
88void get_acpi_description_header(uint8_t * q, s_acpi_description_header * adh)
89{
90 cp_str_struct(adh->signature);
91 cp_struct(&adh->length);
92 cp_struct(&adh->revision);
93 cp_struct(&adh->checksum);
94 cp_str_struct(adh->oem_id);
95 cp_str_struct(adh->oem_table_id);
96 cp_struct(&adh->oem_revision);
97 cp_str_struct(adh->creator_id);
98 cp_struct(&adh->creator_revision);
99 DEBUG_PRINT(("acpi_header at %p = %s | %s | %s\n", q, adh->signature,adh->oem_id, adh->creator_id ));
100}
101
102bool parse_header(uint64_t *address, s_acpi *acpi) {
103 s_acpi_description_header adh;
104 memset(&adh, 0, sizeof(adh));
105
106 get_acpi_description_header((uint8_t *)address, &adh);
107
108
109
110 if (memcmp(adh.signature, FACP, sizeof(FACP) - 1) == 0) {
111 DEBUG_PRINT(("FACP table found\n"));
112 s_fadt *f = &acpi->fadt;
113 s_facs *fa = &acpi->facs;
114 s_dsdt *d = &acpi->dsdt;
115
116 f->valid = true;
117 f->address = address;
118 memcpy(&f->header, &adh, sizeof(adh));
119 parse_fadt(f);
120
121
122
123 if (fa->valid == false) {
124 fa->address = (uint64_t *)f->x_firmware_ctrl;
125 parse_facs(fa);
126 if (fa->valid == false) {
127
128 fa->address = (uint64_t *)f->firmware_ctrl;
129 parse_facs(fa);
130 }
131 }
132
133
134
135 if (d->valid == false) {
136 s_acpi_description_header new_adh;
137 get_acpi_description_header((uint8_t *)f->x_dsdt,
138 &new_adh);
139 if (memcmp(new_adh.signature, DSDT, sizeof(DSDT) - 1) == 0) {
140 DEBUG_PRINT(("DSDT table found via x_dsdt\n"));
141 d->valid = true;
142 d->address = (uint64_t *)f->x_dsdt;
143 memcpy(&d->header, &new_adh, sizeof(new_adh));
144 parse_dsdt(d);
145 } else {
146
147 get_acpi_description_header((uint8_t *)f->dsdt_address,
148 &new_adh);
149 if (memcmp(new_adh.signature, DSDT, sizeof(DSDT) - 1) ==
150 0) {
151 DEBUG_PRINT(("DSDT table found via dsdt_address\n"));
152 d->valid = true;
153 d->address = (uint64_t *)f->dsdt_address;
154 memcpy(&d->header, &new_adh, sizeof(new_adh));
155 parse_dsdt(d);
156 }
157 }
158 }
159 }
160 else if (memcmp(adh.signature, APIC, sizeof(APIC) - 1) == 0) {
161 DEBUG_PRINT(("MADT table found\n"));
162 s_madt *m = &acpi->madt;
163
164 m->valid = true;
165 m->address =address;
166 memcpy(&m->header, &adh, sizeof(adh));
167 parse_madt(acpi);
168 } else if (memcmp(adh.signature, DSDT, sizeof(DSDT) - 1) == 0) {
169 DEBUG_PRINT(("DSDT table found\n"));
170 s_dsdt *d = &acpi->dsdt;
171
172 d->valid = true;
173 d->address = address;
174 memcpy(&d->header, &adh, sizeof(adh));
175 parse_dsdt(d);
176
177 } else if ((memcmp(adh.signature, SSDT, sizeof(SSDT) - 1) == 0)
178 || (memcmp(adh.signature, PSDT, sizeof(PSDT) - 1) == 0)) {
179
180 DEBUG_PRINT(("SSDT table found with %s \n",adh.signature));
181
182 if ((acpi->ssdt_count >= MAX_SSDT - 1))
183 return false;
184
185
186 if ((acpi->ssdt[acpi->ssdt_count] =
187 malloc(sizeof(s_ssdt))) == NULL)
188 return false;
189 s_ssdt *s = acpi->ssdt[acpi->ssdt_count];
190
191
192 s->valid = true;
193 s->address = address;
194 memcpy(&s->header, &adh, sizeof(adh));
195
196
197 uint32_t definition_block_size = adh.length - ACPI_HEADER_SIZE;
198 if ((s->definition_block =
199 malloc(definition_block_size)) != NULL) {
200 memcpy(s->definition_block,
201 (s->address + ACPI_HEADER_SIZE),
202 definition_block_size);
203 }
204
205 acpi->ssdt_count++;
206 } else if (memcmp(adh.signature, SBST, sizeof(SBST) - 1) == 0) {
207 DEBUG_PRINT(("SBST table found\n"));
208 s_sbst *s = &acpi->sbst;
209
210 s->valid = true;
211 s->address = address;
212 memcpy(&s->header, &adh, sizeof(adh));
213 parse_sbst(s);
214 } else if (memcmp(adh.signature, ECDT, sizeof(ECDT) - 1) == 0) {
215 DEBUG_PRINT(("ECDT table found\n"));
216 s_ecdt *e = &acpi->ecdt;
217
218 e->valid = true;
219 e->address = address;
220 memcpy(&e->header, &adh, sizeof(adh));
221 parse_ecdt(e);
222 } else if (memcmp(adh.signature, HPET, sizeof(HPET) - 1) == 0) {
223 DEBUG_PRINT(("HPET table found\n"));
224 s_hpet *h = &acpi->hpet;
225
226 h->valid = true;
227 h->address = address;
228 memcpy(&h->header, &adh, sizeof(adh));
229 } else if (memcmp(adh.signature, TCPA, sizeof(TCPA) - 1) == 0) {
230 DEBUG_PRINT(("TCPA table found\n"));
231 s_tcpa *t = &acpi->tcpa;
232
233 t->valid = true;
234 t->address = address;
235 memcpy(&t->header, &adh, sizeof(adh));
236 } else if (memcmp(adh.signature, MCFG, sizeof(MCFG) - 1) == 0) {
237 DEBUG_PRINT(("MCFG table found\n"));
238 s_mcfg *m = &acpi->mcfg;
239
240 m->valid = true;
241 m->address = address;
242 memcpy(&m->header, &adh, sizeof(adh));
243 } else if (memcmp(adh.signature, SLIC, sizeof(SLIC) - 1) == 0) {
244 DEBUG_PRINT(("SLIC table found\n"));
245 s_slic *s = &acpi->slic;
246
247 s->valid = true;
248 s->address = address;
249 memcpy(&s->header, &adh, sizeof(adh));
250 } else if (memcmp(adh.signature, BOOT, sizeof(BOOT) - 1) == 0) {
251 DEBUG_PRINT(("BOOT table found\n"));
252 s_boot *b = &acpi->boot;
253
254 b->valid = true;
255 b->address = address;
256 memcpy(&b->header, &adh, sizeof(adh));
257 }
258
259 return true;
260}
261