xref: /optee_os/core/lib/libfdt/fdt_wip.c (revision 01d6a9da27b50dac71b223e65411e8392cbf71cd)
1 // SPDX-License-Identifier: (BSD-2-Clause OR GPL-2.0+)
2 /*
3  * libfdt - Flat Device Tree manipulation
4  * Copyright (C) 2006 David Gibson, IBM Corporation.
5  *
6  * libfdt is dual licensed: you can use it either under the terms of
7  * the GPL, or the BSD license, at your option.
8  *
9  *  a) This library is free software; you can redistribute it and/or
10  *     modify it under the terms of the GNU General Public License as
11  *     published by the Free Software Foundation; either version 2 of the
12  *     License, or (at your option) any later version.
13  *
14  *     This library is distributed in the hope that it will be useful,
15  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *     GNU General Public License for more details.
18  *
19  *     You should have received a copy of the GNU General Public
20  *     License along with this library; if not, write to the Free
21  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
22  *     MA 02110-1301 USA
23  *
24  * Alternatively,
25  *
26  *  b) Redistribution and use in source and binary forms, with or
27  *     without modification, are permitted provided that the following
28  *     conditions are met:
29  *
30  *     1. Redistributions of source code must retain the above
31  *        copyright notice, this list of conditions and the following
32  *        disclaimer.
33  *     2. Redistributions in binary form must reproduce the above
34  *        copyright notice, this list of conditions and the following
35  *        disclaimer in the documentation and/or other materials
36  *        provided with the distribution.
37  *
38  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
39  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
40  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
41  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
43  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
48  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
49  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
50  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  */
52 #include "libfdt_env.h"
53 
54 #include <fdt.h>
55 #include <libfdt.h>
56 
57 #include "libfdt_internal.h"
58 
59 int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
60 					const char *name, int namelen,
61 					uint32_t idx, const void *val,
62 					int len)
63 {
64 	void *propval;
65 	int proplen;
66 
67 	propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
68 					&proplen);
69 	if (!propval)
70 		return proplen;
71 
72 	if (proplen < (len + idx))
73 		return -FDT_ERR_NOSPACE;
74 
75 	memcpy((char *)propval + idx, val, len);
76 	return 0;
77 }
78 
79 int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
80 			const void *val, int len)
81 {
82 	const void *propval;
83 	int proplen;
84 
85 	propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
86 	if (!propval)
87 		return proplen;
88 
89 	if (proplen != len)
90 		return -FDT_ERR_NOSPACE;
91 
92 	return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
93 						   strlen(name), 0,
94 						   val, len);
95 }
96 
97 static void fdt_nop_region_(void *start, int len)
98 {
99 	fdt32_t *p;
100 
101 	for (p = start; (char *)p < ((char *)start + len); p++)
102 		*p = cpu_to_fdt32(FDT_NOP);
103 }
104 
105 int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
106 {
107 	struct fdt_property *prop;
108 	int len;
109 
110 	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
111 	if (!prop)
112 		return len;
113 
114 	fdt_nop_region_(prop, len + sizeof(*prop));
115 
116 	return 0;
117 }
118 
119 int fdt_node_end_offset_(void *fdt, int offset)
120 {
121 	int depth = 0;
122 
123 	while ((offset >= 0) && (depth >= 0))
124 		offset = fdt_next_node(fdt, offset, &depth);
125 
126 	return offset;
127 }
128 
129 int fdt_nop_node(void *fdt, int nodeoffset)
130 {
131 	int endoffset;
132 
133 	endoffset = fdt_node_end_offset_(fdt, nodeoffset);
134 	if (endoffset < 0)
135 		return endoffset;
136 
137 	fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0),
138 			endoffset - nodeoffset);
139 	return 0;
140 }
141