Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 9AD4EEB64DA for ; Wed, 28 Jun 2023 11:44:13 +0000 (UTC) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 635EF8663C; Wed, 28 Jun 2023 13:43:59 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (1024-bit key; unprotected) header.d=chromium.org header.i=@chromium.org header.b="aV4A0Cov"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 7166786634; Wed, 28 Jun 2023 13:42:49 +0200 (CEST) Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id EEB9C8663C for ; Wed, 28 Jun 2023 13:42:27 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=pass (p=none dis=none) header.from=chromium.org Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=sjg@chromium.org Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-51d7e8dd118so6086864a12.1 for ; Wed, 28 Jun 2023 04:42:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; t=1687952547; x=1690544547; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/2RO971azqzVFvHdu4uKyTqJP9ly1LtX8unAYub2R2E=; b=aV4A0CovzqzGlYd8eV26qccM0IfvkbA2GDLmAf9lLduHQPl2lHEHURoLSDbh07RTLu aE7hmMFQE3VL8vNWCKvWwr7v6N0HJCw3YVpVp6KXZmOeSZIRlgffQDDQ1+yd0BDdwOLQ 189qyk3EIIbpFr/csN7xCHTGNrzGA3wGxhPwg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1687952547; x=1690544547; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/2RO971azqzVFvHdu4uKyTqJP9ly1LtX8unAYub2R2E=; b=R+GV3lnS0/UO+ISFOe300QJlyMiecfEy5bF1QPSb2RHeGygR7oL1/VnDtHa7ANwOsV X9jaNNTe35BCtcxCYhyRR5Yg0i1R3M9KPp7AdNJOfF9ZclUJgJJwNfyJvAeKh2hlTrpy lC6Hsjho2xLMztbD3Bjv0qW3zI0KUKhyjv6d1XB/lk5zaS2HVQdG1gwXXTQ7VnHUptwt qDGw6fHgSWmWDaddKlnJVy9TX/l0b0EE+qqO+DWJ97TTFSz9JZ64vtVF+YhdkE+KFX4I ddUoJrxlCc0IJbRkSJJwnsWzR2DBSBgwBQJWYQpw+YMtsoJtG2Vt8wj1CRqD6tHy4fgj QUcg== X-Gm-Message-State: AC+VfDwiQCOlsM/m8uLBktVbGoBFnzli806BTMNzBjnOcUNAKdsw4+x6 S+US4m1MDWnhlEVfGZKmqdqLgNByM5tPceAkuX4PYA== X-Google-Smtp-Source: ACHHUZ6Rr7Mf7ffeShfjhFXn+SyJBOhhZyb3oeDYqTClFkkHe3jAw5lUZteL2P2jXCCYC0S2CKwxBg== X-Received: by 2002:aa7:c38a:0:b0:51d:9daa:c337 with SMTP id k10-20020aa7c38a000000b0051d9daac337mr6237310edq.0.1687952547305; Wed, 28 Jun 2023 04:42:27 -0700 (PDT) Received: from sjg1.praguecc.cz ([193.85.242.128]) by smtp.gmail.com with ESMTPSA id r18-20020aa7cb92000000b0051bec856cb4sm4676423edt.50.2023.06.28.04.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jun 2023 04:42:27 -0700 (PDT) From: Simon Glass To: U-Boot Mailing List Cc: Jan Kiszka , Simon Glass , Heinrich Schuchardt Subject: [PATCH 10/12] dtoc: Support copying the contents of a node into another Date: Wed, 28 Jun 2023 12:41:43 +0100 Message-ID: <20230628114149.439723-11-sjg@chromium.org> X-Mailer: git-send-email 2.41.0.162.gfafddb0af9-goog In-Reply-To: <20230628114149.439723-1-sjg@chromium.org> References: <20230628114149.439723-1-sjg@chromium.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.39 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.103.8 at phobos.denx.de X-Virus-Status: Clean This permits implementation of a simple templating system, where a node can be reused as a base for others. For now this adds new subnodes after any existing ones. Signed-off-by: Simon Glass --- tools/dtoc/fdt.py | 22 ++++++++++++++ tools/dtoc/test/dtoc_test_simple.dts | 3 ++ tools/dtoc/test_fdt.py | 43 ++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/tools/dtoc/fdt.py b/tools/dtoc/fdt.py index a8e05349a720..fcf229f83036 100644 --- a/tools/dtoc/fdt.py +++ b/tools/dtoc/fdt.py @@ -13,6 +13,7 @@ from dtoc import fdt_util import libfdt from libfdt import QUIET_NOTFOUND from u_boot_pylib import tools +from u_boot_pylib import tout # This deals with a device tree, presenting it as an assortment of Node and # Prop objects, representing nodes and properties, respectively. This file @@ -635,6 +636,27 @@ class Node: prop.Sync(auto_resize) return added + def copy_node(self, src): + """Copy a node and all its subnodes into this node + + Args: + src (Node): Node to copy + + This works recursively. + + The new node is put after all other nodes. If the node already + exists, just its properties are copied. Properties which exist in the + destination node already are not copied. + """ + dst = self.FindNode(src.name) + if not dst: + dst = self.AddSubnode(src.name) + for name, src_prop in src.props.items(): + if name not in dst.props: + dst.props[name] = Prop(dst, None, name, src_prop.bytes) + for node in src.subnodes: + dst.copy_node(node) + class Fdt: """Provides simple access to a flat device tree blob using libfdts. diff --git a/tools/dtoc/test/dtoc_test_simple.dts b/tools/dtoc/test/dtoc_test_simple.dts index 08f667ee5a10..c51f1a5908ce 100644 --- a/tools/dtoc/test/dtoc_test_simple.dts +++ b/tools/dtoc/test/dtoc_test_simple.dts @@ -45,6 +45,9 @@ stringarray = "one"; longbytearray = [09 0a 0b 0c 0d 0e 0f 10]; maybe-empty-int = <1>; + + first-node { + }; }; i2c@0 { diff --git a/tools/dtoc/test_fdt.py b/tools/dtoc/test_fdt.py index 4fe8d12c403a..5d9d99eb384b 100755 --- a/tools/dtoc/test_fdt.py +++ b/tools/dtoc/test_fdt.py @@ -306,6 +306,49 @@ class TestNode(unittest.TestCase): self.assertIn("Internal error, node '/spl-test' name mismatch 'i2c@0'", str(exc.exception)) + def test_copy_node(self): + """Test copy_node() function""" + tmpl = self.dtb.GetNode('/i2c@0') + dst = self.dtb.GetNode('/spl-test3') + dst.copy_node(tmpl) + + self.assertEqual(['/spl-test3/first-node', '/spl-test3/i2c@0'], + [n.path for n in dst.subnodes]) + + chk = self.dtb.GetNode('/spl-test3/i2c@0') + self.assertTrue(chk) + self.assertEqual( + {'bootph-all', 'compatible', '#address-cells', '#size-cells'}, + chk.props.keys()) + + # Check the first property + prop = chk.props['bootph-all'] + self.assertEqual('bootph-all', prop.name) + self.assertEqual(True, prop.value) + self.assertIsNone(prop._offset) + self.assertEqual(chk.path, prop._node.path) + + # Check the second property + prop = chk.props['compatible'] + self.assertEqual('compatible', prop.name) + self.assertEqual('sandbox,i2c', prop.value) + self.assertIsNone(prop._offset) + self.assertEqual(chk.path, prop._node.path) + + pmic = chk.FindNode('pmic@9') + self.assertTrue(chk) + + pmic = self.dtb.GetNode('/spl-test3/i2c@0/pmic@9') + self.assertTrue(pmic) + self.assertEqual([pmic], chk.subnodes) + self.assertEqual(chk, pmic.parent) + self.assertIsNone(pmic._offset) + self.assertEqual( + {'bootph-all', 'compatible', 'reg', 'low-power'}, + pmic.props.keys()) + + self.dtb.Sync(auto_resize=True) + class TestProp(unittest.TestCase): """Test operation of the Prop class""" -- 2.41.0.162.gfafddb0af9-goog