At present libfdt includes checks on the version of the device tree. These checks are unnecessary if a known device tree version is used. Allow these checks to be disabled to reduce code size. Signed-off-by: Simon Glass <sjg@xxxxxxxxxxxx> --- libfdt/fdt.c | 12 +++++++----- libfdt/fdt_ro.c | 14 +++++++------- libfdt/fdt_rw.c | 4 ++-- libfdt/libfdt.h | 2 +- 4 files changed, 17 insertions(+), 15 deletions(-) diff --git a/libfdt/fdt.c b/libfdt/fdt.c index fea49cb..6ca5d12 100644 --- a/libfdt/fdt.c +++ b/libfdt/fdt.c @@ -122,11 +122,12 @@ const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int len) || ((absoffset + len) < absoffset) || (absoffset + len) > fdt_totalsize(fdt)) return NULL; + + if (_check1() && fdt_version(fdt) >= 0x11) + if (((offset + len) < offset) + || ((offset + len) > fdt_size_dt_struct(fdt))) + return NULL; } - if (fdt_version(fdt) >= 0x11) - if (((offset + len) < offset) - || ((offset + len) > fdt_size_dt_struct(fdt))) - return NULL; return fdt_offset_ptr_(fdt, offset); } @@ -163,7 +164,8 @@ uint32_t fdt_next_tag(const void *fdt, int startoffset, int *nextoffset) /* skip-name offset, length and value */ offset += sizeof(struct fdt_property) - FDT_TAGSIZE + fdt32_to_cpu(*lenp); - if (fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 && + if (_check1() && + fdt_version(fdt) < 0x10 && fdt32_to_cpu(*lenp) >= 8 && ((offset - fdt32_to_cpu(*lenp)) % 8) != 0) offset += 4; break; diff --git a/libfdt/fdt_ro.c b/libfdt/fdt_ro.c index bc1b4e0..82d02d3 100644 --- a/libfdt/fdt_ro.c +++ b/libfdt/fdt_ro.c @@ -57,7 +57,7 @@ const char *fdt_get_string(const void *fdt, int stroffset, int *lenp) if (fdt_magic(fdt) == FDT_MAGIC) { if (stroffset < 0) goto fail; - if (fdt_version(fdt) >= 17) { + if (_check1() && fdt_version(fdt) >= 17) { if (stroffset >= fdt_size_dt_strings(fdt)) goto fail; if ((fdt_size_dt_strings(fdt) - stroffset) < len) @@ -377,7 +377,7 @@ const struct fdt_property *fdt_get_property_by_offset(const void *fdt, /* Prior to version 16, properties may need realignment * and this API does not work. fdt_getprop_*() will, however. */ - if (fdt_version(fdt) < 0x10) { + if (_check1() && fdt_version(fdt) < 0x10) { if (lenp) *lenp = -FDT_ERR_BADVERSION; return NULL; @@ -424,7 +424,7 @@ const struct fdt_property *fdt_get_property_namelen(const void *fdt, { /* Prior to version 16, properties may need realignment * and this API does not work. fdt_getprop_*() will, however. */ - if (fdt_version(fdt) < 0x10) { + if (_check1() && fdt_version(fdt) < 0x10) { if (lenp) *lenp = -FDT_ERR_BADVERSION; return NULL; @@ -455,8 +455,8 @@ const void *fdt_getprop_namelen(const void *fdt, int nodeoffset, return NULL; /* Handle realignment */ - if (fdt_version(fdt) < 0x10 && (poffset + sizeof(*prop)) % 8 && - fdt32_ld(&prop->len) >= 8) + if (_check1() && fdt_version(fdt) < 0x10 && + (poffset + sizeof(*prop)) % 8 && fdt32_ld(&prop->len) >= 8) return prop->data + 4; return prop->data; } @@ -488,8 +488,8 @@ const void *fdt_getprop_by_offset(const void *fdt, int offset, } /* Handle realignment */ - if (fdt_version(fdt) < 0x10 && (offset + sizeof(*prop)) % 8 && - fdt32_ld(&prop->len) >= 8) + if (_check1() && fdt_version(fdt) < 0x10 && + (offset + sizeof(*prop)) % 8 && fdt32_ld(&prop->len) >= 8) return prop->data + 4; return prop->data; } diff --git a/libfdt/fdt_rw.c b/libfdt/fdt_rw.c index 4ff3aab..6d31d4e 100644 --- a/libfdt/fdt_rw.c +++ b/libfdt/fdt_rw.c @@ -26,7 +26,7 @@ static int fdt_blocks_misordered_(const void *fdt, static int fdt_rw_probe_(void *fdt) { - if (!_check2()) + if (!_check1()) return 0; FDT_RO_PROBE(fdt); @@ -417,7 +417,7 @@ int fdt_open_into(const void *fdt, void *buf, int bufsize) mem_rsv_size = (fdt_num_mem_rsv(fdt)+1) * sizeof(struct fdt_reserve_entry); - if (fdt_version(fdt) >= 17) { + if (_check1() && fdt_version(fdt) >= 17) { struct_size = fdt_size_dt_struct(fdt); } else { struct_size = 0; diff --git a/libfdt/libfdt.h b/libfdt/libfdt.h index 3277468..6025bbb 100644 --- a/libfdt/libfdt.h +++ b/libfdt/libfdt.h @@ -310,7 +310,7 @@ fdt_set_hdr_(size_dt_struct); size_t fdt_header_size_(uint32_t version); static inline size_t fdt_header_size(const void *fdt) { - return fdt_header_size_(fdt_version(fdt)); + return _check1() ? fdt_header_size_(fdt_version(fdt)) : FDT_V17_SIZE; } /** -- 2.22.0.657.g960e92d24f-goog