From patchwork Fri Jul 24 06:50:57 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jeremy Kerr X-Patchwork-Id: 499610 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5A7A5140280 for ; Fri, 24 Jul 2015 16:51:42 +1000 (AEST) Received: from lists.ozlabs.org (lists.ozlabs.org [IPv6:2401:3900:2:1::3]) by lists.ozlabs.org (Postfix) with ESMTP id 40CF11A0E9E for ; Fri, 24 Jul 2015 16:51:42 +1000 (AEST) X-Original-To: skiboot@lists.ozlabs.org Delivered-To: skiboot@lists.ozlabs.org Received: from ozlabs.org (ozlabs.org [IPv6:2401:3900:2:1::2]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by lists.ozlabs.org (Postfix) with ESMTPS id EBEEC1A0743 for ; Fri, 24 Jul 2015 16:51:01 +1000 (AEST) Received: by ozlabs.org (Postfix, from userid 1023) id D3EE11402A7; Fri, 24 Jul 2015 16:51:01 +1000 (AEST) MIME-Version: 1.0 Message-Id: <1437720657.166072.261745132728.2.gpush@pablo> In-Reply-To: <1437720657.165281.93625520876.0.gpush@pablo> To: skiboot@lists.ozlabs.org From: Jeremy Kerr Date: Fri, 24 Jul 2015 14:50:57 +0800 Subject: [Skiboot] [PATCH 2/7 v2] core: Add dt_copy X-BeenThere: skiboot@lists.ozlabs.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: Mailing list for skiboot development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: skiboot-bounces+incoming=patchwork.ozlabs.org@lists.ozlabs.org Sender: "Skiboot" This change adds a new function to copy a device tree node to a new parent. Signed-off-by: Jeremy Kerr --- core/device.c | 38 ++++++++++++++++++++++++++++++++++++++ include/device.h | 3 +++ 2 files changed, 41 insertions(+) diff --git a/core/device.c b/core/device.c index 807764c..483baec 100644 --- a/core/device.c +++ b/core/device.c @@ -152,6 +152,44 @@ struct dt_node *dt_new_2addr(struct dt_node *parent, const char *name, return new; } +static struct dt_node *__dt_copy(struct dt_node *node, struct dt_node *parent, + bool root) +{ + struct dt_property *prop, *new_prop; + struct dt_node *new_node, *child; + + new_node = dt_new(parent, node->name); + if (!new_node) + return NULL; + + list_for_each(&node->properties, prop, list) { + new_prop = dt_add_property(new_node, prop->name, prop->prop, + prop->len); + if (!new_prop) + goto fail; + } + + list_for_each(&node->children, child, list) { + child = __dt_copy(child, new_node, false); + if (!child) + goto fail; + } + + return new_node; + +fail: + /* dt_free will recurse for us, so only free when we unwind to the + * top-level failure */ + if (root) + dt_free(new_node); + return NULL; +} + +struct dt_node *dt_copy(struct dt_node *node, struct dt_node *parent) +{ + return __dt_copy(node, parent, true); +} + char *dt_get_path(const struct dt_node *node) { unsigned int len = 0; diff --git a/include/device.h b/include/device.h index b301958..a0fc280 100644 --- a/include/device.h +++ b/include/device.h @@ -68,6 +68,9 @@ struct dt_node *dt_new_addr(struct dt_node *parent, const char *name, struct dt_node *dt_new_2addr(struct dt_node *parent, const char *name, uint64_t unit_addr0, uint64_t unit_addr1); +/* Copy node to new parent, including properties and subnodes */ +struct dt_node *dt_copy(struct dt_node *node, struct dt_node *parent); + /* Add a property node, various forms. */ struct dt_property *dt_add_property(struct dt_node *node, const char *name,