blob: 9fe988655fe342c49a293e7c68d3bcd83d2105b6 [file] [log] [blame]
Gerald Van Baren35748172007-03-31 12:00:56 -04001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
Roger Meier35084762013-07-27 01:12:38 +02004 * SPDX-License-Identifier: GPL-2.0+ BSD-2-Clause
Gerald Van Baren35748172007-03-31 12:00:56 -04005 */
Robert P. J. Day6feed2a2016-05-23 05:40:55 -04006#include <libfdt_env.h>
Gerald Van Baren35748172007-03-31 12:00:56 -04007
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +01008#ifndef USE_HOSTCC
Gerald Van Baren35748172007-03-31 12:00:56 -04009#include <fdt.h>
10#include <libfdt.h>
Bartlomiej Sieka8cf30802008-02-29 16:00:24 +010011#else
12#include "fdt_host.h"
13#endif
Gerald Van Baren35748172007-03-31 12:00:56 -040014
15#include "libfdt_internal.h"
16
17int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
18 const void *val, int len)
19{
20 void *propval;
21 int proplen;
22
Kumar Gala8d04f022007-10-24 11:04:22 -050023 propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
Robert P. J. Day6feed2a2016-05-23 05:40:55 -040024 if (!propval)
Gerald Van Baren35748172007-03-31 12:00:56 -040025 return proplen;
26
27 if (proplen != len)
28 return -FDT_ERR_NOSPACE;
29
30 memcpy(propval, val, len);
31 return 0;
32}
33
David Gibsonfc7758e2008-07-09 14:10:24 +100034static void _fdt_nop_region(void *start, int len)
Gerald Van Baren35748172007-03-31 12:00:56 -040035{
Kim Phillipsb2ba62a2013-01-16 13:59:50 +000036 fdt32_t *p;
Gerald Van Baren35748172007-03-31 12:00:56 -040037
David Gibsonef4e8ce2008-07-07 10:10:48 +100038 for (p = start; (char *)p < ((char *)start + len); p++)
Gerald Van Baren35748172007-03-31 12:00:56 -040039 *p = cpu_to_fdt32(FDT_NOP);
40}
41
42int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
43{
44 struct fdt_property *prop;
45 int len;
46
Kumar Gala8d04f022007-10-24 11:04:22 -050047 prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
Robert P. J. Day6feed2a2016-05-23 05:40:55 -040048 if (!prop)
Gerald Van Baren35748172007-03-31 12:00:56 -040049 return len;
50
David Gibsonfc7758e2008-07-09 14:10:24 +100051 _fdt_nop_region(prop, len + sizeof(*prop));
Gerald Van Baren35748172007-03-31 12:00:56 -040052
53 return 0;
54}
55
David Gibson2c0b8432009-02-06 14:01:56 +110056int _fdt_node_end_offset(void *fdt, int offset)
Gerald Van Baren35748172007-03-31 12:00:56 -040057{
David Gibson2c0b8432009-02-06 14:01:56 +110058 int depth = 0;
Gerald Van Baren35748172007-03-31 12:00:56 -040059
David Gibson2c0b8432009-02-06 14:01:56 +110060 while ((offset >= 0) && (depth >= 0))
61 offset = fdt_next_node(fdt, offset, &depth);
Gerald Van Baren35748172007-03-31 12:00:56 -040062
David Gibson2c0b8432009-02-06 14:01:56 +110063 return offset;
Gerald Van Baren35748172007-03-31 12:00:56 -040064}
65
66int fdt_nop_node(void *fdt, int nodeoffset)
67{
68 int endoffset;
69
70 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
71 if (endoffset < 0)
72 return endoffset;
73
David Gibsonfc7758e2008-07-09 14:10:24 +100074 _fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
75 endoffset - nodeoffset);
Gerald Van Baren3f9f08c2007-04-14 22:46:41 -040076 return 0;
77}
Simon Glass3e06cd12013-06-13 15:10:08 -070078
79#define FDT_MAX_DEPTH 32
80
81static int str_in_list(const char *str, char * const list[], int count)
82{
83 int i;
84
85 for (i = 0; i < count; i++)
86 if (!strcmp(list[i], str))
87 return 1;
88
89 return 0;
90}
91
92int 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)
96{
97 int stack[FDT_MAX_DEPTH];
98 char *end;
99 int nextoffset = 0;
100 uint32_t tag;
101 int count = 0;
102 int start = -1;
103 int depth = -1;
104 int want = 0;
105 int base = fdt_off_dt_struct(fdt);
106
107 end = path;
108 *end = '\0';
109 do {
110 const struct fdt_property *prop;
111 const char *name;
112 const char *str;
113 int include = 0;
114 int stop_at = 0;
115 int offset;
116 int len;
117
118 offset = nextoffset;
119 tag = fdt_next_tag(fdt, offset, &nextoffset);
120 stop_at = nextoffset;
121
122 switch (tag) {
123 case FDT_PROP:
124 include = want >= 2;
125 stop_at = offset;
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))
129 include = 0;
130 break;
131
132 case FDT_NOP:
133 include = want >= 2;
134 stop_at = offset;
135 break;
136
137 case FDT_BEGIN_NODE:
138 depth++;
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;
144 if (end != path + 1)
145 *end++ = '/';
146 strcpy(end, name);
147 end += len;
148 stack[depth] = want;
149 if (want == 1)
150 stop_at = offset;
151 if (str_in_list(path, inc, inc_count))
152 want = 2;
153 else if (want)
154 want--;
155 else
156 stop_at = offset;
157 include = want;
158 break;
159
160 case FDT_END_NODE:
161 include = want;
162 want = stack[depth--];
163 while (end > path && *--end != '/')
164 ;
165 *end = '\0';
166 break;
167
168 case FDT_END:
169 include = 1;
170 break;
171 }
172
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;
179 else
180 start = offset;
181 }
182
183 if (!include && start != -1) {
184 if (count < max_regions) {
185 region[count].offset = base + start;
186 region[count].size = stop_at - start;
187 }
188 count++;
189 start = -1;
190 }
191 } while (tag != FDT_END);
192
193 if (nextoffset != fdt_size_dt_struct(fdt))
194 return -FDT_ERR_BADLAYOUT;
195
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;
200 if (add_string_tab)
201 region[count].size += fdt_size_dt_strings(fdt);
202 }
203 count++;
204
205 return count;
206}