Chameleon

Chameleon Svn Source Tree

Root/trunk/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); // Rek: add the '/' because filename contains no path !
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 = false;
128bool fix_restart;
129
130// Restart Fix
131if (Platform.CPU.Vendor == 0x756E6547) {/* Intel */
132fix_restart = true;
133getBoolForKey(kRestartFix, &fix_restart, &bootInfo->bootConfig);
134} else {
135verbose ("Not an Intel platform: Restart Fix not applied !!!\n");
136fix_restart = false;
137}
138
139if (fix_restart) fadt_rev2_needed = true;
140
141// Allocate new fadt table
142if (fadt->Length < 0x84 && fadt_rev2_needed)
143{
144fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(0x84);
145memcpy(fadt_mod, fadt, fadt->Length);
146fadt_mod->Length = 0x84;
147fadt_mod->Revision = 0x02; // FADT rev 2 (ACPI 1.0B MS extensions)
148}
149else
150{
151fadt_mod=(struct acpi_2_fadt *)AllocateKernelMemory(fadt->Length);
152memcpy(fadt_mod, fadt, fadt->Length);
153}
154
155// Set PM_Profile from System-type
156if (fadt_mod->PM_Profile != Platform.Type) {
157verbose("FADT: changing PM_Profile from 0x%02x to 0x%02x\n", fadt_mod->PM_Profile, Platform.Type);
158fadt_mod->PM_Profile = Platform.Type;
159}
160
161// Patch FADT to fix restart
162if (fix_restart)
163{
164fadt_mod->Flags|= 0x400;
165fadt_mod->Reset_SpaceID= 0x01; // System I/O
166fadt_mod->Reset_BitWidth= 0x08; // 1 byte
167fadt_mod->Reset_BitOffset= 0x00; // Offset 0
168fadt_mod->Reset_AccessWidth= 0x01; // Byte access
169fadt_mod->Reset_Address= 0x0cf9; // Address of the register
170fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
171verbose("FADT: Restart Fix applied !\n");
172}
173
174// Patch DSDT Address
175DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
176
177fadt_mod->DSDT=(uint32_t)new_dsdt;
178if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
179fadt_mod->X_DSDT=(uint32_t)new_dsdt;
180
181DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
182
183// Correct the checksum
184fadt_mod->Checksum=0;
185fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
186
187return fadt_mod;
188}
189
190/* Setup ACPI without replacing DSDT. */
191int setupAcpiNoMod()
192{
193//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
194//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
195/* XXX aserebln why uint32 cast if pointer is uint64 ? */
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(void)
205{
206int version;
207void *new_dsdt;
208const char *dsdt_filename;
209
210int fd;
211int len;
212bool drop_ssdt;
213
214if (!getValueForKey(kDSDT, &dsdt_filename, &len, &bootInfo->bootConfig))
215dsdt_filename="DSDT.aml";
216
217// Load replacement DSDT
218new_dsdt=loadACPITable(dsdt_filename);
219if (!new_dsdt)
220{
221close(fd);
222return setupAcpiNoMod();
223}
224
225DBG("New DSDT Loaded in memory\n");
226
227{
228bool tmp;
229drop_ssdt=getBoolForKey(kDropSSDT, &tmp, &bootInfo->bootConfig)&&tmp;
230}
231
232// Do the same procedure for both versions of ACPI
233for (version=0; version<2; version++) {
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);
309rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
310continue;
311}
312}
313DBG("\n");
314
315// Correct the checksum of RSDT
316rsdt_mod->Length-=4*dropoffset;
317
318DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
319
320rsdt_mod->Checksum=0;
321rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
322
323DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
324}
325else
326{
327rsdp_mod->RsdtAddress=0;
328printf("RSDT not found or RSDT incorrect\n");
329}
330
331if (version)
332{
333struct acpi_2_xsdt *xsdt, *xsdt_mod;
334
335// FIXME: handle 64-bit address correctly
336
337xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
338DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
339xsdt->Length);
340if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
341{
342uint64_t *xsdt_entries;
343int xsdt_entries_num, i;
344int dropoffset=0;
345
346xsdt_mod=(struct acpi_2_xsdt*)AllocateKernelMemory(xsdt->Length);
347memcpy(xsdt_mod, xsdt, xsdt->Length);
348rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
349xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
350xsdt_entries=(uint64_t *)(xsdt_mod+1);
351for (i=0;i<xsdt_entries_num;i++)
352{
353char *table=(char *)((uint32_t)(xsdt_entries[i]));
354if (!table)
355continue;
356xsdt_entries[i-dropoffset]=xsdt_entries[i];
357if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
358{
359dropoffset++;
360continue;
361}
362if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
363{
364DBG("DSDT found\n");
365
366xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
367
368DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
369
370continue;
371}
372if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
373{
374struct acpi_2_fadt *fadt, *fadt_mod;
375fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
376
377DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
378 fadt->Length);
379
380if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
381{
382verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
383goto drop_xsdt;
384}
385
386fadt_mod = patch_fadt(fadt, new_dsdt);
387xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
388
389DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
390
391continue;
392}
393
394DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
395
396}
397
398// Correct the checksum of XSDT
399xsdt_mod->Length-=8*dropoffset;
400xsdt_mod->Checksum=0;
401xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
402}
403else
404{
405drop_xsdt:
406
407DBG("About to drop XSDT\n");
408
409/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
410 * A Better strategy would be to generate
411 */
412
413rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
414verbose("XSDT not found or XSDT incorrect\n");
415}
416}
417
418// Correct the checksum of RSDP
419
420DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
421
422rsdp_mod->Checksum=0;
423rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
424
425DBG("New checksum %d\n", rsdp_mod->Checksum);
426
427if (version)
428{
429DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
430
431rsdp_mod->ExtendedChecksum=0;
432rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
433
434DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
435
436}
437
438verbose("Patched ACPI version %d DSDT\n", version+1);
439if (version)
440{
441/* XXX aserebln why uint32 cast if pointer is uint64 ? */
442acpi20_p = (uint32_t)rsdp_mod;
443addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
444}
445else
446{
447/* XXX aserebln why uint32 cast if pointer is uint64 ? */
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: 40