blob: e146aba6eb7cdb182191954c67a4e3ec99ce8a42 [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 */
6#include "libfdt_env.h"
7
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 Gibsonef4e8ce2008-07-07 10:10:48 +100038 const char *p;
Gerald Van Baren7cd5da02007-03-31 11:59:59 -040039
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
David Gibsona22d9cf2009-02-06 14:03:24 +110052uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
Kumar Gala8d04f022007-10-24 11:04:22 -050053{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +000054 const fdt32_t *tagp, *lenp;
Kumar Gala8d04f022007-10-24 11:04:22 -050055 uint32_t tag;
David Gibsona22d9cf2009-02-06 14:03:24 +110056 int offset = startoffset;
Kumar Gala8d04f022007-10-24 11:04:22 -050057 const char *p;
58
David Gibsona22d9cf2009-02-06 14:03:24 +110059 *nextoffset = -FDT_ERR_TRUNCATED;
Kumar Gala8d04f022007-10-24 11:04:22 -050060 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
David Gibsona22d9cf2009-02-06 14:03:24 +110061 if (!tagp)
Kumar Gala8d04f022007-10-24 11:04:22 -050062 return FDT_END; /* premature end */
63 tag = fdt32_to_cpu(*tagp);
64 offset += FDT_TAGSIZE;
65
David Gibsona22d9cf2009-02-06 14:03:24 +110066 *nextoffset = -FDT_ERR_BADSTRUCTURE;
Kumar Gala8d04f022007-10-24 11:04:22 -050067 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'));
David Gibsona22d9cf2009-02-06 14:03:24 +110073 if (!p)
74 return FDT_END; /* premature end */
Kumar Gala8d04f022007-10-24 11:04:22 -050075 break;
David Gibsona22d9cf2009-02-06 14:03:24 +110076
Kumar Gala8d04f022007-10-24 11:04:22 -050077 case FDT_PROP:
78 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
David Gibsona22d9cf2009-02-06 14:03:24 +110079 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);
Kumar Gala8d04f022007-10-24 11:04:22 -050084 break;
David Gibsona22d9cf2009-02-06 14:03:24 +110085
86 case FDT_END:
87 case FDT_END_NODE:
88 case FDT_NOP:
89 break;
90
91 default:
92 return FDT_END;
Kumar Gala8d04f022007-10-24 11:04:22 -050093 }
94
David Gibsona22d9cf2009-02-06 14:03:24 +110095 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
96 return FDT_END; /* premature end */
Kumar Gala8d04f022007-10-24 11:04:22 -050097
David Gibsona22d9cf2009-02-06 14:03:24 +110098 *nextoffset = FDT_TAGALIGN(offset);
Kumar Gala8d04f022007-10-24 11:04:22 -050099 return tag;
100}
101
David Gibson2f08bfa2008-05-20 17:19:11 +1000102int _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
David Gibsond1c63142010-03-09 17:39:14 +1100111int _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
David Gibsonae0b5902008-02-12 11:58:31 +1100120int fdt_next_node(const void *fdt, int offset, int *depth)
121{
122 int nextoffset = 0;
123 uint32_t tag;
124
David Gibson2f08bfa2008-05-20 17:19:11 +1000125 if (offset >= 0)
126 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
127 return nextoffset;
David Gibsonae0b5902008-02-12 11:58:31 +1100128
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:
David Gibson2c0b8432009-02-06 14:01:56 +1100144 if (depth && ((--(*depth)) < 0))
145 return nextoffset;
David Gibsonae0b5902008-02-12 11:58:31 +1100146 break;
147
148 case FDT_END:
David Gibsona22d9cf2009-02-06 14:03:24 +1100149 if ((nextoffset >= 0)
150 || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
151 return -FDT_ERR_NOTFOUND;
152 else
153 return nextoffset;
David Gibsonae0b5902008-02-12 11:58:31 +1100154 }
155 } while (tag != FDT_BEGIN_NODE);
156
157 return offset;
158}
159
Simon Glass88f95bb2013-05-07 06:11:50 +0000160int 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
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400188const 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++)
David Gibsonfc7758e2008-07-09 14:10:24 +1000195 if (memcmp(p, s, len) == 0)
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400196 return p;
197 return NULL;
198}
199
200int fdt_move(const void *fdt, void *buf, int bufsize)
201{
David Gibsonfc7758e2008-07-09 14:10:24 +1000202 FDT_CHECK_HEADER(fdt);
Gerald Van Baren7cd5da02007-03-31 11:59:59 -0400203
204 if (fdt_totalsize(fdt) > bufsize)
205 return -FDT_ERR_NOSPACE;
206
207 memmove(buf, fdt, fdt_totalsize(fdt));
208 return 0;
209}