1 | /*␊ |
2 | * Copyright 2008 mackerintel␊ |
3 | * 2010 mojodojo, 2012 slice␊ |
4 | */␊ |
5 | ␊ |
6 | #include "libsaio.h"␊ |
7 | #include "boot.h"␊ |
8 | #include "bootstruct.h"␊ |
9 | #include "acpi.h"␊ |
10 | #include "efi_tables.h"␊ |
11 | #include "fake_efi.h"␊ |
12 | #include "acpi_patcher.h"␊ |
13 | #include "platform.h"␊ |
14 | #include "cpu.h"␊ |
15 | #include "aml_generator.h"␊ |
16 | #include "state_generator.h"␊ |
17 | ␊ |
18 | #ifndef DEBUG_ACPI␊ |
19 | #define DEBUG_ACPI 0␊ |
20 | ␉#endif␊ |
21 | ␊ |
22 | #if DEBUG_ACPI==2␊ |
23 | ␉#define DBG(x...) {printf(x); sleep(1);}␊ |
24 | #elif DEBUG_ACPI==1␊ |
25 | ␉#define DBG(x...) printf(x)␊ |
26 | #else␊ |
27 | ␉#define DBG(x...) msglog(x)␊ |
28 | #endif␊ |
29 | ␊ |
30 | // Slice: New signature compare function␊ |
31 | boolean_t tableSign(char *table, const char *sgn)␊ |
32 | {␊ |
33 | ␉int i;␊ |
34 | ␉for (i = 0; i < 4; i++)␊ |
35 | ␉{␊ |
36 | ␉␉if ((table[i] & ~0x20) != (sgn[i] & ~0x20))␊ |
37 | ␉␉{␊ |
38 | ␉␉␉return false;␊ |
39 | ␉␉}␊ |
40 | ␉}␊ |
41 | ␉return true;␊ |
42 | }␊ |
43 | ␊ |
44 | /* Gets the ACPI 1.0 RSDP address */␊ |
45 | static struct acpi_2_rsdp *getAddressOfAcpiTable()␊ |
46 | {␊ |
47 | ␉/* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */␊ |
48 | ␊ |
49 | ␉void *acpi_addr = (void*)ACPI_RANGE_START;␊ |
50 | ␊ |
51 | ␉for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)␊ |
52 | ␉{␊ |
53 | ␉␉if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)␊ |
54 | ␉␉{␊ |
55 | ␉␉␉uint8_t csum = checksum8(acpi_addr, 20);␊ |
56 | ␉␉␉if (csum == 0)␊ |
57 | ␉␉␉{␊ |
58 | ␉␉␉// Only return the table if it is a true version 1.0 table (Revision 0)␊ |
59 | ␉␉␉␉if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)␊ |
60 | ␉␉␉␉return acpi_addr;␊ |
61 | ␉␉␉}␊ |
62 | ␉␉}␊ |
63 | ␉}␊ |
64 | ␉return NULL;␊ |
65 | }␊ |
66 | ␊ |
67 | /* Gets the ACPI 2.0 RSDP address */␊ |
68 | static struct acpi_2_rsdp *getAddressOfAcpi20Table()␊ |
69 | {␊ |
70 | ␉/* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */␊ |
71 | ␊ |
72 | ␉void *acpi_addr = (void *)ACPI_RANGE_START;␊ |
73 | ␊ |
74 | ␉for(; acpi_addr <= (void *)ACPI_RANGE_END; acpi_addr += 16)␊ |
75 | ␉{␊ |
76 | ␉␉if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)␊ |
77 | ␉␉{␊ |
78 | ␉␉␉uint8_t csum = checksum8(acpi_addr, 20);␊ |
79 | ␊ |
80 | ␉␉␉/* Only assume this is a 2.0 or better table if the revision is greater than 0␊ |
81 | ␉␉␉ * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1␊ |
82 | ␉␉␉ * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.␊ |
83 | ␉␉␉ */␊ |
84 | ␊ |
85 | ␉␉␉if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))␊ |
86 | ␉␉␉{␊ |
87 | ␉␉␉␉uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));␊ |
88 | ␉␉␉␉if(csum2 == 0)␊ |
89 | ␉␉␉␉{␊ |
90 | ␉␉␉␉␉return acpi_addr;␊ |
91 | ␉␉␉␉}␊ |
92 | ␉␉␉}␊ |
93 | ␉␉}␊ |
94 | ␉}␊ |
95 | ␉return NULL;␊ |
96 | }␊ |
97 | ␊ |
98 | /* The folowing ACPI Table search algo. should be reused anywhere needed:*/␊ |
99 | /* WARNING: outDirspec string will be overwritten by subsequent calls! */␊ |
100 | int search_and_get_acpi_fd(const char *filename, const char **outDirspec)␊ |
101 | {␊ |
102 | ␉int fd = 0;␊ |
103 | ␉static char dirSpec[512];␊ |
104 | ␊ |
105 | ␉// Try finding 'filename' in the usual places␊ |
106 | ␉// Start searching any potential location for ACPI Table␊ |
107 | ␉strncpy(dirSpec, filename, sizeof(dirSpec) );␊ |
108 | ␉fd = open(dirSpec, 0);␊ |
109 | ␉if (fd < 0)␊ |
110 | ␉{␊ |
111 | ␉␉snprintf(dirSpec, sizeof(dirSpec), "/Extra/%s", filename); ␊ |
112 | ␉␉fd = open(dirSpec, 0);␊ |
113 | ␉␉if (fd < 0)␊ |
114 | ␉␉{␊ |
115 | ␉␉␉snprintf(dirSpec, sizeof(dirSpec), "bt(0,0)/Extra/%s", filename);␊ |
116 | ␉␉␉fd = open(dirSpec, 0);␊ |
117 | ␉␉␉if (fd < 0)␊ |
118 | ␉␉␉{␊ |
119 | ␉␉␉␉// NOT FOUND:␊ |
120 | ␉␉␉␉DBG("\tACPI Table not found: %s\n", filename);␊ |
121 | ␉␉␉␉*dirSpec = '\0';␊ |
122 | ␉␉␉}␊ |
123 | ␉␉}␊ |
124 | ␉}␊ |
125 | ␊ |
126 | ␉if (outDirspec) *outDirspec = dirSpec; ␊ |
127 | ␉return fd;␊ |
128 | }␊ |
129 | ␊ |
130 | void *loadACPITable (const char *filename)␊ |
131 | {␊ |
132 | ␉const char *dirspec = NULL;␊ |
133 | ␊ |
134 | ␉int fd = search_and_get_acpi_fd(filename, &dirspec);␊ |
135 | ␊ |
136 | ␉if (fd >= 0)␊ |
137 | ␉{␊ |
138 | ␉␉void *tableAddr = (void *)AllocateKernelMemory(file_size(fd));␊ |
139 | ␉␉if (tableAddr)␊ |
140 | ␉␉{␊ |
141 | ␉␉␉if (read(fd, tableAddr, file_size(fd)) != file_size(fd))␊ |
142 | ␉␉␉{␊ |
143 | ␉␉␉␉DBG("\tCouldn't read table %s\n", dirspec);␊ |
144 | ␉␉␉␉free(tableAddr);␊ |
145 | ␉␉␉␉close(fd);␊ |
146 | ␉␉␉␉return NULL;␊ |
147 | ␉␉␉}␊ |
148 | ␊ |
149 | ␉␉␉DBG("\tTable %s read and stored at: %x\n", dirspec, tableAddr);␊ |
150 | ␉␉␉close(fd);␊ |
151 | ␉␉␉return tableAddr;␊ |
152 | ␉␉}␊ |
153 | ␉␉close(fd);␊ |
154 | ␉␉DBG("\tCouldn't allocate memory for table: %s.\n", dirspec);␊ |
155 | ␉}␊ |
156 | ␉//printf("Couldn't find table %s\n", filename);␊ |
157 | ␉return NULL;␊ |
158 | }␊ |
159 | ␊ |
160 | struct acpi_2_fadt *patch_fadt(struct acpi_2_fadt *fadt, struct acpi_2_dsdt *new_dsdt)␊ |
161 | {␊ |
162 | ␉extern void setupSystemType();␊ |
163 | ␊ |
164 | ␉struct acpi_2_fadt *fadt_mod = NULL;␊ |
165 | ␉bool fadt_rev2_needed = false;␊ |
166 | ␉bool fix_restart;␊ |
167 | ␉bool fix_restart_ps2;␊ |
168 | ␉const char * value;␊ |
169 | ␊ |
170 | ␉// Restart Fix␊ |
171 | ␉if (Platform.CPU.Vendor == 0x756E6547)␊ |
172 | ␉{␉/* Intel */␊ |
173 | ␉␉fix_restart = true;␊ |
174 | ␉␉fix_restart_ps2 = false;␊ |
175 | ␉␉if ( getBoolForKey(kPS2RestartFix, &fix_restart_ps2, &bootInfo->chameleonConfig) && fix_restart_ps2)␊ |
176 | ␉␉{␊ |
177 | ␉␉␉fix_restart = true;␊ |
178 | ␉␉}␊ |
179 | ␉␉else␊ |
180 | ␉␉{␊ |
181 | ␉␉␉getBoolForKey(kRestartFix, &fix_restart, &bootInfo->chameleonConfig);␊ |
182 | ␉␉}␊ |
183 | ␉}␊ |
184 | ␉else␊ |
185 | ␉{␊ |
186 | ␉␉DBG("\tNot an Intel platform, FACP Restart Fix will not be applied!\n");␊ |
187 | ␉␉fix_restart = false;␊ |
188 | ␉}␊ |
189 | ␊ |
190 | ␉if (fix_restart)␊ |
191 | ␉{␊ |
192 | ␉␉fadt_rev2_needed = true;␊ |
193 | ␉}␊ |
194 | ␊ |
195 | ␉// Allocate new fadt table␊ |
196 | ␉if ((fadt->Length < 0x84) && fadt_rev2_needed)␊ |
197 | ␉{␊ |
198 | ␉␉fadt_mod = (struct acpi_2_fadt *)AllocateKernelMemory(0x84);␊ |
199 | ␉␉memcpy(fadt_mod, fadt, fadt->Length);␊ |
200 | ␉␉fadt_mod->Length = 0x84;␊ |
201 | ␉␉fadt_mod->Revision = 0x02; // FACP rev 2 (ACPI 1.0B MS extensions)␊ |
202 | ␉}␊ |
203 | ␉else␊ |
204 | ␉{␊ |
205 | ␉␉fadt_mod = (struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);␊ |
206 | ␉␉memcpy(fadt_mod, fadt, fadt->Length);␊ |
207 | ␉}␊ |
208 | ␉// Determine system type / PM_Model␊ |
209 | ␉if ( (value=getStringForKey(kSystemType, &bootInfo->chameleonConfig))!=NULL)␊ |
210 | ␉{␊ |
211 | ␉␉if (Platform.Type > 6)␊ |
212 | ␉␉{␊ |
213 | ␉␉␉if(fadt_mod->PM_Profile<=6)␊ |
214 | ␉␉␉{␊ |
215 | ␉␉␉␉Platform.Type = fadt_mod->PM_Profile; // get the fadt if correct␊ |
216 | ␉␉␉}␊ |
217 | ␉␉␉else␊ |
218 | ␉␉␉{␊ |
219 | ␉␉␉␉Platform.Type = 1;␉␉/* Set a fixed value (Desktop) */␊ |
220 | ␉␉␉}␊ |
221 | ␉␉␉DBG("\tError: system-type must be 0..6. Defaulting to %d !\n", Platform.Type);␊ |
222 | ␉␉}␊ |
223 | ␉␉else␊ |
224 | ␉␉{␊ |
225 | ␉␉␉Platform.Type = (unsigned char) strtoul(value, NULL, 10);␊ |
226 | ␉␉}␊ |
227 | ␉}␊ |
228 | ␉// Set PM_Profile from System-type if only user wanted this value to be forced␊ |
229 | ␉if (fadt_mod->PM_Profile != Platform.Type)␊ |
230 | ␉{␊ |
231 | ␉␉if (value)␊ |
232 | ␉␉{␊ |
233 | ␉␉␉// user has overriden the SystemType so take care of it in FACP␊ |
234 | ␉␉␉DBG("\tFADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);␊ |
235 | ␉␉␉fadt_mod->PM_Profile = Platform.Type;␊ |
236 | ␉␉}␊ |
237 | ␉␉else␊ |
238 | ␉␉{␊ |
239 | ␉␉␉// PM_Profile has a different value and no override has been set, so reflect the user value to ioregs␊ |
240 | ␉␉␉Platform.Type = fadt_mod->PM_Profile <= 6 ? fadt_mod->PM_Profile : 1;␊ |
241 | ␉␉}␊ |
242 | ␉}␊ |
243 | ␉// We now have to write the systemm-type in ioregs: we cannot do it before in setupDeviceTree()␊ |
244 | ␉// because we need to take care of FACP original content, if it is correct.␊ |
245 | ␉setupSystemType();␊ |
246 | ␊ |
247 | ␉// Patch FACP to fix restart␊ |
248 | ␉if (fix_restart)␊ |
249 | ␉{␊ |
250 | ␉␉if (fix_restart_ps2)␊ |
251 | ␉␉{␊ |
252 | ␉␉␉fadt_mod->Flags|= 0x400;␊ |
253 | ␉␉␉fadt_mod->Reset_SpaceID␉␉= 0x01; // System I/O␊ |
254 | ␉␉␉fadt_mod->Reset_BitWidth␉= 0x08; // 1 byte␊ |
255 | ␉␉␉fadt_mod->Reset_BitOffset␉= 0x00; // Offset 0␊ |
256 | ␉␉␉fadt_mod->Reset_AccessWidth␉= 0x01; // Byte access␊ |
257 | ␉␉␉fadt_mod->Reset_Address␉␉= 0x64; // Address of the register␊ |
258 | ␉␉␉fadt_mod->Reset_Value␉␉= 0xfe; // Value to write to reset the system␊ |
259 | ␉␉␉DBG("\tFACP PS2 Restart Fix applied!\n");␊ |
260 | ␉␉}␊ |
261 | ␉␉else␊ |
262 | ␉␉{␊ |
263 | ␉␉␉fadt_mod->Flags|= 0x400;␊ |
264 | ␉␉␉fadt_mod->Reset_SpaceID␉␉= 0x01; // System I/O␊ |
265 | ␉␉␉fadt_mod->Reset_BitWidth␉= 0x08; // 1 byte␊ |
266 | ␉␉␉fadt_mod->Reset_BitOffset␉= 0x00; // Offset 0␊ |
267 | ␉␉␉fadt_mod->Reset_AccessWidth␉= 0x01; // Byte access␊ |
268 | ␉␉␉fadt_mod->Reset_Address␉␉= 0x0cf9; // Address of the register␊ |
269 | ␉␉␉fadt_mod->Reset_Value␉␉= 0x06; // Value to write to reset the system␊ |
270 | ␉␉␉DBG("\tFACP Restart Fix applied!\n");␊ |
271 | ␉␉}␊ |
272 | ␊ |
273 | ␉}␊ |
274 | ␊ |
275 | ␉// Bungo: Save Hardware Signature (machine-signature)␊ |
276 | ␉if ((fadt_mod->FACS > 0) && (fadt_mod->FACS < 0xFFFFFFFF) && (((struct acpi_2_facs *)fadt_mod->FACS)->Length >= 64))␊ |
277 | ␉{␊ |
278 | ␉␉Platform.HWSignature = ((struct acpi_2_facs *)fadt_mod->FACS)->HWSignature;␊ |
279 | ␉␉DBG("\tHardware Signature=0x%08X: using.\n", Platform.HWSignature);␊ |
280 | ␉}␊ |
281 | ␉else␊ |
282 | ␉{␊ |
283 | ␉␉Platform.HWSignature = 0;␊ |
284 | ␉␉DBG("\tFixing Hardware Signature=0x%08X.\n", Platform.HWSignature);␊ |
285 | ␉}␊ |
286 | ␊ |
287 | ␉// Patch DSDT address if we have loaded DSDT.aml␊ |
288 | ␉if (new_dsdt)␊ |
289 | ␉{␊ |
290 | ␉␉DBG("\tDSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);␊ |
291 | ␊ |
292 | ␉␉fadt_mod->DSDT = (uint32_t)new_dsdt;␊ |
293 | ␉␉if ((uint32_t)(&(fadt_mod->X_DSDT)) - (uint32_t)fadt_mod + 8<=fadt_mod->Length)␊ |
294 | ␉␉{␊ |
295 | ␉␉␉fadt_mod->X_DSDT = (uint32_t)new_dsdt;␊ |
296 | ␉␉}␊ |
297 | ␊ |
298 | ␉␉DBG("\tNew @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);␊ |
299 | ␊ |
300 | ␉␉DBG("\tFADT: Using custom DSDT!\n");␊ |
301 | ␉}␊ |
302 | ␊ |
303 | ␉// Correct the checksum␊ |
304 | ␉fadt_mod->Checksum=0;␊ |
305 | ␉fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);␊ |
306 | ␊ |
307 | ␉return fadt_mod;␊ |
308 | }␊ |
309 | ␊ |
310 | /* Setup ACPI without replacing DSDT. */␊ |
311 | int setupAcpiNoMod()␊ |
312 | {␊ |
313 | //␉addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");␊ |
314 | //␉addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");␊ |
315 | ␉/* XXX aserebln why uint32 cast if pointer is uint64 ? */␊ |
316 | ␉acpi10_p = (uint64_t)(uint32_t)getAddressOfAcpiTable();␊ |
317 | ␉acpi20_p = (uint64_t)(uint32_t)getAddressOfAcpi20Table();␊ |
318 | ␉addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");␊ |
319 | ␉if(acpi20_p)␊ |
320 | ␉{␊ |
321 | ␉␉addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");␊ |
322 | ␉}␊ |
323 | ␉else␊ |
324 | ␉{␊ |
325 | ␉␉DBG("\tNo ACPI 2.\n");␊ |
326 | ␉}␊ |
327 | ␉return 1;␊ |
328 | }␊ |
329 | ␊ |
330 | /* Setup ACPI. Replace DSDT if DSDT.aml is found */␊ |
331 | int setupAcpi(void)␊ |
332 | {␊ |
333 | ␉verbose("[ ACPI PATCHER ]\n");␊ |
334 | ␉int version;␊ |
335 | ␉void *new_dsdt = NULL;␊ |
336 | ␊ |
337 | ␊ |
338 | ␉const char *filename;␊ |
339 | ␉char dirSpec[128];␊ |
340 | ␉int len = 0;␊ |
341 | ␊ |
342 | ␉// always reset cpu count to 0 when injecting new acpi␊ |
343 | ␉acpi_cpu_count = 0;␊ |
344 | ␊ |
345 | ␉/* Try using the file specified with the DSDT option */␊ |
346 | ␉if (getValueForKey(kDSDT, &filename, &len, &bootInfo->chameleonConfig))␊ |
347 | ␉{␊ |
348 | ␉␉strncpy(dirSpec, filename, sizeof(dirSpec) );␊ |
349 | ␉}␊ |
350 | ␉else␊ |
351 | ␉{␊ |
352 | ␉␉strcpy(dirSpec, "DSDT.aml");␊ |
353 | ␉␉//verbose("dirSpec, DSDT.aml");␊ |
354 | ␉}␊ |
355 | ␊ |
356 | ␉// Load replacement DSDT␊ |
357 | ␉new_dsdt = loadACPITable(dirSpec);␊ |
358 | ␊ |
359 | ␉// Mozodojo: going to patch FACP and load SSDT's even if DSDT.aml is not present␊ |
360 | ␉/*if (!new_dsdt)␊ |
361 | ␉ {␊ |
362 | ␉ return setupAcpiNoMod();␊ |
363 | ␉ }*/␊ |
364 | ␊ |
365 | ␉// Mozodojo: Load additional SSDTs␊ |
366 | ␉struct acpi_2_ssdt *new_ssdt[32]; // 30 + 2 additional tables for pss & cst␊ |
367 | ␉int ssdt_count=0;␊ |
368 | ␊ |
369 | ␉// SSDT Options␊ |
370 | ␉bool drop_ssdt = false, generate_pstates = false, generate_cstates = false;␊ |
371 | ␊ |
372 | ␉getBoolForKey(kDropSSDT, &drop_ssdt, &bootInfo->chameleonConfig);␊ |
373 | ␉getBoolForKey(kGeneratePStates, &generate_pstates, &bootInfo->chameleonConfig);␊ |
374 | ␉getBoolForKey(kGenerateCStates, &generate_cstates, &bootInfo->chameleonConfig);␊ |
375 | ␉//getBoolForKey(kGenerateTStates, &generate_tstates, &bootInfo->chameleonConfig);␊ |
376 | ␊ |
377 | ␉DBG("\tGenerating P-States config: %s\n", generate_pstates ? "Yes" : "No");␊ |
378 | ␉DBG("\tGenerating C-States config: %s\n", generate_cstates ? "Yes" : "No");␊ |
379 | ␉//DBG("Generating T-States config: %s\n", generate_tstates ? "Yes" : "No");␊ |
380 | ␊ |
381 | ␉{␊ |
382 | ␉␉int i;␊ |
383 | ␊ |
384 | ␉␉for (i = 0; i < 30; i++)␊ |
385 | ␉␉{␊ |
386 | ␉␉␉char filename[512];␊ |
387 | ␊ |
388 | ␉␉␉if (i > 0)␊ |
389 | ␉␉␉{␊ |
390 | ␉␉␉␉sprintf(filename, "SSDT-%d.aml", i);␊ |
391 | ␉␉␉}␊ |
392 | ␉␉␉else␊ |
393 | ␉␉␉{␊ |
394 | ␉␉␉␉strcpy(filename, "SSDT.aml");␊ |
395 | ␉␉␉}␊ |
396 | ␊ |
397 | ␉␉␉if ( (new_ssdt[ssdt_count] = loadACPITable(filename)) )␊ |
398 | ␉␉␉{␊ |
399 | ␉␉␉␉ssdt_count++;␊ |
400 | ␉␉␉}␊ |
401 | ␉␉␉else␊ |
402 | ␉␉␉{␊ |
403 | ␉␉␉␉break;␊ |
404 | ␉␉␉}␊ |
405 | ␉␉}␊ |
406 | ␉}␊ |
407 | ␊ |
408 | ␉// Do the same procedure for both versions of ACPI␊ |
409 | ␉for (version = 0; version < 2; version++)␊ |
410 | ␉{␊ |
411 | ␉␉struct acpi_2_rsdp *rsdp, *rsdp_mod;␊ |
412 | ␉␉struct acpi_2_rsdt *rsdt, *rsdt_mod;␊ |
413 | ␉␉int rsdplength;␊ |
414 | ␊ |
415 | ␉␉// Find original rsdp␊ |
416 | ␉␉rsdp = (struct acpi_2_rsdp *)(version ? getAddressOfAcpi20Table() : getAddressOfAcpiTable());␊ |
417 | ␉␉if (!rsdp)␊ |
418 | ␉␉{␊ |
419 | ␉␉␉DBG("\tNo ACPI version %d found. Ignoring\n", version+1);␊ |
420 | ␉␉␉if (version)␊ |
421 | ␉␉␉{␊ |
422 | ␉␉␉␉addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");␊ |
423 | ␉␉␉}␊ |
424 | ␉␉␉else␊ |
425 | ␉␉␉{␊ |
426 | ␉␉␉␉addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");␊ |
427 | ␉␉␉}␊ |
428 | ␉␉␉continue;␊ |
429 | ␉␉}␊ |
430 | ␉␉rsdplength = version ? rsdp->Length : 20;␊ |
431 | ␊ |
432 | ␉␉DBG("\tRSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);␊ |
433 | ␊ |
434 | ␉␉/* FIXME: no check that memory allocation succeeded ␊ |
435 | ␉␉ * Copy and patch RSDP,RSDT, XSDT and FADT␊ |
436 | ␉␉ * For more info see ACPI Specification pages 110 and following␊ |
437 | ␉␉ */␊ |
438 | ␊ |
439 | ␉␉rsdp_mod = (struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);␊ |
440 | ␉␉memcpy(rsdp_mod, rsdp, rsdplength);␊ |
441 | ␊ |
442 | ␉␉rsdt = (struct acpi_2_rsdt *)rsdp->RsdtAddress;␊ |
443 | ␊ |
444 | ␉␉DBG("\tRSDT @%x, Length %d\n",rsdt, rsdt ? rsdt->Length : 0);␊ |
445 | ␊ |
446 | ␉␉if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length < 0x10000)␊ |
447 | ␉␉{␊ |
448 | ␉␉␉uint32_t *rsdt_entries;␊ |
449 | ␉␉␉int rsdt_entries_num;␊ |
450 | ␉␉␉int dropoffset = 0, i;␊ |
451 | ␊ |
452 | ␉␉␉// mozo: using malloc cos I didn't found how to free already allocated kernel memory␊ |
453 | ␉␉␉rsdt_mod = (struct acpi_2_rsdt *)malloc(rsdt->Length);␊ |
454 | ␉␉␉memcpy(rsdt_mod, rsdt, rsdt->Length);␊ |
455 | ␉␉␉rsdp_mod->RsdtAddress = (uint32_t)rsdt_mod;␊ |
456 | ␉␉␉rsdt_entries_num = (rsdt_mod->Length - sizeof(struct acpi_2_rsdt)) / 4;␊ |
457 | ␉␉␉rsdt_entries = (uint32_t *)(rsdt_mod + 1);␊ |
458 | ␉␉␉for (i = 0; i < rsdt_entries_num; i++)␊ |
459 | ␉␉␉{␊ |
460 | ␉␉␉␉char *table=(char *)(rsdt_entries[i]);␊ |
461 | ␉␉␉␉if (!table)␊ |
462 | ␉␉␉␉{␊ |
463 | ␉␉␉␉␉continue;␊ |
464 | ␉␉␉␉}␊ |
465 | ␊ |
466 | ␉␉␉␉DBG("\tTABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);␊ |
467 | ␊ |
468 | ␉␉␉␉rsdt_entries[i-dropoffset]=rsdt_entries[i];␊ |
469 | ␊ |
470 | ␉␉␉␉if (drop_ssdt && tableSign(table, "SSDT"))␊ |
471 | ␉␉␉␉{␊ |
472 | ␉␉␉␉␉DBG("\tOEM SSDT tables was dropped\n");␊ |
473 | ␉␉␉␉␉dropoffset++;␊ |
474 | ␉␉␉␉␉continue;␊ |
475 | ␉␉␉␉}␊ |
476 | ␊ |
477 | ␉␉␉␉if (tableSign(table, "DSDT"))␊ |
478 | ␉␉␉␉{␊ |
479 | ␉␉␉␉␉DBG("\tDSDT found\n");␊ |
480 | ␉␉␉␉␉DBG("\tCustom DSDT table was found\n");␊ |
481 | ␉␉␉␉␉if(new_dsdt)␊ |
482 | ␉␉␉␉␉{␊ |
483 | ␉␉␉␉␉␉rsdt_entries[i-dropoffset] = (uint32_t)new_dsdt;␊ |
484 | ␉␉␉␉␉}␊ |
485 | ␊ |
486 | ␉␉␉␉␉continue;␊ |
487 | ␉␉␉␉}␊ |
488 | ␊ |
489 | ␉␉␉␉if (tableSign(table, "FACP"))␊ |
490 | ␉␉␉␉{␊ |
491 | ␉␉␉␉␉struct acpi_2_fadt *fadt, *fadt_mod;␊ |
492 | ␉␉␉␉␉fadt=(struct acpi_2_fadt *)rsdt_entries[i];␊ |
493 | ␊ |
494 | ␉␉␉␉␉DBG("\tFADT found @%x, Length %d\n",fadt, fadt->Length);␊ |
495 | ␊ |
496 | ␉␉␉␉␉if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)␊ |
497 | ␉␉␉␉␉{␊ |
498 | ␉␉␉␉␉␉DBG("\tFADT incorrect. Not modified\n");␊ |
499 | ␉␉␉␉␉␉continue;␊ |
500 | ␉␉␉␉␉}␊ |
501 | ␉␉␉␉␉␊ |
502 | ␉␉␉␉␉fadt_mod = patch_fadt(fadt, new_dsdt);␊ |
503 | ␉␉␉␉␉rsdt_entries[i-dropoffset] = (uint32_t)fadt_mod;␊ |
504 | ␉␉␉␉␉␊ |
505 | ␉␉␉␉␉// Generate _CST SSDT␊ |
506 | ␉␉␉␉␉if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))␊ |
507 | ␉␉␉␉␉{␊ |
508 | ␉␉␉␉␉␉DBG("\tC-States generated.\n");␊ |
509 | ␉␉␉␉␉␉generate_cstates = false; // Generate SSDT only once!␊ |
510 | ␉␉␉␉␉␉ssdt_count++;␊ |
511 | ␉␉␉␉␉}␊ |
512 | ␊ |
513 | ␉␉␉␉␉// Generating _PSS SSDT␊ |
514 | ␉␉␉␉␉if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))␊ |
515 | ␉␉␉␉␉{␊ |
516 | ␉␉␉␉␉␉DBG("\tP-States generated.\n");␊ |
517 | ␉␉␉␉␉␉generate_pstates = false; // Generate SSDT only once!␊ |
518 | ␉␉␉␉␉␉ssdt_count++;␊ |
519 | ␉␉␉␉␉}␊ |
520 | ␉␉␉␉␉continue;␊ |
521 | ␉␉␉␉}␊ |
522 | ␉␉␉}␊ |
523 | ␉␉␉DBG("\n");␊ |
524 | ␊ |
525 | ␉␉␉// Allocate rsdt in Kernel memory area␊ |
526 | ␉␉␉rsdt_mod->Length += 4*ssdt_count - 4*dropoffset;␊ |
527 | ␉␉␉struct acpi_2_rsdt *rsdt_copy = (struct acpi_2_rsdt *)AllocateKernelMemory(rsdt_mod->Length);␊ |
528 | ␉␉␉memcpy(rsdt_copy, rsdt_mod, rsdt_mod->Length);␊ |
529 | ␉␉␉free(rsdt_mod);␊ |
530 | ␉␉␉rsdt_mod = rsdt_copy;␊ |
531 | ␉␉␉rsdp_mod->RsdtAddress = (uint32_t)rsdt_mod;␊ |
532 | ␉␉␉rsdt_entries_num = (rsdt_mod->Length-sizeof(struct acpi_2_rsdt)) / 4;␊ |
533 | ␉␉␉rsdt_entries = (uint32_t *)(rsdt_mod + 1);␊ |
534 | ␊ |
535 | ␉␉␉// Mozodojo: Insert additional SSDTs into RSDT␊ |
536 | ␉␉␉if(ssdt_count > 0)␊ |
537 | ␉␉␉{␊ |
538 | ␉␉␉␉int j;␊ |
539 | ␊ |
540 | ␉␉␉␉for (j=0; j<ssdt_count; j++)␊ |
541 | ␉␉␉␉{␊ |
542 | ␉␉␉␉␉rsdt_entries[i-dropoffset+j] = (uint32_t)new_ssdt[j];␊ |
543 | ␉␉␉␉}␊ |
544 | ␊ |
545 | ␉␉␉␉DBG("\tRSDT: Added %d SSDT table(s)\n", ssdt_count);␊ |
546 | ␊ |
547 | ␉␉␉}␊ |
548 | ␊ |
549 | ␉␉␉// Correct the checksum of RSDT␊ |
550 | ␉␉␉DBG("\tRSDT: Original checksum %d, ", rsdt_mod->Checksum);␊ |
551 | ␊ |
552 | ␉␉␉rsdt_mod->Checksum=0;␊ |
553 | ␉␉␉rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);␊ |
554 | ␊ |
555 | ␉␉␉DBG("\tNew checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);␊ |
556 | ␉␉}␊ |
557 | ␉␉else␊ |
558 | ␉␉{␊ |
559 | ␉␉␉rsdp_mod->RsdtAddress=0;␊ |
560 | ␉␉␉DBG("\tRSDT not found or RSDT incorrect\n");␊ |
561 | ␉␉}␊ |
562 | ␉␉DBG("\n");␊ |
563 | ␊ |
564 | ␉␉if (version)␊ |
565 | ␉␉{␊ |
566 | ␉␉␉struct acpi_2_xsdt *xsdt, *xsdt_mod;␊ |
567 | ␊ |
568 | ␉␉␉// FIXME: handle 64-bit address correctly␊ |
569 | ␊ |
570 | ␉␉␉xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);␊ |
571 | ␉␉␉DBG("\tXSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress, xsdt->Length);␊ |
572 | ␊ |
573 | ␉␉␉if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)␊ |
574 | ␉␉␉{␊ |
575 | ␉␉␉␉uint64_t *xsdt_entries;␊ |
576 | ␉␉␉␉int xsdt_entries_num, i;␊ |
577 | ␉␉␉␉int dropoffset = 0;␊ |
578 | ␊ |
579 | ␉␉␉␉// mozo: using malloc cos I didn't found how to free already allocated kernel memory␊ |
580 | ␉␉␉␉xsdt_mod = (struct acpi_2_xsdt *)malloc(xsdt->Length);␊ |
581 | ␉␉␉␉memcpy(xsdt_mod, xsdt, xsdt->Length);␊ |
582 | ␊ |
583 | ␉␉␉␉rsdp_mod->XsdtAddress = (uint32_t)xsdt_mod;␊ |
584 | ␉␉␉␉xsdt_entries_num = (xsdt_mod->Length - sizeof(struct acpi_2_xsdt)) / 8;␊ |
585 | ␉␉␉␉xsdt_entries = (uint64_t *)(xsdt_mod + 1);␊ |
586 | ␉␉␉␉for (i = 0;i < xsdt_entries_num; i++)␊ |
587 | ␉␉␉␉{␊ |
588 | ␉␉␉␉␉char *table = (char *)((uint32_t)(xsdt_entries[i]));␊ |
589 | ␉␉␉␉␉if (!table)␊ |
590 | ␉␉␉␉␉{␊ |
591 | ␉␉␉␉␉␉continue;␊ |
592 | ␉␉␉␉␉}␊ |
593 | ␉␉␉␉␉xsdt_entries[i - dropoffset] = xsdt_entries[i];␊ |
594 | ␊ |
595 | ␉␉␉␉␉if (drop_ssdt && tableSign(table, "SSDT"))␊ |
596 | ␉␉␉␉␉{␊ |
597 | ␉␉␉␉␉␉DBG("\tOEM SSDT tables was dropped\n");␊ |
598 | ␉␉␉␉␉␉dropoffset++;␊ |
599 | ␉␉␉␉␉␉continue;␊ |
600 | ␉␉␉␉␉}␊ |
601 | ␉␉␉␉␉if (tableSign(table, "DSDT"))␊ |
602 | ␉␉␉␉␉{␊ |
603 | ␉␉␉␉␉␉DBG("\tDSDT found\n");␊ |
604 | ␊ |
605 | ␉␉␉␉␉␉if (new_dsdt)␊ |
606 | ␉␉␉␉␉␉{␊ |
607 | ␉␉␉␉␉␉␉xsdt_entries[i-dropoffset] = (uint32_t)new_dsdt;␊ |
608 | ␉␉␉␉␉␉␉DBG("\tcustom table added.\n");␊ |
609 | ␉␉␉␉␉␉}␊ |
610 | ␊ |
611 | ␉␉␉␉␉␉DBG("\tTABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);␊ |
612 | ␊ |
613 | ␉␉␉␉␉␉continue;␊ |
614 | ␉␉␉␉␉}␊ |
615 | ␉␉␉␉␉if (tableSign(table, "FACP"))␊ |
616 | ␉␉␉␉␉{␊ |
617 | ␉␉␉␉␉␉struct acpi_2_fadt *fadt, *fadt_mod;␊ |
618 | ␉␉␉␉␉␉fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];␊ |
619 | ␊ |
620 | ␉␉␉␉␉␉DBG("\tFADT found @%x%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt, ␊ |
621 | ␉␉␉␉␉␉␉fadt->Length);␊ |
622 | ␊ |
623 | ␉␉␉␉␉␉if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)␊ |
624 | ␉␉␉␉␉␉{␊ |
625 | ␉␉␉␉␉␉␉DBG("\tFADT incorrect or after 4GB. Dropping XSDT\n");␊ |
626 | ␉␉␉␉␉␉␉goto drop_xsdt;␊ |
627 | ␉␉␉␉␉␉}␊ |
628 | ␊ |
629 | ␉␉␉␉␉␉fadt_mod = patch_fadt(fadt, new_dsdt);␊ |
630 | ␉␉␉␉␉␉xsdt_entries[i - dropoffset] = (uint32_t)fadt_mod;␊ |
631 | ␊ |
632 | ␉␉␉␉␉␉// DBG("\tTABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);␊ |
633 | ␊ |
634 | ␉␉␉␉␉␉// Generate _CST SSDT␊ |
635 | ␉␉␉␉␉␉if (generate_cstates && (new_ssdt[ssdt_count] = generate_cst_ssdt(fadt_mod)))␊ |
636 | ␉␉␉␉␉␉{␊ |
637 | ␉␉␉␉␉␉␉DBG("\tC-States generated.\n");␊ |
638 | ␉␉␉␉␉␉␉generate_cstates = false; // Generate SSDT only once!␊ |
639 | ␉␉␉␉␉␉␉ssdt_count++;␊ |
640 | ␉␉␉␉␉␉}␊ |
641 | ␊ |
642 | ␉␉␉␉␉␉// Generating _PSS SSDT␊ |
643 | ␉␉␉␉␉␉if (generate_pstates && (new_ssdt[ssdt_count] = generate_pss_ssdt((void*)fadt_mod->DSDT)))␊ |
644 | ␉␉␉␉␉␉{␊ |
645 | ␉␉␉␉␉␉␉DBG("\tP-States generated.\n");␊ |
646 | ␉␉␉␉␉␉␉generate_pstates = false; // Generate SSDT only once!␊ |
647 | ␉␉␉␉␉␉␉ssdt_count++;␊ |
648 | ␉␉␉␉␉␉}␊ |
649 | ␊ |
650 | ␉␉␉␉␉␉// Generating _TSS SSDT␊ |
651 | ␉␉␉␉␉␉/*if (generate_tstates && (new_ssdt[ssdt_count] = generate_tss_ssdt((void*)fadt_mod->DSDT)))␊ |
652 | ␉␉␉␉␉␉{␊ |
653 | ␉␉␉␉␉␉␉generate_tstates = false; // Generate SSDT only once!␊ |
654 | ␉␉␉␉␉␉␉ssdt_count++;␊ |
655 | ␉␉␉␉␉␉}*/␊ |
656 | ␉␉␉␉␉␉continue;␊ |
657 | ␉␉␉␉␉}␊ |
658 | ␉␉␉␉␉DBG("\tcopied (OEM)\n");␊ |
659 | ␉␉␉␉␉// DBG("\tTABLE %c%c%c%c@%x \n", table[0],table[1],table[2],table[3],xsdt_entries[i]);␊ |
660 | ␉␉␉␉}␊ |
661 | ␊ |
662 | ␉␉␉␉// Allocate xsdt in Kernel memory area␊ |
663 | ␉␉␉␉xsdt_mod->Length += 8*ssdt_count - 8*dropoffset;␊ |
664 | ␉␉␉␉struct acpi_2_xsdt *xsdt_copy = (struct acpi_2_xsdt *)AllocateKernelMemory(xsdt_mod->Length);␊ |
665 | ␉␉␉␉memcpy(xsdt_copy, xsdt_mod, xsdt_mod->Length);␊ |
666 | ␉␉␉␉free(xsdt_mod);␊ |
667 | ␉␉␉␉xsdt_mod = xsdt_copy;␊ |
668 | ␉␉␉␉rsdp_mod->XsdtAddress = (uint32_t)xsdt_mod;␊ |
669 | ␉␉␉␉xsdt_entries_num = (xsdt_mod->Length - sizeof(struct acpi_2_xsdt)) / 8;␊ |
670 | ␉␉␉␉xsdt_entries = (uint64_t *)(xsdt_mod + 1);␊ |
671 | ␊ |
672 | ␉␉␉␉// Mozodojo: Insert additional SSDTs into XSDT␊ |
673 | ␉␉␉␉if(ssdt_count > 0)␊ |
674 | ␉␉␉␉{␊ |
675 | ␉␉␉␉␉int j;␊ |
676 | ␊ |
677 | ␉␉␉␉␉for (j=0; j<ssdt_count; j++)␊ |
678 | ␉␉␉␉␉{␊ |
679 | ␉␉␉␉␉␉xsdt_entries[i - dropoffset + j] = (uint32_t)new_ssdt[j];␊ |
680 | ␉␉␉␉␉}␊ |
681 | ␊ |
682 | ␉␉␉␉␉verbose("\tAdded %d SSDT table(s) into XSDT\n", ssdt_count);␊ |
683 | ␊ |
684 | ␉␉␉␉}␊ |
685 | ␊ |
686 | ␉␉␉␉// Correct the checksum of XSDT␊ |
687 | ␉␉␉␉xsdt_mod->Checksum=0;␊ |
688 | ␉␉␉␉xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);␊ |
689 | ␉␉␉}␊ |
690 | ␉␉␉else␊ |
691 | ␉␉␉{␊ |
692 | ␉␉␉drop_xsdt:␊ |
693 | ␊ |
694 | ␉␉␉␉DBG("\tAbout to drop XSDT\n");␊ |
695 | ␊ |
696 | ␉␉␉␉/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT. ␊ |
697 | ␉␉␉␉ * A Better strategy would be to generate␊ |
698 | ␉␉␉␉ */␊ |
699 | ␊ |
700 | ␉␉␉␉rsdp_mod->XsdtAddress=0xffffffffffffffffLL;␊ |
701 | ␉␉␉␉verbose("\tXSDT not found or XSDT incorrect\n");␊ |
702 | ␉␉␉}␊ |
703 | ␉␉}␊ |
704 | ␉␉DBG("\n");␊ |
705 | ␊ |
706 | ␉␉// Correct the checksum of RSDP␊ |
707 | ␊ |
708 | ␉␉DBG("\tRSDP: Original checksum %d, ", rsdp_mod->Checksum);␊ |
709 | ␊ |
710 | ␉␉rsdp_mod->Checksum=0;␊ |
711 | ␉␉rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);␊ |
712 | ␊ |
713 | ␉␉DBG("\tNew checksum %d\n", rsdp_mod->Checksum);␊ |
714 | ␊ |
715 | ␉␉if (version)␊ |
716 | ␉␉{␊ |
717 | ␉␉␉DBG("\tRSDP: Original extended checksum %d, ", rsdp_mod->ExtendedChecksum);␊ |
718 | ␊ |
719 | ␉␉␉rsdp_mod->ExtendedChecksum=0;␊ |
720 | ␉␉␉rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);␊ |
721 | ␊ |
722 | ␉␉␉DBG("\tNew extended checksum %d\n", rsdp_mod->ExtendedChecksum);␊ |
723 | ␊ |
724 | ␉␉}␊ |
725 | ␊ |
726 | ␉␉if (version)␊ |
727 | ␉␉{␊ |
728 | ␉␉␉/* XXX aserebln why uint32 cast if pointer is uint64 ? */␊ |
729 | ␉␉␉acpi20_p = (uint64_t)(uint32_t)rsdp_mod;␊ |
730 | ␉␉␉addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");␊ |
731 | ␉␉}␊ |
732 | ␉␉else␊ |
733 | ␉␉{␊ |
734 | ␉␉␉/* XXX aserebln why uint32 cast if pointer is uint64 ? */␊ |
735 | ␉␉␉acpi10_p = (uint64_t)(uint32_t)rsdp_mod;␊ |
736 | ␉␉␉addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");␊ |
737 | ␉␉}␊ |
738 | ␉␉DBG("\tACPI version %d patching finished\n\n", version + 1);␊ |
739 | ␉}␊ |
740 | #if DEBUG_ACPI␊ |
741 | ␉printf("Press a key to continue... (DEBUG_ACPI)\n");␊ |
742 | ␉getchar();␊ |
743 | #endif␊ |
744 | ␉return 1;␊ |
745 | }␊ |
746 | |