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(void *fdt, int nodeoffset, const char *name,
18 const void *val, int len)
23 propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
28 return -FDT_ERR_NOSPACE;
30 memcpy(propval, val, len);
34 static void _fdt_nop_region(void *start, int len)
38 for (p = start; (char *)p < ((char *)start + len); p++)
39 *p = cpu_to_fdt32(FDT_NOP);
42 int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
44 struct fdt_property *prop;
47 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
51 _fdt_nop_region(prop, len + sizeof(*prop));
56 int _fdt_node_end_offset(void *fdt, int offset)
60 while ((offset >= 0) && (depth >= 0))
61 offset = fdt_next_node(fdt, offset, &depth);
66 int fdt_nop_node(void *fdt, int nodeoffset)
70 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
74 _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
75 endoffset - nodeoffset);
79 #define FDT_MAX_DEPTH 32
81 static int str_in_list(const char *str, char * const list[], int count)
85 for (i = 0; i < count; i++)
86 if (!strcmp(list[i], str))
92 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
93 char * const exc_prop[], int exc_prop_count,
94 struct fdt_region region[], int max_regions,
95 char *path, int path_len, int add_string_tab)
97 int stack[FDT_MAX_DEPTH];
105 int base = fdt_off_dt_struct(fdt);
110 const struct fdt_property *prop;
119 tag = fdt_next_tag(fdt, offset, &nextoffset);
120 stop_at = nextoffset;
126 prop = fdt_get_property_by_offset(fdt, offset, NULL);
127 str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
128 if (str_in_list(str, exc_prop, exc_prop_count))
139 if (depth == FDT_MAX_DEPTH)
140 return -FDT_ERR_BADSTRUCTURE;
141 name = fdt_get_name(fdt, offset, &len);
142 if (end - path + 2 + len >= path_len)
143 return -FDT_ERR_NOSPACE;
151 if (str_in_list(path, inc, inc_count))
162 want = stack[depth--];
163 while (end > path && *--end != '/')
173 if (include && start == -1) {
174 /* Should we merge with previous? */
175 if (count && count <= max_regions &&
176 offset == region[count - 1].offset +
177 region[count - 1].size - base)
178 start = region[--count].offset - base;
183 if (!include && start != -1) {
184 if (count < max_regions) {
185 region[count].offset = base + start;
186 region[count].size = stop_at - start;
191 } while (tag != FDT_END);
193 if (nextoffset != fdt_size_dt_struct(fdt))
194 return -FDT_ERR_BADLAYOUT;
196 /* Add a region for the END tag and the string table */
197 if (count < max_regions) {
198 region[count].offset = base + start;
199 region[count].size = nextoffset - start;
201 region[count].size += fdt_size_dt_strings(fdt);