2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
6 #include <libfdt_env.h>
10 #include "libfdt_internal.h"
12 static int _fdt_sw_check_header(void *fdt)
14 if (fdt_magic(fdt) != FDT_SW_MAGIC)
15 return -FDT_ERR_BADMAGIC;
16 /* FIXME: should check more details about the header state */
20 #define FDT_SW_CHECK_HEADER(fdt) \
23 if ((err = _fdt_sw_check_header(fdt)) != 0) \
27 static void *_fdt_grab_space(void *fdt, size_t len)
29 int offset = fdt_size_dt_struct(fdt);
32 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
33 - fdt_size_dt_strings(fdt);
35 if ((offset + len < offset) || (offset + len > spaceleft))
38 fdt_set_size_dt_struct(fdt, offset + len);
39 return _fdt_offset_ptr_w(fdt, offset);
42 int fdt_create(void *buf, int bufsize)
46 if (bufsize < sizeof(struct fdt_header))
47 return -FDT_ERR_NOSPACE;
49 memset(buf, 0, bufsize);
51 fdt_set_magic(fdt, FDT_SW_MAGIC);
52 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
53 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
54 fdt_set_totalsize(fdt, bufsize);
56 fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
57 sizeof(struct fdt_reserve_entry)));
58 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
59 fdt_set_off_dt_strings(fdt, bufsize);
64 int fdt_resize(void *fdt, void *buf, int bufsize)
66 size_t headsize, tailsize;
67 char *oldtail, *newtail;
69 FDT_SW_CHECK_HEADER(fdt);
71 headsize = fdt_off_dt_struct(fdt);
72 tailsize = fdt_size_dt_strings(fdt);
74 if ((headsize + tailsize) > bufsize)
75 return -FDT_ERR_NOSPACE;
77 oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
78 newtail = (char *)buf + bufsize - tailsize;
80 /* Two cases to avoid clobbering data if the old and new
81 * buffers partially overlap */
83 memmove(buf, fdt, headsize);
84 memmove(newtail, oldtail, tailsize);
86 memmove(newtail, oldtail, tailsize);
87 memmove(buf, fdt, headsize);
90 fdt_set_off_dt_strings(buf, bufsize);
91 fdt_set_totalsize(buf, bufsize);
96 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
98 struct fdt_reserve_entry *re;
101 FDT_SW_CHECK_HEADER(fdt);
103 if (fdt_size_dt_struct(fdt))
104 return -FDT_ERR_BADSTATE;
106 offset = fdt_off_dt_struct(fdt);
107 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
108 return -FDT_ERR_NOSPACE;
110 re = (struct fdt_reserve_entry *)((char *)fdt + offset);
111 re->address = cpu_to_fdt64(addr);
112 re->size = cpu_to_fdt64(size);
114 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
119 int fdt_finish_reservemap(void *fdt)
121 return fdt_add_reservemap_entry(fdt, 0, 0);
124 int fdt_begin_node(void *fdt, const char *name)
126 struct fdt_node_header *nh;
127 int namelen = strlen(name) + 1;
129 FDT_SW_CHECK_HEADER(fdt);
131 nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
133 return -FDT_ERR_NOSPACE;
135 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
136 memcpy(nh->name, name, namelen);
140 int fdt_end_node(void *fdt)
144 FDT_SW_CHECK_HEADER(fdt);
146 en = _fdt_grab_space(fdt, FDT_TAGSIZE);
148 return -FDT_ERR_NOSPACE;
150 *en = cpu_to_fdt32(FDT_END_NODE);
154 static int _fdt_find_add_string(void *fdt, const char *s)
156 char *strtab = (char *)fdt + fdt_totalsize(fdt);
158 int strtabsize = fdt_size_dt_strings(fdt);
159 int len = strlen(s) + 1;
160 int struct_top, offset;
162 p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
167 offset = -strtabsize - len;
168 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
169 if (fdt_totalsize(fdt) + offset < struct_top)
170 return 0; /* no more room :( */
172 memcpy(strtab + offset, s, len);
173 fdt_set_size_dt_strings(fdt, strtabsize + len);
177 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
179 struct fdt_property *prop;
182 FDT_SW_CHECK_HEADER(fdt);
184 nameoff = _fdt_find_add_string(fdt, name);
186 return -FDT_ERR_NOSPACE;
188 prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
190 return -FDT_ERR_NOSPACE;
192 prop->tag = cpu_to_fdt32(FDT_PROP);
193 prop->nameoff = cpu_to_fdt32(nameoff);
194 prop->len = cpu_to_fdt32(len);
199 int fdt_property(void *fdt, const char *name, const void *val, int len)
204 ret = fdt_property_placeholder(fdt, name, len, &ptr);
207 memcpy(ptr, val, len);
211 int fdt_finish(void *fdt)
213 char *p = (char *)fdt;
215 int oldstroffset, newstroffset;
217 int offset, nextoffset;
219 FDT_SW_CHECK_HEADER(fdt);
222 end = _fdt_grab_space(fdt, sizeof(*end));
224 return -FDT_ERR_NOSPACE;
225 *end = cpu_to_fdt32(FDT_END);
227 /* Relocate the string table */
228 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
229 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
230 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
231 fdt_set_off_dt_strings(fdt, newstroffset);
233 /* Walk the structure, correcting string offsets */
235 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
236 if (tag == FDT_PROP) {
237 struct fdt_property *prop =
238 _fdt_offset_ptr_w(fdt, offset);
241 nameoff = fdt32_to_cpu(prop->nameoff);
242 nameoff += fdt_size_dt_strings(fdt);
243 prop->nameoff = cpu_to_fdt32(nameoff);
250 /* Finally, adjust the header */
251 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
252 fdt_set_magic(fdt, FDT_MAGIC);