libfdt: overlay: ensure that existing phandles are not overwritten

A phandle in an overlay is not supposed to overwrite a phandle that
already exists in the base dtb as this breaks references to the
respective node in the base.

So add another iteration over the fdto that checks for such overwrites
and fixes the fdto phandle's value to match the fdt's.

A test is added that checks that newly added phandles and existing
phandles work as expected.

Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Message-ID: <20240225175422.156393-2-u.kleine-koenig@pengutronix.de>
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
This commit is contained in:
Uwe Kleine-König 2024-02-25 18:54:23 +01:00 committed by David Gibson
parent b0aacd0a77
commit 1fad065080
4 changed files with 334 additions and 0 deletions

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2024 Uwe Kleine-König
*
* SPDX-License-Identifier: GPL-2.0+
*/
/dts-v1/;
/ {
node_a: a {
value = <17>;
};
node_b: b {
a = <&node_a>;
};
node_d: d {
b = <&node_b>;
};
};

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Uwe Kleine-König
*
* SPDX-License-Identifier: GPL-2.0+
*/
/dts-v1/;
/plugin/;
&{/} {
/*
* /b already has a label "node_b" in the base dts, also b is already
* referenced in the base, so both the base's b and this b have a
* phandle value.
*/
node_b2: b {
value = <42>;
d = <&node_d>;
};
node_c: c {
value = <23>;
b = <&node_b2>;
};
d {
a = <&node_a>;
c = <&node_c>;
};
};
&node_a {
value = <32>;
};

View file

@ -1040,6 +1040,34 @@ fdtoverlay_tests() {
run_dtc_test -@ -I dts -O dtb -o $stacked_addlabeldtb $stacked_addlabel
run_fdtoverlay_test baz "/foonode/barnode/baznode" "baz-property" "-ts" ${stacked_base_nolabeldtb} ${stacked_addlabel_targetdtb} ${stacked_addlabeldtb} ${stacked_bardtb} ${stacked_bazdtb}
# verify that phandles are not overwritten
run_dtc_test -@ -I dts -O dtb -o overlay_base_phandle.test.dtb "$SRCDIR/overlay_base_phandle.dts"
run_dtc_test -@ -I dts -O dtb -o overlay_overlay_phandle.test.dtb "$SRCDIR/overlay_overlay_phandle.dts"
run_wrap_test $FDTOVERLAY -i overlay_base_phandle.test.dtb -o overlay_base_phandleO.test.dtb overlay_overlay_phandle.test.dtb
pa=$($DTGET overlay_base_phandleO.test.dtb /a phandle)
pb=$($DTGET overlay_base_phandleO.test.dtb /b phandle)
pc=$($DTGET overlay_base_phandleO.test.dtb /c phandle)
pd=$($DTGET overlay_base_phandleO.test.dtb /d phandle)
ba=$($DTGET overlay_base_phandleO.test.dtb /b a)
bd=$($DTGET overlay_base_phandleO.test.dtb /b d)
cb=$($DTGET overlay_base_phandleO.test.dtb /c b)
da=$($DTGET overlay_base_phandleO.test.dtb /d a)
db=$($DTGET overlay_base_phandleO.test.dtb /d b)
dc=$($DTGET overlay_base_phandleO.test.dtb /d c)
shorten_echo "check phandle to noda a wasn't overwritten: "
run_wrap_test test "$ba-$da" = "$pa-$pa"
shorten_echo "check phandle to noda b wasn't overwritten: "
run_wrap_test test "$cb-$db" = "$pb-$pb"
shorten_echo "check phandle to noda c wasn't overwritten: "
run_wrap_test test "$dc" = "$pc"
shorten_echo "check phandle to noda d wasn't overwritten: "
run_wrap_test test "$bd" = "$pd"
}
pylibfdt_tests () {