diff options
author | Antonio Niño Díaz <antonio.ninodiaz@arm.com> | 2018-11-06 12:45:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-11-06 12:45:09 +0100 |
commit | 9c10e3485b10ece95f9af8a6af79558397ee1d78 (patch) | |
tree | affcfce48a2f598b9caeb31a61aee959757868d6 /common | |
parent | 4eb835f827cb43cb68473ad4e27f74164dc14c89 (diff) | |
parent | 73f1ac6c8ee4a688ed8e1fddc040b882171d3453 (diff) |
Merge pull request #1665 from antonio-nino-diaz-arm/an/fdt-helpers
Introduce new fdt helpers
Diffstat (limited to 'common')
-rw-r--r-- | common/fdt_wrappers.c | 73 |
1 files changed, 72 insertions, 1 deletions
diff --git a/common/fdt_wrappers.c b/common/fdt_wrappers.c index 1a726a8a..31dafb2e 100644 --- a/common/fdt_wrappers.c +++ b/common/fdt_wrappers.c @@ -10,6 +10,7 @@ #include <debug.h> #include <fdt_wrappers.h> #include <libfdt.h> +#include <string.h> /* * Read cells from a given property of the given node. At most 2 cells of the @@ -39,7 +40,6 @@ int fdtw_read_cells(const void *dtb, int node, const char *prop, return -1; } - /* Verify that property length accords with cell length */ if (NCELLS((unsigned int)value_len) != cells) { WARN("Property length mismatch\n"); @@ -62,6 +62,77 @@ int fdtw_read_cells(const void *dtb, int node, const char *prop, } /* + * Read cells from a given property of the given node. Any number of 32-bit + * cells of the property can be read. The fdt pointer is updated. Returns 0 on + * success, and -1 on error. + */ +int fdtw_read_array(const void *dtb, int node, const char *prop, + unsigned int cells, void *value) +{ + const uint32_t *value_ptr; + int value_len; + + assert(dtb != NULL); + assert(prop != NULL); + assert(value != NULL); + assert(node >= 0); + + /* Access property and obtain its length (in bytes) */ + value_ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), + &value_len); + if (value_ptr == NULL) { + WARN("Couldn't find property %s in dtb\n", prop); + return -1; + } + + /* Verify that property length accords with cell length */ + if (NCELLS((unsigned int)value_len) != cells) { + WARN("Property length mismatch\n"); + return -1; + } + + uint32_t *dst = value; + + for (unsigned int i = 0U; i < cells; i++) { + dst[i] = fdt32_to_cpu(value_ptr[i]); + } + + return 0; +} + +/* + * Read string from a given property of the given node. Up to 'size - 1' + * characters are read, and a NUL terminator is added. Returns 0 on success, + * and -1 upon error. + */ +int fdtw_read_string(const void *dtb, int node, const char *prop, + char *str, size_t size) +{ + const char *ptr; + size_t len; + + assert(dtb != NULL); + assert(node >= 0); + assert(prop != NULL); + assert(str != NULL); + assert(size > 0U); + + ptr = fdt_getprop_namelen(dtb, node, prop, (int)strlen(prop), NULL); + if (ptr == NULL) { + WARN("Couldn't find property %s in dtb\n", prop); + return -1; + } + + len = strlcpy(str, ptr, size); + if (len >= size) { + WARN("String of property %s in dtb has been truncated\n", prop); + return -1; + } + + return 0; +} + +/* * Write cells in place to a given property of the given node. At most 2 cells * of the property are written. Returns 0 on success, and -1 upon error. */ |