Chameleon

Chameleon Svn Source Tree

Root/branches/JrCs/i386/libsaio/dsdt_patcher.c

1/*
2 * Copyright 2008 mackerintel
3 */
4
5#include "libsaio.h"
6#include "boot.h"
7#include "bootstruct.h"
8#include "acpi.h"
9#include "efi_tables.h"
10#include "fake_efi.h"
11#include "dsdt_patcher.h"
12#include "platform.h"
13
14#ifndef DEBUG_DSDT
15#define DEBUG_DSDT 0
16#endif
17
18#if DEBUG_DSDT==2
19#define DBG(x...) {printf(x); sleep(1);}
20#elif DEBUG_DSDT==1
21#define DBG(x...) printf(x)
22#else
23#define DBG(x...)
24#endif
25
26/* Gets the ACPI 1.0 RSDP address */
27static struct acpi_2_rsdp* getAddressOfAcpiTable()
28{
29 /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
30
31 void *acpi_addr = (void*)ACPI_RANGE_START;
32 for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
33 {
34 if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
35 {
36 uint8_t csum = checksum8(acpi_addr, 20);
37 if(csum == 0)
38 {
39 // Only return the table if it is a true version 1.0 table (Revision 0)
40 if(((struct acpi_2_rsdp*)acpi_addr)->Revision == 0)
41 return acpi_addr;
42 }
43 }
44 }
45 return NULL;
46}
47
48/* Gets the ACPI 2.0 RSDP address */
49static struct acpi_2_rsdp* getAddressOfAcpi20Table()
50{
51 /* TODO: Before searching the BIOS space we are supposed to search the first 1K of the EBDA */
52
53 void *acpi_addr = (void*)ACPI_RANGE_START;
54 for(; acpi_addr <= (void*)ACPI_RANGE_END; acpi_addr += 16)
55 {
56 if(*(uint64_t *)acpi_addr == ACPI_SIGNATURE_UINT64_LE)
57 {
58 uint8_t csum = checksum8(acpi_addr, 20);
59
60 /* Only assume this is a 2.0 or better table if the revision is greater than 0
61 * NOTE: ACPI 3.0 spec only seems to say that 1.0 tables have revision 1
62 * and that the current revision is 2.. I am going to assume that rev > 0 is 2.0.
63 */
64
65 if(csum == 0 && (((struct acpi_2_rsdp*)acpi_addr)->Revision > 0))
66 {
67 uint8_t csum2 = checksum8(acpi_addr, sizeof(struct acpi_2_rsdp));
68 if(csum2 == 0)
69 return acpi_addr;
70 }
71 }
72 }
73 return NULL;
74}
75
76void *loadACPITable (const char *filename)
77{
78void *tableAddr;
79int fd;
80char dirspec[512];
81
82// Check booting partition
83sprintf(dirspec,"%s",filename);
84fd=open (dirspec,0);
85if (fd<0)
86{// Check Extra on booting partition
87sprintf(dirspec,"/Extra/%s",filename);
88fd=open (dirspec,0);
89if (fd<0)
90{// Fall back to booter partition
91sprintf(dirspec,"bt(0,0)/Extra/%s",filename);
92fd=open (dirspec,0);
93if (fd<0)
94{
95verbose("ACPI Table not found: %s\n", filename);
96return NULL;
97}
98}
99}
100
101tableAddr=(void*)AllocateKernelMemory(file_size (fd));
102if (tableAddr)
103{
104if (read (fd, tableAddr, file_size (fd))!=file_size (fd))
105{
106printf("Couldn't read table %s\n",dirspec);
107free (tableAddr);
108close (fd);
109return NULL;
110}
111
112DBG("Table %s read and stored at: %x\n", dirspec, tableAddr);
113close (fd);
114return tableAddr;
115}
116
117printf("Couldn't allocate memory for table %s\n", dirspec);
118close (fd);
119return NULL;
120}
121
122struct acpi_2_fadt *
123patch_fadt(struct acpi_2_fadt *fadt, void *new_dsdt)
124{
125
126struct acpi_2_fadt *fadt_mod;
127bool fadt_rev2_needed = NO;
128bool fix_restart;
129
130// Restart Fix
131if (Platform.CPU.Vendor == 0x756E6547) {/* Intel */
132fix_restart = YES;
133getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);
134} else {
135verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
136fix_restart = NO;
137}
138
139if (fix_restart)
140fadt_rev2_needed = YES;
141
142// Allocate new fadt table
143if (fadt->Length < 0x84 && fadt_rev2_needed)
144{
145fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
146memcpy(fadt_mod, fadt, fadt->Length);
147fadt_mod->Length = 0x84;
148fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
149}
150else
151{
152fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
153memcpy(fadt_mod, fadt, fadt->Length);
154}
155
156// Set PM_Profile from System-type
157if (fadt_mod->PM_Profile != Platform.Type) {
158verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
159fadt_mod->PM_Profile = Platform.Type;
160}
161
162// Patch FADT to fix restart
163if (fix_restart)
164{
165fadt_mod->Flags|= 0x400;
166fadt_mod->Reset_SpaceID= 0x01; // System I/O
167fadt_mod->Reset_BitWidth= 0x08; // 1 byte
168fadt_mod->Reset_BitOffset= 0x00; // Offset 0
169fadt_mod->Reset_AccessWidth= 0x01; // Byte access
170fadt_mod->Reset_Address= 0x0cf9; // Address of the register
171fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
172verbose("FADT: Restart Fix applied !\n");
173}
174
175// Patch DSDT Address
176DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
177
178fadt_mod->DSDT=(uint32_t)new_dsdt;
179if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
180fadt_mod->X_DSDT=(uint32_t)new_dsdt;
181
182DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
183
184// Correct the checksum
185fadt_mod->Checksum=0;
186fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
187
188return fadt_mod;
189}
190
191/* Setup ACPI without replacing DSDT. */
192int setupAcpiNoMod()
193{
194//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
195//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
196acpi10_p = (uint32_t)getAddressOfAcpiTable();
197acpi20_p = (uint32_t)getAddressOfAcpi20Table();
198addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
199if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
200return 1;
201}
202
203/* Setup ACPI. Replace DSDT if DSDT.aml is found */
204int setupAcpi()
205{
206int version;
207void *new_dsdt;
208const char *dsdt_filename;
209/* const char *facp_filename; */
210int len;
211boolean_t drop_ssdt=NO;
212
213//DBG("Enter setupACPI\n");
214
215if (!getValueForKey("DSDT", &dsdt_filename, &len, &bootInfo->bootConfig))
216dsdt_filename="DSDT.aml";
217
218// Load replacement DSDT
219new_dsdt=loadACPITable(dsdt_filename);
220if (!new_dsdt)
221{
222return setupAcpiNoMod();
223}
224DBG("New DSDT Loaded in memory\n");
225
226{
227bool tmp;
228drop_ssdt=getBoolForKey("DropSSDT",&tmp, &bootInfo->bootConfig)&&tmp;
229}
230
231// Do the same procedure for both versions of ACPI
232for (version=0;version<2;version++)
233{
234struct acpi_2_rsdp *rsdp, *rsdp_mod;
235struct acpi_2_rsdt *rsdt, *rsdt_mod;
236int rsdplength;
237
238// Find original rsdp
239rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
240if (!rsdp)
241{
242DBG("No ACPI version %d found. Ignoring\n", version+1);
243if (version)
244addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
245else
246addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
247continue;
248}
249rsdplength=version?rsdp->Length:20;
250
251DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
252
253/* FIXME: no check that memory allocation succeeded
254 * Copy and patch RSDP,RSDT, XSDT and FADT
255 * For more info see ACPI Specification pages 110 and following
256 */
257
258rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
259memcpy(rsdp_mod, rsdp, rsdplength);
260rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
261
262DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
263
264if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
265{
266uint32_t *rsdt_entries;
267int rsdt_entries_num;
268int dropoffset=0, i;
269
270rsdt_mod=(struct acpi_2_rsdt *)AllocateKernelMemory(rsdt->Length);
271memcpy (rsdt_mod, rsdt, rsdt->Length);
272rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
273rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
274rsdt_entries=(uint32_t *)(rsdt_mod+1);
275for (i=0;i<rsdt_entries_num;i++)
276{
277char *table=(char *)(rsdt_entries[i]);
278if (!table)
279continue;
280
281DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
282
283rsdt_entries[i-dropoffset]=rsdt_entries[i];
284if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
285{
286dropoffset++;
287continue;
288}
289if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
290{
291DBG("DSDT found\n");
292rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
293continue;
294}
295if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
296{
297struct acpi_2_fadt *fadt, *fadt_mod;
298fadt=(struct acpi_2_fadt *)rsdt_entries[i];
299
300DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
301
302if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
303{
304printf("FADT incorrect. Not modified\n");
305continue;
306}
307
308fadt_mod = patch_fadt(fadt, new_dsdt);
309
310rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
311continue;
312}
313}
314DBG("\n");
315
316// Correct the checksum of RSDT
317rsdt_mod->Length-=4*dropoffset;
318
319DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
320
321rsdt_mod->Checksum=0;
322rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
323
324DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
325}
326else
327{
328rsdp_mod->RsdtAddress=0;
329printf("RSDT not found or RSDT incorrect\n");
330}
331
332if (version)
333{
334struct acpi_2_xsdt *xsdt, *xsdt_mod;
335
336// FIXME: handle 64-bit address correctly
337
338xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
339DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
340xsdt->Length);
341if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
342{
343uint64_t *xsdt_entries;
344int xsdt_entries_num, i;
345int dropoffset=0;
346
347xsdt_mod=(struct acpi_2_xsdt*)AllocateKernelMemory(xsdt->Length);
348memcpy(xsdt_mod, xsdt, xsdt->Length);
349rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
350xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
351xsdt_entries=(uint64_t *)(xsdt_mod+1);
352for (i=0;i<xsdt_entries_num;i++)
353{
354char *table=(char *)((uint32_t)(xsdt_entries[i]));
355if (!table)
356continue;
357xsdt_entries[i-dropoffset]=xsdt_entries[i];
358if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
359{
360dropoffset++;
361continue;
362}
363if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
364{
365DBG("DSDT found\n");
366
367xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
368
369DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
370
371continue;
372}
373if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
374{
375struct acpi_2_fadt *fadt, *fadt_mod;
376fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
377
378DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
379 fadt->Length);
380
381if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
382{
383printf("FADT incorrect or after 4GB. Dropping XSDT\n");
384goto drop_xsdt;
385}
386
387fadt_mod = patch_fadt(fadt, new_dsdt);
388
389xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
390
391DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
392
393continue;
394}
395
396DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
397
398}
399
400// Correct the checksum of XSDT
401xsdt_mod->Length-=8*dropoffset;
402xsdt_mod->Checksum=0;
403xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
404}
405else
406{
407drop_xsdt:
408
409DBG("About to drop XSDT\n");
410
411/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
412 * A Better strategy would be to generate
413 */
414
415rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
416printf("XSDT not found or XSDT incorrect\n");
417}
418}
419
420// Correct the checksum of RSDP
421
422DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
423
424rsdp_mod->Checksum=0;
425rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
426
427DBG("New checksum %d\n", rsdp_mod->Checksum);
428
429if (version)
430{
431DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
432
433rsdp_mod->ExtendedChecksum=0;
434rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
435
436DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
437
438}
439
440verbose("Patched ACPI version %d DSDT\n", version+1);
441if (version)
442{
443acpi20_p = (uint32_t)rsdp_mod;
444addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
445}
446else
447{
448acpi10_p = (uint32_t)rsdp_mod;
449addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
450}
451}
452#if DEBUG_DSDT
453printf("Press a key to continue... (DEBUG_DSDT)\n");
454getc();
455#endif
456return 1;
457}
458

Archive Download this file

Revision: 21