libfdt: Enhanced and published fdt_next_tag()
[platform/kernel/u-boot.git] / libfdt / fdt_ro.c
1 /*
2  * libfdt - Flat Device Tree manipulation
3  * Copyright (C) 2006 David Gibson, IBM Corporation.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public License
7  * as published by the Free Software Foundation; either version 2.1 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 #include "libfdt_env.h"
20
21 #include <fdt.h>
22 #include <libfdt.h>
23
24 #include "libfdt_internal.h"
25
26 #define CHECK_HEADER(fdt) \
27         { \
28                 int err; \
29                 if ((err = _fdt_check_header(fdt)) != 0) \
30                         return err; \
31         }
32
33 static int offset_streq(const void *fdt, int offset,
34                         const char *s, int len)
35 {
36         const char *p = fdt_offset_ptr(fdt, offset, len+1);
37
38         if (! p)
39                 /* short match */
40                 return 0;
41
42         if (memcmp(p, s, len) != 0)
43                 return 0;
44
45         if (p[len] != '\0')
46                 return 0;
47
48         return 1;
49 }
50
51 /*
52  * Return a pointer to the string at the given string offset.
53  */
54 char *fdt_string(const void *fdt, int stroffset)
55 {
56         return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
57 }
58
59 /*
60  * Return the node offset of the node specified by:
61  *   parentoffset - starting place (0 to start at the root)
62  *   name         - name being searched for
63  *   namelen      - length of the name: typically strlen(name)
64  *
65  * Notes:
66  *   If the start node has subnodes, the subnodes are _not_ searched for the
67  *     requested name.
68  */
69 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
70                                const char *name, int namelen)
71 {
72         int level = 0;
73         uint32_t tag;
74         int offset, nextoffset;
75
76         CHECK_HEADER(fdt);
77
78         tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
79         if (tag != FDT_BEGIN_NODE)
80                 return -FDT_ERR_BADOFFSET;
81
82         do {
83                 offset = nextoffset;
84                 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
85
86                 switch (tag) {
87                 case FDT_END:
88                         return -FDT_ERR_TRUNCATED;
89
90                 case FDT_BEGIN_NODE:
91                         level++;
92                         /*
93                          * If we are nested down levels, ignore the strings
94                          * until we get back to the proper level.
95                          */
96                         if (level != 1)
97                                 continue;
98
99                         /* Return the offset if this is "our" string. */
100                         if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
101                                 return offset;
102                         break;
103
104                 case FDT_END_NODE:
105                         level--;
106                         break;
107
108                 case FDT_PROP:
109                 case FDT_NOP:
110                         break;
111
112                 default:
113                         return -FDT_ERR_BADSTRUCTURE;
114                 }
115         } while (level >= 0);
116
117         return -FDT_ERR_NOTFOUND;
118 }
119
120 /*
121  * See fdt_subnode_offset_namelen()
122  */
123 int fdt_subnode_offset(const void *fdt, int parentoffset,
124                        const char *name)
125 {
126         return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
127 }
128
129 /*
130  * Searches for the node corresponding to the given path and returns the
131  * offset of that node.
132  */
133 int fdt_path_offset(const void *fdt, const char *path)
134 {
135         const char *end = path + strlen(path);
136         const char *p = path;
137         int offset = 0;
138
139         CHECK_HEADER(fdt);
140
141         /* Paths must be absolute */
142         if (*path != '/')
143                 return -FDT_ERR_BADPATH;
144
145         while (*p) {
146                 const char *q;
147
148                 /* Skip path separator(s) */
149                 while (*p == '/')
150                         p++;
151                 if (! *p)
152                         return -FDT_ERR_BADPATH;
153
154                 /*
155                  * Find the next path separator.  The characters between
156                  * p and q are the next segment of the the path to find.
157                  */
158                 q = strchr(p, '/');
159                 if (! q)
160                         q = end;
161
162                 /*
163                  * Find the offset corresponding to the this path segment.
164                  */
165                 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
166
167                 /* Oops, error, abort abort abort */
168                 if (offset < 0)
169                         return offset;
170
171                 p = q;
172         }
173
174         return offset;  
175 }
176
177 /*
178  * Given the offset of a node and a name of a property in that node, return
179  * a pointer to the property struct.
180  */
181 struct fdt_property *fdt_get_property(const void *fdt,
182                                       int nodeoffset,
183                                       const char *name, int *lenp)
184 {
185         int level = 0;
186         uint32_t tag;
187         struct fdt_property *prop;
188         int namestroff;
189         int offset, nextoffset;
190         int err;
191
192         if ((err = _fdt_check_header(fdt)) != 0)
193                 goto fail;
194
195         err = -FDT_ERR_BADOFFSET;
196         if (nodeoffset % FDT_TAGSIZE)
197                 goto fail;
198
199         tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
200         if (tag != FDT_BEGIN_NODE)
201                 goto fail;
202
203         do {
204                 offset = nextoffset;
205
206                 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
207                 switch (tag) {
208                 case FDT_END:
209                         err = -FDT_ERR_TRUNCATED;
210                         goto fail;
211
212                 case FDT_BEGIN_NODE:
213                         level++;
214                         break;
215
216                 case FDT_END_NODE:
217                         level--;
218                         break;
219
220                 case FDT_PROP:
221                         /*
222                          * If we are nested down levels, ignore the strings
223                          * until we get back to the proper level.
224                          */
225                         if (level != 0)
226                                 continue;
227
228                         err = -FDT_ERR_BADSTRUCTURE;
229                         prop = fdt_offset_ptr_typed(fdt, offset, prop);
230                         if (! prop)
231                                 goto fail;
232                         namestroff = fdt32_to_cpu(prop->nameoff);
233                         if (streq(fdt_string(fdt, namestroff), name)) {
234                                 /* Found it! */
235                                 int len = fdt32_to_cpu(prop->len);
236                                 prop = fdt_offset_ptr(fdt, offset,
237                                                       sizeof(*prop)+len);
238                                 if (! prop)
239                                         goto fail;
240
241                                 if (lenp)
242                                         *lenp = len;
243                                 
244                                 return prop;
245                         }
246                         break;
247
248                 case FDT_NOP:
249                         break;
250
251                 default:
252                         err = -FDT_ERR_BADSTRUCTURE;
253                         goto fail;
254                 }
255         } while (level >= 0);
256
257         err = -FDT_ERR_NOTFOUND;
258  fail:
259         if (lenp)
260                 *lenp = err;
261         return NULL;
262 }
263
264 /*
265  * Given the offset of a node and a name of a property in that node, return
266  * a pointer to the property data (ONLY).
267  */
268 void *fdt_getprop(const void *fdt, int nodeoffset,
269                   const char *name, int *lenp)
270 {
271         const struct fdt_property *prop;
272
273         prop = fdt_get_property(fdt, nodeoffset, name, lenp);
274         if (! prop)
275                 return NULL;
276
277         return (void *)prop->data;
278 }
279
280
281 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
282 {
283         const uint32_t *tagp, *lenp;
284         uint32_t tag;
285         const char *p;
286
287         if (offset % FDT_TAGSIZE)
288                 return -1;
289
290         tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
291         if (! tagp)
292                 return FDT_END; /* premature end */
293         tag = fdt32_to_cpu(*tagp);
294         offset += FDT_TAGSIZE;
295
296         switch (tag) {
297         case FDT_BEGIN_NODE:
298                 if(namep)
299                         *namep = fdt_offset_ptr(fdt, offset, 1);
300
301                 /* skip name */
302                 do {
303                         p = fdt_offset_ptr(fdt, offset++, 1);
304                 } while (p && (*p != '\0'));
305                 if (! p)
306                         return FDT_END;
307                 break;
308         case FDT_PROP:
309                 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
310                 if (! lenp)
311                         return FDT_END;
312                 /*
313                  * Get the property and set the namep to the name.
314                  */
315                 if(namep) {
316                         struct fdt_property *prop;
317
318                         prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
319                         if (! prop)
320                                 return -FDT_ERR_BADSTRUCTURE;
321                         *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
322                 }
323                 /* skip name offset, length and value */
324                 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
325                 break;
326         }
327
328         if (nextoffset)
329                 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
330
331         return tag;
332 }
333