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