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