dt-bindings: Add missing newline at end of file
[linux-2.6-block.git] / scripts / dtc / libfdt / fdt_sw.c
1 /*
2  * libfdt - Flat Device Tree manipulation
3  * Copyright (C) 2006 David Gibson, IBM Corporation.
4  *
5  * libfdt is dual licensed: you can use it either under the terms of
6  * the GPL, or the BSD license, at your option.
7  *
8  *  a) This library is free software; you can redistribute it and/or
9  *     modify it under the terms of the GNU General Public License as
10  *     published by the Free Software Foundation; either version 2 of the
11  *     License, or (at your option) any later version.
12  *
13  *     This library is distributed in the hope that it will be useful,
14  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *     GNU General Public License for more details.
17  *
18  *     You should have received a copy of the GNU General Public
19  *     License along with this library; if not, write to the Free
20  *     Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
21  *     MA 02110-1301 USA
22  *
23  * Alternatively,
24  *
25  *  b) Redistribution and use in source and binary forms, with or
26  *     without modification, are permitted provided that the following
27  *     conditions are met:
28  *
29  *     1. Redistributions of source code must retain the above
30  *        copyright notice, this list of conditions and the following
31  *        disclaimer.
32  *     2. Redistributions in binary form must reproduce the above
33  *        copyright notice, this list of conditions and the following
34  *        disclaimer in the documentation and/or other materials
35  *        provided with the distribution.
36  *
37  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
38  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
39  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
40  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
42  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
47  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
49  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
50  */
51 #include "libfdt_env.h"
52
53 #include <fdt.h>
54 #include <libfdt.h>
55
56 #include "libfdt_internal.h"
57
58 static int fdt_sw_probe_(void *fdt)
59 {
60         if (fdt_magic(fdt) == FDT_MAGIC)
61                 return -FDT_ERR_BADSTATE;
62         else if (fdt_magic(fdt) != FDT_SW_MAGIC)
63                 return -FDT_ERR_BADMAGIC;
64         return 0;
65 }
66
67 #define FDT_SW_PROBE(fdt) \
68         { \
69                 int err; \
70                 if ((err = fdt_sw_probe_(fdt)) != 0) \
71                         return err; \
72         }
73
74 /* 'memrsv' state:      Initial state after fdt_create()
75  *
76  * Allowed functions:
77  *      fdt_add_reservmap_entry()
78  *      fdt_finish_reservemap()         [moves to 'struct' state]
79  */
80 static int fdt_sw_probe_memrsv_(void *fdt)
81 {
82         int err = fdt_sw_probe_(fdt);
83         if (err)
84                 return err;
85
86         if (fdt_off_dt_strings(fdt) != 0)
87                 return -FDT_ERR_BADSTATE;
88         return 0;
89 }
90
91 #define FDT_SW_PROBE_MEMRSV(fdt) \
92         { \
93                 int err; \
94                 if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
95                         return err; \
96         }
97
98 /* 'struct' state:      Enter this state after fdt_finish_reservemap()
99  *
100  * Allowed functions:
101  *      fdt_begin_node()
102  *      fdt_end_node()
103  *      fdt_property*()
104  *      fdt_finish()                    [moves to 'complete' state]
105  */
106 static int fdt_sw_probe_struct_(void *fdt)
107 {
108         int err = fdt_sw_probe_(fdt);
109         if (err)
110                 return err;
111
112         if (fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
113                 return -FDT_ERR_BADSTATE;
114         return 0;
115 }
116
117 #define FDT_SW_PROBE_STRUCT(fdt) \
118         { \
119                 int err; \
120                 if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
121                         return err; \
122         }
123
124 static inline uint32_t sw_flags(void *fdt)
125 {
126         /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
127         return fdt_last_comp_version(fdt);
128 }
129
130 /* 'complete' state:    Enter this state after fdt_finish()
131  *
132  * Allowed functions: none
133  */
134
135 static void *fdt_grab_space_(void *fdt, size_t len)
136 {
137         int offset = fdt_size_dt_struct(fdt);
138         int spaceleft;
139
140         spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
141                 - fdt_size_dt_strings(fdt);
142
143         if ((offset + len < offset) || (offset + len > spaceleft))
144                 return NULL;
145
146         fdt_set_size_dt_struct(fdt, offset + len);
147         return fdt_offset_ptr_w_(fdt, offset);
148 }
149
150 int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
151 {
152         const size_t hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
153                                          sizeof(struct fdt_reserve_entry));
154         void *fdt = buf;
155
156         if (bufsize < hdrsize)
157                 return -FDT_ERR_NOSPACE;
158
159         if (flags & ~FDT_CREATE_FLAGS_ALL)
160                 return -FDT_ERR_BADFLAGS;
161
162         memset(buf, 0, bufsize);
163
164         /*
165          * magic and last_comp_version keep intermediate state during the fdt
166          * creation process, which is replaced with the proper FDT format by
167          * fdt_finish().
168          *
169          * flags should be accessed with sw_flags().
170          */
171         fdt_set_magic(fdt, FDT_SW_MAGIC);
172         fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
173         fdt_set_last_comp_version(fdt, flags);
174
175         fdt_set_totalsize(fdt,  bufsize);
176
177         fdt_set_off_mem_rsvmap(fdt, hdrsize);
178         fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
179         fdt_set_off_dt_strings(fdt, 0);
180
181         return 0;
182 }
183
184 int fdt_create(void *buf, int bufsize)
185 {
186         return fdt_create_with_flags(buf, bufsize, 0);
187 }
188
189 int fdt_resize(void *fdt, void *buf, int bufsize)
190 {
191         size_t headsize, tailsize;
192         char *oldtail, *newtail;
193
194         FDT_SW_PROBE(fdt);
195
196         headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
197         tailsize = fdt_size_dt_strings(fdt);
198
199         if ((headsize + tailsize) > fdt_totalsize(fdt))
200                 return -FDT_ERR_INTERNAL;
201
202         if ((headsize + tailsize) > bufsize)
203                 return -FDT_ERR_NOSPACE;
204
205         oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
206         newtail = (char *)buf + bufsize - tailsize;
207
208         /* Two cases to avoid clobbering data if the old and new
209          * buffers partially overlap */
210         if (buf <= fdt) {
211                 memmove(buf, fdt, headsize);
212                 memmove(newtail, oldtail, tailsize);
213         } else {
214                 memmove(newtail, oldtail, tailsize);
215                 memmove(buf, fdt, headsize);
216         }
217
218         fdt_set_totalsize(buf, bufsize);
219         if (fdt_off_dt_strings(buf))
220                 fdt_set_off_dt_strings(buf, bufsize);
221
222         return 0;
223 }
224
225 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
226 {
227         struct fdt_reserve_entry *re;
228         int offset;
229
230         FDT_SW_PROBE_MEMRSV(fdt);
231
232         offset = fdt_off_dt_struct(fdt);
233         if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
234                 return -FDT_ERR_NOSPACE;
235
236         re = (struct fdt_reserve_entry *)((char *)fdt + offset);
237         re->address = cpu_to_fdt64(addr);
238         re->size = cpu_to_fdt64(size);
239
240         fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
241
242         return 0;
243 }
244
245 int fdt_finish_reservemap(void *fdt)
246 {
247         int err = fdt_add_reservemap_entry(fdt, 0, 0);
248
249         if (err)
250                 return err;
251
252         fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
253         return 0;
254 }
255
256 int fdt_begin_node(void *fdt, const char *name)
257 {
258         struct fdt_node_header *nh;
259         int namelen;
260
261         FDT_SW_PROBE_STRUCT(fdt);
262
263         namelen = strlen(name) + 1;
264         nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
265         if (! nh)
266                 return -FDT_ERR_NOSPACE;
267
268         nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
269         memcpy(nh->name, name, namelen);
270         return 0;
271 }
272
273 int fdt_end_node(void *fdt)
274 {
275         fdt32_t *en;
276
277         FDT_SW_PROBE_STRUCT(fdt);
278
279         en = fdt_grab_space_(fdt, FDT_TAGSIZE);
280         if (! en)
281                 return -FDT_ERR_NOSPACE;
282
283         *en = cpu_to_fdt32(FDT_END_NODE);
284         return 0;
285 }
286
287 static int fdt_add_string_(void *fdt, const char *s)
288 {
289         char *strtab = (char *)fdt + fdt_totalsize(fdt);
290         int strtabsize = fdt_size_dt_strings(fdt);
291         int len = strlen(s) + 1;
292         int struct_top, offset;
293
294         offset = -strtabsize - len;
295         struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
296         if (fdt_totalsize(fdt) + offset < struct_top)
297                 return 0; /* no more room :( */
298
299         memcpy(strtab + offset, s, len);
300         fdt_set_size_dt_strings(fdt, strtabsize + len);
301         return offset;
302 }
303
304 /* Must only be used to roll back in case of error */
305 static void fdt_del_last_string_(void *fdt, const char *s)
306 {
307         int strtabsize = fdt_size_dt_strings(fdt);
308         int len = strlen(s) + 1;
309
310         fdt_set_size_dt_strings(fdt, strtabsize - len);
311 }
312
313 static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
314 {
315         char *strtab = (char *)fdt + fdt_totalsize(fdt);
316         int strtabsize = fdt_size_dt_strings(fdt);
317         const char *p;
318
319         *allocated = 0;
320
321         p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
322         if (p)
323                 return p - strtab;
324
325         *allocated = 1;
326
327         return fdt_add_string_(fdt, s);
328 }
329
330 int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
331 {
332         struct fdt_property *prop;
333         int nameoff;
334         int allocated;
335
336         FDT_SW_PROBE_STRUCT(fdt);
337
338         /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
339         if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
340                 allocated = 1;
341                 nameoff = fdt_add_string_(fdt, name);
342         } else {
343                 nameoff = fdt_find_add_string_(fdt, name, &allocated);
344         }
345         if (nameoff == 0)
346                 return -FDT_ERR_NOSPACE;
347
348         prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
349         if (! prop) {
350                 if (allocated)
351                         fdt_del_last_string_(fdt, name);
352                 return -FDT_ERR_NOSPACE;
353         }
354
355         prop->tag = cpu_to_fdt32(FDT_PROP);
356         prop->nameoff = cpu_to_fdt32(nameoff);
357         prop->len = cpu_to_fdt32(len);
358         *valp = prop->data;
359         return 0;
360 }
361
362 int fdt_property(void *fdt, const char *name, const void *val, int len)
363 {
364         void *ptr;
365         int ret;
366
367         ret = fdt_property_placeholder(fdt, name, len, &ptr);
368         if (ret)
369                 return ret;
370         memcpy(ptr, val, len);
371         return 0;
372 }
373
374 int fdt_finish(void *fdt)
375 {
376         char *p = (char *)fdt;
377         fdt32_t *end;
378         int oldstroffset, newstroffset;
379         uint32_t tag;
380         int offset, nextoffset;
381
382         FDT_SW_PROBE_STRUCT(fdt);
383
384         /* Add terminator */
385         end = fdt_grab_space_(fdt, sizeof(*end));
386         if (! end)
387                 return -FDT_ERR_NOSPACE;
388         *end = cpu_to_fdt32(FDT_END);
389
390         /* Relocate the string table */
391         oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
392         newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
393         memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
394         fdt_set_off_dt_strings(fdt, newstroffset);
395
396         /* Walk the structure, correcting string offsets */
397         offset = 0;
398         while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
399                 if (tag == FDT_PROP) {
400                         struct fdt_property *prop =
401                                 fdt_offset_ptr_w_(fdt, offset);
402                         int nameoff;
403
404                         nameoff = fdt32_to_cpu(prop->nameoff);
405                         nameoff += fdt_size_dt_strings(fdt);
406                         prop->nameoff = cpu_to_fdt32(nameoff);
407                 }
408                 offset = nextoffset;
409         }
410         if (nextoffset < 0)
411                 return nextoffset;
412
413         /* Finally, adjust the header */
414         fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
415
416         /* And fix up fields that were keeping intermediate state. */
417         fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
418         fdt_set_magic(fdt, FDT_MAGIC);
419
420         return 0;
421 }