Chameleon

Chameleon Svn Source Tree

Root/trunk/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...)
17#endif
18
19static int rootuid = 10; //value means function wasnt 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{
48void *new_dsdt;
49const char *dsdt_filename;
50const char *val;
51int fd;
52int dsdt_uid;
53int len,fsize;
54
55if (rootuid < 10) {
56return rootuid;
57}
58rootuid = 0;/* default uid = 0 */
59
60if (getValueForKey(kPCIRootUID, &val, &len, &bootInfo->bootConfig)) {
61if (isdigit(val[0])) {
62rootuid = val[0] - '0';
63}
64goto out;
65}
66#if 1
67/* Chameleon compatibility */
68if (getValueForKey("PciRoot", &val, &len, &bootInfo->bootConfig)) {
69if (isdigit(val[0])) {
70rootuid = val[0] - '0';
71}
72goto out;
73}
74
75/* PCEFI compatibility */
76if (getValueForKey("-pci0", &val, &len, &bootInfo->bootConfig)) {
77rootuid = 0;
78goto out;
79}
80if (getValueForKey("-pci1", &val, &len, &bootInfo->bootConfig)) {
81rootuid = 1;
82goto out;
83}
84#endif
85if (!getValueForKey(kDSDT, &dsdt_filename, &len, &bootInfo->bootConfig)) {
86dsdt_filename="/Extra/DSDT.aml";
87}
88
89if ((fd = open_bvdev("bt(0,0)", dsdt_filename, 0)) < 0) {
90verbose("[WARNING] %s not found\n", dsdt_filename);
91goto out;
92}
93fsize = file_size(fd);
94
95if ((new_dsdt = malloc(fsize)) == NULL) {
96verbose("[ERROR] alloc DSDT memory failed\n");
97close (fd);
98goto out;
99}
100if (read (fd, new_dsdt, fsize) != fsize) {
101verbose("[ERROR] read %s failed\n", dsdt_filename);
102close (fd);
103goto out;
104}
105close (fd);
106
107dsdt_uid = findpciroot(new_dsdt, fsize);
108free(new_dsdt);
109
110if(dsdt_uid == 11) dsdt_uid=0; //usually when _UID isnt present, it means uid is zero
111else if (dsdt_uid < 0 || dsdt_uid > 9)
112{
113printf("PciRoot uid value wasnt found, using 0, if you want it to be 1, use -PciRootUID flag");
114dsdt_uid = 0;
115//if(dsdt_uid == 10) //algo failed, PCI0 wasnt found
116}
117out:
118verbose("Using PCI-Root-UID value: %d\n", rootuid);
119return rootuid;
120}
121

Archive Download this file

Revision: 57