blob: 2eb2b38703be69cee07e71da17b5dc3dd9e7169f [file] [log] [blame]
Simon Glassa5e1a582019-10-27 09:47:42 -06001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
Tom Rinibd391732017-09-23 12:52:44 -04002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
Tom Rinibd391732017-09-23 12:52:44 -04005 */
6#include "libfdt_env.h"
7
8#include <fdt.h>
9#include <libfdt.h>
10
11#include "libfdt_internal.h"
12
Rob Herringcd5f70d2018-05-19 14:13:53 +020013static int fdt_blocks_misordered_(const void *fdt,
14 int mem_rsv_size, int struct_size)
Tom Rinibd391732017-09-23 12:52:44 -040015{
Simon Glassa5e1a582019-10-27 09:47:42 -060016 if (!fdt_chk_basic())
17 return false;
Tom Rinibd391732017-09-23 12:52:44 -040018 return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
19 || (fdt_off_dt_struct(fdt) <
20 (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
21 || (fdt_off_dt_strings(fdt) <
22 (fdt_off_dt_struct(fdt) + struct_size))
23 || (fdt_totalsize(fdt) <
24 (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
25}
26
Simon Glassa5e1a582019-10-27 09:47:42 -060027static int fdt_rw_probe_(void *fdt)
Tom Rinibd391732017-09-23 12:52:44 -040028{
Simon Glassa5e1a582019-10-27 09:47:42 -060029 if (!fdt_chk_basic())
30 return 0;
31 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -040032
Simon Glassa5e1a582019-10-27 09:47:42 -060033 if (fdt_chk_version() && fdt_version(fdt) < 17)
Tom Rinibd391732017-09-23 12:52:44 -040034 return -FDT_ERR_BADVERSION;
Rob Herringcd5f70d2018-05-19 14:13:53 +020035 if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
Tom Rinibd391732017-09-23 12:52:44 -040036 fdt_size_dt_struct(fdt)))
37 return -FDT_ERR_BADLAYOUT;
Simon Glassa5e1a582019-10-27 09:47:42 -060038 if (fdt_chk_version() && fdt_version(fdt) > 17)
Tom Rinibd391732017-09-23 12:52:44 -040039 fdt_set_version(fdt, 17);
40
41 return 0;
42}
43
Simon Glassa5e1a582019-10-27 09:47:42 -060044#define FDT_RW_PROBE(fdt) \
Tom Rinibd391732017-09-23 12:52:44 -040045 { \
Rob Herringcd5f70d2018-05-19 14:13:53 +020046 int err_; \
Simon Glassa5e1a582019-10-27 09:47:42 -060047 if (fdt_chk_extra() && (err_ = fdt_rw_probe_(fdt)) != 0) \
Rob Herringcd5f70d2018-05-19 14:13:53 +020048 return err_; \
Tom Rinibd391732017-09-23 12:52:44 -040049 }
50
Rob Herringcd5f70d2018-05-19 14:13:53 +020051static inline int fdt_data_size_(void *fdt)
Tom Rinibd391732017-09-23 12:52:44 -040052{
53 return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
54}
55
Rob Herringcd5f70d2018-05-19 14:13:53 +020056static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
Tom Rinibd391732017-09-23 12:52:44 -040057{
58 char *p = splicepoint;
Rob Herringcd5f70d2018-05-19 14:13:53 +020059 char *end = (char *)fdt + fdt_data_size_(fdt);
Tom Rinibd391732017-09-23 12:52:44 -040060
61 if (((p + oldlen) < p) || ((p + oldlen) > end))
62 return -FDT_ERR_BADOFFSET;
63 if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt))
64 return -FDT_ERR_BADOFFSET;
65 if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
66 return -FDT_ERR_NOSPACE;
67 memmove(p + newlen, p + oldlen, end - p - oldlen);
68 return 0;
69}
70
Rob Herringcd5f70d2018-05-19 14:13:53 +020071static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
Tom Rinibd391732017-09-23 12:52:44 -040072 int oldn, int newn)
73{
74 int delta = (newn - oldn) * sizeof(*p);
75 int err;
Rob Herringcd5f70d2018-05-19 14:13:53 +020076 err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
Tom Rinibd391732017-09-23 12:52:44 -040077 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
Rob Herringcd5f70d2018-05-19 14:13:53 +020084static int fdt_splice_struct_(void *fdt, void *p,
Tom Rinibd391732017-09-23 12:52:44 -040085 int oldlen, int newlen)
86{
87 int delta = newlen - oldlen;
88 int err;
89
Rob Herringcd5f70d2018-05-19 14:13:53 +020090 if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
Tom Rinibd391732017-09-23 12:52:44 -040091 return err;
92
93 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);
95 return 0;
96}
97
Simon Glassa5e1a582019-10-27 09:47:42 -060098/* Must only be used to roll back in case of error */
99static void fdt_del_last_string_(void *fdt, const char *s)
100{
101 int newlen = strlen(s) + 1;
102
103 fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
104}
105
Rob Herringcd5f70d2018-05-19 14:13:53 +0200106static int fdt_splice_string_(void *fdt, int newlen)
Tom Rinibd391732017-09-23 12:52:44 -0400107{
108 void *p = (char *)fdt
109 + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
110 int err;
111
Rob Herringcd5f70d2018-05-19 14:13:53 +0200112 if ((err = fdt_splice_(fdt, p, 0, newlen)))
Tom Rinibd391732017-09-23 12:52:44 -0400113 return err;
114
115 fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
116 return 0;
117}
118
Simon Glassa5e1a582019-10-27 09:47:42 -0600119/**
120 * fdt_find_add_string_() - Find or allocate a string
121 *
122 * @fdt: pointer to the device tree to check/adjust
123 * @s: string to find/add
124 * @allocated: Set to 0 if the string was found, 1 if not found and so
125 * allocated. Ignored if !fdt_chk_basic()
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100126 * Return: offset of string in the string table (whether found or added)
Simon Glassa5e1a582019-10-27 09:47:42 -0600127 */
128static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
Tom Rinibd391732017-09-23 12:52:44 -0400129{
130 char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
131 const char *p;
132 char *new;
133 int len = strlen(s) + 1;
134 int err;
135
Simon Glassa5e1a582019-10-27 09:47:42 -0600136 if (fdt_chk_basic())
137 *allocated = 0;
138
Rob Herringcd5f70d2018-05-19 14:13:53 +0200139 p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
Tom Rinibd391732017-09-23 12:52:44 -0400140 if (p)
141 /* found it */
142 return (p - strtab);
143
144 new = strtab + fdt_size_dt_strings(fdt);
Rob Herringcd5f70d2018-05-19 14:13:53 +0200145 err = fdt_splice_string_(fdt, len);
Tom Rinibd391732017-09-23 12:52:44 -0400146 if (err)
147 return err;
148
Simon Glassa5e1a582019-10-27 09:47:42 -0600149 if (fdt_chk_basic())
150 *allocated = 1;
151
Tom Rinibd391732017-09-23 12:52:44 -0400152 memcpy(new, s, len);
153 return (new - strtab);
154}
155
156int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
157{
158 struct fdt_reserve_entry *re;
159 int err;
160
Simon Glassa5e1a582019-10-27 09:47:42 -0600161 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400162
Rob Herringcd5f70d2018-05-19 14:13:53 +0200163 re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
164 err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
Tom Rinibd391732017-09-23 12:52:44 -0400165 if (err)
166 return err;
167
168 re->address = cpu_to_fdt64(address);
169 re->size = cpu_to_fdt64(size);
170 return 0;
171}
172
173int fdt_del_mem_rsv(void *fdt, int n)
174{
Rob Herringcd5f70d2018-05-19 14:13:53 +0200175 struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
Tom Rinibd391732017-09-23 12:52:44 -0400176
Simon Glassa5e1a582019-10-27 09:47:42 -0600177 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400178
179 if (n >= fdt_num_mem_rsv(fdt))
180 return -FDT_ERR_NOTFOUND;
181
Rob Herringcd5f70d2018-05-19 14:13:53 +0200182 return fdt_splice_mem_rsv_(fdt, re, 1, 0);
Tom Rinibd391732017-09-23 12:52:44 -0400183}
184
Rob Herringcd5f70d2018-05-19 14:13:53 +0200185static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
Tom Rinibd391732017-09-23 12:52:44 -0400186 int len, struct fdt_property **prop)
187{
188 int oldlen;
189 int err;
190
191 *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
Tom Rini0eb096d2017-09-23 17:31:59 -0400192 if (!*prop)
Tom Rinibd391732017-09-23 12:52:44 -0400193 return oldlen;
194
Rob Herringcd5f70d2018-05-19 14:13:53 +0200195 if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
Tom Rinibd391732017-09-23 12:52:44 -0400196 FDT_TAGALIGN(len))))
197 return err;
198
199 (*prop)->len = cpu_to_fdt32(len);
200 return 0;
201}
202
Rob Herringcd5f70d2018-05-19 14:13:53 +0200203static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
Tom Rinibd391732017-09-23 12:52:44 -0400204 int len, struct fdt_property **prop)
205{
206 int proplen;
207 int nextoffset;
208 int namestroff;
209 int err;
Simon Glassa5e1a582019-10-27 09:47:42 -0600210 int allocated;
Tom Rinibd391732017-09-23 12:52:44 -0400211
Rob Herringcd5f70d2018-05-19 14:13:53 +0200212 if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
Tom Rinibd391732017-09-23 12:52:44 -0400213 return nextoffset;
214
Simon Glassa5e1a582019-10-27 09:47:42 -0600215 namestroff = fdt_find_add_string_(fdt, name, &allocated);
Tom Rinibd391732017-09-23 12:52:44 -0400216 if (namestroff < 0)
217 return namestroff;
218
Rob Herringcd5f70d2018-05-19 14:13:53 +0200219 *prop = fdt_offset_ptr_w_(fdt, nextoffset);
Tom Rinibd391732017-09-23 12:52:44 -0400220 proplen = sizeof(**prop) + FDT_TAGALIGN(len);
221
Rob Herringcd5f70d2018-05-19 14:13:53 +0200222 err = fdt_splice_struct_(fdt, *prop, 0, proplen);
Simon Glassa5e1a582019-10-27 09:47:42 -0600223 if (err) {
224 /* Delete the string if we failed to add it */
225 if (fdt_chk_basic() && allocated)
226 fdt_del_last_string_(fdt, name);
Tom Rinibd391732017-09-23 12:52:44 -0400227 return err;
Simon Glassa5e1a582019-10-27 09:47:42 -0600228 }
Tom Rinibd391732017-09-23 12:52:44 -0400229
230 (*prop)->tag = cpu_to_fdt32(FDT_PROP);
231 (*prop)->nameoff = cpu_to_fdt32(namestroff);
232 (*prop)->len = cpu_to_fdt32(len);
233 return 0;
234}
235
236int fdt_set_name(void *fdt, int nodeoffset, const char *name)
237{
238 char *namep;
239 int oldlen, newlen;
240 int err;
241
Simon Glassa5e1a582019-10-27 09:47:42 -0600242 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400243
244 namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
245 if (!namep)
246 return oldlen;
247
248 newlen = strlen(name);
249
Rob Herringcd5f70d2018-05-19 14:13:53 +0200250 err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
Tom Rinibd391732017-09-23 12:52:44 -0400251 FDT_TAGALIGN(newlen+1));
252 if (err)
253 return err;
254
255 memcpy(namep, name, newlen+1);
256 return 0;
257}
258
Tom Rini0eb096d2017-09-23 17:31:59 -0400259int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
260 int len, void **prop_data)
Tom Rinibd391732017-09-23 12:52:44 -0400261{
262 struct fdt_property *prop;
263 int err;
264
Simon Glassa5e1a582019-10-27 09:47:42 -0600265 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400266
Rob Herringcd5f70d2018-05-19 14:13:53 +0200267 err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
Tom Rinibd391732017-09-23 12:52:44 -0400268 if (err == -FDT_ERR_NOTFOUND)
Rob Herringcd5f70d2018-05-19 14:13:53 +0200269 err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
Tom Rinibd391732017-09-23 12:52:44 -0400270 if (err)
271 return err;
272
Tom Rini0eb096d2017-09-23 17:31:59 -0400273 *prop_data = prop->data;
274 return 0;
275}
276
277int fdt_setprop(void *fdt, int nodeoffset, const char *name,
278 const void *val, int len)
279{
280 void *prop_data;
281 int err;
282
283 err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
284 if (err)
285 return err;
286
Tom Rinibd391732017-09-23 12:52:44 -0400287 if (len)
Tom Rini0eb096d2017-09-23 17:31:59 -0400288 memcpy(prop_data, val, len);
Tom Rinibd391732017-09-23 12:52:44 -0400289 return 0;
290}
291
292int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
293 const void *val, int len)
294{
295 struct fdt_property *prop;
296 int err, oldlen, newlen;
297
Simon Glassa5e1a582019-10-27 09:47:42 -0600298 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400299
300 prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
301 if (prop) {
302 newlen = len + oldlen;
Rob Herringcd5f70d2018-05-19 14:13:53 +0200303 err = fdt_splice_struct_(fdt, prop->data,
Tom Rinibd391732017-09-23 12:52:44 -0400304 FDT_TAGALIGN(oldlen),
305 FDT_TAGALIGN(newlen));
306 if (err)
307 return err;
308 prop->len = cpu_to_fdt32(newlen);
309 memcpy(prop->data + oldlen, val, len);
310 } else {
Rob Herringcd5f70d2018-05-19 14:13:53 +0200311 err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
Tom Rinibd391732017-09-23 12:52:44 -0400312 if (err)
313 return err;
314 memcpy(prop->data, val, len);
315 }
316 return 0;
317}
318
319int fdt_delprop(void *fdt, int nodeoffset, const char *name)
320{
321 struct fdt_property *prop;
322 int len, proplen;
323
Simon Glassa5e1a582019-10-27 09:47:42 -0600324 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400325
326 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Tom Rini0eb096d2017-09-23 17:31:59 -0400327 if (!prop)
Tom Rinibd391732017-09-23 12:52:44 -0400328 return len;
329
330 proplen = sizeof(*prop) + FDT_TAGALIGN(len);
Rob Herringcd5f70d2018-05-19 14:13:53 +0200331 return fdt_splice_struct_(fdt, prop, proplen, 0);
Tom Rinibd391732017-09-23 12:52:44 -0400332}
333
334int fdt_add_subnode_namelen(void *fdt, int parentoffset,
335 const char *name, int namelen)
336{
337 struct fdt_node_header *nh;
338 int offset, nextoffset;
339 int nodelen;
340 int err;
341 uint32_t tag;
342 fdt32_t *endtag;
343
Simon Glassa5e1a582019-10-27 09:47:42 -0600344 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400345
346 offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
347 if (offset >= 0)
348 return -FDT_ERR_EXISTS;
349 else if (offset != -FDT_ERR_NOTFOUND)
350 return offset;
351
352 /* Try to place the new node after the parent's properties */
353 fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
354 do {
355 offset = nextoffset;
356 tag = fdt_next_tag(fdt, offset, &nextoffset);
357 } while ((tag == FDT_PROP) || (tag == FDT_NOP));
358
Rob Herringcd5f70d2018-05-19 14:13:53 +0200359 nh = fdt_offset_ptr_w_(fdt, offset);
Tom Rinibd391732017-09-23 12:52:44 -0400360 nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
361
Rob Herringcd5f70d2018-05-19 14:13:53 +0200362 err = fdt_splice_struct_(fdt, nh, 0, nodelen);
Tom Rinibd391732017-09-23 12:52:44 -0400363 if (err)
364 return err;
365
366 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
367 memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
368 memcpy(nh->name, name, namelen);
369 endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
370 *endtag = cpu_to_fdt32(FDT_END_NODE);
371
372 return offset;
373}
374
375int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
376{
377 return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
378}
379
380int fdt_del_node(void *fdt, int nodeoffset)
381{
382 int endoffset;
383
Simon Glassa5e1a582019-10-27 09:47:42 -0600384 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400385
Rob Herringcd5f70d2018-05-19 14:13:53 +0200386 endoffset = fdt_node_end_offset_(fdt, nodeoffset);
Tom Rinibd391732017-09-23 12:52:44 -0400387 if (endoffset < 0)
388 return endoffset;
389
Rob Herringcd5f70d2018-05-19 14:13:53 +0200390 return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
Tom Rinibd391732017-09-23 12:52:44 -0400391 endoffset - nodeoffset, 0);
392}
393
Rob Herringcd5f70d2018-05-19 14:13:53 +0200394static void fdt_packblocks_(const char *old, char *new,
Tom Rinibd391732017-09-23 12:52:44 -0400395 int mem_rsv_size, int struct_size)
396{
397 int mem_rsv_off, struct_off, strings_off;
398
399 mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
400 struct_off = mem_rsv_off + mem_rsv_size;
401 strings_off = struct_off + struct_size;
402
403 memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
404 fdt_set_off_mem_rsvmap(new, mem_rsv_off);
405
406 memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
407 fdt_set_off_dt_struct(new, struct_off);
408 fdt_set_size_dt_struct(new, struct_size);
409
410 memmove(new + strings_off, old + fdt_off_dt_strings(old),
411 fdt_size_dt_strings(old));
412 fdt_set_off_dt_strings(new, strings_off);
413 fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
414}
415
416int fdt_open_into(const void *fdt, void *buf, int bufsize)
417{
418 int err;
419 int mem_rsv_size, struct_size;
420 int newsize;
421 const char *fdtstart = fdt;
422 const char *fdtend = fdtstart + fdt_totalsize(fdt);
423 char *tmp;
424
Simon Glassa5e1a582019-10-27 09:47:42 -0600425 FDT_RO_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400426
427 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
428 * sizeof(struct fdt_reserve_entry);
429
Simon Glassa5e1a582019-10-27 09:47:42 -0600430 if (!fdt_chk_version() || fdt_version(fdt) >= 17) {
Tom Rinibd391732017-09-23 12:52:44 -0400431 struct_size = fdt_size_dt_struct(fdt);
432 } else {
433 struct_size = 0;
434 while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
435 ;
436 if (struct_size < 0)
437 return struct_size;
438 }
439
Rob Herringcd5f70d2018-05-19 14:13:53 +0200440 if (!fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
Tom Rinibd391732017-09-23 12:52:44 -0400441 /* 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 */
452 newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
453 + struct_size + fdt_size_dt_strings(fdt);
454
455 if (bufsize < newsize)
456 return -FDT_ERR_NOSPACE;
457
458 /* 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))
465 return -FDT_ERR_NOSPACE;
466 }
467
Rob Herringcd5f70d2018-05-19 14:13:53 +0200468 fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size);
Tom Rinibd391732017-09-23 12:52:44 -0400469 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;
483
Simon Glassa5e1a582019-10-27 09:47:42 -0600484 FDT_RW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400485
486 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
487 * sizeof(struct fdt_reserve_entry);
Rob Herringcd5f70d2018-05-19 14:13:53 +0200488 fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
489 fdt_set_totalsize(fdt, fdt_data_size_(fdt));
Tom Rinibd391732017-09-23 12:52:44 -0400490
491 return 0;
492}