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 | uint32_t builtin_set = 0;␊ |
28 | struct DevPropString *string = 0;␊ |
29 | uint8_t *stringdata = 0;␊ |
30 | uint32_t stringlength = 0;␊ |
31 | ␊ |
32 | char *efi_inject_get_devprop_string(uint32_t *len)␊ |
33 | {␊ |
34 | ␉if(string) {␊ |
35 | ␉␉*len = string->length;␊ |
36 | ␉␉return devprop_generate_string(string);␊ |
37 | ␉}␊ |
38 | ␉verbose("efi_inject_get_devprop_string NULL trying stringdata\n");␊ |
39 | ␉return NULL;␊ |
40 | }␊ |
41 | ␊ |
42 | void setupDeviceProperties(Node *node)␊ |
43 | {␊ |
44 | const char *val;␊ |
45 | uint8_t *binStr;␊ |
46 | int cnt, cnt2;␊ |
47 | ␊ |
48 | static char DEVICE_PROPERTIES_PROP[] = "device-properties";␊ |
49 | ␊ |
50 | /* Generate devprop string.␊ |
51 | */␊ |
52 | uint32_t strlength;␊ |
53 | char *string = efi_inject_get_devprop_string(&strlength);␊ |
54 | ␊ |
55 | /* Use the static "device-properties" boot config key contents if available,␊ |
56 | * otheriwse use the generated one.␊ |
57 | */ ␊ |
58 | ␉if (!getValueForKey(kDeviceProperties, &val, &cnt, &bootInfo->chameleonConfig) && string) {␊ |
59 | ␉␉val = (const char*)string;␊ |
60 | ␉␉cnt = strlength * 2;␊ |
61 | ␉}␊ |
62 | ␊ |
63 | ␉if (cnt > 1) {␊ |
64 | ␉␉binStr = convertHexStr2Binary(val, &cnt2);␊ |
65 | ␉␉if (cnt2 > 0) {␊ |
66 | ␉␉␉DT__AddProperty(node, DEVICE_PROPERTIES_PROP, cnt2, binStr);␊ |
67 | ␉␉}␊ |
68 | ␉}␊ |
69 | }␊ |
70 | ␊ |
71 | struct DevPropString *devprop_create_string(void)␊ |
72 | {␊ |
73 | ␉string = (struct DevPropString*)malloc(sizeof(struct DevPropString));␊ |
74 | ␊ |
75 | ␉if(string == NULL) {␊ |
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 = NULL;␊ |
88 | ␉static const char␉␉pciroot_string[] = "PciRoot(0x";␊ |
89 | ␉static const char␉␉pci_device_string[] = "Pci(0x";␊ |
90 | ␊ |
91 | ␉if (string == NULL || path == NULL) {␊ |
92 | ␉␉printf("ERROR null device path\n");␊ |
93 | ␉␉return NULL;␊ |
94 | ␉}␊ |
95 | ␊ |
96 | ␉if (strncmp(path, pciroot_string, strlen(pciroot_string))) {␊ |
97 | ␉␉printf("ERROR parsing device path\n");␊ |
98 | ␉␉return NULL;␊ |
99 | ␉}␊ |
100 | ␊ |
101 | ␉if (!(device = malloc(sizeof(struct DevPropDevice)))) {␊ |
102 | ␉␉printf("ERROR malloc failed\n");␊ |
103 | ␉␉return NULL;␊ |
104 | ␉}␊ |
105 | ␊ |
106 | ␉memset(device, 0, sizeof(struct DevPropDevice));␊ |
107 | ␉device->acpi_dev_path._UID = getPciRootUID();␊ |
108 | ␊ |
109 | ␉int numpaths = 0;␊ |
110 | ␉int␉␉x, curr = 0;␊ |
111 | ␉char␉buff[] = "00";␊ |
112 | ␊ |
113 | ␉for (x = 0; x < strlen(path); x++) {␊ |
114 | ␉␉if (!strncmp(&path[x], pci_device_string, strlen(pci_device_string))) {␊ |
115 | ␉␉␉x+=strlen(pci_device_string);␊ |
116 | ␉␉␉curr=x;␊ |
117 | ␉␉␉while(path[++x] != ',');␊ |
118 | ␉␉␉if(x-curr == 2) {␊ |
119 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
120 | ␉␉␉} else if(x-curr == 1) {␊ |
121 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
122 | ␉␉␉} else {␊ |
123 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
124 | ␉␉␉␉numpaths = 0;␊ |
125 | ␉␉␉␉break;␊ |
126 | ␉␉␉}␊ |
127 | ␉␉␉device->pci_dev_path[numpaths].device =␉ascii_hex_to_int(buff);␊ |
128 | ␊ |
129 | ␉␉␉x += 3; // 0x␊ |
130 | ␉␉␉curr = x;␊ |
131 | ␉␉␉while(path[++x] != ')');␊ |
132 | ␉␉␉if(x-curr == 2) {␊ |
133 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
134 | ␉␉␉} else if(x-curr == 1) {␊ |
135 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
136 | ␉␉␉} else {␊ |
137 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
138 | ␉␉␉␉numpaths = 0;␊ |
139 | ␉␉␉␉break;␊ |
140 | ␉␉␉}␊ |
141 | ␉␉␉device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path␊ |
142 | ␊ |
143 | ␉␉␉numpaths++;␊ |
144 | ␉␉}␊ |
145 | ␉}␊ |
146 | ␉␊ |
147 | ␉if(!numpaths) {␊ |
148 | ␉␉free(device);␊ |
149 | ␉␉return NULL;␊ |
150 | ␉}␊ |
151 | ␉␊ |
152 | ␉device->numentries = 0x00;␊ |
153 | ␉␊ |
154 | ␉device->acpi_dev_path.length = 0x0c;␊ |
155 | ␉device->acpi_dev_path.type = 0x02;␊ |
156 | ␉device->acpi_dev_path.subtype = 0x01;␊ |
157 | ␉device->acpi_dev_path._HID = 0xd041030a;␊ |
158 | ␉␊ |
159 | ␉device->num_pci_devpaths = numpaths;␊ |
160 | ␉device->length = 24 + (6*numpaths);␊ |
161 | ␉␊ |
162 | ␉int␉␉i;␊ |
163 | ␉␊ |
164 | ␉for(i = 0; i < numpaths; i++)␊ |
165 | ␉{␊ |
166 | ␉␉device->pci_dev_path[i].length = 0x06;␊ |
167 | ␉␉device->pci_dev_path[i].type = 0x01;␊ |
168 | ␉␉device->pci_dev_path[i].subtype = 0x01;␊ |
169 | ␉}␊ |
170 | ␉␊ |
171 | ␉device->path_end.length = 0x04;␊ |
172 | ␉device->path_end.type = 0x7f;␊ |
173 | ␉device->path_end.subtype = 0xff;␊ |
174 | ␉␊ |
175 | ␉device->string = string;␊ |
176 | ␉device->data = NULL;␊ |
177 | ␊ |
178 | ␉if(!string->entries) {␊ |
179 | ␉␉if (!(string->entries = (struct DevPropDevice**) malloc(sizeof(device) * DEV_PROP_DEVICE_MAX_ENTRIES))) {␊ |
180 | ␉␉␉free(device);␊ |
181 | ␉␉␉return NULL;␊ |
182 | ␉␉}␊ |
183 | ␉}␊ |
184 | ␊ |
185 | ␉/* FIXME: probably needs bounds checking, as well as error handling in event of malloc failure */␊ |
186 | ␉string->length += device->length;␊ |
187 | ␉string->entries[string->numentries++] = (struct DevPropDevice*)malloc(sizeof(device));␊ |
188 | ␉string->entries[string->numentries-1] = device;␊ |
189 | ␊ |
190 | ␉return device;␊ |
191 | }␊ |
192 | ␊ |
193 | int devprop_add_value(struct DevPropDevice *device, char *nm, uint8_t *vl, uint32_t len)␊ |
194 | {␊ |
195 | ␊ |
196 | ␉if(!nm || !vl || !len) {␊ |
197 | ␉␉return 0;␊ |
198 | ␉}␊ |
199 | ␉uint32_t length = ((strlen(nm) * 2) + len + (2 * sizeof(uint32_t)) + 2);␊ |
200 | ␉uint8_t *data = (uint8_t*)malloc(length);␊ |
201 | ␉{␊ |
202 | ␉␉if(!data)␊ |
203 | ␉␉{␊ |
204 | ␉␉␉return 0;␊ |
205 | ␉␉}␊ |
206 | ␊ |
207 | ␉␉memset(data, 0, length);␊ |
208 | ␉␉uint32_t off= 0;␊ |
209 | ␉␉data[off+1] = ((strlen(nm) * 2) + 6) >> 8;␊ |
210 | ␉␉data[off] = ((strlen(nm) * 2) + 6) & 0x00FF;␊ |
211 | ␉␉␊ |
212 | ␉␉off += 4;␊ |
213 | ␉␉uint32_t i=0, l = strlen(nm);␊ |
214 | ␉␉for(i = 0 ; i < l ; i++, off += 2)␊ |
215 | ␉␉{␊ |
216 | ␉␉␉data[off] = *nm++;␊ |
217 | ␉␉}␊ |
218 | ␊ |
219 | ␉␉off += 2;␊ |
220 | ␉␉l = len;␊ |
221 | ␉␉uint32_t *datalength = (uint32_t*)&data[off];␊ |
222 | ␉␉*datalength = (uint32_t)(l + 4);␊ |
223 | ␉␉off += 4;␊ |
224 | ␉␉for(i = 0 ; i < l ; i++, off++)␊ |
225 | ␉␉{␊ |
226 | ␉␉␉data[off] = *vl++;␊ |
227 | ␉␉}␊ |
228 | ␉}␊ |
229 | ␉␊ |
230 | ␉uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));␊ |
231 | ␉␊ |
232 | ␉uint8_t *newdata = (uint8_t*)malloc((length + offset));␊ |
233 | ␉if(!newdata)␊ |
234 | ␉{␊ |
235 | ␉␉return 0;␊ |
236 | ␉}␊ |
237 | ␉if(device->data)␊ |
238 | ␉{␊ |
239 | ␉␉if(offset > 1)␊ |
240 | ␉␉{␊ |
241 | ␉␉␉memcpy(newdata, device->data, offset);␊ |
242 | ␉␉}␊ |
243 | ␉}␊ |
244 | ␊ |
245 | ␉memcpy(newdata + offset, data, length);␊ |
246 | ␉␊ |
247 | ␉device->length += length;␊ |
248 | ␉device->string->length += length;␊ |
249 | ␉device->numentries++;␊ |
250 | ␊ |
251 | ␉if(!device->data)␊ |
252 | ␉{␊ |
253 | ␉␉device->data = (uint8_t*)malloc(sizeof(uint8_t));␊ |
254 | ␉}␊ |
255 | ␉else␊ |
256 | ␉{␊ |
257 | ␉␉free(device->data);␊ |
258 | ␉}␊ |
259 | ␊ |
260 | ␉free(data);␊ |
261 | ␉device->data = newdata;␊ |
262 | ␊ |
263 | ␉return 1;␊ |
264 | }␊ |
265 | ␊ |
266 | char *devprop_generate_string(struct DevPropString *string)␊ |
267 | {␊ |
268 | ␉char *buffer = (char*)malloc(string->length * 2);␊ |
269 | ␉char *ptr = buffer;␊ |
270 | ␊ |
271 | ␉if(!buffer)␊ |
272 | ␉{␊ |
273 | ␉␉return NULL;␊ |
274 | ␉}␊ |
275 | ␊ |
276 | ␉sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,␊ |
277 | ␉␉␉dp_swap16(string->numentries), string->WHAT3);␊ |
278 | ␉buffer += 24;␊ |
279 | ␉int i = 0, x = 0;␊ |
280 | ␊ |
281 | ␉while(i < string->numentries)␊ |
282 | ␉{␊ |
283 | ␉␉sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),␊ |
284 | ␉␉␉␉dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);␊ |
285 | ␉␉␊ |
286 | ␉␉buffer += 16;␊ |
287 | ␉␉sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,␊ |
288 | ␉␉␉␉string->entries[i]->acpi_dev_path.subtype,␊ |
289 | ␉␉␉␉dp_swap16(string->entries[i]->acpi_dev_path.length),␊ |
290 | ␉␉␉␉string->entries[i]->acpi_dev_path._HID,␊ |
291 | ␉␉␉␉dp_swap32(string->entries[i]->acpi_dev_path._UID));␊ |
292 | ␊ |
293 | ␉␉buffer += 24;␊ |
294 | ␉␉for(x = 0;x < string->entries[i]->num_pci_devpaths; x++)␊ |
295 | ␉␉{␊ |
296 | ␉␉␉sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,␊ |
297 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].subtype,␊ |
298 | ␉␉␉␉␉dp_swap16(string->entries[i]->pci_dev_path[x].length),␊ |
299 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].function,␊ |
300 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].device);␊ |
301 | ␉␉␉buffer += 12;␊ |
302 | ␉␉}␊ |
303 | ␉␉␊ |
304 | ␉␉sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,␊ |
305 | ␉␉␉␉string->entries[i]->path_end.subtype,␊ |
306 | ␉␉␉␉dp_swap16(string->entries[i]->path_end.length));␊ |
307 | ␉␉␊ |
308 | ␉␉buffer += 8;␊ |
309 | ␉␉uint8_t *dataptr = string->entries[i]->data;␊ |
310 | ␉␉for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)␊ |
311 | ␉␉{␊ |
312 | ␉␉␉sprintf(buffer, "%02x", *dataptr++);␊ |
313 | ␉␉␉buffer += 2;␊ |
314 | ␉␉}␊ |
315 | ␉␉i++;␊ |
316 | ␉}␊ |
317 | ␉return ptr;␊ |
318 | }␊ |
319 | ␊ |
320 | void devprop_free_string(struct DevPropString *string)␊ |
321 | {␊ |
322 | ␊ |
323 | ␉if(!string)␊ |
324 | ␉{␊ |
325 | ␉␉return;␊ |
326 | ␉}␊ |
327 | ␊ |
328 | ␉int i;␊ |
329 | ␉for(i = 0; i < string->numentries; i++)␊ |
330 | ␉{␊ |
331 | ␉␉if(string->entries[i])␊ |
332 | ␉␉{␊ |
333 | ␉␉␉if(string->entries[i]->data)␊ |
334 | ␉␉␉{␊ |
335 | ␉␉␉␉free(string->entries[i]->data);␊ |
336 | ␉␉␉␉string->entries[i]->data = NULL;␊ |
337 | ␉␉␉}␊ |
338 | ␉␉␉free(string->entries[i]);␊ |
339 | ␉␉␉string->entries[i] = NULL;␊ |
340 | ␉␉}␊ |
341 | ␉}␊ |
342 | ␊ |
343 | ␉free(string);␊ |
344 | ␉string = NULL;␊ |
345 | }␊ |
346 | ␊ |
347 | /* ======================================================= */␊ |
348 | ␊ |
349 | ␊ |
350 | /*******************************************************************␊ |
351 | * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *␊ |
352 | * a binary. returns -1 in case of error (i.e. badly formed chars) *␊ |
353 | *******************************************************************/␊ |
354 | int hex2bin(const char *hex, uint8_t *bin, int len)␊ |
355 | {␊ |
356 | ␉char␉*p;␊ |
357 | ␉int␉i;␊ |
358 | ␉char␉buf[3];␊ |
359 | ␊ |
360 | ␉if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2)␊ |
361 | ␉{␊ |
362 | ␉␉printf("[ERROR] bin2hex input error\n");␊ |
363 | ␉␉return -1;␊ |
364 | ␉}␊ |
365 | ␊ |
366 | ␉buf[2] = '\0';␊ |
367 | ␉p = (char *) hex;␊ |
368 | ␊ |
369 | ␉for (i = 0; i < len; i++)␊ |
370 | ␉{␊ |
371 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))␊ |
372 | ␉␉{␊ |
373 | ␉␉␉printf("[ERROR] bin2hex '%s' syntax error\n", hex);␊ |
374 | ␉␉␉return -2;␊ |
375 | ␉␉}␊ |
376 | ␉␉buf[0] = *p++;␊ |
377 | ␉␉buf[1] = *p++;␊ |
378 | ␉␉bin[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
379 | ␉}␊ |
380 | ␉return 0;␊ |
381 | }␊ |
382 | ␊ |
383 | /* ======================================================= */␊ |
384 | ␊ |
385 | /* a fine place for this code */␊ |
386 | ␊ |
387 | int devprop_add_network_template(struct DevPropDevice *device, uint16_t vendor_id)␊ |
388 | {␊ |
389 | ␉if(!device)␊ |
390 | ␉␉return 0;␊ |
391 | ␉uint8_t builtin = 0x0;␊ |
392 | ␉if((vendor_id != 0x168c) && (builtin_set == 0)) ␊ |
393 | ␉{␊ |
394 | ␉␉builtin_set = 1;␊ |
395 | ␉␉builtin = 0x01;␊ |
396 | ␉}␊ |
397 | ␉if(!devprop_add_value(device, "built-in", (uint8_t*)&builtin, 1))␊ |
398 | ␉␉return 0;␊ |
399 | ␉devices_number++;␊ |
400 | ␉return 1;␊ |
401 | }␊ |
402 | ␊ |
403 | void set_eth_builtin(pci_dt_t *eth_dev)␊ |
404 | {␊ |
405 | ␉char *devicepath = get_pci_dev_path(eth_dev);␊ |
406 | ␉struct DevPropDevice *device = NULL;␊ |
407 | ␊ |
408 | ␉verbose("LAN Controller [%04x:%04x] :: %s\n", eth_dev->vendor_id, eth_dev->device_id, devicepath);␊ |
409 | ␊ |
410 | ␉if(!string) {␊ |
411 | ␉␉string = devprop_create_string();␊ |
412 | ␉}␊ |
413 | ␊ |
414 | ␉device = devprop_add_device(string, devicepath);␊ |
415 | ␉if(device) {␊ |
416 | ␉␉verbose("Setting up lan keys\n");␊ |
417 | ␉␉devprop_add_network_template(device, eth_dev->vendor_id);␊ |
418 | ␉␉stringdata = (uint8_t*)malloc(sizeof(uint8_t) * string->length);␊ |
419 | ␉␉if(stringdata) {␊ |
420 | ␉␉␉memcpy(stringdata, (uint8_t*)devprop_generate_string(string), string->length);␊ |
421 | ␉␉␉stringlength = string->length;␊ |
422 | ␉␉}␊ |
423 | ␉}␊ |
424 | }␊ |
425 | |