Chameleon

Chameleon Svn Source Tree

Root/branches/Bungo/i386/libsaio/pci_root.c

1/*
2 * Copyright 2009 netkas
3 */
4
5#include "libsaio.h"
6#include "boot.h"
7#include "bootstruct.h"
8
9#ifndef DEBUG_PCIROOT
10#define DEBUG_PCIROOT 0
11#endif
12
13#if DEBUG_PCIROOT
14#define DBG(x...) printf(x)
15#else
16#define DBG(x...) msglog(x)
17#endif
18
19static int rootuid = 10; //value means function wasn't ran yet
20
21static unsigned int findrootuid(unsigned char * dsdt, int len)
22{
23int i;
24for (i=0; i<64 && i<len-5; i++) //not far than 64 symbols from pci root
25{
26if(dsdt[i] == '_' && dsdt[i+1] == 'U' && dsdt[i+2] == 'I' && dsdt[i+3] == 'D' && dsdt[i+5] == 0x08)
27{
28return dsdt[i+4];
29}
30}
31return 11;
32}
33
34static unsigned int findpciroot(unsigned char * dsdt,int len)
35{
36int i;
37
38for (i=0; i<len-4; i++) {
39if(dsdt[i] == 'P' && dsdt[i+1] == 'C' && dsdt[i+2] == 'I' && (dsdt[i+3] == 0x08 || dsdt [i+4] == 0x08)) {
40return findrootuid(dsdt+i, len-i);
41}
42}
43return 10;
44}
45
46int getPciRootUID(void)
47{
48char dsdt_dirSpec[256];
49const char *val = NULL;
50int len = 0;
51const char *dsdt_filename = NULL;
52//extern int search_and_get_acpi_fd(const char *, const char **);
53 //extern void *loadACPITable (const char *filename);
54
55if (rootuid < 10)
56 return rootuid;
57
58rootuid = 0;/* default _UID = 0 */
59
60if (getValueForKey(kPCIRootUID, &val, &len, &bootInfo->chameleonConfig) && len) {
61if (isdigit(val[0])) rootuid = val[0] - '0';
62goto out;
63}
64/* Chameleon compatibility */
65else if (getValueForKey("PciRoot", &val, &len, &bootInfo->chameleonConfig) && len) {
66if (isdigit(val[0])) rootuid = val[0] - '0';
67goto out;
68}
69/* PCEFI compatibility */
70else if (getValueForKey("-pci0", &val, &len, &bootInfo->chameleonConfig) && len) {
71rootuid = 0;
72goto out;
73}
74else if (getValueForKey("-pci1", &val, &len, &bootInfo->chameleonConfig) && len) {
75rootuid = 1;
76goto out;
77}
78
79 if (new_dsdt != NULL) {
80 len = *(int *)(new_dsdt + 4);
81 verbose("PCIrootUID: custom DSDT already loaded @%08X, length=%d.\n", new_dsdt, len);
82 } else {
83 // Try using the file specified with the DSDT option
84 if (getValueForKey(kDSDT, &dsdt_filename, &len, &bootInfo->chameleonConfig) && len) {
85 snprintf(dsdt_dirSpec, sizeof(dsdt_dirSpec), dsdt_filename);
86 } else {
87 sprintf(dsdt_dirSpec, "DSDT.aml");
88 }
89
90 verbose("PCIrootUID: attempting to load custom DSDT...\n");
91 if ((new_dsdt = loadACPITable(dsdt_dirSpec))) {
92 len = *(int *)(new_dsdt + 4);
93 verbose("PCIrootUID: custom DSDT loaded @%08X, length=%d\n", new_dsdt, len);
94 } else {
95 //verbose("PCIrootUID: custom DSDT not found!.\n");
96 }
97 }
98/*
99// Try using the file specified with the DSDT option
100if (getValueForKey(kDSDT, &dsdt_filename, &len, &bootInfo->chameleonConfig) && len)
101{
102snprintf(dsdt_dirSpec, sizeof(dsdt_dirSpec), dsdt_filename);
103}
104else
105{
106sprintf(dsdt_dirSpec, "DSDT.aml");
107}
108
109 DBG("PCIrootUID: trying file DSDT.aml... ");
110
111int fd = search_and_get_acpi_fd(dsdt_dirSpec, &dsdt_filename);
112
113// Check booting partition
114if (fd<0)
115{
116 DBG("PCIrootUID: file '%s' not found, using default value.\n", dsdt_filename);
117 rootuid = 0;
118 goto out;
119}
120
121fsize = file_size(fd);
122
123if (!(new_dsdt = malloc(fsize))) {
124DBG("PCIrootUID: ERROR: allocating DSDT memory failed.\n");
125close (fd);
126goto out;
127}
128
129if (read (fd, new_dsdt, fsize) != fsize) {
130DBG("PCIrootUID: ERROR: failed reading DSDT from '%s'.\n", dsdt_filename);
131free(new_dsdt);
132close (fd);
133goto out;
134}
135
136close (fd);
137*/
138if (len)
139 rootuid = findpciroot(new_dsdt, len);
140/*
141free(new_dsdt);
142*/
143// make sure it really works:
144if (rootuid == 11)
145 rootuid = 0; //usually when _UID isn't present, it means uid is zero
146else if (rootuid < 0 || rootuid > 9)
147{
148verbose("PCIrootUID: proper value wasn't found. Using default value (0).\n");
149rootuid = 0;
150 return rootuid;
151}
152
153out:
154 verbose("PCIrootUID=0x%02X: using.\n", rootuid);
155return rootuid;
156}
157

Archive Download this file

Revision: 2537