4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
28 #include <sys/sunddi.h>
29 #include <sys/ddi_impldefs.h>
30 #include <sys/openpromio.h>
33 static int getpropval(struct openpromio
*opp
, char *prop
);
35 static char *promdev
= "/dev/openprom";
37 static char *mfail
= "malloc";
40 * 128 is the size of the largest (currently) property name
41 * 16384 - MAXPROPSIZE - sizeof (int) is the size of the largest
42 * (currently) property value that is allowed.
43 * the sizeof (u_int) is from struct openpromio
46 #define MAXPROPSIZE 128
47 #define MAXVALSIZE (16384 - MAXPROPSIZE - sizeof (u_int))
48 #define BUFSIZE (MAXPROPSIZE + MAXVALSIZE + sizeof (u_int))
49 #define MINVALSIZE (4 * sizeof (u_long))
50 #define MINBUFSIZE (MINVALSIZE + sizeof (u_long))
54 struct openpromio opp
;
58 char buf
[MINVALSIZE
+ sizeof (u_int
)];
59 struct openpromio opp
;
65 next(unsigned long id
)
68 struct openpromio
*opp
= &(oppbuf
.opp
);
69 unsigned long *ip
= (unsigned long *)(opp
->oprom_array
);
71 memset(oppbuf
.buf
, 0, MINBUFSIZE
);
72 opp
->oprom_size
= MINVALSIZE
;
74 if (ioctl(prom_fd
, OPROMNEXT
, opp
) < 0)
76 return (*(unsigned long *)opp
->oprom_array
);
80 child(unsigned long id
)
83 struct openpromio
*opp
= &(oppbuf
.opp
);
84 unsigned long *ip
= (unsigned long *)(opp
->oprom_array
);
86 memset(oppbuf
.buf
, 0, MINBUFSIZE
);
87 opp
->oprom_size
= MINVALSIZE
;
89 if (ioctl(prom_fd
, OPROMCHILD
, opp
) < 0)
91 return (*(unsigned long *)opp
->oprom_array
);
95 * Find a node by name from the prom device tree.
96 * Return the id or 0 if it is not found.
99 prom_findnode_byname(unsigned long id
, char *name
)
101 struct openpromio
*opp
= &(oppbuf
.opp
);
106 if (!getpropval(opp
, "name"))
108 if (strcmp(opp
->oprom_array
, name
) == 0)
110 if (nid
= prom_findnode_byname(child(id
), name
))
112 if (nid
= prom_findnode_byname(next(id
), name
))
118 * Make the current prom node be the rootnode and return its id.
127 getpropval(struct openpromio
*opp
, char *prop
)
129 opp
->oprom_size
= MAXVALSIZE
;
131 (void) strlcpy(opp
->oprom_array
, prop
, MAXPROPSIZE
);
132 if (ioctl(prom_fd
, OPROMGETPROP
, opp
) < 0)
134 if (opp
->oprom_size
== 0)
140 getnextprop(struct openpromio
*opp
, char *prop
)
142 opp
->oprom_size
= MAXVALSIZE
;
144 (void) strlcpy(opp
->oprom_array
, prop
, MAXPROPSIZE
);
145 if (ioctl(prom_fd
, OPROMNXTPROP
, opp
) < 0)
147 if (opp
->oprom_size
== 0)
155 struct openpromio
*opp
= &(oppbuf
.opp
);
156 opp
->oprom_size
= MAXVALSIZE
;
157 if (ioctl(prom_fd
, OPROMGETBOOTPATH
, opp
) < 0)
159 return (opp
->oprom_array
);
163 * Get a pointer to the requested property from the current node.
164 * The property is stored in static storage and the returned pointer
165 * points into the static storage. The property length is placed in
166 * the location pointed to by the third argument.
168 static unsigned char *
169 prom_getprop(char *prop
, int *lenp
)
171 struct openpromio
*opp
= &(oppbuf
.opp
);
173 if (!getpropval(opp
, prop
))
175 *lenp
= opp
->oprom_size
;
176 return ((unsigned char *)opp
->oprom_array
);
179 static unsigned char *
180 prom_nextprop(char *prop
)
182 struct openpromio
*opp
= &(oppbuf
.opp
);
184 if (!getnextprop(opp
, prop
))
185 return ((unsigned char *)0);
186 return ((unsigned char *)opp
->oprom_array
);
190 get_proplist(char *name
)
192 ddi_prop_t
*plist
, *npp
, *plast
;
193 char *curprop
, *newprop
;
194 unsigned char *propval
;
199 id
= prom_findnode_byname(prom_rootnode(), name
);
203 while (newprop
= (char *)prom_nextprop(curprop
)) {
204 curprop
= strdup(newprop
);
205 npp
= (ddi_prop_t
*)malloc(sizeof (ddi_prop_t
));
207 exit(_error(PERROR
, mfail
));
208 propval
= prom_getprop(curprop
, &npp
->prop_len
);
209 npp
->prop_name
= curprop
;
210 if (propval
!= NULL
) {
211 npp
->prop_val
= (char *)malloc(npp
->prop_len
);
212 if (npp
->prop_val
== 0)
213 exit(_error(PERROR
, mfail
));
214 memcpy(npp
->prop_val
, propval
, npp
->prop_len
);
216 npp
->prop_val
= NULL
;
217 npp
->prop_next
= NULL
;
221 plast
->prop_next
= npp
;
229 get_propval(char *name
, char *node
)
231 ddi_prop_t
*prop
, *plist
;
233 if ((plist
= get_proplist(node
)) == NULL
)
236 for (prop
= plist
; prop
!= NULL
; prop
= prop
->prop_next
)
237 if (strcmp(prop
->prop_name
, name
) == 0)
238 return (prop
->prop_val
);
246 if ((prom_fd
= open(promdev
, O_RDONLY
)) < 0) {
247 exit(_error(PERROR
, "prom open failed"));
254 (void) close(prom_fd
);