Chameleon

Chameleon Svn Source Tree

Root/branches/Chimera/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++) {
164device->pci_dev_path[i].length = 0x06;
165device->pci_dev_path[i].type = 0x01;
166device->pci_dev_path[i].subtype = 0x01;
167}
168
169device->path_end.length = 0x04;
170device->path_end.type = 0x7f;
171device->path_end.subtype = 0xff;
172
173device->string = string;
174device->data = NULL;
175
176if(!string->entries) {
177if (!(string->entries = (struct DevPropDevice**) malloc(sizeof(device) * DEV_PROP_DEVICE_MAX_ENTRIES))) {
178free(device);
179return NULL;
180}
181}
182
183/* FIXME: probably needs bounds checking, as well as error handling in event of malloc failure */
184string->length += device->length;
185string->entries[string->numentries++] = (DevPropDevice*)malloc(sizeof(device));
186string->entries[string->numentries-1] = device;
187
188return device;
189}
190
191int devprop_add_value(DevPropDevice *device, char *nm, uint8_t *vl, uint32_t len)
192{
193
194if(!nm || !vl || !len) {
195return 0;
196}
197uint32_t length = ((strlen(nm) * 2) + len + (2 * sizeof(uint32_t)) + 2);
198uint8_t *data = (uint8_t*)malloc(length);
199
200if(!data) {
201return 0;
202}
203
204memset(data, 0, length);
205uint32_t off= 0;
206data[off+1] = ((strlen(nm) * 2) + 6) >> 8;
207data[off] = ((strlen(nm) * 2) + 6) & 0x00FF;
208
209off += 4;
210uint32_t i=0, l = strlen(nm);
211for(i = 0 ; i < l ; i++, off += 2) {
212data[off] = *nm++;
213}
214
215off += 2;
216l = len;
217uint32_t *datalength = (uint32_t*)&data[off];
218*datalength = (uint32_t)(l + 4);
219off += 4;
220for(i = 0 ; i < l ; i++, off++) {
221data[off] = *vl++;
222}
223
224uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));
225
226uint8_t *newdata = (uint8_t*)malloc((length + offset));
227if(!newdata) {
228return 0;
229}
230if(device->data) {
231if(offset > 1) {
232memcpy(newdata, device->data, offset);
233}
234}
235
236memcpy(newdata + offset, data, length);
237
238device->length += length;
239device->string->length += length;
240device->numentries++;
241
242if(!device->data) {
243device->data = (uint8_t*)malloc(sizeof(uint8_t));
244} else {
245free(device->data);
246}
247
248free(data);
249device->data = newdata;
250
251return 1;
252}
253
254char *devprop_generate_string(DevPropString *string)
255{
256char *buffer = (char*)malloc(string->length * 2);
257char *ptr = buffer;
258
259if(!buffer)
260{
261return NULL;
262}
263
264sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,
265dp_swap16(string->numentries), string->WHAT3);
266buffer += 24;
267int i = 0, x = 0;
268
269while(i < string->numentries)
270{
271sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),
272dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);
273
274buffer += 16;
275sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,
276string->entries[i]->acpi_dev_path.subtype,
277dp_swap16(string->entries[i]->acpi_dev_path.length),
278string->entries[i]->acpi_dev_path._HID,
279dp_swap32(string->entries[i]->acpi_dev_path._UID));
280
281buffer += 24;
282for(x = 0;x < string->entries[i]->num_pci_devpaths; x++)
283{
284sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,
285string->entries[i]->pci_dev_path[x].subtype,
286dp_swap16(string->entries[i]->pci_dev_path[x].length),
287string->entries[i]->pci_dev_path[x].function,
288string->entries[i]->pci_dev_path[x].device);
289buffer += 12;
290}
291
292sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,
293string->entries[i]->path_end.subtype,
294dp_swap16(string->entries[i]->path_end.length));
295
296buffer += 8;
297uint8_t *dataptr = string->entries[i]->data;
298for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)
299{
300sprintf(buffer, "%02x", *dataptr++);
301buffer += 2;
302}
303i++;
304}
305return ptr;
306}
307
308void devprop_free_string(DevPropString *string)
309{
310
311if(!string) {
312return;
313}
314
315int i;
316for(i = 0; i < string->numentries; i++) {
317if(string->entries[i]) {
318if(string->entries[i]->data) {
319free(string->entries[i]->data);
320string->entries[i]->data = NULL;
321}
322free(string->entries[i]);
323string->entries[i] = NULL;
324}
325}
326
327free(string);
328string = 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 *******************************************************************/
338int hex2bin(const char *hex, uint8_t *bin, int len)
339{
340char*p;
341inti;
342charbuf[3];
343
344if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2) {
345printf("[ERROR] bin2hex input error\n");
346return -1;
347}
348
349buf[2] = '\0';
350p = (char *) hex;
351
352for (i = 0; i < len; i++) {
353if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1])) {
354printf("[ERROR] bin2hex '%s' syntax error\n", hex);
355return -2;
356}
357buf[0] = *p++;
358buf[1] = *p++;
359bin[i] = (unsigned char) strtoul(buf, NULL, 16);
360}
361return 0;
362}
363
364/* ======================================================= */
365
366/* a fine place for this code */
367
368int devprop_add_network_template(DevPropDevice *device, uint16_t vendor_id)
369{
370if(!device)
371return 0;
372uint8_t builtin = 0x0;
373if((vendor_id != 0x168c) && (builtin_set == 0))
374{
375builtin_set = 1;
376builtin = 0x01;
377}
378if(!devprop_add_value(device, "built-in", (uint8_t*)&builtin, 1))
379return 0;
380devices_number++;
381return 1;
382}
383
384void set_eth_builtin(pci_dt_t *eth_dev)
385{
386char *devicepath = get_pci_dev_path(eth_dev);
387DevPropDevice *device = NULL;
388
389verbose("LAN Controller [%04x:%04x] :: %s\n", eth_dev->vendor_id, eth_dev->device_id, devicepath);
390
391if(!string) {
392string = devprop_create_string();
393}
394
395device = devprop_add_device(string, devicepath);
396if(device) {
397verbose("Setting up lan keys\n");
398devprop_add_network_template(device, eth_dev->vendor_id);
399stringdata = (uint8_t*)malloc(sizeof(uint8_t) * string->length);
400if(stringdata) {
401memcpy(stringdata, (uint8_t*)devprop_generate_string(string), string->length);
402stringlength = string->length;
403}
404}
405}
406

Archive Download this file

Revision: 2403