fdt_sw.c revision 1.1 1 1.1 macallan /*
2 1.1 macallan * libfdt - Flat Device Tree manipulation
3 1.1 macallan * Copyright (C) 2006 David Gibson, IBM Corporation.
4 1.1 macallan *
5 1.1 macallan * libfdt is dual licensed: you can use it either under the terms of
6 1.1 macallan * the GPL, or the BSD license, at your option.
7 1.1 macallan *
8 1.1 macallan * a) This library is free software; you can redistribute it and/or
9 1.1 macallan * modify it under the terms of the GNU General Public License as
10 1.1 macallan * published by the Free Software Foundation; either version 2 of the
11 1.1 macallan * License, or (at your option) any later version.
12 1.1 macallan *
13 1.1 macallan * This library is distributed in the hope that it will be useful,
14 1.1 macallan * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 1.1 macallan * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 1.1 macallan * GNU General Public License for more details.
17 1.1 macallan *
18 1.1 macallan * You should have received a copy of the GNU General Public
19 1.1 macallan * License along with this library; if not, write to the Free
20 1.1 macallan * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21 1.1 macallan * MA 02110-1301 USA
22 1.1 macallan *
23 1.1 macallan * Alternatively,
24 1.1 macallan *
25 1.1 macallan * b) Redistribution and use in source and binary forms, with or
26 1.1 macallan * without modification, are permitted provided that the following
27 1.1 macallan * conditions are met:
28 1.1 macallan *
29 1.1 macallan * 1. Redistributions of source code must retain the above
30 1.1 macallan * copyright notice, this list of conditions and the following
31 1.1 macallan * disclaimer.
32 1.1 macallan * 2. Redistributions in binary form must reproduce the above
33 1.1 macallan * copyright notice, this list of conditions and the following
34 1.1 macallan * disclaimer in the documentation and/or other materials
35 1.1 macallan * provided with the distribution.
36 1.1 macallan *
37 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38 1.1 macallan * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39 1.1 macallan * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40 1.1 macallan * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41 1.1 macallan * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42 1.1 macallan * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 1.1 macallan * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 1.1 macallan * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 1.1 macallan * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 1.1 macallan * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 1.1 macallan * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49 1.1 macallan * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50 1.1 macallan */
51 1.1 macallan #include "libfdt_env.h"
52 1.1 macallan
53 1.1 macallan #include <fdt.h>
54 1.1 macallan #include <libfdt.h>
55 1.1 macallan
56 1.1 macallan #include "libfdt_internal.h"
57 1.1 macallan
58 1.1 macallan static int _fdt_sw_check_header(void *fdt)
59 1.1 macallan {
60 1.1 macallan if (fdt_magic(fdt) != FDT_SW_MAGIC)
61 1.1 macallan return -FDT_ERR_BADMAGIC;
62 1.1 macallan /* FIXME: should check more details about the header state */
63 1.1 macallan return 0;
64 1.1 macallan }
65 1.1 macallan
66 1.1 macallan #define FDT_SW_CHECK_HEADER(fdt) \
67 1.1 macallan { \
68 1.1 macallan int err; \
69 1.1 macallan if ((err = _fdt_sw_check_header(fdt)) != 0) \
70 1.1 macallan return err; \
71 1.1 macallan }
72 1.1 macallan
73 1.1 macallan static void *_fdt_grab_space(void *fdt, size_t len)
74 1.1 macallan {
75 1.1 macallan int offset = fdt_size_dt_struct(fdt);
76 1.1 macallan int spaceleft;
77 1.1 macallan
78 1.1 macallan spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
79 1.1 macallan - fdt_size_dt_strings(fdt);
80 1.1 macallan
81 1.1 macallan if ((offset + len < offset) || (offset + len > spaceleft))
82 1.1 macallan return NULL;
83 1.1 macallan
84 1.1 macallan fdt_set_size_dt_struct(fdt, offset + len);
85 1.1 macallan return _fdt_offset_ptr_w(fdt, offset);
86 1.1 macallan }
87 1.1 macallan
88 1.1 macallan int fdt_create(void *buf, int bufsize)
89 1.1 macallan {
90 1.1 macallan void *fdt = buf;
91 1.1 macallan
92 1.1 macallan if (bufsize < sizeof(struct fdt_header))
93 1.1 macallan return -FDT_ERR_NOSPACE;
94 1.1 macallan
95 1.1 macallan memset(buf, 0, bufsize);
96 1.1 macallan
97 1.1 macallan fdt_set_magic(fdt, FDT_SW_MAGIC);
98 1.1 macallan fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
99 1.1 macallan fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
100 1.1 macallan fdt_set_totalsize(fdt, bufsize);
101 1.1 macallan
102 1.1 macallan fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
103 1.1 macallan sizeof(struct fdt_reserve_entry)));
104 1.1 macallan fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
105 1.1 macallan fdt_set_off_dt_strings(fdt, bufsize);
106 1.1 macallan
107 1.1 macallan return 0;
108 1.1 macallan }
109 1.1 macallan
110 1.1 macallan int fdt_resize(void *fdt, void *buf, int bufsize)
111 1.1 macallan {
112 1.1 macallan size_t headsize, tailsize;
113 1.1 macallan char *oldtail, *newtail;
114 1.1 macallan
115 1.1 macallan FDT_SW_CHECK_HEADER(fdt);
116 1.1 macallan
117 1.1 macallan headsize = fdt_off_dt_struct(fdt);
118 1.1 macallan tailsize = fdt_size_dt_strings(fdt);
119 1.1 macallan
120 1.1 macallan if ((headsize + tailsize) > bufsize)
121 1.1 macallan return -FDT_ERR_NOSPACE;
122 1.1 macallan
123 1.1 macallan oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
124 1.1 macallan newtail = (char *)buf + bufsize - tailsize;
125 1.1 macallan
126 1.1 macallan /* Two cases to avoid clobbering data if the old and new
127 1.1 macallan * buffers partially overlap */
128 1.1 macallan if (buf <= fdt) {
129 1.1 macallan memmove(buf, fdt, headsize);
130 1.1 macallan memmove(newtail, oldtail, tailsize);
131 1.1 macallan } else {
132 1.1 macallan memmove(newtail, oldtail, tailsize);
133 1.1 macallan memmove(buf, fdt, headsize);
134 1.1 macallan }
135 1.1 macallan
136 1.1 macallan fdt_set_off_dt_strings(buf, bufsize);
137 1.1 macallan fdt_set_totalsize(buf, bufsize);
138 1.1 macallan
139 1.1 macallan return 0;
140 1.1 macallan }
141 1.1 macallan
142 1.1 macallan int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
143 1.1 macallan {
144 1.1 macallan struct fdt_reserve_entry *re;
145 1.1 macallan int offset;
146 1.1 macallan
147 1.1 macallan FDT_SW_CHECK_HEADER(fdt);
148 1.1 macallan
149 1.1 macallan if (fdt_size_dt_struct(fdt))
150 1.1 macallan return -FDT_ERR_BADSTATE;
151 1.1 macallan
152 1.1 macallan offset = fdt_off_dt_struct(fdt);
153 1.1 macallan if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
154 1.1 macallan return -FDT_ERR_NOSPACE;
155 1.1 macallan
156 1.1 macallan re = (struct fdt_reserve_entry *)((char *)fdt + offset);
157 1.1 macallan re->address = cpu_to_fdt64(addr);
158 1.1 macallan re->size = cpu_to_fdt64(size);
159 1.1 macallan
160 1.1 macallan fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
161 1.1 macallan
162 1.1 macallan return 0;
163 1.1 macallan }
164 1.1 macallan
165 1.1 macallan int fdt_finish_reservemap(void *fdt)
166 1.1 macallan {
167 1.1 macallan return fdt_add_reservemap_entry(fdt, 0, 0);
168 1.1 macallan }
169 1.1 macallan
170 1.1 macallan int fdt_begin_node(void *fdt, const char *name)
171 1.1 macallan {
172 1.1 macallan struct fdt_node_header *nh;
173 1.1 macallan int namelen = strlen(name) + 1;
174 1.1 macallan
175 1.1 macallan FDT_SW_CHECK_HEADER(fdt);
176 1.1 macallan
177 1.1 macallan nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
178 1.1 macallan if (! nh)
179 1.1 macallan return -FDT_ERR_NOSPACE;
180 1.1 macallan
181 1.1 macallan nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
182 1.1 macallan memcpy(nh->name, name, namelen);
183 1.1 macallan return 0;
184 1.1 macallan }
185 1.1 macallan
186 1.1 macallan int fdt_end_node(void *fdt)
187 1.1 macallan {
188 1.1 macallan fdt32_t *en;
189 1.1 macallan
190 1.1 macallan FDT_SW_CHECK_HEADER(fdt);
191 1.1 macallan
192 1.1 macallan en = _fdt_grab_space(fdt, FDT_TAGSIZE);
193 1.1 macallan if (! en)
194 1.1 macallan return -FDT_ERR_NOSPACE;
195 1.1 macallan
196 1.1 macallan *en = cpu_to_fdt32(FDT_END_NODE);
197 1.1 macallan return 0;
198 1.1 macallan }
199 1.1 macallan
200 1.1 macallan static int _fdt_find_add_string(void *fdt, const char *s)
201 1.1 macallan {
202 1.1 macallan char *strtab = (char *)fdt + fdt_totalsize(fdt);
203 1.1 macallan const char *p;
204 1.1 macallan int strtabsize = fdt_size_dt_strings(fdt);
205 1.1 macallan int len = strlen(s) + 1;
206 1.1 macallan int struct_top, offset;
207 1.1 macallan
208 1.1 macallan p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
209 1.1 macallan if (p)
210 1.1 macallan return p - strtab;
211 1.1 macallan
212 1.1 macallan /* Add it */
213 1.1 macallan offset = -strtabsize - len;
214 1.1 macallan struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
215 1.1 macallan if (fdt_totalsize(fdt) + offset < struct_top)
216 1.1 macallan return 0; /* no more room :( */
217 1.1 macallan
218 1.1 macallan memcpy(strtab + offset, s, len);
219 1.1 macallan fdt_set_size_dt_strings(fdt, strtabsize + len);
220 1.1 macallan return offset;
221 1.1 macallan }
222 1.1 macallan
223 1.1 macallan int fdt_property(void *fdt, const char *name, const void *val, int len)
224 1.1 macallan {
225 1.1 macallan struct fdt_property *prop;
226 1.1 macallan int nameoff;
227 1.1 macallan
228 1.1 macallan FDT_SW_CHECK_HEADER(fdt);
229 1.1 macallan
230 1.1 macallan nameoff = _fdt_find_add_string(fdt, name);
231 1.1 macallan if (nameoff == 0)
232 1.1 macallan return -FDT_ERR_NOSPACE;
233 1.1 macallan
234 1.1 macallan prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
235 1.1 macallan if (! prop)
236 1.1 macallan return -FDT_ERR_NOSPACE;
237 1.1 macallan
238 1.1 macallan prop->tag = cpu_to_fdt32(FDT_PROP);
239 1.1 macallan prop->nameoff = cpu_to_fdt32(nameoff);
240 1.1 macallan prop->len = cpu_to_fdt32(len);
241 1.1 macallan memcpy(prop->data, val, len);
242 1.1 macallan return 0;
243 1.1 macallan }
244 1.1 macallan
245 1.1 macallan int fdt_finish(void *fdt)
246 1.1 macallan {
247 1.1 macallan char *p = (char *)fdt;
248 1.1 macallan fdt32_t *end;
249 1.1 macallan int oldstroffset, newstroffset;
250 1.1 macallan uint32_t tag;
251 1.1 macallan int offset, nextoffset;
252 1.1 macallan
253 1.1 macallan FDT_SW_CHECK_HEADER(fdt);
254 1.1 macallan
255 1.1 macallan /* Add terminator */
256 1.1 macallan end = _fdt_grab_space(fdt, sizeof(*end));
257 1.1 macallan if (! end)
258 1.1 macallan return -FDT_ERR_NOSPACE;
259 1.1 macallan *end = cpu_to_fdt32(FDT_END);
260 1.1 macallan
261 1.1 macallan /* Relocate the string table */
262 1.1 macallan oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
263 1.1 macallan newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
264 1.1 macallan memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
265 1.1 macallan fdt_set_off_dt_strings(fdt, newstroffset);
266 1.1 macallan
267 1.1 macallan /* Walk the structure, correcting string offsets */
268 1.1 macallan offset = 0;
269 1.1 macallan while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
270 1.1 macallan if (tag == FDT_PROP) {
271 1.1 macallan struct fdt_property *prop =
272 1.1 macallan _fdt_offset_ptr_w(fdt, offset);
273 1.1 macallan int nameoff;
274 1.1 macallan
275 1.1 macallan nameoff = fdt32_to_cpu(prop->nameoff);
276 1.1 macallan nameoff += fdt_size_dt_strings(fdt);
277 1.1 macallan prop->nameoff = cpu_to_fdt32(nameoff);
278 1.1 macallan }
279 1.1 macallan offset = nextoffset;
280 1.1 macallan }
281 1.1 macallan if (nextoffset < 0)
282 1.1 macallan return nextoffset;
283 1.1 macallan
284 1.1 macallan /* Finally, adjust the header */
285 1.1 macallan fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
286 1.1 macallan fdt_set_magic(fdt, FDT_MAGIC);
287 1.1 macallan return 0;
288 1.1 macallan }
289