Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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