From 4231c48fa1249671a3085fbc448768597143c4e0 Mon Sep 17 00:00:00 2001 From: Nathan Whitehorn Date: Tue, 22 Oct 2013 21:20:05 +0000 Subject: [PATCH] A few other common cases for encode-int decoding: OF_getencprop_alloc() and OF_searchencprop(). I thought about using the element size parameter to OF_getprop_alloc() to do endian-switching automatically, but it breaks use with structs and a *lot* of FDT code (which can hopefully be moved to these new APIs). MFC after: 2 weeks --- sys/dev/ofw/openfirm.c | 31 +++++++++++++++++++++++++++++++ sys/dev/ofw/openfirm.h | 4 ++++ 2 files changed, 35 insertions(+) diff --git a/sys/dev/ofw/openfirm.c b/sys/dev/ofw/openfirm.c index 95b78c596300..49dc05b0a892 100644 --- a/sys/dev/ofw/openfirm.c +++ b/sys/dev/ofw/openfirm.c @@ -312,6 +312,17 @@ OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len) return (-1); } +ssize_t +OF_searchencprop(phandle_t node, const char *propname, void *buf, size_t len) +{ + ssize_t rv; + + for (; node != 0; node = OF_parent(node)) + if ((rv = OF_getencprop(node, propname, buf, len)) != -1) + return (rv); + return (-1); +} + /* * Store the value of a property of a package into newly allocated memory * (using the M_OFWPROP malloc pool and M_WAITOK). elsz is the size of a @@ -336,6 +347,26 @@ OF_getprop_alloc(phandle_t package, const char *propname, int elsz, void **buf) return (len / elsz); } +ssize_t +OF_getencprop_alloc(phandle_t package, const char *name, int elsz, void **buf) +{ + ssize_t retval; + pcell_t *cell; + int i; + + KASSERT(elsz % 4 == 0, "Need a multiple of 4 bytes"); + + retval = OF_getprop_alloc(package, name, elsz, buf); + if (retval == -1) + return (retval); + + cell = *buf; + for (i = 0; i < retval*elsz/4; i++) + cell[i] = be32toh(cell[i]); + + return (retval); +} + /* Get the next property of a package. */ int OF_nextprop(phandle_t package, const char *previous, char *buf, size_t size) diff --git a/sys/dev/ofw/openfirm.h b/sys/dev/ofw/openfirm.h index 1ffface2ef63..5ac08fe12897 100644 --- a/sys/dev/ofw/openfirm.h +++ b/sys/dev/ofw/openfirm.h @@ -110,8 +110,12 @@ ssize_t OF_getencprop(phandle_t node, const char *prop, pcell_t *buf, int OF_hasprop(phandle_t node, const char *propname); ssize_t OF_searchprop(phandle_t node, const char *propname, void *buf, size_t len); +ssize_t OF_searchencprop(phandle_t node, const char *propname, + void *buf, size_t len); ssize_t OF_getprop_alloc(phandle_t node, const char *propname, int elsz, void **buf); +ssize_t OF_getencprop_alloc(phandle_t node, const char *propname, + int elsz, void **buf); int OF_nextprop(phandle_t node, const char *propname, char *buf, size_t len); int OF_setprop(phandle_t node, const char *name, const void *buf,