treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / scripts / dtc / libfdt / fdt_sw.c
blob76bea22f734f94c663f369dba031e0fab27a0410
1 // SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
2 /*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
5 */
6 #include "libfdt_env.h"
8 #include <fdt.h>
9 #include <libfdt.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;
19 return 0;
22 #define FDT_SW_PROBE(fdt) \
23 { \
24 int err; \
25 if ((err = fdt_sw_probe_(fdt)) != 0) \
26 return err; \
29 /* 'memrsv' state: Initial state after fdt_create()
31 * Allowed functions:
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);
38 if (err)
39 return err;
41 if (fdt_off_dt_strings(fdt) != 0)
42 return -FDT_ERR_BADSTATE;
43 return 0;
46 #define FDT_SW_PROBE_MEMRSV(fdt) \
47 { \
48 int err; \
49 if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
50 return err; \
53 /* 'struct' state: Enter this state after fdt_finish_reservemap()
55 * Allowed functions:
56 * fdt_begin_node()
57 * fdt_end_node()
58 * fdt_property*()
59 * fdt_finish() [moves to 'complete' state]
61 static int fdt_sw_probe_struct_(void *fdt)
63 int err = fdt_sw_probe_(fdt);
64 if (err)
65 return err;
67 if (fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
68 return -FDT_ERR_BADSTATE;
69 return 0;
72 #define FDT_SW_PROBE_STRUCT(fdt) \
73 { \
74 int err; \
75 if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
76 return err; \
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);
93 int spaceleft;
95 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
96 - fdt_size_dt_strings(fdt);
98 if ((offset + len < offset) || (offset + len > spaceleft))
99 return NULL;
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));
109 void *fdt = buf;
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
122 * fdt_finish().
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);
136 return 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;
149 FDT_SW_PROBE(fdt);
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 */
165 if (buf <= fdt) {
166 memmove(buf, fdt, headsize);
167 memmove(newtail, oldtail, tailsize);
168 } else {
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);
177 return 0;
180 int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
182 struct fdt_reserve_entry *re;
183 int offset;
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));
197 return 0;
200 int fdt_finish_reservemap(void *fdt)
202 int err = fdt_add_reservemap_entry(fdt, 0, 0);
204 if (err)
205 return err;
207 fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
208 return 0;
211 int fdt_begin_node(void *fdt, const char *name)
213 struct fdt_node_header *nh;
214 int namelen;
216 FDT_SW_PROBE_STRUCT(fdt);
218 namelen = strlen(name) + 1;
219 nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
220 if (! nh)
221 return -FDT_ERR_NOSPACE;
223 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
224 memcpy(nh->name, name, namelen);
225 return 0;
228 int fdt_end_node(void *fdt)
230 fdt32_t *en;
232 FDT_SW_PROBE_STRUCT(fdt);
234 en = fdt_grab_space_(fdt, FDT_TAGSIZE);
235 if (! en)
236 return -FDT_ERR_NOSPACE;
238 *en = cpu_to_fdt32(FDT_END_NODE);
239 return 0;
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);
256 return offset;
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);
272 const char *p;
274 *allocated = 0;
276 p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
277 if (p)
278 return p - strtab;
280 *allocated = 1;
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;
288 int nameoff;
289 int allocated;
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) {
295 allocated = 1;
296 nameoff = fdt_add_string_(fdt, name);
297 } else {
298 nameoff = fdt_find_add_string_(fdt, name, &allocated);
300 if (nameoff == 0)
301 return -FDT_ERR_NOSPACE;
303 prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
304 if (! prop) {
305 if (allocated)
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);
313 *valp = prop->data;
314 return 0;
317 int fdt_property(void *fdt, const char *name, const void *val, int len)
319 void *ptr;
320 int ret;
322 ret = fdt_property_placeholder(fdt, name, len, &ptr);
323 if (ret)
324 return ret;
325 memcpy(ptr, val, len);
326 return 0;
329 int fdt_finish(void *fdt)
331 char *p = (char *)fdt;
332 fdt32_t *end;
333 int oldstroffset, newstroffset;
334 uint32_t tag;
335 int offset, nextoffset;
337 FDT_SW_PROBE_STRUCT(fdt);
339 /* Add terminator */
340 end = fdt_grab_space_(fdt, sizeof(*end));
341 if (! 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 */
352 offset = 0;
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);
357 int nameoff;
359 nameoff = fdt32_to_cpu(prop->nameoff);
360 nameoff += fdt_size_dt_strings(fdt);
361 prop->nameoff = cpu_to_fdt32(nameoff);
363 offset = nextoffset;
365 if (nextoffset < 0)
366 return nextoffset;
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);
375 return 0;