]> git.ipfire.org Git - people/ms/u-boot.git/blame - lib/libfdt/fdt.c
Merge branch 'master' of git://git.denx.de/u-boot-spi
[people/ms/u-boot.git] / lib / libfdt / fdt.c
CommitLineData
7cd5da0f
GVB
1/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
35084760 4 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
7cd5da0f
GVB
5 */
6#include "libfdt_env.h"
7
8cf30809 8#ifndef USE_HOSTCC
7cd5da0f
GVB
9#include <fdt.h>
10#include <libfdt.h>
8cf30809
BS
11#else
12#include "fdt_host.h"
13#endif
7cd5da0f
GVB
14
15#include "libfdt_internal.h"
16
6679f929 17int fdt_check_header(const void *fdt)
7cd5da0f
GVB
18{
19 if (fdt_magic(fdt) == FDT_MAGIC) {
20 /* Complete tree */
21 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
22 return -FDT_ERR_BADVERSION;
23 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
24 return -FDT_ERR_BADVERSION;
fc7758ee 25 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
7cd5da0f
GVB
26 /* Unfinished sequential-write blob */
27 if (fdt_size_dt_struct(fdt) == 0)
28 return -FDT_ERR_BADSTATE;
29 } else {
30 return -FDT_ERR_BADMAGIC;
31 }
32
33 return 0;
34}
35
741a6d01 36const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
7cd5da0f 37{
ef4e8ce1 38 const char *p;
7cd5da0f
GVB
39
40 if (fdt_version(fdt) >= 0x11)
41 if (((offset + len) < offset)
42 || ((offset + len) > fdt_size_dt_struct(fdt)))
43 return NULL;
44
45 p = _fdt_offset_ptr(fdt, offset);
46
47 if (p + len < p)
48 return NULL;
49 return p;
50}
51
a22d9cfb 52uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
8d04f02f 53{
b2ba62a1 54 const fdt32_t *tagp, *lenp;
8d04f02f 55 uint32_t tag;
a22d9cfb 56 int offset = startoffset;
8d04f02f
KG
57 const char *p;
58
a22d9cfb 59 *nextoffset = -FDT_ERR_TRUNCATED;
8d04f02f 60 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
a22d9cfb 61 if (!tagp)
8d04f02f
KG
62 return FDT_END; /* premature end */
63 tag = fdt32_to_cpu(*tagp);
64 offset += FDT_TAGSIZE;
65
a22d9cfb 66 *nextoffset = -FDT_ERR_BADSTRUCTURE;
8d04f02f
KG
67 switch (tag) {
68 case FDT_BEGIN_NODE:
69 /* skip name */
70 do {
71 p = fdt_offset_ptr(fdt, offset++, 1);
72 } while (p && (*p != '\0'));
a22d9cfb
DG
73 if (!p)
74 return FDT_END; /* premature end */
8d04f02f 75 break;
a22d9cfb 76
8d04f02f
KG
77 case FDT_PROP:
78 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
a22d9cfb
DG
79 if (!lenp)
80 return FDT_END; /* premature end */
81 /* skip-name offset, length and value */
82 offset += sizeof(struct fdt_property) - FDT_TAGSIZE
83 + fdt32_to_cpu(*lenp);
84 break;
85
86 case FDT_END:
87 case FDT_END_NODE:
88 case FDT_NOP:
8d04f02f 89 break;
a22d9cfb
DG
90
91 default:
92 return FDT_END;
8d04f02f
KG
93 }
94
a22d9cfb
DG
95 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
96 return FDT_END; /* premature end */
8d04f02f 97
a22d9cfb 98 *nextoffset = FDT_TAGALIGN(offset);
8d04f02f
KG
99 return tag;
100}
101
2f08bfa9
DG
102int _fdt_check_node_offset(const void *fdt, int offset)
103{
104 if ((offset < 0) || (offset % FDT_TAGSIZE)
105 || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
106 return -FDT_ERR_BADOFFSET;
107
108 return offset;
109}
110
d1c63148
DG
111int _fdt_check_prop_offset(const void *fdt, int offset)
112{
113 if ((offset < 0) || (offset % FDT_TAGSIZE)
114 || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
115 return -FDT_ERR_BADOFFSET;
116
117 return offset;
118}
119
ae0b5908
DG
120int fdt_next_node(const void *fdt, int offset, int *depth)
121{
122 int nextoffset = 0;
123 uint32_t tag;
124
2f08bfa9
DG
125 if (offset >= 0)
126 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
127 return nextoffset;
ae0b5908
DG
128
129 do {
130 offset = nextoffset;
131 tag = fdt_next_tag(fdt, offset, &nextoffset);
132
133 switch (tag) {
134 case FDT_PROP:
135 case FDT_NOP:
136 break;
137
138 case FDT_BEGIN_NODE:
139 if (depth)
140 (*depth)++;
141 break;
142
143 case FDT_END_NODE:
2c0b843e
DG
144 if (depth && ((--(*depth)) < 0))
145 return nextoffset;
ae0b5908
DG
146 break;
147
148 case FDT_END:
a22d9cfb
DG
149 if ((nextoffset >= 0)
150 || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
151 return -FDT_ERR_NOTFOUND;
152 else
153 return nextoffset;
ae0b5908
DG
154 }
155 } while (tag != FDT_BEGIN_NODE);
156
157 return offset;
158}
159
88f95bba
SG
160int fdt_first_subnode(const void *fdt, int offset)
161{
162 int depth = 0;
163
164 offset = fdt_next_node(fdt, offset, &depth);
165 if (offset < 0 || depth != 1)
166 return -FDT_ERR_NOTFOUND;
167
168 return offset;
169}
170
171int fdt_next_subnode(const void *fdt, int offset)
172{
173 int depth = 1;
174
175 /*
176 * With respect to the parent, the depth of the next subnode will be
177 * the same as the last.
178 */
179 do {
180 offset = fdt_next_node(fdt, offset, &depth);
181 if (offset < 0 || depth < 1)
182 return -FDT_ERR_NOTFOUND;
183 } while (depth > 1);
184
185 return offset;
186}
187
7cd5da0f
GVB
188const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
189{
190 int len = strlen(s) + 1;
191 const char *last = strtab + tabsize - len;
192 const char *p;
193
194 for (p = strtab; p <= last; p++)
fc7758ee 195 if (memcmp(p, s, len) == 0)
7cd5da0f
GVB
196 return p;
197 return NULL;
198}
199
200int fdt_move(const void *fdt, void *buf, int bufsize)
201{
fc7758ee 202 FDT_CHECK_HEADER(fdt);
7cd5da0f
GVB
203
204 if (fdt_totalsize(fdt) > bufsize)
205 return -FDT_ERR_NOSPACE;
206
207 memmove(buf, fdt, fdt_totalsize(fdt));
208 return 0;
209}