blob: 6fa4f13073d28fd76c886b1154e254ca1c6e1fd0 [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 */
6#include "libfdt_env.h"
7
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 { \
46 int err; \
David Gibson9457d9d2008-07-09 14:10:24 +100047 if ((err = _fdt_rw_check_header(fdt)) != 0) \
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040048 return err; \
49 }
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;
David Gibsonb4141b82008-07-07 10:10:48 +100063 if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040064 return -FDT_ERR_NOSPACE;
65 memmove(p + newlen, p + oldlen, end - p - oldlen);
66 return 0;
67}
68
David Gibson9457d9d2008-07-09 14:10:24 +100069static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
70 int oldn, int newn)
Kumar Galac8ab7052007-10-24 11:04:22 -050071{
72 int delta = (newn - oldn) * sizeof(*p);
73 int err;
David Gibson9457d9d2008-07-09 14:10:24 +100074 err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
Kumar Galac8ab7052007-10-24 11:04:22 -050075 if (err)
76 return err;
77 fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
78 fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
79 return 0;
80}
81
David Gibson9457d9d2008-07-09 14:10:24 +100082static int _fdt_splice_struct(void *fdt, void *p,
83 int oldlen, int newlen)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040084{
85 int delta = newlen - oldlen;
86 int err;
87
David Gibson9457d9d2008-07-09 14:10:24 +100088 if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040089 return err;
90
Kumar Galac8ab7052007-10-24 11:04:22 -050091 fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
92 fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040093 return 0;
94}
95
David Gibson9457d9d2008-07-09 14:10:24 +100096static int _fdt_splice_string(void *fdt, int newlen)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040097{
David Gibsonb4141b82008-07-07 10:10:48 +100098 void *p = (char *)fdt
99 + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400100 int err;
101
David Gibson9457d9d2008-07-09 14:10:24 +1000102 if ((err = _fdt_splice(fdt, p, 0, newlen)))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400103 return err;
104
Kumar Galac8ab7052007-10-24 11:04:22 -0500105 fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400106 return 0;
107}
108
David Gibson9457d9d2008-07-09 14:10:24 +1000109static int _fdt_find_add_string(void *fdt, const char *s)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400110{
111 char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
112 const char *p;
113 char *new;
114 int len = strlen(s) + 1;
115 int err;
116
117 p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
118 if (p)
119 /* found it */
120 return (p - strtab);
121
122 new = strtab + fdt_size_dt_strings(fdt);
David Gibson9457d9d2008-07-09 14:10:24 +1000123 err = _fdt_splice_string(fdt, len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400124 if (err)
125 return err;
126
127 memcpy(new, s, len);
128 return (new - strtab);
129}
130
Kumar Galac8ab7052007-10-24 11:04:22 -0500131int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
132{
133 struct fdt_reserve_entry *re;
134 int err;
135
David Gibson9457d9d2008-07-09 14:10:24 +1000136 FDT_RW_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500137
138 re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
David Gibson9457d9d2008-07-09 14:10:24 +1000139 err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
Kumar Galac8ab7052007-10-24 11:04:22 -0500140 if (err)
141 return err;
142
143 re->address = cpu_to_fdt64(address);
144 re->size = cpu_to_fdt64(size);
145 return 0;
146}
147
148int fdt_del_mem_rsv(void *fdt, int n)
149{
150 struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
151 int err;
152
David Gibson9457d9d2008-07-09 14:10:24 +1000153 FDT_RW_CHECK_HEADER(fdt);
David Gibson148a49b2008-05-20 17:19:11 +1000154
Kumar Galac8ab7052007-10-24 11:04:22 -0500155 if (n >= fdt_num_mem_rsv(fdt))
156 return -FDT_ERR_NOTFOUND;
157
David Gibson9457d9d2008-07-09 14:10:24 +1000158 err = _fdt_splice_mem_rsv(fdt, re, 1, 0);
Kumar Galac8ab7052007-10-24 11:04:22 -0500159 if (err)
160 return err;
161 return 0;
162}
163
David Gibson9457d9d2008-07-09 14:10:24 +1000164static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
165 int len, struct fdt_property **prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400166{
167 int oldlen;
168 int err;
169
Kumar Galac8ab7052007-10-24 11:04:22 -0500170 *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400171 if (! (*prop))
172 return oldlen;
173
David Gibson9457d9d2008-07-09 14:10:24 +1000174 if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
175 FDT_TAGALIGN(len))))
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400176 return err;
177
178 (*prop)->len = cpu_to_fdt32(len);
179 return 0;
180}
181
David Gibson9457d9d2008-07-09 14:10:24 +1000182static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
183 int len, struct fdt_property **prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400184{
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400185 int proplen;
186 int nextoffset;
187 int namestroff;
188 int err;
189
David Gibson148a49b2008-05-20 17:19:11 +1000190 if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
191 return nextoffset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400192
David Gibson9457d9d2008-07-09 14:10:24 +1000193 namestroff = _fdt_find_add_string(fdt, name);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400194 if (namestroff < 0)
195 return namestroff;
196
Kumar Galac8ab7052007-10-24 11:04:22 -0500197 *prop = _fdt_offset_ptr_w(fdt, nextoffset);
David Gibson9457d9d2008-07-09 14:10:24 +1000198 proplen = sizeof(**prop) + FDT_TAGALIGN(len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400199
David Gibson9457d9d2008-07-09 14:10:24 +1000200 err = _fdt_splice_struct(fdt, *prop, 0, proplen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400201 if (err)
202 return err;
203
204 (*prop)->tag = cpu_to_fdt32(FDT_PROP);
205 (*prop)->nameoff = cpu_to_fdt32(namestroff);
206 (*prop)->len = cpu_to_fdt32(len);
207 return 0;
208}
209
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100210int fdt_set_name(void *fdt, int nodeoffset, const char *name)
211{
212 char *namep;
213 int oldlen, newlen;
214 int err;
215
David Gibson9457d9d2008-07-09 14:10:24 +1000216 FDT_RW_CHECK_HEADER(fdt);
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100217
David Gibson870fa472008-07-07 10:14:15 +1000218 namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100219 if (!namep)
220 return oldlen;
221
222 newlen = strlen(name);
223
David Gibson9457d9d2008-07-09 14:10:24 +1000224 err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
225 FDT_TAGALIGN(newlen+1));
David Gibsonfd1ea8f2008-01-11 14:55:05 +1100226 if (err)
227 return err;
228
229 memcpy(namep, name, newlen+1);
230 return 0;
231}
232
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400233int fdt_setprop(void *fdt, int nodeoffset, const char *name,
234 const void *val, int len)
235{
236 struct fdt_property *prop;
237 int err;
238
David Gibson9457d9d2008-07-09 14:10:24 +1000239 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400240
David Gibson9457d9d2008-07-09 14:10:24 +1000241 err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400242 if (err == -FDT_ERR_NOTFOUND)
David Gibson9457d9d2008-07-09 14:10:24 +1000243 err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400244 if (err)
245 return err;
246
247 memcpy(prop->data, val, len);
248 return 0;
249}
250
Minghuan Lian30648272012-08-27 19:38:59 -0500251int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
252 const void *val, int len)
253{
254 struct fdt_property *prop;
255 int err, oldlen, newlen;
256
257 FDT_RW_CHECK_HEADER(fdt);
258
259 prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
260 if (prop) {
261 newlen = len + oldlen;
262 err = _fdt_splice_struct(fdt, prop->data,
263 FDT_TAGALIGN(oldlen),
264 FDT_TAGALIGN(newlen));
265 if (err)
266 return err;
267 prop->len = cpu_to_fdt32(newlen);
268 memcpy(prop->data + oldlen, val, len);
269 } else {
270 err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
271 if (err)
272 return err;
273 memcpy(prop->data, val, len);
274 }
275 return 0;
276}
277
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400278int fdt_delprop(void *fdt, int nodeoffset, const char *name)
279{
280 struct fdt_property *prop;
281 int len, proplen;
282
David Gibson9457d9d2008-07-09 14:10:24 +1000283 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400284
Kumar Galac8ab7052007-10-24 11:04:22 -0500285 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400286 if (! prop)
287 return len;
288
David Gibson9457d9d2008-07-09 14:10:24 +1000289 proplen = sizeof(*prop) + FDT_TAGALIGN(len);
290 return _fdt_splice_struct(fdt, prop, proplen, 0);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400291}
292
293int fdt_add_subnode_namelen(void *fdt, int parentoffset,
294 const char *name, int namelen)
295{
296 struct fdt_node_header *nh;
297 int offset, nextoffset;
298 int nodelen;
299 int err;
300 uint32_t tag;
Kim Phillips38c38152013-01-16 13:59:50 +0000301 fdt32_t *endtag;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400302
David Gibson9457d9d2008-07-09 14:10:24 +1000303 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400304
305 offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
306 if (offset >= 0)
307 return -FDT_ERR_EXISTS;
308 else if (offset != -FDT_ERR_NOTFOUND)
309 return offset;
310
311 /* Try to place the new node after the parent's properties */
Kumar Galac8ab7052007-10-24 11:04:22 -0500312 fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400313 do {
314 offset = nextoffset;
Kumar Galac8ab7052007-10-24 11:04:22 -0500315 tag = fdt_next_tag(fdt, offset, &nextoffset);
David Gibsone3524b12008-02-14 16:50:34 +1100316 } while ((tag == FDT_PROP) || (tag == FDT_NOP));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400317
Kumar Galac8ab7052007-10-24 11:04:22 -0500318 nh = _fdt_offset_ptr_w(fdt, offset);
David Gibson9457d9d2008-07-09 14:10:24 +1000319 nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400320
David Gibson9457d9d2008-07-09 14:10:24 +1000321 err = _fdt_splice_struct(fdt, nh, 0, nodelen);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400322 if (err)
323 return err;
324
325 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
David Gibson9457d9d2008-07-09 14:10:24 +1000326 memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400327 memcpy(nh->name, name, namelen);
Kim Phillips38c38152013-01-16 13:59:50 +0000328 endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400329 *endtag = cpu_to_fdt32(FDT_END_NODE);
330
331 return offset;
332}
333
334int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
335{
336 return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
337}
338
339int fdt_del_node(void *fdt, int nodeoffset)
340{
341 int endoffset;
342
David Gibson9457d9d2008-07-09 14:10:24 +1000343 FDT_RW_CHECK_HEADER(fdt);
Kumar Galac8ab7052007-10-24 11:04:22 -0500344
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400345 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
346 if (endoffset < 0)
347 return endoffset;
348
David Gibson9457d9d2008-07-09 14:10:24 +1000349 return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
350 endoffset - nodeoffset, 0);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400351}
352
David Gibson9457d9d2008-07-09 14:10:24 +1000353static void _fdt_packblocks(const char *old, char *new,
354 int mem_rsv_size, int struct_size)
Kumar Galac8ab7052007-10-24 11:04:22 -0500355{
356 int mem_rsv_off, struct_off, strings_off;
357
David Gibson9457d9d2008-07-09 14:10:24 +1000358 mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
Kumar Galac8ab7052007-10-24 11:04:22 -0500359 struct_off = mem_rsv_off + mem_rsv_size;
360 strings_off = struct_off + struct_size;
361
David Gibsonb4141b82008-07-07 10:10:48 +1000362 memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
363 fdt_set_off_mem_rsvmap(new, mem_rsv_off);
Kumar Galac8ab7052007-10-24 11:04:22 -0500364
David Gibsonb4141b82008-07-07 10:10:48 +1000365 memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
366 fdt_set_off_dt_struct(new, struct_off);
367 fdt_set_size_dt_struct(new, struct_size);
Kumar Galac8ab7052007-10-24 11:04:22 -0500368
David Gibsonb4141b82008-07-07 10:10:48 +1000369 memmove(new + strings_off, old + fdt_off_dt_strings(old),
370 fdt_size_dt_strings(old));
371 fdt_set_off_dt_strings(new, strings_off);
372 fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
Kumar Galac8ab7052007-10-24 11:04:22 -0500373}
374
375int fdt_open_into(const void *fdt, void *buf, int bufsize)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400376{
377 int err;
Kumar Galac8ab7052007-10-24 11:04:22 -0500378 int mem_rsv_size, struct_size;
379 int newsize;
David Gibsonb4141b82008-07-07 10:10:48 +1000380 const char *fdtstart = fdt;
381 const char *fdtend = fdtstart + fdt_totalsize(fdt);
382 char *tmp;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400383
David Gibson9457d9d2008-07-09 14:10:24 +1000384 FDT_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400385
Kumar Galac8ab7052007-10-24 11:04:22 -0500386 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
387 * sizeof(struct fdt_reserve_entry);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400388
Kumar Galac8ab7052007-10-24 11:04:22 -0500389 if (fdt_version(fdt) >= 17) {
390 struct_size = fdt_size_dt_struct(fdt);
391 } else {
392 struct_size = 0;
393 while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
394 ;
David Gibson82c10232009-02-06 14:03:24 +1100395 if (struct_size < 0)
396 return struct_size;
Kumar Galac8ab7052007-10-24 11:04:22 -0500397 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400398
David Gibson9457d9d2008-07-09 14:10:24 +1000399 if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
Kumar Galac8ab7052007-10-24 11:04:22 -0500400 /* no further work necessary */
401 err = fdt_move(fdt, buf, bufsize);
402 if (err)
403 return err;
404 fdt_set_version(buf, 17);
405 fdt_set_size_dt_struct(buf, struct_size);
406 fdt_set_totalsize(buf, bufsize);
407 return 0;
408 }
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400409
Kumar Galac8ab7052007-10-24 11:04:22 -0500410 /* Need to reorder */
David Gibson9457d9d2008-07-09 14:10:24 +1000411 newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
Kumar Galac8ab7052007-10-24 11:04:22 -0500412 + struct_size + fdt_size_dt_strings(fdt);
413
414 if (bufsize < newsize)
415 return -FDT_ERR_NOSPACE;
416
David Gibsonb4141b82008-07-07 10:10:48 +1000417 /* First attempt to build converted tree at beginning of buffer */
418 tmp = buf;
419 /* But if that overlaps with the old tree... */
420 if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
421 /* Try right after the old tree instead */
David Gibson870fa472008-07-07 10:14:15 +1000422 tmp = (char *)(uintptr_t)fdtend;
David Gibsonb4141b82008-07-07 10:10:48 +1000423 if ((tmp + newsize) > ((char *)buf + bufsize))
Kumar Galac8ab7052007-10-24 11:04:22 -0500424 return -FDT_ERR_NOSPACE;
425 }
426
David Gibson9457d9d2008-07-09 14:10:24 +1000427 _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
Kumar Galac8ab7052007-10-24 11:04:22 -0500428 memmove(buf, tmp, newsize);
429
430 fdt_set_magic(buf, FDT_MAGIC);
431 fdt_set_totalsize(buf, bufsize);
432 fdt_set_version(buf, 17);
433 fdt_set_last_comp_version(buf, 16);
434 fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400435
436 return 0;
437}
438
439int fdt_pack(void *fdt)
440{
Kumar Galac8ab7052007-10-24 11:04:22 -0500441 int mem_rsv_size;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400442
David Gibson9457d9d2008-07-09 14:10:24 +1000443 FDT_RW_CHECK_HEADER(fdt);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400444
Kumar Galac8ab7052007-10-24 11:04:22 -0500445 mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
446 * sizeof(struct fdt_reserve_entry);
David Gibson9457d9d2008-07-09 14:10:24 +1000447 _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
448 fdt_set_totalsize(fdt, _fdt_data_size(fdt));
Kumar Galac8ab7052007-10-24 11:04:22 -0500449
Gerald Van Baren9e61ed82007-03-31 12:00:56 -0400450 return 0;
451}