1 | /*␊ |
2 | *␉Copyright 2009 Jasmin Fazlic All rights reserved.␊ |
3 | */␊ |
4 | /*␊ |
5 | *␉Cleaned and merged by iNDi␊ |
6 | */␊ |
7 | ␊ |
8 | #include "libsaio.h"␊ |
9 | #include "bootstruct.h"␊ |
10 | #include "pci.h"␊ |
11 | #include "pci_root.h"␊ |
12 | #include "device_inject.h"␊ |
13 | #include "convert.h"␊ |
14 | ␊ |
15 | #ifndef DEBUG_INJECT␊ |
16 | #define DEBUG_INJECT 0␊ |
17 | #endif␊ |
18 | ␊ |
19 | #if DEBUG_INJECT␊ |
20 | #define DBG(x...)␉printf(x)␊ |
21 | #else␊ |
22 | #define DBG(x...)␊ |
23 | #endif␊ |
24 | ␊ |
25 | uint32_t devices_number = 1;␊ |
26 | struct DevPropString *string = 0;␊ |
27 | uint8_t *stringdata = 0;␊ |
28 | uint32_t stringlength = 0;␊ |
29 | ␊ |
30 | char *efi_inject_get_devprop_string(uint32_t *len)␊ |
31 | {␊ |
32 | ␉if(string)␊ |
33 | ␉{␊ |
34 | ␉␉*len = string->length;␊ |
35 | ␉␉return devprop_generate_string(string);␊ |
36 | ␉}␊ |
37 | ␉verbose("efi_inject_get_devprop_string NULL trying stringdata\n");␊ |
38 | ␉return NULL;␊ |
39 | }␊ |
40 | ␊ |
41 | void setupDeviceProperties(Node *node)␊ |
42 | {␊ |
43 | ␉const char *val;␊ |
44 | ␉uint8_t *binStr;␊ |
45 | ␉int cnt, cnt2;␊ |
46 | ␉␊ |
47 | ␉static char DEVICE_PROPERTIES_PROP[] = "device-properties";␊ |
48 | ␉␊ |
49 | ␉/* Generate devprop string.␊ |
50 | ␉ */␊ |
51 | ␉uint32_t strlength;␊ |
52 | ␉char *string = efi_inject_get_devprop_string(&strlength);␊ |
53 | ␉␊ |
54 | ␉/* Use the static "device-properties" boot config key contents if available,␊ |
55 | ␉ * otheriwse use the generated one.␊ |
56 | ␉ */ ␊ |
57 | ␉if (!getValueForKey(kDeviceProperties, &val, &cnt, DEFAULT_BOOT_CONFIG) && string)␊ |
58 | ␉{␊ |
59 | ␉␉val = (const char*)string;␊ |
60 | ␉␉cnt = strlength * 2;␊ |
61 | ␉} ␊ |
62 | ␊ |
63 | ␉if (cnt > 1)␊ |
64 | ␉{␊ |
65 | ␉␉binStr = convertHexStr2Binary(val, &cnt2);␊ |
66 | ␉␉if (cnt2 > 0) DT__AddProperty(node, DEVICE_PROPERTIES_PROP, cnt2, binStr);␊ |
67 | ␉}␊ |
68 | }␊ |
69 | ␊ |
70 | struct DevPropString *devprop_create_string(void)␊ |
71 | {␊ |
72 | ␉string = (struct DevPropString*)malloc(sizeof(struct DevPropString));␊ |
73 | ␉␊ |
74 | ␉if(string == NULL)␊ |
75 | ␉{␊ |
76 | ␉␉return NULL;␊ |
77 | ␉}␊ |
78 | ␉␊ |
79 | ␉memset(string, 0, sizeof(struct DevPropString));␊ |
80 | ␉string->length = 12;␊ |
81 | ␉string->WHAT2 = 0x01000000;␊ |
82 | ␉return string;␊ |
83 | }␊ |
84 | ␊ |
85 | struct DevPropDevice *devprop_add_device(struct DevPropString *string, char *path)␊ |
86 | {␊ |
87 | ␉struct DevPropDevice␉*device;␊ |
88 | ␉const char␉␉pciroot_string[] = "PciRoot(0x";␊ |
89 | ␉const char␉␉pci_device_string[] = "Pci(0x";␊ |
90 | ␉␊ |
91 | ␉if (string == NULL || path == NULL) {␊ |
92 | ␉␉return NULL;␊ |
93 | ␉}␊ |
94 | ␉device = malloc(sizeof(struct DevPropDevice));␊ |
95 | ␉␊ |
96 | ␉if (strncmp(path, pciroot_string, strlen(pciroot_string))) {␊ |
97 | ␉␉printf("ERROR parsing device path\n");␊ |
98 | ␉␉return NULL;␊ |
99 | ␉}␊ |
100 | ␉␊ |
101 | ␉memset(device, 0, sizeof(struct DevPropDevice));␊ |
102 | ␉device->acpi_dev_path._UID = getPciRootUID();␊ |
103 | ␉␊ |
104 | ␉int numpaths = 0;␊ |
105 | ␉int␉␉x, curr = 0;␊ |
106 | ␉char␉buff[] = "00";␊ |
107 | ␉␊ |
108 | ␉for (x = 0; x < strlen(path); x++) ␊ |
109 | ␉{␊ |
110 | ␉␉if (!strncmp(&path[x], pci_device_string, strlen(pci_device_string)))␊ |
111 | ␉␉{␊ |
112 | ␉␉␉x+=strlen(pci_device_string);␊ |
113 | ␉␉␉curr=x;␊ |
114 | ␉␉␉while(path[++x] != ',');␊ |
115 | ␉␉␉if(x-curr == 2)␊ |
116 | ␉␉␉{␊ |
117 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
118 | ␉␉␉}␊ |
119 | ␉␉␉else if(x-curr == 1)␊ |
120 | ␉␉␉{␊ |
121 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
122 | ␉␉␉}␊ |
123 | ␉␉␉else ␊ |
124 | ␉␉␉{␊ |
125 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
126 | ␉␉␉␉numpaths = 0;␊ |
127 | ␉␉␉␉break;␊ |
128 | ␉␉␉}␊ |
129 | ␉␉␉device->pci_dev_path[numpaths].device =␉ascii_hex_to_int(buff);␊ |
130 | ␉␉␉␊ |
131 | ␉␉␉x += 3; // 0x␊ |
132 | ␉␉␉curr = x;␊ |
133 | ␉␉␉while(path[++x] != ')');␊ |
134 | ␉␉␉if(x-curr == 2)␊ |
135 | ␉␉␉{␊ |
136 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
137 | ␉␉␉}␊ |
138 | ␉␉␉else if(x-curr == 1)␊ |
139 | ␉␉␉{␊ |
140 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
141 | ␉␉␉}␊ |
142 | ␉␉␉else␊ |
143 | ␉␉␉{␊ |
144 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
145 | ␉␉␉␉numpaths = 0;␊ |
146 | ␉␉␉␉break;␊ |
147 | ␉␉␉}␊ |
148 | ␉␉␉device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path␊ |
149 | ␉␉␉␊ |
150 | ␉␉␉numpaths++;␊ |
151 | ␉␉}␊ |
152 | ␉}␊ |
153 | ␉␊ |
154 | ␉if(!numpaths)␊ |
155 | ␉{␊ |
156 | ␉␉return NULL;␊ |
157 | ␉}␊ |
158 | ␉␊ |
159 | ␉device->numentries = 0x00;␊ |
160 | ␉␊ |
161 | ␉device->acpi_dev_path.length = 0x0c;␊ |
162 | ␉device->acpi_dev_path.type = 0x02;␊ |
163 | ␉device->acpi_dev_path.subtype = 0x01;␊ |
164 | ␉device->acpi_dev_path._HID = 0xd041030a;␊ |
165 | ␉␊ |
166 | ␉device->num_pci_devpaths = numpaths;␊ |
167 | ␉device->length = 24 + (6*numpaths);␊ |
168 | ␉␊ |
169 | ␉int␉␉i; ␊ |
170 | ␉␊ |
171 | ␉for(i = 0; i < numpaths; i++)␊ |
172 | ␉{␊ |
173 | ␉␉device->pci_dev_path[i].length = 0x06;␊ |
174 | ␉␉device->pci_dev_path[i].type = 0x01;␊ |
175 | ␉␉device->pci_dev_path[i].subtype = 0x01;␊ |
176 | ␉}␊ |
177 | ␉␊ |
178 | ␉device->path_end.length = 0x04;␊ |
179 | ␉device->path_end.type = 0x7f;␊ |
180 | ␉device->path_end.subtype = 0xff;␊ |
181 | ␉␊ |
182 | ␉device->string = string;␊ |
183 | ␉device->data = NULL;␊ |
184 | ␉string->length += device->length;␊ |
185 | ␉␊ |
186 | ␉if(!string->entries)␊ |
187 | ␉{␊ |
188 | ␉␉if((string->entries = (struct DevPropDevice**)malloc(sizeof(device)))== NULL)␊ |
189 | ␉␉{␊ |
190 | ␉␉␉return 0;␊ |
191 | ␉␉}␊ |
192 | ␉}␊ |
193 | ␉␊ |
194 | ␉string->entries[string->numentries++] = (struct DevPropDevice*)malloc(sizeof(device));␊ |
195 | ␉string->entries[string->numentries-1] = device;␊ |
196 | ␉␊ |
197 | ␉return device;␊ |
198 | }␊ |
199 | ␊ |
200 | int devprop_add_value(struct DevPropDevice *device, char *nm, uint8_t *vl, uint32_t len)␊ |
201 | {␊ |
202 | ␉␊ |
203 | ␉if(!nm || !vl || !len)␊ |
204 | ␉{␊ |
205 | ␉␉return 0;␊ |
206 | ␉}␊ |
207 | ␉␊ |
208 | ␉uint32_t length = ((strlen(nm) * 2) + len + (2 * sizeof(uint32_t)) + 2);␊ |
209 | ␉uint8_t *data = (uint8_t*)malloc(length);␊ |
210 | ␉{␊ |
211 | ␉␉if(!data)␊ |
212 | ␉␉{␊ |
213 | ␉␉␉return 0;␊ |
214 | ␉␉}␊ |
215 | ␉␉␊ |
216 | ␉␉memset(data, 0, length);␊ |
217 | ␉␉uint32_t off= 0;␊ |
218 | ␉␉data[off+1] = ((strlen(nm) * 2) + 6) >> 8;␊ |
219 | ␉␉data[off] = ((strlen(nm) * 2) + 6) & 0x00FF;␊ |
220 | ␉␉␊ |
221 | ␉␉off += 4;␊ |
222 | ␉␉uint32_t i=0, l = strlen(nm);␊ |
223 | ␉␉for(i = 0 ; i < l ; i++, off += 2)␊ |
224 | ␉␉{␊ |
225 | ␉␉␉data[off] = *nm++;␊ |
226 | ␉␉}␊ |
227 | ␉␉␊ |
228 | ␉␉off += 2;␊ |
229 | ␉␉l = len;␊ |
230 | ␉␉uint32_t *datalength = (uint32_t*)&data[off];␊ |
231 | ␉␉*datalength = l + 4;␊ |
232 | ␉␉off += 4;␊ |
233 | ␉␉for(i = 0 ; i < l ; i++, off++)␊ |
234 | ␉␉{␊ |
235 | ␉␉␉data[off] = *vl++;␊ |
236 | ␉␉}␊ |
237 | ␉}␉␊ |
238 | ␉␊ |
239 | ␉uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));␊ |
240 | ␉␊ |
241 | ␉uint8_t *newdata = (uint8_t*)malloc((length + offset));␊ |
242 | ␉if(!newdata)␊ |
243 | ␉{␊ |
244 | ␉␉return 0;␊ |
245 | ␉}␊ |
246 | ␉if(device->data)␊ |
247 | ␉{␊ |
248 | ␉␉if(offset > 1)␊ |
249 | ␉␉{␊ |
250 | ␉␉␉memcpy(newdata, device->data, offset);␊ |
251 | ␉␉}␊ |
252 | ␉}␊ |
253 | ␉␊ |
254 | ␉memcpy(newdata + offset, data, length);␊ |
255 | ␉␊ |
256 | ␉device->length += length;␊ |
257 | ␉device->string->length += length;␊ |
258 | ␉device->numentries++;␊ |
259 | ␉␊ |
260 | ␉if(!device->data)␊ |
261 | ␉{␊ |
262 | ␉␉device->data = (uint8_t*)malloc(sizeof(uint8_t));␊ |
263 | ␉}␊ |
264 | ␉else␊ |
265 | ␉{␊ |
266 | ␉␉free(device->data);␊ |
267 | ␉}␊ |
268 | ␉␊ |
269 | ␉free(data);␊ |
270 | ␉device->data = newdata;␊ |
271 | ␉␊ |
272 | ␉return 1;␊ |
273 | }␊ |
274 | ␊ |
275 | char *devprop_generate_string(struct DevPropString *string)␊ |
276 | {␊ |
277 | ␉char *buffer = (char*)malloc(string->length * 2);␊ |
278 | ␉char *ptr = buffer;␊ |
279 | ␉␊ |
280 | ␉if(!buffer)␊ |
281 | ␉{␊ |
282 | ␉␉return NULL;␊ |
283 | ␉}␊ |
284 | ␉␊ |
285 | ␉sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,␊ |
286 | ␉␉␉dp_swap16(string->numentries), string->WHAT3);␊ |
287 | ␉buffer += 24;␊ |
288 | ␉int i = 0, x = 0;␊ |
289 | ␉␊ |
290 | ␉while(i < string->numentries)␊ |
291 | ␉{␊ |
292 | ␉␉sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),␊ |
293 | ␉␉␉␉dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);␊ |
294 | ␉␉␊ |
295 | ␉␉buffer += 16;␊ |
296 | ␉␉sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,␊ |
297 | ␉␉␉␉string->entries[i]->acpi_dev_path.subtype,␊ |
298 | ␉␉␉␉dp_swap16(string->entries[i]->acpi_dev_path.length),␊ |
299 | ␉␉␉␉string->entries[i]->acpi_dev_path._HID,␊ |
300 | ␉␉␉␉dp_swap32(string->entries[i]->acpi_dev_path._UID));␊ |
301 | ␉␉␊ |
302 | ␉␉buffer += 24;␊ |
303 | ␉␉for(x=0;x < string->entries[i]->num_pci_devpaths; x++)␊ |
304 | ␉␉{␊ |
305 | ␉␉␉sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,␊ |
306 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].subtype,␊ |
307 | ␉␉␉␉␉dp_swap16(string->entries[i]->pci_dev_path[x].length),␊ |
308 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].function,␊ |
309 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].device);␊ |
310 | ␉␉␉buffer += 12;␊ |
311 | ␉␉}␊ |
312 | ␉␉␊ |
313 | ␉␉sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,␊ |
314 | ␉␉␉␉string->entries[i]->path_end.subtype,␊ |
315 | ␉␉␉␉dp_swap16(string->entries[i]->path_end.length));␊ |
316 | ␉␉␊ |
317 | ␉␉buffer += 8;␊ |
318 | ␉␉uint8_t *dataptr = string->entries[i]->data;␊ |
319 | ␉␉for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)␊ |
320 | ␉␉{␊ |
321 | ␉␉␉sprintf(buffer, "%02x", *dataptr++);␊ |
322 | ␉␉␉buffer += 2;␊ |
323 | ␉␉}␊ |
324 | ␉␉i++;␊ |
325 | ␉}␊ |
326 | ␉return ptr;␊ |
327 | }␊ |
328 | ␊ |
329 | void devprop_free_string(struct DevPropString *string)␊ |
330 | {␊ |
331 | ␉if(!string)␊ |
332 | ␉{␊ |
333 | ␉␉return;␊ |
334 | ␉}␊ |
335 | ␉␊ |
336 | ␉int i;␊ |
337 | ␉for(i = 0; i < string->numentries; i++)␊ |
338 | ␉{␊ |
339 | ␉␉if(string->entries[i])␊ |
340 | ␉␉{␊ |
341 | ␉␉␉if(string->entries[i]->data)␊ |
342 | ␉␉␉{␊ |
343 | ␉␉␉␉free(string->entries[i]->data);␊ |
344 | ␉␉␉␉string->entries[i]->data = NULL;␊ |
345 | ␉␉␉}␊ |
346 | ␉␉␉free(string->entries[i]);␊ |
347 | ␉␉␉string->entries[i] = NULL;␊ |
348 | ␉␉}␊ |
349 | ␉}␊ |
350 | ␉␊ |
351 | ␉free(string);␊ |
352 | ␉string = NULL;␊ |
353 | } |