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);
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
210char dirspec[512];
211
212int fd;
213
214int len;
215bool tmp;
216bool drop_ssdt;
217bool fix_restart;
218
219if (!getValueForKey(kDSDT, &dsdt_filename, &len, &bootInfo->bootConfig))
220dsdt_filename="DSDT.aml";
221
222// Load replacement DSDT
223new_dsdt=loadACPITable(dsdt_filename);
224if (!new_dsdt)
225{
226close(fd);
227return setupAcpiNoMod();
228}
229
230DBG("New DSDT Loaded in memory\n");
231
232{
233bool tmp;
234drop_ssdt=getBoolForKey(kDropSSDT, &tmp, &bootInfo->bootConfig)&&tmp;
235}
236
237// Do the same procedure for both versions of ACPI
238for (version=0; version<2; version++) {
239struct acpi_2_rsdp *rsdp, *rsdp_mod;
240struct acpi_2_rsdt *rsdt, *rsdt_mod;
241int rsdplength;
242
243// Find original rsdp
244rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
245if (!rsdp)
246{
247DBG("No ACPI version %d found. Ignoring\n", version+1);
248if (version)
249addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
250else
251addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
252continue;
253}
254rsdplength=version?rsdp->Length:20;
255
256DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
257
258/* FIXME: no check that memory allocation succeeded
259 * Copy and patch RSDP,RSDT, XSDT and FADT
260 * For more info see ACPI Specification pages 110 and following
261 */
262
263rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
264memcpy(rsdp_mod, rsdp, rsdplength);
265rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
266
267DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
268
269if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
270{
271uint32_t *rsdt_entries;
272int rsdt_entries_num;
273int dropoffset=0, i;
274
275rsdt_mod=(struct acpi_2_rsdt *)AllocateKernelMemory(rsdt->Length);
276memcpy (rsdt_mod, rsdt, rsdt->Length);
277rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
278rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
279rsdt_entries=(uint32_t *)(rsdt_mod+1);
280for (i=0;i<rsdt_entries_num;i++)
281{
282char *table=(char *)(rsdt_entries[i]);
283if (!table)
284continue;
285
286DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
287
288rsdt_entries[i-dropoffset]=rsdt_entries[i];
289if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
290{
291dropoffset++;
292continue;
293}
294if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
295{
296DBG("DSDT found\n");
297rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
298continue;
299}
300if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
301{
302struct acpi_2_fadt *fadt, *fadt_mod;
303fadt=(struct acpi_2_fadt *)rsdt_entries[i];
304
305DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
306
307if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
308{
309printf("FADT incorrect. Not modified\n");
310continue;
311}
312
313fadt_mod = patch_fadt(fadt, new_dsdt);
314rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
315continue;
316}
317}
318DBG("\n");
319
320// Correct the checksum of RSDT
321rsdt_mod->Length-=4*dropoffset;
322
323DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
324
325rsdt_mod->Checksum=0;
326rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
327
328DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
329}
330else
331{
332rsdp_mod->RsdtAddress=0;
333printf("RSDT not found or RSDT incorrect\n");
334}
335
336if (version)
337{
338struct acpi_2_xsdt *xsdt, *xsdt_mod;
339
340// FIXME: handle 64-bit address correctly
341
342xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
343DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
344xsdt->Length);
345if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
346{
347uint64_t *xsdt_entries;
348int xsdt_entries_num, i;
349int dropoffset=0;
350
351xsdt_mod=(struct acpi_2_xsdt*)AllocateKernelMemory(xsdt->Length);
352memcpy(xsdt_mod, xsdt, xsdt->Length);
353rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
354xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
355xsdt_entries=(uint64_t *)(xsdt_mod+1);
356for (i=0;i<xsdt_entries_num;i++)
357{
358char *table=(char *)((uint32_t)(xsdt_entries[i]));
359if (!table)
360continue;
361xsdt_entries[i-dropoffset]=xsdt_entries[i];
362if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
363{
364dropoffset++;
365continue;
366}
367if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
368{
369DBG("DSDT found\n");
370
371xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
372
373DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
374
375continue;
376}
377if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
378{
379struct acpi_2_fadt *fadt, *fadt_mod;
380fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
381
382DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
383 fadt->Length);
384
385if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
386{
387verbose("FADT incorrect or after 4GB. Dropping XSDT\n");
388goto drop_xsdt;
389}
390
391fadt_mod = patch_fadt(fadt, new_dsdt);
392xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
393
394DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
395
396continue;
397}
398
399DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
400
401}
402
403// Correct the checksum of XSDT
404xsdt_mod->Length-=8*dropoffset;
405xsdt_mod->Checksum=0;
406xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
407}
408else
409{
410drop_xsdt:
411
412DBG("About to drop XSDT\n");
413
414/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
415 * A Better strategy would be to generate
416 */
417
418rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
419verbose("XSDT not found or XSDT incorrect\n");
420}
421}
422
423// Correct the checksum of RSDP
424
425DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
426
427rsdp_mod->Checksum=0;
428rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
429
430DBG("New checksum %d\n", rsdp_mod->Checksum);
431
432if (version)
433{
434DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
435
436rsdp_mod->ExtendedChecksum=0;
437rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
438
439DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
440
441}
442
443verbose("Patched ACPI version %d DSDT\n", version+1);
444if (version)
445{
446/* XXX aserebln why uint32 cast if pointer is uint64 ? */
447acpi20_p = (uint32_t)rsdp_mod;
448addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
449}
450else
451{
452/* XXX aserebln why uint32 cast if pointer is uint64 ? */
453acpi10_p = (uint32_t)rsdp_mod;
454addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
455}
456}
457#if DEBUG_DSDT
458printf("Press a key to continue... (DEBUG_DSDT)\n");
459getc();
460#endif
461return 1;
462}
463

Archive Download this file

Revision: 30