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