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_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
18 const char *name, int namelen,
19 uint32_t idx, const void *val,
25 propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
30 if (proplen < (len + idx))
31 return -FDT_ERR_NOSPACE;
33 memcpy((char *)propval + idx, val, len);
37 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
38 const void *val, int len)
43 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
48 return -FDT_ERR_NOSPACE;
50 return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
55 static void _fdt_nop_region(void *start, int len)
59 for (p = start; (char *)p < ((char *)start + len); p++)
60 *p = cpu_to_fdt32(FDT_NOP);
63 int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
65 struct fdt_property *prop;
68 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
72 _fdt_nop_region(prop, len + sizeof(*prop));
77 int _fdt_node_end_offset(void *fdt, int offset)
81 while ((offset >= 0) && (depth >= 0))
82 offset = fdt_next_node(fdt, offset, &depth);
87 int fdt_nop_node(void *fdt, int nodeoffset)
91 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
95 _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
96 endoffset - nodeoffset);
100 #define FDT_MAX_DEPTH 32
102 static int str_in_list(const char *str, char * const list[], int count)
106 for (i = 0; i < count; i++)
107 if (!strcmp(list[i], str))
113 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
114 char * const exc_prop[], int exc_prop_count,
115 struct fdt_region region[], int max_regions,
116 char *path, int path_len, int add_string_tab)
118 int stack[FDT_MAX_DEPTH];
126 int base = fdt_off_dt_struct(fdt);
131 const struct fdt_property *prop;
140 tag = fdt_next_tag(fdt, offset, &nextoffset);
141 stop_at = nextoffset;
147 prop = fdt_get_property_by_offset(fdt, offset, NULL);
148 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
149 if (str_in_list(str, exc_prop, exc_prop_count))
160 if (depth == FDT_MAX_DEPTH)
161 return -FDT_ERR_BADSTRUCTURE;
162 name = fdt_get_name(fdt, offset, &len);
163 if (end - path + 2 + len >= path_len)
164 return -FDT_ERR_NOSPACE;
172 if (str_in_list(path, inc, inc_count))
183 want = stack[depth--];
184 while (end > path && *--end != '/')
194 if (include && start == -1) {
195 /* Should we merge with previous? */
196 if (count && count <= max_regions &&
197 offset == region[count - 1].offset +
198 region[count - 1].size - base)
199 start = region[--count].offset - base;
204 if (!include && start != -1) {
205 if (count < max_regions) {
206 region[count].offset = base + start;
207 region[count].size = stop_at - start;
212 } while (tag != FDT_END);
214 if (nextoffset != fdt_size_dt_struct(fdt))
215 return -FDT_ERR_BADLAYOUT;
217 /* Add a region for the END tag and the string table */
218 if (count < max_regions) {
219 region[count].offset = base + start;
220 region[count].size = nextoffset - start;
222 region[count].size += fdt_size_dt_strings(fdt);