2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
6 #include <libfdt_env.h>
15 #include "libfdt_internal.h"
17 int fdt_check_header(const void *fdt)
19 if (fdt_magic(fdt) == FDT_MAGIC) {
21 if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
22 return -FDT_ERR_BADVERSION;
23 if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
24 return -FDT_ERR_BADVERSION;
25 } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
26 /* Unfinished sequential-write blob */
27 if (fdt_size_dt_struct(fdt) == 0)
28 return -FDT_ERR_BADSTATE;
30 return -FDT_ERR_BADMAGIC;
36 const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
38 unsigned absoffset = offset + fdt_off_dt_struct(fdt);
40 if ((absoffset < offset)
41 || ((absoffset + len) < absoffset)
42 || (absoffset + len) > fdt_totalsize(fdt))
45 if (fdt_version(fdt) >= 0x11)
46 if (((offset + len) < offset)
47 || ((offset + len) > fdt_size_dt_struct(fdt)))
50 return _fdt_offset_ptr(fdt, offset);
53 uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
55 const fdt32_t *tagp, *lenp;
57 int offset = startoffset;
60 *nextoffset = -FDT_ERR_TRUNCATED;
61 tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
63 return FDT_END; /* premature end */
64 tag = fdt32_to_cpu(*tagp);
65 offset += FDT_TAGSIZE;
67 *nextoffset = -FDT_ERR_BADSTRUCTURE;
72 p = fdt_offset_ptr(fdt, offset++, 1);
73 } while (p && (*p != '\0'));
75 return FDT_END; /* premature end */
79 lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
81 return FDT_END; /* premature end */
82 /* skip-name offset, length and value */
83 offset += sizeof(struct fdt_property) - FDT_TAGSIZE
84 + fdt32_to_cpu(*lenp);
96 if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
97 return FDT_END; /* premature end */
99 *nextoffset = FDT_TAGALIGN(offset);
103 int _fdt_check_node_offset(const void *fdt, int offset)
105 if ((offset < 0) || (offset % FDT_TAGSIZE)
106 || (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE))
107 return -FDT_ERR_BADOFFSET;
112 int _fdt_check_prop_offset(const void *fdt, int offset)
114 if ((offset < 0) || (offset % FDT_TAGSIZE)
115 || (fdt_next_tag(fdt, offset, &offset) != FDT_PROP))
116 return -FDT_ERR_BADOFFSET;
121 int fdt_next_node(const void *fdt, int offset, int *depth)
127 if ((nextoffset = _fdt_check_node_offset(fdt, offset)) < 0)
132 tag = fdt_next_tag(fdt, offset, &nextoffset);
145 if (depth && ((--(*depth)) < 0))
150 if ((nextoffset >= 0)
151 || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
152 return -FDT_ERR_NOTFOUND;
156 } while (tag != FDT_BEGIN_NODE);
161 int fdt_first_subnode(const void *fdt, int offset)
165 offset = fdt_next_node(fdt, offset, &depth);
166 if (offset < 0 || depth != 1)
167 return -FDT_ERR_NOTFOUND;
172 int fdt_next_subnode(const void *fdt, int offset)
177 * With respect to the parent, the depth of the next subnode will be
178 * the same as the last.
181 offset = fdt_next_node(fdt, offset, &depth);
182 if (offset < 0 || depth < 1)
183 return -FDT_ERR_NOTFOUND;
189 const char *_fdt_find_string(const char *strtab, int tabsize, const char *s)
191 int len = strlen(s) + 1;
192 const char *last = strtab + tabsize - len;
195 for (p = strtab; p <= last; p++)
196 if (memcmp(p, s, len) == 0)
201 int fdt_move(const void *fdt, void *buf, int bufsize)
203 FDT_CHECK_HEADER(fdt);
205 if (fdt_totalsize(fdt) > bufsize)
206 return -FDT_ERR_NOSPACE;
208 memmove(buf, fdt, fdt_totalsize(fdt));