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// Patch FADT to fix restart
157if (fix_restart)
158{
159fadt_mod->Flags|= 0x400;
160fadt_mod->Reset_SpaceID= 0x01; // System I/O
161fadt_mod->Reset_BitWidth= 0x08; // 1 byte
162fadt_mod->Reset_BitOffset= 0x00; // Offset 0
163fadt_mod->Reset_AccessWidth= 0x01; // Byte access
164fadt_mod->Reset_Address= 0x0cf9; // Address of the register
165fadt_mod->Reset_Value= 0x06; // Value to write to reset the system
166verbose("FADT: Restart Fix applied !\n");
167}
168
169// Patch DSDT Address
170DBG("DSDT: Old @%x,%x, ",fadt_mod->DSDT,fadt_mod->X_DSDT);
171
172fadt_mod->DSDT=(uint32_t)new_dsdt;
173if ((uint32_t)(&(fadt_mod->X_DSDT))-(uint32_t)fadt_mod+8<=fadt_mod->Length)
174fadt_mod->X_DSDT=(uint32_t)new_dsdt;
175
176DBG("New @%x,%x\n",fadt_mod->DSDT,fadt_mod->X_DSDT);
177
178// Correct the checksum
179fadt_mod->Checksum=0;
180fadt_mod->Checksum=256-checksum8(fadt_mod,fadt_mod->Length);
181
182return fadt_mod;
183}
184
185/* Setup ACPI without replacing DSDT. */
186int setupAcpiNoMod()
187{
188//addConfigurationTable(&gEfiAcpiTableGuid, getAddressOfAcpiTable(), "ACPI");
189//addConfigurationTable(&gEfiAcpi20TableGuid, getAddressOfAcpi20Table(), "ACPI_20");
190acpi10_p = (uint32_t)getAddressOfAcpiTable();
191acpi20_p = (uint32_t)getAddressOfAcpi20Table();
192addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
193if(acpi20_p) addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
194return 1;
195}
196
197/* Setup ACPI. Replace DSDT if DSDT.aml is found */
198int setupAcpi()
199{
200int version;
201void *new_dsdt;
202const char *dsdt_filename;
203/* const char *facp_filename; */
204int len;
205boolean_t drop_ssdt=NO;
206
207//DBG("Enter setupACPI\n");
208
209if (!getValueForKey("DSDT", &dsdt_filename, &len, &bootInfo->bootConfig))
210dsdt_filename="DSDT.aml";
211
212// Load replacement DSDT
213new_dsdt=loadACPITable(dsdt_filename);
214if (!new_dsdt)
215{
216return setupAcpiNoMod();
217}
218DBG("New DSDT Loaded in memory\n");
219
220{
221BOOL tmp;
222drop_ssdt=getBoolForKey("DropSSDT",&tmp, &bootInfo->bootConfig)&&tmp;
223}
224
225// Do the same procedure for both versions of ACPI
226for (version=0;version<2;version++)
227{
228struct acpi_2_rsdp *rsdp, *rsdp_mod;
229struct acpi_2_rsdt *rsdt, *rsdt_mod;
230int rsdplength;
231
232// Find original rsdp
233rsdp=(struct acpi_2_rsdp *)(version?getAddressOfAcpi20Table():getAddressOfAcpiTable());
234if (!rsdp)
235{
236DBG("No ACPI version %d found. Ignoring\n", version+1);
237if (version)
238addConfigurationTable(&gEfiAcpi20TableGuid, NULL, "ACPI_20");
239else
240addConfigurationTable(&gEfiAcpiTableGuid, NULL, "ACPI");
241continue;
242}
243rsdplength=version?rsdp->Length:20;
244
245DBG("RSDP version %d found @%x. Length=%d\n",version+1,rsdp,rsdplength);
246
247/* FIXME: no check that memory allocation succeeded
248 * Copy and patch RSDP,RSDT, XSDT and FADT
249 * For more info see ACPI Specification pages 110 and following
250 */
251
252rsdp_mod=(struct acpi_2_rsdp *) AllocateKernelMemory(rsdplength);
253memcpy(rsdp_mod, rsdp, rsdplength);
254rsdt=(struct acpi_2_rsdt *)(rsdp->RsdtAddress);
255
256DBG("RSDT @%x, Length %d\n",rsdt, rsdt->Length);
257
258if (rsdt && (uint32_t)rsdt !=0xffffffff && rsdt->Length<0x10000)
259{
260uint32_t *rsdt_entries;
261int rsdt_entries_num;
262int dropoffset=0, i;
263
264rsdt_mod=(struct acpi_2_rsdt *)AllocateKernelMemory(rsdt->Length);
265memcpy (rsdt_mod, rsdt, rsdt->Length);
266rsdp_mod->RsdtAddress=(uint32_t)rsdt_mod;
267rsdt_entries_num=(rsdt_mod->Length-sizeof(struct acpi_2_rsdt))/4;
268rsdt_entries=(uint32_t *)(rsdt_mod+1);
269for (i=0;i<rsdt_entries_num;i++)
270{
271char *table=(char *)(rsdt_entries[i]);
272if (!table)
273continue;
274
275DBG("TABLE %c%c%c%c,",table[0],table[1],table[2],table[3]);
276
277rsdt_entries[i-dropoffset]=rsdt_entries[i];
278if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
279{
280dropoffset++;
281continue;
282}
283if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
284{
285DBG("DSDT found\n");
286rsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
287continue;
288}
289if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
290{
291struct acpi_2_fadt *fadt, *fadt_mod;
292fadt=(struct acpi_2_fadt *)rsdt_entries[i];
293
294DBG("FADT found @%x, Length %d\n",fadt, fadt->Length);
295
296if (!fadt || (uint32_t)fadt == 0xffffffff || fadt->Length>0x10000)
297{
298printf("FADT incorrect. Not modified\n");
299continue;
300}
301
302fadt_mod = patch_fadt(fadt, new_dsdt);
303
304rsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
305continue;
306}
307}
308DBG("\n");
309
310// Correct the checksum of RSDT
311rsdt_mod->Length-=4*dropoffset;
312
313DBG("RSDT: Original checksum %d, ", rsdt_mod->Checksum);
314
315rsdt_mod->Checksum=0;
316rsdt_mod->Checksum=256-checksum8(rsdt_mod,rsdt_mod->Length);
317
318DBG("New checksum %d at %x\n", rsdt_mod->Checksum,rsdt_mod);
319}
320else
321{
322rsdp_mod->RsdtAddress=0;
323printf("RSDT not found or RSDT incorrect\n");
324}
325
326if (version)
327{
328struct acpi_2_xsdt *xsdt, *xsdt_mod;
329
330// FIXME: handle 64-bit address correctly
331
332xsdt=(struct acpi_2_xsdt*) ((uint32_t)rsdp->XsdtAddress);
333DBG("XSDT @%x;%x, Length=%d\n", (uint32_t)(rsdp->XsdtAddress>>32),(uint32_t)rsdp->XsdtAddress,
334xsdt->Length);
335if (xsdt && (uint64_t)rsdp->XsdtAddress<0xffffffff && xsdt->Length<0x10000)
336{
337uint64_t *xsdt_entries;
338int xsdt_entries_num, i;
339int dropoffset=0;
340
341xsdt_mod=(struct acpi_2_xsdt*)AllocateKernelMemory(xsdt->Length);
342memcpy(xsdt_mod, xsdt, xsdt->Length);
343rsdp_mod->XsdtAddress=(uint32_t)xsdt_mod;
344xsdt_entries_num=(xsdt_mod->Length-sizeof(struct acpi_2_xsdt))/8;
345xsdt_entries=(uint64_t *)(xsdt_mod+1);
346for (i=0;i<xsdt_entries_num;i++)
347{
348char *table=(char *)((uint32_t)(xsdt_entries[i]));
349if (!table)
350continue;
351xsdt_entries[i-dropoffset]=xsdt_entries[i];
352if (drop_ssdt && table[0]=='S' && table[1]=='S' && table[2]=='D' && table[3]=='T')
353{
354dropoffset++;
355continue;
356}
357if (table[0]=='D' && table[1]=='S' && table[2]=='D' && table[3]=='T')
358{
359DBG("DSDT found\n");
360
361xsdt_entries[i-dropoffset]=(uint32_t)new_dsdt;
362
363DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
364
365continue;
366}
367if (table[0]=='F' && table[1]=='A' && table[2]=='C' && table[3]=='P')
368{
369struct acpi_2_fadt *fadt, *fadt_mod;
370fadt=(struct acpi_2_fadt *)(uint32_t)xsdt_entries[i];
371
372DBG("FADT found @%x,%x, Length %d\n",(uint32_t)(xsdt_entries[i]>>32),fadt,
373 fadt->Length);
374
375if (!fadt || (uint64_t)xsdt_entries[i] >= 0xffffffff || fadt->Length>0x10000)
376{
377printf("FADT incorrect or after 4GB. Dropping XSDT\n");
378goto drop_xsdt;
379}
380
381fadt_mod = patch_fadt(fadt, new_dsdt);
382
383xsdt_entries[i-dropoffset]=(uint32_t)fadt_mod;
384
385DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
386
387continue;
388}
389
390DBG("TABLE %c%c%c%c@%x,",table[0],table[1],table[2],table[3],xsdt_entries[i]);
391
392}
393
394// Correct the checksum of XSDT
395xsdt_mod->Length-=8*dropoffset;
396xsdt_mod->Checksum=0;
397xsdt_mod->Checksum=256-checksum8(xsdt_mod,xsdt_mod->Length);
398}
399else
400{
401drop_xsdt:
402
403DBG("About to drop XSDT\n");
404
405/*FIXME: Now we just hope that if MacOS doesn't find XSDT it reverts to RSDT.
406 * A Better strategy would be to generate
407 */
408
409rsdp_mod->XsdtAddress=0xffffffffffffffffLL;
410printf("XSDT not found or XSDT incorrect\n");
411}
412}
413
414// Correct the checksum of RSDP
415
416DBG("RSDP: Original checksum %d, ", rsdp_mod->Checksum);
417
418rsdp_mod->Checksum=0;
419rsdp_mod->Checksum=256-checksum8(rsdp_mod,20);
420
421DBG("New checksum %d\n", rsdp_mod->Checksum);
422
423if (version)
424{
425DBG("RSDP: Original extended checksum %d", rsdp_mod->ExtendedChecksum);
426
427rsdp_mod->ExtendedChecksum=0;
428rsdp_mod->ExtendedChecksum=256-checksum8(rsdp_mod,rsdp_mod->Length);
429
430DBG("New extended checksum %d\n", rsdp_mod->ExtendedChecksum);
431
432}
433
434verbose("Patched ACPI version %d DSDT\n", version+1);
435if (version)
436{
437acpi20_p = (uint32_t)rsdp_mod;
438addConfigurationTable(&gEfiAcpi20TableGuid, &acpi20_p, "ACPI_20");
439}
440else
441{
442acpi10_p = (uint32_t)rsdp_mod;
443addConfigurationTable(&gEfiAcpiTableGuid, &acpi10_p, "ACPI");
444}
445}
446#if DEBUG_DSDT
447printf("Press a key to continue... (DEBUG_DSDT)\n");
448getc();
449#endif
450return 1;
451}
452

Archive Download this file

Revision: 18