dm: core: Add a function to create an empty tree

Provide a function to create a new, empty tree.

Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
Simon Glass
2023-09-26 08:14:40 -06:00
committed by Tom Rini
parent 9bf78a5add
commit e0c3c21d8b
5 changed files with 104 additions and 4 deletions

View File

@@ -47,6 +47,17 @@ static int oftree_find(const void *fdt)
return -1;
}
static int check_tree_count(void)
{
if (oftree_count == CONFIG_OFNODE_MULTI_TREE_MAX) {
log_warning("Too many registered device trees (max %d)\n",
CONFIG_OFNODE_MULTI_TREE_MAX);
return -E2BIG;
}
return 0;
}
static oftree oftree_ensure(void *fdt)
{
oftree tree;
@@ -69,11 +80,8 @@ static oftree oftree_ensure(void *fdt)
if (gd->flags & GD_FLG_RELOC) {
i = oftree_find(fdt);
if (i == -1) {
if (oftree_count == CONFIG_OFNODE_MULTI_TREE_MAX) {
log_warning("Too many registered device trees (max %d)\n",
CONFIG_OFNODE_MULTI_TREE_MAX);
if (check_tree_count())
return oftree_null();
}
/* register the new tree */
i = oftree_count++;
@@ -92,6 +100,41 @@ static oftree oftree_ensure(void *fdt)
return tree;
}
int oftree_new(oftree *treep)
{
oftree tree = oftree_null();
int ret;
if (of_live_active()) {
struct device_node *root;
ret = of_live_create_empty(&root);
if (ret)
return log_msg_ret("liv", ret);
tree = oftree_from_np(root);
} else {
const int size = 1024;
void *fdt;
ret = check_tree_count();
if (ret)
return log_msg_ret("fla", ret);
/* register the new tree with a small size */
fdt = malloc(size);
if (!fdt)
return log_msg_ret("fla", -ENOMEM);
ret = fdt_create_empty_tree(fdt, size);
if (ret)
return log_msg_ret("fla", -EINVAL);
oftree_list[oftree_count++] = fdt;
tree.fdt = fdt;
}
*treep = tree;
return 0;
}
void oftree_dispose(oftree tree)
{
if (of_live_active())
@@ -193,6 +236,11 @@ static inline int oftree_find(const void *fdt)
return 0;
}
int oftree_new(oftree *treep)
{
return -ENOSYS;
}
#endif /* OFNODE_MULTI_TREE */
/**