blob: 80a321214153708b85a962a94f38186335c29a20 [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier3eaedea2013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04005 */
Robert P. J. Day55019c52016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04007
Bartlomiej Sieka61250862008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04009#include <fdt.h>
10#include <libfdt.h>
Bartlomiej Sieka61250862008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040014
15#include "libfdt_internal.h"
16
David Gibson9457d9d2008-07-09 14:10:24 +100017static int _fdt_blocks_misordered(const void *fdt,
Kumar Galac8ab7052007-10-24 11:04:22 -050018 int mem_rsv_size, int struct_size)
19{
David Gibson9457d9d2008-07-09 14:10:24 +100020 return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
Kumar Galac8ab7052007-10-24 11:04:22 -050021 || (fdt_off_dt_struct(fdt) <
22 (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
23 || (fdt_off_dt_strings(fdt) <
24 (fdt_off_dt_struct(fdt) + struct_size))
25 || (fdt_totalsize(fdt) <
26 (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
27}
28
David Gibson9457d9d2008-07-09 14:10:24 +100029static int _fdt_rw_check_header(void *fdt)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040030{
David Gibson9457d9d2008-07-09 14:10:24 +100031 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040032
Kumar Galac8ab7052007-10-24 11:04:22 -050033 if (fdt_version(fdt) < 17)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040034 return -FDT_ERR_BADVERSION;
David Gibson9457d9d2008-07-09 14:10:24 +100035 if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
36 fdt_size_dt_struct(fdt)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040037 return -FDT_ERR_BADLAYOUT;
Kumar Galac8ab7052007-10-24 11:04:22 -050038 if (fdt_version(fdt) > 17)
39 fdt_set_version(fdt, 17);
40
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040041 return 0;
42}
43
David Gibson9457d9d2008-07-09 14:10:24 +100044#define FDT_RW_CHECK_HEADER(fdt) \
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040045 { \
Simon Glass536774d2014-07-30 03:59:03 -060046 int __err; \
47 if ((__err = _fdt_rw_check_header(fdt)) != 0) \
48 return __err; \
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040049 }
50
David Gibson9457d9d2008-07-09 14:10:24 +100051static inline int _fdt_data_size(void *fdt)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040052{
53 return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
54}
55
David Gibson9457d9d2008-07-09 14:10:24 +100056static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040057{
David Gibsonb4141b82008-07-07 10:10:48 +100058 char *p = splicepoint;
David Gibson9457d9d2008-07-09 14:10:24 +100059 char *end = (char *)fdt + _fdt_data_size(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040060
61 if (((p + oldlen) < p) || ((p + oldlen) > end))
62 return -FDT_ERR_BADOFFSET;
Simon Glassdfdcd6f2016-10-02 17:59:27 -060063 if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt))
64 return -FDT_ERR_BADOFFSET;
David Gibsonb4141b82008-07-07 10:10:48 +100065 if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040066 return -FDT_ERR_NOSPACE;
67 memmove(p + newlen, p + oldlen, end - p - oldlen);
68 return 0;
69}
70
David Gibson9457d9d2008-07-09 14:10:24 +100071static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
72 int oldn, int newn)
Kumar Galac8ab7052007-10-24 11:04:22 -050073{
74 int delta = (newn - oldn) * sizeof(*p);
75 int err;
David Gibson9457d9d2008-07-09 14:10:24 +100076 err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
Kumar Galac8ab7052007-10-24 11:04:22 -050077 if (err)
78 return err;
79 fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
80 fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
81 return 0;
82}
83
David Gibson9457d9d2008-07-09 14:10:24 +100084static int _fdt_splice_struct(void *fdt, void *p,
85 int oldlen, int newlen)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040086{
87 int delta = newlen - oldlen;
88 int err;
89
David Gibson9457d9d2008-07-09 14:10:24 +100090 if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040091 return err;
92
Kumar Galac8ab7052007-10-24 11:04:22 -050093 fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
94 fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040095 return 0;
96}
97
David Gibson9457d9d2008-07-09 14:10:24 +100098static int _fdt_splice_string(void *fdt, int newlen)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040099{
David Gibsonb4141b82008-07-07 10:10:48 +1000100 void *p = (char *)fdt
101 + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400102 int err;
103
David Gibson9457d9d2008-07-09 14:10:24 +1000104 if ((err = _fdt_splice(fdt, p, 0, newlen)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400105 return err;
106
Kumar Galac8ab7052007-10-24 11:04:22 -0500107 fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400108 return 0;
109}
110
David Gibson9457d9d2008-07-09 14:10:24 +1000111static int _fdt_find_add_string(void *fdt, const char *s)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400112{
113 char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
114 const char *p;
115 char *new;
116 int len = strlen(s) + 1;
117 int err;
118
119 p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
120 if (p)
121 /* found it */
122 return (p - strtab);
123
124 new = strtab + fdt_size_dt_strings(fdt);
David Gibson9457d9d2008-07-09 14:10:24 +1000125 err = _fdt_splice_string(fdt, len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400126 if (err)
127 return err;
128
129 memcpy(new, s, len);
130 return (new - strtab);
131}
132
Kumar Galac8ab7052007-10-24 11:04:22 -0500133int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
134{
135 struct fdt_reserve_entry *re;
136 int err;
137
David Gibson9457d9d2008-07-09 14:10:24 +1000138 FDT_RW_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500139
140 re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
David Gibson9457d9d2008-07-09 14:10:24 +1000141 err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
Kumar Galac8ab7052007-10-24 11:04:22 -0500142 if (err)
143 return err;
144
145 re->address = cpu_to_fdt64(address);
146 re->size = cpu_to_fdt64(size);
147 return 0;
148}
149
150int fdt_del_mem_rsv(void *fdt, int n)
151{
152 struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
Kumar Galac8ab7052007-10-24 11:04:22 -0500153
David Gibson9457d9d2008-07-09 14:10:24 +1000154 FDT_RW_CHECK_HEADER(fdt);
David Gibson148a49b2008-05-20 17:19:11 +1000155
Kumar Galac8ab7052007-10-24 11:04:22 -0500156 if (n >= fdt_num_mem_rsv(fdt))
157 return -FDT_ERR_NOTFOUND;
158
Masahiro Yamadae7e8ba92016-09-06 22:17:37 +0900159 return _fdt_splice_mem_rsv(fdt, re, 1, 0);
Kumar Galac8ab7052007-10-24 11:04:22 -0500160}
161
David Gibson9457d9d2008-07-09 14:10:24 +1000162static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
163 int len, struct fdt_property **prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400164{
165 int oldlen;
166 int err;
167
Kumar Galac8ab7052007-10-24 11:04:22 -0500168 *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
Simon Glassdfdcd6f2016-10-02 17:59:27 -0600169 if (!*prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400170 return oldlen;
171
David Gibson9457d9d2008-07-09 14:10:24 +1000172 if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
173 FDT_TAGALIGN(len))))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400174 return err;
175
176 (*prop)->len = cpu_to_fdt32(len);
177 return 0;
178}
179
David Gibson9457d9d2008-07-09 14:10:24 +1000180static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
181 int len, struct fdt_property **prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400182{
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400183 int proplen;
184 int nextoffset;
185 int namestroff;
186 int err;
187
David Gibson148a49b2008-05-20 17:19:11 +1000188 if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
189 return nextoffset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400190
David Gibson9457d9d2008-07-09 14:10:24 +1000191 namestroff = _fdt_find_add_string(fdt, name);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400192 if (namestroff < 0)
193 return namestroff;
194
Kumar Galac8ab7052007-10-24 11:04:22 -0500195 *prop = _fdt_offset_ptr_w(fdt, nextoffset);
David Gibson9457d9d2008-07-09 14:10:24 +1000196 proplen = sizeof(**prop) + FDT_TAGALIGN(len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400197
David Gibson9457d9d2008-07-09 14:10:24 +1000198 err = _fdt_splice_struct(fdt, *prop, 0, proplen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400199 if (err)
200 return err;
201
202 (*prop)->tag = cpu_to_fdt32(FDT_PROP);
203 (*prop)->nameoff = cpu_to_fdt32(namestroff);
204 (*prop)->len = cpu_to_fdt32(len);
205 return 0;
206}
207
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100208int fdt_set_name(void *fdt, int nodeoffset, const char *name)
209{
210 char *namep;
211 int oldlen, newlen;
212 int err;
213
David Gibson9457d9d2008-07-09 14:10:24 +1000214 FDT_RW_CHECK_HEADER(fdt);
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100215
David Gibson870fa472008-07-07 10:14:15 +1000216 namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100217 if (!namep)
218 return oldlen;
219
220 newlen = strlen(name);
221
David Gibson9457d9d2008-07-09 14:10:24 +1000222 err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
223 FDT_TAGALIGN(newlen+1));
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100224 if (err)
225 return err;
226
227 memcpy(namep, name, newlen+1);
228 return 0;
229}
230
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400231int fdt_setprop(void *fdt, int nodeoffset, const char *name,
232 const void *val, int len)
233{
234 struct fdt_property *prop;
235 int err;
236
David Gibson9457d9d2008-07-09 14:10:24 +1000237 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400238
David Gibson9457d9d2008-07-09 14:10:24 +1000239 err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400240 if (err == -FDT_ERR_NOTFOUND)
David Gibson9457d9d2008-07-09 14:10:24 +1000241 err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400242 if (err)
243 return err;
244
Simon Glassdc9b2292017-03-28 10:23:31 -0600245 if (len)
246 memcpy(prop->data, val, len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400247 return 0;
248}
249
Minghuan Lian30648272012-08-27 19:38:59 -0500250int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
251 const void *val, int len)
252{
253 struct fdt_property *prop;
254 int err, oldlen, newlen;
255
256 FDT_RW_CHECK_HEADER(fdt);
257
258 prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
259 if (prop) {
260 newlen = len + oldlen;
261 err = _fdt_splice_struct(fdt, prop->data,
262 FDT_TAGALIGN(oldlen),
263 FDT_TAGALIGN(newlen));
264 if (err)
265 return err;
266 prop->len = cpu_to_fdt32(newlen);
267 memcpy(prop->data + oldlen, val, len);
268 } else {
269 err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
270 if (err)
271 return err;
272 memcpy(prop->data, val, len);
273 }
274 return 0;
275}
276
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400277int fdt_delprop(void *fdt, int nodeoffset, const char *name)
278{
279 struct fdt_property *prop;
280 int len, proplen;
281
David Gibson9457d9d2008-07-09 14:10:24 +1000282 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400283
Kumar Galac8ab7052007-10-24 11:04:22 -0500284 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Robert P. J. Day55019c52016-05-23 05:40:55 -0400285 if (!prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400286 return len;
287
David Gibson9457d9d2008-07-09 14:10:24 +1000288 proplen = sizeof(*prop) + FDT_TAGALIGN(len);
289 return _fdt_splice_struct(fdt, prop, proplen, 0);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400290}
291
292int fdt_add_subnode_namelen(void *fdt, int parentoffset,
293 const char *name, int namelen)
294{
295 struct fdt_node_header *nh;
296 int offset, nextoffset;
297 int nodelen;
298 int err;
299 uint32_t tag;
Kim Phillips38c38152013-01-16 13:59:50 +0000300 fdt32_t *endtag;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400301
David Gibson9457d9d2008-07-09 14:10:24 +1000302 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400303
304 offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
305 if (offset >= 0)
306 return -FDT_ERR_EXISTS;
307 else if (offset != -FDT_ERR_NOTFOUND)
308 return offset;
309
310 /* Try to place the new node after the parent's properties */
Kumar Galac8ab7052007-10-24 11:04:22 -0500311 fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400312 do {
313 offset = nextoffset;
Kumar Galac8ab7052007-10-24 11:04:22 -0500314 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibsone3524b12008-02-14 16:50:34 +1100315 } while ((tag == FDT_PROP) || (tag == FDT_NOP));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400316
Kumar Galac8ab7052007-10-24 11:04:22 -0500317 nh = _fdt_offset_ptr_w(fdt, offset);
David Gibson9457d9d2008-07-09 14:10:24 +1000318 nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400319
David Gibson9457d9d2008-07-09 14:10:24 +1000320 err = _fdt_splice_struct(fdt, nh, 0, nodelen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400321 if (err)
322 return err;
323
324 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
David Gibson9457d9d2008-07-09 14:10:24 +1000325 memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400326 memcpy(nh->name, name, namelen);
Kim Phillips38c38152013-01-16 13:59:50 +0000327 endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400328 *endtag = cpu_to_fdt32(FDT_END_NODE);
329
330 return offset;
331}
332
333int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
334{
335 return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
336}
337
338int fdt_del_node(void *fdt, int nodeoffset)
339{
340 int endoffset;
341
David Gibson9457d9d2008-07-09 14:10:24 +1000342 FDT_RW_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500343
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400344 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
345 if (endoffset < 0)
346 return endoffset;
347
David Gibson9457d9d2008-07-09 14:10:24 +1000348 return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
349 endoffset - nodeoffset, 0);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400350}
351
David Gibson9457d9d2008-07-09 14:10:24 +1000352static void _fdt_packblocks(const char *old, char *new,
353 int mem_rsv_size, int struct_size)
Kumar Galac8ab7052007-10-24 11:04:22 -0500354{
355 int mem_rsv_off, struct_off, strings_off;
356
David Gibson9457d9d2008-07-09 14:10:24 +1000357 mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
Kumar Galac8ab7052007-10-24 11:04:22 -0500358 struct_off = mem_rsv_off + mem_rsv_size;
359 strings_off = struct_off + struct_size;
360
David Gibsonb4141b82008-07-07 10:10:48 +1000361 memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
362 fdt_set_off_mem_rsvmap(new, mem_rsv_off);
Kumar Galac8ab7052007-10-24 11:04:22 -0500363
David Gibsonb4141b82008-07-07 10:10:48 +1000364 memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
365 fdt_set_off_dt_struct(new, struct_off);
366 fdt_set_size_dt_struct(new, struct_size);
Kumar Galac8ab7052007-10-24 11:04:22 -0500367
David Gibsonb4141b82008-07-07 10:10:48 +1000368 memmove(new + strings_off, old + fdt_off_dt_strings(old),
369 fdt_size_dt_strings(old));
370 fdt_set_off_dt_strings(new, strings_off);
371 fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
Kumar Galac8ab7052007-10-24 11:04:22 -0500372}
373
374int fdt_open_into(const void *fdt, void *buf, int bufsize)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400375{
376 int err;
Kumar Galac8ab7052007-10-24 11:04:22 -0500377 int mem_rsv_size, struct_size;
378 int newsize;
David Gibsonb4141b82008-07-07 10:10:48 +1000379 const char *fdtstart = fdt;
380 const char *fdtend = fdtstart + fdt_totalsize(fdt);
381 char *tmp;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400382
David Gibson9457d9d2008-07-09 14:10:24 +1000383 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400384
Kumar Galac8ab7052007-10-24 11:04:22 -0500385 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
386 * sizeof(struct fdt_reserve_entry);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400387
Kumar Galac8ab7052007-10-24 11:04:22 -0500388 if (fdt_version(fdt) >= 17) {
389 struct_size = fdt_size_dt_struct(fdt);
390 } else {
391 struct_size = 0;
392 while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
393 ;
David Gibson82c10232009-02-06 14:03:24 +1100394 if (struct_size < 0)
395 return struct_size;
Kumar Galac8ab7052007-10-24 11:04:22 -0500396 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400397
David Gibson9457d9d2008-07-09 14:10:24 +1000398 if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
Kumar Galac8ab7052007-10-24 11:04:22 -0500399 /* no further work necessary */
400 err = fdt_move(fdt, buf, bufsize);
401 if (err)
402 return err;
403 fdt_set_version(buf, 17);
404 fdt_set_size_dt_struct(buf, struct_size);
405 fdt_set_totalsize(buf, bufsize);
406 return 0;
407 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400408
Kumar Galac8ab7052007-10-24 11:04:22 -0500409 /* Need to reorder */
David Gibson9457d9d2008-07-09 14:10:24 +1000410 newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
Kumar Galac8ab7052007-10-24 11:04:22 -0500411 + struct_size + fdt_size_dt_strings(fdt);
412
413 if (bufsize < newsize)
414 return -FDT_ERR_NOSPACE;
415
David Gibsonb4141b82008-07-07 10:10:48 +1000416 /* First attempt to build converted tree at beginning of buffer */
417 tmp = buf;
418 /* But if that overlaps with the old tree... */
419 if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
420 /* Try right after the old tree instead */
David Gibson870fa472008-07-07 10:14:15 +1000421 tmp = (char *)(uintptr_t)fdtend;
David Gibsonb4141b82008-07-07 10:10:48 +1000422 if ((tmp + newsize) > ((char *)buf + bufsize))
Kumar Galac8ab7052007-10-24 11:04:22 -0500423 return -FDT_ERR_NOSPACE;
424 }
425
David Gibson9457d9d2008-07-09 14:10:24 +1000426 _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
Kumar Galac8ab7052007-10-24 11:04:22 -0500427 memmove(buf, tmp, newsize);
428
429 fdt_set_magic(buf, FDT_MAGIC);
430 fdt_set_totalsize(buf, bufsize);
431 fdt_set_version(buf, 17);
432 fdt_set_last_comp_version(buf, 16);
433 fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400434
435 return 0;
436}
437
438int fdt_pack(void *fdt)
439{
Kumar Galac8ab7052007-10-24 11:04:22 -0500440 int mem_rsv_size;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400441
David Gibson9457d9d2008-07-09 14:10:24 +1000442 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400443
Kumar Galac8ab7052007-10-24 11:04:22 -0500444 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
445 * sizeof(struct fdt_reserve_entry);
David Gibson9457d9d2008-07-09 14:10:24 +1000446 _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
447 fdt_set_totalsize(fdt, _fdt_data_size(fdt));
Kumar Galac8ab7052007-10-24 11:04:22 -0500448
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400449 return 0;
450}
Simon Glasse2976fe2015-06-23 15:38:26 -0600451
452int fdt_remove_unused_strings(const void *old, void *new)
453{
454 const struct fdt_property *old_prop;
455 struct fdt_property *new_prop;
456 int size = fdt_totalsize(old);
457 int next_offset, offset;
458 const char *str;
459 int ret;
460 int tag = FDT_PROP;
461
462 /* Make a copy and remove the strings */
463 memcpy(new, old, size);
464 fdt_set_size_dt_strings(new, 0);
465
466 /* Add every property name back into the new string table */
467 for (offset = 0; tag != FDT_END; offset = next_offset) {
468 tag = fdt_next_tag(old, offset, &next_offset);
469 if (tag != FDT_PROP)
470 continue;
471 old_prop = fdt_get_property_by_offset(old, offset, NULL);
472 new_prop = (struct fdt_property *)(unsigned long)
473 fdt_get_property_by_offset(new, offset, NULL);
474 str = fdt_string(old, fdt32_to_cpu(old_prop->nameoff));
475 ret = _fdt_find_add_string(new, str);
476 if (ret < 0)
477 return ret;
478 new_prop->nameoff = cpu_to_fdt32(ret);
479 }
480
481 return 0;
482}