From a6c76f923dcc42102fac58375eaca28057811c20 Mon Sep 17 00:00:00 2001 From: David Gibson Date: Wed, 13 Jun 2007 14:18:10 +1000 Subject: [PATCH] libfdt: More thorough use of constification As a read-only functions, which take a const pointer to the fdt, treat fdt_get_property() and fdt_getprop() as returning const pointers to within the blob. fdt_get_property_w() and fdt_getprop_w() versions are supplied which take a non-const fdt pointer and return a non-const pointer for the benefit of callers wishing to alter the device tree contents. Likewise the lower-level fdt_offset_ptr() and _fdt_offset_ptr() functions are changed to return const pointers, with *_w() versions supplied. Signed-off-by: David Gibson --- fdt.c | 4 ++-- fdt_ro.c | 10 +++++----- fdt_rw.c | 10 +++++----- fdt_sw.c | 8 ++++---- fdt_wip.c | 6 +++--- libfdt.h | 33 +++++++++++++++++++++++++++------ libfdt_internal.h | 9 +++++++-- tests/del_property.c | 4 ++-- tests/nop_property.c | 4 ++-- tests/notfound.c | 4 ++-- tests/setprop.c | 4 ++-- tests/setprop_inplace.c | 5 +++-- tests/tests.h | 4 ++-- tests/testutils.c | 6 +++--- tests/truncated_property.c | 2 +- 15 files changed, 70 insertions(+), 43 deletions(-) diff --git a/fdt.c b/fdt.c index 772da46..3572ef8 100644 --- a/fdt.c +++ b/fdt.c @@ -42,9 +42,9 @@ int _fdt_check_header(const void *fdt) return 0; } -void *fdt_offset_ptr(const void *fdt, int offset, int len) +const void *fdt_offset_ptr(const void *fdt, int offset, int len) { - void *p; + const void *p; if (fdt_version(fdt) >= 0x11) if (((offset + len) < offset) diff --git a/fdt_ro.c b/fdt_ro.c index 9112c6a..28a6d16 100644 --- a/fdt_ro.c +++ b/fdt_ro.c @@ -137,13 +137,13 @@ int fdt_path_offset(const void *fdt, const char *path) return offset; } -struct fdt_property *fdt_get_property(const void *fdt, - int nodeoffset, - const char *name, int *lenp) +const struct fdt_property *fdt_get_property(const void *fdt, + int nodeoffset, + const char *name, int *lenp) { int level = 0; uint32_t tag; - struct fdt_property *prop; + const struct fdt_property *prop; int namestroff; int offset, nextoffset; int err; @@ -216,7 +216,7 @@ struct fdt_property *fdt_get_property(const void *fdt, return NULL; } -void *fdt_getprop(const void *fdt, int nodeoffset, +const void *fdt_getprop(const void *fdt, int nodeoffset, const char *name, int *lenp) { const struct fdt_property *prop; diff --git a/fdt_rw.c b/fdt_rw.c index 7396645..02b36f0 100644 --- a/fdt_rw.c +++ b/fdt_rw.c @@ -123,7 +123,7 @@ static int _resize_property(void *fdt, int nodeoffset, const char *name, int len int oldlen; int err; - *prop = fdt_get_property(fdt, nodeoffset, name, &oldlen); + *prop = fdt_get_property_w(fdt, nodeoffset, name, &oldlen); if (! (*prop)) return oldlen; @@ -153,7 +153,7 @@ static int _add_property(void *fdt, int nodeoffset, const char *name, int len, if (namestroff < 0) return namestroff; - *prop = _fdt_offset_ptr(fdt, nextoffset); + *prop = _fdt_offset_ptr_w(fdt, nextoffset); proplen = sizeof(**prop) + ALIGN(len, FDT_TAGSIZE); err = _blob_splice_struct(fdt, *prop, 0, proplen); @@ -192,7 +192,7 @@ int fdt_delprop(void *fdt, int nodeoffset, const char *name) RW_CHECK_HEADER(fdt); - prop = fdt_get_property(fdt, nodeoffset, name, &len); + prop = fdt_get_property_w(fdt, nodeoffset, name, &len); if (! prop) return len; @@ -225,7 +225,7 @@ int fdt_add_subnode_namelen(void *fdt, int parentoffset, tag = _fdt_next_tag(fdt, offset, &nextoffset); } while (tag == FDT_PROP); - nh = _fdt_offset_ptr(fdt, offset); + nh = _fdt_offset_ptr_w(fdt, offset); nodelen = sizeof(*nh) + ALIGN(namelen+1, FDT_TAGSIZE) + FDT_TAGSIZE; err = _blob_splice_struct(fdt, nh, 0, nodelen); @@ -254,7 +254,7 @@ int fdt_del_node(void *fdt, int nodeoffset) if (endoffset < 0) return endoffset; - return _blob_splice_struct(fdt, _fdt_offset_ptr(fdt, nodeoffset), + return _blob_splice_struct(fdt, _fdt_offset_ptr_w(fdt, nodeoffset), endoffset - nodeoffset, 0); } diff --git a/fdt_sw.c b/fdt_sw.c index 41d4891..dd7a165 100644 --- a/fdt_sw.c +++ b/fdt_sw.c @@ -42,7 +42,7 @@ static void *grab_space(void *fdt, int len) return NULL; fdt_set_header(fdt, size_dt_struct, offset + len); - return fdt_offset_ptr(fdt, offset, len); + return fdt_offset_ptr_w(fdt, offset, len); } int fdt_create(void *buf, int bufsize) @@ -82,7 +82,7 @@ int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size) if ((offset + sizeof(*re)) > fdt_totalsize(fdt)) return -FDT_ERR_NOSPACE; - re = (struct fdt_reserve_entry *)((void *)fdt + offset); + re = (struct fdt_reserve_entry *)(fdt + offset); re->address = cpu_to_fdt64(addr); re->size = cpu_to_fdt64(size); @@ -205,8 +205,8 @@ int fdt_finish(void *fdt) offset = 0; while ((tag = _fdt_next_tag(fdt, offset, &nextoffset)) != FDT_END) { if (tag == FDT_PROP) { - struct fdt_property *prop = fdt_offset_ptr(fdt, offset, - sizeof(*prop)); + struct fdt_property *prop = + fdt_offset_ptr_w(fdt, offset, sizeof(*prop)); int nameoff; if (! prop) diff --git a/fdt_wip.c b/fdt_wip.c index 0db7d25..932a193 100644 --- a/fdt_wip.c +++ b/fdt_wip.c @@ -29,7 +29,7 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, void *propval; int proplen; - propval = fdt_getprop(fdt, nodeoffset, name, &proplen); + propval = fdt_getprop_w(fdt, nodeoffset, name, &proplen); if (! propval) return proplen; @@ -53,7 +53,7 @@ int fdt_nop_property(void *fdt, int nodeoffset, const char *name) struct fdt_property *prop; int len; - prop = fdt_get_property(fdt, nodeoffset, name, &len); + prop = fdt_get_property_w(fdt, nodeoffset, name, &len); if (! prop) return len; @@ -107,6 +107,6 @@ int fdt_nop_node(void *fdt, int nodeoffset) if (endoffset < 0) return endoffset; - nop_region(fdt_offset_ptr(fdt, nodeoffset, 0), endoffset - nodeoffset); + nop_region(fdt_offset_ptr_w(fdt, nodeoffset, 0), endoffset - nodeoffset); return 0; } diff --git a/libfdt.h b/libfdt.h index acdc72e..be75f9a 100644 --- a/libfdt.h +++ b/libfdt.h @@ -45,7 +45,7 @@ #define FDT_ERR_MAX 11 #define fdt_get_header(fdt, field) \ - (fdt32_to_cpu(((struct fdt_header *)(fdt))->field)) + (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field)) #define fdt_magic(fdt) (fdt_get_header(fdt, magic)) #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize)) #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct)) @@ -60,10 +60,17 @@ #define fdt_set_header(fdt, field, val) \ ((struct fdt_header *)(fdt))->field = cpu_to_fdt32(val) -void *fdt_offset_ptr(const void *fdt, int offset, int checklen); +const void *fdt_offset_ptr(const void *fdt, int offset, int checklen); +static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen) +{ + return (void *)fdt_offset_ptr(fdt, offset, checklen); +} + #define fdt_offset_ptr_typed(fdt, offset, var) \ ((typeof(var))(fdt_offset_ptr((fdt), (offset), sizeof(*(var))))) +#define fdt_offset_ptr_typed_w(fdt, offset, var) \ + ((typeof(var))(fdt_offset_ptr_w((fdt), (offset), sizeof(*(var))))) int fdt_move(const void *fdt, void *buf, int bufsize); @@ -76,10 +83,24 @@ int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name); int fdt_path_offset(const void *fdt, const char *path); -struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset, - const char *name, int *lenp); -void *fdt_getprop(const void *fdt, int nodeoffset, - const char *name, int *lenp); +const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset, + const char *name, int *lenp); + +static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset, + const char *name, + int *lenp) +{ + return (struct fdt_property *)fdt_get_property(fdt, nodeoffset, + name, lenp); +} + +const void *fdt_getprop(const void *fdt, int nodeoffset, + const char *name, int *lenp); +static inline void *fdt_getprop_w(void *fdt, int nodeoffset, + const char *name, int *lenp) +{ + return (void *)fdt_getprop(fdt, nodeoffset, name, lenp); +} /* Write-in-place functions */ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, diff --git a/libfdt_internal.h b/libfdt_internal.h index 124bef7..a46af51 100644 --- a/libfdt_internal.h +++ b/libfdt_internal.h @@ -31,9 +31,14 @@ uint32_t _fdt_next_tag(const void *fdt, int startoffset, int *nextoffset); const char *_fdt_find_string(const char *strtab, int tabsize, const char *s); int _fdt_node_end_offset(void *fdt, int nodeoffset); -static inline void *_fdt_offset_ptr(const struct fdt_header *fdt, int offset) +static inline const void *_fdt_offset_ptr(const void *fdt, int offset) { - return (void *)fdt + fdt_off_dt_struct(fdt) + offset; + return fdt + fdt_off_dt_struct(fdt) + offset; +} + +static inline void *_fdt_offset_ptr_w(void *fdt, int offset) +{ + return (void *)_fdt_offset_ptr(fdt, offset); } #define SW_MAGIC (~FDT_MAGIC) diff --git a/tests/del_property.c b/tests/del_property.c index a7928a3..35dc932 100644 --- a/tests/del_property.c +++ b/tests/del_property.c @@ -33,8 +33,8 @@ int main(int argc, char *argv[]) { void *fdt; - uint32_t *intp; - char *strp; + const uint32_t *intp; + const char *strp; int err, lenerr; int oldsize, delsize, newsize; diff --git a/tests/nop_property.c b/tests/nop_property.c index 84e735a..56256c4 100644 --- a/tests/nop_property.c +++ b/tests/nop_property.c @@ -33,8 +33,8 @@ int main(int argc, char *argv[]) { void *fdt; - uint32_t *intp; - char *strp; + const uint32_t *intp; + const char *strp; int err; int lenerr; diff --git a/tests/notfound.c b/tests/notfound.c index 3b18664..a93b605 100644 --- a/tests/notfound.c +++ b/tests/notfound.c @@ -37,11 +37,11 @@ void check_error(const char *s, int err) int main(int argc, char *argv[]) { - struct fdt_property *prop; + const struct fdt_property *prop; void *fdt; int offset; int subnode1_offset; - void *val; + const void *val; int lenerr; test_init(argc, argv); diff --git a/tests/setprop.c b/tests/setprop.c index f58a33d..7f9be3e 100644 --- a/tests/setprop.c +++ b/tests/setprop.c @@ -37,8 +37,8 @@ int main(int argc, char *argv[]) { void *fdt; void *buf; - uint32_t *intp; - char *strp; + const uint32_t *intp; + const char *strp; int err; test_init(argc, argv); diff --git a/tests/setprop_inplace.c b/tests/setprop_inplace.c index bcfd357..59c1209 100644 --- a/tests/setprop_inplace.c +++ b/tests/setprop_inplace.c @@ -33,8 +33,9 @@ int main(int argc, char *argv[]) { void *fdt; - uint32_t *intp; - char *strp, *xstr; + const uint32_t *intp; + const char *strp; + char *xstr; int xlen, i; int err; diff --git a/tests/tests.h b/tests/tests.h index b262e31..ace7ba3 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -117,8 +117,8 @@ void check_property(void *fdt, int nodeoffset, const char *name, }) -void *check_getprop(void *fdt, int nodeoffset, const char *name, - int len, const void *val); +const void *check_getprop(void *fdt, int nodeoffset, const char *name, + int len, const void *val); #define check_getprop_typed(fdt, nodeoffset, name, val) \ ({ \ typeof(val) x = val; \ diff --git a/tests/testutils.c b/tests/testutils.c index efc4f4a..fcb1c88 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -107,10 +107,10 @@ void check_property(void *fdt, int nodeoffset, const char *name, } -void *check_getprop(void *fdt, int nodeoffset, const char *name, - int len, const void *val) +const void *check_getprop(void *fdt, int nodeoffset, const char *name, + int len, const void *val) { - void *propval; + const void *propval; int proplen; propval = fdt_getprop(fdt, nodeoffset, name, &proplen); diff --git a/tests/truncated_property.c b/tests/truncated_property.c index dbd3652..5642d8d 100644 --- a/tests/truncated_property.c +++ b/tests/truncated_property.c @@ -32,7 +32,7 @@ int main(int argc, char *argv[]) { void *fdt = &_truncated_property; - void *prop; + const void *prop; int err; int len;