2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 * libfdt is dual licensed: you can use it either under the terms of
6 * the GPL, or the BSD license, at your option.
8 * a) This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public
19 * License along with this library; if not, write to the Free
20 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
25 * b) Redistribution and use in source and binary forms, with or
26 * without modification, are permitted provided that the following
29 * 1. Redistributions of source code must retain the above
30 * copyright notice, this list of conditions and the following
32 * 2. Redistributions in binary form must reproduce the above
33 * copyright notice, this list of conditions and the following
34 * disclaimer in the documentation and/or other materials
35 * provided with the distribution.
37 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51 #include "libfdt_env.h"
56 #include "libfdt_internal.h"
58 static int fdt_sw_probe_(void *fdt)
60 if (fdt_magic(fdt) == FDT_MAGIC)
61 return -FDT_ERR_BADSTATE;
62 else if (fdt_magic(fdt) != FDT_SW_MAGIC)
63 return -FDT_ERR_BADMAGIC;
67 #define FDT_SW_PROBE(fdt) \
70 if ((err = fdt_sw_probe_(fdt)) != 0) \
74 /* 'memrsv' state: Initial state after fdt_create()
77 * fdt_add_reservmap_entry()
78 * fdt_finish_reservemap() [moves to 'struct' state]
80 static int fdt_sw_probe_memrsv_(void *fdt)
82 int err = fdt_sw_probe_(fdt);
86 if (fdt_off_dt_strings(fdt) != 0)
87 return -FDT_ERR_BADSTATE;
91 #define FDT_SW_PROBE_MEMRSV(fdt) \
94 if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
98 /* 'struct' state: Enter this state after fdt_finish_reservemap()
104 * fdt_finish() [moves to 'complete' state]
106 static int fdt_sw_probe_struct_(void *fdt)
108 int err = fdt_sw_probe_(fdt);
112 if (fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
113 return -FDT_ERR_BADSTATE;
117 #define FDT_SW_PROBE_STRUCT(fdt) \
120 if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
124 static inline uint32_t sw_flags(void *fdt)
126 /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
127 return fdt_last_comp_version(fdt);
130 /* 'complete' state: Enter this state after fdt_finish()
132 * Allowed functions: none
135 static void *fdt_grab_space_(void *fdt, size_t len)
137 int offset = fdt_size_dt_struct(fdt);
140 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
141 - fdt_size_dt_strings(fdt);
143 if ((offset + len < offset) || (offset + len > spaceleft))
146 fdt_set_size_dt_struct(fdt, offset + len);
147 return fdt_offset_ptr_w_(fdt, offset);
150 int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
152 const size_t hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
153 sizeof(struct fdt_reserve_entry));
156 if (bufsize < hdrsize)
157 return -FDT_ERR_NOSPACE;
159 if (flags & ~FDT_CREATE_FLAGS_ALL)
160 return -FDT_ERR_BADFLAGS;
162 memset(buf, 0, bufsize);
165 * magic and last_comp_version keep intermediate state during the fdt
166 * creation process, which is replaced with the proper FDT format by
169 * flags should be accessed with sw_flags().
171 fdt_set_magic(fdt, FDT_SW_MAGIC);
172 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
173 fdt_set_last_comp_version(fdt, flags);
175 fdt_set_totalsize(fdt, bufsize);
177 fdt_set_off_mem_rsvmap(fdt, hdrsize);
178 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
179 fdt_set_off_dt_strings(fdt, 0);
184 int fdt_create(void *buf, int bufsize)
186 return fdt_create_with_flags(buf, bufsize, 0);
189 int fdt_resize(void *fdt, void *buf, int bufsize)
191 size_t headsize, tailsize;
192 char *oldtail, *newtail;
196 headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
197 tailsize = fdt_size_dt_strings(fdt);
199 if ((headsize + tailsize) > fdt_totalsize(fdt))
200 return -FDT_ERR_INTERNAL;
202 if ((headsize + tailsize) > bufsize)
203 return -FDT_ERR_NOSPACE;
205 oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
206 newtail = (char *)buf + bufsize - tailsize;
208 /* Two cases to avoid clobbering data if the old and new
209 * buffers partially overlap */
211 memmove(buf, fdt, headsize);
212 memmove(newtail, oldtail, tailsize);
214 memmove(newtail, oldtail, tailsize);
215 memmove(buf, fdt, headsize);
218 fdt_set_totalsize(buf, bufsize);
219 if (fdt_off_dt_strings(buf))
220 fdt_set_off_dt_strings(buf, bufsize);
225 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
227 struct fdt_reserve_entry *re;
230 FDT_SW_PROBE_MEMRSV(fdt);
232 offset = fdt_off_dt_struct(fdt);
233 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
234 return -FDT_ERR_NOSPACE;
236 re = (struct fdt_reserve_entry *)((char *)fdt + offset);
237 re->address = cpu_to_fdt64(addr);
238 re->size = cpu_to_fdt64(size);
240 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
245 int fdt_finish_reservemap(void *fdt)
247 int err = fdt_add_reservemap_entry(fdt, 0, 0);
252 fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
256 int fdt_begin_node(void *fdt, const char *name)
258 struct fdt_node_header *nh;
261 FDT_SW_PROBE_STRUCT(fdt);
263 namelen = strlen(name) + 1;
264 nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
266 return -FDT_ERR_NOSPACE;
268 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
269 memcpy(nh->name, name, namelen);
273 int fdt_end_node(void *fdt)
277 FDT_SW_PROBE_STRUCT(fdt);
279 en = fdt_grab_space_(fdt, FDT_TAGSIZE);
281 return -FDT_ERR_NOSPACE;
283 *en = cpu_to_fdt32(FDT_END_NODE);
287 static int fdt_add_string_(void *fdt, const char *s)
289 char *strtab = (char *)fdt + fdt_totalsize(fdt);
290 int strtabsize = fdt_size_dt_strings(fdt);
291 int len = strlen(s) + 1;
292 int struct_top, offset;
294 offset = -strtabsize - len;
295 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
296 if (fdt_totalsize(fdt) + offset < struct_top)
297 return 0; /* no more room :( */
299 memcpy(strtab + offset, s, len);
300 fdt_set_size_dt_strings(fdt, strtabsize + len);
304 /* Must only be used to roll back in case of error */
305 static void fdt_del_last_string_(void *fdt, const char *s)
307 int strtabsize = fdt_size_dt_strings(fdt);
308 int len = strlen(s) + 1;
310 fdt_set_size_dt_strings(fdt, strtabsize - len);
313 static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
315 char *strtab = (char *)fdt + fdt_totalsize(fdt);
316 int strtabsize = fdt_size_dt_strings(fdt);
321 p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
327 return fdt_add_string_(fdt, s);
330 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
332 struct fdt_property *prop;
336 FDT_SW_PROBE_STRUCT(fdt);
338 /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
339 if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
341 nameoff = fdt_add_string_(fdt, name);
343 nameoff = fdt_find_add_string_(fdt, name, &allocated);
346 return -FDT_ERR_NOSPACE;
348 prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
351 fdt_del_last_string_(fdt, name);
352 return -FDT_ERR_NOSPACE;
355 prop->tag = cpu_to_fdt32(FDT_PROP);
356 prop->nameoff = cpu_to_fdt32(nameoff);
357 prop->len = cpu_to_fdt32(len);
362 int fdt_property(void *fdt, const char *name, const void *val, int len)
367 ret = fdt_property_placeholder(fdt, name, len, &ptr);
370 memcpy(ptr, val, len);
374 int fdt_finish(void *fdt)
376 char *p = (char *)fdt;
378 int oldstroffset, newstroffset;
380 int offset, nextoffset;
382 FDT_SW_PROBE_STRUCT(fdt);
385 end = fdt_grab_space_(fdt, sizeof(*end));
387 return -FDT_ERR_NOSPACE;
388 *end = cpu_to_fdt32(FDT_END);
390 /* Relocate the string table */
391 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
392 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
393 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
394 fdt_set_off_dt_strings(fdt, newstroffset);
396 /* Walk the structure, correcting string offsets */
398 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
399 if (tag == FDT_PROP) {
400 struct fdt_property *prop =
401 fdt_offset_ptr_w_(fdt, offset);
404 nameoff = fdt32_to_cpu(prop->nameoff);
405 nameoff += fdt_size_dt_strings(fdt);
406 prop->nameoff = cpu_to_fdt32(nameoff);
413 /* Finally, adjust the header */
414 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
416 /* And fix up fields that were keeping intermediate state. */
417 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
418 fdt_set_magic(fdt, FDT_MAGIC);