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, see <http://www.gnu.org/licenses/>.
20  *
21  * Alternatively,
22  *
23  *  b) Redistribution and use in source and binary forms, with or
24  *     without modification, are permitted provided that the following
25  *     conditions are met:
26  *
27  *     1. Redistributions of source code must retain the above
28  *        copyright notice, this list of conditions and the following
29  *        disclaimer.
30  *     2. Redistributions in binary form must reproduce the above
31  *        copyright notice, this list of conditions and the following
32  *        disclaimer in the documentation and/or other materials
33  *        provided with the distribution.
34  *
35  *     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
36  *     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
37  *     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38  *     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  *     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
40  *     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  *     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42  *     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43  *     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  *     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  *     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
46  *     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
47  *     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  */
49 #include "libfdt_env.h"
50 
51 #include <fdt.h>
52 #include <libfdt.h>
53 
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_add_reservemap_entry(void * fdt,uint64_t addr,uint64_t size)108 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
109 {
110 	struct fdt_reserve_entry *re;
111 	int offset;
112 
113 	FDT_SW_CHECK_HEADER(fdt);
114 
115 	if (fdt_size_dt_struct(fdt))
116 		return -FDT_ERR_BADSTATE;
117 
118 	offset = fdt_off_dt_struct(fdt);
119 	if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
120 		return -FDT_ERR_NOSPACE;
121 
122 	re = (struct fdt_reserve_entry *)((char *)fdt + offset);
123 	re->address = cpu_to_fdt64(addr);
124 	re->size = cpu_to_fdt64(size);
125 
126 	fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
127 
128 	return 0;
129 }
130 
fdt_finish_reservemap(void * fdt)131 int fdt_finish_reservemap(void *fdt)
132 {
133 	return fdt_add_reservemap_entry(fdt, 0, 0);
134 }
135 
fdt_begin_node(void * fdt,const char * name)136 int fdt_begin_node(void *fdt, const char *name)
137 {
138 	struct fdt_node_header *nh;
139 	int namelen = strlen(name) + 1;
140 
141 	FDT_SW_CHECK_HEADER(fdt);
142 
143 	nh = _fdt_grab_space(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
144 	if (! nh)
145 		return -FDT_ERR_NOSPACE;
146 
147 	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
148 	memcpy(nh->name, name, namelen);
149 	return 0;
150 }
151 
fdt_end_node(void * fdt)152 int fdt_end_node(void *fdt)
153 {
154 	fdt32_t *en;
155 
156 	FDT_SW_CHECK_HEADER(fdt);
157 
158 	en = _fdt_grab_space(fdt, FDT_TAGSIZE);
159 	if (! en)
160 		return -FDT_ERR_NOSPACE;
161 
162 	*en = cpu_to_fdt32(FDT_END_NODE);
163 	return 0;
164 }
165 
_fdt_find_add_string(void * fdt,const char * s)166 static int _fdt_find_add_string(void *fdt, const char *s)
167 {
168 	char *strtab = (char *)fdt + fdt_totalsize(fdt);
169 	const char *p;
170 	int strtabsize = fdt_size_dt_strings(fdt);
171 	int len = strlen(s) + 1;
172 	int struct_top, offset;
173 
174 	p = _fdt_find_string(strtab - strtabsize, strtabsize, s);
175 	if (p)
176 		return p - strtab;
177 
178 	/* Add it */
179 	offset = -strtabsize - len;
180 	struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
181 	if (fdt_totalsize(fdt) + offset < struct_top)
182 		return 0; /* no more room :( */
183 
184 	memcpy(strtab + offset, s, len);
185 	fdt_set_size_dt_strings(fdt, strtabsize + len);
186 	return offset;
187 }
188 
fdt_property(void * fdt,const char * name,const void * val,int len)189 int fdt_property(void *fdt, const char *name, const void *val, int len)
190 {
191 	struct fdt_property *prop;
192 	int nameoff;
193 
194 	FDT_SW_CHECK_HEADER(fdt);
195 
196 	nameoff = _fdt_find_add_string(fdt, name);
197 	if (nameoff == 0)
198 		return -FDT_ERR_NOSPACE;
199 
200 	prop = _fdt_grab_space(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
201 	if (! prop)
202 		return -FDT_ERR_NOSPACE;
203 
204 	prop->tag = cpu_to_fdt32(FDT_PROP);
205 	prop->nameoff = cpu_to_fdt32(nameoff);
206 	prop->len = cpu_to_fdt32(len);
207 	memcpy(prop->data, val, len);
208 	return 0;
209 }
210 
fdt_finish(void * fdt)211 int fdt_finish(void *fdt)
212 {
213 	char *p = (char *)fdt;
214 	fdt32_t *end;
215 	int oldstroffset, newstroffset;
216 	uint32_t tag;
217 	int offset, nextoffset;
218 
219 	FDT_SW_CHECK_HEADER(fdt);
220 
221 	/* Add terminator */
222 	end = _fdt_grab_space(fdt, sizeof(*end));
223 	if (! end)
224 		return -FDT_ERR_NOSPACE;
225 	*end = cpu_to_fdt32(FDT_END);
226 
227 	/* Relocate the string table */
228 	oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
229 	newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
230 	memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
231 	fdt_set_off_dt_strings(fdt, newstroffset);
232 
233 	/* Walk the structure, correcting string offsets */
234 	offset = 0;
235 	while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
236 		if (tag == FDT_PROP) {
237 			struct fdt_property *prop =
238 				_fdt_offset_ptr_w(fdt, offset);
239 			int nameoff;
240 
241 			nameoff = fdt32_to_cpu(prop->nameoff);
242 			nameoff += fdt_size_dt_strings(fdt);
243 			prop->nameoff = cpu_to_fdt32(nameoff);
244 		}
245 		offset = nextoffset;
246 	}
247 	if (nextoffset < 0)
248 		return nextoffset;
249 
250 	/* Finally, adjust the header */
251 	fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
252 	fdt_set_magic(fdt, FDT_MAGIC);
253 	return 0;
254 }
255