Chameleon

Chameleon Svn Source Tree

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

Archive Download this file

Revision: 2331