blob: 55fcc41d1a2abd2852878b9d9760d17a57998799 [file] [log] [blame]
Gerald Van Baren35748172007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
Gerald Van Baren8096b3b2007-04-20 22:46:53 -040019#include "config.h"
20#if CONFIG_OF_LIBFDT
21
Gerald Van Baren35748172007-03-31 12:00:56 -040022#include "libfdt_env.h"
23
24#include <fdt.h>
25#include <libfdt.h>
26
27#include "libfdt_internal.h"
28
29static int rw_check_header(void *fdt)
30{
31 int err;
32
Gerald Van Baren6679f922007-04-06 14:17:14 -040033 if ((err = fdt_check_header(fdt)))
Gerald Van Baren35748172007-03-31 12:00:56 -040034 return err;
35 if (fdt_version(fdt) < 0x11)
36 return -FDT_ERR_BADVERSION;
37 if (fdt_off_mem_rsvmap(fdt) < ALIGN(sizeof(struct fdt_header), 8))
38 return -FDT_ERR_BADLAYOUT;
39 if (fdt_off_dt_struct(fdt) <
40 (fdt_off_mem_rsvmap(fdt) + sizeof(struct fdt_reserve_entry)))
41 return -FDT_ERR_BADLAYOUT;
42 if (fdt_off_dt_strings(fdt) <
43 (fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt)))
44 return -FDT_ERR_BADLAYOUT;
45 if (fdt_totalsize(fdt) <
46 (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)))
47 return -FDT_ERR_BADLAYOUT;
48 return 0;
49}
50
51#define RW_CHECK_HEADER(fdt) \
52 { \
53 int err; \
54 if ((err = rw_check_header(fdt)) != 0) \
55 return err; \
56 }
57
58static inline int _blob_data_size(void *fdt)
59{
60 return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
61}
62
63static int _blob_splice(void *fdt, void *p, int oldlen, int newlen)
64{
65 void *end = fdt + _blob_data_size(fdt);
66
67 if (((p + oldlen) < p) || ((p + oldlen) > end))
68 return -FDT_ERR_BADOFFSET;
69 if ((end - oldlen + newlen) > (fdt + fdt_totalsize(fdt)))
70 return -FDT_ERR_NOSPACE;
71 memmove(p + newlen, p + oldlen, end - p - oldlen);
72 return 0;
73}
74
75static int _blob_splice_struct(void *fdt, void *p,
76 int oldlen, int newlen)
77{
78 int delta = newlen - oldlen;
79 int err;
80
81 if ((err = _blob_splice(fdt, p, oldlen, newlen)))
82 return err;
83
84 fdt_set_header(fdt, size_dt_struct, fdt_size_dt_struct(fdt) + delta);
85 fdt_set_header(fdt, off_dt_strings, fdt_off_dt_strings(fdt) + delta);
86 return 0;
87}
88
89static int _blob_splice_string(void *fdt, int newlen)
90{
91 void *p = fdt + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
92 int err;
93
94 if ((err = _blob_splice(fdt, p, 0, newlen)))
95 return err;
96
97 fdt_set_header(fdt, size_dt_strings, fdt_size_dt_strings(fdt) + newlen);
98 return 0;
99}
100
101static int _find_add_string(void *fdt, const char *s)
102{
103 char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
104 const char *p;
105 char *new;
106 int len = strlen(s) + 1;
107 int err;
108
109 p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
110 if (p)
111 /* found it */
112 return (p - strtab);
113
114 new = strtab + fdt_size_dt_strings(fdt);
115 err = _blob_splice_string(fdt, len);
116 if (err)
117 return err;
118
119 memcpy(new, s, len);
120 return (new - strtab);
121}
122
123static int _resize_property(void *fdt, int nodeoffset, const char *name, int len,
124 struct fdt_property **prop)
125{
126 int oldlen;
127 int err;
128
129 *prop = fdt_get_property(fdt, nodeoffset, name, &oldlen);
130 if (! (*prop))
131 return oldlen;
132
133 if ((err = _blob_splice_struct(fdt, (*prop)->data,
134 ALIGN(oldlen, FDT_TAGSIZE),
135 ALIGN(len, FDT_TAGSIZE))))
136 return err;
137
138 (*prop)->len = cpu_to_fdt32(len);
139 return 0;
140}
141
142static int _add_property(void *fdt, int nodeoffset, const char *name, int len,
143 struct fdt_property **prop)
144{
145 uint32_t tag;
146 int proplen;
147 int nextoffset;
148 int namestroff;
149 int err;
150
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400151 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
Gerald Van Baren35748172007-03-31 12:00:56 -0400152 if (tag != FDT_BEGIN_NODE)
153 return -FDT_ERR_BADOFFSET;
154
155 namestroff = _find_add_string(fdt, name);
156 if (namestroff < 0)
157 return namestroff;
158
159 *prop = _fdt_offset_ptr(fdt, nextoffset);
160 proplen = sizeof(**prop) + ALIGN(len, FDT_TAGSIZE);
161
162 err = _blob_splice_struct(fdt, *prop, 0, proplen);
163 if (err)
164 return err;
165
166 (*prop)->tag = cpu_to_fdt32(FDT_PROP);
167 (*prop)->nameoff = cpu_to_fdt32(namestroff);
168 (*prop)->len = cpu_to_fdt32(len);
169 return 0;
170}
171
172int fdt_setprop(void *fdt, int nodeoffset, const char *name,
173 const void *val, int len)
174{
175 struct fdt_property *prop;
176 int err;
177
178 if ((err = rw_check_header(fdt)))
179 return err;
180
181 err = _resize_property(fdt, nodeoffset, name, len, &prop);
182 if (err == -FDT_ERR_NOTFOUND)
183 err = _add_property(fdt, nodeoffset, name, len, &prop);
184 if (err)
185 return err;
186
187 memcpy(prop->data, val, len);
188 return 0;
189}
190
Grant Likely41bb76e2007-09-06 09:46:17 -0600191/**
192 * fdt_find_and_setprop: Find a node and set it's property
193 *
194 * @fdt: ptr to device tree
195 * @node: path of node
196 * @prop: property name
197 * @val: ptr to new value
198 * @len: length of new property value
199 * @create: flag to create the property if it doesn't exist
200 *
201 * Convenience function to directly set a property given the path to the node.
202 */
203int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
204 const void *val, int len, int create)
205{
206 int nodeoff = fdt_find_node_by_path(fdt, node);
207
208 if (nodeoff < 0)
209 return nodeoff;
210
211 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, 0) == NULL))
212 return 0; /* create flag not set; so exit quietly */
213
214 return fdt_setprop(fdt, nodeoff, prop, val, len);
215}
216
Gerald Van Baren35748172007-03-31 12:00:56 -0400217int fdt_delprop(void *fdt, int nodeoffset, const char *name)
218{
219 struct fdt_property *prop;
220 int len, proplen;
221
222 RW_CHECK_HEADER(fdt);
223
224 prop = fdt_get_property(fdt, nodeoffset, name, &len);
225 if (! prop)
226 return len;
227
228 proplen = sizeof(*prop) + ALIGN(len, FDT_TAGSIZE);
229 return _blob_splice_struct(fdt, prop, proplen, 0);
230}
231
232int fdt_add_subnode_namelen(void *fdt, int parentoffset,
233 const char *name, int namelen)
234{
235 struct fdt_node_header *nh;
236 int offset, nextoffset;
237 int nodelen;
238 int err;
239 uint32_t tag;
240 uint32_t *endtag;
241
242 RW_CHECK_HEADER(fdt);
243
244 offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
245 if (offset >= 0)
246 return -FDT_ERR_EXISTS;
247 else if (offset != -FDT_ERR_NOTFOUND)
248 return offset;
249
250 /* Try to place the new node after the parent's properties */
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400251 fdt_next_tag(fdt, parentoffset, &nextoffset, NULL); /* skip the BEGIN_NODE */
Gerald Van Baren35748172007-03-31 12:00:56 -0400252 do {
253 offset = nextoffset;
Gerald Van Baren3af0d582007-03-31 12:13:43 -0400254 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
Gerald Van Baren35748172007-03-31 12:00:56 -0400255 } while (tag == FDT_PROP);
256
257 nh = _fdt_offset_ptr(fdt, offset);
258 nodelen = sizeof(*nh) + ALIGN(namelen+1, FDT_TAGSIZE) + FDT_TAGSIZE;
259
260 err = _blob_splice_struct(fdt, nh, 0, nodelen);
261 if (err)
262 return err;
263
264 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
265 memset(nh->name, 0, ALIGN(namelen+1, FDT_TAGSIZE));
266 memcpy(nh->name, name, namelen);
267 endtag = (uint32_t *)((void *)nh + nodelen - FDT_TAGSIZE);
268 *endtag = cpu_to_fdt32(FDT_END_NODE);
269
270 return offset;
271}
272
273int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
274{
275 return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
276}
277
278int fdt_del_node(void *fdt, int nodeoffset)
279{
280 int endoffset;
281
282 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
283 if (endoffset < 0)
284 return endoffset;
285
286 return _blob_splice_struct(fdt, _fdt_offset_ptr(fdt, nodeoffset),
287 endoffset - nodeoffset, 0);
288}
289
290int fdt_open_into(void *fdt, void *buf, int bufsize)
291{
292 int err;
293
294 err = fdt_move(fdt, buf, bufsize);
295 if (err)
296 return err;
297
298 fdt = buf;
299
300 fdt_set_header(fdt, totalsize, bufsize);
301
302 /* FIXME: re-order if necessary */
303
304 err = rw_check_header(fdt);
305 if (err)
306 return err;
307
308 return 0;
309}
310
311int fdt_pack(void *fdt)
312{
313 int err;
314
315 err = rw_check_header(fdt);
316 if (err)
317 return err;
318
319 /* FIXME: pack components */
320 fdt_set_header(fdt, totalsize, _blob_data_size(fdt));
321 return 0;
322}
Gerald Van Baren8096b3b2007-04-20 22:46:53 -0400323
324#endif /* CONFIG_OF_LIBFDT */