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