xref: /openbmc/u-boot/lib/libfdt/fdt_rw.c (revision 9c21d06c)
1 /*
2  * libfdt - Flat Device Tree manipulation
3  * Copyright (C) 2006 David Gibson, IBM Corporation.
4  * SPDX-License-Identifier:	GPL-2.0+ BSD-2-Clause
5  */
6 #include <libfdt_env.h>
7 
8 #ifndef USE_HOSTCC
9 #include <fdt.h>
10 #include <libfdt.h>
11 #else
12 #include "fdt_host.h"
13 #endif
14 
15 #include "libfdt_internal.h"
16 
17 static int _fdt_blocks_misordered(const void *fdt,
18 			      int mem_rsv_size, int struct_size)
19 {
20 	return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
21 		|| (fdt_off_dt_struct(fdt) <
22 		    (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
23 		|| (fdt_off_dt_strings(fdt) <
24 		    (fdt_off_dt_struct(fdt) + struct_size))
25 		|| (fdt_totalsize(fdt) <
26 		    (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
27 }
28 
29 static int _fdt_rw_check_header(void *fdt)
30 {
31 	FDT_CHECK_HEADER(fdt);
32 
33 	if (fdt_version(fdt) < 17)
34 		return -FDT_ERR_BADVERSION;
35 	if (_fdt_blocks_misordered(fdt, sizeof(struct fdt_reserve_entry),
36 				   fdt_size_dt_struct(fdt)))
37 		return -FDT_ERR_BADLAYOUT;
38 	if (fdt_version(fdt) > 17)
39 		fdt_set_version(fdt, 17);
40 
41 	return 0;
42 }
43 
44 #define FDT_RW_CHECK_HEADER(fdt) \
45 	{ \
46 		int __err; \
47 		if ((__err = _fdt_rw_check_header(fdt)) != 0) \
48 			return __err; \
49 	}
50 
51 static inline int _fdt_data_size(void *fdt)
52 {
53 	return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
54 }
55 
56 static int _fdt_splice(void *fdt, void *splicepoint, int oldlen, int newlen)
57 {
58 	char *p = splicepoint;
59 	char *end = (char *)fdt + _fdt_data_size(fdt);
60 
61 	if (((p + oldlen) < p) || ((p + oldlen) > end))
62 		return -FDT_ERR_BADOFFSET;
63 	if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt))
64 		return -FDT_ERR_BADOFFSET;
65 	if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
66 		return -FDT_ERR_NOSPACE;
67 	memmove(p + newlen, p + oldlen, end - p - oldlen);
68 	return 0;
69 }
70 
71 static int _fdt_splice_mem_rsv(void *fdt, struct fdt_reserve_entry *p,
72 			       int oldn, int newn)
73 {
74 	int delta = (newn - oldn) * sizeof(*p);
75 	int err;
76 	err = _fdt_splice(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
77 	if (err)
78 		return err;
79 	fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
80 	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
81 	return 0;
82 }
83 
84 static int _fdt_splice_struct(void *fdt, void *p,
85 			      int oldlen, int newlen)
86 {
87 	int delta = newlen - oldlen;
88 	int err;
89 
90 	if ((err = _fdt_splice(fdt, p, oldlen, newlen)))
91 		return err;
92 
93 	fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
94 	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
95 	return 0;
96 }
97 
98 static int _fdt_splice_string(void *fdt, int newlen)
99 {
100 	void *p = (char *)fdt
101 		+ fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
102 	int err;
103 
104 	if ((err = _fdt_splice(fdt, p, 0, newlen)))
105 		return err;
106 
107 	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
108 	return 0;
109 }
110 
111 static int _fdt_find_add_string(void *fdt, const char *s)
112 {
113 	char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
114 	const char *p;
115 	char *new;
116 	int len = strlen(s) + 1;
117 	int err;
118 
119 	p = _fdt_find_string(strtab, fdt_size_dt_strings(fdt), s);
120 	if (p)
121 		/* found it */
122 		return (p - strtab);
123 
124 	new = strtab + fdt_size_dt_strings(fdt);
125 	err = _fdt_splice_string(fdt, len);
126 	if (err)
127 		return err;
128 
129 	memcpy(new, s, len);
130 	return (new - strtab);
131 }
132 
133 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
134 {
135 	struct fdt_reserve_entry *re;
136 	int err;
137 
138 	FDT_RW_CHECK_HEADER(fdt);
139 
140 	re = _fdt_mem_rsv_w(fdt, fdt_num_mem_rsv(fdt));
141 	err = _fdt_splice_mem_rsv(fdt, re, 0, 1);
142 	if (err)
143 		return err;
144 
145 	re->address = cpu_to_fdt64(address);
146 	re->size = cpu_to_fdt64(size);
147 	return 0;
148 }
149 
150 int fdt_del_mem_rsv(void *fdt, int n)
151 {
152 	struct fdt_reserve_entry *re = _fdt_mem_rsv_w(fdt, n);
153 
154 	FDT_RW_CHECK_HEADER(fdt);
155 
156 	if (n >= fdt_num_mem_rsv(fdt))
157 		return -FDT_ERR_NOTFOUND;
158 
159 	return _fdt_splice_mem_rsv(fdt, re, 1, 0);
160 }
161 
162 static int _fdt_resize_property(void *fdt, int nodeoffset, const char *name,
163 				int len, struct fdt_property **prop)
164 {
165 	int oldlen;
166 	int err;
167 
168 	*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
169 	if (!*prop)
170 		return oldlen;
171 
172 	if ((err = _fdt_splice_struct(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
173 				      FDT_TAGALIGN(len))))
174 		return err;
175 
176 	(*prop)->len = cpu_to_fdt32(len);
177 	return 0;
178 }
179 
180 static int _fdt_add_property(void *fdt, int nodeoffset, const char *name,
181 			     int len, struct fdt_property **prop)
182 {
183 	int proplen;
184 	int nextoffset;
185 	int namestroff;
186 	int err;
187 
188 	if ((nextoffset = _fdt_check_node_offset(fdt, nodeoffset)) < 0)
189 		return nextoffset;
190 
191 	namestroff = _fdt_find_add_string(fdt, name);
192 	if (namestroff < 0)
193 		return namestroff;
194 
195 	*prop = _fdt_offset_ptr_w(fdt, nextoffset);
196 	proplen = sizeof(**prop) + FDT_TAGALIGN(len);
197 
198 	err = _fdt_splice_struct(fdt, *prop, 0, proplen);
199 	if (err)
200 		return err;
201 
202 	(*prop)->tag = cpu_to_fdt32(FDT_PROP);
203 	(*prop)->nameoff = cpu_to_fdt32(namestroff);
204 	(*prop)->len = cpu_to_fdt32(len);
205 	return 0;
206 }
207 
208 int fdt_set_name(void *fdt, int nodeoffset, const char *name)
209 {
210 	char *namep;
211 	int oldlen, newlen;
212 	int err;
213 
214 	FDT_RW_CHECK_HEADER(fdt);
215 
216 	namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
217 	if (!namep)
218 		return oldlen;
219 
220 	newlen = strlen(name);
221 
222 	err = _fdt_splice_struct(fdt, namep, FDT_TAGALIGN(oldlen+1),
223 				 FDT_TAGALIGN(newlen+1));
224 	if (err)
225 		return err;
226 
227 	memcpy(namep, name, newlen+1);
228 	return 0;
229 }
230 
231 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
232 		const void *val, int len)
233 {
234 	struct fdt_property *prop;
235 	int err;
236 
237 	FDT_RW_CHECK_HEADER(fdt);
238 
239 	err = _fdt_resize_property(fdt, nodeoffset, name, len, &prop);
240 	if (err == -FDT_ERR_NOTFOUND)
241 		err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
242 	if (err)
243 		return err;
244 
245 	memcpy(prop->data, val, len);
246 	return 0;
247 }
248 
249 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
250 		   const void *val, int len)
251 {
252 	struct fdt_property *prop;
253 	int err, oldlen, newlen;
254 
255 	FDT_RW_CHECK_HEADER(fdt);
256 
257 	prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
258 	if (prop) {
259 		newlen = len + oldlen;
260 		err = _fdt_splice_struct(fdt, prop->data,
261 					 FDT_TAGALIGN(oldlen),
262 					 FDT_TAGALIGN(newlen));
263 		if (err)
264 			return err;
265 		prop->len = cpu_to_fdt32(newlen);
266 		memcpy(prop->data + oldlen, val, len);
267 	} else {
268 		err = _fdt_add_property(fdt, nodeoffset, name, len, &prop);
269 		if (err)
270 			return err;
271 		memcpy(prop->data, val, len);
272 	}
273 	return 0;
274 }
275 
276 int fdt_delprop(void *fdt, int nodeoffset, const char *name)
277 {
278 	struct fdt_property *prop;
279 	int len, proplen;
280 
281 	FDT_RW_CHECK_HEADER(fdt);
282 
283 	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
284 	if (!prop)
285 		return len;
286 
287 	proplen = sizeof(*prop) + FDT_TAGALIGN(len);
288 	return _fdt_splice_struct(fdt, prop, proplen, 0);
289 }
290 
291 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
292 			    const char *name, int namelen)
293 {
294 	struct fdt_node_header *nh;
295 	int offset, nextoffset;
296 	int nodelen;
297 	int err;
298 	uint32_t tag;
299 	fdt32_t *endtag;
300 
301 	FDT_RW_CHECK_HEADER(fdt);
302 
303 	offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
304 	if (offset >= 0)
305 		return -FDT_ERR_EXISTS;
306 	else if (offset != -FDT_ERR_NOTFOUND)
307 		return offset;
308 
309 	/* Try to place the new node after the parent's properties */
310 	fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
311 	do {
312 		offset = nextoffset;
313 		tag = fdt_next_tag(fdt, offset, &nextoffset);
314 	} while ((tag == FDT_PROP) || (tag == FDT_NOP));
315 
316 	nh = _fdt_offset_ptr_w(fdt, offset);
317 	nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
318 
319 	err = _fdt_splice_struct(fdt, nh, 0, nodelen);
320 	if (err)
321 		return err;
322 
323 	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
324 	memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
325 	memcpy(nh->name, name, namelen);
326 	endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
327 	*endtag = cpu_to_fdt32(FDT_END_NODE);
328 
329 	return offset;
330 }
331 
332 int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
333 {
334 	return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
335 }
336 
337 int fdt_del_node(void *fdt, int nodeoffset)
338 {
339 	int endoffset;
340 
341 	FDT_RW_CHECK_HEADER(fdt);
342 
343 	endoffset = _fdt_node_end_offset(fdt, nodeoffset);
344 	if (endoffset < 0)
345 		return endoffset;
346 
347 	return _fdt_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset),
348 				  endoffset - nodeoffset, 0);
349 }
350 
351 static void _fdt_packblocks(const char *old, char *new,
352 			    int mem_rsv_size, int struct_size)
353 {
354 	int mem_rsv_off, struct_off, strings_off;
355 
356 	mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
357 	struct_off = mem_rsv_off + mem_rsv_size;
358 	strings_off = struct_off + struct_size;
359 
360 	memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
361 	fdt_set_off_mem_rsvmap(new, mem_rsv_off);
362 
363 	memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
364 	fdt_set_off_dt_struct(new, struct_off);
365 	fdt_set_size_dt_struct(new, struct_size);
366 
367 	memmove(new + strings_off, old + fdt_off_dt_strings(old),
368 		fdt_size_dt_strings(old));
369 	fdt_set_off_dt_strings(new, strings_off);
370 	fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
371 }
372 
373 int fdt_open_into(const void *fdt, void *buf, int bufsize)
374 {
375 	int err;
376 	int mem_rsv_size, struct_size;
377 	int newsize;
378 	const char *fdtstart = fdt;
379 	const char *fdtend = fdtstart + fdt_totalsize(fdt);
380 	char *tmp;
381 
382 	FDT_CHECK_HEADER(fdt);
383 
384 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
385 		* sizeof(struct fdt_reserve_entry);
386 
387 	if (fdt_version(fdt) >= 17) {
388 		struct_size = fdt_size_dt_struct(fdt);
389 	} else {
390 		struct_size = 0;
391 		while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
392 			;
393 		if (struct_size < 0)
394 			return struct_size;
395 	}
396 
397 	if (!_fdt_blocks_misordered(fdt, mem_rsv_size, struct_size)) {
398 		/* no further work necessary */
399 		err = fdt_move(fdt, buf, bufsize);
400 		if (err)
401 			return err;
402 		fdt_set_version(buf, 17);
403 		fdt_set_size_dt_struct(buf, struct_size);
404 		fdt_set_totalsize(buf, bufsize);
405 		return 0;
406 	}
407 
408 	/* Need to reorder */
409 	newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
410 		+ struct_size + fdt_size_dt_strings(fdt);
411 
412 	if (bufsize < newsize)
413 		return -FDT_ERR_NOSPACE;
414 
415 	/* First attempt to build converted tree at beginning of buffer */
416 	tmp = buf;
417 	/* But if that overlaps with the old tree... */
418 	if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
419 		/* Try right after the old tree instead */
420 		tmp = (char *)(uintptr_t)fdtend;
421 		if ((tmp + newsize) > ((char *)buf + bufsize))
422 			return -FDT_ERR_NOSPACE;
423 	}
424 
425 	_fdt_packblocks(fdt, tmp, mem_rsv_size, struct_size);
426 	memmove(buf, tmp, newsize);
427 
428 	fdt_set_magic(buf, FDT_MAGIC);
429 	fdt_set_totalsize(buf, bufsize);
430 	fdt_set_version(buf, 17);
431 	fdt_set_last_comp_version(buf, 16);
432 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
433 
434 	return 0;
435 }
436 
437 int fdt_pack(void *fdt)
438 {
439 	int mem_rsv_size;
440 
441 	FDT_RW_CHECK_HEADER(fdt);
442 
443 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
444 		* sizeof(struct fdt_reserve_entry);
445 	_fdt_packblocks(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
446 	fdt_set_totalsize(fdt, _fdt_data_size(fdt));
447 
448 	return 0;
449 }
450 
451 int fdt_remove_unused_strings(const void *old, void *new)
452 {
453 	const struct fdt_property *old_prop;
454 	struct fdt_property *new_prop;
455 	int size = fdt_totalsize(old);
456 	int next_offset, offset;
457 	const char *str;
458 	int ret;
459 	int tag = FDT_PROP;
460 
461 	/* Make a copy and remove the strings */
462 	memcpy(new, old, size);
463 	fdt_set_size_dt_strings(new, 0);
464 
465 	/* Add every property name back into the new string table */
466 	for (offset = 0; tag != FDT_END; offset = next_offset) {
467 		tag = fdt_next_tag(old, offset, &next_offset);
468 		if (tag != FDT_PROP)
469 			continue;
470 		old_prop = fdt_get_property_by_offset(old, offset, NULL);
471 		new_prop = (struct fdt_property *)(unsigned long)
472 			fdt_get_property_by_offset(new, offset, NULL);
473 		str = fdt_string(old, fdt32_to_cpu(old_prop->nameoff));
474 		ret = _fdt_find_add_string(new, str);
475 		if (ret < 0)
476 			return ret;
477 		new_prop->nameoff = cpu_to_fdt32(ret);
478 	}
479 
480 	return 0;
481 }
482