Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/Chazileon/i386/libsaio/pci_root.c

1/*
2 * Copyright 2009 netkas
3 */
4
5#include "libsaio.h"
6#include "boot.h"
7#include "bootstruct.h"
8
9#ifndef DEBUG_PCIROOT
10#define DEBUG_PCIROOT 1
11#endif
12
13#if DEBUG_PCIROOT
14#define DBG(x...) printf(x)
15#else
16#define DBG(x...)
17#endif
18
19static int rootuid = 10; //value means function wasnt ran yet
20
21static unsigned int findrootuid(unsigned char * dsdt, int len)
22{
23int i;
24for (i=0; i<64 && i<len-5; i++) //not far than 64 symbols from pci root
25{
26if(dsdt[i] == '_' && dsdt[i+1] == 'U' && dsdt[i+2] == 'I' && dsdt[i+3] == 'D' && dsdt[i+5] == 0x08)
27{
28return dsdt[i+4];
29}
30}
31return 11; // _UID isnt present on ACPI data.
32}
33
34static unsigned int findpciroot(unsigned char * dsdt,int len)
35{
36int i;
37
38for (i=0; i<len-4; i++)
39{
40if(dsdt[i] == 'P' && dsdt[i+1] == 'C' && dsdt[i+2] == 'I' && (dsdt[i+3] == 0x08 || dsdt [i+4] == 0x08))
41{
42return findrootuid(dsdt+i, len-i);
43}
44}
45return 10;
46}
47
48int getPciRootUID(void)
49{
50const char *val;
51const char * dsdt_filename = NULL; //Azi:dsdt
52int len, fd, fsize;
53void *new_dsdt;
54//Azi: warning: implicit declaration of function ‘search_and_get_acpi_fd’, if removed... hum... but works fine!
55extern int search_and_get_acpi_fd(const char *, const char **);
56
57if (rootuid < 10) return rootuid;
58
59// If user supplied a key...
60if (getValueForKey(kPciRootKey, &val, &len, &bootInfo->bootConfig))
61{
62if (isdigit(val[0]))
63rootuid = val[0] - '0';
64// ... use the value in it.
65verbose("Using PCI-Root-UID value from PciRoot key: %d\n", rootuid);
66goto out_out;
67}
68
69// Search for a user supplied ACPI Table, to fetch the value from.
70fd = search_and_get_acpi_fd("DSDT.aml", &dsdt_filename); //Azi:dsdt call 1
71
72// If no ACPI Table is supplied by user...
73if (fd < 0)
74{
75verbose("No ACPI Table supplied by user.\n");
76rootuid = 0; // default uid to 0.
77goto out;
78}
79
80// Found ACPI Table supplied by user, check size...
81fsize = file_size(fd);
82
83// ... try to allocate memory...
84if ((new_dsdt = malloc(fsize)) == NULL)
85{
86verbose("[ERROR] DSDT memory allocation failed.\n");
87close (fd); // ... if allocation fails close file.
88rootuid = 0; // Default uid to 0.
89goto out;
90}
91
92// Try to read file...
93if (read(fd, new_dsdt, fsize) != fsize)
94{
95verbose("[ERROR] read %s failed.\n", dsdt_filename);
96close (fd); // ... if reading fails close file.
97rootuid = 0; // Default uid to 0.
98goto out;
99}
100// else new_dsdt = supplied ACPI Table data.
101close (fd); // Supplied Table can be closed.
102
103// Find value on supplied data.
104rootuid = findpciroot(new_dsdt, fsize);
105free(new_dsdt); // Free allocated data.
106
107// If _UID isn't present on ACPI data (value 11)...
108if (rootuid < 0 || rootuid > 9) // ... it's negative or bigger than 9...
109{
110rootuid = 0; // ... default uid to 0.
111}
112else
113printf("Found UID value on user supplied ACPI Table...\n");
114out:
115verbose("Using PCI-Root-UID value: %d\n", rootuid);
116out_out:
117return rootuid;
118}
119

Archive Download this file

Revision: 296