56#include "libfdt_internal.h"
63 if (fdt_magic (fdt) != FDT_SW_MAGIC) {
64 return -FDT_ERR_BADMAGIC;
71#define FDT_SW_CHECK_HEADER(fdt) \
74 if ((err = _fdt_sw_check_header(fdt)) != 0) \
84 int offset = fdt_size_dt_struct (fdt);
87 spaceleft = fdt_totalsize (fdt) - fdt_off_dt_struct (fdt)
88 - fdt_size_dt_strings (fdt);
90 if ((offset + len < offset) || (offset + len > spaceleft)) {
94 fdt_set_size_dt_struct (fdt, offset + len);
95 return _fdt_offset_ptr_w (fdt, offset);
107 return -FDT_ERR_NOSPACE;
110 memset (buf, 0, bufsize);
112 fdt_set_magic (fdt, FDT_SW_MAGIC);
113 fdt_set_version (fdt, FDT_LAST_SUPPORTED_VERSION);
114 fdt_set_last_comp_version (fdt, FDT_FIRST_SUPPORTED_VERSION);
115 fdt_set_totalsize (fdt, bufsize);
117 fdt_set_off_mem_rsvmap (
124 fdt_set_off_dt_struct (fdt, fdt_off_mem_rsvmap (fdt));
125 fdt_set_off_dt_strings (fdt, bufsize);
137 size_t headsize, tailsize;
138 char *oldtail, *newtail;
140 FDT_SW_CHECK_HEADER (fdt);
142 headsize = fdt_off_dt_struct (fdt);
143 tailsize = fdt_size_dt_strings (fdt);
145 if ((headsize + tailsize) > bufsize) {
146 return -FDT_ERR_NOSPACE;
149 oldtail = (
char *)fdt + fdt_totalsize (fdt) - tailsize;
150 newtail = (
char *)buf + bufsize - tailsize;
155 memmove (buf, fdt, headsize);
156 memmove (newtail, oldtail, tailsize);
158 memmove (newtail, oldtail, tailsize);
159 memmove (buf, fdt, headsize);
162 fdt_set_off_dt_strings (buf, bufsize);
163 fdt_set_totalsize (buf, bufsize);
169fdt_add_reservemap_entry (
178 FDT_SW_CHECK_HEADER (fdt);
180 if (fdt_size_dt_struct (fdt)) {
181 return -FDT_ERR_BADSTATE;
184 offset = fdt_off_dt_struct (fdt);
185 if ((offset +
sizeof (*re)) > fdt_totalsize (fdt)) {
186 return -FDT_ERR_NOSPACE;
190 re->address = cpu_to_fdt64 (addr);
191 re->size = cpu_to_fdt64 (size);
193 fdt_set_off_dt_struct (fdt, offset +
sizeof (*re));
199fdt_finish_reservemap (
203 return fdt_add_reservemap_entry (fdt, 0, 0);
213 int namelen = strlen (name) + 1;
215 FDT_SW_CHECK_HEADER (fdt);
217 nh = _fdt_grab_space (fdt,
sizeof (*nh) + FDT_TAGALIGN (namelen));
219 return -FDT_ERR_NOSPACE;
222 nh->tag = cpu_to_fdt32 (FDT_BEGIN_NODE);
223 memcpy (nh->name, name, namelen);
234 FDT_SW_CHECK_HEADER (fdt);
236 en = _fdt_grab_space (fdt, FDT_TAGSIZE);
238 return -FDT_ERR_NOSPACE;
241 *en = cpu_to_fdt32 (FDT_END_NODE);
246_fdt_find_add_string (
251 char *strtab = (
char *)fdt + fdt_totalsize (fdt);
253 int strtabsize = fdt_size_dt_strings (fdt);
254 int len = strlen (s) + 1;
255 int struct_top, offset;
257 p = _fdt_find_string (strtab - strtabsize, strtabsize, s);
263 offset = -strtabsize - len;
264 struct_top = fdt_off_dt_struct (fdt) + fdt_size_dt_struct (fdt);
265 if (fdt_totalsize (fdt) + offset < struct_top) {
269 memcpy (strtab + offset, s, len);
270 fdt_set_size_dt_strings (fdt, strtabsize + len);
275fdt_property_placeholder (
285 FDT_SW_CHECK_HEADER (fdt);
287 nameoff = _fdt_find_add_string (fdt, name);
289 return -FDT_ERR_NOSPACE;
292 prop = _fdt_grab_space (fdt,
sizeof (*prop) + FDT_TAGALIGN (len));
294 return -FDT_ERR_NOSPACE;
297 prop->tag = cpu_to_fdt32 (FDT_PROP);
298 prop->nameoff = cpu_to_fdt32 (nameoff);
299 prop->len = cpu_to_fdt32 (len);
315 ret = fdt_property_placeholder (fdt, name, len, &ptr);
320 memcpy (ptr, val, len);
329 char *p = (
char *)fdt;
331 int oldstroffset, newstroffset;
333 int offset, nextoffset;
335 FDT_SW_CHECK_HEADER (fdt);
338 end = _fdt_grab_space (fdt,
sizeof (*end));
340 return -FDT_ERR_NOSPACE;
343 *end = cpu_to_fdt32 (FDT_END);
346 oldstroffset = fdt_totalsize (fdt) - fdt_size_dt_strings (fdt);
347 newstroffset = fdt_off_dt_struct (fdt) + fdt_size_dt_struct (fdt);
348 memmove (p + newstroffset, p + oldstroffset, fdt_size_dt_strings (fdt));
349 fdt_set_off_dt_strings (fdt, newstroffset);
353 while ((tag = fdt_next_tag (fdt, offset, &nextoffset)) != FDT_END) {
354 if (tag == FDT_PROP) {
356 _fdt_offset_ptr_w (fdt, offset);
359 nameoff = fdt32_to_cpu (prop->nameoff);
360 nameoff += fdt_size_dt_strings (fdt);
361 prop->nameoff = cpu_to_fdt32 (nameoff);
367 if (nextoffset < 0) {
372 fdt_set_totalsize (fdt, newstroffset + fdt_size_dt_strings (fdt));
373 fdt_set_magic (fdt, FDT_MAGIC);