Chameleon

Chameleon Svn Source Tree

Root/branches/cparm/i386/libsaio/device_inject.c

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

Archive Download this file

Revision: 1919