fdt_rw.c revision 1.1.1.2 1 1.1.1.2 skrll /* $NetBSD: fdt_rw.c,v 1.1.1.2 2017/06/08 15:53:12 skrll Exp $ */
2 1.1.1.2 skrll
3 1.1 macallan /*
4 1.1 macallan * libfdt - Flat Device Tree manipulation
5 1.1 macallan * Copyright (C) 2006 David Gibson, IBM Corporation.
6 1.1 macallan *
7 1.1 macallan * libfdt is dual licensed: you can use it either under the terms of
8 1.1 macallan * the GPL, or the BSD license, at your option.
9 1.1 macallan *
10 1.1 macallan * a) This library is free software; you can redistribute it and/or
11 1.1 macallan * modify it under the terms of the GNU General Public License as
12 1.1 macallan * published by the Free Software Foundation; either version 2 of the
13 1.1 macallan * License, or (at your option) any later version.
14 1.1 macallan *
15 1.1 macallan * This library is distributed in the hope that it will be useful,
16 1.1 macallan * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 1.1 macallan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 1.1 macallan * GNU General Public License for more details.
19 1.1 macallan *
20 1.1 macallan * You should have received a copy of the GNU General Public
21 1.1 macallan * License along with this library; if not, write to the Free
22 1.1 macallan * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
23 1.1 macallan * MA 02110-1301 USA
24 1.1 macallan *
25 1.1 macallan * Alternatively,
26 1.1 macallan *
27 1.1 macallan * b) Redistribution and use in source and binary forms, with or
28 1.1 macallan * without modification, are permitted provided that the following
29 1.1 macallan * conditions are met:
30 1.1 macallan *
31 1.1 macallan * 1. Redistributions of source code must retain the above
32 1.1 macallan * copyright notice, this list of conditions and the following
33 1.1 macallan * disclaimer.
34 1.1 macallan * 2. Redistributions in binary form must reproduce the above
35 1.1 macallan * copyright notice, this list of conditions and the following
36 1.1 macallan * disclaimer in the documentation and/or other materials
37 1.1 macallan * provided with the distribution.
38 1.1 macallan *
39 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
40 1.1 macallan * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
41 1.1 macallan * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
42 1.1 macallan * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43 1.1 macallan * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
44 1.1 macallan * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 1.1 macallan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 1.1 macallan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 1.1 macallan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
49 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
50 1.1 macallan * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
51 1.1 macallan * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
52 1.1 macallan */
53 1.1 macallan #include "libfdt_env.h"
54 1.1 macallan
55 1.1 macallan #include <fdt.h>
56 1.1 macallan #include <libfdt.h>
57 1.1 macallan
58 1.1 macallan #include "libfdt_internal.h"
59 1.1 macallan
60 1.1 macallan static int _fdt_blocks_misordered(const void *fdt,
61 1.1 macallan int mem_rsv_size, int struct_size)
62 1.1 macallan {
63 1.1 macallan return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
64 1.1 macallan || (fdt_off_dt_struct(fdt) <
65 1.1 macallan (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
66 1.1 macallan || (fdt_off_dt_strings(fdt) <
67 1.1 macallan (fdt_off_dt_struct(fdt) + struct_size))
68 1.1 macallan || (fdt_totalsize(fdt) <
69 1.1 macallan (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
70 1.1 macallan }
71 1.1 macallan
72 1.1 macallan static int _fdt_rw_check_header(void *fdt)
73 1.1 macallan {
74 1.1 macallan FDT_CHECK_HEADER(fdt);
75 1.1 macallan
76 1.1 macallan if (fdt_version(fdt) < 17)
77 1.1 macallan return -FDT_ERR_BADVERSION;
78 1.1 macallan if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
79 1.1 macallan fdt_size_dt_struct(fdt)))
80 1.1 macallan return -FDT_ERR_BADLAYOUT;
81 1.1 macallan if (fdt_version(fdt) > 17)
82 1.1 macallan fdt_set_version(fdt, 17);
83 1.1 macallan
84 1.1 macallan return 0;
85 1.1 macallan }
86 1.1 macallan
87 1.1 macallan #define FDT_RW_CHECK_HEADER(fdt) \
88 1.1 macallan { \
89 1.1 macallan int __err; \
90 1.1 macallan if ((__err = _fdt_rw_check_header(fdt)) != 0) \
91 1.1 macallan return __err; \
92 1.1 macallan }
93 1.1 macallan
94 1.1 macallan static inline int _fdt_data_size(void *fdt)
95 1.1 macallan {
96 1.1 macallan return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
97 1.1 macallan }
98 1.1 macallan
99 1.1 macallan static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
100 1.1 macallan {
101 1.1 macallan char *p = splicepoint;
102 1.1 macallan char *end = (char *)fdt + _fdt_data_size(fdt);
103 1.1 macallan
104 1.1 macallan if (((p + oldlen) < p) || ((p + oldlen) > end))
105 1.1 macallan return -FDT_ERR_BADOFFSET;
106 1.1 macallan if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt))
107 1.1 macallan return -FDT_ERR_BADOFFSET;
108 1.1 macallan if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
109 1.1 macallan return -FDT_ERR_NOSPACE;
110 1.1 macallan memmove(p + newlen, p + oldlen, end - p - oldlen);
111 1.1 macallan return 0;
112 1.1 macallan }
113 1.1 macallan
114 1.1 macallan static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
115 1.1 macallan int oldn, int newn)
116 1.1 macallan {
117 1.1 macallan int delta = (newn - oldn) * sizeof(*p);
118 1.1 macallan int err;
119 1.1 macallan err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
120 1.1 macallan if (err)
121 1.1 macallan return err;
122 1.1 macallan fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
123 1.1 macallan fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
124 1.1 macallan return 0;
125 1.1 macallan }
126 1.1 macallan
127 1.1 macallan static int _fdt_splice_struct(void *fdt, void *p,
128 1.1 macallan int oldlen, int newlen)
129 1.1 macallan {
130 1.1 macallan int delta = newlen - oldlen;
131 1.1 macallan int err;
132 1.1 macallan
133 1.1 macallan if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
134 1.1 macallan return err;
135 1.1 macallan
136 1.1 macallan fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
137 1.1 macallan fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
138 1.1 macallan return 0;
139 1.1 macallan }
140 1.1 macallan
141 1.1 macallan static int _fdt_splice_string(void *fdt, int newlen)
142 1.1 macallan {
143 1.1 macallan void *p = (char *)fdt
144 1.1 macallan + fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
145 1.1 macallan int err;
146 1.1 macallan
147 1.1 macallan if ((err = _fdt_splice(fdt, p, 0, newlen)))
148 1.1 macallan return err;
149 1.1 macallan
150 1.1 macallan fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
151 1.1 macallan return 0;
152 1.1 macallan }
153 1.1 macallan
154 1.1 macallan static int _fdt_find_add_string(void *fdt, const char *s)
155 1.1 macallan {
156 1.1 macallan char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
157 1.1 macallan const char *p;
158 1.1 macallan char *new;
159 1.1 macallan int len = strlen(s) + 1;
160 1.1 macallan int err;
161 1.1 macallan
162 1.1 macallan p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
163 1.1 macallan if (p)
164 1.1 macallan /* found it */
165 1.1 macallan return (p - strtab);
166 1.1 macallan
167 1.1 macallan new = strtab + fdt_size_dt_strings(fdt);
168 1.1 macallan err = _fdt_splice_string(fdt, len);
169 1.1 macallan if (err)
170 1.1 macallan return err;
171 1.1 macallan
172 1.1 macallan memcpy(new, s, len);
173 1.1 macallan return (new - strtab);
174 1.1 macallan }
175 1.1 macallan
176 1.1 macallan int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
177 1.1 macallan {
178 1.1 macallan struct fdt_reserve_entry *re;
179 1.1 macallan int err;
180 1.1 macallan
181 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
182 1.1 macallan
183 1.1 macallan re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
184 1.1 macallan err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
185 1.1 macallan if (err)
186 1.1 macallan return err;
187 1.1 macallan
188 1.1 macallan re->address = cpu_to_fdt64(address);
189 1.1 macallan re->size = cpu_to_fdt64(size);
190 1.1 macallan return 0;
191 1.1 macallan }
192 1.1 macallan
193 1.1 macallan int fdt_del_mem_rsv(void *fdt, int n)
194 1.1 macallan {
195 1.1 macallan struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
196 1.1 macallan
197 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
198 1.1 macallan
199 1.1 macallan if (n >= fdt_num_mem_rsv(fdt))
200 1.1 macallan return -FDT_ERR_NOTFOUND;
201 1.1 macallan
202 1.1.1.2 skrll return _fdt_splice_mem_rsv(fdt, re, 1, 0);
203 1.1 macallan }
204 1.1 macallan
205 1.1 macallan static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
206 1.1 macallan int len, struct fdt_property **prop)
207 1.1 macallan {
208 1.1 macallan int oldlen;
209 1.1 macallan int err;
210 1.1 macallan
211 1.1 macallan *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
212 1.1 macallan if (! (*prop))
213 1.1 macallan return oldlen;
214 1.1 macallan
215 1.1 macallan if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
216 1.1 macallan FDT_TAGALIGN(len))))
217 1.1 macallan return err;
218 1.1 macallan
219 1.1 macallan (*prop)->len = cpu_to_fdt32(len);
220 1.1 macallan return 0;
221 1.1 macallan }
222 1.1 macallan
223 1.1 macallan static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
224 1.1 macallan int len, struct fdt_property **prop)
225 1.1 macallan {
226 1.1 macallan int proplen;
227 1.1 macallan int nextoffset;
228 1.1 macallan int namestroff;
229 1.1 macallan int err;
230 1.1 macallan
231 1.1 macallan if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
232 1.1 macallan return nextoffset;
233 1.1 macallan
234 1.1 macallan namestroff = _fdt_find_add_string(fdt, name);
235 1.1 macallan if (namestroff < 0)
236 1.1 macallan return namestroff;
237 1.1 macallan
238 1.1 macallan *prop = _fdt_offset_ptr_w(fdt, nextoffset);
239 1.1 macallan proplen = sizeof(**prop) + FDT_TAGALIGN(len);
240 1.1 macallan
241 1.1 macallan err = _fdt_splice_struct(fdt, *prop, 0, proplen);
242 1.1 macallan if (err)
243 1.1 macallan return err;
244 1.1 macallan
245 1.1 macallan (*prop)->tag = cpu_to_fdt32(FDT_PROP);
246 1.1 macallan (*prop)->nameoff = cpu_to_fdt32(namestroff);
247 1.1 macallan (*prop)->len = cpu_to_fdt32(len);
248 1.1 macallan return 0;
249 1.1 macallan }
250 1.1 macallan
251 1.1 macallan int fdt_set_name(void *fdt, int nodeoffset, const char *name)
252 1.1 macallan {
253 1.1 macallan char *namep;
254 1.1 macallan int oldlen, newlen;
255 1.1 macallan int err;
256 1.1 macallan
257 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
258 1.1 macallan
259 1.1 macallan namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
260 1.1 macallan if (!namep)
261 1.1 macallan return oldlen;
262 1.1 macallan
263 1.1 macallan newlen = strlen(name);
264 1.1 macallan
265 1.1 macallan err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
266 1.1 macallan FDT_TAGALIGN(newlen+1));
267 1.1 macallan if (err)
268 1.1 macallan return err;
269 1.1 macallan
270 1.1 macallan memcpy(namep, name, newlen+1);
271 1.1 macallan return 0;
272 1.1 macallan }
273 1.1 macallan
274 1.1 macallan int fdt_setprop(void *fdt, int nodeoffset, const char *name,
275 1.1 macallan const void *val, int len)
276 1.1 macallan {
277 1.1 macallan struct fdt_property *prop;
278 1.1 macallan int err;
279 1.1 macallan
280 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
281 1.1 macallan
282 1.1 macallan err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
283 1.1 macallan if (err == -FDT_ERR_NOTFOUND)
284 1.1 macallan err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
285 1.1 macallan if (err)
286 1.1 macallan return err;
287 1.1 macallan
288 1.1.1.2 skrll if (len)
289 1.1.1.2 skrll memcpy(prop->data, val, len);
290 1.1 macallan return 0;
291 1.1 macallan }
292 1.1 macallan
293 1.1 macallan int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
294 1.1 macallan const void *val, int len)
295 1.1 macallan {
296 1.1 macallan struct fdt_property *prop;
297 1.1 macallan int err, oldlen, newlen;
298 1.1 macallan
299 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
300 1.1 macallan
301 1.1 macallan prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
302 1.1 macallan if (prop) {
303 1.1 macallan newlen = len + oldlen;
304 1.1 macallan err = _fdt_splice_struct(fdt, prop->data,
305 1.1 macallan FDT_TAGALIGN(oldlen),
306 1.1 macallan FDT_TAGALIGN(newlen));
307 1.1 macallan if (err)
308 1.1 macallan return err;
309 1.1 macallan prop->len = cpu_to_fdt32(newlen);
310 1.1 macallan memcpy(prop->data + oldlen, val, len);
311 1.1 macallan } else {
312 1.1 macallan err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
313 1.1 macallan if (err)
314 1.1 macallan return err;
315 1.1 macallan memcpy(prop->data, val, len);
316 1.1 macallan }
317 1.1 macallan return 0;
318 1.1 macallan }
319 1.1 macallan
320 1.1 macallan int fdt_delprop(void *fdt, int nodeoffset, const char *name)
321 1.1 macallan {
322 1.1 macallan struct fdt_property *prop;
323 1.1 macallan int len, proplen;
324 1.1 macallan
325 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
326 1.1 macallan
327 1.1 macallan prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
328 1.1 macallan if (! prop)
329 1.1 macallan return len;
330 1.1 macallan
331 1.1 macallan proplen = sizeof(*prop) + FDT_TAGALIGN(len);
332 1.1 macallan return _fdt_splice_struct(fdt, prop, proplen, 0);
333 1.1 macallan }
334 1.1 macallan
335 1.1 macallan int fdt_add_subnode_namelen(void *fdt, int parentoffset,
336 1.1 macallan const char *name, int namelen)
337 1.1 macallan {
338 1.1 macallan struct fdt_node_header *nh;
339 1.1 macallan int offset, nextoffset;
340 1.1 macallan int nodelen;
341 1.1 macallan int err;
342 1.1 macallan uint32_t tag;
343 1.1 macallan fdt32_t *endtag;
344 1.1 macallan
345 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
346 1.1 macallan
347 1.1 macallan offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
348 1.1 macallan if (offset >= 0)
349 1.1 macallan return -FDT_ERR_EXISTS;
350 1.1 macallan else if (offset != -FDT_ERR_NOTFOUND)
351 1.1 macallan return offset;
352 1.1 macallan
353 1.1 macallan /* Try to place the new node after the parent's properties */
354 1.1 macallan fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
355 1.1 macallan do {
356 1.1 macallan offset = nextoffset;
357 1.1 macallan tag = fdt_next_tag(fdt, offset, &nextoffset);
358 1.1 macallan } while ((tag == FDT_PROP) || (tag == FDT_NOP));
359 1.1 macallan
360 1.1 macallan nh = _fdt_offset_ptr_w(fdt, offset);
361 1.1 macallan nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
362 1.1 macallan
363 1.1 macallan err = _fdt_splice_struct(fdt, nh, 0, nodelen);
364 1.1 macallan if (err)
365 1.1 macallan return err;
366 1.1 macallan
367 1.1 macallan nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
368 1.1 macallan memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
369 1.1 macallan memcpy(nh->name, name, namelen);
370 1.1 macallan endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
371 1.1 macallan *endtag = cpu_to_fdt32(FDT_END_NODE);
372 1.1 macallan
373 1.1 macallan return offset;
374 1.1 macallan }
375 1.1 macallan
376 1.1 macallan int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
377 1.1 macallan {
378 1.1 macallan return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
379 1.1 macallan }
380 1.1 macallan
381 1.1 macallan int fdt_del_node(void *fdt, int nodeoffset)
382 1.1 macallan {
383 1.1 macallan int endoffset;
384 1.1 macallan
385 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
386 1.1 macallan
387 1.1 macallan endoffset = _fdt_node_end_offset(fdt, nodeoffset);
388 1.1 macallan if (endoffset < 0)
389 1.1 macallan return endoffset;
390 1.1 macallan
391 1.1 macallan return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
392 1.1 macallan endoffset - nodeoffset, 0);
393 1.1 macallan }
394 1.1 macallan
395 1.1 macallan static void _fdt_packblocks(const char *old, char *new,
396 1.1 macallan int mem_rsv_size, int struct_size)
397 1.1 macallan {
398 1.1 macallan int mem_rsv_off, struct_off, strings_off;
399 1.1 macallan
400 1.1 macallan mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
401 1.1 macallan struct_off = mem_rsv_off + mem_rsv_size;
402 1.1 macallan strings_off = struct_off + struct_size;
403 1.1 macallan
404 1.1 macallan memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
405 1.1 macallan fdt_set_off_mem_rsvmap(new, mem_rsv_off);
406 1.1 macallan
407 1.1 macallan memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
408 1.1 macallan fdt_set_off_dt_struct(new, struct_off);
409 1.1 macallan fdt_set_size_dt_struct(new, struct_size);
410 1.1 macallan
411 1.1 macallan memmove(new + strings_off, old + fdt_off_dt_strings(old),
412 1.1 macallan fdt_size_dt_strings(old));
413 1.1 macallan fdt_set_off_dt_strings(new, strings_off);
414 1.1 macallan fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
415 1.1 macallan }
416 1.1 macallan
417 1.1 macallan int fdt_open_into(const void *fdt, void *buf, int bufsize)
418 1.1 macallan {
419 1.1 macallan int err;
420 1.1 macallan int mem_rsv_size, struct_size;
421 1.1 macallan int newsize;
422 1.1 macallan const char *fdtstart = fdt;
423 1.1 macallan const char *fdtend = fdtstart + fdt_totalsize(fdt);
424 1.1 macallan char *tmp;
425 1.1 macallan
426 1.1 macallan FDT_CHECK_HEADER(fdt);
427 1.1 macallan
428 1.1 macallan mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
429 1.1 macallan * sizeof(struct fdt_reserve_entry);
430 1.1 macallan
431 1.1 macallan if (fdt_version(fdt) >= 17) {
432 1.1 macallan struct_size = fdt_size_dt_struct(fdt);
433 1.1 macallan } else {
434 1.1 macallan struct_size = 0;
435 1.1 macallan while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
436 1.1 macallan ;
437 1.1 macallan if (struct_size < 0)
438 1.1 macallan return struct_size;
439 1.1 macallan }
440 1.1 macallan
441 1.1 macallan if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
442 1.1 macallan /* no further work necessary */
443 1.1 macallan err = fdt_move(fdt, buf, bufsize);
444 1.1 macallan if (err)
445 1.1 macallan return err;
446 1.1 macallan fdt_set_version(buf, 17);
447 1.1 macallan fdt_set_size_dt_struct(buf, struct_size);
448 1.1 macallan fdt_set_totalsize(buf, bufsize);
449 1.1 macallan return 0;
450 1.1 macallan }
451 1.1 macallan
452 1.1 macallan /* Need to reorder */
453 1.1 macallan newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
454 1.1 macallan + struct_size + fdt_size_dt_strings(fdt);
455 1.1 macallan
456 1.1 macallan if (bufsize < newsize)
457 1.1 macallan return -FDT_ERR_NOSPACE;
458 1.1 macallan
459 1.1 macallan /* First attempt to build converted tree at beginning of buffer */
460 1.1 macallan tmp = buf;
461 1.1 macallan /* But if that overlaps with the old tree... */
462 1.1 macallan if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
463 1.1 macallan /* Try right after the old tree instead */
464 1.1 macallan tmp = (char *)(uintptr_t)fdtend;
465 1.1 macallan if ((tmp + newsize) > ((char *)buf + bufsize))
466 1.1 macallan return -FDT_ERR_NOSPACE;
467 1.1 macallan }
468 1.1 macallan
469 1.1 macallan _fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
470 1.1 macallan memmove(buf, tmp, newsize);
471 1.1 macallan
472 1.1 macallan fdt_set_magic(buf, FDT_MAGIC);
473 1.1 macallan fdt_set_totalsize(buf, bufsize);
474 1.1 macallan fdt_set_version(buf, 17);
475 1.1 macallan fdt_set_last_comp_version(buf, 16);
476 1.1 macallan fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
477 1.1 macallan
478 1.1 macallan return 0;
479 1.1 macallan }
480 1.1 macallan
481 1.1 macallan int fdt_pack(void *fdt)
482 1.1 macallan {
483 1.1 macallan int mem_rsv_size;
484 1.1 macallan
485 1.1 macallan FDT_RW_CHECK_HEADER(fdt);
486 1.1 macallan
487 1.1 macallan mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
488 1.1 macallan * sizeof(struct fdt_reserve_entry);
489 1.1 macallan _fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
490 1.1 macallan fdt_set_totalsize(fdt, _fdt_data_size(fdt));
491 1.1 macallan
492 1.1 macallan return 0;
493 1.1 macallan }
494