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 "device_inject.h"␊ |
12 | ␊ |
13 | ␊ |
14 | #ifndef DEBUG_INJECT␊ |
15 | #define DEBUG_INJECT 0␊ |
16 | #endif␊ |
17 | ␊ |
18 | #if DEBUG_INJECT␊ |
19 | #define DBG(x...)␉printf(x)␊ |
20 | #else␊ |
21 | #define DBG(x...)␊ |
22 | #endif␊ |
23 | ␊ |
24 | uint32_t devices_number = 1;␊ |
25 | uint32_t builtin_set = 0;␊ |
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 | //␉printf("efi_inject_get_devprop_string NULL trying stringdata\n");␊ |
38 | ␉return NULL;␊ |
39 | }␊ |
40 | ␊ |
41 | uint32_t ascii_hex_to_int(char *buff) ␊ |
42 | {␊ |
43 | ␉uint32_t␉value = 0, i, digit;␊ |
44 | ␉for(i = 0; i < strlen(buff); i++)␊ |
45 | ␉{␊ |
46 | ␉␉if (buff[i] >= 48 && buff[i] <= 57)␉␉␉// '0' through '9'␊ |
47 | ␉␉␉digit = buff[i] - 48;␉␊ |
48 | ␉␉else if (buff[i] >= 65 && buff[i] <= 70)␉// 'A' through 'F'␊ |
49 | ␉␉␉digit = buff[i] - 55;␊ |
50 | ␉␉else if (buff[i] >= 97 && buff[i] <= 102)␉// 'a' through 'f'␊ |
51 | ␉␉␉digit = buff[i] - 87;␊ |
52 | ␉␉else␊ |
53 | ␉␉␉return value;␊ |
54 | ␉␉␊ |
55 | ␉␉value = digit + 16 * value;␊ |
56 | ␉}␊ |
57 | ␉return␉value;␊ |
58 | }␊ |
59 | ␊ |
60 | void *convertHexStr2Binary(const char *hexStr, int *outLength)␊ |
61 | {␊ |
62 | int len;␊ |
63 | char hexNibble;␊ |
64 | char hexByte[2];␊ |
65 | uint8_t binChar;␊ |
66 | uint8_t *binStr;␊ |
67 | int hexStrIdx, binStrIdx, hexNibbleIdx;␊ |
68 | ␊ |
69 | len = strlen(hexStr);␊ |
70 | if (len > 1)␊ |
71 | {␊ |
72 | // the resulting binary will be the half size of the input hex string␊ |
73 | binStr = malloc(len / 2);␊ |
74 | binStrIdx = 0;␊ |
75 | hexNibbleIdx = 0;␊ |
76 | for (hexStrIdx = 0; hexStrIdx < len; hexStrIdx++)␊ |
77 | {␊ |
78 | hexNibble = hexStr[hexStrIdx];␊ |
79 | ␊ |
80 | // ignore all chars except valid hex numbers␊ |
81 | if (hexNibble >= '0' && hexNibble <= '9'␊ |
82 | || hexNibble >= 'A' && hexNibble <= 'F'␊ |
83 | || hexNibble >= 'a' && hexNibble <= 'f')␊ |
84 | {␊ |
85 | hexByte[hexNibbleIdx++] = hexNibble;␊ |
86 | ␊ |
87 | // found both two nibbles, convert to binary␊ |
88 | if (hexNibbleIdx == 2)␊ |
89 | {␊ |
90 | binChar = 0;␊ |
91 | ␊ |
92 | for (hexNibbleIdx = 0; hexNibbleIdx < sizeof(hexByte); hexNibbleIdx++)␊ |
93 | {␊ |
94 | if (hexNibbleIdx > 0) binChar = binChar << 4;␊ |
95 | ␊ |
96 | if (hexByte[hexNibbleIdx] <= '9') binChar += hexByte[hexNibbleIdx] - '0';␊ |
97 | else if (hexByte[hexNibbleIdx] <= 'F') binChar += hexByte[hexNibbleIdx] - ('A' - 10);␊ |
98 | else if (hexByte[hexNibbleIdx] <= 'f') binChar += hexByte[hexNibbleIdx] - ('a' - 10);␊ |
99 | }␊ |
100 | ␊ |
101 | binStr[binStrIdx++] = binChar;␉␉␉␉␉␉␊ |
102 | hexNibbleIdx = 0;␊ |
103 | }␊ |
104 | }␊ |
105 | }␊ |
106 | *outLength = binStrIdx;␊ |
107 | return binStr;␊ |
108 | }␊ |
109 | else␊ |
110 | {␊ |
111 | *outLength = 0;␊ |
112 | return NULL;␊ |
113 | }␊ |
114 | }␊ |
115 | ␊ |
116 | void setupDeviceProperties(Node *node)␊ |
117 | {␊ |
118 | const char *val;␊ |
119 | uint8_t *binStr;␊ |
120 | int cnt, cnt2;␊ |
121 | ␊ |
122 | static char DEVICE_PROPERTIES_PROP[] = "device-properties";␊ |
123 | ␊ |
124 | /* Generate devprop string.␊ |
125 | */␊ |
126 | uint32_t strlength;␊ |
127 | char *string = efi_inject_get_devprop_string(&strlength);␊ |
128 | ␊ |
129 | /* Use the static "device-properties" boot config key contents if available,␊ |
130 | * otheriwse use the generated one.␊ |
131 | */ ␊ |
132 | if (!getValueForKey(DEVICE_PROPERTIES_PROP, &val, &cnt, &bootInfo->bootConfig) && string)␊ |
133 | {␊ |
134 | val = (const char*)string;␊ |
135 | cnt = strlength * 2;␊ |
136 | } ␊ |
137 | ␊ |
138 | if (cnt > 1)␊ |
139 | {␊ |
140 | binStr = convertHexStr2Binary(val, &cnt2);␊ |
141 | if (cnt2 > 0) DT__AddProperty(node, DEVICE_PROPERTIES_PROP, cnt2, binStr);␊ |
142 | }␊ |
143 | }␊ |
144 | ␊ |
145 | uint16_t dp_swap16(uint16_t toswap)␊ |
146 | {␊ |
147 | ␉return (((toswap & 0x00FF) << 8) | ((toswap & 0xFF00) >> 8));␊ |
148 | }␊ |
149 | ␊ |
150 | uint32_t dp_swap32(uint32_t toswap)␊ |
151 | {␊ |
152 | ␉return ((toswap & 0x000000FF) << 24) |␊ |
153 | ␉␉␉((toswap & 0x0000FF00) << 8 ) |␊ |
154 | ␉␉␉((toswap & 0x00FF0000) >> 8 ) |␊ |
155 | ␉␉␉((toswap & 0xFF000000) >> 24);␊ |
156 | }␉␊ |
157 | ␊ |
158 | struct DevPropString *devprop_create_string(void)␊ |
159 | {␊ |
160 | ␉string = (struct DevPropString*)malloc(sizeof(struct DevPropString));␊ |
161 | ␉␊ |
162 | ␉if(string == NULL)␊ |
163 | ␉␉return NULL;␊ |
164 | ␉␊ |
165 | ␉memset(string, 0, sizeof(struct DevPropString));␊ |
166 | ␉string->length = 12;␊ |
167 | ␉string->WHAT2 = 0x01000000;␊ |
168 | ␉return string;␊ |
169 | }␊ |
170 | ␊ |
171 | struct DevPropDevice *devprop_add_device(struct DevPropString *string, char *path)␊ |
172 | {␊ |
173 | ␉uint32_t PciRootID = 0;␊ |
174 | ␉const char *val;␊ |
175 | ␉int len;␊ |
176 | ␊ |
177 | ␉struct DevPropDevice *device = (struct DevPropDevice*)malloc(sizeof(struct DevPropDevice));␊ |
178 | ␉if(!device || !string || !path) {␊ |
179 | ␉␉if(device)␊ |
180 | ␉␉␉free(device);␊ |
181 | ␉␉return NULL;␊ |
182 | ␉}␊ |
183 | ␊ |
184 | ␉const char pciroot_string[]␉␉= "PciRoot(0x";␊ |
185 | ␉const char pci_device_string[]␉= "Pci(0x";␊ |
186 | ␊ |
187 | ␉if (getValueForKey("PciRoot", &val, &len, &bootInfo->bootConfig))␊ |
188 | ␉ PciRootID = atoi(val);␊ |
189 | ␉␊ |
190 | ␉if(strncmp(path, pciroot_string, strlen(pciroot_string)))␊ |
191 | ␉{␊ |
192 | ␉␉printf("ERROR parsing device path\n");␊ |
193 | ␉␉return NULL;␊ |
194 | ␉}␊ |
195 | ␉␊ |
196 | ␉memset(device, 0, sizeof(struct DevPropDevice));␊ |
197 | ␉␊ |
198 | ␉device->acpi_dev_path._UID = PciRootID;␊ |
199 | ␉␊ |
200 | ␉int numpaths = 0;␊ |
201 | ␉int␉␉x, curr = 0;␊ |
202 | ␉char␉buff[] = "00";␊ |
203 | ␊ |
204 | ␉for(x = 0; x < strlen(path); x++)␊ |
205 | ␉{␊ |
206 | ␉␉if(!strncmp(&path[x], pci_device_string, strlen(pci_device_string)))␊ |
207 | ␉␉{␊ |
208 | ␉␉␉x+=strlen(pci_device_string);␊ |
209 | ␉␉␉curr=x;␊ |
210 | ␉␉␉while(path[++x] != ',');␊ |
211 | ␉␉␉if(x-curr == 2)␊ |
212 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
213 | ␉␉␉else if(x-curr == 1)␊ |
214 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
215 | ␉␉␉else ␊ |
216 | ␉␉␉{␊ |
217 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
218 | ␉␉␉␉numpaths = 0;␊ |
219 | ␉␉␉␉break;␊ |
220 | ␉␉␉}␊ |
221 | ␉␉␉device->pci_dev_path[numpaths].device =␉ascii_hex_to_int(buff);␊ |
222 | ␉␉␉␊ |
223 | ␉␉␉x += 3; // 0x␊ |
224 | ␉␉␉curr = x;␊ |
225 | ␉␉␉while(path[++x] != ')');␊ |
226 | ␉␉␉if(x-curr == 2)␊ |
227 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
228 | ␉␉␉else if(x-curr == 1)␊ |
229 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
230 | ␉␉␉else␊ |
231 | ␉␉␉{␊ |
232 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
233 | ␉␉␉␉numpaths = 0;␊ |
234 | ␉␉␉␉break;␊ |
235 | ␉␉␉}␊ |
236 | ␉␉␉device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path␊ |
237 | ␉␉␉␊ |
238 | ␉␉␉numpaths++;␊ |
239 | ␉␉}␊ |
240 | ␉}␊ |
241 | ␉␊ |
242 | ␉if(!numpaths)␊ |
243 | ␉␉return NULL;␊ |
244 | ␉␊ |
245 | ␉device->numentries = 0x00;␊ |
246 | ␉␊ |
247 | ␉device->acpi_dev_path.length = 0x0c;␊ |
248 | ␉device->acpi_dev_path.type = 0x02;␊ |
249 | ␉device->acpi_dev_path.subtype = 0x01;␊ |
250 | ␉device->acpi_dev_path._HID = 0xd041030a;␊ |
251 | ␉␊ |
252 | ␉device->num_pci_devpaths = numpaths;␊ |
253 | ␉device->length = 24 + (6*numpaths);␊ |
254 | ␉␊ |
255 | ␉int␉␉i; ␊ |
256 | ␉␊ |
257 | ␉for(i = 0; i < numpaths; i++)␊ |
258 | ␉{␊ |
259 | ␉␉device->pci_dev_path[i].length = 0x06;␊ |
260 | ␉␉device->pci_dev_path[i].type = 0x01;␊ |
261 | ␉␉device->pci_dev_path[i].subtype = 0x01;␊ |
262 | ␉}␊ |
263 | ␉␊ |
264 | ␉device->path_end.length = 0x04;␊ |
265 | ␉device->path_end.type = 0x7f;␊ |
266 | ␉device->path_end.subtype = 0xff;␊ |
267 | ␉␊ |
268 | ␉device->string = string;␊ |
269 | ␉device->data = NULL;␊ |
270 | ␉string->length += device->length;␊ |
271 | ␉␊ |
272 | ␉if(!string->entries)␊ |
273 | ␉␉if((string->entries = (struct DevPropDevice**)malloc(sizeof(device)))== NULL)␊ |
274 | ␉␉␉return 0;␊ |
275 | ␉␊ |
276 | ␉string->entries[string->numentries++] = (struct DevPropDevice*)malloc(sizeof(device));␊ |
277 | ␉string->entries[string->numentries-1] = device;␊ |
278 | ␉␊ |
279 | ␉return device;␊ |
280 | }␊ |
281 | ␊ |
282 | int devprop_add_value(struct DevPropDevice *device, char *nm, uint8_t *vl, uint32_t len)␊ |
283 | {␊ |
284 | ␉␊ |
285 | ␉if(!nm || !vl || !len)␊ |
286 | ␉␉return 0;␊ |
287 | ␉␊ |
288 | ␉uint32_t length = ((strlen(nm) * 2) + len + (2 * sizeof(uint32_t)) + 2);␊ |
289 | ␉uint8_t *data = (uint8_t*)malloc(length);␊ |
290 | ␉{␊ |
291 | ␉␉if(!data)␊ |
292 | ␉␉␉return 0;␊ |
293 | ␉␉␊ |
294 | ␉␉memset(data, 0, length);␊ |
295 | ␉␉uint32_t off= 0;␊ |
296 | ␉␉data[off+1] = ((strlen(nm) * 2) + 6) >> 8;␊ |
297 | ␉␉data[off] = ((strlen(nm) * 2) + 6) & 0x00FF;␊ |
298 | ␉␉␊ |
299 | ␉␉off += 4;␊ |
300 | ␉␉uint32_t i=0, l = strlen(nm);␊ |
301 | ␉␉for(i = 0 ; i < l ; i++, off += 2)␊ |
302 | ␉␉{␊ |
303 | ␉␉␉data[off] = *nm++;␊ |
304 | ␉␉}␊ |
305 | ␉␉␊ |
306 | ␉␉off += 2;␊ |
307 | ␉␉l = len;␊ |
308 | ␉␉uint32_t *datalength = (uint32_t*)&data[off];␊ |
309 | ␉␉*datalength = l + 4;␊ |
310 | ␉␉off += 4;␊ |
311 | ␉␉for(i = 0 ; i < l ; i++, off++)␊ |
312 | ␉␉{␊ |
313 | ␉␉␉data[off] = *vl++;␊ |
314 | ␉␉}␊ |
315 | ␉}␉␊ |
316 | ␉␊ |
317 | ␉uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));␊ |
318 | ␉␊ |
319 | ␉uint8_t *newdata = (uint8_t*)malloc((length + offset));␊ |
320 | ␉if(!newdata)␊ |
321 | ␉␉return 0;␊ |
322 | ␉if(device->data)␊ |
323 | ␉␉if(offset > 1)␊ |
324 | ␉␉␉memcpy(newdata, device->data, offset);␊ |
325 | ␊ |
326 | ␉memcpy(newdata + offset, data, length);␊ |
327 | ␉␊ |
328 | ␉device->length += length;␊ |
329 | ␉device->string->length += length;␊ |
330 | ␉device->numentries++;␊ |
331 | ␉␊ |
332 | ␉if(!device->data)␊ |
333 | ␉␉device->data = (uint8_t*)malloc(sizeof(uint8_t));␊ |
334 | ␉else␊ |
335 | ␉␉free(device->data);␊ |
336 | ␉␊ |
337 | ␉free(data);␊ |
338 | ␉device->data = newdata;␊ |
339 | ␉␊ |
340 | ␉return 1;␊ |
341 | }␊ |
342 | ␊ |
343 | char *devprop_generate_string(struct DevPropString *string)␊ |
344 | {␊ |
345 | ␉char *buffer = (char*)malloc(string->length * 2);␊ |
346 | ␉char *ptr = buffer;␊ |
347 | ␉␊ |
348 | ␉if(!buffer)␊ |
349 | ␉␉return NULL;␊ |
350 | ␊ |
351 | ␉sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,␊ |
352 | ␉␉␉dp_swap16(string->numentries), string->WHAT3);␊ |
353 | ␉buffer += 24;␊ |
354 | ␉int i = 0, x = 0;␊ |
355 | ␉␊ |
356 | ␉while(i < string->numentries)␊ |
357 | ␉{␊ |
358 | ␉␉sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),␊ |
359 | ␉␉␉␉dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);␊ |
360 | ␉␉␊ |
361 | ␉␉buffer += 16;␊ |
362 | ␉␉sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,␊ |
363 | ␉␉␉␉string->entries[i]->acpi_dev_path.subtype,␊ |
364 | ␉␉␉␉dp_swap16(string->entries[i]->acpi_dev_path.length),␊ |
365 | ␉␉␉␉string->entries[i]->acpi_dev_path._HID,␊ |
366 | ␉␉␉␉dp_swap32(string->entries[i]->acpi_dev_path._UID));␊ |
367 | ␊ |
368 | ␉␉buffer += 24;␊ |
369 | ␉␉for(x=0;x < string->entries[i]->num_pci_devpaths; x++)␊ |
370 | ␉␉{␊ |
371 | ␉␉␉sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,␊ |
372 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].subtype,␊ |
373 | ␉␉␉␉␉dp_swap16(string->entries[i]->pci_dev_path[x].length),␊ |
374 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].function,␊ |
375 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].device);␊ |
376 | ␉␉␉buffer += 12;␊ |
377 | ␉␉}␊ |
378 | ␉␉␊ |
379 | ␉␉sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,␊ |
380 | ␉␉␉␉string->entries[i]->path_end.subtype,␊ |
381 | ␉␉␉␉dp_swap16(string->entries[i]->path_end.length));␊ |
382 | ␉␉␊ |
383 | ␉␉buffer += 8;␊ |
384 | ␉␉uint8_t *dataptr = string->entries[i]->data;␊ |
385 | ␉␉for(x = 0; x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)␊ |
386 | ␉␉{␊ |
387 | ␉␉␉sprintf(buffer, "%02x", *dataptr++);␊ |
388 | ␉␉␉buffer += 2;␊ |
389 | ␉␉}␊ |
390 | ␉␉i++;␊ |
391 | ␉}␊ |
392 | ␉return ptr;␊ |
393 | }␊ |
394 | ␊ |
395 | void devprop_free_string(struct DevPropString *string)␊ |
396 | {␊ |
397 | ␉if(!string)␊ |
398 | ␉␉return;␊ |
399 | ␉␊ |
400 | ␉int i;␊ |
401 | ␉for(i = 0; i < string->numentries; i++)␊ |
402 | ␉{␊ |
403 | ␉␉if(string->entries[i])␊ |
404 | ␉␉{␊ |
405 | ␉␉␉if(string->entries[i]->data)␊ |
406 | ␉␉␉{␊ |
407 | ␉␉␉␉free(string->entries[i]->data);␊ |
408 | ␉␉␉␉string->entries[i]->data = NULL;␊ |
409 | ␉␉␉}␊ |
410 | ␉␉␉free(string->entries[i]);␊ |
411 | ␉␉␉string->entries[i] = NULL;␊ |
412 | ␉␉}␊ |
413 | ␉}␊ |
414 | ␉␊ |
415 | ␉free(string);␊ |
416 | ␉string = NULL;␊ |
417 | }␊ |
418 | ␊ |
419 | /* a fine place for this code */␊ |
420 | ␊ |
421 | int devprop_add_network_template(struct DevPropDevice *device, uint16_t vendor_id)␊ |
422 | {␊ |
423 | ␉if(!device)␊ |
424 | ␉␉return 0;␊ |
425 | ␉uint8_t builtin = 0x0;␊ |
426 | ␉if((vendor_id != 0x168c) && (builtin_set == 0)) ␊ |
427 | ␉{␊ |
428 | ␉␉builtin_set = 1;␊ |
429 | ␉␉builtin = 0x01;␊ |
430 | ␉}␊ |
431 | ␉if(!devprop_add_value(device, "built-in", (uint8_t*)&builtin, 1))␊ |
432 | ␉␉return 0;␊ |
433 | ␉devices_number++;␊ |
434 | ␉return 1;␊ |
435 | }␊ |
436 | ␊ |
437 | void set_eth_builtin(pci_dt_t *eth_dev)␊ |
438 | {␊ |
439 | ␉char *devicepath = get_pci_dev_path(eth_dev);␊ |
440 | ␉struct DevPropDevice *device = (struct DevPropDevice*)malloc(sizeof(struct DevPropDevice));␊ |
441 | ␊ |
442 | ␉verbose("LAN Controller [%04x:%04x] :: %s\n", eth_dev->vendor_id, eth_dev->device_id, devicepath);␊ |
443 | ␊ |
444 | ␉if (!string)␊ |
445 | ␉␉string = devprop_create_string();␊ |
446 | ␊ |
447 | ␉device = devprop_add_device(string, devicepath);␊ |
448 | ␉if(device)␊ |
449 | ␉{␊ |
450 | ␉␉verbose("Setting up lan keys\n");␊ |
451 | ␉␉devprop_add_network_template(device, eth_dev->vendor_id);␊ |
452 | ␉␉stringdata = (uint8_t*)malloc(sizeof(uint8_t) * string->length);␊ |
453 | ␉␉if(stringdata)␊ |
454 | ␉␉{␊ |
455 | ␉␉␉memcpy(stringdata, (uint8_t*)devprop_generate_string(string), string->length);␊ |
456 | ␉␉␉stringlength = string->length;␊ |
457 | ␉␉}␊ |
458 | ␉}␊ |
459 | }␊ |
460 | |