]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - libfdt/fdt_ro.c
Removed some nonused fdt functions and moved fdt_find_and_setprop out of libfdt
[karo-tx-uboot.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         int err; \
28         if ((err = fdt_check_header(fdt)) != 0) \
29                 return err; \
30 }
31
32 static int offset_streq(const void *fdt, int offset,
33                         const char *s, int len)
34 {
35         const char *p = fdt_offset_ptr(fdt, offset, len+1);
36
37         if (! p)
38                 /* short match */
39                 return 0;
40
41         if (memcmp(p, s, len) != 0)
42                 return 0;
43
44         if (p[len] != '\0')
45                 return 0;
46
47         return 1;
48 }
49
50 /*
51  * Checks if the property name matches.
52  */
53 static int prop_name_eq(const void *fdt, int offset, const char *name,
54                         struct fdt_property **prop, int *lenp)
55 {
56         int namestroff, len;
57
58         *prop = fdt_offset_ptr_typed(fdt, offset, *prop);
59         if (! *prop)
60                 return -FDT_ERR_BADSTRUCTURE;
61
62         namestroff = fdt32_to_cpu((*prop)->nameoff);
63         if (streq(fdt_string(fdt, namestroff), name)) {
64                 len = fdt32_to_cpu((*prop)->len);
65                 *prop = fdt_offset_ptr(fdt, offset,
66                                        sizeof(**prop) + len);
67                 if (*prop) {
68                         if (lenp)
69                                 *lenp = len;
70                         return 1;
71                 } else
72                         return -FDT_ERR_BADSTRUCTURE;
73         }
74         return 0;
75 }
76
77 /*
78  * Return a pointer to the string at the given string offset.
79  */
80 char *fdt_string(const void *fdt, int stroffset)
81 {
82         return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
83 }
84
85 /*
86  * Return the node offset of the node specified by:
87  *   parentoffset - starting place (0 to start at the root)
88  *   name         - name being searched for
89  *   namelen      - length of the name: typically strlen(name)
90  *
91  * Notes:
92  *   If the start node has subnodes, the subnodes are _not_ searched for the
93  *     requested name.
94  */
95 int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
96                                const char *name, int namelen)
97 {
98         int level = 0;
99         uint32_t tag;
100         int offset, nextoffset;
101
102         CHECK_HEADER(fdt);
103
104         tag = fdt_next_tag(fdt, parentoffset, &nextoffset, NULL);
105         if (tag != FDT_BEGIN_NODE)
106                 return -FDT_ERR_BADOFFSET;
107
108         do {
109                 offset = nextoffset;
110                 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
111
112                 switch (tag) {
113                 case FDT_END:
114                         return -FDT_ERR_TRUNCATED;
115
116                 case FDT_BEGIN_NODE:
117                         level++;
118                         /*
119                          * If we are nested down levels, ignore the strings
120                          * until we get back to the proper level.
121                          */
122                         if (level != 1)
123                                 continue;
124
125                         /* Return the offset if this is "our" string. */
126                         if (offset_streq(fdt, offset+FDT_TAGSIZE, name, namelen))
127                                 return offset;
128                         break;
129
130                 case FDT_END_NODE:
131                         level--;
132                         break;
133
134                 case FDT_PROP:
135                 case FDT_NOP:
136                         break;
137
138                 default:
139                         return -FDT_ERR_BADSTRUCTURE;
140                 }
141         } while (level >= 0);
142
143         return -FDT_ERR_NOTFOUND;
144 }
145
146 /*
147  * See fdt_subnode_offset_namelen()
148  */
149 int fdt_subnode_offset(const void *fdt, int parentoffset,
150                        const char *name)
151 {
152         return fdt_subnode_offset_namelen(fdt, parentoffset, name, strlen(name));
153 }
154
155 /*
156  * Searches for the node corresponding to the given path and returns the
157  * offset of that node.
158  */
159 int fdt_find_node_by_path(const void *fdt, const char *path)
160 {
161         const char *end = path + strlen(path);
162         const char *p = path;
163         int offset = 0;
164
165         CHECK_HEADER(fdt);
166
167         /* Paths must be absolute */
168         if (*path != '/')
169                 return -FDT_ERR_BADPATH;
170
171         /* Handle the root path: root offset is 0 */
172         if (strcmp(path, "/") == 0)
173                 return 0;
174
175         while (*p) {
176                 const char *q;
177
178                 /* Skip path separator(s) */
179                 while (*p == '/')
180                         p++;
181                 if (! *p)
182                         return -FDT_ERR_BADPATH;
183
184                 /*
185                  * Find the next path separator.  The characters between
186                  * p and q are the next segment of the the path to find.
187                  */
188                 q = strchr(p, '/');
189                 if (! q)
190                         q = end;
191
192                 /*
193                  * Find the offset corresponding to the this path segment.
194                  */
195                 offset = fdt_subnode_offset_namelen(fdt, offset, p, q-p);
196
197                 /* Oops, error, abort abort abort */
198                 if (offset < 0)
199                         return offset;
200
201                 p = q;
202         }
203
204         return offset;
205 }
206
207 /*
208  * Given the offset of a node and a name of a property in that node, return
209  * a pointer to the property struct.
210  */
211 struct fdt_property *fdt_get_property(const void *fdt,
212                                       int nodeoffset,
213                                       const char *name, int *lenp)
214 {
215         int level = 0;
216         uint32_t tag;
217         struct fdt_property *prop;
218         int offset, nextoffset;
219         int err;
220
221         if ((err = fdt_check_header(fdt)) != 0)
222                 goto fail;
223
224         err = -FDT_ERR_BADOFFSET;
225         if (nodeoffset % FDT_TAGSIZE)
226                 goto fail;
227
228         tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
229         if (tag != FDT_BEGIN_NODE)
230                 goto fail;
231
232         do {
233                 offset = nextoffset;
234
235                 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
236                 switch (tag) {
237                 case FDT_END:
238                         err = -FDT_ERR_TRUNCATED;
239                         goto fail;
240
241                 case FDT_BEGIN_NODE:
242                         level++;
243                         break;
244
245                 case FDT_END_NODE:
246                         level--;
247                         break;
248
249                 case FDT_PROP:
250                         /*
251                          * If we are nested down levels, ignore the strings
252                          * until we get back to the proper level.
253                          */
254                         if (level != 0)
255                                 continue;
256
257                         err = prop_name_eq(fdt, offset, name, &prop, lenp);
258                         if (err > 0)
259                                 return prop;
260                         else if (err < 0)
261                                 goto fail;
262                         break;
263
264                 case FDT_NOP:
265                         break;
266
267                 default:
268                         err = -FDT_ERR_BADSTRUCTURE;
269                         goto fail;
270                 }
271         } while (level >= 0);
272
273         err = -FDT_ERR_NOTFOUND;
274 fail:
275         if (lenp)
276                 *lenp = err;
277         return NULL;
278 }
279
280 /*
281  * Given the offset of a node and a name of a property in that node, return
282  * a pointer to the property data (ONLY).
283  */
284 void *fdt_getprop(const void *fdt, int nodeoffset,
285                   const char *name, int *lenp)
286 {
287         const struct fdt_property *prop;
288
289         prop = fdt_get_property(fdt, nodeoffset, name, lenp);
290         if (! prop)
291                 return NULL;
292
293         return (void *)prop->data;
294 }
295
296
297 uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset, char **namep)
298 {
299         const uint32_t *tagp, *lenp;
300         uint32_t tag;
301         const char *p;
302
303         if (offset % FDT_TAGSIZE)
304                 return -1;
305
306         tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
307         if (! tagp)
308                 return FDT_END; /* premature end */
309         tag = fdt32_to_cpu(*tagp);
310         offset += FDT_TAGSIZE;
311
312         switch (tag) {
313         case FDT_BEGIN_NODE:
314                 if(namep)
315                         *namep = fdt_offset_ptr(fdt, offset, 1);
316
317                 /* skip name */
318                 do {
319                         p = fdt_offset_ptr(fdt, offset++, 1);
320                 } while (p && (*p != '\0'));
321                 if (! p)
322                         return FDT_END;
323                 break;
324         case FDT_PROP:
325                 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
326                 if (! lenp)
327                         return FDT_END;
328                 /*
329                  * Get the property and set the namep to the name.
330                  */
331                 if(namep) {
332                         struct fdt_property *prop;
333
334                         prop = fdt_offset_ptr_typed(fdt, offset - FDT_TAGSIZE, prop);
335                         if (! prop)
336                                 return -FDT_ERR_BADSTRUCTURE;
337                         *namep = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
338                 }
339                 /* skip name offset, length and value */
340                 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
341                 break;
342         }
343
344         if (nextoffset)
345                 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
346
347         return tag;
348 }
349
350 /*
351  * Return the number of used reserve map entries and total slots available.
352  */
353 int fdt_num_reservemap(void *fdt, int *used, int *total)
354 {
355         struct fdt_reserve_entry *re;
356         int  start;
357         int  end;
358         int  err = fdt_check_header(fdt);
359
360         if (err != 0)
361                 return err;
362
363         start = fdt_off_mem_rsvmap(fdt);
364
365         /*
366          * Convention is that the reserve map is before the dt_struct,
367          * but it does not have to be.
368          */
369         end = fdt_totalsize(fdt);
370         if (end > fdt_off_dt_struct(fdt))
371                 end = fdt_off_dt_struct(fdt);
372         if (end > fdt_off_dt_strings(fdt))
373                 end = fdt_off_dt_strings(fdt);
374
375         /*
376          * Since the reserved area list is zero terminated, you get one fewer.
377          */
378         if (total)
379                 *total = ((end - start) / sizeof(struct fdt_reserve_entry)) - 1;
380
381         if (used) {
382                 *used = 0;
383                 while (start < end) {
384                         re = (struct fdt_reserve_entry *)(fdt + start);
385                         if (re->size == 0)
386                                 return 0;       /* zero size terminates the list */
387
388                         *used += 1;
389                         start += sizeof(struct fdt_reserve_entry);
390                 }
391                 /*
392                  * If we get here, there was no zero size termination.
393                  */
394                 return -FDT_ERR_BADLAYOUT;
395         }
396         return 0;
397 }
398
399 /*
400  * Return the nth reserve map entry.
401  */
402 int fdt_get_reservemap(void *fdt, int n, struct fdt_reserve_entry *re)
403 {
404         int  used;
405         int  total;
406         int  err;
407
408         err = fdt_num_reservemap(fdt, &used, &total);
409         if (err != 0)
410                 return err;
411
412         if (n >= total)
413                 return -FDT_ERR_NOSPACE;
414         if (re) {
415                 *re = *(struct fdt_reserve_entry *)
416                         _fdt_offset_ptr(fdt, n * sizeof(struct fdt_reserve_entry));
417         }
418         return 0;
419 }