fdt/wrappers: Generalise fdtw_read_array()

Currently our fdtw_read_array() implementation requires the length of
the property to exactly match the requested size, which makes it less
flexible for parsing generic device trees.
Also the name is slightly misleading, since we treat the cells of the
array as 32 bit unsigned integers, performing the endianess conversion.

To fix those issues and align the code more with other DT users (Linux
kernel or U-Boot), rename the function to "fdt_read_uint32_array", and
relax the length check to only check if the property covers at least the
number of cells we request.
This also changes the variable names to be more in-line with other DT
users, and switches to the proper data types.

This makes this function more useful in later patches.

Change-Id: Id86f4f588ffcb5106d4476763ecdfe35a735fa6c
Signed-off-by: Andre Przywara <andre.przywara@arm.com>
This commit is contained in:
Andre Przywara 2020-03-30 23:21:13 +01:00
parent 455a6f3b14
commit 6e3a89f449
7 changed files with 24 additions and 24 deletions

View File

@ -65,38 +65,35 @@ 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.
* cells of the property can be read. Returns 0 on success, or a negative
* FDT error value otherwise.
*/
int fdtw_read_array(const void *dtb, int node, const char *prop,
unsigned int cells, void *value)
int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
unsigned int cells, uint32_t *value)
{
const uint32_t *value_ptr;
const fdt32_t *prop;
int value_len;
assert(dtb != NULL);
assert(prop != NULL);
assert(prop_name != 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;
prop = fdt_getprop(dtb, node, prop_name, &value_len);
if (prop == NULL) {
WARN("Couldn't find property %s in dtb\n", prop_name);
return -FDT_ERR_NOTFOUND;
}
/* Verify that property length accords with cell length */
if (NCELLS((unsigned int)value_len) != cells) {
/* Verify that property length can fill the entire array. */
if (NCELLS((unsigned int)value_len) < cells) {
WARN("Property length mismatch\n");
return -1;
return -FDT_ERR_BADVALUE;
}
uint32_t *dst = value;
for (unsigned int i = 0U; i < cells; i++) {
dst[i] = fdt32_to_cpu(value_ptr[i]);
value[i] = fdt32_to_cpu(prop[i]);
}
return 0;

View File

@ -14,8 +14,8 @@
int fdtw_read_cells(const void *dtb, int node, const char *prop,
unsigned int cells, void *value);
int fdtw_read_array(const void *dtb, int node, const char *prop,
unsigned int cells, void *value);
int fdt_read_uint32_array(const void *dtb, int node, const char *prop_name,
unsigned int cells, uint32_t *value);
int fdtw_read_string(const void *dtb, int node, const char *prop,
char *str, size_t size);
int fdtw_write_inplace_cells(void *dtb, int node, const char *prop,

View File

@ -18,7 +18,7 @@ int fconf_populate_gicv3_config(uintptr_t config)
{
int err;
int node;
int addr[20];
uint32_t addr[20];
/* Necessary to work with libfdt APIs */
const void *hw_config_dtb = (const void *)config;
@ -42,7 +42,7 @@ int fconf_populate_gicv3_config(uintptr_t config)
<0x0 0x2c02f000 0 0x2000>; // GICV
*/
err = fdtw_read_array(hw_config_dtb, node, "reg", 20, &addr);
err = fdt_read_uint32_array(hw_config_dtb, node, "reg", 20, addr);
if (err < 0) {
ERROR("FCONF: Failed to read reg property of GIC node\n");
}

View File

@ -15,6 +15,7 @@
# fdt fdt_getprop_namelen patch
rom rom_lib_init
fdt fdt_getprop
fdt fdt_getprop_namelen
fdt fdt_setprop_inplace
fdt fdt_check_header

View File

@ -15,6 +15,7 @@
# fdt fdt_getprop_namelen patch
rom rom_lib_init
fdt fdt_getprop
fdt fdt_getprop_namelen
fdt fdt_setprop_inplace
fdt fdt_check_header

View File

@ -241,7 +241,8 @@ int fconf_populate_arm_io_policies(uintptr_t config)
/* Locate the uuid cells and read the value for all the load info uuid */
for (i = 0; i < FCONF_ARM_IO_UUID_NUMBER; i++) {
uuid_ptr = pool_alloc(&fconf_arm_uuids_pool);
err = fdtw_read_array(dtb, node, load_info[i].name, 4, &uuid_helper.word);
err = fdt_read_uint32_array(dtb, node, load_info[i].name,
4, uuid_helper.word);
if (err < 0) {
WARN("FCONF: Read cell failed for %s\n", load_info[i].name);
return err;

View File

@ -44,8 +44,8 @@ int fconf_populate_arm_sp(uintptr_t config)
}
fdt_for_each_subnode(sp_node, dtb, node) {
err = fdtw_read_array(dtb, sp_node, "uuid", 4,
&uuid_helper.word);
err = fdt_read_uint32_array(dtb, sp_node, "uuid", 4,
uuid_helper.word);
if (err < 0) {
ERROR("FCONF: cannot read SP uuid\n");
return -1;