~ubuntu-branches/ubuntu/hardy/silo/hardy-proposed

« back to all changes in this revision

Viewing changes to common/tree.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2007-10-25 09:28:08 UTC
  • mfrom: (15.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20071025092808-1yhj12t7s4zqsfu5
Tags: 1.4.13a+git20070930-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Build with -fno-stack-protector.
  - Change silo.postinst to automatically update the boot block without
    invoking siloconfig and keep asking questions on upgrades.
  - Convert silo.conf to use /dev/disk/by-uuid.
  - Ubuntu maintainer foobar.
  - Fix debian/rules call to dh_installdocs.
  - Drop the requirement of gcc-4.1 and start using default gcc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: tree.c,v 1.3 2003/04/14 03:24:43 bencollins Exp $
 
2
 * tree.c: Basic device tree traversal/scanning for the Linux
 
3
 *         prom library.
 
4
 *
 
5
 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
 
6
 * Copyright (C) 1997 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
 
7
 */
 
8
 
 
9
#include <silo.h>
 
10
#include <stringops.h>
 
11
 
 
12
/* Return the child of node 'node' or zero if no this node has no
 
13
 * direct descendent.
 
14
 */
 
15
int prom_getchild(int node)
 
16
{
 
17
        int cnode;
 
18
 
 
19
        if (node == -1)
 
20
                return 0;
 
21
 
 
22
        if (prom_vers != PROM_P1275)
 
23
                cnode = prom_nodeops->no_child(node);
 
24
        else
 
25
                cnode = p1275_cmd ("child", 1, node);
 
26
                
 
27
        if (cnode == 0 || cnode == -1)
 
28
                return 0;
 
29
 
 
30
        return cnode;
 
31
}
 
32
 
 
33
/* Return the next sibling of node 'node' or zero if no more siblings
 
34
 * at this level of depth in the tree.
 
35
 */
 
36
int prom_getsibling(int node)
 
37
{
 
38
        int sibnode;
 
39
 
 
40
        if (node == -1)
 
41
                return 0;
 
42
 
 
43
        if (prom_vers != PROM_P1275)
 
44
                sibnode = prom_nodeops->no_nextnode(node);
 
45
        else
 
46
                sibnode = p1275_cmd ("peer", 1, node);
 
47
                
 
48
        if (sibnode == 0 || sibnode == -1)
 
49
                return 0;
 
50
 
 
51
        return sibnode;
 
52
}
 
53
 
 
54
/* Return the length in bytes of property 'prop' at node 'node'.
 
55
 * Return -1 on error.
 
56
 */
 
57
int prom_getproplen(int node, char *prop)
 
58
{
 
59
        int ret;
 
60
 
 
61
        if((!node) || (!prop))
 
62
                ret = -1;
 
63
        else {
 
64
                if (prom_vers != PROM_P1275)
 
65
                        ret = prom_nodeops->no_proplen(node, prop);
 
66
                else
 
67
                        ret = p1275_cmd ("getproplen", 2, node, prop);
 
68
        }
 
69
        return ret;
 
70
}
 
71
 
 
72
/* Acquire a property 'prop' at node 'node' and place it in
 
73
 * 'buffer' which has a size of 'bufsize'.  If the acquisition
 
74
 * was successful the length will be returned, else -1 is returned.
 
75
 */
 
76
int prom_getproperty(int node, char *prop, char *buffer, int bufsize)
 
77
{
 
78
        int plen, ret;
 
79
 
 
80
        plen = prom_getproplen(node, prop);
 
81
        if((plen > bufsize) || (plen == 0) || (plen == -1))
 
82
                ret = -1;
 
83
        else {
 
84
                /* Ok, things seem all right. */
 
85
                if (prom_vers != PROM_P1275)
 
86
                        ret = prom_nodeops->no_getprop(node, prop, buffer);
 
87
                else
 
88
                        ret = p1275_cmd ("getprop", 4, node, prop, buffer, bufsize);
 
89
        }
 
90
        return ret;
 
91
}
 
92
 
 
93
/* Acquire an integer property and return its value.  Returns -1
 
94
 * on failure.
 
95
 */
 
96
int prom_getint(int node, char *prop)
 
97
{
 
98
        static int intprop;
 
99
 
 
100
        if(prom_getproperty(node, prop, (char *) &intprop, sizeof(int)) != -1)
 
101
                return intprop;
 
102
 
 
103
        return -1;
 
104
}
 
105
 
 
106
/* Acquire an integer property, upon error return the passed default
 
107
 * integer.
 
108
 */
 
109
int prom_getintdefault(int node, char *property, int deflt)
 
110
{
 
111
        int retval;
 
112
 
 
113
        retval = prom_getint(node, property);
 
114
        if(retval == -1) return deflt;
 
115
 
 
116
        return retval;
 
117
}
 
118
 
 
119
/* Acquire a property whose value is a string, returns a null
 
120
 * string on error.  The char pointer is the user supplied string
 
121
 * buffer.
 
122
 */
 
123
void prom_getstring(int node, char *prop, char *user_buf, int ubuf_size)
 
124
{
 
125
        int len;
 
126
 
 
127
        len = prom_getproperty(node, prop, user_buf, ubuf_size);
 
128
        if(len != -1) return;
 
129
        user_buf[0] = 0;
 
130
        return;
 
131
}
 
132
 
 
133
/* Search siblings at 'node_start' for a node with name
 
134
 * 'nodename'.  Return node if successful, zero if not.
 
135
 */
 
136
int prom_searchsiblings(int node_start, char *nodename)
 
137
{
 
138
 
 
139
        int thisnode, error;
 
140
        static char promlib_buf[128];
 
141
 
 
142
        for(thisnode = node_start; thisnode;
 
143
            thisnode=prom_getsibling(thisnode)) {
 
144
                error = prom_getproperty(thisnode, "name", promlib_buf,
 
145
                                         sizeof(promlib_buf));
 
146
                /* Should this ever happen? */
 
147
                if(error == -1) continue;
 
148
                if (strcmp(nodename, promlib_buf) == 0)
 
149
                        return thisnode;
 
150
        }
 
151
 
 
152
        return 0;
 
153
}
 
154
 
 
155
int prom_finddevice(char *path)
 
156
{
 
157
        int node;
 
158
 
 
159
        switch (prom_vers) {
 
160
        case PROM_P1275:
 
161
                node = p1275_cmd ("finddevice", 1, path);
 
162
                break;
 
163
        default:
 
164
                return 0;
 
165
        }
 
166
        if (node == -1) return 0;
 
167
        return node;
 
168
}