blob: 45fb9641206f4f74948c639004e7c8b92958a54e [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier3eaedea2013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04005 */
Robert P. J. Day55019c52016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04007
Bartlomiej Sieka61250862008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04009#include <fdt.h>
10#include <libfdt.h>
Bartlomiej Sieka61250862008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040014
15#include "libfdt_internal.h"
16
Maxime Ripard3559c792016-07-05 10:26:43 +020017int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
18 const char *name, int namelen,
Simon Glassdfdcd6f2016-10-02 17:59:27 -060019 uint32_t idx, const void *val,
Maxime Ripard3559c792016-07-05 10:26:43 +020020 int len)
21{
22 void *propval;
23 int proplen;
24
25 propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
26 &proplen);
27 if (!propval)
28 return proplen;
29
Simon Glassdfdcd6f2016-10-02 17:59:27 -060030 if (proplen < (len + idx))
Maxime Ripard3559c792016-07-05 10:26:43 +020031 return -FDT_ERR_NOSPACE;
32
Simon Glassdfdcd6f2016-10-02 17:59:27 -060033 memcpy((char *)propval + idx, val, len);
Maxime Ripard3559c792016-07-05 10:26:43 +020034 return 0;
35}
36
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040037int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
38 const void *val, int len)
39{
Maxime Ripard3559c792016-07-05 10:26:43 +020040 const void *propval;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040041 int proplen;
42
Maxime Ripard3559c792016-07-05 10:26:43 +020043 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
Robert P. J. Day55019c52016-05-23 05:40:55 -040044 if (!propval)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040045 return proplen;
46
47 if (proplen != len)
48 return -FDT_ERR_NOSPACE;
49
Maxime Ripard3559c792016-07-05 10:26:43 +020050 return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
51 strlen(name), 0,
52 val, len);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040053}
54
David Gibson9457d9d2008-07-09 14:10:24 +100055static void _fdt_nop_region(void *start, int len)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040056{
Kim Phillips38c38152013-01-16 13:59:50 +000057 fdt32_t *p;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040058
David Gibsonb4141b82008-07-07 10:10:48 +100059 for (p = start; (char *)p < ((char *)start + len); p++)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040060 *p = cpu_to_fdt32(FDT_NOP);
61}
62
63int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
64{
65 struct fdt_property *prop;
66 int len;
67
Kumar Galac8ab7052007-10-24 11:04:22 -050068 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Robert P. J. Day55019c52016-05-23 05:40:55 -040069 if (!prop)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040070 return len;
71
David Gibson9457d9d2008-07-09 14:10:24 +100072 _fdt_nop_region(prop, len + sizeof(*prop));
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040073
74 return 0;
75}
76
David Gibson954914a2009-02-06 14:01:56 +110077int _fdt_node_end_offset(void *fdt, int offset)
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040078{
David Gibson954914a2009-02-06 14:01:56 +110079 int depth = 0;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040080
David Gibson954914a2009-02-06 14:01:56 +110081 while ((offset >= 0) && (depth >= 0))
82 offset = fdt_next_node(fdt, offset, &depth);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040083
David Gibson954914a2009-02-06 14:01:56 +110084 return offset;
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040085}
86
87int fdt_nop_node(void *fdt, int nodeoffset)
88{
89 int endoffset;
90
91 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
92 if (endoffset < 0)
93 return endoffset;
94
David Gibson9457d9d2008-07-09 14:10:24 +100095 _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
96 endoffset - nodeoffset);
Gerald Van Barenc077a132007-04-14 22:46:41 -040097 return 0;
98}
Simon Glassb0167022013-06-13 15:10:08 -070099
100#define FDT_MAX_DEPTH 32
101
102static int str_in_list(const char *str, char * const list[], int count)
103{
104 int i;
105
106 for (i = 0; i < count; i++)
107 if (!strcmp(list[i], str))
108 return 1;
109
110 return 0;
111}
112
113int 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)
117{
118 int stack[FDT_MAX_DEPTH];
119 char *end;
120 int nextoffset = 0;
121 uint32_t tag;
122 int count = 0;
123 int start = -1;
124 int depth = -1;
125 int want = 0;
126 int base = fdt_off_dt_struct(fdt);
127
128 end = path;
129 *end = '\0';
130 do {
131 const struct fdt_property *prop;
132 const char *name;
133 const char *str;
134 int include = 0;
135 int stop_at = 0;
136 int offset;
137 int len;
138
139 offset = nextoffset;
140 tag = fdt_next_tag(fdt, offset, &nextoffset);
141 stop_at = nextoffset;
142
143 switch (tag) {
144 case FDT_PROP:
145 include = want >= 2;
146 stop_at = offset;
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))
150 include = 0;
151 break;
152
153 case FDT_NOP:
154 include = want >= 2;
155 stop_at = offset;
156 break;
157
158 case FDT_BEGIN_NODE:
159 depth++;
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;
165 if (end != path + 1)
166 *end++ = '/';
167 strcpy(end, name);
168 end += len;
169 stack[depth] = want;
170 if (want == 1)
171 stop_at = offset;
172 if (str_in_list(path, inc, inc_count))
173 want = 2;
174 else if (want)
175 want--;
176 else
177 stop_at = offset;
178 include = want;
179 break;
180
181 case FDT_END_NODE:
182 include = want;
183 want = stack[depth--];
184 while (end > path && *--end != '/')
185 ;
186 *end = '\0';
187 break;
188
189 case FDT_END:
190 include = 1;
191 break;
192 }
193
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;
200 else
201 start = offset;
202 }
203
204 if (!include && start != -1) {
205 if (count < max_regions) {
206 region[count].offset = base + start;
207 region[count].size = stop_at - start;
208 }
209 count++;
210 start = -1;
211 }
212 } while (tag != FDT_END);
213
214 if (nextoffset != fdt_size_dt_struct(fdt))
215 return -FDT_ERR_BADLAYOUT;
216
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;
221 if (add_string_tab)
222 region[count].size += fdt_size_dt_strings(fdt);
223 }
224 count++;
225
226 return count;
227}