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"
7 
8 #include <fdt.h>
9 #include <libfdt.h>
10 
11 #include "libfdt_internal.h"
12 
fdt_blocks_misordered_(const void * fdt,int mem_rsv_size,int struct_size)13 static int fdt_blocks_misordered_(const void *fdt,
14 				  int mem_rsv_size, int struct_size)
15 {
16 	if (!fdt_chk_basic())
17 		return false;
18 	return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
19 		|| (fdt_off_dt_struct(fdt) <
20 		    (fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
21 		|| (fdt_off_dt_strings(fdt) <
22 		    (fdt_off_dt_struct(fdt) + struct_size))
23 		|| (fdt_totalsize(fdt) <
24 		    (fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
25 }
26 
fdt_rw_probe_(void * fdt)27 static int fdt_rw_probe_(void *fdt)
28 {
29 	if (!fdt_chk_basic())
30 		return 0;
31 	FDT_RO_PROBE(fdt);
32 
33 	if (fdt_chk_version() && fdt_version(fdt) < 17)
34 		return -FDT_ERR_BADVERSION;
35 	if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
36 				   fdt_size_dt_struct(fdt)))
37 		return -FDT_ERR_BADLAYOUT;
38 	if (fdt_chk_version() && fdt_version(fdt) > 17)
39 		fdt_set_version(fdt, 17);
40 
41 	return 0;
42 }
43 
44 #define FDT_RW_PROBE(fdt) \
45 	{ \
46 		int err_; \
47 		if (fdt_chk_extra() && (err_ = fdt_rw_probe_(fdt)) != 0) \
48 			return err_; \
49 	}
50 
fdt_data_size_(void * fdt)51 static inline int fdt_data_size_(void *fdt)
52 {
53 	return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
54 }
55 
fdt_splice_(void * fdt,void * splicepoint,int oldlen,int newlen)56 static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
57 {
58 	char *p = splicepoint;
59 	char *end = (char *)fdt + fdt_data_size_(fdt);
60 
61 	if (((p + oldlen) < p) || ((p + oldlen) > end))
62 		return -FDT_ERR_BADOFFSET;
63 	if ((p < (char *)fdt) || ((end - oldlen + newlen) < (char *)fdt))
64 		return -FDT_ERR_BADOFFSET;
65 	if ((end - oldlen + newlen) > ((char *)fdt + fdt_totalsize(fdt)))
66 		return -FDT_ERR_NOSPACE;
67 	memmove(p + newlen, p + oldlen, end - p - oldlen);
68 	return 0;
69 }
70 
fdt_splice_mem_rsv_(void * fdt,struct fdt_reserve_entry * p,int oldn,int newn)71 static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
72 			       int oldn, int newn)
73 {
74 	int delta = (newn - oldn) * sizeof(*p);
75 	int err;
76 	err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
77 	if (err)
78 		return err;
79 	fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
80 	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
81 	return 0;
82 }
83 
fdt_splice_struct_(void * fdt,void * p,int oldlen,int newlen)84 static int fdt_splice_struct_(void *fdt, void *p,
85 			      int oldlen, int newlen)
86 {
87 	int delta = newlen - oldlen;
88 	int err;
89 
90 	if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
91 		return err;
92 
93 	fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
94 	fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
95 	return 0;
96 }
97 
98 /* Must only be used to roll back in case of error */
fdt_del_last_string_(void * fdt,const char * s)99 static void fdt_del_last_string_(void *fdt, const char *s)
100 {
101 	int newlen = strlen(s) + 1;
102 
103 	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
104 }
105 
fdt_splice_string_(void * fdt,int newlen)106 static int fdt_splice_string_(void *fdt, int newlen)
107 {
108 	void *p = (char *)fdt
109 		+ fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
110 	int err;
111 
112 	if ((err = fdt_splice_(fdt, p, 0, newlen)))
113 		return err;
114 
115 	fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
116 	return 0;
117 }
118 
119 /**
120  * fdt_find_add_string_() - Find or allocate a string
121  *
122  * @fdt: pointer to the device tree to check/adjust
123  * @s: string to find/add
124  * @allocated: Set to 0 if the string was found, 1 if not found and so
125  *	allocated. Ignored if !fdt_chk_basic()
126  * @return offset of string in the string table (whether found or added)
127  */
fdt_find_add_string_(void * fdt,const char * s,int * allocated)128 static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
129 {
130 	char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
131 	const char *p;
132 	char *new;
133 	int len = strlen(s) + 1;
134 	int err;
135 
136 	if (fdt_chk_basic())
137 		*allocated = 0;
138 
139 	p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
140 	if (p)
141 		/* found it */
142 		return (p - strtab);
143 
144 	new = strtab + fdt_size_dt_strings(fdt);
145 	err = fdt_splice_string_(fdt, len);
146 	if (err)
147 		return err;
148 
149 	if (fdt_chk_basic())
150 		*allocated = 1;
151 
152 	memcpy(new, s, len);
153 	return (new - strtab);
154 }
155 
fdt_add_mem_rsv(void * fdt,uint64_t address,uint64_t size)156 int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
157 {
158 	struct fdt_reserve_entry *re;
159 	int err;
160 
161 	FDT_RW_PROBE(fdt);
162 
163 	re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
164 	err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
165 	if (err)
166 		return err;
167 
168 	re->address = cpu_to_fdt64(address);
169 	re->size = cpu_to_fdt64(size);
170 	return 0;
171 }
172 
fdt_del_mem_rsv(void * fdt,int n)173 int fdt_del_mem_rsv(void *fdt, int n)
174 {
175 	struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
176 
177 	FDT_RW_PROBE(fdt);
178 
179 	if (n >= fdt_num_mem_rsv(fdt))
180 		return -FDT_ERR_NOTFOUND;
181 
182 	return fdt_splice_mem_rsv_(fdt, re, 1, 0);
183 }
184 
fdt_resize_property_(void * fdt,int nodeoffset,const char * name,int len,struct fdt_property ** prop)185 static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
186 				int len, struct fdt_property **prop)
187 {
188 	int oldlen;
189 	int err;
190 
191 	*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
192 	if (!*prop)
193 		return oldlen;
194 
195 	if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
196 				      FDT_TAGALIGN(len))))
197 		return err;
198 
199 	(*prop)->len = cpu_to_fdt32(len);
200 	return 0;
201 }
202 
fdt_add_property_(void * fdt,int nodeoffset,const char * name,int len,struct fdt_property ** prop)203 static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
204 			     int len, struct fdt_property **prop)
205 {
206 	int proplen;
207 	int nextoffset;
208 	int namestroff;
209 	int err;
210 	int allocated;
211 
212 	if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
213 		return nextoffset;
214 
215 	namestroff = fdt_find_add_string_(fdt, name, &allocated);
216 	if (namestroff < 0)
217 		return namestroff;
218 
219 	*prop = fdt_offset_ptr_w_(fdt, nextoffset);
220 	proplen = sizeof(**prop) + FDT_TAGALIGN(len);
221 
222 	err = fdt_splice_struct_(fdt, *prop, 0, proplen);
223 	if (err) {
224 		/* Delete the string if we failed to add it */
225 		if (fdt_chk_basic() && allocated)
226 			fdt_del_last_string_(fdt, name);
227 		return err;
228 	}
229 
230 	(*prop)->tag = cpu_to_fdt32(FDT_PROP);
231 	(*prop)->nameoff = cpu_to_fdt32(namestroff);
232 	(*prop)->len = cpu_to_fdt32(len);
233 	return 0;
234 }
235 
fdt_set_name(void * fdt,int nodeoffset,const char * name)236 int fdt_set_name(void *fdt, int nodeoffset, const char *name)
237 {
238 	char *namep;
239 	int oldlen, newlen;
240 	int err;
241 
242 	FDT_RW_PROBE(fdt);
243 
244 	namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
245 	if (!namep)
246 		return oldlen;
247 
248 	newlen = strlen(name);
249 
250 	err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
251 				 FDT_TAGALIGN(newlen+1));
252 	if (err)
253 		return err;
254 
255 	memcpy(namep, name, newlen+1);
256 	return 0;
257 }
258 
fdt_setprop_placeholder(void * fdt,int nodeoffset,const char * name,int len,void ** prop_data)259 int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
260 			    int len, void **prop_data)
261 {
262 	struct fdt_property *prop;
263 	int err;
264 
265 	FDT_RW_PROBE(fdt);
266 
267 	err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
268 	if (err == -FDT_ERR_NOTFOUND)
269 		err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
270 	if (err)
271 		return err;
272 
273 	*prop_data = prop->data;
274 	return 0;
275 }
276 
fdt_setprop(void * fdt,int nodeoffset,const char * name,const void * val,int len)277 int fdt_setprop(void *fdt, int nodeoffset, const char *name,
278 		const void *val, int len)
279 {
280 	void *prop_data;
281 	int err;
282 
283 	err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
284 	if (err)
285 		return err;
286 
287 	if (len)
288 		memcpy(prop_data, val, len);
289 	return 0;
290 }
291 
fdt_appendprop(void * fdt,int nodeoffset,const char * name,const void * val,int len)292 int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
293 		   const void *val, int len)
294 {
295 	struct fdt_property *prop;
296 	int err, oldlen, newlen;
297 
298 	FDT_RW_PROBE(fdt);
299 
300 	prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
301 	if (prop) {
302 		newlen = len + oldlen;
303 		err = fdt_splice_struct_(fdt, prop->data,
304 					 FDT_TAGALIGN(oldlen),
305 					 FDT_TAGALIGN(newlen));
306 		if (err)
307 			return err;
308 		prop->len = cpu_to_fdt32(newlen);
309 		memcpy(prop->data + oldlen, val, len);
310 	} else {
311 		err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
312 		if (err)
313 			return err;
314 		memcpy(prop->data, val, len);
315 	}
316 	return 0;
317 }
318 
fdt_delprop(void * fdt,int nodeoffset,const char * name)319 int fdt_delprop(void *fdt, int nodeoffset, const char *name)
320 {
321 	struct fdt_property *prop;
322 	int len, proplen;
323 
324 	FDT_RW_PROBE(fdt);
325 
326 	prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
327 	if (!prop)
328 		return len;
329 
330 	proplen = sizeof(*prop) + FDT_TAGALIGN(len);
331 	return fdt_splice_struct_(fdt, prop, proplen, 0);
332 }
333 
fdt_add_subnode_namelen(void * fdt,int parentoffset,const char * name,int namelen)334 int fdt_add_subnode_namelen(void *fdt, int parentoffset,
335 			    const char *name, int namelen)
336 {
337 	struct fdt_node_header *nh;
338 	int offset, nextoffset;
339 	int nodelen;
340 	int err;
341 	uint32_t tag;
342 	fdt32_t *endtag;
343 
344 	FDT_RW_PROBE(fdt);
345 
346 	offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
347 	if (offset >= 0)
348 		return -FDT_ERR_EXISTS;
349 	else if (offset != -FDT_ERR_NOTFOUND)
350 		return offset;
351 
352 	/* Try to place the new node after the parent's properties */
353 	fdt_next_tag(fdt, parentoffset, &nextoffset); /* skip the BEGIN_NODE */
354 	do {
355 		offset = nextoffset;
356 		tag = fdt_next_tag(fdt, offset, &nextoffset);
357 	} while ((tag == FDT_PROP) || (tag == FDT_NOP));
358 
359 	nh = fdt_offset_ptr_w_(fdt, offset);
360 	nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
361 
362 	err = fdt_splice_struct_(fdt, nh, 0, nodelen);
363 	if (err)
364 		return err;
365 
366 	nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
367 	memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
368 	memcpy(nh->name, name, namelen);
369 	endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
370 	*endtag = cpu_to_fdt32(FDT_END_NODE);
371 
372 	return offset;
373 }
374 
fdt_add_subnode(void * fdt,int parentoffset,const char * name)375 int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
376 {
377 	return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
378 }
379 
fdt_del_node(void * fdt,int nodeoffset)380 int fdt_del_node(void *fdt, int nodeoffset)
381 {
382 	int endoffset;
383 
384 	FDT_RW_PROBE(fdt);
385 
386 	endoffset = fdt_node_end_offset_(fdt, nodeoffset);
387 	if (endoffset < 0)
388 		return endoffset;
389 
390 	return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
391 				  endoffset - nodeoffset, 0);
392 }
393 
fdt_packblocks_(const char * old,char * new,int mem_rsv_size,int struct_size)394 static void fdt_packblocks_(const char *old, char *new,
395 			    int mem_rsv_size, int struct_size)
396 {
397 	int mem_rsv_off, struct_off, strings_off;
398 
399 	mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
400 	struct_off = mem_rsv_off + mem_rsv_size;
401 	strings_off = struct_off + struct_size;
402 
403 	memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
404 	fdt_set_off_mem_rsvmap(new, mem_rsv_off);
405 
406 	memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
407 	fdt_set_off_dt_struct(new, struct_off);
408 	fdt_set_size_dt_struct(new, struct_size);
409 
410 	memmove(new + strings_off, old + fdt_off_dt_strings(old),
411 		fdt_size_dt_strings(old));
412 	fdt_set_off_dt_strings(new, strings_off);
413 	fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
414 }
415 
fdt_open_into(const void * fdt,void * buf,int bufsize)416 int fdt_open_into(const void *fdt, void *buf, int bufsize)
417 {
418 	int err;
419 	int mem_rsv_size, struct_size;
420 	int newsize;
421 	const char *fdtstart = fdt;
422 	const char *fdtend = fdtstart + fdt_totalsize(fdt);
423 	char *tmp;
424 
425 	FDT_RO_PROBE(fdt);
426 
427 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
428 		* sizeof(struct fdt_reserve_entry);
429 
430 	if (!fdt_chk_version() || fdt_version(fdt) >= 17) {
431 		struct_size = fdt_size_dt_struct(fdt);
432 	} else {
433 		struct_size = 0;
434 		while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
435 			;
436 		if (struct_size < 0)
437 			return struct_size;
438 	}
439 
440 	if (!fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
441 		/* no further work necessary */
442 		err = fdt_move(fdt, buf, bufsize);
443 		if (err)
444 			return err;
445 		fdt_set_version(buf, 17);
446 		fdt_set_size_dt_struct(buf, struct_size);
447 		fdt_set_totalsize(buf, bufsize);
448 		return 0;
449 	}
450 
451 	/* Need to reorder */
452 	newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
453 		+ struct_size + fdt_size_dt_strings(fdt);
454 
455 	if (bufsize < newsize)
456 		return -FDT_ERR_NOSPACE;
457 
458 	/* First attempt to build converted tree at beginning of buffer */
459 	tmp = buf;
460 	/* But if that overlaps with the old tree... */
461 	if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
462 		/* Try right after the old tree instead */
463 		tmp = (char *)(uintptr_t)fdtend;
464 		if ((tmp + newsize) > ((char *)buf + bufsize))
465 			return -FDT_ERR_NOSPACE;
466 	}
467 
468 	fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size);
469 	memmove(buf, tmp, newsize);
470 
471 	fdt_set_magic(buf, FDT_MAGIC);
472 	fdt_set_totalsize(buf, bufsize);
473 	fdt_set_version(buf, 17);
474 	fdt_set_last_comp_version(buf, 16);
475 	fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
476 
477 	return 0;
478 }
479 
fdt_pack(void * fdt)480 int fdt_pack(void *fdt)
481 {
482 	int mem_rsv_size;
483 
484 	FDT_RW_PROBE(fdt);
485 
486 	mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
487 		* sizeof(struct fdt_reserve_entry);
488 	fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt));
489 	fdt_set_totalsize(fdt, fdt_data_size_(fdt));
490 
491 	return 0;
492 }
493