1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
6 #include "libfdt_env.h"
11 #include "libfdt_internal.h"
13 static int fdt_sw_probe_(void *fdt
)
15 if (!can_assume(VALID_INPUT
)) {
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
;
25 #define FDT_SW_PROBE(fdt) \
28 if ((err = fdt_sw_probe_(fdt)) != 0) \
32 /* 'memrsv' state: Initial state after fdt_create()
35 * fdt_add_reservemap_entry()
36 * fdt_finish_reservemap() [moves to 'struct' state]
38 static int fdt_sw_probe_memrsv_(void *fdt
)
40 int err
= fdt_sw_probe_(fdt
);
44 if (!can_assume(VALID_INPUT
) && fdt_off_dt_strings(fdt
) != 0)
45 return -FDT_ERR_BADSTATE
;
49 #define FDT_SW_PROBE_MEMRSV(fdt) \
52 if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
56 /* 'struct' state: Enter this state after fdt_finish_reservemap()
62 * fdt_finish() [moves to 'complete' state]
64 static int fdt_sw_probe_struct_(void *fdt
)
66 int err
= fdt_sw_probe_(fdt
);
70 if (!can_assume(VALID_INPUT
) &&
71 fdt_off_dt_strings(fdt
) != fdt_totalsize(fdt
))
72 return -FDT_ERR_BADSTATE
;
76 #define FDT_SW_PROBE_STRUCT(fdt) \
79 if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
83 static inline uint32_t sw_flags(void *fdt
)
85 /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
86 return fdt_last_comp_version(fdt
);
89 /* 'complete' state: Enter this state after fdt_finish()
91 * Allowed functions: none
94 static void *fdt_grab_space_(void *fdt
, size_t len
)
96 unsigned int offset
= fdt_size_dt_struct(fdt
);
97 unsigned int spaceleft
;
99 spaceleft
= fdt_totalsize(fdt
) - fdt_off_dt_struct(fdt
)
100 - fdt_size_dt_strings(fdt
);
102 if ((offset
+ len
< offset
) || (offset
+ len
> spaceleft
))
105 fdt_set_size_dt_struct(fdt
, offset
+ len
);
106 return fdt_offset_ptr_w_(fdt
, offset
);
109 int fdt_create_with_flags(void *buf
, int bufsize
, uint32_t flags
)
111 const int hdrsize
= FDT_ALIGN(sizeof(struct fdt_header
),
112 sizeof(struct fdt_reserve_entry
));
115 if (bufsize
< hdrsize
)
116 return -FDT_ERR_NOSPACE
;
118 if (flags
& ~FDT_CREATE_FLAGS_ALL
)
119 return -FDT_ERR_BADFLAGS
;
121 memset(buf
, 0, bufsize
);
124 * magic and last_comp_version keep intermediate state during the fdt
125 * creation process, which is replaced with the proper FDT format by
128 * flags should be accessed with sw_flags().
130 fdt_set_magic(fdt
, FDT_SW_MAGIC
);
131 fdt_set_version(fdt
, FDT_LAST_SUPPORTED_VERSION
);
132 fdt_set_last_comp_version(fdt
, flags
);
134 fdt_set_totalsize(fdt
, bufsize
);
136 fdt_set_off_mem_rsvmap(fdt
, hdrsize
);
137 fdt_set_off_dt_struct(fdt
, fdt_off_mem_rsvmap(fdt
));
138 fdt_set_off_dt_strings(fdt
, 0);
143 int fdt_create(void *buf
, int bufsize
)
145 return fdt_create_with_flags(buf
, bufsize
, 0);
148 int fdt_resize(void *fdt
, void *buf
, int bufsize
)
150 size_t headsize
, tailsize
;
151 char *oldtail
, *newtail
;
156 return -FDT_ERR_NOSPACE
;
158 headsize
= fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
);
159 tailsize
= fdt_size_dt_strings(fdt
);
161 if (!can_assume(VALID_DTB
) &&
162 headsize
+ tailsize
> fdt_totalsize(fdt
))
163 return -FDT_ERR_INTERNAL
;
165 if ((headsize
+ tailsize
) > (unsigned)bufsize
)
166 return -FDT_ERR_NOSPACE
;
168 oldtail
= (char *)fdt
+ fdt_totalsize(fdt
) - tailsize
;
169 newtail
= (char *)buf
+ bufsize
- tailsize
;
171 /* Two cases to avoid clobbering data if the old and new
172 * buffers partially overlap */
174 memmove(buf
, fdt
, headsize
);
175 memmove(newtail
, oldtail
, tailsize
);
177 memmove(newtail
, oldtail
, tailsize
);
178 memmove(buf
, fdt
, headsize
);
181 fdt_set_totalsize(buf
, bufsize
);
182 if (fdt_off_dt_strings(buf
))
183 fdt_set_off_dt_strings(buf
, bufsize
);
188 int fdt_add_reservemap_entry(void *fdt
, uint64_t addr
, uint64_t size
)
190 struct fdt_reserve_entry
*re
;
193 FDT_SW_PROBE_MEMRSV(fdt
);
195 offset
= fdt_off_dt_struct(fdt
);
196 if ((offset
+ sizeof(*re
)) > fdt_totalsize(fdt
))
197 return -FDT_ERR_NOSPACE
;
199 re
= (struct fdt_reserve_entry
*)((char *)fdt
+ offset
);
200 re
->address
= cpu_to_fdt64(addr
);
201 re
->size
= cpu_to_fdt64(size
);
203 fdt_set_off_dt_struct(fdt
, offset
+ sizeof(*re
));
208 int fdt_finish_reservemap(void *fdt
)
210 int err
= fdt_add_reservemap_entry(fdt
, 0, 0);
215 fdt_set_off_dt_strings(fdt
, fdt_totalsize(fdt
));
219 int fdt_begin_node(void *fdt
, const char *name
)
221 struct fdt_node_header
*nh
;
224 FDT_SW_PROBE_STRUCT(fdt
);
226 namelen
= strlen(name
) + 1;
227 nh
= fdt_grab_space_(fdt
, sizeof(*nh
) + FDT_TAGALIGN(namelen
));
229 return -FDT_ERR_NOSPACE
;
231 nh
->tag
= cpu_to_fdt32(FDT_BEGIN_NODE
);
232 memcpy(nh
->name
, name
, namelen
);
236 int fdt_end_node(void *fdt
)
240 FDT_SW_PROBE_STRUCT(fdt
);
242 en
= fdt_grab_space_(fdt
, FDT_TAGSIZE
);
244 return -FDT_ERR_NOSPACE
;
246 *en
= cpu_to_fdt32(FDT_END_NODE
);
250 static int fdt_add_string_(void *fdt
, const char *s
)
252 char *strtab
= (char *)fdt
+ fdt_totalsize(fdt
);
253 unsigned int strtabsize
= fdt_size_dt_strings(fdt
);
254 unsigned int len
= strlen(s
) + 1;
255 unsigned int struct_top
, offset
;
257 offset
= strtabsize
+ len
;
258 struct_top
= fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
);
259 if (fdt_totalsize(fdt
) - offset
< struct_top
)
260 return 0; /* no more room :( */
262 memcpy(strtab
- offset
, s
, len
);
263 fdt_set_size_dt_strings(fdt
, strtabsize
+ len
);
267 /* Must only be used to roll back in case of error */
268 static void fdt_del_last_string_(void *fdt
, const char *s
)
270 int strtabsize
= fdt_size_dt_strings(fdt
);
271 int len
= strlen(s
) + 1;
273 fdt_set_size_dt_strings(fdt
, strtabsize
- len
);
276 static int fdt_find_add_string_(void *fdt
, const char *s
, int *allocated
)
278 char *strtab
= (char *)fdt
+ fdt_totalsize(fdt
);
279 int strtabsize
= fdt_size_dt_strings(fdt
);
284 p
= fdt_find_string_(strtab
- strtabsize
, strtabsize
, s
);
290 return fdt_add_string_(fdt
, s
);
293 int fdt_property_placeholder(void *fdt
, const char *name
, int len
, void **valp
)
295 struct fdt_property
*prop
;
299 FDT_SW_PROBE_STRUCT(fdt
);
301 /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
302 if (sw_flags(fdt
) & FDT_CREATE_FLAG_NO_NAME_DEDUP
) {
304 nameoff
= fdt_add_string_(fdt
, name
);
306 nameoff
= fdt_find_add_string_(fdt
, name
, &allocated
);
309 return -FDT_ERR_NOSPACE
;
311 prop
= fdt_grab_space_(fdt
, sizeof(*prop
) + FDT_TAGALIGN(len
));
314 fdt_del_last_string_(fdt
, name
);
315 return -FDT_ERR_NOSPACE
;
318 prop
->tag
= cpu_to_fdt32(FDT_PROP
);
319 prop
->nameoff
= cpu_to_fdt32(nameoff
);
320 prop
->len
= cpu_to_fdt32(len
);
325 int fdt_property(void *fdt
, const char *name
, const void *val
, int len
)
330 ret
= fdt_property_placeholder(fdt
, name
, len
, &ptr
);
333 memcpy(ptr
, val
, len
);
337 int fdt_finish(void *fdt
)
339 char *p
= (char *)fdt
;
341 int oldstroffset
, newstroffset
;
343 int offset
, nextoffset
;
345 FDT_SW_PROBE_STRUCT(fdt
);
348 end
= fdt_grab_space_(fdt
, sizeof(*end
));
350 return -FDT_ERR_NOSPACE
;
351 *end
= cpu_to_fdt32(FDT_END
);
353 /* Relocate the string table */
354 oldstroffset
= fdt_totalsize(fdt
) - fdt_size_dt_strings(fdt
);
355 newstroffset
= fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
);
356 memmove(p
+ newstroffset
, p
+ oldstroffset
, fdt_size_dt_strings(fdt
));
357 fdt_set_off_dt_strings(fdt
, newstroffset
);
359 /* Walk the structure, correcting string offsets */
361 while ((tag
= fdt_next_tag(fdt
, offset
, &nextoffset
)) != FDT_END
) {
362 if (tag
== FDT_PROP
) {
363 struct fdt_property
*prop
=
364 fdt_offset_ptr_w_(fdt
, offset
);
367 nameoff
= fdt32_to_cpu(prop
->nameoff
);
368 nameoff
+= fdt_size_dt_strings(fdt
);
369 prop
->nameoff
= cpu_to_fdt32(nameoff
);
376 /* Finally, adjust the header */
377 fdt_set_totalsize(fdt
, newstroffset
+ fdt_size_dt_strings(fdt
));
379 /* And fix up fields that were keeping intermediate state. */
380 fdt_set_last_comp_version(fdt
, FDT_FIRST_SUPPORTED_VERSION
);
381 fdt_set_magic(fdt
, FDT_MAGIC
);