blob: 2055734012a1009438adf10b2d2e232f5b647c3d [file] [log] [blame]
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier35084762013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04005 */
Robert P. J. Day6feed2a2016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04007
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren7cd5da02007-03-31 11:59:59 -04009#include <fdt.h>
10#include <libfdt.h>
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040014
15#include "libfdt_internal.h"
16
Gerald Van Baren6679f922007-04-06 14:17:14 -040017int fdt_check_header(const void *fdt)
Gerald Van Baren7cd5da02007-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 Gibsonfc7758e2008-07-09 14:10:24 +100025 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
Gerald Van Baren7cd5da02007-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 Loeliger741a6d02008-09-25 11:02:17 -050036const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040037{
David Gibson491c7b62016-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 Baren7cd5da02007-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 Gibson491c7b62016-10-02 17:59:26 -060050 return _fdt_offset_ptr(fdt, offset);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040051}
52
David Gibsona22d9cf2009-02-06 14:03:24 +110053uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
Kumar Gala8d04f022007-10-24 11:04:22 -050054{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +000055 const fdt32_t *tagp, *lenp;
Kumar Gala8d04f022007-10-24 11:04:22 -050056 uint32_t tag;
David Gibsona22d9cf2009-02-06 14:03:24 +110057 int offset = startoffset;
Kumar Gala8d04f022007-10-24 11:04:22 -050058 const char *p;
59
David Gibsona22d9cf2009-02-06 14:03:24 +110060 *nextoffset = -FDT_ERR_TRUNCATED;
Kumar Gala8d04f022007-10-24 11:04:22 -050061 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
David Gibsona22d9cf2009-02-06 14:03:24 +110062 if (!tagp)
Kumar Gala8d04f022007-10-24 11:04:22 -050063 return FDT_END; /* premature end */
64 tag = fdt32_to_cpu(*tagp);
65 offset += FDT_TAGSIZE;
66
David Gibsona22d9cf2009-02-06 14:03:24 +110067 *nextoffset = -FDT_ERR_BADSTRUCTURE;
Kumar Gala8d04f022007-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 Gibsona22d9cf2009-02-06 14:03:24 +110074 if (!p)
75 return FDT_END; /* premature end */
Kumar Gala8d04f022007-10-24 11:04:22 -050076 break;
David Gibsona22d9cf2009-02-06 14:03:24 +110077
Kumar Gala8d04f022007-10-24 11:04:22 -050078 case FDT_PROP:
79 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
David Gibsona22d9cf2009-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);
Kumar Gala8d04f022007-10-24 11:04:22 -050085 break;
David Gibsona22d9cf2009-02-06 14:03:24 +110086
87 case FDT_END:
88 case FDT_END_NODE:
89 case FDT_NOP:
90 break;
91
92 default:
93 return FDT_END;
Kumar Gala8d04f022007-10-24 11:04:22 -050094 }
95
David Gibsona22d9cf2009-02-06 14:03:24 +110096 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
97 return FDT_END; /* premature end */
Kumar Gala8d04f022007-10-24 11:04:22 -050098
David Gibsona22d9cf2009-02-06 14:03:24 +110099 *nextoffset = FDT_TAGALIGN(offset);
Kumar Gala8d04f022007-10-24 11:04:22 -0500100 return tag;
101}
102
David Gibson2f08bfa2008-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 Gibsond1c63142010-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 Gibsonae0b5902008-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 Gibson2f08bfa2008-05-20 17:19:11 +1000126 if (offset >= 0)
127 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
128 return nextoffset;
David Gibsonae0b5902008-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 Gibson2c0b8432009-02-06 14:01:56 +1100145 if (depth && ((--(*depth)) < 0))
146 return nextoffset;
David Gibsonae0b5902008-02-12 11:58:31 +1100147 break;
148
149 case FDT_END:
David Gibsona22d9cf2009-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 Gibsonae0b5902008-02-12 11:58:31 +1100155 }
156 } while (tag != FDT_BEGIN_NODE);
157
158 return offset;
159}
160
Simon Glass88f95bb2013-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 Baren7cd5da02007-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 Gibsonfc7758e2008-07-09 14:10:24 +1000196 if (memcmp(p, s, len) == 0)
Gerald Van Baren7cd5da02007-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 Gibsonfc7758e2008-07-09 14:10:24 +1000203 FDT_CHECK_HEADER(fdt);
Gerald Van Baren7cd5da02007-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}