]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - libfdt/fdt.c
libfdt: Import libfdt source (1 of 2)
[karo-tx-uboot.git] / libfdt / fdt.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 int _fdt_check_header(const void *fdt)
27 {
28         if (fdt_magic(fdt) == FDT_MAGIC) {
29                 /* Complete tree */
30                 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
31                         return -FDT_ERR_BADVERSION;
32                 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
33                         return -FDT_ERR_BADVERSION;
34         } else if (fdt_magic(fdt) == SW_MAGIC) {
35                 /* Unfinished sequential-write blob */
36                 if (fdt_size_dt_struct(fdt) == 0)
37                         return -FDT_ERR_BADSTATE;
38         } else {
39                 return -FDT_ERR_BADMAGIC;
40         }
41
42         return 0;
43 }
44
45 void *fdt_offset_ptr(const void *fdt, int offset, int len)
46 {
47         void *p;
48
49         if (fdt_version(fdt) >= 0x11)
50                 if (((offset + len) < offset)
51                     || ((offset + len) > fdt_size_dt_struct(fdt)))
52                         return NULL;
53
54         p = _fdt_offset_ptr(fdt, offset);
55
56         if (p + len < p)
57                 return NULL;
58         return p;
59 }
60
61 uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset)
62 {
63         const uint32_t *tagp, *lenp;
64         uint32_t tag;
65         const char *p;
66
67         if (offset % FDT_TAGSIZE)
68                 return -1;
69
70         tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
71         if (! tagp)
72                 return FDT_END; /* premature end */
73         tag = fdt32_to_cpu(*tagp);
74         offset += FDT_TAGSIZE;
75
76         switch (tag) {
77         case FDT_BEGIN_NODE:
78                 /* skip name */
79                 do {
80                         p = fdt_offset_ptr(fdt, offset++, 1);
81                 } while (p && (*p != '\0'));
82                 if (! p)
83                         return FDT_END;
84                 break;
85         case FDT_PROP:
86                 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
87                 if (! lenp)
88                         return FDT_END;
89                 /* skip name offset, length and value */
90                 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
91                 break;
92         }
93
94         if (nextoffset)
95                 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
96
97         return tag;
98 }
99
100 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
101 {
102         int len = strlen(s) + 1;
103         const char *last = strtab + tabsize - len;
104         const char *p;
105
106         for (p = strtab; p <= last; p++)
107                 if (memeq(p, s, len))
108                         return p;
109         return NULL;
110 }
111
112 int fdt_move(const void *fdt, void *buf, int bufsize)
113 {
114         int err = _fdt_check_header(fdt);
115
116         if (err)
117                 return err;
118
119         if (fdt_totalsize(fdt) > bufsize)
120                 return -FDT_ERR_NOSPACE;
121
122         memmove(buf, fdt, fdt_totalsize(fdt));
123         return 0;
124 }