blob: a8c924675a6ad11f282c706bd346cd09ff7fd9d6 [file] [log] [blame]
Simon Glassa5e1a582019-10-27 09:47:42 -06001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
Tom Rinibd391732017-09-23 12:52:44 -04002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
Tom Rinibd391732017-09-23 12:52:44 -04005 */
6#include "libfdt_env.h"
7
8#include <fdt.h>
9#include <libfdt.h>
10
11#include "libfdt_internal.h"
12
Simon Glassa5e1a582019-10-27 09:47:42 -060013static int fdt_sw_probe_(void *fdt)
14{
15 if (fdt_chk_basic()) {
16 if (fdt_magic(fdt) == FDT_MAGIC)
17 return -FDT_ERR_BADSTATE;
18 else if (fdt_magic(fdt) != FDT_SW_MAGIC)
19 return -FDT_ERR_BADMAGIC;
20 }
21
22 return 0;
23}
24
25#define FDT_SW_PROBE(fdt) \
26 { \
27 int err; \
28 if (fdt_chk_basic() && (err = fdt_sw_probe_(fdt)) != 0) \
29 return err; \
30 }
31
32/* 'memrsv' state: Initial state after fdt_create()
33 *
34 * Allowed functions:
35 * fdt_add_reservmap_entry()
36 * fdt_finish_reservemap() [moves to 'struct' state]
37 */
38static int fdt_sw_probe_memrsv_(void *fdt)
39{
40 int err = fdt_sw_probe_(fdt);
41 if (err)
42 return err;
43
44 if (fdt_chk_extra() && fdt_off_dt_strings(fdt) != 0)
45 return -FDT_ERR_BADSTATE;
46 return 0;
47}
48
49#define FDT_SW_PROBE_MEMRSV(fdt) \
50 { \
51 int err; \
52 if (fdt_chk_extra() && (err = fdt_sw_probe_memrsv_(fdt)) != 0) \
53 return err; \
54 }
55
56/* 'struct' state: Enter this state after fdt_finish_reservemap()
57 *
58 * Allowed functions:
59 * fdt_begin_node()
60 * fdt_end_node()
61 * fdt_property*()
62 * fdt_finish() [moves to 'complete' state]
63 */
64static int fdt_sw_probe_struct_(void *fdt)
Tom Rinibd391732017-09-23 12:52:44 -040065{
Simon Glassa5e1a582019-10-27 09:47:42 -060066 int err;
67
68 if (!fdt_chk_extra())
69 return 0;
70 err = fdt_sw_probe_(fdt);
71 if (err)
72 return err;
73
74 if (fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
75 return -FDT_ERR_BADSTATE;
Tom Rinibd391732017-09-23 12:52:44 -040076 return 0;
77}
78
Simon Glassa5e1a582019-10-27 09:47:42 -060079#define FDT_SW_PROBE_STRUCT(fdt) \
Tom Rinibd391732017-09-23 12:52:44 -040080 { \
81 int err; \
Simon Glassa5e1a582019-10-27 09:47:42 -060082 if (fdt_chk_extra() && (err = fdt_sw_probe_struct_(fdt)) != 0) \
Tom Rinibd391732017-09-23 12:52:44 -040083 return err; \
84 }
85
Simon Glassa5e1a582019-10-27 09:47:42 -060086static inline uint32_t sw_flags(void *fdt)
87{
88 /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
89 return fdt_last_comp_version(fdt);
90}
91
92/* 'complete' state: Enter this state after fdt_finish()
93 *
94 * Allowed functions: none
95 */
96
Rob Herringcd5f70d2018-05-19 14:13:53 +020097static void *fdt_grab_space_(void *fdt, size_t len)
Tom Rinibd391732017-09-23 12:52:44 -040098{
99 int offset = fdt_size_dt_struct(fdt);
100 int spaceleft;
101
102 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
103 - fdt_size_dt_strings(fdt);
104
105 if ((offset + len < offset) || (offset + len > spaceleft))
106 return NULL;
107
108 fdt_set_size_dt_struct(fdt, offset + len);
Rob Herringcd5f70d2018-05-19 14:13:53 +0200109 return fdt_offset_ptr_w_(fdt, offset);
Tom Rinibd391732017-09-23 12:52:44 -0400110}
111
Simon Glassa5e1a582019-10-27 09:47:42 -0600112int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
Tom Rinibd391732017-09-23 12:52:44 -0400113{
Simon Glassa5e1a582019-10-27 09:47:42 -0600114 const size_t hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
115 sizeof(struct fdt_reserve_entry));
Tom Rinibd391732017-09-23 12:52:44 -0400116 void *fdt = buf;
117
Simon Glassa5e1a582019-10-27 09:47:42 -0600118 if (bufsize < hdrsize)
Tom Rinibd391732017-09-23 12:52:44 -0400119 return -FDT_ERR_NOSPACE;
120
Simon Glassa5e1a582019-10-27 09:47:42 -0600121 if (flags & ~FDT_CREATE_FLAGS_ALL)
122 return -FDT_ERR_BADFLAGS;
123
Tom Rinibd391732017-09-23 12:52:44 -0400124 memset(buf, 0, bufsize);
125
Simon Glassa5e1a582019-10-27 09:47:42 -0600126 /*
127 * magic and last_comp_version keep intermediate state during the fdt
128 * creation process, which is replaced with the proper FDT format by
129 * fdt_finish().
130 *
131 * flags should be accessed with sw_flags().
132 */
Tom Rinibd391732017-09-23 12:52:44 -0400133 fdt_set_magic(fdt, FDT_SW_MAGIC);
134 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
Simon Glassa5e1a582019-10-27 09:47:42 -0600135 fdt_set_last_comp_version(fdt, flags);
136
Tom Rinibd391732017-09-23 12:52:44 -0400137 fdt_set_totalsize(fdt, bufsize);
138
Simon Glassa5e1a582019-10-27 09:47:42 -0600139 fdt_set_off_mem_rsvmap(fdt, hdrsize);
Tom Rinibd391732017-09-23 12:52:44 -0400140 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
Simon Glassa5e1a582019-10-27 09:47:42 -0600141 fdt_set_off_dt_strings(fdt, 0);
Tom Rinibd391732017-09-23 12:52:44 -0400142
143 return 0;
144}
145
Simon Glassa5e1a582019-10-27 09:47:42 -0600146int fdt_create(void *buf, int bufsize)
147{
148 return fdt_create_with_flags(buf, bufsize, 0);
149}
150
Tom Rinibd391732017-09-23 12:52:44 -0400151int fdt_resize(void *fdt, void *buf, int bufsize)
152{
153 size_t headsize, tailsize;
154 char *oldtail, *newtail;
155
Simon Glassa5e1a582019-10-27 09:47:42 -0600156 FDT_SW_PROBE(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400157
Simon Glass14ec68c2019-07-20 12:23:59 -0600158 headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400159 tailsize = fdt_size_dt_strings(fdt);
160
Simon Glassa5e1a582019-10-27 09:47:42 -0600161 if (fdt_chk_extra() && (headsize + tailsize) > fdt_totalsize(fdt))
162 return -FDT_ERR_INTERNAL;
163
Tom Rinibd391732017-09-23 12:52:44 -0400164 if ((headsize + tailsize) > bufsize)
165 return -FDT_ERR_NOSPACE;
166
167 oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
168 newtail = (char *)buf + bufsize - tailsize;
169
170 /* Two cases to avoid clobbering data if the old and new
171 * buffers partially overlap */
172 if (buf <= fdt) {
173 memmove(buf, fdt, headsize);
174 memmove(newtail, oldtail, tailsize);
175 } else {
176 memmove(newtail, oldtail, tailsize);
177 memmove(buf, fdt, headsize);
178 }
179
Tom Rinibd391732017-09-23 12:52:44 -0400180 fdt_set_totalsize(buf, bufsize);
Simon Glassa5e1a582019-10-27 09:47:42 -0600181 if (fdt_off_dt_strings(buf))
182 fdt_set_off_dt_strings(buf, bufsize);
Tom Rinibd391732017-09-23 12:52:44 -0400183
184 return 0;
185}
186
187int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
188{
189 struct fdt_reserve_entry *re;
190 int offset;
191
Simon Glassa5e1a582019-10-27 09:47:42 -0600192 FDT_SW_PROBE_MEMRSV(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400193
194 offset = fdt_off_dt_struct(fdt);
195 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
196 return -FDT_ERR_NOSPACE;
197
198 re = (struct fdt_reserve_entry *)((char *)fdt + offset);
199 re->address = cpu_to_fdt64(addr);
200 re->size = cpu_to_fdt64(size);
201
202 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
203
204 return 0;
205}
206
207int fdt_finish_reservemap(void *fdt)
208{
Simon Glassa5e1a582019-10-27 09:47:42 -0600209 int err = fdt_add_reservemap_entry(fdt, 0, 0);
210
211 if (err)
212 return err;
213
214 fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
215 return 0;
Tom Rinibd391732017-09-23 12:52:44 -0400216}
217
218int fdt_begin_node(void *fdt, const char *name)
219{
220 struct fdt_node_header *nh;
Simon Glassa5e1a582019-10-27 09:47:42 -0600221 int namelen;
Tom Rinibd391732017-09-23 12:52:44 -0400222
Simon Glassa5e1a582019-10-27 09:47:42 -0600223 FDT_SW_PROBE_STRUCT(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400224
Simon Glassa5e1a582019-10-27 09:47:42 -0600225 namelen = strlen(name) + 1;
Rob Herringcd5f70d2018-05-19 14:13:53 +0200226 nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
Tom Rinibd391732017-09-23 12:52:44 -0400227 if (! nh)
228 return -FDT_ERR_NOSPACE;
229
230 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
231 memcpy(nh->name, name, namelen);
232 return 0;
233}
234
235int fdt_end_node(void *fdt)
236{
237 fdt32_t *en;
238
Simon Glassa5e1a582019-10-27 09:47:42 -0600239 FDT_SW_PROBE_STRUCT(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400240
Rob Herringcd5f70d2018-05-19 14:13:53 +0200241 en = fdt_grab_space_(fdt, FDT_TAGSIZE);
Tom Rinibd391732017-09-23 12:52:44 -0400242 if (! en)
243 return -FDT_ERR_NOSPACE;
244
245 *en = cpu_to_fdt32(FDT_END_NODE);
246 return 0;
247}
248
Simon Glassa5e1a582019-10-27 09:47:42 -0600249static int fdt_add_string_(void *fdt, const char *s)
Tom Rinibd391732017-09-23 12:52:44 -0400250{
251 char *strtab = (char *)fdt + fdt_totalsize(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400252 int strtabsize = fdt_size_dt_strings(fdt);
253 int len = strlen(s) + 1;
254 int struct_top, offset;
255
Tom Rinibd391732017-09-23 12:52:44 -0400256 offset = -strtabsize - len;
257 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
258 if (fdt_totalsize(fdt) + offset < struct_top)
259 return 0; /* no more room :( */
260
261 memcpy(strtab + offset, s, len);
262 fdt_set_size_dt_strings(fdt, strtabsize + len);
263 return offset;
264}
265
Simon Glassa5e1a582019-10-27 09:47:42 -0600266/* Must only be used to roll back in case of error */
267static void fdt_del_last_string_(void *fdt, const char *s)
268{
269 int strtabsize = fdt_size_dt_strings(fdt);
270 int len = strlen(s) + 1;
271
272 fdt_set_size_dt_strings(fdt, strtabsize - len);
273}
274
275static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
276{
277 char *strtab = (char *)fdt + fdt_totalsize(fdt);
278 int strtabsize = fdt_size_dt_strings(fdt);
279 const char *p;
280
281 *allocated = 0;
282
283 p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
284 if (p)
285 return p - strtab;
286
287 *allocated = 1;
288
289 return fdt_add_string_(fdt, s);
290}
291
Tom Rini0eb096d2017-09-23 17:31:59 -0400292int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
Tom Rinibd391732017-09-23 12:52:44 -0400293{
294 struct fdt_property *prop;
295 int nameoff;
Simon Glassa5e1a582019-10-27 09:47:42 -0600296 int allocated;
Tom Rinibd391732017-09-23 12:52:44 -0400297
Simon Glassa5e1a582019-10-27 09:47:42 -0600298 FDT_SW_PROBE_STRUCT(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400299
Simon Glassa5e1a582019-10-27 09:47:42 -0600300 /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
301 if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
302 allocated = 1;
303 nameoff = fdt_add_string_(fdt, name);
304 } else {
305 nameoff = fdt_find_add_string_(fdt, name, &allocated);
306 }
Tom Rinibd391732017-09-23 12:52:44 -0400307 if (nameoff == 0)
308 return -FDT_ERR_NOSPACE;
309
Rob Herringcd5f70d2018-05-19 14:13:53 +0200310 prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
Simon Glassa5e1a582019-10-27 09:47:42 -0600311 if (! prop) {
312 if (allocated)
313 fdt_del_last_string_(fdt, name);
Tom Rinibd391732017-09-23 12:52:44 -0400314 return -FDT_ERR_NOSPACE;
Simon Glassa5e1a582019-10-27 09:47:42 -0600315 }
Tom Rinibd391732017-09-23 12:52:44 -0400316
317 prop->tag = cpu_to_fdt32(FDT_PROP);
318 prop->nameoff = cpu_to_fdt32(nameoff);
319 prop->len = cpu_to_fdt32(len);
Tom Rini0eb096d2017-09-23 17:31:59 -0400320 *valp = prop->data;
321 return 0;
322}
323
324int fdt_property(void *fdt, const char *name, const void *val, int len)
325{
326 void *ptr;
327 int ret;
328
329 ret = fdt_property_placeholder(fdt, name, len, &ptr);
330 if (ret)
331 return ret;
332 memcpy(ptr, val, len);
Tom Rinibd391732017-09-23 12:52:44 -0400333 return 0;
334}
335
336int fdt_finish(void *fdt)
337{
338 char *p = (char *)fdt;
339 fdt32_t *end;
340 int oldstroffset, newstroffset;
341 uint32_t tag;
342 int offset, nextoffset;
343
Simon Glassa5e1a582019-10-27 09:47:42 -0600344 FDT_SW_PROBE_STRUCT(fdt);
Tom Rinibd391732017-09-23 12:52:44 -0400345
346 /* Add terminator */
Rob Herringcd5f70d2018-05-19 14:13:53 +0200347 end = fdt_grab_space_(fdt, sizeof(*end));
Tom Rinibd391732017-09-23 12:52:44 -0400348 if (! end)
349 return -FDT_ERR_NOSPACE;
350 *end = cpu_to_fdt32(FDT_END);
351
352 /* Relocate the string table */
353 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
354 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
355 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
356 fdt_set_off_dt_strings(fdt, newstroffset);
357
358 /* Walk the structure, correcting string offsets */
359 offset = 0;
360 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
361 if (tag == FDT_PROP) {
362 struct fdt_property *prop =
Rob Herringcd5f70d2018-05-19 14:13:53 +0200363 fdt_offset_ptr_w_(fdt, offset);
Tom Rinibd391732017-09-23 12:52:44 -0400364 int nameoff;
365
366 nameoff = fdt32_to_cpu(prop->nameoff);
367 nameoff += fdt_size_dt_strings(fdt);
368 prop->nameoff = cpu_to_fdt32(nameoff);
369 }
370 offset = nextoffset;
371 }
372 if (nextoffset < 0)
373 return nextoffset;
374
375 /* Finally, adjust the header */
376 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
Simon Glassa5e1a582019-10-27 09:47:42 -0600377
378 /* And fix up fields that were keeping intermediate state. */
379 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
Tom Rinibd391732017-09-23 12:52:44 -0400380 fdt_set_magic(fdt, FDT_MAGIC);
Simon Glassa5e1a582019-10-27 09:47:42 -0600381
Tom Rinibd391732017-09-23 12:52:44 -0400382 return 0;
383}