xref: /rk3399_rockchip-uboot/lib/libfdt/fdt_wip.c (revision e1c372819040cec511ced88ea990c38b91d0e2a0)
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 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
18 					const char *name, int namelen,
19 					uint32_t idx, const void *val,
20 					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 
30 	if (proplen < (len + idx))
31 		return -FDT_ERR_NOSPACE;
32 
33 	memcpy((char *)propval + idx, val, len);
34 	return 0;
35 }
36 
37 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
38 			const void *val, int len)
39 {
40 	const void *propval;
41 	int proplen;
42 
43 	propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
44 	if (!propval)
45 		return proplen;
46 
47 	if (proplen != len)
48 		return -FDT_ERR_NOSPACE;
49 
50 	return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
51 						   strlen(name), 0,
52 						   val, len);
53 }
54 
55 static void _fdt_nop_region(void *start, int len)
56 {
57 	fdt32_t *p;
58 
59 	for (p = start; (char *)p < ((char *)start + len); p++)
60 		*p = cpu_to_fdt32(FDT_NOP);
61 }
62 
63 int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
64 {
65 	struct fdt_property *prop;
66 	int len;
67 
68 	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
69 	if (!prop)
70 		return len;
71 
72 	_fdt_nop_region(prop, len + sizeof(*prop));
73 
74 	return 0;
75 }
76 
77 int _fdt_node_end_offset(void *fdt, int offset)
78 {
79 	int depth = 0;
80 
81 	while ((offset >= 0) && (depth >= 0))
82 		offset = fdt_next_node(fdt, offset, &depth);
83 
84 	return offset;
85 }
86 
87 int 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 
95 	_fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
96 			endoffset - nodeoffset);
97 	return 0;
98 }
99