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