dm: core: Add ofnode_read_prop()

Add a new function to read a property that supports reading the length as
well.

Reimplement ofnode_read_string() using it and fix its comment.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2020-01-27 08:49:44 -07:00
parent 14ca9f7f5a
commit a8167d8ee2
3 changed files with 62 additions and 8 deletions

View File

@@ -101,30 +101,47 @@ bool ofnode_read_bool(ofnode node, const char *propname)
return prop ? true : false;
}
const char *ofnode_read_string(ofnode node, const char *propname)
const void *ofnode_read_prop(ofnode node, const char *propname, int *sizep)
{
const char *str = NULL;
int len = -1;
const char *val = NULL;
int len;
assert(ofnode_valid(node));
debug("%s: %s: ", __func__, propname);
if (ofnode_is_np(node)) {
struct property *prop = of_find_property(
ofnode_to_np(node), propname, NULL);
ofnode_to_np(node), propname, &len);
if (prop) {
str = prop->value;
val = prop->value;
len = prop->length;
}
} else {
str = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
val = fdt_getprop(gd->fdt_blob, ofnode_to_offset(node),
propname, &len);
}
if (!str) {
if (!val) {
debug("<not found>\n");
if (sizep)
*sizep = -FDT_ERR_NOTFOUND;
return NULL;
}
if (sizep)
*sizep = len;
return val;
}
const char *ofnode_read_string(ofnode node, const char *propname)
{
const char *str;
int len;
str = ofnode_read_prop(node, propname, &len);
if (!str)
return NULL;
if (strnlen(str, len) >= len) {
debug("<invalid>\n");
return NULL;