Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/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;
27DevPropString *string= NULL;
28uint8_t *stringdata= NULL;
29uint32_t stringlength= 0;
30
31char *efi_inject_get_devprop_string(uint32_t *len)
32{
33if(string) {
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{
43 const char *val;
44 uint8_t *binStr;
45 int cnt, cnt2;
46
47 static char DEVICE_PROPERTIES_PROP[] = "device-properties";
48
49 /* Generate devprop string.
50 */
51 uint32_t strlength;
52 char *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, &bootInfo->chameleonConfig) && string) {
58val = (const char*)string;
59cnt = strlength * 2;
60}
61
62if (cnt > 1) {
63binStr = convertHexStr2Binary(val, &cnt2);
64if (cnt2 > 0) {
65DT__AddProperty(node, DEVICE_PROPERTIES_PROP, cnt2, binStr);
66}
67}
68}
69
70DevPropString *devprop_create_string(void)
71{
72string = (struct DevPropString*)malloc(sizeof(struct DevPropString));
73
74if(string == NULL) {
75return NULL;
76}
77
78memset(string, 0, sizeof(struct DevPropString));
79string->length = 12;
80string->WHAT2 = 0x01000000;
81return string;
82}
83
84DevPropDevice *devprop_add_device(DevPropString *string, char *path)
85{
86DevPropDevice*device = NULL;
87const charpciroot_string[] = "PciRoot(0x";
88const charpci_device_string[] = "Pci(0x";
89
90if (string == NULL || path == NULL) {
91printf("ERROR null device path\n");
92return NULL;
93}
94
95if (strncmp(path, pciroot_string, strlen(pciroot_string))) {
96printf("ERROR parsing device path\n");
97return NULL;
98}
99if (!(device = malloc(sizeof(DevPropDevice)))) {
100printf("ERROR malloc failed\n");
101return NULL;
102}
103
104memset(device, 0, sizeof(DevPropDevice));
105device->acpi_dev_path._UID = getPciRootUID();
106
107int numpaths = 0;
108intx, curr = 0;
109charbuff[] = "00";
110
111for (x = 0; x < strlen(path); x++) {
112if (!strncmp(&path[x], pci_device_string, strlen(pci_device_string))) {
113x+=strlen(pci_device_string);
114curr=x;
115while(path[++x] != ',');
116if(x-curr == 2) {
117sprintf(buff, "%c%c", path[curr], path[curr+1]);
118} else if(x-curr == 1) {
119sprintf(buff, "%c", path[curr]);
120} else {
121printf("ERROR parsing device path\n");
122numpaths = 0;
123break;
124}
125device->pci_dev_path[numpaths].device =ascii_hex_to_int(buff);
126
127x += 3; // 0x
128curr = x;
129while(path[++x] != ')');
130if(x-curr == 2) {
131sprintf(buff, "%c%c", path[curr], path[curr+1]);
132} else if(x-curr == 1) {
133sprintf(buff, "%c", path[curr]);
134} else {
135printf("ERROR parsing device path\n");
136numpaths = 0;
137break;
138}
139device->pci_dev_path[numpaths].function = ascii_hex_to_int(buff); // TODO: find dev from char *path
140
141numpaths++;
142}
143}
144
145if(!numpaths) {
146free(device);
147return NULL;
148}
149
150device->numentries = 0x00;
151
152device->acpi_dev_path.length = 0x0c;
153device->acpi_dev_path.type = 0x02;
154device->acpi_dev_path.subtype = 0x01;
155device->acpi_dev_path._HID = 0xd041030a;
156
157device->num_pci_devpaths = numpaths;
158device->length = 24 + (6*numpaths);
159
160inti;
161
162for(i = 0; i < numpaths; i++)
163{
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)
201{
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)
213{
214data[off] = *nm++;
215}
216
217off += 2;
218l = len;
219uint32_t *datalength = (uint32_t*)&data[off];
220*datalength = (uint32_t)(l + 4);
221off += 4;
222for(i = 0 ; i < l ; i++, off++)
223{
224data[off] = *vl++;
225}
226}
227
228uint32_t offset = device->length - (24 + (6 * device->num_pci_devpaths));
229
230uint8_t *newdata = (uint8_t*)malloc((length + offset));
231if(!newdata)
232{
233return 0;
234}
235if(device->data)
236{
237if(offset > 1)
238{
239memcpy(newdata, device->data, offset);
240}
241}
242
243memcpy(newdata + offset, data, length);
244
245device->length += length;
246device->string->length += length;
247device->numentries++;
248
249if(!device->data)
250{
251device->data = (uint8_t*)malloc(sizeof(uint8_t));
252}
253else
254{
255free(device->data);
256}
257
258free(data);
259device->data = newdata;
260
261return 1;
262}
263
264char *devprop_generate_string(DevPropString *string)
265{
266char *buffer = (char*)malloc(string->length * 2);
267char *ptr = buffer;
268
269if(!buffer)
270{
271return NULL;
272}
273
274sprintf(buffer, "%08x%08x%04x%04x", dp_swap32(string->length), string->WHAT2,
275dp_swap16(string->numentries), string->WHAT3);
276buffer += 24;
277int i = 0, x = 0;
278
279while(i < string->numentries)
280{
281sprintf(buffer, "%08x%04x%04x", dp_swap32(string->entries[i]->length),
282dp_swap16(string->entries[i]->numentries), string->entries[i]->WHAT2);
283
284buffer += 16;
285sprintf(buffer, "%02x%02x%04x%08x%08x", string->entries[i]->acpi_dev_path.type,
286string->entries[i]->acpi_dev_path.subtype,
287dp_swap16(string->entries[i]->acpi_dev_path.length),
288string->entries[i]->acpi_dev_path._HID,
289dp_swap32(string->entries[i]->acpi_dev_path._UID));
290
291buffer += 24;
292for(x = 0;x < string->entries[i]->num_pci_devpaths; x++)
293{
294sprintf(buffer, "%02x%02x%04x%02x%02x", string->entries[i]->pci_dev_path[x].type,
295string->entries[i]->pci_dev_path[x].subtype,
296dp_swap16(string->entries[i]->pci_dev_path[x].length),
297string->entries[i]->pci_dev_path[x].function,
298string->entries[i]->pci_dev_path[x].device);
299buffer += 12;
300}
301
302sprintf(buffer, "%02x%02x%04x", string->entries[i]->path_end.type,
303string->entries[i]->path_end.subtype,
304dp_swap16(string->entries[i]->path_end.length));
305
306buffer += 8;
307uint8_t *dataptr = string->entries[i]->data;
308for(x = 0; (uint32_t)x < (string->entries[i]->length) - (24 + (6 * string->entries[i]->num_pci_devpaths)) ; x++)
309{
310sprintf(buffer, "%02x", *dataptr++);
311buffer += 2;
312}
313i++;
314}
315return ptr;
316}
317
318void devprop_free_string(DevPropString *string)
319{
320
321if(!string)
322{
323return;
324}
325
326int i;
327for(i = 0; i < string->numentries; i++)
328{
329if(string->entries[i])
330{
331if(string->entries[i]->data)
332{
333free(string->entries[i]->data);
334string->entries[i]->data = NULL;
335}
336free(string->entries[i]);
337string->entries[i] = NULL;
338}
339}
340
341free(string);
342string = NULL;
343}
344
345/* ======================================================= */
346
347
348/*******************************************************************
349 * Decodes a sequence of 'len' hexadecimal chars from 'hex' into *
350 * a binary. returns -1 in case of error (i.e. badly formed chars) *
351 *******************************************************************/
352int hex2bin(const char *hex, uint8_t *bin, int len)
353{
354char*p;
355inti;
356charbuf[3];
357
358if (hex == NULL || bin == NULL || len <= 0 || strlen(hex) != len * 2)
359{
360printf("[ERROR] bin2hex input error\n");
361return -1;
362}
363
364buf[2] = '\0';
365p = (char *) hex;
366
367for (i = 0; i < len; i++)
368{
369if (p[0] == '\0' || p[1] == '\0' || !isxdigit(p[0]) || !isxdigit(p[1]))
370{
371printf("[ERROR] bin2hex '%s' syntax error\n", hex);
372return -2;
373}
374buf[0] = *p++;
375buf[1] = *p++;
376bin[i] = (unsigned char) strtoul(buf, NULL, 16);
377}
378return 0;
379}
380
381/* ======================================================= */
382
383/* a fine place for this code */
384
385

Archive Download this file

Revision: 2323