blob: cf811830a73bdba81b71239940fcd958f8b6d89a [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 */
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(void *fdt, int nodeoffset, const char *name,
27 const void *val, int len)
28{
29 void *propval;
30 int proplen;
31
32 propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
33 if (! propval)
34 return proplen;
35
36 if (proplen != len)
37 return -FDT_ERR_NOSPACE;
38
39 memcpy(propval, val, len);
40 return 0;
41}
42
43static void nop_region(void *start, int len)
44{
45 uint32_t *p;
46
47 for (p = start; (void *)p < (start + len); p++)
48 *p = cpu_to_fdt32(FDT_NOP);
49}
50
51int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
52{
53 struct fdt_property *prop;
54 int len;
55
56 prop = fdt_get_property(fdt, nodeoffset, name, &len);
57 if (! prop)
58 return len;
59
60 nop_region(prop, len + sizeof(*prop));
61
62 return 0;
63}
64
65int _fdt_node_end_offset(void *fdt, int nodeoffset)
66{
67 int level = 0;
68 uint32_t tag;
69 int offset, nextoffset;
70
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040071 tag = fdt_next_tag(fdt, nodeoffset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040072 if (tag != FDT_BEGIN_NODE)
73 return -FDT_ERR_BADOFFSET;
74 do {
75 offset = nextoffset;
Gerald Van Barenca8d8a62007-03-31 12:13:43 -040076 tag = fdt_next_tag(fdt, offset, &nextoffset, NULL);
Gerald Van Baren9e61ed82007-03-31 12:00:56 -040077
78 switch (tag) {
79 case FDT_END:
80 return offset;
81
82 case FDT_BEGIN_NODE:
83 level++;
84 break;
85
86 case FDT_END_NODE:
87 level--;
88 break;
89
90 case FDT_PROP:
91 case FDT_NOP:
92 break;
93
94 default:
95 return -FDT_ERR_BADSTRUCTURE;
96 }
97 } while (level >= 0);
98
99 return nextoffset;
100}
101
102int fdt_nop_node(void *fdt, int nodeoffset)
103{
104 int endoffset;
105
106 endoffset = _fdt_node_end_offset(fdt, nodeoffset);
107 if (endoffset < 0)
108 return endoffset;
109
110 nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset);
111 return 0;
112}
Gerald Van Barenc077a132007-04-14 22:46:41 -0400113
114/*
115 * Replace a reserve map entry in the nth slot.
116 */
117int fdt_replace_reservemap_entry(void *fdt, int n, uint64_t addr, uint64_t size)
118{
119 struct fdt_reserve_entry *re;
120 int used;
121 int total;
122 int err;
123
124 err = fdt_num_reservemap(fdt, &used, &total);
125 if (err != 0)
126 return err;
127
128 if (n >= total)
129 return -FDT_ERR_NOSPACE;
130 re = (struct fdt_reserve_entry *)
131 (fdt + fdt_off_mem_rsvmap(fdt) +
132 (n * sizeof(struct fdt_reserve_entry)));
133 re->address = cpu_to_fdt64(addr);
134 re->size = cpu_to_fdt64(size);
135
136 return 0;
137}
138