blob: 2055734012a1009438adf10b2d2e232f5b647c3d [file] [log] [blame]
Gerald Van Barenc0577a62007-03-31 11:59:59 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier3eaedea2013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Barenc0577a62007-03-31 11:59:59 -04005 */
Robert P. J. Day55019c52016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Barenc0577a62007-03-31 11:59:59 -04007
Bartlomiej Sieka61250862008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Barenc0577a62007-03-31 11:59:59 -04009#include <fdt.h>
10#include <libfdt.h>
Bartlomiej Sieka61250862008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Barenc0577a62007-03-31 11:59:59 -040014
15#include "libfdt_internal.h"
16
Gerald Van Baren9f0f3582007-04-06 14:17:14 -040017int fdt_check_header(const void *fdt)
Gerald Van Barenc0577a62007-03-31 11:59:59 -040018{
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;
David Gibson9457d9d2008-07-09 14:10:24 +100025 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
Gerald Van Barenc0577a62007-03-31 11:59:59 -040026 /* 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
Jon Loeligerd94d0f12008-09-25 11:02:17 -050036const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
Gerald Van Barenc0577a62007-03-31 11:59:59 -040037{
David Gibsonf8857be2016-10-02 17:59:26 -060038 unsigned absoffset = offset + fdt_off_dt_struct(fdt);
39
40 if ((absoffset < offset)
41 || ((absoffset + len) < absoffset)
42 || (absoffset + len) > fdt_totalsize(fdt))
43 return NULL;
Gerald Van Barenc0577a62007-03-31 11:59:59 -040044
45 if (fdt_version(fdt) >= 0x11)
46 if (((offset + len) < offset)
47 || ((offset + len) > fdt_size_dt_struct(fdt)))
48 return NULL;
49
David Gibsonf8857be2016-10-02 17:59:26 -060050 return _fdt_offset_ptr(fdt, offset);
Gerald Van Barenc0577a62007-03-31 11:59:59 -040051}
52
David Gibson82c10232009-02-06 14:03:24 +110053uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
Kumar Galac8ab7052007-10-24 11:04:22 -050054{
Kim Phillips38c38152013-01-16 13:59:50 +000055 const fdt32_t *tagp, *lenp;
Kumar Galac8ab7052007-10-24 11:04:22 -050056 uint32_t tag;
David Gibson82c10232009-02-06 14:03:24 +110057 int offset = startoffset;
Kumar Galac8ab7052007-10-24 11:04:22 -050058 const char *p;
59
David Gibson82c10232009-02-06 14:03:24 +110060 *nextoffset = -FDT_ERR_TRUNCATED;
Kumar Galac8ab7052007-10-24 11:04:22 -050061 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
David Gibson82c10232009-02-06 14:03:24 +110062 if (!tagp)
Kumar Galac8ab7052007-10-24 11:04:22 -050063 return FDT_END; /* premature end */
64 tag = fdt32_to_cpu(*tagp);
65 offset += FDT_TAGSIZE;
66
David Gibson82c10232009-02-06 14:03:24 +110067 *nextoffset = -FDT_ERR_BADSTRUCTURE;
Kumar Galac8ab7052007-10-24 11:04:22 -050068 switch (tag) {
69 case FDT_BEGIN_NODE:
70 /* skip name */
71 do {
72 p = fdt_offset_ptr(fdt, offset++, 1);
73 } while (p && (*p != '\0'));
David Gibson82c10232009-02-06 14:03:24 +110074 if (!p)
75 return FDT_END; /* premature end */
Kumar Galac8ab7052007-10-24 11:04:22 -050076 break;
David Gibson82c10232009-02-06 14:03:24 +110077
Kumar Galac8ab7052007-10-24 11:04:22 -050078 case FDT_PROP:
79 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
David Gibson82c10232009-02-06 14:03:24 +110080 if (!lenp)
81 return FDT_END; /* premature end */
82 /* skip-name offset, length and value */
83 offset += sizeof(struct fdt_property) - FDT_TAGSIZE
84 + fdt32_to_cpu(*lenp);
85 break;
86
87 case FDT_END:
88 case FDT_END_NODE:
89 case FDT_NOP:
Kumar Galac8ab7052007-10-24 11:04:22 -050090 break;
David Gibson82c10232009-02-06 14:03:24 +110091
92 default:
93 return FDT_END;
Kumar Galac8ab7052007-10-24 11:04:22 -050094 }
95
David Gibson82c10232009-02-06 14:03:24 +110096 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
97 return FDT_END; /* premature end */
Kumar Galac8ab7052007-10-24 11:04:22 -050098
David Gibson82c10232009-02-06 14:03:24 +110099 *nextoffset = FDT_TAGALIGN(offset);
Kumar Galac8ab7052007-10-24 11:04:22 -0500100 return tag;
101}
102
David Gibson148a49b2008-05-20 17:19:11 +1000103int _fdt_check_node_offset(const void *fdt, int offset)
104{
105 if ((offset < 0) || (offset % FDT_TAGSIZE)
106 || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
107 return -FDT_ERR_BADOFFSET;
108
109 return offset;
110}
111
David Gibson7b64da12010-03-09 17:39:14 +1100112int _fdt_check_prop_offset(const void *fdt, int offset)
113{
114 if ((offset < 0) || (offset % FDT_TAGSIZE)
115 || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
116 return -FDT_ERR_BADOFFSET;
117
118 return offset;
119}
120
David Gibsond9d793c2008-02-12 11:58:31 +1100121int fdt_next_node(const void *fdt, int offset, int *depth)
122{
123 int nextoffset = 0;
124 uint32_t tag;
125
David Gibson148a49b2008-05-20 17:19:11 +1000126 if (offset >= 0)
127 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
128 return nextoffset;
David Gibsond9d793c2008-02-12 11:58:31 +1100129
130 do {
131 offset = nextoffset;
132 tag = fdt_next_tag(fdt, offset, &nextoffset);
133
134 switch (tag) {
135 case FDT_PROP:
136 case FDT_NOP:
137 break;
138
139 case FDT_BEGIN_NODE:
140 if (depth)
141 (*depth)++;
142 break;
143
144 case FDT_END_NODE:
David Gibson954914a2009-02-06 14:01:56 +1100145 if (depth && ((--(*depth)) < 0))
146 return nextoffset;
David Gibsond9d793c2008-02-12 11:58:31 +1100147 break;
148
149 case FDT_END:
David Gibson82c10232009-02-06 14:03:24 +1100150 if ((nextoffset >= 0)
151 || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
152 return -FDT_ERR_NOTFOUND;
153 else
154 return nextoffset;
David Gibsond9d793c2008-02-12 11:58:31 +1100155 }
156 } while (tag != FDT_BEGIN_NODE);
157
158 return offset;
159}
160
Simon Glass3a4de332013-05-07 06:11:50 +0000161int fdt_first_subnode(const void *fdt, int offset)
162{
163 int depth = 0;
164
165 offset = fdt_next_node(fdt, offset, &depth);
166 if (offset < 0 || depth != 1)
167 return -FDT_ERR_NOTFOUND;
168
169 return offset;
170}
171
172int fdt_next_subnode(const void *fdt, int offset)
173{
174 int depth = 1;
175
176 /*
177 * With respect to the parent, the depth of the next subnode will be
178 * the same as the last.
179 */
180 do {
181 offset = fdt_next_node(fdt, offset, &depth);
182 if (offset < 0 || depth < 1)
183 return -FDT_ERR_NOTFOUND;
184 } while (depth > 1);
185
186 return offset;
187}
188
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400189const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
190{
191 int len = strlen(s) + 1;
192 const char *last = strtab + tabsize - len;
193 const char *p;
194
195 for (p = strtab; p <= last; p++)
David Gibson9457d9d2008-07-09 14:10:24 +1000196 if (memcmp(p, s, len) == 0)
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400197 return p;
198 return NULL;
199}
200
201int fdt_move(const void *fdt, void *buf, int bufsize)
202{
David Gibson9457d9d2008-07-09 14:10:24 +1000203 FDT_CHECK_HEADER(fdt);
Gerald Van Barenc0577a62007-03-31 11:59:59 -0400204
205 if (fdt_totalsize(fdt) > bufsize)
206 return -FDT_ERR_NOSPACE;
207
208 memmove(buf, fdt, fdt_totalsize(fdt));
209 return 0;
210}