raspberrypi: dtc: Prevent overlays from modifying phandle properties
[AGL/meta-agl.git] / meta-agl-bsp / meta-raspberrypi / recipes-kernel / dtc / files / 0001-fdtoverlay-Prevent-overlays-from-modifying-phandle-p.patch
1 From caba0117dc30f2357eac6d04f3510095dcbaa7f4 Mon Sep 17 00:00:00 2001
2 From: Paul Barker <pbarker@konsulko.com>
3 Date: Fri, 18 Dec 2020 23:00:07 +0000
4 Subject: [PATCH] fdtoverlay: Prevent overlays from modifying phandle
5  properties
6 To: David Gibson <david@gibson.dropbear.id.au>,
7     Jon Loeliger <jdl@jdl.com>,
8     devicetree-compiler@vger.kernel.org
9 Cc: Rob Herring <robh@kernel.org>,
10     Pantelis Antoniou <pantelis.antoniou@konsulko.com>,
11     Scott Murray <scott.murray@konsulko.com>,
12     Jan Simon Moeller <jsmoeller@linuxfoundation.org>
13
14 When applying an overlay fragment, we should take care not to overwrite
15 an existing phandle property of the target node as this could break
16 references to the target node elsewhere in the base dtb.
17
18 In addition to potentially breaking references within the resulting fdt,
19 if the overlay is built with symbols enabled (`-@` option to dtc) then
20 fdtoverlay will be unable to merge the overlay with a base dtb file.
21
22 A new test case is added to check how fdtoverlay handles this case.
23 Attempting to apply this test overlay without the fix in this patch
24 results in the following output:
25
26     input  = tests/overlay_base_ref.test.dtb
27     output = tests/overlay_overlay_ref.fdtoverlay.dtb
28     overlay[0] = tests/overlay_overlay_ref.test.dtb
29
30     Failed to apply 'tests/overlay_overlay_ref.test.dtb': FDT_ERR_NOTFOUND
31
32 In this test case the __overlay__ node in question does not explicitly
33 contain a phandle property in the dts file, the phandle is added during
34 compilation as it is referenced by another node within the overlay dts.
35
36 This failure occurs due to a sequence of events in the functions called
37 by fdt_overlay_apply():
38
39 1) In overlay_fixup_phandles(), the target of the overlay fragment is
40    looked up and the target property is set to the phandle of the target
41    node.
42
43 2) In overlay_merge(), the target node is looked up by phandle via
44    overlay_get_target(). As the __overlay__ node in this test case
45    itself has a phandle property, the phandle of the target node is
46    modified.
47
48 3) In overlay_symbol_update(), the target node is again looked up by
49    phandle via overlay_get_target(). But this time the target node
50    cannot be found as its phandle property was modified.
51
52 The fix for this issue is to skip modification of the phandle property
53 of the target node in step (2) of the above sequence. If the target node
54 doesn't already contain a phandle property, we can add one without risk.
55
56 Upstream-Status: Submitted
57     https://www.spinics.net/lists/devicetree-compiler/msg03537.html
58 Signed-off-by: Paul Barker <pbarker@konsulko.com>
59 ---
60  libfdt/fdt_overlay.c          |  2 ++
61  tests/overlay_base_ref.dts    | 19 +++++++++++++++++++
62  tests/overlay_overlay_ref.dts | 24 ++++++++++++++++++++++++
63  tests/run_tests.sh            |  5 +++++
64  4 files changed, 50 insertions(+)
65  create mode 100644 tests/overlay_base_ref.dts
66  create mode 100644 tests/overlay_overlay_ref.dts
67
68 diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
69 index d217e79..b3c217a 100644
70 --- a/libfdt/fdt_overlay.c
71 +++ b/libfdt/fdt_overlay.c
72 @@ -573,6 +573,8 @@ static int overlay_apply_node(void *fdt, int target,
73                 if (prop_len < 0)
74                         return prop_len;
75  
76 +               if (!strcmp(name, "phandle") && fdt_getprop(fdt, target, name, NULL))
77 +                       continue;
78                 ret = fdt_setprop(fdt, target, name, prop, prop_len);
79                 if (ret)
80                         return ret;
81 diff --git a/tests/overlay_base_ref.dts b/tests/overlay_base_ref.dts
82 new file mode 100644
83 index 0000000..1fc02a2
84 --- /dev/null
85 +++ b/tests/overlay_base_ref.dts
86 @@ -0,0 +1,19 @@
87 +/*
88 + * Copyright (c) 2016 NextThing Co
89 + * Copyright (c) 2016 Free Electrons
90 + * Copyright (c) 2016 Konsulko Inc.
91 + *
92 + * SPDX-License-Identifier:    GPL-2.0+
93 + */
94 +
95 +/dts-v1/;
96 +
97 +/ {
98 +       test: test-node {
99 +               test-int-property = <42>;
100 +       };
101 +
102 +       test-refs {
103 +               refs = <&test>;
104 +       };
105 +};
106 diff --git a/tests/overlay_overlay_ref.dts b/tests/overlay_overlay_ref.dts
107 new file mode 100644
108 index 0000000..a45c95d
109 --- /dev/null
110 +++ b/tests/overlay_overlay_ref.dts
111 @@ -0,0 +1,24 @@
112 +/*
113 + * Copyright (c) 2016 NextThing Co
114 + * Copyright (c) 2016 Free Electrons
115 + * Copyright (c) 2016 Konsulko Inc.
116 + *
117 + * SPDX-License-Identifier:    GPL-2.0+
118 + */
119 +
120 +/dts-v1/;
121 +/plugin/;
122 +
123 +/ {
124 +       fragment@0 {
125 +               target = <&test>;
126 +
127 +               frag0: __overlay__ {
128 +                       test-int-property = <43>;
129 +               };
130 +       };
131 +
132 +    test-ref {
133 +        ref = <&frag0>;
134 +    };
135 +};
136 diff --git a/tests/run_tests.sh b/tests/run_tests.sh
137 index 294585b..a65b166 100755
138 --- a/tests/run_tests.sh
139 +++ b/tests/run_tests.sh
140 @@ -329,6 +329,11 @@ dtc_overlay_tests () {
141      run_test check_path overlay_base_with_aliases.dtb not-exists "/__symbols__"
142      run_test check_path overlay_base_with_aliases.dtb not-exists "/__fixups__"
143      run_test check_path overlay_base_with_aliases.dtb not-exists "/__local_fixups__"
144 +
145 +    # Test taking a reference to an overlay fragment
146 +    run_dtc_test -@ -I dts -O dtb -o overlay_base_ref.test.dtb "$SRCDIR/overlay_base_ref.dts"
147 +    run_dtc_test -@ -I dts -O dtb -o overlay_overlay_ref.test.dtb "$SRCDIR/overlay_overlay_ref.dts"
148 +    run_wrap_test $FDTOVERLAY -i overlay_base_ref.test.dtb overlay_overlay_ref.test.dtb -o overlay_overlay_ref.fdtoverlay.dtb
149  }
150  
151  tree1_tests () {
152 -- 
153 2.26.2
154