【libfdt】replace tab with space

This commit is contained in:
erikchan 2023-07-06 11:02:13 +08:00
parent 81c8919889
commit cc8781930c
13 changed files with 2782 additions and 2782 deletions

View File

@ -17,323 +17,323 @@
*/ */
int32_t fdt_ro_probe_(const void *fdt) int32_t fdt_ro_probe_(const void *fdt)
{ {
uint32_t totalsize = fdt_totalsize(fdt); uint32_t totalsize = fdt_totalsize(fdt);
if (can_assume(VALID_DTB)) if (can_assume(VALID_DTB))
return totalsize; return totalsize;
/* The device tree must be at an 8-byte aligned address */ /* The device tree must be at an 8-byte aligned address */
if ((uintptr_t)fdt & 7) if ((uintptr_t)fdt & 7)
return -FDT_ERR_ALIGNMENT; return -FDT_ERR_ALIGNMENT;
if (fdt_magic(fdt) == FDT_MAGIC) { if (fdt_magic(fdt) == FDT_MAGIC) {
/* Complete tree */ /* Complete tree */
if (!can_assume(LATEST)) { if (!can_assume(LATEST)) {
if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
return -FDT_ERR_BADVERSION; return -FDT_ERR_BADVERSION;
if (fdt_last_comp_version(fdt) > if (fdt_last_comp_version(fdt) >
FDT_LAST_SUPPORTED_VERSION) FDT_LAST_SUPPORTED_VERSION)
return -FDT_ERR_BADVERSION; return -FDT_ERR_BADVERSION;
} }
} else if (fdt_magic(fdt) == FDT_SW_MAGIC) { } else if (fdt_magic(fdt) == FDT_SW_MAGIC) {
/* Unfinished sequential-write blob */ /* Unfinished sequential-write blob */
if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0) if (!can_assume(VALID_INPUT) && fdt_size_dt_struct(fdt) == 0)
return -FDT_ERR_BADSTATE; return -FDT_ERR_BADSTATE;
} else { } else {
return -FDT_ERR_BADMAGIC; return -FDT_ERR_BADMAGIC;
} }
if (totalsize < INT32_MAX) if (totalsize < INT32_MAX)
return totalsize; return totalsize;
else else
return -FDT_ERR_TRUNCATED; return -FDT_ERR_TRUNCATED;
} }
static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off) static int check_off_(uint32_t hdrsize, uint32_t totalsize, uint32_t off)
{ {
return (off >= hdrsize) && (off <= totalsize); return (off >= hdrsize) && (off <= totalsize);
} }
static int check_block_(uint32_t hdrsize, uint32_t totalsize, static int check_block_(uint32_t hdrsize, uint32_t totalsize,
uint32_t base, uint32_t size) uint32_t base, uint32_t size)
{ {
if (!check_off_(hdrsize, totalsize, base)) if (!check_off_(hdrsize, totalsize, base))
return 0; /* block start out of bounds */ return 0; /* block start out of bounds */
if ((base + size) < base) if ((base + size) < base)
return 0; /* overflow */ return 0; /* overflow */
if (!check_off_(hdrsize, totalsize, base + size)) if (!check_off_(hdrsize, totalsize, base + size))
return 0; /* block end out of bounds */ return 0; /* block end out of bounds */
return 1; return 1;
} }
size_t fdt_header_size_(uint32_t version) size_t fdt_header_size_(uint32_t version)
{ {
if (version <= 1) if (version <= 1)
return FDT_V1_SIZE; return FDT_V1_SIZE;
else if (version <= 2) else if (version <= 2)
return FDT_V2_SIZE; return FDT_V2_SIZE;
else if (version <= 3) else if (version <= 3)
return FDT_V3_SIZE; return FDT_V3_SIZE;
else if (version <= 16) else if (version <= 16)
return FDT_V16_SIZE; return FDT_V16_SIZE;
else else
return FDT_V17_SIZE; return FDT_V17_SIZE;
} }
size_t fdt_header_size(const void *fdt) size_t fdt_header_size(const void *fdt)
{ {
return can_assume(LATEST) ? FDT_V17_SIZE : return can_assume(LATEST) ? FDT_V17_SIZE :
fdt_header_size_(fdt_version(fdt)); fdt_header_size_(fdt_version(fdt));
} }
int fdt_check_header(const void *fdt) int fdt_check_header(const void *fdt)
{ {
size_t hdrsize; size_t hdrsize;
/* The device tree must be at an 8-byte aligned address */ /* The device tree must be at an 8-byte aligned address */
if ((uintptr_t)fdt & 7) if ((uintptr_t)fdt & 7)
return -FDT_ERR_ALIGNMENT; return -FDT_ERR_ALIGNMENT;
if (fdt_magic(fdt) != FDT_MAGIC) if (fdt_magic(fdt) != FDT_MAGIC)
return -FDT_ERR_BADMAGIC; return -FDT_ERR_BADMAGIC;
if (!can_assume(LATEST)) { if (!can_assume(LATEST)) {
if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION) if ((fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
|| (fdt_last_comp_version(fdt) > || (fdt_last_comp_version(fdt) >
FDT_LAST_SUPPORTED_VERSION)) FDT_LAST_SUPPORTED_VERSION))
return -FDT_ERR_BADVERSION; return -FDT_ERR_BADVERSION;
if (fdt_version(fdt) < fdt_last_comp_version(fdt)) if (fdt_version(fdt) < fdt_last_comp_version(fdt))
return -FDT_ERR_BADVERSION; return -FDT_ERR_BADVERSION;
} }
hdrsize = fdt_header_size(fdt); hdrsize = fdt_header_size(fdt);
if (!can_assume(VALID_DTB)) { if (!can_assume(VALID_DTB)) {
if ((fdt_totalsize(fdt) < hdrsize) if ((fdt_totalsize(fdt) < hdrsize)
|| (fdt_totalsize(fdt) > INT_MAX)) || (fdt_totalsize(fdt) > INT_MAX))
return -FDT_ERR_TRUNCATED; return -FDT_ERR_TRUNCATED;
/* Bounds check memrsv block */ /* Bounds check memrsv block */
if (!check_off_(hdrsize, fdt_totalsize(fdt), if (!check_off_(hdrsize, fdt_totalsize(fdt),
fdt_off_mem_rsvmap(fdt))) fdt_off_mem_rsvmap(fdt)))
return -FDT_ERR_TRUNCATED; return -FDT_ERR_TRUNCATED;
/* Bounds check structure block */ /* Bounds check structure block */
if (!can_assume(LATEST) && fdt_version(fdt) < 17) { if (!can_assume(LATEST) && fdt_version(fdt) < 17) {
if (!check_off_(hdrsize, fdt_totalsize(fdt), if (!check_off_(hdrsize, fdt_totalsize(fdt),
fdt_off_dt_struct(fdt))) fdt_off_dt_struct(fdt)))
return -FDT_ERR_TRUNCATED; return -FDT_ERR_TRUNCATED;
} else { } else {
if (!check_block_(hdrsize, fdt_totalsize(fdt), if (!check_block_(hdrsize, fdt_totalsize(fdt),
fdt_off_dt_struct(fdt), fdt_off_dt_struct(fdt),
fdt_size_dt_struct(fdt))) fdt_size_dt_struct(fdt)))
return -FDT_ERR_TRUNCATED; return -FDT_ERR_TRUNCATED;
} }
/* Bounds check strings block */ /* Bounds check strings block */
if (!check_block_(hdrsize, fdt_totalsize(fdt), if (!check_block_(hdrsize, fdt_totalsize(fdt),
fdt_off_dt_strings(fdt), fdt_off_dt_strings(fdt),
fdt_size_dt_strings(fdt))) fdt_size_dt_strings(fdt)))
return -FDT_ERR_TRUNCATED; return -FDT_ERR_TRUNCATED;
} }
return 0; return 0;
} }
const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len)
{ {
unsigned int uoffset = offset; unsigned int uoffset = offset;
unsigned int absoffset = offset + fdt_off_dt_struct(fdt); unsigned int absoffset = offset + fdt_off_dt_struct(fdt);
if (offset < 0) if (offset < 0)
return NULL; return NULL;
if (!can_assume(VALID_INPUT)) if (!can_assume(VALID_INPUT))
if ((absoffset < uoffset) if ((absoffset < uoffset)
|| ((absoffset + len) < absoffset) || ((absoffset + len) < absoffset)
|| (absoffset + len) > fdt_totalsize(fdt)) || (absoffset + len) > fdt_totalsize(fdt))
return NULL; return NULL;
if (can_assume(LATEST) || fdt_version(fdt) >= 0x11) if (can_assume(LATEST) || fdt_version(fdt) >= 0x11)
if (((uoffset + len) < uoffset) if (((uoffset + len) < uoffset)
|| ((offset + len) > fdt_size_dt_struct(fdt))) || ((offset + len) > fdt_size_dt_struct(fdt)))
return NULL; return NULL;
return fdt_offset_ptr_(fdt, offset); return fdt_offset_ptr_(fdt, offset);
} }
uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset)
{ {
const fdt32_t *tagp, *lenp; const fdt32_t *tagp, *lenp;
uint32_t tag, len, sum; uint32_t tag, len, sum;
int offset = startoffset; int offset = startoffset;
const char *p; const char *p;
*nextoffset = -FDT_ERR_TRUNCATED; *nextoffset = -FDT_ERR_TRUNCATED;
tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE); tagp = fdt_offset_ptr(fdt, offset, FDT_TAGSIZE);
if (!can_assume(VALID_DTB) && !tagp) if (!can_assume(VALID_DTB) && !tagp)
return FDT_END; /* premature end */ return FDT_END; /* premature end */
tag = fdt32_to_cpu(*tagp); tag = fdt32_to_cpu(*tagp);
offset += FDT_TAGSIZE; offset += FDT_TAGSIZE;
*nextoffset = -FDT_ERR_BADSTRUCTURE; *nextoffset = -FDT_ERR_BADSTRUCTURE;
switch (tag) { switch (tag) {
case FDT_BEGIN_NODE: case FDT_BEGIN_NODE:
/* skip name */ /* skip name */
do { do {
p = fdt_offset_ptr(fdt, offset++, 1); p = fdt_offset_ptr(fdt, offset++, 1);
} while (p && (*p != '\0')); } while (p && (*p != '\0'));
if (!can_assume(VALID_DTB) && !p) if (!can_assume(VALID_DTB) && !p)
return FDT_END; /* premature end */ return FDT_END; /* premature end */
break; break;
case FDT_PROP: case FDT_PROP:
lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp)); lenp = fdt_offset_ptr(fdt, offset, sizeof(*lenp));
if (!can_assume(VALID_DTB) && !lenp) if (!can_assume(VALID_DTB) && !lenp)
return FDT_END; /* premature end */ return FDT_END; /* premature end */
len = fdt32_to_cpu(*lenp); len = fdt32_to_cpu(*lenp);
sum = len + offset; sum = len + offset;
if (!can_assume(VALID_DTB) && if (!can_assume(VALID_DTB) &&
(INT_MAX <= sum || sum < (uint32_t) offset)) (INT_MAX <= sum || sum < (uint32_t) offset))
return FDT_END; /* premature end */ return FDT_END; /* premature end */
/* skip-name offset, length and value */ /* skip-name offset, length and value */
offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len; offset += sizeof(struct fdt_property) - FDT_TAGSIZE + len;
if (!can_assume(LATEST) && if (!can_assume(LATEST) &&
fdt_version(fdt) < 0x10 && len >= 8 && fdt_version(fdt) < 0x10 && len >= 8 &&
((offset - len) % 8) != 0) ((offset - len) % 8) != 0)
offset += 4; offset += 4;
break; break;
case FDT_END: case FDT_END:
case FDT_END_NODE: case FDT_END_NODE:
case FDT_NOP: case FDT_NOP:
break; break;
default: default:
return FDT_END; return FDT_END;
} }
if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset)) if (!fdt_offset_ptr(fdt, startoffset, offset - startoffset))
return FDT_END; /* premature end */ return FDT_END; /* premature end */
*nextoffset = FDT_TAGALIGN(offset); *nextoffset = FDT_TAGALIGN(offset);
return tag; return tag;
} }
int fdt_check_node_offset_(const void *fdt, int offset) int fdt_check_node_offset_(const void *fdt, int offset)
{ {
if (!can_assume(VALID_INPUT) if (!can_assume(VALID_INPUT)
&& ((offset < 0) || (offset % FDT_TAGSIZE))) && ((offset < 0) || (offset % FDT_TAGSIZE)))
return -FDT_ERR_BADOFFSET; return -FDT_ERR_BADOFFSET;
if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE) if (fdt_next_tag(fdt, offset, &offset) != FDT_BEGIN_NODE)
return -FDT_ERR_BADOFFSET; return -FDT_ERR_BADOFFSET;
return offset; return offset;
} }
int fdt_check_prop_offset_(const void *fdt, int offset) int fdt_check_prop_offset_(const void *fdt, int offset)
{ {
if (!can_assume(VALID_INPUT) if (!can_assume(VALID_INPUT)
&& ((offset < 0) || (offset % FDT_TAGSIZE))) && ((offset < 0) || (offset % FDT_TAGSIZE)))
return -FDT_ERR_BADOFFSET; return -FDT_ERR_BADOFFSET;
if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP) if (fdt_next_tag(fdt, offset, &offset) != FDT_PROP)
return -FDT_ERR_BADOFFSET; return -FDT_ERR_BADOFFSET;
return offset; return offset;
} }
int fdt_next_node(const void *fdt, int offset, int *depth) int fdt_next_node(const void *fdt, int offset, int *depth)
{ {
int nextoffset = 0; int nextoffset = 0;
uint32_t tag; uint32_t tag;
if (offset >= 0) if (offset >= 0)
if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0) if ((nextoffset = fdt_check_node_offset_(fdt, offset)) < 0)
return nextoffset; return nextoffset;
do { do {
offset = nextoffset; offset = nextoffset;
tag = fdt_next_tag(fdt, offset, &nextoffset); tag = fdt_next_tag(fdt, offset, &nextoffset);
switch (tag) { switch (tag) {
case FDT_PROP: case FDT_PROP:
case FDT_NOP: case FDT_NOP:
break; break;
case FDT_BEGIN_NODE: case FDT_BEGIN_NODE:
if (depth) if (depth)
(*depth)++; (*depth)++;
break; break;
case FDT_END_NODE: case FDT_END_NODE:
if (depth && ((--(*depth)) < 0)) if (depth && ((--(*depth)) < 0))
return nextoffset; return nextoffset;
break; break;
case FDT_END: case FDT_END:
if ((nextoffset >= 0) if ((nextoffset >= 0)
|| ((nextoffset == -FDT_ERR_TRUNCATED) && !depth)) || ((nextoffset == -FDT_ERR_TRUNCATED) && !depth))
return -FDT_ERR_NOTFOUND; return -FDT_ERR_NOTFOUND;
else else
return nextoffset; return nextoffset;
} }
} while (tag != FDT_BEGIN_NODE); } while (tag != FDT_BEGIN_NODE);
return offset; return offset;
} }
int fdt_first_subnode(const void *fdt, int offset) int fdt_first_subnode(const void *fdt, int offset)
{ {
int depth = 0; int depth = 0;
offset = fdt_next_node(fdt, offset, &depth); offset = fdt_next_node(fdt, offset, &depth);
if (offset < 0 || depth != 1) if (offset < 0 || depth != 1)
return -FDT_ERR_NOTFOUND; return -FDT_ERR_NOTFOUND;
return offset; return offset;
} }
int fdt_next_subnode(const void *fdt, int offset) int fdt_next_subnode(const void *fdt, int offset)
{ {
int depth = 1; int depth = 1;
/* /*
* With respect to the parent, the depth of the next subnode will be * With respect to the parent, the depth of the next subnode will be
* the same as the last. * the same as the last.
*/ */
do { do {
offset = fdt_next_node(fdt, offset, &depth); offset = fdt_next_node(fdt, offset, &depth);
if (offset < 0 || depth < 1) if (offset < 0 || depth < 1)
return -FDT_ERR_NOTFOUND; return -FDT_ERR_NOTFOUND;
} while (depth > 1); } while (depth > 1);
return offset; return offset;
} }
const char *fdt_find_string_(const char *strtab, int tabsize, const char *s) const char *fdt_find_string_(const char *strtab, int tabsize, const char *s)
{ {
int len = strlen(s) + 1; int len = strlen(s) + 1;
const char *last = strtab + tabsize - len; const char *last = strtab + tabsize - len;
const char *p; const char *p;
for (p = strtab; p <= last; p++) for (p = strtab; p <= last; p++)
if (memcmp(p, s, len) == 0) if (memcmp(p, s, len) == 0)
return p; return p;
return NULL; return NULL;
} }
int fdt_move(const void *fdt, void *buf, int bufsize) int fdt_move(const void *fdt, void *buf, int bufsize)
{ {
if (!can_assume(VALID_INPUT) && bufsize < 0) if (!can_assume(VALID_INPUT) && bufsize < 0)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
FDT_RO_PROBE(fdt); FDT_RO_PROBE(fdt);
if (fdt_totalsize(fdt) > (unsigned int)bufsize) if (fdt_totalsize(fdt) > (unsigned int)bufsize)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
memmove(buf, fdt, fdt_totalsize(fdt)); memmove(buf, fdt, fdt_totalsize(fdt));
return 0; return 0;
} }

View File

@ -10,57 +10,57 @@
#ifndef __ASSEMBLY__ #ifndef __ASSEMBLY__
struct fdt_header { struct fdt_header {
fdt32_t magic; /* magic word FDT_MAGIC */ fdt32_t magic; /* magic word FDT_MAGIC */
fdt32_t totalsize; /* total size of DT block */ fdt32_t totalsize; /* total size of DT block */
fdt32_t off_dt_struct; /* offset to structure */ fdt32_t off_dt_struct; /* offset to structure */
fdt32_t off_dt_strings; /* offset to strings */ fdt32_t off_dt_strings; /* offset to strings */
fdt32_t off_mem_rsvmap; /* offset to memory reserve map */ fdt32_t off_mem_rsvmap; /* offset to memory reserve map */
fdt32_t version; /* format version */ fdt32_t version; /* format version */
fdt32_t last_comp_version; /* last compatible version */ fdt32_t last_comp_version; /* last compatible version */
/* version 2 fields below */ /* version 2 fields below */
fdt32_t boot_cpuid_phys; /* Which physical CPU id we're fdt32_t boot_cpuid_phys; /* Which physical CPU id we're
booting on */ booting on */
/* version 3 fields below */ /* version 3 fields below */
fdt32_t size_dt_strings; /* size of the strings block */ fdt32_t size_dt_strings; /* size of the strings block */
/* version 17 fields below */ /* version 17 fields below */
fdt32_t size_dt_struct; /* size of the structure block */ fdt32_t size_dt_struct; /* size of the structure block */
}; };
struct fdt_reserve_entry { struct fdt_reserve_entry {
fdt64_t address; fdt64_t address;
fdt64_t size; fdt64_t size;
}; };
struct fdt_node_header { struct fdt_node_header {
fdt32_t tag; fdt32_t tag;
char name[]; char name[];
}; };
struct fdt_property { struct fdt_property {
fdt32_t tag; fdt32_t tag;
fdt32_t len; fdt32_t len;
fdt32_t nameoff; fdt32_t nameoff;
char data[]; char data[];
}; };
#endif /* !__ASSEMBLY */ #endif /* !__ASSEMBLY */
#define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */ #define FDT_MAGIC 0xd00dfeed /* 4: version, 4: total size */
#define FDT_TAGSIZE sizeof(fdt32_t) #define FDT_TAGSIZE sizeof(fdt32_t)
#define FDT_BEGIN_NODE 0x1 /* Start node: full name */ #define FDT_BEGIN_NODE 0x1 /* Start node: full name */
#define FDT_END_NODE 0x2 /* End node */ #define FDT_END_NODE 0x2 /* End node */
#define FDT_PROP 0x3 /* Property: name off, #define FDT_PROP 0x3 /* Property: name off,
size, content */ size, content */
#define FDT_NOP 0x4 /* nop */ #define FDT_NOP 0x4 /* nop */
#define FDT_END 0x9 #define FDT_END 0x9
#define FDT_V1_SIZE (7*sizeof(fdt32_t)) #define FDT_V1_SIZE (7*sizeof(fdt32_t))
#define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t)) #define FDT_V2_SIZE (FDT_V1_SIZE + sizeof(fdt32_t))
#define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t)) #define FDT_V3_SIZE (FDT_V2_SIZE + sizeof(fdt32_t))
#define FDT_V16_SIZE FDT_V3_SIZE #define FDT_V16_SIZE FDT_V3_SIZE
#define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t)) #define FDT_V17_SIZE (FDT_V16_SIZE + sizeof(fdt32_t))
#endif /* FDT_H */ #endif /* FDT_H */

View File

@ -13,89 +13,89 @@
static int fdt_cells(const void *fdt, int nodeoffset, const char *name) static int fdt_cells(const void *fdt, int nodeoffset, const char *name)
{ {
const fdt32_t *c; const fdt32_t *c;
uint32_t val; uint32_t val;
int len; int len;
c = fdt_getprop(fdt, nodeoffset, name, &len); c = fdt_getprop(fdt, nodeoffset, name, &len);
if (!c) if (!c)
return len; return len;
if (len != sizeof(*c)) if (len != sizeof(*c))
return -FDT_ERR_BADNCELLS; return -FDT_ERR_BADNCELLS;
val = fdt32_to_cpu(*c); val = fdt32_to_cpu(*c);
if (val > FDT_MAX_NCELLS) if (val > FDT_MAX_NCELLS)
return -FDT_ERR_BADNCELLS; return -FDT_ERR_BADNCELLS;
return (int)val; return (int)val;
} }
int fdt_address_cells(const void *fdt, int nodeoffset) int fdt_address_cells(const void *fdt, int nodeoffset)
{ {
int val; int val;
val = fdt_cells(fdt, nodeoffset, "#address-cells"); val = fdt_cells(fdt, nodeoffset, "#address-cells");
if (val == 0) if (val == 0)
return -FDT_ERR_BADNCELLS; return -FDT_ERR_BADNCELLS;
if (val == -FDT_ERR_NOTFOUND) if (val == -FDT_ERR_NOTFOUND)
return 2; return 2;
return val; return val;
} }
int fdt_size_cells(const void *fdt, int nodeoffset) int fdt_size_cells(const void *fdt, int nodeoffset)
{ {
int val; int val;
val = fdt_cells(fdt, nodeoffset, "#size-cells"); val = fdt_cells(fdt, nodeoffset, "#size-cells");
if (val == -FDT_ERR_NOTFOUND) if (val == -FDT_ERR_NOTFOUND)
return 1; return 1;
return val; return val;
} }
/* This function assumes that [address|size]_cells is 1 or 2 */ /* This function assumes that [address|size]_cells is 1 or 2 */
int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset, int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
const char *name, uint64_t addr, uint64_t size) const char *name, uint64_t addr, uint64_t size)
{ {
int addr_cells, size_cells, ret; int addr_cells, size_cells, ret;
uint8_t data[sizeof(fdt64_t) * 2], *prop; uint8_t data[sizeof(fdt64_t) * 2], *prop;
ret = fdt_address_cells(fdt, parent); ret = fdt_address_cells(fdt, parent);
if (ret < 0) if (ret < 0)
return ret; return ret;
addr_cells = ret; addr_cells = ret;
ret = fdt_size_cells(fdt, parent); ret = fdt_size_cells(fdt, parent);
if (ret < 0) if (ret < 0)
return ret; return ret;
size_cells = ret; size_cells = ret;
/* check validity of address */ /* check validity of address */
prop = data; prop = data;
if (addr_cells == 1) { if (addr_cells == 1) {
if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size)) if ((addr > UINT32_MAX) || (((uint64_t) UINT32_MAX + 1 - addr) < size))
return -FDT_ERR_BADVALUE; return -FDT_ERR_BADVALUE;
fdt32_st(prop, (uint32_t)addr); fdt32_st(prop, (uint32_t)addr);
} else if (addr_cells == 2) { } else if (addr_cells == 2) {
fdt64_st(prop, addr); fdt64_st(prop, addr);
} else { } else {
return -FDT_ERR_BADNCELLS; return -FDT_ERR_BADNCELLS;
} }
/* check validity of size */ /* check validity of size */
prop += addr_cells * sizeof(fdt32_t); prop += addr_cells * sizeof(fdt32_t);
if (size_cells == 1) { if (size_cells == 1) {
if (size > UINT32_MAX) if (size > UINT32_MAX)
return -FDT_ERR_BADVALUE; return -FDT_ERR_BADVALUE;
fdt32_st(prop, (uint32_t)size); fdt32_st(prop, (uint32_t)size);
} else if (size_cells == 2) { } else if (size_cells == 2) {
fdt64_st(prop, size); fdt64_st(prop, size);
} else { } else {
return -FDT_ERR_BADNCELLS; return -FDT_ERR_BADNCELLS;
} }
return fdt_appendprop(fdt, nodeoffset, name, data, return fdt_appendprop(fdt, nodeoffset, name, data,
(addr_cells + size_cells) * sizeof(fdt32_t)); (addr_cells + size_cells) * sizeof(fdt32_t));
} }

View File

@ -12,27 +12,27 @@
int fdt_create_empty_tree(void *buf, int bufsize) int fdt_create_empty_tree(void *buf, int bufsize)
{ {
int err; int err;
err = fdt_create(buf, bufsize); err = fdt_create(buf, bufsize);
if (err) if (err)
return err; return err;
err = fdt_finish_reservemap(buf); err = fdt_finish_reservemap(buf);
if (err) if (err)
return err; return err;
err = fdt_begin_node(buf, ""); err = fdt_begin_node(buf, "");
if (err) if (err)
return err; return err;
err = fdt_end_node(buf); err = fdt_end_node(buf);
if (err) if (err)
return err; return err;
err = fdt_finish(buf); err = fdt_finish(buf);
if (err) if (err)
return err; return err;
return fdt_open_into(buf, buf, bufsize); return fdt_open_into(buf, buf, bufsize);
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -12,49 +12,49 @@
#include "libfdt_internal.h" #include "libfdt_internal.h"
struct fdt_errtabent { struct fdt_errtabent {
const char *str; const char *str;
}; };
#define FDT_ERRTABENT(val) \ #define FDT_ERRTABENT(val) \
[(val)] = { .str = #val, } [(val)] = { .str = #val, }
static struct fdt_errtabent fdt_errtable[] = { static struct fdt_errtabent fdt_errtable[] = {
FDT_ERRTABENT(FDT_ERR_NOTFOUND), FDT_ERRTABENT(FDT_ERR_NOTFOUND),
FDT_ERRTABENT(FDT_ERR_EXISTS), FDT_ERRTABENT(FDT_ERR_EXISTS),
FDT_ERRTABENT(FDT_ERR_NOSPACE), FDT_ERRTABENT(FDT_ERR_NOSPACE),
FDT_ERRTABENT(FDT_ERR_BADOFFSET), FDT_ERRTABENT(FDT_ERR_BADOFFSET),
FDT_ERRTABENT(FDT_ERR_BADPATH), FDT_ERRTABENT(FDT_ERR_BADPATH),
FDT_ERRTABENT(FDT_ERR_BADPHANDLE), FDT_ERRTABENT(FDT_ERR_BADPHANDLE),
FDT_ERRTABENT(FDT_ERR_BADSTATE), FDT_ERRTABENT(FDT_ERR_BADSTATE),
FDT_ERRTABENT(FDT_ERR_TRUNCATED), FDT_ERRTABENT(FDT_ERR_TRUNCATED),
FDT_ERRTABENT(FDT_ERR_BADMAGIC), FDT_ERRTABENT(FDT_ERR_BADMAGIC),
FDT_ERRTABENT(FDT_ERR_BADVERSION), FDT_ERRTABENT(FDT_ERR_BADVERSION),
FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE), FDT_ERRTABENT(FDT_ERR_BADSTRUCTURE),
FDT_ERRTABENT(FDT_ERR_BADLAYOUT), FDT_ERRTABENT(FDT_ERR_BADLAYOUT),
FDT_ERRTABENT(FDT_ERR_INTERNAL), FDT_ERRTABENT(FDT_ERR_INTERNAL),
FDT_ERRTABENT(FDT_ERR_BADNCELLS), FDT_ERRTABENT(FDT_ERR_BADNCELLS),
FDT_ERRTABENT(FDT_ERR_BADVALUE), FDT_ERRTABENT(FDT_ERR_BADVALUE),
FDT_ERRTABENT(FDT_ERR_BADOVERLAY), FDT_ERRTABENT(FDT_ERR_BADOVERLAY),
FDT_ERRTABENT(FDT_ERR_NOPHANDLES), FDT_ERRTABENT(FDT_ERR_NOPHANDLES),
FDT_ERRTABENT(FDT_ERR_BADFLAGS), FDT_ERRTABENT(FDT_ERR_BADFLAGS),
FDT_ERRTABENT(FDT_ERR_ALIGNMENT), FDT_ERRTABENT(FDT_ERR_ALIGNMENT),
}; };
#define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0]))) #define FDT_ERRTABSIZE ((int)(sizeof(fdt_errtable) / sizeof(fdt_errtable[0])))
const char *fdt_strerror(int errval) const char *fdt_strerror(int errval)
{ {
if (errval > 0) if (errval > 0)
return "<valid offset/length>"; return "<valid offset/length>";
else if (errval == 0) else if (errval == 0)
return "<no error>"; return "<no error>";
else if (-errval < FDT_ERRTABSIZE) { else if (-errval < FDT_ERRTABSIZE) {
const char *s = fdt_errtable[-errval].str; const char *s = fdt_errtable[-errval].str;
if (s) if (s)
return s; return s;
} }
return "<unknown error>"; return "<unknown error>";
} }

View File

@ -12,373 +12,373 @@
static int fdt_sw_probe_(void *fdt) static int fdt_sw_probe_(void *fdt)
{ {
if (!can_assume(VALID_INPUT)) { if (!can_assume(VALID_INPUT)) {
if (fdt_magic(fdt) == FDT_MAGIC) if (fdt_magic(fdt) == FDT_MAGIC)
return -FDT_ERR_BADSTATE; return -FDT_ERR_BADSTATE;
else if (fdt_magic(fdt) != FDT_SW_MAGIC) else if (fdt_magic(fdt) != FDT_SW_MAGIC)
return -FDT_ERR_BADMAGIC; return -FDT_ERR_BADMAGIC;
} }
return 0; return 0;
} }
#define FDT_SW_PROBE(fdt) \ #define FDT_SW_PROBE(fdt) \
{ \ { \
int err; \ int err; \
if ((err = fdt_sw_probe_(fdt)) != 0) \ if ((err = fdt_sw_probe_(fdt)) != 0) \
return err; \ return err; \
} }
/* 'memrsv' state: Initial state after fdt_create() /* 'memrsv' state: Initial state after fdt_create()
* *
* Allowed functions: * Allowed functions:
* fdt_add_reservemap_entry() * fdt_add_reservemap_entry()
* fdt_finish_reservemap() [moves to 'struct' state] * fdt_finish_reservemap() [moves to 'struct' state]
*/ */
static int fdt_sw_probe_memrsv_(void *fdt) static int fdt_sw_probe_memrsv_(void *fdt)
{ {
int err = fdt_sw_probe_(fdt); int err = fdt_sw_probe_(fdt);
if (err) if (err)
return err; return err;
if (!can_assume(VALID_INPUT) && fdt_off_dt_strings(fdt) != 0) if (!can_assume(VALID_INPUT) && fdt_off_dt_strings(fdt) != 0)
return -FDT_ERR_BADSTATE; return -FDT_ERR_BADSTATE;
return 0; return 0;
} }
#define FDT_SW_PROBE_MEMRSV(fdt) \ #define FDT_SW_PROBE_MEMRSV(fdt) \
{ \ { \
int err; \ int err; \
if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \ if ((err = fdt_sw_probe_memrsv_(fdt)) != 0) \
return err; \ return err; \
} }
/* 'struct' state: Enter this state after fdt_finish_reservemap() /* 'struct' state: Enter this state after fdt_finish_reservemap()
* *
* Allowed functions: * Allowed functions:
* fdt_begin_node() * fdt_begin_node()
* fdt_end_node() * fdt_end_node()
* fdt_property*() * fdt_property*()
* fdt_finish() [moves to 'complete' state] * fdt_finish() [moves to 'complete' state]
*/ */
static int fdt_sw_probe_struct_(void *fdt) static int fdt_sw_probe_struct_(void *fdt)
{ {
int err = fdt_sw_probe_(fdt); int err = fdt_sw_probe_(fdt);
if (err) if (err)
return err; return err;
if (!can_assume(VALID_INPUT) && if (!can_assume(VALID_INPUT) &&
fdt_off_dt_strings(fdt) != fdt_totalsize(fdt)) fdt_off_dt_strings(fdt) != fdt_totalsize(fdt))
return -FDT_ERR_BADSTATE; return -FDT_ERR_BADSTATE;
return 0; return 0;
} }
#define FDT_SW_PROBE_STRUCT(fdt) \ #define FDT_SW_PROBE_STRUCT(fdt) \
{ \ { \
int err; \ int err; \
if ((err = fdt_sw_probe_struct_(fdt)) != 0) \ if ((err = fdt_sw_probe_struct_(fdt)) != 0) \
return err; \ return err; \
} }
static inline uint32_t sw_flags(void *fdt) static inline uint32_t sw_flags(void *fdt)
{ {
/* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */ /* assert: (fdt_magic(fdt) == FDT_SW_MAGIC) */
return fdt_last_comp_version(fdt); return fdt_last_comp_version(fdt);
} }
/* 'complete' state: Enter this state after fdt_finish() /* 'complete' state: Enter this state after fdt_finish()
* *
* Allowed functions: none * Allowed functions: none
*/ */
static void *fdt_grab_space_(void *fdt, size_t len) static void *fdt_grab_space_(void *fdt, size_t len)
{ {
unsigned int offset = fdt_size_dt_struct(fdt); unsigned int offset = fdt_size_dt_struct(fdt);
unsigned int spaceleft; unsigned int spaceleft;
spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt) spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
- fdt_size_dt_strings(fdt); - fdt_size_dt_strings(fdt);
if ((offset + len < offset) || (offset + len > spaceleft)) if ((offset + len < offset) || (offset + len > spaceleft))
return NULL; return NULL;
fdt_set_size_dt_struct(fdt, offset + len); fdt_set_size_dt_struct(fdt, offset + len);
return fdt_offset_ptr_w_(fdt, offset); return fdt_offset_ptr_w_(fdt, offset);
} }
int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags) int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags)
{ {
const int hdrsize = FDT_ALIGN(sizeof(struct fdt_header), const int hdrsize = FDT_ALIGN(sizeof(struct fdt_header),
sizeof(struct fdt_reserve_entry)); sizeof(struct fdt_reserve_entry));
void *fdt = buf; void *fdt = buf;
if (bufsize < hdrsize) if (bufsize < hdrsize)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
if (flags & ~FDT_CREATE_FLAGS_ALL) if (flags & ~FDT_CREATE_FLAGS_ALL)
return -FDT_ERR_BADFLAGS; return -FDT_ERR_BADFLAGS;
memset(buf, 0, bufsize); memset(buf, 0, bufsize);
/* /*
* magic and last_comp_version keep intermediate state during the fdt * magic and last_comp_version keep intermediate state during the fdt
* creation process, which is replaced with the proper FDT format by * creation process, which is replaced with the proper FDT format by
* fdt_finish(). * fdt_finish().
* *
* flags should be accessed with sw_flags(). * flags should be accessed with sw_flags().
*/ */
fdt_set_magic(fdt, FDT_SW_MAGIC); fdt_set_magic(fdt, FDT_SW_MAGIC);
fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION); fdt_set_version(fdt, FDT_LAST_SUPPORTED_VERSION);
fdt_set_last_comp_version(fdt, flags); fdt_set_last_comp_version(fdt, flags);
fdt_set_totalsize(fdt, bufsize); fdt_set_totalsize(fdt, bufsize);
fdt_set_off_mem_rsvmap(fdt, hdrsize); fdt_set_off_mem_rsvmap(fdt, hdrsize);
fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt)); fdt_set_off_dt_struct(fdt, fdt_off_mem_rsvmap(fdt));
fdt_set_off_dt_strings(fdt, 0); fdt_set_off_dt_strings(fdt, 0);
return 0; return 0;
} }
int fdt_create(void *buf, int bufsize) int fdt_create(void *buf, int bufsize)
{ {
return fdt_create_with_flags(buf, bufsize, 0); return fdt_create_with_flags(buf, bufsize, 0);
} }
int fdt_resize(void *fdt, void *buf, int bufsize) int fdt_resize(void *fdt, void *buf, int bufsize)
{ {
size_t headsize, tailsize; size_t headsize, tailsize;
char *oldtail, *newtail; char *oldtail, *newtail;
FDT_SW_PROBE(fdt); FDT_SW_PROBE(fdt);
if (bufsize < 0) if (bufsize < 0)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); headsize = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
tailsize = fdt_size_dt_strings(fdt); tailsize = fdt_size_dt_strings(fdt);
if (!can_assume(VALID_DTB) && if (!can_assume(VALID_DTB) &&
headsize + tailsize > fdt_totalsize(fdt)) headsize + tailsize > fdt_totalsize(fdt))
return -FDT_ERR_INTERNAL; return -FDT_ERR_INTERNAL;
if ((headsize + tailsize) > (unsigned)bufsize) if ((headsize + tailsize) > (unsigned)bufsize)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize; oldtail = (char *)fdt + fdt_totalsize(fdt) - tailsize;
newtail = (char *)buf + bufsize - tailsize; newtail = (char *)buf + bufsize - tailsize;
/* Two cases to avoid clobbering data if the old and new /* Two cases to avoid clobbering data if the old and new
* buffers partially overlap */ * buffers partially overlap */
if (buf <= fdt) { if (buf <= fdt) {
memmove(buf, fdt, headsize); memmove(buf, fdt, headsize);
memmove(newtail, oldtail, tailsize); memmove(newtail, oldtail, tailsize);
} else { } else {
memmove(newtail, oldtail, tailsize); memmove(newtail, oldtail, tailsize);
memmove(buf, fdt, headsize); memmove(buf, fdt, headsize);
} }
fdt_set_totalsize(buf, bufsize); fdt_set_totalsize(buf, bufsize);
if (fdt_off_dt_strings(buf)) if (fdt_off_dt_strings(buf))
fdt_set_off_dt_strings(buf, bufsize); fdt_set_off_dt_strings(buf, bufsize);
return 0; return 0;
} }
int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size)
{ {
struct fdt_reserve_entry *re; struct fdt_reserve_entry *re;
int offset; int offset;
FDT_SW_PROBE_MEMRSV(fdt); FDT_SW_PROBE_MEMRSV(fdt);
offset = fdt_off_dt_struct(fdt); offset = fdt_off_dt_struct(fdt);
if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
re = (struct fdt_reserve_entry *)((char *)fdt + offset); re = (struct fdt_reserve_entry *)((char *)fdt + offset);
re->address = cpu_to_fdt64(addr); re->address = cpu_to_fdt64(addr);
re->size = cpu_to_fdt64(size); re->size = cpu_to_fdt64(size);
fdt_set_off_dt_struct(fdt, offset + sizeof(*re)); fdt_set_off_dt_struct(fdt, offset + sizeof(*re));
return 0; return 0;
} }
int fdt_finish_reservemap(void *fdt) int fdt_finish_reservemap(void *fdt)
{ {
int err = fdt_add_reservemap_entry(fdt, 0, 0); int err = fdt_add_reservemap_entry(fdt, 0, 0);
if (err) if (err)
return err; return err;
fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt)); fdt_set_off_dt_strings(fdt, fdt_totalsize(fdt));
return 0; return 0;
} }
int fdt_begin_node(void *fdt, const char *name) int fdt_begin_node(void *fdt, const char *name)
{ {
struct fdt_node_header *nh; struct fdt_node_header *nh;
int namelen; int namelen;
FDT_SW_PROBE_STRUCT(fdt); FDT_SW_PROBE_STRUCT(fdt);
namelen = strlen(name) + 1; namelen = strlen(name) + 1;
nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen)); nh = fdt_grab_space_(fdt, sizeof(*nh) + FDT_TAGALIGN(namelen));
if (! nh) if (! nh)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE); nh->tag = cpu_to_fdt32(FDT_BEGIN_NODE);
memcpy(nh->name, name, namelen); memcpy(nh->name, name, namelen);
return 0; return 0;
} }
int fdt_end_node(void *fdt) int fdt_end_node(void *fdt)
{ {
fdt32_t *en; fdt32_t *en;
FDT_SW_PROBE_STRUCT(fdt); FDT_SW_PROBE_STRUCT(fdt);
en = fdt_grab_space_(fdt, FDT_TAGSIZE); en = fdt_grab_space_(fdt, FDT_TAGSIZE);
if (! en) if (! en)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
*en = cpu_to_fdt32(FDT_END_NODE); *en = cpu_to_fdt32(FDT_END_NODE);
return 0; return 0;
} }
static int fdt_add_string_(void *fdt, const char *s) static int fdt_add_string_(void *fdt, const char *s)
{ {
char *strtab = (char *)fdt + fdt_totalsize(fdt); char *strtab = (char *)fdt + fdt_totalsize(fdt);
unsigned int strtabsize = fdt_size_dt_strings(fdt); unsigned int strtabsize = fdt_size_dt_strings(fdt);
unsigned int len = strlen(s) + 1; unsigned int len = strlen(s) + 1;
unsigned int struct_top, offset; unsigned int struct_top, offset;
offset = strtabsize + len; offset = strtabsize + len;
struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); struct_top = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
if (fdt_totalsize(fdt) - offset < struct_top) if (fdt_totalsize(fdt) - offset < struct_top)
return 0; /* no more room :( */ return 0; /* no more room :( */
memcpy(strtab - offset, s, len); memcpy(strtab - offset, s, len);
fdt_set_size_dt_strings(fdt, strtabsize + len); fdt_set_size_dt_strings(fdt, strtabsize + len);
return -offset; return -offset;
} }
/* Must only be used to roll back in case of error */ /* Must only be used to roll back in case of error */
static void fdt_del_last_string_(void *fdt, const char *s) static void fdt_del_last_string_(void *fdt, const char *s)
{ {
int strtabsize = fdt_size_dt_strings(fdt); int strtabsize = fdt_size_dt_strings(fdt);
int len = strlen(s) + 1; int len = strlen(s) + 1;
fdt_set_size_dt_strings(fdt, strtabsize - len); fdt_set_size_dt_strings(fdt, strtabsize - len);
} }
static int fdt_find_add_string_(void *fdt, const char *s, int *allocated) static int fdt_find_add_string_(void *fdt, const char *s, int *allocated)
{ {
char *strtab = (char *)fdt + fdt_totalsize(fdt); char *strtab = (char *)fdt + fdt_totalsize(fdt);
int strtabsize = fdt_size_dt_strings(fdt); int strtabsize = fdt_size_dt_strings(fdt);
const char *p; const char *p;
*allocated = 0; *allocated = 0;
p = fdt_find_string_(strtab - strtabsize, strtabsize, s); p = fdt_find_string_(strtab - strtabsize, strtabsize, s);
if (p) if (p)
return p - strtab; return p - strtab;
*allocated = 1; *allocated = 1;
return fdt_add_string_(fdt, s); return fdt_add_string_(fdt, s);
} }
int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp) int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp)
{ {
struct fdt_property *prop; struct fdt_property *prop;
int nameoff; int nameoff;
int allocated; int allocated;
FDT_SW_PROBE_STRUCT(fdt); FDT_SW_PROBE_STRUCT(fdt);
/* String de-duplication can be slow, _NO_NAME_DEDUP skips it */ /* String de-duplication can be slow, _NO_NAME_DEDUP skips it */
if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) { if (sw_flags(fdt) & FDT_CREATE_FLAG_NO_NAME_DEDUP) {
allocated = 1; allocated = 1;
nameoff = fdt_add_string_(fdt, name); nameoff = fdt_add_string_(fdt, name);
} else { } else {
nameoff = fdt_find_add_string_(fdt, name, &allocated); nameoff = fdt_find_add_string_(fdt, name, &allocated);
} }
if (nameoff == 0) if (nameoff == 0)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len)); prop = fdt_grab_space_(fdt, sizeof(*prop) + FDT_TAGALIGN(len));
if (! prop) { if (! prop) {
if (allocated) if (allocated)
fdt_del_last_string_(fdt, name); fdt_del_last_string_(fdt, name);
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
} }
prop->tag = cpu_to_fdt32(FDT_PROP); prop->tag = cpu_to_fdt32(FDT_PROP);
prop->nameoff = cpu_to_fdt32(nameoff); prop->nameoff = cpu_to_fdt32(nameoff);
prop->len = cpu_to_fdt32(len); prop->len = cpu_to_fdt32(len);
*valp = prop->data; *valp = prop->data;
return 0; return 0;
} }
int fdt_property(void *fdt, const char *name, const void *val, int len) int fdt_property(void *fdt, const char *name, const void *val, int len)
{ {
void *ptr; void *ptr;
int ret; int ret;
ret = fdt_property_placeholder(fdt, name, len, &ptr); ret = fdt_property_placeholder(fdt, name, len, &ptr);
if (ret) if (ret)
return ret; return ret;
memcpy(ptr, val, len); memcpy(ptr, val, len);
return 0; return 0;
} }
int fdt_finish(void *fdt) int fdt_finish(void *fdt)
{ {
char *p = (char *)fdt; char *p = (char *)fdt;
fdt32_t *end; fdt32_t *end;
int oldstroffset, newstroffset; int oldstroffset, newstroffset;
uint32_t tag; uint32_t tag;
int offset, nextoffset; int offset, nextoffset;
FDT_SW_PROBE_STRUCT(fdt); FDT_SW_PROBE_STRUCT(fdt);
/* Add terminator */ /* Add terminator */
end = fdt_grab_space_(fdt, sizeof(*end)); end = fdt_grab_space_(fdt, sizeof(*end));
if (! end) if (! end)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
*end = cpu_to_fdt32(FDT_END); *end = cpu_to_fdt32(FDT_END);
/* Relocate the string table */ /* Relocate the string table */
oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt); oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt); newstroffset = fdt_off_dt_struct(fdt) + fdt_size_dt_struct(fdt);
memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt)); memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
fdt_set_off_dt_strings(fdt, newstroffset); fdt_set_off_dt_strings(fdt, newstroffset);
/* Walk the structure, correcting string offsets */ /* Walk the structure, correcting string offsets */
offset = 0; offset = 0;
while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { while ((tag = fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) {
if (tag == FDT_PROP) { if (tag == FDT_PROP) {
struct fdt_property *prop = struct fdt_property *prop =
fdt_offset_ptr_w_(fdt, offset); fdt_offset_ptr_w_(fdt, offset);
int nameoff; int nameoff;
nameoff = fdt32_to_cpu(prop->nameoff); nameoff = fdt32_to_cpu(prop->nameoff);
nameoff += fdt_size_dt_strings(fdt); nameoff += fdt_size_dt_strings(fdt);
prop->nameoff = cpu_to_fdt32(nameoff); prop->nameoff = cpu_to_fdt32(nameoff);
} }
offset = nextoffset; offset = nextoffset;
} }
if (nextoffset < 0) if (nextoffset < 0)
return nextoffset; return nextoffset;
/* Finally, adjust the header */ /* Finally, adjust the header */
fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt)); fdt_set_totalsize(fdt, newstroffset + fdt_size_dt_strings(fdt));
/* And fix up fields that were keeping intermediate state. */ /* And fix up fields that were keeping intermediate state. */
fdt_set_last_comp_version(fdt, FDT_LAST_COMPATIBLE_VERSION); fdt_set_last_comp_version(fdt, FDT_LAST_COMPATIBLE_VERSION);
fdt_set_magic(fdt, FDT_MAGIC); fdt_set_magic(fdt, FDT_MAGIC);
return 0; return 0;
} }

View File

@ -11,84 +11,84 @@
#include "libfdt_internal.h" #include "libfdt_internal.h"
int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset, int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
const char *name, int namelen, const char *name, int namelen,
uint32_t idx, const void *val, uint32_t idx, const void *val,
int len) int len)
{ {
void *propval; void *propval;
int proplen; int proplen;
propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen, propval = fdt_getprop_namelen_w(fdt, nodeoffset, name, namelen,
&proplen); &proplen);
if (!propval) if (!propval)
return proplen; return proplen;
if ((unsigned)proplen < (len + idx)) if ((unsigned)proplen < (len + idx))
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
memcpy((char *)propval + idx, val, len); memcpy((char *)propval + idx, val, len);
return 0; return 0;
} }
int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
const void *val, int len) const void *val, int len)
{ {
const void *propval; const void *propval;
int proplen; int proplen;
propval = fdt_getprop(fdt, nodeoffset, name, &proplen); propval = fdt_getprop(fdt, nodeoffset, name, &proplen);
if (!propval) if (!propval)
return proplen; return proplen;
if (proplen != len) if (proplen != len)
return -FDT_ERR_NOSPACE; return -FDT_ERR_NOSPACE;
return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name, return fdt_setprop_inplace_namelen_partial(fdt, nodeoffset, name,
strlen(name), 0, strlen(name), 0,
val, len); val, len);
} }
static void fdt_nop_region_(void *start, int len) static void fdt_nop_region_(void *start, int len)
{ {
fdt32_t *p; fdt32_t *p;
for (p = start; (char *)p < ((char *)start + len); p++) for (p = start; (char *)p < ((char *)start + len); p++)
*p = cpu_to_fdt32(FDT_NOP); *p = cpu_to_fdt32(FDT_NOP);
} }
int fdt_nop_property(void *fdt, int nodeoffset, const char *name) int fdt_nop_property(void *fdt, int nodeoffset, const char *name)
{ {
struct fdt_property *prop; struct fdt_property *prop;
int len; int len;
prop = fdt_get_property_w(fdt, nodeoffset, name, &len); prop = fdt_get_property_w(fdt, nodeoffset, name, &len);
if (!prop) if (!prop)
return len; return len;
fdt_nop_region_(prop, len + sizeof(*prop)); fdt_nop_region_(prop, len + sizeof(*prop));
return 0; return 0;
} }
int fdt_node_end_offset_(void *fdt, int offset) int fdt_node_end_offset_(void *fdt, int offset)
{ {
int depth = 0; int depth = 0;
while ((offset >= 0) && (depth >= 0)) while ((offset >= 0) && (depth >= 0))
offset = fdt_next_node(fdt, offset, &depth); offset = fdt_next_node(fdt, offset, &depth);
return offset; return offset;
} }
int fdt_nop_node(void *fdt, int nodeoffset) int fdt_nop_node(void *fdt, int nodeoffset)
{ {
int endoffset; int endoffset;
endoffset = fdt_node_end_offset_(fdt, nodeoffset); endoffset = fdt_node_end_offset_(fdt, nodeoffset);
if (endoffset < 0) if (endoffset < 0)
return endoffset; return endoffset;
fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0), fdt_nop_region_(fdt_offset_ptr_w(fdt, nodeoffset, 0),
endoffset - nodeoffset); endoffset - nodeoffset);
return 0; return 0;
} }

File diff suppressed because it is too large Load Diff

View File

@ -30,40 +30,40 @@ typedef uint16_t FDT_BITWISE fdt16_t;
typedef uint32_t FDT_BITWISE fdt32_t; typedef uint32_t FDT_BITWISE fdt32_t;
typedef uint64_t FDT_BITWISE fdt64_t; typedef uint64_t FDT_BITWISE fdt64_t;
#define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n]) #define EXTRACT_BYTE(x, n) ((unsigned long long)((uint8_t *)&x)[n])
#define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1)) #define CPU_TO_FDT16(x) ((EXTRACT_BYTE(x, 0) << 8) | EXTRACT_BYTE(x, 1))
#define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \ #define CPU_TO_FDT32(x) ((EXTRACT_BYTE(x, 0) << 24) | (EXTRACT_BYTE(x, 1) << 16) | \
(EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3)) (EXTRACT_BYTE(x, 2) << 8) | EXTRACT_BYTE(x, 3))
#define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \ #define CPU_TO_FDT64(x) ((EXTRACT_BYTE(x, 0) << 56) | (EXTRACT_BYTE(x, 1) << 48) | \
(EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \ (EXTRACT_BYTE(x, 2) << 40) | (EXTRACT_BYTE(x, 3) << 32) | \
(EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \ (EXTRACT_BYTE(x, 4) << 24) | (EXTRACT_BYTE(x, 5) << 16) | \
(EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7)) (EXTRACT_BYTE(x, 6) << 8) | EXTRACT_BYTE(x, 7))
static inline uint16_t fdt16_to_cpu(fdt16_t x) static inline uint16_t fdt16_to_cpu(fdt16_t x)
{ {
return (FDT_FORCE uint16_t)CPU_TO_FDT16(x); return (FDT_FORCE uint16_t)CPU_TO_FDT16(x);
} }
static inline fdt16_t cpu_to_fdt16(uint16_t x) static inline fdt16_t cpu_to_fdt16(uint16_t x)
{ {
return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x); return (FDT_FORCE fdt16_t)CPU_TO_FDT16(x);
} }
static inline uint32_t fdt32_to_cpu(fdt32_t x) static inline uint32_t fdt32_to_cpu(fdt32_t x)
{ {
return (FDT_FORCE uint32_t)CPU_TO_FDT32(x); return (FDT_FORCE uint32_t)CPU_TO_FDT32(x);
} }
static inline fdt32_t cpu_to_fdt32(uint32_t x) static inline fdt32_t cpu_to_fdt32(uint32_t x)
{ {
return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x); return (FDT_FORCE fdt32_t)CPU_TO_FDT32(x);
} }
static inline uint64_t fdt64_to_cpu(fdt64_t x) static inline uint64_t fdt64_to_cpu(fdt64_t x)
{ {
return (FDT_FORCE uint64_t)CPU_TO_FDT64(x); return (FDT_FORCE uint64_t)CPU_TO_FDT64(x);
} }
static inline fdt64_t cpu_to_fdt64(uint64_t x) static inline fdt64_t cpu_to_fdt64(uint64_t x)
{ {
return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x); return (FDT_FORCE fdt64_t)CPU_TO_FDT64(x);
} }
#undef CPU_TO_FDT64 #undef CPU_TO_FDT64
#undef CPU_TO_FDT32 #undef CPU_TO_FDT32

View File

@ -7,16 +7,16 @@
*/ */
#include <fdt.h> #include <fdt.h>
#define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1)) #define FDT_ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
#define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE)) #define FDT_TAGALIGN(x) (FDT_ALIGN((x), FDT_TAGSIZE))
int32_t fdt_ro_probe_(const void *fdt); int32_t fdt_ro_probe_(const void *fdt);
#define FDT_RO_PROBE(fdt) \ #define FDT_RO_PROBE(fdt) \
{ \ { \
int32_t totalsize_; \ int32_t totalsize_; \
if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \ if ((totalsize_ = fdt_ro_probe_(fdt)) < 0) \
return totalsize_; \ return totalsize_; \
} }
int fdt_check_node_offset_(const void *fdt, int offset); int fdt_check_node_offset_(const void *fdt, int offset);
int fdt_check_prop_offset_(const void *fdt, int offset); int fdt_check_prop_offset_(const void *fdt, int offset);
@ -25,25 +25,25 @@ int fdt_node_end_offset_(void *fdt, int nodeoffset);
static inline const void *fdt_offset_ptr_(const void *fdt, int offset) static inline const void *fdt_offset_ptr_(const void *fdt, int offset)
{ {
return (const char *)fdt + fdt_off_dt_struct(fdt) + offset; return (const char *)fdt + fdt_off_dt_struct(fdt) + offset;
} }
static inline void *fdt_offset_ptr_w_(void *fdt, int offset) static inline void *fdt_offset_ptr_w_(void *fdt, int offset)
{ {
return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset); return (void *)(uintptr_t)fdt_offset_ptr_(fdt, offset);
} }
static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n) static inline const struct fdt_reserve_entry *fdt_mem_rsv_(const void *fdt, int n)
{ {
const struct fdt_reserve_entry *rsv_table = const struct fdt_reserve_entry *rsv_table =
(const struct fdt_reserve_entry *) (const struct fdt_reserve_entry *)
((const char *)fdt + fdt_off_mem_rsvmap(fdt)); ((const char *)fdt + fdt_off_mem_rsvmap(fdt));
return rsv_table + n; return rsv_table + n;
} }
static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n) static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
{ {
return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n); return (void *)(uintptr_t)fdt_mem_rsv_(fdt, n);
} }
/* /*
@ -57,15 +57,15 @@ static inline struct fdt_reserve_entry *fdt_mem_rsv_w_(void *fdt, int n)
*/ */
static inline uint32_t fdt32_ld_(const fdt32_t *p) static inline uint32_t fdt32_ld_(const fdt32_t *p)
{ {
return fdt32_to_cpu(*p); return fdt32_to_cpu(*p);
} }
static inline uint64_t fdt64_ld_(const fdt64_t *p) static inline uint64_t fdt64_ld_(const fdt64_t *p)
{ {
return fdt64_to_cpu(*p); return fdt64_to_cpu(*p);
} }
#define FDT_SW_MAGIC (~FDT_MAGIC) #define FDT_SW_MAGIC (~FDT_MAGIC)
/**********************************************************************/ /**********************************************************************/
/* Checking controls */ /* Checking controls */
@ -87,92 +87,92 @@ static inline uint64_t fdt64_ld_(const fdt64_t *p)
* ASSUME_SANE. * ASSUME_SANE.
*/ */
enum { enum {
/* /*
* This does essentially no checks. Only the latest device-tree * This does essentially no checks. Only the latest device-tree
* version is correctly handled. Inconsistencies or errors in the device * version is correctly handled. Inconsistencies or errors in the device
* tree may cause undefined behaviour or crashes. Invalid parameters * tree may cause undefined behaviour or crashes. Invalid parameters
* passed to libfdt may do the same. * passed to libfdt may do the same.
* *
* If an error occurs when modifying the tree it may leave the tree in * If an error occurs when modifying the tree it may leave the tree in
* an intermediate (but valid) state. As an example, adding a property * an intermediate (but valid) state. As an example, adding a property
* where there is insufficient space may result in the property name * where there is insufficient space may result in the property name
* being added to the string table even though the property itself is * being added to the string table even though the property itself is
* not added to the struct section. * not added to the struct section.
* *
* Only use this if you have a fully validated device tree with * Only use this if you have a fully validated device tree with
* the latest supported version and wish to minimise code size. * the latest supported version and wish to minimise code size.
*/ */
ASSUME_PERFECT = 0xff, ASSUME_PERFECT = 0xff,
/* /*
* This assumes that the device tree is sane. i.e. header metadata * This assumes that the device tree is sane. i.e. header metadata
* and basic hierarchy are correct. * and basic hierarchy are correct.
* *
* With this assumption enabled, normal device trees produced by libfdt * With this assumption enabled, normal device trees produced by libfdt
* and the compiler should be handled safely. Malicious device trees and * and the compiler should be handled safely. Malicious device trees and
* complete garbage may cause libfdt to behave badly or crash. Truncated * complete garbage may cause libfdt to behave badly or crash. Truncated
* device trees (e.g. those only partially loaded) can also cause * device trees (e.g. those only partially loaded) can also cause
* problems. * problems.
* *
* Note: Only checks that relate exclusively to the device tree itself * Note: Only checks that relate exclusively to the device tree itself
* (not the parameters passed to libfdt) are disabled by this * (not the parameters passed to libfdt) are disabled by this
* assumption. This includes checking headers, tags and the like. * assumption. This includes checking headers, tags and the like.
*/ */
ASSUME_VALID_DTB = 1 << 0, ASSUME_VALID_DTB = 1 << 0,
/* /*
* This builds on ASSUME_VALID_DTB and further assumes that libfdt * This builds on ASSUME_VALID_DTB and further assumes that libfdt
* functions are called with valid parameters, i.e. not trigger * functions are called with valid parameters, i.e. not trigger
* FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any * FDT_ERR_BADOFFSET or offsets that are out of bounds. It disables any
* extensive checking of parameters and the device tree, making various * extensive checking of parameters and the device tree, making various
* assumptions about correctness. * assumptions about correctness.
* *
* It doesn't make sense to enable this assumption unless * It doesn't make sense to enable this assumption unless
* ASSUME_VALID_DTB is also enabled. * ASSUME_VALID_DTB is also enabled.
*/ */
ASSUME_VALID_INPUT = 1 << 1, ASSUME_VALID_INPUT = 1 << 1,
/* /*
* This disables checks for device-tree version and removes all code * This disables checks for device-tree version and removes all code
* which handles older versions. * which handles older versions.
* *
* Only enable this if you know you have a device tree with the latest * Only enable this if you know you have a device tree with the latest
* version. * version.
*/ */
ASSUME_LATEST = 1 << 2, ASSUME_LATEST = 1 << 2,
/* /*
* This assumes that it is OK for a failed addition to the device tree, * This assumes that it is OK for a failed addition to the device tree,
* due to lack of space or some other problem, to skip any rollback * due to lack of space or some other problem, to skip any rollback
* steps (such as dropping the property name from the string table). * steps (such as dropping the property name from the string table).
* This is safe to enable in most circumstances, even though it may * This is safe to enable in most circumstances, even though it may
* leave the tree in a sub-optimal state. * leave the tree in a sub-optimal state.
*/ */
ASSUME_NO_ROLLBACK = 1 << 3, ASSUME_NO_ROLLBACK = 1 << 3,
/* /*
* This assumes that the device tree components appear in a 'convenient' * This assumes that the device tree components appear in a 'convenient'
* order, i.e. the memory reservation block first, then the structure * order, i.e. the memory reservation block first, then the structure
* block and finally the string block. * block and finally the string block.
* *
* This order is not specified by the device-tree specification, * This order is not specified by the device-tree specification,
* but is expected by libfdt. The device-tree compiler always created * but is expected by libfdt. The device-tree compiler always created
* device trees with this order. * device trees with this order.
* *
* This assumption disables a check in fdt_open_into() and removes the * This assumption disables a check in fdt_open_into() and removes the
* ability to fix the problem there. This is safe if you know that the * ability to fix the problem there. This is safe if you know that the
* device tree is correctly ordered. See fdt_blocks_misordered_(). * device tree is correctly ordered. See fdt_blocks_misordered_().
*/ */
ASSUME_LIBFDT_ORDER = 1 << 4, ASSUME_LIBFDT_ORDER = 1 << 4,
/* /*
* This assumes that libfdt itself does not have any internal bugs. It * This assumes that libfdt itself does not have any internal bugs. It
* drops certain checks that should never be needed unless libfdt has an * drops certain checks that should never be needed unless libfdt has an
* undiscovered bug. * undiscovered bug.
* *
* This can generally be considered safe to enable. * This can generally be considered safe to enable.
*/ */
ASSUME_LIBFDT_FLAWLESS = 1 << 5, ASSUME_LIBFDT_FLAWLESS = 1 << 5,
}; };
/** /**
@ -183,10 +183,10 @@ enum {
*/ */
static inline bool can_assume_(int mask) static inline bool can_assume_(int mask)
{ {
return FDT_ASSUME_MASK & mask; return FDT_ASSUME_MASK & mask;
} }
/** helper macros for checking assumptions */ /** helper macros for checking assumptions */
#define can_assume(_assume) can_assume_(ASSUME_ ## _assume) #define can_assume(_assume) can_assume_(ASSUME_ ## _assume)
#endif /* LIBFDT_INTERNAL_H */ #endif /* LIBFDT_INTERNAL_H */