blob: 2d39aabe1fe9872c2a9e5e441979426244e063fd [file] [log] [blame]
Gerald Van Baren9e61ed82007-03-31 12:00:56 -04001/*
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 */
Gerald Van Baren960caf32007-04-20 22:46:53 -040019#include "config.h"
20#if CONFIG_OF_LIBFDT
21
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040022#include "libfdt_env.h"
23
24#include <fdt.h>
25#include <libfdt.h>
26
27#include "libfdt_internal.h"
28
29int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
30 const void *val, int len)
31{
32 void *propval;
33 int proplen;
34
35 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
36 if (! propval)
37 return proplen;
38
39 if (proplen != len)
40 return -FDT_ERR_NOSPACE;
41
42 memcpy(propval, val, len);
43 return 0;
44}
45
46static void nop_region(void *start, int len)
47{
48 uint32_t *p;
49
50 for (p = start; (void *)p < (start + len); p++)
51 *p = cpu_to_fdt32(FDT_NOP);
52}
53
54int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
55{
56 struct fdt_property *prop;
57 int len;
58
59 prop = fdt_get_property(fdt, nodeoffset, name, &len);
60 if (! prop)
61 return len;
62
63 nop_region(prop, len + sizeof(*prop));
64
65 return 0;
66}
67
68int _fdt_node_end_offset(void *fdt, int nodeoffset)
69{
70 int level = 0;
71 uint32_t tag;
72 int offset, nextoffset;
73
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040074 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040075 if (tag != FDT_BEGIN_NODE)
76 return -FDT_ERR_BADOFFSET;
77 do {
78 offset = nextoffset;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040079 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040080
81 switch (tag) {
82 case FDT_END:
83 return offset;
84
85 case FDT_BEGIN_NODE:
86 level++;
87 break;
88
89 case FDT_END_NODE:
90 level--;
91 break;
92
93 case FDT_PROP:
94 case FDT_NOP:
95 break;
96
97 default:
98 return -FDT_ERR_BADSTRUCTURE;
99 }
100 } while (level >= 0);
101
102 return nextoffset;
103}
104
105int 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 nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
114 return 0;
115}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400116
117/*
118 * Replace a reserve map entry in the nth slot.
119 */
120int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size)
121{
122 struct fdt_reserve_entry *re;
123 int used;
124 int total;
125 int err;
126
127 err = fdt_num_reservemap(fdt, &used, &total);
128 if (err != 0)
129 return err;
130
131 if (n >= total)
132 return -FDT_ERR_NOSPACE;
133 re = (struct fdt_reserve_entry *)
134 (fdt + fdt_off_mem_rsvmap(fdt) +
135 (n * sizeof(struct fdt_reserve_entry)));
136 re->address = cpu_to_fdt64(addr);
137 re->size = cpu_to_fdt64(size);
138
139 return 0;
140}
Gerald Van Baren960caf32007-04-20 22:46:53 -0400141
142#endif /* CONFIG_OF_LIBFDT */