1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include "libfdt_env.h"
52
53#ifndef USE_HOSTCC
54#include <fdt.h>
55#include <libfdt.h>
56#else
57#include "fdt_host.h"
58#endif
59
60#include "libfdt_internal.h"
61
62int fdt_check_header(const void *fdt)
63{
64 if (fdt_magic(fdt) == FDT_MAGIC) {
65
66 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
67 return -FDT_ERR_BADVERSION;
68 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
69 return -FDT_ERR_BADVERSION;
70 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
71
72 if (fdt_size_dt_struct(fdt) == 0)
73 return -FDT_ERR_BADSTATE;
74 } else {
75 return -FDT_ERR_BADMAGIC;
76 }
77
78 return 0;
79}
80
81const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
82{
83 const char *p;
84
85 if (fdt_version(fdt) >= 0x11)
86 if (((offset + len) < offset)
87 || ((offset + len) > fdt_size_dt_struct(fdt)))
88 return NULL;
89
90 p = _fdt_offset_ptr(fdt, offset);
91
92 if (p + len < p)
93 return NULL;
94 return p;
95}
96
97uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset)
98{
99 const uint32_t *tagp, *lenp;
100 uint32_t tag;
101 const char *p;
102
103 if (offset % FDT_TAGSIZE)
104 return -1;
105
106 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
107 if (! tagp)
108 return FDT_END;
109 tag = fdt32_to_cpu(*tagp);
110 offset += FDT_TAGSIZE;
111
112 switch (tag) {
113 case FDT_BEGIN_NODE:
114
115 do {
116 p = fdt_offset_ptr(fdt, offset++, 1);
117 } while (p && (*p != '\0'));
118 if (! p)
119 return FDT_END;
120 break;
121 case FDT_PROP:
122 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
123 if (! lenp)
124 return FDT_END;
125
126 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
127 break;
128 }
129
130 if (nextoffset)
131 *nextoffset = FDT_TAGALIGN(offset);
132
133 return tag;
134}
135
136int _fdt_check_node_offset(const void *fdt, int offset)
137{
138 if ((offset < 0) || (offset % FDT_TAGSIZE)
139 || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
140 return -FDT_ERR_BADOFFSET;
141
142 return offset;
143}
144
145int fdt_next_node(const void *fdt, int offset, int *depth)
146{
147 int nextoffset = 0;
148 uint32_t tag;
149
150 if (offset >= 0)
151 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
152 return nextoffset;
153
154 do {
155 offset = nextoffset;
156 tag = fdt_next_tag(fdt, offset, &nextoffset);
157
158 switch (tag) {
159 case FDT_PROP:
160 case FDT_NOP:
161 break;
162
163 case FDT_BEGIN_NODE:
164 if (depth)
165 (*depth)++;
166 break;
167
168 case FDT_END_NODE:
169 if (depth)
170 (*depth)--;
171 break;
172
173 case FDT_END:
174 return -FDT_ERR_NOTFOUND;
175
176 default:
177 return -FDT_ERR_BADSTRUCTURE;
178 }
179 } while (tag != FDT_BEGIN_NODE);
180
181 return offset;
182}
183
184const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
185{
186 int len = strlen(s) + 1;
187 const char *last = strtab + tabsize - len;
188 const char *p;
189
190 for (p = strtab; p <= last; p++)
191 if (memcmp(p, s, len) == 0)
192 return p;
193 return NULL;
194}
195
196int fdt_move(const void *fdt, void *buf, int bufsize)
197{
198 FDT_CHECK_HEADER(fdt);
199
200 if (fdt_totalsize(fdt) > bufsize)
201 return -FDT_ERR_NOSPACE;
202
203 memmove(buf, fdt, fdt_totalsize(fdt));
204 return 0;
205}
206