2023-07-05 13:50:18 +08:00
|
|
|
// SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause)
|
|
|
|
/*
|
|
|
|
* libfdt - Flat Device Tree manipulation
|
|
|
|
* Copyright (C) 2006 David Gibson, IBM Corporation.
|
|
|
|
*/
|
|
|
|
#include "libfdt_env.h"
|
|
|
|
|
|
|
|
#include <fdt.h>
|
|
|
|
#include <libfdt.h>
|
|
|
|
|
|
|
|
#include "libfdt_internal.h"
|
|
|
|
|
|
|
|
static int fdt_blocks_misordered_(const void *fdt,
|
2023-07-06 11:02:13 +08:00
|
|
|
int mem_rsv_size, int struct_size)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
return (fdt_off_mem_rsvmap(fdt) < FDT_ALIGN(sizeof(struct fdt_header), 8))
|
|
|
|
|| (fdt_off_dt_struct(fdt) <
|
|
|
|
(fdt_off_mem_rsvmap(fdt) + mem_rsv_size))
|
|
|
|
|| (fdt_off_dt_strings(fdt) <
|
|
|
|
(fdt_off_dt_struct(fdt) + struct_size))
|
|
|
|
|| (fdt_totalsize(fdt) <
|
|
|
|
(fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt)));
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_rw_probe_(void *fdt)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
if (can_assume(VALID_DTB))
|
|
|
|
return 0;
|
|
|
|
FDT_RO_PROBE(fdt);
|
|
|
|
|
|
|
|
if (!can_assume(LATEST) && fdt_version(fdt) < 17)
|
|
|
|
return -FDT_ERR_BADVERSION;
|
|
|
|
if (fdt_blocks_misordered_(fdt, sizeof(struct fdt_reserve_entry),
|
|
|
|
fdt_size_dt_struct(fdt)))
|
|
|
|
return -FDT_ERR_BADLAYOUT;
|
|
|
|
if (!can_assume(LATEST) && fdt_version(fdt) > 17)
|
|
|
|
fdt_set_version(fdt, 17);
|
|
|
|
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#define FDT_RW_PROBE(fdt) \
|
2023-07-06 11:02:13 +08:00
|
|
|
{ \
|
|
|
|
int err_; \
|
|
|
|
if ((err_ = fdt_rw_probe_(fdt)) != 0) \
|
|
|
|
return err_; \
|
|
|
|
}
|
2023-07-05 13:50:18 +08:00
|
|
|
|
|
|
|
static inline unsigned int fdt_data_size_(void *fdt)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
return fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_splice_(void *fdt, void *splicepoint, int oldlen, int newlen)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
char *p = splicepoint;
|
|
|
|
unsigned int dsize = fdt_data_size_(fdt);
|
|
|
|
size_t soff = p - (char *)fdt;
|
|
|
|
|
|
|
|
if ((oldlen < 0) || (soff + oldlen < soff) || (soff + oldlen > dsize))
|
|
|
|
return -FDT_ERR_BADOFFSET;
|
|
|
|
if ((p < (char *)fdt) || (dsize + newlen < (unsigned)oldlen))
|
|
|
|
return -FDT_ERR_BADOFFSET;
|
|
|
|
if (dsize - oldlen + newlen > fdt_totalsize(fdt))
|
|
|
|
return -FDT_ERR_NOSPACE;
|
|
|
|
memmove(p + newlen, p + oldlen, ((char *)fdt + dsize) - (p + oldlen));
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_splice_mem_rsv_(void *fdt, struct fdt_reserve_entry *p,
|
2023-07-06 11:02:13 +08:00
|
|
|
int oldn, int newn)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int delta = (newn - oldn) * sizeof(*p);
|
|
|
|
int err;
|
|
|
|
err = fdt_splice_(fdt, p, oldn * sizeof(*p), newn * sizeof(*p));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
fdt_set_off_dt_struct(fdt, fdt_off_dt_struct(fdt) + delta);
|
|
|
|
fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_splice_struct_(void *fdt, void *p,
|
2023-07-06 11:02:13 +08:00
|
|
|
int oldlen, int newlen)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int delta = newlen - oldlen;
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
if ((err = fdt_splice_(fdt, p, oldlen, newlen)))
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
fdt_set_size_dt_struct(fdt, fdt_size_dt_struct(fdt) + delta);
|
|
|
|
fdt_set_off_dt_strings(fdt, fdt_off_dt_strings(fdt) + delta);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Must only be used to roll back in case of error */
|
|
|
|
static void fdt_del_last_string_(void *fdt, const char *s)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int newlen = strlen(s) + 1;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) - newlen);
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_splice_string_(void *fdt, int newlen)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
void *p = (char *)fdt
|
|
|
|
+ fdt_off_dt_strings(fdt) + fdt_size_dt_strings(fdt);
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
if ((err = fdt_splice_(fdt, p, 0, newlen)))
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
fdt_set_size_dt_strings(fdt, fdt_size_dt_strings(fdt) + newlen);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* fdt_find_add_string_() - Find or allocate a string
|
|
|
|
*
|
|
|
|
* @fdt: pointer to the device tree to check/adjust
|
|
|
|
* @s: string to find/add
|
|
|
|
* @allocated: Set to 0 if the string was found, 1 if not found and so
|
2023-07-06 11:02:13 +08:00
|
|
|
* allocated. Ignored if can_assume(NO_ROLLBACK)
|
2023-07-05 13:50:18 +08:00
|
|
|
* @return offset of string in the string table (whether found or added)
|
|
|
|
*/
|
|
|
|
static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
char *strtab = (char *)fdt + fdt_off_dt_strings(fdt);
|
|
|
|
const char *p;
|
|
|
|
char *new;
|
|
|
|
int len = strlen(s) + 1;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (!can_assume(NO_ROLLBACK))
|
|
|
|
*allocated = 0;
|
|
|
|
|
|
|
|
p = fdt_find_string_(strtab, fdt_size_dt_strings(fdt), s);
|
|
|
|
if (p)
|
|
|
|
/* found it */
|
|
|
|
return (p - strtab);
|
|
|
|
|
|
|
|
new = strtab + fdt_size_dt_strings(fdt);
|
|
|
|
err = fdt_splice_string_(fdt, len);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!can_assume(NO_ROLLBACK))
|
|
|
|
*allocated = 1;
|
|
|
|
|
|
|
|
memcpy(new, s, len);
|
|
|
|
return (new - strtab);
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
struct fdt_reserve_entry *re;
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
re = fdt_mem_rsv_w_(fdt, fdt_num_mem_rsv(fdt));
|
|
|
|
err = fdt_splice_mem_rsv_(fdt, re, 0, 1);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
re->address = cpu_to_fdt64(address);
|
|
|
|
re->size = cpu_to_fdt64(size);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_del_mem_rsv(void *fdt, int n)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
struct fdt_reserve_entry *re = fdt_mem_rsv_w_(fdt, n);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
if (n >= fdt_num_mem_rsv(fdt))
|
|
|
|
return -FDT_ERR_NOTFOUND;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
return fdt_splice_mem_rsv_(fdt, re, 1, 0);
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_resize_property_(void *fdt, int nodeoffset, const char *name,
|
2023-07-06 11:02:13 +08:00
|
|
|
int len, struct fdt_property **prop)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int oldlen;
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
*prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
|
|
|
|
if (!*prop)
|
|
|
|
return oldlen;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
if ((err = fdt_splice_struct_(fdt, (*prop)->data, FDT_TAGALIGN(oldlen),
|
|
|
|
FDT_TAGALIGN(len))))
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
(*prop)->len = cpu_to_fdt32(len);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int fdt_add_property_(void *fdt, int nodeoffset, const char *name,
|
2023-07-06 11:02:13 +08:00
|
|
|
int len, struct fdt_property **prop)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int proplen;
|
|
|
|
int nextoffset;
|
|
|
|
int namestroff;
|
|
|
|
int err;
|
|
|
|
int allocated;
|
|
|
|
|
|
|
|
if ((nextoffset = fdt_check_node_offset_(fdt, nodeoffset)) < 0)
|
|
|
|
return nextoffset;
|
|
|
|
|
|
|
|
namestroff = fdt_find_add_string_(fdt, name, &allocated);
|
|
|
|
if (namestroff < 0)
|
|
|
|
return namestroff;
|
|
|
|
|
|
|
|
*prop = fdt_offset_ptr_w_(fdt, nextoffset);
|
|
|
|
proplen = sizeof(**prop) + FDT_TAGALIGN(len);
|
|
|
|
|
|
|
|
err = fdt_splice_struct_(fdt, *prop, 0, proplen);
|
|
|
|
if (err) {
|
|
|
|
/* Delete the string if we failed to add it */
|
|
|
|
if (!can_assume(NO_ROLLBACK) && allocated)
|
|
|
|
fdt_del_last_string_(fdt, name);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*prop)->tag = cpu_to_fdt32(FDT_PROP);
|
|
|
|
(*prop)->nameoff = cpu_to_fdt32(namestroff);
|
|
|
|
(*prop)->len = cpu_to_fdt32(len);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_set_name(void *fdt, int nodeoffset, const char *name)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
char *namep;
|
|
|
|
int oldlen, newlen;
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
namep = (char *)(uintptr_t)fdt_get_name(fdt, nodeoffset, &oldlen);
|
|
|
|
if (!namep)
|
|
|
|
return oldlen;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
newlen = strlen(name);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
err = fdt_splice_struct_(fdt, namep, FDT_TAGALIGN(oldlen+1),
|
|
|
|
FDT_TAGALIGN(newlen+1));
|
|
|
|
if (err)
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
memcpy(namep, name, newlen+1);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_setprop_placeholder(void *fdt, int nodeoffset, const char *name,
|
2023-07-06 11:02:13 +08:00
|
|
|
int len, void **prop_data)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
struct fdt_property *prop;
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
err = fdt_resize_property_(fdt, nodeoffset, name, len, &prop);
|
|
|
|
if (err == -FDT_ERR_NOTFOUND)
|
|
|
|
err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
*prop_data = prop->data;
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_setprop(void *fdt, int nodeoffset, const char *name,
|
2023-07-06 11:02:13 +08:00
|
|
|
const void *val, int len)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
void *prop_data;
|
|
|
|
int err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
err = fdt_setprop_placeholder(fdt, nodeoffset, name, len, &prop_data);
|
|
|
|
if (err)
|
|
|
|
return err;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
if (len)
|
|
|
|
memcpy(prop_data, val, len);
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
|
2023-07-06 11:02:13 +08:00
|
|
|
const void *val, int len)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
struct fdt_property *prop;
|
|
|
|
int err, oldlen, newlen;
|
|
|
|
|
|
|
|
FDT_RW_PROBE(fdt);
|
|
|
|
|
|
|
|
prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen);
|
|
|
|
if (prop) {
|
|
|
|
newlen = len + oldlen;
|
|
|
|
err = fdt_splice_struct_(fdt, prop->data,
|
|
|
|
FDT_TAGALIGN(oldlen),
|
|
|
|
FDT_TAGALIGN(newlen));
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
prop->len = cpu_to_fdt32(newlen);
|
|
|
|
memcpy(prop->data + oldlen, val, len);
|
|
|
|
} else {
|
|
|
|
err = fdt_add_property_(fdt, nodeoffset, name, len, &prop);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
memcpy(prop->data, val, len);
|
|
|
|
}
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_delprop(void *fdt, int nodeoffset, const char *name)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
struct fdt_property *prop;
|
|
|
|
int len, proplen;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
|
|
|
|
if (!prop)
|
|
|
|
return len;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
proplen = sizeof(*prop) + FDT_TAGALIGN(len);
|
|
|
|
return fdt_splice_struct_(fdt, prop, proplen, 0);
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_add_subnode_namelen(void *fdt, int parentoffset,
|
2023-07-06 11:02:13 +08:00
|
|
|
const char *name, int namelen)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
struct fdt_node_header *nh;
|
|
|
|
int offset, nextoffset;
|
|
|
|
int nodelen;
|
|
|
|
int err;
|
|
|
|
uint32_t tag;
|
|
|
|
fdt32_t *endtag;
|
|
|
|
|
|
|
|
FDT_RW_PROBE(fdt);
|
|
|
|
|
|
|
|
offset = fdt_subnode_offset_namelen(fdt, parentoffset, name, namelen);
|
|
|
|
if (offset >= 0)
|
|
|
|
return -FDT_ERR_EXISTS;
|
|
|
|
else if (offset != -FDT_ERR_NOTFOUND)
|
|
|
|
return offset;
|
|
|
|
|
|
|
|
/* Try to place the new node after the parent's properties */
|
|
|
|
tag = fdt_next_tag(fdt, parentoffset, &nextoffset);
|
|
|
|
/* the fdt_subnode_offset_namelen() should ensure this never hits */
|
|
|
|
if (!can_assume(LIBFDT_FLAWLESS) && (tag != FDT_BEGIN_NODE))
|
|
|
|
return -FDT_ERR_INTERNAL;
|
|
|
|
do {
|
|
|
|
offset = nextoffset;
|
|
|
|
tag = fdt_next_tag(fdt, offset, &nextoffset);
|
|
|
|
} while ((tag == FDT_PROP) || (tag == FDT_NOP));
|
|
|
|
|
|
|
|
nh = fdt_offset_ptr_w_(fdt, offset);
|
|
|
|
nodelen = sizeof(*nh) + FDT_TAGALIGN(namelen+1) + FDT_TAGSIZE;
|
|
|
|
|
|
|
|
err = fdt_splice_struct_(fdt, nh, 0, nodelen);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
|
|
|
|
memset(nh->name, 0, FDT_TAGALIGN(namelen+1));
|
|
|
|
memcpy(nh->name, name, namelen);
|
|
|
|
endtag = (fdt32_t *)((char *)nh + nodelen - FDT_TAGSIZE);
|
|
|
|
*endtag = cpu_to_fdt32(FDT_END_NODE);
|
|
|
|
|
|
|
|
return offset;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_add_subnode(void *fdt, int parentoffset, const char *name)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
return fdt_add_subnode_namelen(fdt, parentoffset, name, strlen(name));
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_del_node(void *fdt, int nodeoffset)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int endoffset;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
endoffset = fdt_node_end_offset_(fdt, nodeoffset);
|
|
|
|
if (endoffset < 0)
|
|
|
|
return endoffset;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
return fdt_splice_struct_(fdt, fdt_offset_ptr_w_(fdt, nodeoffset),
|
|
|
|
endoffset - nodeoffset, 0);
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fdt_packblocks_(const char *old, char *new,
|
2023-07-06 11:02:13 +08:00
|
|
|
int mem_rsv_size,
|
|
|
|
int struct_size,
|
|
|
|
int strings_size)
|
2023-07-05 13:50:18 +08:00
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int mem_rsv_off, struct_off, strings_off;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
mem_rsv_off = FDT_ALIGN(sizeof(struct fdt_header), 8);
|
|
|
|
struct_off = mem_rsv_off + mem_rsv_size;
|
|
|
|
strings_off = struct_off + struct_size;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
memmove(new + mem_rsv_off, old + fdt_off_mem_rsvmap(old), mem_rsv_size);
|
|
|
|
fdt_set_off_mem_rsvmap(new, mem_rsv_off);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
memmove(new + struct_off, old + fdt_off_dt_struct(old), struct_size);
|
|
|
|
fdt_set_off_dt_struct(new, struct_off);
|
|
|
|
fdt_set_size_dt_struct(new, struct_size);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
memmove(new + strings_off, old + fdt_off_dt_strings(old), strings_size);
|
|
|
|
fdt_set_off_dt_strings(new, strings_off);
|
|
|
|
fdt_set_size_dt_strings(new, fdt_size_dt_strings(old));
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_open_into(const void *fdt, void *buf, int bufsize)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int err;
|
|
|
|
int mem_rsv_size, struct_size;
|
|
|
|
int newsize;
|
|
|
|
const char *fdtstart = fdt;
|
|
|
|
const char *fdtend = fdtstart + fdt_totalsize(fdt);
|
|
|
|
char *tmp;
|
|
|
|
|
|
|
|
FDT_RO_PROBE(fdt);
|
|
|
|
|
|
|
|
mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
|
|
|
|
* sizeof(struct fdt_reserve_entry);
|
|
|
|
|
|
|
|
if (can_assume(LATEST) || fdt_version(fdt) >= 17) {
|
|
|
|
struct_size = fdt_size_dt_struct(fdt);
|
|
|
|
} else if (fdt_version(fdt) == 16) {
|
|
|
|
struct_size = 0;
|
|
|
|
while (fdt_next_tag(fdt, struct_size, &struct_size) != FDT_END)
|
|
|
|
;
|
|
|
|
if (struct_size < 0)
|
|
|
|
return struct_size;
|
|
|
|
} else {
|
|
|
|
return -FDT_ERR_BADVERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_assume(LIBFDT_ORDER) ||
|
|
|
|
!fdt_blocks_misordered_(fdt, mem_rsv_size, struct_size)) {
|
|
|
|
/* no further work necessary */
|
|
|
|
err = fdt_move(fdt, buf, bufsize);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
fdt_set_version(buf, 17);
|
|
|
|
fdt_set_size_dt_struct(buf, struct_size);
|
|
|
|
fdt_set_totalsize(buf, bufsize);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Need to reorder */
|
|
|
|
newsize = FDT_ALIGN(sizeof(struct fdt_header), 8) + mem_rsv_size
|
|
|
|
+ struct_size + fdt_size_dt_strings(fdt);
|
|
|
|
|
|
|
|
if (bufsize < newsize)
|
|
|
|
return -FDT_ERR_NOSPACE;
|
|
|
|
|
|
|
|
/* First attempt to build converted tree at beginning of buffer */
|
|
|
|
tmp = buf;
|
|
|
|
/* But if that overlaps with the old tree... */
|
|
|
|
if (((tmp + newsize) > fdtstart) && (tmp < fdtend)) {
|
|
|
|
/* Try right after the old tree instead */
|
|
|
|
tmp = (char *)(uintptr_t)fdtend;
|
|
|
|
if ((tmp + newsize) > ((char *)buf + bufsize))
|
|
|
|
return -FDT_ERR_NOSPACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
fdt_packblocks_(fdt, tmp, mem_rsv_size, struct_size,
|
|
|
|
fdt_size_dt_strings(fdt));
|
|
|
|
memmove(buf, tmp, newsize);
|
|
|
|
|
|
|
|
fdt_set_magic(buf, FDT_MAGIC);
|
|
|
|
fdt_set_totalsize(buf, bufsize);
|
|
|
|
fdt_set_version(buf, 17);
|
|
|
|
fdt_set_last_comp_version(buf, 16);
|
|
|
|
fdt_set_boot_cpuid_phys(buf, fdt_boot_cpuid_phys(fdt));
|
|
|
|
|
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int fdt_pack(void *fdt)
|
|
|
|
{
|
2023-07-06 11:02:13 +08:00
|
|
|
int mem_rsv_size;
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
FDT_RW_PROBE(fdt);
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
mem_rsv_size = (fdt_num_mem_rsv(fdt)+1)
|
|
|
|
* sizeof(struct fdt_reserve_entry);
|
|
|
|
fdt_packblocks_(fdt, fdt, mem_rsv_size, fdt_size_dt_struct(fdt),
|
|
|
|
fdt_size_dt_strings(fdt));
|
|
|
|
fdt_set_totalsize(fdt, fdt_data_size_(fdt));
|
2023-07-05 13:50:18 +08:00
|
|
|
|
2023-07-06 11:02:13 +08:00
|
|
|
return 0;
|
2023-07-05 13:50:18 +08:00
|
|
|
}
|