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