File: tree.c

package info (click to toggle)
silo 1.4.14%2Bgit20120819-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,644 kB
  • sloc: ansic: 13,383; asm: 3,004; makefile: 351; sh: 102; perl: 85
file content (168 lines) | stat: -rw-r--r-- 3,588 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* $Id: tree.c,v 1.3 2003/04/14 03:24:43 bencollins Exp $
 * tree.c: Basic device tree traversal/scanning for the Linux
 *         prom library.
 *
 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
 */

#include <silo.h>
#include <stringops.h>

/* Return the child of node 'node' or zero if no this node has no
 * direct descendent.
 */
int prom_getchild(int node)
{
	int cnode;

	if (node == -1)
		return 0;

	if (prom_vers != PROM_P1275)
		cnode = prom_nodeops->no_child(node);
	else
		cnode = p1275_cmd ("child", 1, node);
		
	if (cnode == 0 || cnode == -1)
		return 0;

	return cnode;
}

/* Return the next sibling of node 'node' or zero if no more siblings
 * at this level of depth in the tree.
 */
int prom_getsibling(int node)
{
	int sibnode;

	if (node == -1)
		return 0;

	if (prom_vers != PROM_P1275)
		sibnode = prom_nodeops->no_nextnode(node);
	else
		sibnode = p1275_cmd ("peer", 1, node);
		
	if (sibnode == 0 || sibnode == -1)
		return 0;

	return sibnode;
}

/* Return the length in bytes of property 'prop' at node 'node'.
 * Return -1 on error.
 */
int prom_getproplen(int node, char *prop)
{
	int ret;

	if((!node) || (!prop))
		ret = -1;
	else {
		if (prom_vers != PROM_P1275)
			ret = prom_nodeops->no_proplen(node, prop);
		else
			ret = p1275_cmd ("getproplen", 2, node, prop);
	}
	return ret;
}

/* Acquire a property 'prop' at node 'node' and place it in
 * 'buffer' which has a size of 'bufsize'.  If the acquisition
 * was successful the length will be returned, else -1 is returned.
 */
int prom_getproperty(int node, char *prop, char *buffer, int bufsize)
{
	int plen, ret;

	plen = prom_getproplen(node, prop);
	if((plen > bufsize) || (plen == 0) || (plen == -1))
		ret = -1;
	else {
		/* Ok, things seem all right. */
		if (prom_vers != PROM_P1275)
			ret = prom_nodeops->no_getprop(node, prop, buffer);
		else
			ret = p1275_cmd ("getprop", 4, node, prop, buffer, bufsize);
	}
	return ret;
}

/* Acquire an integer property and return its value.  Returns -1
 * on failure.
 */
int prom_getint(int node, char *prop)
{
	static int intprop;

	if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
		return intprop;

	return -1;
}

/* Acquire an integer property, upon error return the passed default
 * integer.
 */
int prom_getintdefault(int node, char *property, int deflt)
{
	int retval;

	retval = prom_getint(node, property);
	if(retval == -1) return deflt;

	return retval;
}

/* Acquire a property whose value is a string, returns a null
 * string on error.  The char pointer is the user supplied string
 * buffer.
 */
void prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
{
	int len;

	len = prom_getproperty(node, prop, user_buf, ubuf_size);
	if(len != -1) return;
	user_buf[0] = 0;
	return;
}

/* Search siblings at 'node_start' for a node with name
 * 'nodename'.  Return node if successful, zero if not.
 */
int prom_searchsiblings(int node_start, char *nodename)
{

	int thisnode, error;
	static char promlib_buf[128];

	for(thisnode = node_start; thisnode;
	    thisnode=prom_getsibling(thisnode)) {
		error = prom_getproperty(thisnode, "name", promlib_buf,
					 sizeof(promlib_buf));
		/* Should this ever happen? */
		if(error == -1) continue;
		if (strcmp(nodename, promlib_buf) == 0)
			return thisnode;
	}

	return 0;
}

int prom_finddevice(char *path)
{
	int node;

	switch (prom_vers) {
	case PROM_P1275:
		node = p1275_cmd ("finddevice", 1, path);
		break;
	default:
		return 0;
	}
	if (node == -1) return 0;
	return node;
}