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 "boot.h"␊ |
10 | #include "bootstruct.h"␊ |
11 | #include "pci.h"␊ |
12 | #include "pci_root.h"␊ |
13 | #include "device_inject.h"␊ |
14 | #include "convert.h"␊ |
15 | ␊ |
16 | #ifndef DEBUG_INJECT␊ |
17 | #define DEBUG_INJECT 0␊ |
18 | #endif␊ |
19 | ␊ |
20 | #if DEBUG_INJECT␊ |
21 | #define DBG(x...)␉printf(x)␊ |
22 | #else␊ |
23 | #define DBG(x...)␊ |
24 | #endif␊ |
25 | ␊ |
26 | uint32_t devices_number␉␉= 1;␊ |
27 | DevPropString *string␉␉= NULL;␊ |
28 | uint8_t *stringdata␉␉= NULL;␊ |
29 | uint32_t stringlength␉␉= 0;␊ |
30 | ␊ |
31 | char *efi_inject_get_devprop_string(uint32_t *len)␊ |
32 | {␊ |
33 | ␉if(string) {␊ |
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, &bootInfo->chameleonConfig) && string) {␊ |
58 | ␉␉val = (const char*)string;␊ |
59 | ␉␉cnt = strlength * 2;␊ |
60 | ␉}␊ |
61 | ␊ |
62 | ␉if (cnt > 1) {␊ |
63 | ␉␉binStr = convertHexStr2Binary(val, &cnt2);␊ |
64 | ␉␉if (cnt2 > 0) {␊ |
65 | ␉␉␉DT__AddProperty(node, DEVICE_PROPERTIES_PROP, cnt2, binStr);␊ |
66 | ␉␉}␊ |
67 | ␉}␊ |
68 | }␊ |
69 | ␊ |
70 | DevPropString *devprop_create_string(void)␊ |
71 | {␊ |
72 | ␉string = (struct DevPropString*)malloc(sizeof(struct DevPropString));␊ |
73 | ␊ |
74 | ␉if(string == NULL) {␊ |
75 | ␉␉return NULL;␊ |
76 | ␉}␊ |
77 | ␉␊ |
78 | ␉memset(string, 0, sizeof(struct DevPropString));␊ |
79 | ␉string->length = 12;␊ |
80 | ␉string->WHAT2 = 0x01000000;␊ |
81 | ␉return string;␊ |
82 | }␊ |
83 | ␊ |
84 | DevPropDevice *devprop_add_device(DevPropString *string, char *path)␊ |
85 | {␊ |
86 | ␉DevPropDevice␉*device = NULL;␊ |
87 | ␉const char␉␉pciroot_string[] = "PciRoot(0x";␊ |
88 | ␉const char␉␉pci_device_string[] = "Pci(0x";␊ |
89 | ␊ |
90 | ␉if (string == NULL || path == NULL) {␊ |
91 | ␉␉printf("ERROR null device path\n");␊ |
92 | ␉␉return NULL;␊ |
93 | ␉}␊ |
94 | ␊ |
95 | ␉if (strncmp(path, pciroot_string, strlen(pciroot_string))) {␊ |
96 | ␉␉printf("ERROR parsing device path\n");␊ |
97 | ␉␉return NULL;␊ |
98 | ␉}␊ |
99 | ␉if (!(device = malloc(sizeof(DevPropDevice)))) {␊ |
100 | ␉␉printf("ERROR malloc failed\n");␊ |
101 | ␉␉return NULL;␊ |
102 | ␉}␊ |
103 | ␊ |
104 | ␉memset(device, 0, sizeof(DevPropDevice));␊ |
105 | ␉device->acpi_dev_path._UID = getPciRootUID();␊ |
106 | ␊ |
107 | ␉int numpaths = 0;␊ |
108 | ␉int␉␉x, curr = 0;␊ |
109 | ␉char␉buff[] = "00";␊ |
110 | ␊ |
111 | ␉for (x = 0; x < strlen(path); x++) {␊ |
112 | ␉␉if (!strncmp(&path[x], pci_device_string, strlen(pci_device_string))) {␊ |
113 | ␉␉␉x+=strlen(pci_device_string);␊ |
114 | ␉␉␉curr=x;␊ |
115 | ␉␉␉while(path[++x] != ',');␊ |
116 | ␉␉␉if(x-curr == 2) {␊ |
117 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
118 | ␉␉␉} else if(x-curr == 1) {␊ |
119 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
120 | ␉␉␉} else {␊ |
121 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
122 | ␉␉␉␉numpaths = 0;␊ |
123 | ␉␉␉␉break;␊ |
124 | ␉␉␉}␊ |
125 | ␉␉␉device->pci_dev_path[numpaths].device =␉ascii_hex_to_int(buff);␊ |
126 | ␊ |
127 | ␉␉␉x += 3; // 0x␊ |
128 | ␉␉␉curr = x;␊ |
129 | ␉␉␉while(path[++x] != ')');␊ |
130 | ␉␉␉if(x-curr == 2) {␊ |
131 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
132 | ␉␉␉} else if(x-curr == 1) {␊ |
133 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
134 | ␉␉␉} else {␊ |
135 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
136 | ␉␉␉␉numpaths = 0;␊ |
137 | ␉␉␉␉break;␊ |
138 | ␉␉␉}␊ |
139 | ␉␉␉device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path␊ |
140 | ␊ |
141 | ␉␉␉numpaths++;␊ |
142 | ␉␉}␊ |
143 | ␉}␊ |
144 | ␉␊ |
145 | ␉if(!numpaths) {␊ |
146 | ␉␉free(device);␊ |
147 | ␉␉return NULL;␊ |
148 | ␉}␊ |
149 | ␉␊ |
150 | ␉device->numentries = 0x00;␊ |
151 | ␉␊ |
152 | ␉device->acpi_dev_path.length = 0x0c;␊ |
153 | ␉device->acpi_dev_path.type = 0x02;␊ |
154 | ␉device->acpi_dev_path.subtype = 0x01;␊ |
155 | ␉device->acpi_dev_path._HID = 0xd041030a;␊ |
156 | ␉␊ |
157 | ␉device->num_pci_devpaths = numpaths;␊ |
158 | ␉device->length = 24 + (6*numpaths);␊ |
159 | ␉␊ |
160 | ␉int␉␉i;␊ |
161 | ␉␊ |
162 | ␉for(i = 0; i < numpaths; i++)␊ |
163 | ␉{␊ |
164 | ␉␉device->pci_dev_path[i].length = 0x06;␊ |
165 | ␉␉device->pci_dev_path[i].type = 0x01;␊ |
166 | ␉␉device->pci_dev_path[i].subtype = 0x01;␊ |
167 | ␉}␊ |
168 | ␉␊ |
169 | ␉device->path_end.length = 0x04;␊ |
170 | ␉device->path_end.type = 0x7f;␊ |
171 | ␉device->path_end.subtype = 0xff;␊ |
172 | ␉␊ |
173 | ␉device->string = string;␊ |
174 | ␉device->data = NULL;␊ |
175 | ␊ |
176 | ␉if(!string->entries) {␊ |
177 | ␉␉if (!(string->entries = (struct DevPropDevice**) malloc(sizeof(device) * DEV_PROP_DEVICE_MAX_ENTRIES))) {␊ |
178 | ␉␉␉free(device);␊ |
179 | ␉␉␉return NULL;␊ |
180 | ␉␉}␊ |
181 | ␉}␊ |
182 | ␊ |
183 | ␉/* FIXME: probably needs bounds checking, as well as error handling in event of malloc failure */␊ |
184 | ␉string->length += device->length;␊ |
185 | ␉string->entries[string->numentries++] = (DevPropDevice*)malloc(sizeof(device));␊ |
186 | ␉string->entries[string->numentries-1] = device;␊ |
187 | ␊ |
188 | ␉return device;␊ |
189 | }␊ |
190 | ␊ |
191 | int devprop_add_value(DevPropDevice *device, char *nm, uint8_t *vl, uint32_t len)␊ |
192 | {␊ |
193 | ␊ |
194 | ␉if(!nm || !vl || !len) {␊ |
195 | ␉␉return 0;␊ |
196 | ␉}␊ |
197 | ␉uint32_t length = ((strlen(nm) * 2) + len + (2 * sizeof(uint32_t)) + 2);␊ |
198 | ␉uint8_t *data = (uint8_t*)malloc(length);␊ |
199 | ␉{␊ |
200 | ␉␉if(!data)␊ |
201 | ␉␉{␊ |
202 | ␉␉␉return 0;␊ |
203 | ␉␉}␊ |
204 | ␊ |
205 | ␉␉memset(data, 0, length);␊ |
206 | ␉␉uint32_t off= 0;␊ |
207 | ␉␉data[off+1] = ((strlen(nm) * 2) + 6) >> 8;␊ |
208 | ␉␉data[off] = ((strlen(nm) * 2) + 6) & 0x00FF;␊ |
209 | ␉␉␊ |
210 | ␉␉off += 4;␊ |
211 | ␉␉uint32_t i=0, l = strlen(nm);␊ |
212 | ␉␉for(i = 0 ; i < l ; i++, off += 2)␊ |
213 | ␉␉{␊ |
214 | ␉␉␉data[off] = *nm++;␊ |
215 | ␉␉}␊ |
216 | ␊ |
217 | ␉␉off += 2;␊ |
218 | ␉␉l = len;␊ |
219 | ␉␉uint32_t *datalength = (uint32_t*)&data[off];␊ |
220 | ␉␉*datalength = (uint32_t)(l + 4);␊ |
221 | ␉␉off += 4;␊ |
222 | ␉␉for(i = 0 ; i < l ; i++, off++)␊ |
223 | ␉␉{␊ |
224 | ␉␉␉data[off] = *vl++;␊ |
225 | ␉␉}␊ |
226 | ␉}␊ |
227 | ␉␊ |
228 | ␉uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));␊ |
229 | ␉␊ |
230 | ␉uint8_t *newdata = (uint8_t*)malloc((length + offset));␊ |
231 | ␉if(!newdata)␊ |
232 | ␉{␊ |
233 | ␉␉return 0;␊ |
234 | ␉}␊ |
235 | ␉if(device->data)␊ |
236 | ␉{␊ |
237 | ␉␉if(offset > 1)␊ |
238 | ␉␉{␊ |
239 | ␉␉␉memcpy(newdata, device->data, offset);␊ |
240 | ␉␉}␊ |
241 | ␉}␊ |
242 | ␊ |
243 | ␉memcpy(newdata + offset, data, length);␊ |
244 | ␉␊ |
245 | ␉device->length += length;␊ |
246 | ␉device->string->length += length;␊ |
247 | ␉device->numentries++;␊ |
248 | ␊ |
249 | ␉if(!device->data)␊ |
250 | ␉{␊ |
251 | ␉␉device->data = (uint8_t*)malloc(sizeof(uint8_t));␊ |
252 | ␉}␊ |
253 | ␉else␊ |
254 | ␉{␊ |
255 | ␉␉free(device->data);␊ |
256 | ␉}␊ |
257 | ␊ |
258 | ␉free(data);␊ |
259 | ␉device->data = newdata;␊ |
260 | ␊ |
261 | ␉return 1;␊ |
262 | }␊ |
263 | ␊ |
264 | char *devprop_generate_string(DevPropString *string)␊ |
265 | {␊ |
266 | ␉char *buffer = (char*)malloc(string->length * 2);␊ |
267 | ␉char *ptr = buffer;␊ |
268 | ␊ |
269 | ␉if(!buffer)␊ |
270 | ␉{␊ |
271 | ␉␉return NULL;␊ |
272 | ␉}␊ |
273 | ␊ |
274 | ␉sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,␊ |
275 | ␉␉␉dp_swap16(string->numentries), string->WHAT3);␊ |
276 | ␉buffer += 24;␊ |
277 | ␉int i = 0, x = 0;␊ |
278 | ␊ |
279 | ␉while(i < string->numentries)␊ |
280 | ␉{␊ |
281 | ␉␉sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),␊ |
282 | ␉␉␉␉dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);␊ |
283 | ␉␉␊ |
284 | ␉␉buffer += 16;␊ |
285 | ␉␉sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,␊ |
286 | ␉␉␉␉string->entries[i]->acpi_dev_path.subtype,␊ |
287 | ␉␉␉␉dp_swap16(string->entries[i]->acpi_dev_path.length),␊ |
288 | ␉␉␉␉string->entries[i]->acpi_dev_path._HID,␊ |
289 | ␉␉␉␉dp_swap32(string->entries[i]->acpi_dev_path._UID));␊ |
290 | ␊ |
291 | ␉␉buffer += 24;␊ |
292 | ␉␉for(x = 0;x < string->entries[i]->num_pci_devpaths; x++)␊ |
293 | ␉␉{␊ |
294 | ␉␉␉sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,␊ |
295 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].subtype,␊ |
296 | ␉␉␉␉␉dp_swap16(string->entries[i]->pci_dev_path[x].length),␊ |
297 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].function,␊ |
298 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].device);␊ |
299 | ␉␉␉buffer += 12;␊ |
300 | ␉␉}␊ |
301 | ␉␉␊ |
302 | ␉␉sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,␊ |
303 | ␉␉␉␉string->entries[i]->path_end.subtype,␊ |
304 | ␉␉␉␉dp_swap16(string->entries[i]->path_end.length));␊ |
305 | ␉␉␊ |
306 | ␉␉buffer += 8;␊ |
307 | ␉␉uint8_t *dataptr = string->entries[i]->data;␊ |
308 | ␉␉for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)␊ |
309 | ␉␉{␊ |
310 | ␉␉␉sprintf(buffer, "%02x", *dataptr++);␊ |
311 | ␉␉␉buffer += 2;␊ |
312 | ␉␉}␊ |
313 | ␉␉i++;␊ |
314 | ␉}␊ |
315 | ␉return ptr;␊ |
316 | }␊ |
317 | ␊ |
318 | void devprop_free_string(DevPropString *string)␊ |
319 | {␊ |
320 | ␊ |
321 | ␉if(!string)␊ |
322 | ␉{␊ |
323 | ␉␉return;␊ |
324 | ␉}␊ |
325 | ␊ |
326 | ␉int i;␊ |
327 | ␉for(i = 0; i < string->numentries; i++)␊ |
328 | ␉{␊ |
329 | ␉␉if(string->entries[i])␊ |
330 | ␉␉{␊ |
331 | ␉␉␉if(string->entries[i]->data)␊ |
332 | ␉␉␉{␊ |
333 | ␉␉␉␉free(string->entries[i]->data);␊ |
334 | ␉␉␉␉string->entries[i]->data = NULL;␊ |
335 | ␉␉␉}␊ |
336 | ␉␉␉free(string->entries[i]);␊ |
337 | ␉␉␉string->entries[i] = NULL;␊ |
338 | ␉␉}␊ |
339 | ␉}␊ |
340 | ␊ |
341 | ␉free(string);␊ |
342 | ␉string = NULL;␊ |
343 | }␊ |
344 | ␊ |
345 | /* ======================================================= */␊ |
346 | ␊ |
347 | ␊ |
348 | /*******************************************************************␊ |
349 | * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *␊ |
350 | * a binary. returns -1 in case of error (i.e. badly formed chars) *␊ |
351 | *******************************************************************/␊ |
352 | int hex2bin(const char *hex, uint8_t *bin, int len)␊ |
353 | {␊ |
354 | ␉char␉*p;␊ |
355 | ␉int␉i;␊ |
356 | ␉char␉buf[3];␊ |
357 | ␊ |
358 | ␉if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2)␊ |
359 | ␉{␊ |
360 | ␉␉printf("[ERROR] bin2hex input error\n");␊ |
361 | ␉␉return -1;␊ |
362 | ␉}␊ |
363 | ␊ |
364 | ␉buf[2] = '\0';␊ |
365 | ␉p = (char *) hex;␊ |
366 | ␊ |
367 | ␉for (i = 0; i < len; i++)␊ |
368 | ␉{␊ |
369 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
370 | ␉␉{␊ |
371 | ␉␉␉printf("[ERROR] bin2hex '%s' syntax error\n", hex);␊ |
372 | ␉␉␉return -2;␊ |
373 | ␉␉}␊ |
374 | ␉␉buf[0] = *p++;␊ |
375 | ␉␉buf[1] = *p++;␊ |
376 | ␉␉bin[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
377 | ␉}␊ |
378 | ␉return 0;␊ |
379 | }␊ |
380 | ␊ |
381 | /* ======================================================= */␊ |
382 | ␊ |
383 | /* a fine place for this code */␊ |
384 | ␊ |
385 | |