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

Archive Download this file

Revision: 16