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 1
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{
48char dsdt_dirSpec[128];
49
50void *new_dsdt;
51const char *val;
52int len, fsize;
53const char * dsdt_filename = NULL;
54extern int search_and_get_acpi_fd(const char *, const char **);
55
56if (rootuid < 10)
57{
58return rootuid;
59}
60
61rootuid = 0;/* default uid = 0 */
62
63if (getValueForKey(kPCIRootUID, &val, &len, &bootInfo->chameleonConfig))
64{
65if (isdigit(val[0]))
66{
67rootuid = val[0] - '0';
68}
69
70goto out;
71}
72/* Chameleon compatibility */
73else if (getValueForKey("PciRoot", &val, &len, &bootInfo->chameleonConfig))
74{
75if (isdigit(val[0]))
76{
77rootuid = val[0] - '0';
78}
79
80goto out;
81}
82/* PCEFI compatibility */
83else if (getValueForKey("-pci0", &val, &len, &bootInfo->chameleonConfig))
84{
85rootuid = 0;
86goto out;
87}
88else if (getValueForKey("-pci1", &val, &len, &bootInfo->chameleonConfig))
89{
90rootuid = 1;
91goto out;
92}
93
94// Try using the file specified with the DSDT option
95if (getValueForKey(kDSDT, &dsdt_filename, &len, &bootInfo->chameleonConfig))
96{
97strncpy(dsdt_dirSpec, dsdt_filename, sizeof(dsdt_dirSpec) );
98}
99else
100{
101strcpy(dsdt_dirSpec, "DSDT.aml");
102}
103
104int fd = search_and_get_acpi_fd(dsdt_dirSpec, &dsdt_filename);
105
106// Check booting partition
107if (fd<0)
108{
109verbose("No DSDT found, using 0 as uid value.\n");
110rootuid = 0;
111goto out;
112}
113
114fsize = file_size(fd);
115
116if (!(new_dsdt = malloc(fsize)))
117{
118verbose("[ERROR] alloc DSDT memory failed\n");
119close (fd);
120goto out;
121}
122if (read (fd, new_dsdt, fsize) != fsize)
123{
124verbose("[ERROR] read %s failed\n", dsdt_filename);
125free(new_dsdt);
126close (fd);
127goto out;
128}
129close (fd);
130
131rootuid = findpciroot(new_dsdt, fsize);
132free(new_dsdt);
133
134// make sure it really works:
135if (rootuid == 11)
136{
137rootuid=0; //usually when _UID isnt present, it means uid is zero
138}
139else if (rootuid < 0 || rootuid > 9)
140{
141printf("PciRoot uid value wasnt found, using 0, if you want it to be 1, use -PciRootUID flag");
142rootuid = 0;
143}
144out:
145verbose("Using PCI-Root-UID value: %d\n", rootuid);
146return rootuid;
147}
148

Archive Download this file

Revision: 2713