blob: fa0df786efb1875aed6ed23e07dcddd1f812ce35 [file] [log] [blame]
David Gibson3da0f9a2006-11-27 16:21:28 +11001/*
2 * libfdt - Flat Device Tree manipulation
3 * Copyright (C) 2006 David Gibson, IBM Corporation.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19#include "libfdt_env.h"
20
21#include <fdt.h>
22#include <libfdt.h>
23
24#include "libfdt_internal.h"
25
26int fdt_setprop_inplace(struct fdt_header *fdt, int nodeoffset, const char *name,
27 const void *val, int len)
28{
29 void *propval;
30 int proplen;
31 int err;
32
33 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
34 if ((err = fdt_ptr_error(propval)))
35 return err;
36
37 if (proplen != len)
38 return FDT_ERR_SIZE_MISMATCH;
39
40 memcpy(propval, val, len);
41 return 0;
42}
43
44static void nop_region(void *start, int len)
45{
46 uint32_t *p;
47
48 for (p = start; (void *)p < (start + len); p++)
David Gibsone25487d2006-12-04 12:52:45 +110049 *p = cpu_to_fdt32(FDT_NOP);
David Gibson3da0f9a2006-11-27 16:21:28 +110050}
51
52int fdt_nop_property(struct fdt_header *fdt, int nodeoffset, const char *name)
53{
54 struct fdt_property *prop;
55 int len;
56 int err;
57
David Gibson94993f42006-12-11 16:15:34 +110058 prop = fdt_get_property(fdt, nodeoffset, name, &len);
David Gibson3da0f9a2006-11-27 16:21:28 +110059 if ((err = fdt_ptr_error(prop)))
60 return err;
61
62 nop_region(prop, len + sizeof(*prop));
63
64 return 0;
65}
66
David Gibson7ba551f2006-12-01 16:59:43 +110067int _fdt_node_end_offset(struct fdt_header *fdt, int nodeoffset)
David Gibson3da0f9a2006-11-27 16:21:28 +110068{
69 int level = 0;
David Gibson3da0f9a2006-11-27 16:21:28 +110070 uint32_t tag;
71 int offset, nextoffset;
72
73 tag = _fdt_next_tag(fdt, nodeoffset, &nextoffset);
74 if (tag != FDT_BEGIN_NODE)
75 return FDT_ERR_BADOFFSET;
David Gibson3da0f9a2006-11-27 16:21:28 +110076 do {
77 offset = nextoffset;
78 tag = _fdt_next_tag(fdt, offset, &nextoffset);
79
80 switch (tag) {
81 case FDT_END:
David Gibson7ba551f2006-12-01 16:59:43 +110082 return offset;
David Gibson3da0f9a2006-11-27 16:21:28 +110083
84 case FDT_BEGIN_NODE:
85 level++;
86 break;
87
88 case FDT_END_NODE:
89 level--;
90 break;
91
92 case FDT_PROP:
93 case FDT_NOP:
94 break;
95
96 default:
97 return FDT_ERR_BADSTRUCTURE;
98 }
99 } while (level >= 0);
100
David Gibson7ba551f2006-12-01 16:59:43 +1100101 return nextoffset;
102}
David Gibson3da0f9a2006-11-27 16:21:28 +1100103
David Gibson7ba551f2006-12-01 16:59:43 +1100104int fdt_nop_node(struct fdt_header *fdt, int nodeoffset)
105{
106 int endoffset;
107 int err;
108
109 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
110 if ((err = fdt_offset_error(endoffset)))
111 return err;
112
113 nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
114 return 0;
David Gibson3da0f9a2006-11-27 16:21:28 +1100115}