Browse Source

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 <david@gibson.dropbear.id.au>
main
David Gibson 18 years ago
parent
commit
a6c76f923d
  1. 4
      fdt.c
  2. 10
      fdt_ro.c
  3. 10
      fdt_rw.c
  4. 8
      fdt_sw.c
  5. 6
      fdt_wip.c
  6. 33
      libfdt.h
  7. 9
      libfdt_internal.h
  8. 4
      tests/del_property.c
  9. 4
      tests/nop_property.c
  10. 4
      tests/notfound.c
  11. 4
      tests/setprop.c
  12. 5
      tests/setprop_inplace.c
  13. 4
      tests/tests.h
  14. 6
      tests/testutils.c
  15. 2
      tests/truncated_property.c

4
fdt.c

@ -42,9 +42,9 @@ int _fdt_check_header(const void *fdt) @@ -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)

10
fdt_ro.c

@ -137,13 +137,13 @@ int fdt_path_offset(const void *fdt, const char *path) @@ -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, @@ -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;

10
fdt_rw.c

@ -123,7 +123,7 @@ static int _resize_property(void *fdt, int nodeoffset, const char *name, int len @@ -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, @@ -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) @@ -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, @@ -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) @@ -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);
}


8
fdt_sw.c

@ -42,7 +42,7 @@ static void *grab_space(void *fdt, int len) @@ -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) @@ -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) @@ -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)

6
fdt_wip.c

@ -29,7 +29,7 @@ int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name, @@ -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) @@ -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) @@ -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;
}

33
libfdt.h

@ -45,7 +45,7 @@ @@ -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 @@ @@ -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); @@ -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,

9
libfdt_internal.h

@ -31,9 +31,14 @@ uint32_t _fdt_next_tag(const void *fdt, int startoffset, int *nextoffset); @@ -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)

4
tests/del_property.c

@ -33,8 +33,8 @@ @@ -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;


4
tests/nop_property.c

@ -33,8 +33,8 @@ @@ -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;


4
tests/notfound.c

@ -37,11 +37,11 @@ void check_error(const char *s, int err) @@ -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);

4
tests/setprop.c

@ -37,8 +37,8 @@ int main(int argc, char *argv[]) @@ -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);

5
tests/setprop_inplace.c

@ -33,8 +33,9 @@ @@ -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;


4
tests/tests.h

@ -117,8 +117,8 @@ void check_property(void *fdt, int nodeoffset, const char *name, @@ -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; \

6
tests/testutils.c

@ -107,10 +107,10 @@ void check_property(void *fdt, int nodeoffset, const char *name, @@ -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);

2
tests/truncated_property.c

@ -32,7 +32,7 @@ @@ -32,7 +32,7 @@
int main(int argc, char *argv[])
{
void *fdt = &_truncated_property;
void *prop;
const void *prop;
int err;
int len;


Loading…
Cancel
Save