blob: 93e4a2b563486de74eff03b2051ac1ecc708b7fe [file] [log] [blame]
Rob Herring12869ec2019-06-21 08:18:32 -06001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
David Gibson1cade992007-12-10 14:28:39 +11002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
David Gibson1cade992007-12-10 14:28:39 +11005 */
6#include "libfdt_env.h"
7
8#include <fdt.h>
9#include <libfdt.h>
10
11#include "libfdt_internal.h"
12
Rob Herring9130ba82018-02-27 17:40:38 -060013static int fdt_blocks_misordered_(const void *fdt,
14 int mem_rsv_size, int struct_size)
David Gibson1cade992007-12-10 14:28:39 +110015{
David Gibsoned95d742008-08-07 12:24:17 +100016 return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
David Gibson1cade992007-12-10 14:28:39 +110017 || (fdt_off_dt_struct(fdt) <
18 (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
19 || (fdt_off_dt_strings(fdt) <
20 (fdt_off_dt_struct(fdt) + struct_size))
21 || (fdt_totalsize(fdt) <
22 (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
23}
24
Rob Herringf8589272018-09-13 08:59:25 -050025static int fdt_rw_probe_(void *fdt)
David Gibson1cade992007-12-10 14:28:39 +110026{
Rob Herringd047cd82020-03-13 08:56:58 -050027 if (can_assume(VALID_DTB))
28 return 0;
Rob Herringf8589272018-09-13 08:59:25 -050029 FDT_RO_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +110030
Rob Herringd047cd82020-03-13 08:56:58 -050031 if (!can_assume(LATEST) && fdt_version(fdt) < 17)
David Gibson1cade992007-12-10 14:28:39 +110032 return -FDT_ERR_BADVERSION;
Rob Herring9130ba82018-02-27 17:40:38 -060033 if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
David Gibsoned95d742008-08-07 12:24:17 +100034 fdt_size_dt_struct(fdt)))
David Gibson1cade992007-12-10 14:28:39 +110035 return -FDT_ERR_BADLAYOUT;
Rob Herringd047cd82020-03-13 08:56:58 -050036 if (!can_assume(LATEST) && fdt_version(fdt) > 17)
David Gibson1cade992007-12-10 14:28:39 +110037 fdt_set_version(fdt, 17);
38
39 return 0;
40}
41
Rob Herringf8589272018-09-13 08:59:25 -050042#define FDT_RW_PROBE(fdt) \
David Gibson1cade992007-12-10 14:28:39 +110043 { \
Rob Herring9130ba82018-02-27 17:40:38 -060044 int err_; \
Rob Herringf8589272018-09-13 08:59:25 -050045 if ((err_ = fdt_rw_probe_(fdt)) != 0) \
Rob Herring9130ba82018-02-27 17:40:38 -060046 return err_; \
David Gibson1cade992007-12-10 14:28:39 +110047 }
48
Rob Herringd047cd82020-03-13 08:56:58 -050049static inline unsigned int fdt_data_size_(void *fdt)
David Gibson1cade992007-12-10 14:28:39 +110050{
51 return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
52}
53
Rob Herring9130ba82018-02-27 17:40:38 -060054static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
David Gibson1cade992007-12-10 14:28:39 +110055{
David Gibsoned95d742008-08-07 12:24:17 +100056 char *p = splicepoint;
Rob Herringd047cd82020-03-13 08:56:58 -050057 unsigned int dsize = fdt_data_size_(fdt);
58 size_t soff = p - (char *)fdt;
David Gibson1cade992007-12-10 14:28:39 +110059
Rob Herringd047cd82020-03-13 08:56:58 -050060 if ((oldlen < 0) || (soff + oldlen < soff) || (soff + oldlen > dsize))
David Gibson1cade992007-12-10 14:28:39 +110061 return -FDT_ERR_BADOFFSET;
Rob Herringd047cd82020-03-13 08:56:58 -050062 if ((p < (char *)fdt) || (dsize + newlen < oldlen))
Rob Herring91feabc2016-01-26 09:04:11 -060063 return -FDT_ERR_BADOFFSET;
Rob Herringd047cd82020-03-13 08:56:58 -050064 if (dsize - oldlen + newlen > fdt_totalsize(fdt))
David Gibson1cade992007-12-10 14:28:39 +110065 return -FDT_ERR_NOSPACE;
Rob Herringd047cd82020-03-13 08:56:58 -050066 memmove(p + newlen, p + oldlen, ((char *)fdt + dsize) - (p + oldlen));
David Gibson1cade992007-12-10 14:28:39 +110067 return 0;
68}
69
Rob Herring9130ba82018-02-27 17:40:38 -060070static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
David Gibsoned95d742008-08-07 12:24:17 +100071 int oldn, int newn)
David Gibson1cade992007-12-10 14:28:39 +110072{
73 int delta = (newn - oldn) * sizeof(*p);
74 int err;
Rob Herring9130ba82018-02-27 17:40:38 -060075 err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
David Gibson1cade992007-12-10 14:28:39 +110076 if (err)
77 return err;
78 fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
79 fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
80 return 0;
81}
82
Rob Herring9130ba82018-02-27 17:40:38 -060083static int fdt_splice_struct_(void *fdt, void *p,
David Gibsoned95d742008-08-07 12:24:17 +100084 int oldlen, int newlen)
David Gibson1cade992007-12-10 14:28:39 +110085{
86 int delta = newlen - oldlen;
87 int err;
88
Rob Herring9130ba82018-02-27 17:40:38 -060089 if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
David Gibson1cade992007-12-10 14:28:39 +110090 return err;
91
92 fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
93 fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
94 return 0;
95}
96
Rob Herring9bb9c6a2019-06-12 07:05:52 -060097/* Must only be used to roll back in case of error */
98static void fdt_del_last_string_(void *fdt, const char *s)
99{
100 int newlen = strlen(s) + 1;
101
102 fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
103}
104
Rob Herring9130ba82018-02-27 17:40:38 -0600105static int fdt_splice_string_(void *fdt, int newlen)
David Gibson1cade992007-12-10 14:28:39 +1100106{
David Gibsoned95d742008-08-07 12:24:17 +1000107 void *p = (char *)fdt
108 + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100109 int err;
110
Rob Herring9130ba82018-02-27 17:40:38 -0600111 if ((err = fdt_splice_(fdt, p, 0, newlen)))
David Gibson1cade992007-12-10 14:28:39 +1100112 return err;
113
114 fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
115 return 0;
116}
117
Rob Herringd047cd82020-03-13 08:56:58 -0500118/**
119 * fdt_find_add_string_() - Find or allocate a string
120 *
121 * @fdt: pointer to the device tree to check/adjust
122 * @s: string to find/add
123 * @allocated: Set to 0 if the string was found, 1 if not found and so
124 * allocated. Ignored if can_assume(NO_ROLLBACK)
125 * @return offset of string in the string table (whether found or added)
126 */
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600127static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
David Gibson1cade992007-12-10 14:28:39 +1100128{
129 char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
130 const char *p;
131 char *new;
132 int len = strlen(s) + 1;
133 int err;
134
Rob Herringd047cd82020-03-13 08:56:58 -0500135 if (!can_assume(NO_ROLLBACK))
136 *allocated = 0;
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600137
Rob Herring9130ba82018-02-27 17:40:38 -0600138 p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
David Gibson1cade992007-12-10 14:28:39 +1100139 if (p)
140 /* found it */
141 return (p - strtab);
142
143 new = strtab + fdt_size_dt_strings(fdt);
Rob Herring9130ba82018-02-27 17:40:38 -0600144 err = fdt_splice_string_(fdt, len);
David Gibson1cade992007-12-10 14:28:39 +1100145 if (err)
146 return err;
147
Rob Herringd047cd82020-03-13 08:56:58 -0500148 if (!can_assume(NO_ROLLBACK))
149 *allocated = 1;
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600150
David Gibson1cade992007-12-10 14:28:39 +1100151 memcpy(new, s, len);
152 return (new - strtab);
153}
154
155int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
156{
157 struct fdt_reserve_entry *re;
158 int err;
159
Rob Herringf8589272018-09-13 08:59:25 -0500160 FDT_RW_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100161
Rob Herring9130ba82018-02-27 17:40:38 -0600162 re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
163 err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
David Gibson1cade992007-12-10 14:28:39 +1100164 if (err)
165 return err;
166
167 re->address = cpu_to_fdt64(address);
168 re->size = cpu_to_fdt64(size);
169 return 0;
170}
171
172int fdt_del_mem_rsv(void *fdt, int n)
173{
Rob Herring9130ba82018-02-27 17:40:38 -0600174 struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
David Gibson1cade992007-12-10 14:28:39 +1100175
Rob Herringf8589272018-09-13 08:59:25 -0500176 FDT_RW_PROBE(fdt);
David Gibsoned95d742008-08-07 12:24:17 +1000177
David Gibson1cade992007-12-10 14:28:39 +1100178 if (n >= fdt_num_mem_rsv(fdt))
179 return -FDT_ERR_NOTFOUND;
180
Rob Herring9130ba82018-02-27 17:40:38 -0600181 return fdt_splice_mem_rsv_(fdt, re, 1, 0);
David Gibson1cade992007-12-10 14:28:39 +1100182}
183
Rob Herring9130ba82018-02-27 17:40:38 -0600184static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
David Gibsoned95d742008-08-07 12:24:17 +1000185 int len, struct fdt_property **prop)
David Gibson1cade992007-12-10 14:28:39 +1100186{
187 int oldlen;
188 int err;
189
190 *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
Rob Herring4201d052017-10-03 11:37:04 -0500191 if (!*prop)
David Gibson1cade992007-12-10 14:28:39 +1100192 return oldlen;
193
Rob Herring9130ba82018-02-27 17:40:38 -0600194 if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
David Gibsoned95d742008-08-07 12:24:17 +1000195 FDT_TAGALIGN(len))))
David Gibson1cade992007-12-10 14:28:39 +1100196 return err;
197
198 (*prop)->len = cpu_to_fdt32(len);
199 return 0;
200}
201
Rob Herring9130ba82018-02-27 17:40:38 -0600202static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
David Gibsoned95d742008-08-07 12:24:17 +1000203 int len, struct fdt_property **prop)
David Gibson1cade992007-12-10 14:28:39 +1100204{
David Gibson1cade992007-12-10 14:28:39 +1100205 int proplen;
206 int nextoffset;
207 int namestroff;
208 int err;
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600209 int allocated;
David Gibson1cade992007-12-10 14:28:39 +1100210
Rob Herring9130ba82018-02-27 17:40:38 -0600211 if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
David Gibsoned95d742008-08-07 12:24:17 +1000212 return nextoffset;
David Gibson1cade992007-12-10 14:28:39 +1100213
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600214 namestroff = fdt_find_add_string_(fdt, name, &allocated);
David Gibson1cade992007-12-10 14:28:39 +1100215 if (namestroff < 0)
216 return namestroff;
217
Rob Herring9130ba82018-02-27 17:40:38 -0600218 *prop = fdt_offset_ptr_w_(fdt, nextoffset);
David Gibsoned95d742008-08-07 12:24:17 +1000219 proplen = sizeof(**prop) + FDT_TAGALIGN(len);
David Gibson1cade992007-12-10 14:28:39 +1100220
Rob Herring9130ba82018-02-27 17:40:38 -0600221 err = fdt_splice_struct_(fdt, *prop, 0, proplen);
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600222 if (err) {
Rob Herringd047cd82020-03-13 08:56:58 -0500223 /* Delete the string if we failed to add it */
224 if (!can_assume(NO_ROLLBACK) && allocated)
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600225 fdt_del_last_string_(fdt, name);
David Gibson1cade992007-12-10 14:28:39 +1100226 return err;
Rob Herring9bb9c6a2019-06-12 07:05:52 -0600227 }
David Gibson1cade992007-12-10 14:28:39 +1100228
229 (*prop)->tag = cpu_to_fdt32(FDT_PROP);
230 (*prop)->nameoff = cpu_to_fdt32(namestroff);
231 (*prop)->len = cpu_to_fdt32(len);
232 return 0;
233}
234
David Gibsoned95d742008-08-07 12:24:17 +1000235int fdt_set_name(void *fdt, int nodeoffset, const char *name)
236{
237 char *namep;
238 int oldlen, newlen;
239 int err;
240
Rob Herringf8589272018-09-13 08:59:25 -0500241 FDT_RW_PROBE(fdt);
David Gibsoned95d742008-08-07 12:24:17 +1000242
243 namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
244 if (!namep)
245 return oldlen;
246
247 newlen = strlen(name);
248
Rob Herring9130ba82018-02-27 17:40:38 -0600249 err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
David Gibsoned95d742008-08-07 12:24:17 +1000250 FDT_TAGALIGN(newlen+1));
251 if (err)
252 return err;
253
254 memcpy(namep, name, newlen+1);
255 return 0;
256}
257
Rob Herring4201d052017-10-03 11:37:04 -0500258int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
259 int len, void **prop_data)
David Gibson1cade992007-12-10 14:28:39 +1100260{
261 struct fdt_property *prop;
262 int err;
263
Rob Herringf8589272018-09-13 08:59:25 -0500264 FDT_RW_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100265
Rob Herring9130ba82018-02-27 17:40:38 -0600266 err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
David Gibson1cade992007-12-10 14:28:39 +1100267 if (err == -FDT_ERR_NOTFOUND)
Rob Herring9130ba82018-02-27 17:40:38 -0600268 err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
David Gibson1cade992007-12-10 14:28:39 +1100269 if (err)
270 return err;
271
Rob Herring4201d052017-10-03 11:37:04 -0500272 *prop_data = prop->data;
273 return 0;
274}
275
276int fdt_setprop(void *fdt, int nodeoffset, const char *name,
277 const void *val, int len)
278{
279 void *prop_data;
280 int err;
281
282 err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
283 if (err)
284 return err;
285
Rob Herring89d12312017-03-21 09:01:08 -0500286 if (len)
Rob Herring4201d052017-10-03 11:37:04 -0500287 memcpy(prop_data, val, len);
David Gibson1cade992007-12-10 14:28:39 +1100288 return 0;
289}
290
Stephen Warrencd296722012-09-28 21:25:59 +0000291int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
292 const void *val, int len)
293{
294 struct fdt_property *prop;
295 int err, oldlen, newlen;
296
Rob Herringf8589272018-09-13 08:59:25 -0500297 FDT_RW_PROBE(fdt);
Stephen Warrencd296722012-09-28 21:25:59 +0000298
299 prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
300 if (prop) {
301 newlen = len + oldlen;
Rob Herring9130ba82018-02-27 17:40:38 -0600302 err = fdt_splice_struct_(fdt, prop->data,
Stephen Warrencd296722012-09-28 21:25:59 +0000303 FDT_TAGALIGN(oldlen),
304 FDT_TAGALIGN(newlen));
305 if (err)
306 return err;
307 prop->len = cpu_to_fdt32(newlen);
308 memcpy(prop->data + oldlen, val, len);
309 } else {
Rob Herring9130ba82018-02-27 17:40:38 -0600310 err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
Stephen Warrencd296722012-09-28 21:25:59 +0000311 if (err)
312 return err;
313 memcpy(prop->data, val, len);
314 }
315 return 0;
316}
317
David Gibson1cade992007-12-10 14:28:39 +1100318int fdt_delprop(void *fdt, int nodeoffset, const char *name)
319{
320 struct fdt_property *prop;
321 int len, proplen;
322
Rob Herringf8589272018-09-13 08:59:25 -0500323 FDT_RW_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100324
325 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Rob Herring4201d052017-10-03 11:37:04 -0500326 if (!prop)
David Gibson1cade992007-12-10 14:28:39 +1100327 return len;
328
David Gibsoned95d742008-08-07 12:24:17 +1000329 proplen = sizeof(*prop) + FDT_TAGALIGN(len);
Rob Herring9130ba82018-02-27 17:40:38 -0600330 return fdt_splice_struct_(fdt, prop, proplen, 0);
David Gibson1cade992007-12-10 14:28:39 +1100331}
332
333int fdt_add_subnode_namelen(void *fdt, int parentoffset,
334 const char *name, int namelen)
335{
336 struct fdt_node_header *nh;
337 int offset, nextoffset;
338 int nodelen;
339 int err;
340 uint32_t tag;
Rob Herring47605972015-04-29 16:00:05 -0500341 fdt32_t *endtag;
David Gibson1cade992007-12-10 14:28:39 +1100342
Rob Herringf8589272018-09-13 08:59:25 -0500343 FDT_RW_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100344
345 offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
346 if (offset >= 0)
347 return -FDT_ERR_EXISTS;
348 else if (offset != -FDT_ERR_NOTFOUND)
349 return offset;
350
351 /* Try to place the new node after the parent's properties */
352 fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
353 do {
354 offset = nextoffset;
355 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibsoned95d742008-08-07 12:24:17 +1000356 } while ((tag == FDT_PROP) || (tag == FDT_NOP));
David Gibson1cade992007-12-10 14:28:39 +1100357
Rob Herring9130ba82018-02-27 17:40:38 -0600358 nh = fdt_offset_ptr_w_(fdt, offset);
David Gibsoned95d742008-08-07 12:24:17 +1000359 nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
David Gibson1cade992007-12-10 14:28:39 +1100360
Rob Herring9130ba82018-02-27 17:40:38 -0600361 err = fdt_splice_struct_(fdt, nh, 0, nodelen);
David Gibson1cade992007-12-10 14:28:39 +1100362 if (err)
363 return err;
364
365 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
David Gibsoned95d742008-08-07 12:24:17 +1000366 memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
David Gibson1cade992007-12-10 14:28:39 +1100367 memcpy(nh->name, name, namelen);
Rob Herring47605972015-04-29 16:00:05 -0500368 endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
David Gibson1cade992007-12-10 14:28:39 +1100369 *endtag = cpu_to_fdt32(FDT_END_NODE);
370
371 return offset;
372}
373
374int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
375{
376 return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
377}
378
379int fdt_del_node(void *fdt, int nodeoffset)
380{
381 int endoffset;
382
Rob Herringf8589272018-09-13 08:59:25 -0500383 FDT_RW_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100384
Rob Herring9130ba82018-02-27 17:40:38 -0600385 endoffset = fdt_node_end_offset_(fdt, nodeoffset);
David Gibson1cade992007-12-10 14:28:39 +1100386 if (endoffset < 0)
387 return endoffset;
388
Rob Herring9130ba82018-02-27 17:40:38 -0600389 return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
David Gibsoned95d742008-08-07 12:24:17 +1000390 endoffset - nodeoffset, 0);
David Gibson1cade992007-12-10 14:28:39 +1100391}
392
Rob Herring9130ba82018-02-27 17:40:38 -0600393static void fdt_packblocks_(const char *old, char *new,
David Gibsoned95d742008-08-07 12:24:17 +1000394 int mem_rsv_size, int struct_size)
David Gibson1cade992007-12-10 14:28:39 +1100395{
396 int mem_rsv_off, struct_off, strings_off;
397
David Gibsoned95d742008-08-07 12:24:17 +1000398 mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
David Gibson1cade992007-12-10 14:28:39 +1100399 struct_off = mem_rsv_off + mem_rsv_size;
400 strings_off = struct_off + struct_size;
401
David Gibsoned95d742008-08-07 12:24:17 +1000402 memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
403 fdt_set_off_mem_rsvmap(new, mem_rsv_off);
David Gibson1cade992007-12-10 14:28:39 +1100404
David Gibsoned95d742008-08-07 12:24:17 +1000405 memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
406 fdt_set_off_dt_struct(new, struct_off);
407 fdt_set_size_dt_struct(new, struct_size);
David Gibson1cade992007-12-10 14:28:39 +1100408
David Gibsoned95d742008-08-07 12:24:17 +1000409 memmove(new + strings_off, old + fdt_off_dt_strings(old),
410 fdt_size_dt_strings(old));
411 fdt_set_off_dt_strings(new, strings_off);
412 fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
David Gibson1cade992007-12-10 14:28:39 +1100413}
414
415int fdt_open_into(const void *fdt, void *buf, int bufsize)
416{
417 int err;
418 int mem_rsv_size, struct_size;
419 int newsize;
David Gibsoned95d742008-08-07 12:24:17 +1000420 const char *fdtstart = fdt;
421 const char *fdtend = fdtstart + fdt_totalsize(fdt);
422 char *tmp;
David Gibson1cade992007-12-10 14:28:39 +1100423
Rob Herringf8589272018-09-13 08:59:25 -0500424 FDT_RO_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100425
426 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
427 * sizeof(struct fdt_reserve_entry);
428
Rob Herringd047cd82020-03-13 08:56:58 -0500429 if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
David Gibson1cade992007-12-10 14:28:39 +1100430 struct_size = fdt_size_dt_struct(fdt);
431 } else {
432 struct_size = 0;
433 while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
434 ;
Stephen Warrencd296722012-09-28 21:25:59 +0000435 if (struct_size < 0)
436 return struct_size;
David Gibson1cade992007-12-10 14:28:39 +1100437 }
438
Rob Herring3eb619b2020-06-29 12:15:13 -0600439 if (can_assume(LIBFDT_ORDER) ||
Rob Herringd047cd82020-03-13 08:56:58 -0500440 !fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
David Gibson1cade992007-12-10 14:28:39 +1100441 /* no further work necessary */
442 err = fdt_move(fdt, buf, bufsize);
443 if (err)
444 return err;
445 fdt_set_version(buf, 17);
446 fdt_set_size_dt_struct(buf, struct_size);
447 fdt_set_totalsize(buf, bufsize);
448 return 0;
449 }
450
451 /* Need to reorder */
David Gibsoned95d742008-08-07 12:24:17 +1000452 newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
David Gibson1cade992007-12-10 14:28:39 +1100453 + struct_size + fdt_size_dt_strings(fdt);
454
455 if (bufsize < newsize)
456 return -FDT_ERR_NOSPACE;
457
David Gibsoned95d742008-08-07 12:24:17 +1000458 /* First attempt to build converted tree at beginning of buffer */
459 tmp = buf;
460 /* But if that overlaps with the old tree... */
461 if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
462 /* Try right after the old tree instead */
463 tmp = (char *)(uintptr_t)fdtend;
464 if ((tmp + newsize) > ((char *)buf + bufsize))
David Gibson1cade992007-12-10 14:28:39 +1100465 return -FDT_ERR_NOSPACE;
466 }
467
Rob Herring9130ba82018-02-27 17:40:38 -0600468 fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size);
David Gibson1cade992007-12-10 14:28:39 +1100469 memmove(buf, tmp, newsize);
470
471 fdt_set_magic(buf, FDT_MAGIC);
472 fdt_set_totalsize(buf, bufsize);
473 fdt_set_version(buf, 17);
474 fdt_set_last_comp_version(buf, 16);
475 fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
476
477 return 0;
478}
479
480int fdt_pack(void *fdt)
481{
482 int mem_rsv_size;
David Gibson1cade992007-12-10 14:28:39 +1100483
Rob Herringf8589272018-09-13 08:59:25 -0500484 FDT_RW_PROBE(fdt);
David Gibson1cade992007-12-10 14:28:39 +1100485
486 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
487 * sizeof(struct fdt_reserve_entry);
Rob Herring9130ba82018-02-27 17:40:38 -0600488 fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
489 fdt_set_totalsize(fdt, fdt_data_size_(fdt));
David Gibson1cade992007-12-10 14:28:39 +1100490
491 return 0;
492}