blob: 26759d5dfb8cd47c04c0c8114eae15bbd58f36bb [file] [log] [blame]
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -05001// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +01002/*
3 * libfdt - Flat Device Tree manipulation
4 * Copyright (C) 2006 David Gibson, IBM Corporation.
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +01005 */
6#include "libfdt_env.h"
7
8#include <fdt.h>
9#include <libfdt.h>
10
11#include "libfdt_internal.h"
12
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050013static int fdt_sw_probe_(void *fdt)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010014{
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050015 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;
20 }
21
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010022 return 0;
23}
24
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050025#define FDT_SW_PROBE(fdt) \
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010026 { \
27 int err; \
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050028 if ((err = fdt_sw_probe_(fdt)) != 0) \
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010029 return err; \
30 }
31
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -050032/* '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 (!can_assume(VALID_INPUT) && 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 ((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)
65{
66 int err = fdt_sw_probe_(fdt);
67 if (err)
68 return err;
69
70 if (!can_assume(VALID_INPUT) &&
71 fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
72 return -FDT_ERR_BADSTATE;
73 return 0;
74}
75
76#define FDT_SW_PROBE_STRUCT(fdt) \
77 { \
78 int err; \
79 if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
80 return err; \
81 }
82
83static inline uint32_t sw_flags(void *fdt)
84{
85 /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
86 return fdt_last_comp_version(fdt);
87}
88
89/* 'complete' state: Enter this state after fdt_finish()
90 *
91 * Allowed functions: none
92 */
93
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +010094static void *fdt_grab_space_(void *fdt, size_t len)
95{
96 int offset = fdt_size_dt_struct(fdt);
97 int spaceleft;
98
99 spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
100 - fdt_size_dt_strings(fdt);
101
102 if ((offset + len < offset) || (offset + len > spaceleft))
103 return NULL;
104
105 fdt_set_size_dt_struct(fdt, offset + len);
106 return fdt_offset_ptr_w_(fdt, offset);
107}
108
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500109int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100110{
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500111 const size_t hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
112 sizeof(struct fdt_reserve_entry));
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100113 void *fdt = buf;
114
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500115 if (bufsize < hdrsize)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100116 return -FDT_ERR_NOSPACE;
117
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500118 if (flags & ~FDT_CREATE_FLAGS_ALL)
119 return -FDT_ERR_BADFLAGS;
120
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100121 memset(buf, 0, bufsize);
122
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500123 /*
124 * magic and last_comp_version keep intermediate state during the fdt
125 * creation process, which is replaced with the proper FDT format by
126 * fdt_finish().
127 *
128 * flags should be accessed with sw_flags().
129 */
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100130 fdt_set_magic(fdt, FDT_SW_MAGIC);
131 fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500132 fdt_set_last_comp_version(fdt, flags);
133
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100134 fdt_set_totalsize(fdt, bufsize);
135
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500136 fdt_set_off_mem_rsvmap(fdt, hdrsize);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100137 fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500138 fdt_set_off_dt_strings(fdt, 0);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100139
140 return 0;
141}
142
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500143int fdt_create(void *buf, int bufsize)
144{
145 return fdt_create_with_flags(buf, bufsize, 0);
146}
147
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100148int fdt_resize(void *fdt, void *buf, int bufsize)
149{
150 size_t headsize, tailsize;
151 char *oldtail, *newtail;
152
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500153 FDT_SW_PROBE(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100154
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500155 headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100156 tailsize = fdt_size_dt_strings(fdt);
157
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500158 if (!can_assume(VALID_DTB) &&
159 headsize + tailsize > fdt_totalsize(fdt))
160 return -FDT_ERR_INTERNAL;
161
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100162 if ((headsize + tailsize) > bufsize)
163 return -FDT_ERR_NOSPACE;
164
165 oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
166 newtail = (char *)buf + bufsize - tailsize;
167
168 /* Two cases to avoid clobbering data if the old and new
169 * buffers partially overlap */
170 if (buf <= fdt) {
171 memmove(buf, fdt, headsize);
172 memmove(newtail, oldtail, tailsize);
173 } else {
174 memmove(newtail, oldtail, tailsize);
175 memmove(buf, fdt, headsize);
176 }
177
178 fdt_set_totalsize(buf, bufsize);
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500179 if (fdt_off_dt_strings(buf))
180 fdt_set_off_dt_strings(buf, bufsize);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100181
182 return 0;
183}
184
185int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
186{
187 struct fdt_reserve_entry *re;
188 int offset;
189
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500190 FDT_SW_PROBE_MEMRSV(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100191
192 offset = fdt_off_dt_struct(fdt);
193 if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
194 return -FDT_ERR_NOSPACE;
195
196 re = (struct fdt_reserve_entry *)((char *)fdt + offset);
197 re->address = cpu_to_fdt64(addr);
198 re->size = cpu_to_fdt64(size);
199
200 fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
201
202 return 0;
203}
204
205int fdt_finish_reservemap(void *fdt)
206{
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500207 int err = fdt_add_reservemap_entry(fdt, 0, 0);
208
209 if (err)
210 return err;
211
212 fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
213 return 0;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100214}
215
216int fdt_begin_node(void *fdt, const char *name)
217{
218 struct fdt_node_header *nh;
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500219 int namelen;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100220
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500221 FDT_SW_PROBE_STRUCT(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100222
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500223 namelen = strlen(name) + 1;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100224 nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
225 if (! nh)
226 return -FDT_ERR_NOSPACE;
227
228 nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
229 memcpy(nh->name, name, namelen);
230 return 0;
231}
232
233int fdt_end_node(void *fdt)
234{
235 fdt32_t *en;
236
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500237 FDT_SW_PROBE_STRUCT(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100238
239 en = fdt_grab_space_(fdt, FDT_TAGSIZE);
240 if (! en)
241 return -FDT_ERR_NOSPACE;
242
243 *en = cpu_to_fdt32(FDT_END_NODE);
244 return 0;
245}
246
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500247static int fdt_add_string_(void *fdt, const char *s)
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100248{
249 char *strtab = (char *)fdt + fdt_totalsize(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100250 int strtabsize = fdt_size_dt_strings(fdt);
251 int len = strlen(s) + 1;
252 int struct_top, offset;
253
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100254 offset = -strtabsize - len;
255 struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
256 if (fdt_totalsize(fdt) + offset < struct_top)
257 return 0; /* no more room :( */
258
259 memcpy(strtab + offset, s, len);
260 fdt_set_size_dt_strings(fdt, strtabsize + len);
261 return offset;
262}
263
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500264/* Must only be used to roll back in case of error */
265static void fdt_del_last_string_(void *fdt, const char *s)
266{
267 int strtabsize = fdt_size_dt_strings(fdt);
268 int len = strlen(s) + 1;
269
270 fdt_set_size_dt_strings(fdt, strtabsize - len);
271}
272
273static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
274{
275 char *strtab = (char *)fdt + fdt_totalsize(fdt);
276 int strtabsize = fdt_size_dt_strings(fdt);
277 const char *p;
278
279 *allocated = 0;
280
281 p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
282 if (p)
283 return p - strtab;
284
285 *allocated = 1;
286
287 return fdt_add_string_(fdt, s);
288}
289
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100290int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
291{
292 struct fdt_property *prop;
293 int nameoff;
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500294 int allocated;
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100295
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500296 FDT_SW_PROBE_STRUCT(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100297
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500298 /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
299 if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
300 allocated = 1;
301 nameoff = fdt_add_string_(fdt, name);
302 } else {
303 nameoff = fdt_find_add_string_(fdt, name, &allocated);
304 }
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100305 if (nameoff == 0)
306 return -FDT_ERR_NOSPACE;
307
308 prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500309 if (! prop) {
310 if (allocated)
311 fdt_del_last_string_(fdt, name);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100312 return -FDT_ERR_NOSPACE;
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500313 }
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100314
315 prop->tag = cpu_to_fdt32(FDT_PROP);
316 prop->nameoff = cpu_to_fdt32(nameoff);
317 prop->len = cpu_to_fdt32(len);
318 *valp = prop->data;
319 return 0;
320}
321
322int fdt_property(void *fdt, const char *name, const void *val, int len)
323{
324 void *ptr;
325 int ret;
326
327 ret = fdt_property_placeholder(fdt, name, len, &ptr);
328 if (ret)
329 return ret;
330 memcpy(ptr, val, len);
331 return 0;
332}
333
334int fdt_finish(void *fdt)
335{
336 char *p = (char *)fdt;
337 fdt32_t *end;
338 int oldstroffset, newstroffset;
339 uint32_t tag;
340 int offset, nextoffset;
341
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500342 FDT_SW_PROBE_STRUCT(fdt);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100343
344 /* Add terminator */
345 end = fdt_grab_space_(fdt, sizeof(*end));
346 if (! end)
347 return -FDT_ERR_NOSPACE;
348 *end = cpu_to_fdt32(FDT_END);
349
350 /* Relocate the string table */
351 oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
352 newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
353 memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
354 fdt_set_off_dt_strings(fdt, newstroffset);
355
356 /* Walk the structure, correcting string offsets */
357 offset = 0;
358 while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
359 if (tag == FDT_PROP) {
360 struct fdt_property *prop =
361 fdt_offset_ptr_w_(fdt, offset);
362 int nameoff;
363
364 nameoff = fdt32_to_cpu(prop->nameoff);
365 nameoff += fdt_size_dt_strings(fdt);
366 prop->nameoff = cpu_to_fdt32(nameoff);
367 }
368 offset = nextoffset;
369 }
370 if (nextoffset < 0)
371 return nextoffset;
372
373 /* Finally, adjust the header */
374 fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500375
376 /* And fix up fields that were keeping intermediate state. */
377 fdt_set_last_comp_version(fdt, FDT_FIRST_SUPPORTED_VERSION);
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100378 fdt_set_magic(fdt, FDT_MAGIC);
Madhukar Pappireddy596fe0a2020-06-15 17:19:09 -0500379
Antonio Nino Diazb2db96f2018-10-19 00:56:54 +0100380 return 0;
381}