1204431Sraj/*
2204431Sraj * libfdt - Flat Device Tree manipulation
3204431Sraj * Copyright (C) 2006 David Gibson, IBM Corporation.
4204431Sraj *
5204431Sraj * libfdt is dual licensed: you can use it either under the terms of
6204431Sraj * the GPL, or the BSD license, at your option.
7204431Sraj *
8204431Sraj *  a) This library is free software; you can redistribute it and/or
9204431Sraj *     modify it under the terms of the GNU General Public License as
10204431Sraj *     published by the Free Software Foundation; either version 2 of the
11204431Sraj *     License, or (at your option) any later version.
12204431Sraj *
13204431Sraj *     This library is distributed in the hope that it will be useful,
14204431Sraj *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15204431Sraj *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16204431Sraj *     GNU General Public License for more details.
17204431Sraj *
18204431Sraj *     You should have received a copy of the GNU General Public
19204431Sraj *     License along with this library; if not, write to the Free
20204431Sraj *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21204431Sraj *     MA 02110-1301 USA
22204431Sraj *
23204431Sraj * Alternatively,
24204431Sraj *
25204431Sraj *  b) Redistribution and use in source and binary forms, with or
26204431Sraj *     without modification, are permitted provided that the following
27204431Sraj *     conditions are met:
28204431Sraj *
29204431Sraj *     1. Redistributions of source code must retain the above
30204431Sraj *        copyright notice, this list of conditions and the following
31204431Sraj *        disclaimer.
32204431Sraj *     2. Redistributions in binary form must reproduce the above
33204431Sraj *        copyright notice, this list of conditions and the following
34204431Sraj *        disclaimer in the documentation and/or other materials
35204431Sraj *        provided with the distribution.
36204431Sraj *
37204431Sraj *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38204431Sraj *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39204431Sraj *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40204431Sraj *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41204431Sraj *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42204431Sraj *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43204431Sraj *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44204431Sraj *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45204431Sraj *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46204431Sraj *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47204431Sraj *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48204431Sraj *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49204431Sraj *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50204431Sraj */
51204431Sraj#include "libfdt_env.h"
52204431Sraj
53204431Sraj#include <fdt.h>
54204431Sraj#include <libfdt.h>
55204431Sraj
56204431Sraj#include "libfdt_internal.h"
57204431Sraj
58204431Srajint fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
59204431Sraj			const void *val, int len)
60204431Sraj{
61204431Sraj	void *propval;
62204431Sraj	int proplen;
63204431Sraj
64204431Sraj	propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen);
65204431Sraj	if (! propval)
66204431Sraj		return proplen;
67204431Sraj
68204431Sraj	if (proplen != len)
69204431Sraj		return -FDT_ERR_NOSPACE;
70204431Sraj
71204431Sraj	memcpy(propval, val, len);
72204431Sraj	return 0;
73204431Sraj}
74204431Sraj
75204431Srajstatic void _fdt_nop_region(void *start, int len)
76204431Sraj{
77266130Sian	fdt32_t *p;
78204431Sraj
79204431Sraj	for (p = start; (char *)p < ((char *)start + len); p++)
80204431Sraj		*p = cpu_to_fdt32(FDT_NOP);
81204431Sraj}
82204431Sraj
83204431Srajint fdt_nop_property(void *fdt, int nodeoffset, const char *name)
84204431Sraj{
85204431Sraj	struct fdt_property *prop;
86204431Sraj	int len;
87204431Sraj
88204431Sraj	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
89204431Sraj	if (! prop)
90204431Sraj		return len;
91204431Sraj
92204431Sraj	_fdt_nop_region(prop, len + sizeof(*prop));
93204431Sraj
94204431Sraj	return 0;
95204431Sraj}
96204431Sraj
97204433Srajint _fdt_node_end_offset(void *fdt, int offset)
98204431Sraj{
99204433Sraj	int depth = 0;
100204431Sraj
101204433Sraj	while ((offset >= 0) && (depth >= 0))
102204433Sraj		offset = fdt_next_node(fdt, offset, &depth);
103204431Sraj
104204433Sraj	return offset;
105204431Sraj}
106204431Sraj
107204431Srajint fdt_nop_node(void *fdt, int nodeoffset)
108204431Sraj{
109204431Sraj	int endoffset;
110204431Sraj
111204431Sraj	endoffset = _fdt_node_end_offset(fdt, nodeoffset);
112204431Sraj	if (endoffset < 0)
113204431Sraj		return endoffset;
114204431Sraj
115204431Sraj	_fdt_nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0),
116204431Sraj			endoffset - nodeoffset);
117204431Sraj	return 0;
118204431Sraj}
119