Chameleon

Chameleon Svn Source Tree

Root/trunk/i386/libsaio/pci_root.c

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

Archive Download this file

Revision: HEAD