2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include "libfdt_env.h"
24 #include "libfdt_internal.h"
26 int _fdt_check_header(const void *fdt)
28 if (fdt_magic(fdt) == FDT_MAGIC) {
30 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
31 return -FDT_ERR_BADVERSION;
32 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
33 return -FDT_ERR_BADVERSION;
34 } else if (fdt_magic(fdt) == SW_MAGIC) {
35 /* Unfinished sequential-write blob */
36 if (fdt_size_dt_struct(fdt) == 0)
37 return -FDT_ERR_BADSTATE;
39 return -FDT_ERR_BADMAGIC;
45 void *fdt_offset_ptr(const void *fdt, int offset, int len)
49 if (fdt_version(fdt) >= 0x11)
50 if (((offset + len) < offset)
51 || ((offset + len) > fdt_size_dt_struct(fdt)))
54 p = _fdt_offset_ptr(fdt, offset);
61 uint32_t _fdt_next_tag(const void *fdt, int offset, int *nextoffset)
63 const uint32_t *tagp, *lenp;
67 if (offset % FDT_TAGSIZE)
70 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
72 return FDT_END; /* premature end */
73 tag = fdt32_to_cpu(*tagp);
74 offset += FDT_TAGSIZE;
80 p = fdt_offset_ptr(fdt, offset++, 1);
81 } while (p && (*p != '\0'));
86 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
89 /* skip name offset, length and value */
90 offset += 2*FDT_TAGSIZE + fdt32_to_cpu(*lenp);
95 *nextoffset = ALIGN(offset, FDT_TAGSIZE);
100 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
102 int len = strlen(s) + 1;
103 const char *last = strtab + tabsize - len;
106 for (p = strtab; p <= last; p++)
107 if (memeq(p, s, len))
112 int fdt_move(const void *fdt, void *buf, int bufsize)
114 int err = _fdt_check_header(fdt);
119 if (fdt_totalsize(fdt) > bufsize)
120 return -FDT_ERR_NOSPACE;
122 memmove(buf, fdt, fdt_totalsize(fdt));