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 | 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 | 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 | DevPropDevice *devprop_add_device(DevPropString *string, char *path)␊ |
86 | {␊ |
87 | ␉DevPropDevice␉*device = NULL;␊ |
88 | ␉const char␉␉pciroot_string[] = "PciRoot(0x";␊ |
89 | ␉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 | ␉if (!(device = malloc(sizeof(DevPropDevice)))) {␊ |
101 | ␉␉printf("ERROR malloc failed\n");␊ |
102 | ␉␉return NULL;␊ |
103 | ␉}␊ |
104 | ␊ |
105 | ␉memset(device, 0, sizeof(DevPropDevice));␊ |
106 | ␉device->acpi_dev_path._UID = getPciRootUID();␊ |
107 | ␊ |
108 | ␉int numpaths = 0;␊ |
109 | ␉int␉␉x, curr = 0;␊ |
110 | ␉char␉buff[] = "00";␊ |
111 | ␊ |
112 | ␉for (x = 0; x < strlen(path); x++) {␊ |
113 | ␉␉if (!strncmp(&path[x], pci_device_string, strlen(pci_device_string))) {␊ |
114 | ␉␉␉x+=strlen(pci_device_string);␊ |
115 | ␉␉␉curr=x;␊ |
116 | ␉␉␉while(path[++x] != ',');␊ |
117 | ␉␉␉if(x-curr == 2) {␊ |
118 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
119 | ␉␉␉} else if(x-curr == 1) {␊ |
120 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
121 | ␉␉␉} else {␊ |
122 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
123 | ␉␉␉␉numpaths = 0;␊ |
124 | ␉␉␉␉break;␊ |
125 | ␉␉␉}␊ |
126 | ␉␉␉device->pci_dev_path[numpaths].device =␉ascii_hex_to_int(buff);␊ |
127 | ␊ |
128 | ␉␉␉x += 3; // 0x␊ |
129 | ␉␉␉curr = x;␊ |
130 | ␉␉␉while(path[++x] != ')');␊ |
131 | ␉␉␉if(x-curr == 2) {␊ |
132 | ␉␉␉␉sprintf(buff, "%c%c", path[curr], path[curr+1]);␊ |
133 | ␉␉␉} else if(x-curr == 1) {␊ |
134 | ␉␉␉␉sprintf(buff, "%c", path[curr]);␊ |
135 | ␉␉␉} else {␊ |
136 | ␉␉␉␉printf("ERROR parsing device path\n");␊ |
137 | ␉␉␉␉numpaths = 0;␊ |
138 | ␉␉␉␉break;␊ |
139 | ␉␉␉}␊ |
140 | ␉␉␉device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path␊ |
141 | ␊ |
142 | ␉␉␉numpaths++;␊ |
143 | ␉␉}␊ |
144 | ␉}␊ |
145 | ␉␊ |
146 | ␉if(!numpaths) {␊ |
147 | ␉␉free(device);␊ |
148 | ␉␉return NULL;␊ |
149 | ␉}␊ |
150 | ␉␊ |
151 | ␉device->numentries = 0x00;␊ |
152 | ␉␊ |
153 | ␉device->acpi_dev_path.length = 0x0c;␊ |
154 | ␉device->acpi_dev_path.type = 0x02;␊ |
155 | ␉device->acpi_dev_path.subtype = 0x01;␊ |
156 | ␉device->acpi_dev_path._HID = 0xd041030a;␊ |
157 | ␉␊ |
158 | ␉device->num_pci_devpaths = numpaths;␊ |
159 | ␉device->length = 24 + (6*numpaths);␊ |
160 | ␉␊ |
161 | ␉int␉␉i;␊ |
162 | ␉␊ |
163 | ␉for(i = 0; i < numpaths; i++) {␊ |
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 | ␉␉return 0;␊ |
202 | ␉}␊ |
203 | ␊ |
204 | ␉memset(data, 0, length);␊ |
205 | ␉uint32_t off= 0;␊ |
206 | ␉data[off+1] = ((strlen(nm) * 2) + 6) >> 8;␊ |
207 | ␉data[off] = ((strlen(nm) * 2) + 6) & 0x00FF;␊ |
208 | ␊ |
209 | ␉off += 4;␊ |
210 | ␉uint32_t i=0, l = strlen(nm);␊ |
211 | ␉for(i = 0 ; i < l ; i++, off += 2) {␊ |
212 | ␉␉data[off] = *nm++;␊ |
213 | ␉}␊ |
214 | ␊ |
215 | ␉off += 2;␊ |
216 | ␉l = len;␊ |
217 | ␉uint32_t *datalength = (uint32_t*)&data[off];␊ |
218 | ␉*datalength = (uint32_t)(l + 4);␊ |
219 | ␉off += 4;␊ |
220 | ␉for(i = 0 ; i < l ; i++, off++) {␊ |
221 | ␉␉data[off] = *vl++;␊ |
222 | ␉}␊ |
223 | ␉␊ |
224 | ␉uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));␊ |
225 | ␉␊ |
226 | ␉uint8_t *newdata = (uint8_t*)malloc((length + offset));␊ |
227 | ␉if(!newdata) {␊ |
228 | ␉␉return 0;␊ |
229 | ␉}␊ |
230 | ␉if(device->data) {␊ |
231 | ␉␉if(offset > 1) {␊ |
232 | ␉␉␉memcpy(newdata, device->data, offset);␊ |
233 | ␉␉}␊ |
234 | ␉}␊ |
235 | ␊ |
236 | ␉memcpy(newdata + offset, data, length);␊ |
237 | ␉␊ |
238 | ␉device->length += length;␊ |
239 | ␉device->string->length += length;␊ |
240 | ␉device->numentries++;␊ |
241 | ␊ |
242 | ␉if(!device->data) {␊ |
243 | ␉␉device->data = (uint8_t*)malloc(sizeof(uint8_t));␊ |
244 | ␉} else {␊ |
245 | ␉␉free(device->data);␊ |
246 | ␉}␊ |
247 | ␊ |
248 | ␉free(data);␊ |
249 | ␉device->data = newdata;␊ |
250 | ␊ |
251 | ␉return 1;␊ |
252 | }␊ |
253 | ␊ |
254 | char *devprop_generate_string(DevPropString *string)␊ |
255 | {␊ |
256 | ␉char *buffer = (char*)malloc(string->length * 2);␊ |
257 | ␉char *ptr = buffer;␊ |
258 | ␊ |
259 | ␉if(!buffer)␊ |
260 | ␉{␊ |
261 | ␉␉return NULL;␊ |
262 | ␉}␊ |
263 | ␊ |
264 | ␉sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,␊ |
265 | ␉␉␉dp_swap16(string->numentries), string->WHAT3);␊ |
266 | ␉buffer += 24;␊ |
267 | ␉int i = 0, x = 0;␊ |
268 | ␊ |
269 | ␉while(i < string->numentries)␊ |
270 | ␉{␊ |
271 | ␉␉sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),␊ |
272 | ␉␉␉␉dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);␊ |
273 | ␉␉␊ |
274 | ␉␉buffer += 16;␊ |
275 | ␉␉sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,␊ |
276 | ␉␉␉␉string->entries[i]->acpi_dev_path.subtype,␊ |
277 | ␉␉␉␉dp_swap16(string->entries[i]->acpi_dev_path.length),␊ |
278 | ␉␉␉␉string->entries[i]->acpi_dev_path._HID,␊ |
279 | ␉␉␉␉dp_swap32(string->entries[i]->acpi_dev_path._UID));␊ |
280 | ␊ |
281 | ␉␉buffer += 24;␊ |
282 | ␉␉for(x = 0;x < string->entries[i]->num_pci_devpaths; x++)␊ |
283 | ␉␉{␊ |
284 | ␉␉␉sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,␊ |
285 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].subtype,␊ |
286 | ␉␉␉␉␉dp_swap16(string->entries[i]->pci_dev_path[x].length),␊ |
287 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].function,␊ |
288 | ␉␉␉␉␉string->entries[i]->pci_dev_path[x].device);␊ |
289 | ␉␉␉buffer += 12;␊ |
290 | ␉␉}␊ |
291 | ␉␉␊ |
292 | ␉␉sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,␊ |
293 | ␉␉␉␉string->entries[i]->path_end.subtype,␊ |
294 | ␉␉␉␉dp_swap16(string->entries[i]->path_end.length));␊ |
295 | ␉␉␊ |
296 | ␉␉buffer += 8;␊ |
297 | ␉␉uint8_t *dataptr = string->entries[i]->data;␊ |
298 | ␉␉for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)␊ |
299 | ␉␉{␊ |
300 | ␉␉␉sprintf(buffer, "%02x", *dataptr++);␊ |
301 | ␉␉␉buffer += 2;␊ |
302 | ␉␉}␊ |
303 | ␉␉i++;␊ |
304 | ␉}␊ |
305 | ␉return ptr;␊ |
306 | }␊ |
307 | ␊ |
308 | void devprop_free_string(DevPropString *string)␊ |
309 | {␊ |
310 | ␊ |
311 | ␉if(!string) {␊ |
312 | ␉␉return;␊ |
313 | ␉}␊ |
314 | ␊ |
315 | ␉int i;␊ |
316 | ␉for(i = 0; i < string->numentries; i++) {␊ |
317 | ␉␉if(string->entries[i]) {␊ |
318 | ␉␉␉if(string->entries[i]->data) {␊ |
319 | ␉␉␉␉free(string->entries[i]->data);␊ |
320 | ␉␉␉␉string->entries[i]->data = NULL;␊ |
321 | ␉␉␉}␊ |
322 | ␉␉␉free(string->entries[i]);␊ |
323 | ␉␉␉string->entries[i] = NULL;␊ |
324 | ␉␉}␊ |
325 | ␉}␊ |
326 | ␊ |
327 | ␉free(string);␊ |
328 | ␉string = NULL;␊ |
329 | }␊ |
330 | ␊ |
331 | /* ======================================================= */␊ |
332 | ␊ |
333 | ␊ |
334 | /*******************************************************************␊ |
335 | * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *␊ |
336 | * a binary. returns -1 in case of error (i.e. badly formed chars) *␊ |
337 | *******************************************************************/␊ |
338 | int hex2bin(const char *hex, uint8_t *bin, int len)␊ |
339 | {␊ |
340 | ␉char␉*p;␊ |
341 | ␉int␉i;␊ |
342 | ␉char␉buf[3];␊ |
343 | ␊ |
344 | ␉if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2) {␊ |
345 | ␉␉printf("[ERROR] bin2hex input error\n");␊ |
346 | ␉␉return -1;␊ |
347 | ␉}␊ |
348 | ␊ |
349 | ␉buf[2] = '\0';␊ |
350 | ␉p = (char *) hex;␊ |
351 | ␊ |
352 | ␉for (i = 0; i < len; i++) {␊ |
353 | ␉␉if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {␊ |
354 | ␉␉␉printf("[ERROR] bin2hex '%s' syntax error\n", hex);␊ |
355 | ␉␉␉return -2;␊ |
356 | ␉␉}␊ |
357 | ␉␉buf[0] = *p++;␊ |
358 | ␉␉buf[1] = *p++;␊ |
359 | ␉␉bin[i] = (unsigned char) strtoul(buf, NULL, 16);␊ |
360 | ␉}␊ |
361 | ␉return 0;␊ |
362 | }␊ |
363 | ␊ |
364 | /* ======================================================= */␊ |
365 | ␊ |
366 | /* a fine place for this code */␊ |
367 | ␊ |
368 | int devprop_add_network_template(DevPropDevice *device, uint16_t vendor_id)␊ |
369 | {␊ |
370 | ␉if(!device)␊ |
371 | ␉␉return 0;␊ |
372 | ␉uint8_t builtin = 0x0;␊ |
373 | ␉if((vendor_id != 0x168c) && (builtin_set == 0)) ␊ |
374 | ␉{␊ |
375 | ␉␉builtin_set = 1;␊ |
376 | ␉␉builtin = 0x01;␊ |
377 | ␉}␊ |
378 | ␉if(!devprop_add_value(device, "built-in", (uint8_t*)&builtin, 1))␊ |
379 | ␉␉return 0;␊ |
380 | ␉devices_number++;␊ |
381 | ␉return 1;␊ |
382 | }␊ |
383 | ␊ |
384 | void set_eth_builtin(pci_dt_t *eth_dev)␊ |
385 | {␊ |
386 | ␉char *devicepath = get_pci_dev_path(eth_dev);␊ |
387 | ␉DevPropDevice *device = NULL;␊ |
388 | ␊ |
389 | ␉verbose("LAN Controller [%04x:%04x] :: %s\n", eth_dev->vendor_id, eth_dev->device_id, devicepath);␊ |
390 | ␊ |
391 | ␉if(!string) {␊ |
392 | ␉␉string = devprop_create_string();␊ |
393 | ␉}␊ |
394 | ␊ |
395 | ␉device = devprop_add_device(string, devicepath);␊ |
396 | ␉if(device) {␊ |
397 | ␉␉verbose("Setting up lan keys\n");␊ |
398 | ␉␉devprop_add_network_template(device, eth_dev->vendor_id);␊ |
399 | ␉␉stringdata = (uint8_t*)malloc(sizeof(uint8_t) * string->length);␊ |
400 | ␉␉if(stringdata) {␊ |
401 | ␉␉␉memcpy(stringdata, (uint8_t*)devprop_generate_string(string), string->length);␊ |
402 | ␉␉␉stringlength = string->length;␊ |
403 | ␉␉}␊ |
404 | ␉}␊ |
405 | }␊ |
406 | |