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 (fdt_magic(fdt
) == FDT_MAGIC
)
16 return -FDT_ERR_BADSTATE
;
17 else if (fdt_magic(fdt
) != FDT_SW_MAGIC
)
18 return -FDT_ERR_BADMAGIC
;
22 #define FDT_SW_PROBE(fdt) \
25 if ((err = fdt_sw_probe_(fdt)) != 0) \
29 /* 'memrsv' state: Initial state after fdt_create()
32 * fdt_add_reservmap_entry()
33 * fdt_finish_reservemap() [moves to 'struct' state]
35 static int fdt_sw_probe_memrsv_(void *fdt
)
37 int err
= fdt_sw_probe_(fdt
);
41 if (fdt_off_dt_strings(fdt
) != 0)
42 return -FDT_ERR_BADSTATE
;
46 #define FDT_SW_PROBE_MEMRSV(fdt) \
49 if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
53 /* 'struct' state: Enter this state after fdt_finish_reservemap()
59 * fdt_finish() [moves to 'complete' state]
61 static int fdt_sw_probe_struct_(void *fdt
)
63 int err
= fdt_sw_probe_(fdt
);
67 if (fdt_off_dt_strings(fdt
) != fdt_totalsize(fdt
))
68 return -FDT_ERR_BADSTATE
;
72 #define FDT_SW_PROBE_STRUCT(fdt) \
75 if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
79 static inline uint32_t sw_flags(void *fdt
)
81 /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
82 return fdt_last_comp_version(fdt
);
85 /* 'complete' state: Enter this state after fdt_finish()
87 * Allowed functions: none
90 static void *fdt_grab_space_(void *fdt
, size_t len
)
92 int offset
= fdt_size_dt_struct(fdt
);
95 spaceleft
= fdt_totalsize(fdt
) - fdt_off_dt_struct(fdt
)
96 - fdt_size_dt_strings(fdt
);
98 if ((offset
+ len
< offset
) || (offset
+ len
> spaceleft
))
101 fdt_set_size_dt_struct(fdt
, offset
+ len
);
102 return fdt_offset_ptr_w_(fdt
, offset
);
105 int fdt_create_with_flags(void *buf
, int bufsize
, uint32_t flags
)
107 const size_t hdrsize
= FDT_ALIGN(sizeof(struct fdt_header
),
108 sizeof(struct fdt_reserve_entry
));
111 if (bufsize
< hdrsize
)
112 return -FDT_ERR_NOSPACE
;
114 if (flags
& ~FDT_CREATE_FLAGS_ALL
)
115 return -FDT_ERR_BADFLAGS
;
117 memset(buf
, 0, bufsize
);
120 * magic and last_comp_version keep intermediate state during the fdt
121 * creation process, which is replaced with the proper FDT format by
124 * flags should be accessed with sw_flags().
126 fdt_set_magic(fdt
, FDT_SW_MAGIC
);
127 fdt_set_version(fdt
, FDT_LAST_SUPPORTED_VERSION
);
128 fdt_set_last_comp_version(fdt
, flags
);
130 fdt_set_totalsize(fdt
, bufsize
);
132 fdt_set_off_mem_rsvmap(fdt
, hdrsize
);
133 fdt_set_off_dt_struct(fdt
, fdt_off_mem_rsvmap(fdt
));
134 fdt_set_off_dt_strings(fdt
, 0);
139 int fdt_create(void *buf
, int bufsize
)
141 return fdt_create_with_flags(buf
, bufsize
, 0);
144 int fdt_resize(void *fdt
, void *buf
, int bufsize
)
146 size_t headsize
, tailsize
;
147 char *oldtail
, *newtail
;
151 headsize
= fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
);
152 tailsize
= fdt_size_dt_strings(fdt
);
154 if ((headsize
+ tailsize
) > fdt_totalsize(fdt
))
155 return -FDT_ERR_INTERNAL
;
157 if ((headsize
+ tailsize
) > bufsize
)
158 return -FDT_ERR_NOSPACE
;
160 oldtail
= (char *)fdt
+ fdt_totalsize(fdt
) - tailsize
;
161 newtail
= (char *)buf
+ bufsize
- tailsize
;
163 /* Two cases to avoid clobbering data if the old and new
164 * buffers partially overlap */
166 memmove(buf
, fdt
, headsize
);
167 memmove(newtail
, oldtail
, tailsize
);
169 memmove(newtail
, oldtail
, tailsize
);
170 memmove(buf
, fdt
, headsize
);
173 fdt_set_totalsize(buf
, bufsize
);
174 if (fdt_off_dt_strings(buf
))
175 fdt_set_off_dt_strings(buf
, bufsize
);
180 int fdt_add_reservemap_entry(void *fdt
, uint64_t addr
, uint64_t size
)
182 struct fdt_reserve_entry
*re
;
185 FDT_SW_PROBE_MEMRSV(fdt
);
187 offset
= fdt_off_dt_struct(fdt
);
188 if ((offset
+ sizeof(*re
)) > fdt_totalsize(fdt
))
189 return -FDT_ERR_NOSPACE
;
191 re
= (struct fdt_reserve_entry
*)((char *)fdt
+ offset
);
192 re
->address
= cpu_to_fdt64(addr
);
193 re
->size
= cpu_to_fdt64(size
);
195 fdt_set_off_dt_struct(fdt
, offset
+ sizeof(*re
));
200 int fdt_finish_reservemap(void *fdt
)
202 int err
= fdt_add_reservemap_entry(fdt
, 0, 0);
207 fdt_set_off_dt_strings(fdt
, fdt_totalsize(fdt
));
211 int fdt_begin_node(void *fdt
, const char *name
)
213 struct fdt_node_header
*nh
;
216 FDT_SW_PROBE_STRUCT(fdt
);
218 namelen
= strlen(name
) + 1;
219 nh
= fdt_grab_space_(fdt
, sizeof(*nh
) + FDT_TAGALIGN(namelen
));
221 return -FDT_ERR_NOSPACE
;
223 nh
->tag
= cpu_to_fdt32(FDT_BEGIN_NODE
);
224 memcpy(nh
->name
, name
, namelen
);
228 int fdt_end_node(void *fdt
)
232 FDT_SW_PROBE_STRUCT(fdt
);
234 en
= fdt_grab_space_(fdt
, FDT_TAGSIZE
);
236 return -FDT_ERR_NOSPACE
;
238 *en
= cpu_to_fdt32(FDT_END_NODE
);
242 static int fdt_add_string_(void *fdt
, const char *s
)
244 char *strtab
= (char *)fdt
+ fdt_totalsize(fdt
);
245 int strtabsize
= fdt_size_dt_strings(fdt
);
246 int len
= strlen(s
) + 1;
247 int struct_top
, offset
;
249 offset
= -strtabsize
- len
;
250 struct_top
= fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
);
251 if (fdt_totalsize(fdt
) + offset
< struct_top
)
252 return 0; /* no more room :( */
254 memcpy(strtab
+ offset
, s
, len
);
255 fdt_set_size_dt_strings(fdt
, strtabsize
+ len
);
259 /* Must only be used to roll back in case of error */
260 static void fdt_del_last_string_(void *fdt
, const char *s
)
262 int strtabsize
= fdt_size_dt_strings(fdt
);
263 int len
= strlen(s
) + 1;
265 fdt_set_size_dt_strings(fdt
, strtabsize
- len
);
268 static int fdt_find_add_string_(void *fdt
, const char *s
, int *allocated
)
270 char *strtab
= (char *)fdt
+ fdt_totalsize(fdt
);
271 int strtabsize
= fdt_size_dt_strings(fdt
);
276 p
= fdt_find_string_(strtab
- strtabsize
, strtabsize
, s
);
282 return fdt_add_string_(fdt
, s
);
285 int fdt_property_placeholder(void *fdt
, const char *name
, int len
, void **valp
)
287 struct fdt_property
*prop
;
291 FDT_SW_PROBE_STRUCT(fdt
);
293 /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
294 if (sw_flags(fdt
) & FDT_CREATE_FLAG_NO_NAME_DEDUP
) {
296 nameoff
= fdt_add_string_(fdt
, name
);
298 nameoff
= fdt_find_add_string_(fdt
, name
, &allocated
);
301 return -FDT_ERR_NOSPACE
;
303 prop
= fdt_grab_space_(fdt
, sizeof(*prop
) + FDT_TAGALIGN(len
));
306 fdt_del_last_string_(fdt
, name
);
307 return -FDT_ERR_NOSPACE
;
310 prop
->tag
= cpu_to_fdt32(FDT_PROP
);
311 prop
->nameoff
= cpu_to_fdt32(nameoff
);
312 prop
->len
= cpu_to_fdt32(len
);
317 int fdt_property(void *fdt
, const char *name
, const void *val
, int len
)
322 ret
= fdt_property_placeholder(fdt
, name
, len
, &ptr
);
325 memcpy(ptr
, val
, len
);
329 int fdt_finish(void *fdt
)
331 char *p
= (char *)fdt
;
333 int oldstroffset
, newstroffset
;
335 int offset
, nextoffset
;
337 FDT_SW_PROBE_STRUCT(fdt
);
340 end
= fdt_grab_space_(fdt
, sizeof(*end
));
342 return -FDT_ERR_NOSPACE
;
343 *end
= cpu_to_fdt32(FDT_END
);
345 /* Relocate the string table */
346 oldstroffset
= fdt_totalsize(fdt
) - fdt_size_dt_strings(fdt
);
347 newstroffset
= fdt_off_dt_struct(fdt
) + fdt_size_dt_struct(fdt
);
348 memmove(p
+ newstroffset
, p
+ oldstroffset
, fdt_size_dt_strings(fdt
));
349 fdt_set_off_dt_strings(fdt
, newstroffset
);
351 /* Walk the structure, correcting string offsets */
353 while ((tag
= fdt_next_tag(fdt
, offset
, &nextoffset
)) != FDT_END
) {
354 if (tag
== FDT_PROP
) {
355 struct fdt_property
*prop
=
356 fdt_offset_ptr_w_(fdt
, offset
);
359 nameoff
= fdt32_to_cpu(prop
->nameoff
);
360 nameoff
+= fdt_size_dt_strings(fdt
);
361 prop
->nameoff
= cpu_to_fdt32(nameoff
);
368 /* Finally, adjust the header */
369 fdt_set_totalsize(fdt
, newstroffset
+ fdt_size_dt_strings(fdt
));
371 /* And fix up fields that were keeping intermediate state. */
372 fdt_set_last_comp_version(fdt
, FDT_FIRST_SUPPORTED_VERSION
);
373 fdt_set_magic(fdt
, FDT_MAGIC
);