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 #include "fdt.h"
53 #include "libfdt.h"
54 #include "libfdt_internal.h"
55 
_fdt_sw_check_header(void * fdt)56 static int _fdt_sw_check_header(void *fdt)
57 {
58     if (fdt_magic(fdt) != FDT_SW_MAGIC)
59         return -FDT_ERR_BADMAGIC;
60     /* FIXME: should check more details about the header state */
61     return 0;
62 }
63 
64 #define FDT_SW_CHECK_HEADER(fdt) \
65     { \
66         int err; \
67         if ((err = _fdt_sw_check_header(fdt)) != 0) \
68             return err; \
69     }
70 
_fdt_grab_space(void * fdt,size_t len)71 static void *_fdt_grab_space(void *fdt, size_t len)
72 {
73     int offset = fdt_size_dt_struct(fdt);
74     int spaceleft;
75 
76     spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
77         - fdt_size_dt_strings(fdt);
78 
79     if ((offset + len < offset) || (offset + len > spaceleft))
80         return NULL;
81 
82     fdt_set_size_dt_struct(fdt, offset + len);
83     return _fdt_offset_ptr_w(fdt, offset);
84 }
85 
fdt_create(void * buf,int bufsize)86 int fdt_create(void *buf, int bufsize)
87 {
88     void *fdt = buf;
89 
90     if (bufsize < sizeof(struct fdt_header))
91         return -FDT_ERR_NOSPACE;
92 
93     memset(buf, 0, bufsize);
94 
95     fdt_set_magic(fdt, FDT_SW_MAGIC);
96     fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
97     fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
98     fdt_set_totalsize(fdt,  bufsize);
99 
100     fdt_set_off_mem_rsvmap(fdt, FDT_ALIGN(sizeof(struct fdt_header),
101                           sizeof(struct fdt_reserve_entry)));
102     fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
103     fdt_set_off_dt_strings(fdt, bufsize);
104 
105     return 0;
106 }
107 
fdt_resize(void * fdt,void * buf,int bufsize)108 int fdt_resize(void *fdt, void *buf, int bufsize)
109 {
110     size_t headsize, tailsize;
111     char *oldtail, *newtail;
112 
113     FDT_SW_CHECK_HEADER(fdt);
114 
115     headsize = fdt_off_dt_struct(fdt);
116     tailsize = fdt_size_dt_strings(fdt);
117 
118     if ((headsize + tailsize) > bufsize)
119         return -FDT_ERR_NOSPACE;
120 
121     oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
122     newtail = (char *)buf + bufsize - tailsize;
123 
124     /* Two cases to avoid clobbering data if the old and new
125      * buffers partially overlap */
126     if (buf <= fdt) {
127         memmove(buf, fdt, headsize);
128         memmove(newtail, oldtail, tailsize);
129     } else {
130         memmove(newtail, oldtail, tailsize);
131         memmove(buf, fdt, headsize);
132     }
133 
134     fdt_set_off_dt_strings(buf, bufsize);
135     fdt_set_totalsize(buf, bufsize);
136 
137     return 0;
138 }
139 
fdt_add_reservemap_entry(void * fdt,uint64_t addr,uint64_t size)140 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
141 {
142     struct fdt_reserve_entry *re;
143     int offset;
144 
145     FDT_SW_CHECK_HEADER(fdt);
146 
147     if (fdt_size_dt_struct(fdt))
148         return -FDT_ERR_BADSTATE;
149 
150     offset = fdt_off_dt_struct(fdt);
151     if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
152         return -FDT_ERR_NOSPACE;
153 
154     re = (struct fdt_reserve_entry *)((char *)fdt + offset);
155     re->address = cpu_to_fdt64(addr);
156     re->size = cpu_to_fdt64(size);
157 
158     fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
159 
160     return 0;
161 }
162 
fdt_finish_reservemap(void * fdt)163 int fdt_finish_reservemap(void *fdt)
164 {
165     return fdt_add_reservemap_entry(fdt, 0, 0);
166 }
167 
fdt_begin_node(void * fdt,const char * name)168 int fdt_begin_node(void *fdt, const char *name)
169 {
170     struct fdt_node_header *nh;
171     int namelen = strlen(name) + 1;
172 
173     FDT_SW_CHECK_HEADER(fdt);
174 
175     nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
176     if (! nh)
177         return -FDT_ERR_NOSPACE;
178 
179     nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
180     memcpy(nh->name, name, namelen);
181     return 0;
182 }
183 
fdt_end_node(void * fdt)184 int fdt_end_node(void *fdt)
185 {
186     fdt32_t *en;
187 
188     FDT_SW_CHECK_HEADER(fdt);
189 
190     en = _fdt_grab_space(fdt, FDT_TAGSIZE);
191     if (! en)
192         return -FDT_ERR_NOSPACE;
193 
194     *en = cpu_to_fdt32(FDT_END_NODE);
195     return 0;
196 }
197 
_fdt_find_add_string(void * fdt,const char * s)198 static int _fdt_find_add_string(void *fdt, const char *s)
199 {
200     char *strtab = (char *)fdt + fdt_totalsize(fdt);
201     const char *p;
202     int strtabsize = fdt_size_dt_strings(fdt);
203     int len = strlen(s) + 1;
204     int struct_top, offset;
205 
206     p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
207     if (p)
208         return p - strtab;
209 
210     /* Add it */
211     offset = -strtabsize - len;
212     struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
213     if (fdt_totalsize(fdt) + offset < struct_top)
214         return 0; /* no more room :( */
215 
216     memcpy(strtab + offset, s, len);
217     fdt_set_size_dt_strings(fdt, strtabsize + len);
218     return offset;
219 }
220 
fdt_property(void * fdt,const char * name,const void * val,int len)221 int fdt_property(void *fdt, const char *name, const void *val, int len)
222 {
223     struct fdt_property *prop;
224     int nameoff;
225 
226     FDT_SW_CHECK_HEADER(fdt);
227 
228     nameoff = _fdt_find_add_string(fdt, name);
229     if (nameoff == 0)
230         return -FDT_ERR_NOSPACE;
231 
232     prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
233     if (! prop)
234         return -FDT_ERR_NOSPACE;
235 
236     prop->tag = cpu_to_fdt32(FDT_PROP);
237     prop->nameoff = cpu_to_fdt32(nameoff);
238     prop->len = cpu_to_fdt32(len);
239     memcpy(prop->data, val, len);
240     return 0;
241 }
242 
fdt_finish(void * fdt)243 int fdt_finish(void *fdt)
244 {
245     char *p = (char *)fdt;
246     fdt32_t *end;
247     int oldstroffset, newstroffset;
248     uint32_t tag;
249     int offset, nextoffset;
250 
251     FDT_SW_CHECK_HEADER(fdt);
252 
253     /* Add terminator */
254     end = _fdt_grab_space(fdt, sizeof(*end));
255     if (! end)
256         return -FDT_ERR_NOSPACE;
257     *end = cpu_to_fdt32(FDT_END);
258 
259     /* Relocate the string table */
260     oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
261     newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
262     memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
263     fdt_set_off_dt_strings(fdt, newstroffset);
264 
265     /* Walk the structure, correcting string offsets */
266     offset = 0;
267     while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
268         if (tag == FDT_PROP) {
269             struct fdt_property *prop =
270                 _fdt_offset_ptr_w(fdt, offset);
271             int nameoff;
272 
273             nameoff = fdt32_to_cpu(prop->nameoff);
274             nameoff += fdt_size_dt_strings(fdt);
275             prop->nameoff = cpu_to_fdt32(nameoff);
276         }
277         offset = nextoffset;
278     }
279     if (nextoffset < 0)
280         return nextoffset;
281 
282     /* Finally, adjust the header */
283     fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
284     fdt_set_magic(fdt, FDT_MAGIC);
285     return 0;
286 }
287